-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
496 lines (449 loc) · 14.7 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
const { Signature, RSAKey, KEYUTIL } = require("jsrsasign")
const crypto = require("crypto")
const { Ed25519KeyIdentity } = require("@dfinity/identity")
const { Wallet } = require("ethers")
const Arweave = require("arweave")
const fs = require("fs")
const path = require("path")
const { expect } = require("chai")
const { isNil, range, pick } = require("ramda")
const ethSigUtil = require("@metamask/eth-sig-util")
const { init, stop, initBeforeEach, addFunds } = require("./util")
const { Buffer } = require("buffer")
describe("WeaveDB", function () {
let wallet, walletAddress, wallet2, db
const _ii = [
"302a300506032b6570032100ccd1d1f725fc35a681d8ef5d563a3c347829bf3f0fe822b4a4b004ee0224fc0d",
"010925abb4cf8ccb7accbcfcbf0a6adf1bbdca12644694bb47afc7182a4ade66ccd1d1f725fc35a681d8ef5d563a3c347829bf3f0fe822b4a4b004ee0224fc0d",
]
this.timeout(0)
before(async () => {
db = await init()
})
after(async () => await stop())
beforeEach(async () => {
;({ walletAddress, wallet, wallet2 } = await initBeforeEach())
})
it("should get nonce", async () => {
expect(await db.getNonce(wallet.getAddressString())).to.equal(1)
await db.set({ id: 1 }, "col", "doc")
expect(await db.getNonce(wallet.getAddressString())).to.equal(2)
})
it("should add & get", async () => {
const data = { name: "Bob", age: 20 }
const tx = await db.add(data, "ppl")
expect(await db.get("ppl", (await db.getIds(tx))[0])).to.eql(data)
})
it("should set & get", async () => {
const data = { name: "Bob", age: 20 }
const data2 = { name: "Alice", height: 160 }
await db.set(data, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql(data)
await db.set(data2, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql(data2)
})
it("should cget & pagenate", async () => {
const data = { name: "Bob", age: 20 }
const data2 = { name: "Alice", age: 160 }
await db.set(data, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql(data)
await db.set(data2, "ppl", "Alice")
const cursor = (await db.cget("ppl", ["age"], 1))[0]
expect(await db.get("ppl", ["age"], ["startAfter", cursor])).to.eql([data2])
})
it("should update", async () => {
const data = { name: "Bob", age: 20 }
await db.set(data, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql(data)
await db.update({ age: 25 }, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql({ name: "Bob", age: 25 })
await db.update({ age: db.inc(5) }, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql({ name: "Bob", age: 30 })
await db.update({ age: db.del(5) }, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql({ name: "Bob" })
// arrayUnion
await db.update({ foods: db.union("pasta", "cake", "wine") }, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql({
name: "Bob",
foods: ["pasta", "cake", "wine"],
})
// arrayRemove
await db.update({ foods: db.remove("pasta", "cake") }, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql({
name: "Bob",
foods: ["wine"],
})
// timestamp
const tx = await db.update({ death: db.ts() }, "ppl", "Bob")
const tx_data = await db.arweave.transactions.get(tx)
const timestamp = (await db.arweave.blocks.get(tx_data.block)).timestamp
expect((await db.get("ppl", "Bob")).death).to.be.lte(timestamp)
})
it("should upsert", async () => {
const data = { name: "Bob", age: 20 }
await db.upsert(data, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql(data)
})
it("should delete", async () => {
const data = { name: "Bob", age: 20 }
await db.set(data, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql(data)
await db.delete("ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql(null)
})
it("should get a collection", async () => {
const Bob = {
name: "Bob",
age: 20,
height: 170,
weight: 75,
letters: ["b", "o"],
}
const Alice = {
name: "Alice",
age: 30,
height: 160,
weight: 60,
letters: ["a", "l", "i", "c", "e"],
}
const John = {
name: "John",
age: 40,
height: 180,
weight: 100,
letters: ["j", "o", "h", "n"],
}
const Beth = {
name: "Beth",
age: 30,
height: 165,
weight: 70,
letters: ["b", "e", "t", "h"],
}
await db.set(Bob, "ppl", "Bob")
await db.set(Alice, "ppl", "Alice")
await db.set(John, "ppl", "John")
await db.set(Beth, "ppl", "Beth")
expect(await db.get("ppl")).to.eql([Bob, Alice, John, Beth])
// limit
expect((await db.get("ppl", 1)).length).to.eql(1)
// sort
expect(await db.get("ppl", ["height"])).to.eql([Alice, Beth, Bob, John])
// sort desc
expect(await db.get("ppl", ["height", "desc"])).to.eql([
John,
Bob,
Beth,
Alice,
])
// sort multiple fields
await db.addIndex([["age"], ["weight", "desc"]], "ppl")
expect(await db.get("ppl", ["age"], ["weight", "desc"])).to.eql([
Bob,
Beth,
Alice,
John,
])
// where =
expect(await db.get("ppl", ["age", "=", 30])).to.eql([Alice, Beth])
// where >
expect(await db.get("ppl", ["age"], ["age", ">", 30])).to.eql([John])
// where >=
expect(await db.get("ppl", ["age"], ["age", ">=", 30])).to.eql([
Beth,
Alice,
John,
])
// where <
expect(await db.get("ppl", ["age"], ["age", "<", 30])).to.eql([Bob])
// where <=
expect(await db.get("ppl", ["age"], ["age", "<=", 30])).to.eql([
Bob,
Beth,
Alice,
])
// where =!
expect(await db.get("ppl", ["age"], ["age", "!=", 30])).to.eql([Bob, John])
// where in
expect(await db.get("ppl", ["age", "in", [20, 30]])).to.eql([
Bob,
Alice,
Beth,
])
// where not-in
expect(await db.get("ppl", ["age"], ["age", "not-in", [20, 30]])).to.eql([
John,
])
// where array-contains
expect(await db.get("ppl", ["letters", "array-contains", "b"])).to.eql([
Bob,
Beth,
])
// where array-contains-any
expect(
await db.get("ppl", ["letters", "array-contains-any", ["j", "t"]])
).to.eql([John, Beth])
// skip startAt
expect(await db.get("ppl", ["age"], ["startAt", 30])).to.eql([
Beth,
Alice,
John,
])
// skip startAfter
expect(await db.get("ppl", ["age"], ["startAfter", 30])).to.eql([John])
// skip endAt
expect(await db.get("ppl", ["age"], ["endAt", 30])).to.eql([
Bob,
Beth,
Alice,
])
// skip endBefore
expect(await db.get("ppl", ["age"], ["endBefore", 30])).to.eql([Bob])
// skip startAt multiple fields
await db.addIndex([["age"], ["weight"]], "ppl")
expect(
await db.get("ppl", ["age"], ["weight"], ["startAt", 30, 70])
).to.eql([Beth, John])
// skip endAt multiple fields
expect(await db.get("ppl", ["age"], ["weight"], ["endAt", 30, 60])).to.eql([
Bob,
Alice,
])
})
it("should batch execute", async () => {
const data = { name: "Bob", age: 20 }
const data2 = { name: "Alice", age: 40 }
const data3 = { name: "Beth", age: 10 }
const tx = await db.batch([
["set", data, "ppl", "Bob"],
["set", data3, "ppl", "Beth"],
["update", { age: 30 }, "ppl", "Bob"],
["upsert", { age: 20 }, "ppl", "Bob"],
["add", data2, "ppl"],
["delete", "ppl", "Beth"],
])
expect(await db.get("ppl", "Bob")).to.eql({ name: "Bob", age: 20 })
expect(await db.get("ppl", (await db.getIds(tx))[0])).to.eql(data2)
expect(await db.get("ppl", "Beth")).to.eql(null)
})
it("should set schema", async () => {
const data = { name: "Bob", age: 20 }
const schema = {
type: "object",
required: ["name"],
properties: {
name: {
type: "number",
},
},
}
const schema2 = {
type: "object",
required: ["name"],
properties: {
name: {
type: "string",
},
},
}
await db.setSchema(schema, "ppl")
expect(await db.getSchema("ppl")).to.eql(schema)
await db.set(data, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql(null)
await db.setSchema(schema2, "ppl")
expect(await db.getSchema("ppl")).to.eql(schema2)
await db.set(data, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql(data)
})
it("should set rules", async () => {
const data = { name: "Bob", age: 20 }
const rules = {
"allow create,update": {
and: [
{ "!=": [{ var: "request.auth.signer" }, null] },
{ "<": [{ var: "resource.newData.age" }, 30] },
],
},
"deny delete": { "!=": [{ var: "request.auth.signer" }, null] },
}
await db.setRules(rules, "ppl")
expect(await db.getRules("ppl")).to.eql(rules)
await db.set(data, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql(data)
await db.delete("ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql(data)
await db.update({ age: db.inc(10) }, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql({ name: "Bob", age: 20 })
await db.update({ age: db.inc(5) }, "ppl", "Bob")
expect(await db.get("ppl", "Bob")).to.eql({ name: "Bob", age: 25 })
})
it("should add index", async () => {
const data = { name: "Bob", age: 20 }
const data2 = { name: "Alice", age: 25 }
const data3 = { name: "Beth", age: 5 }
const data4 = { name: "John", age: 20, height: 150 }
await db.add(data, "ppl")
expect(await db.get("ppl", ["age"])).to.eql([data])
await db.set(data2, "ppl", "Alice")
expect(await db.get("ppl", ["age", "desc"])).to.eql([data2, data])
await db.upsert(data3, "ppl", "Beth")
expect(await db.get("ppl", ["age", "desc"])).to.eql([data2, data, data3])
await db.update({ age: 30 }, "ppl", "Beth")
expect(await db.get("ppl", ["age", "desc"])).to.eql([
{ name: "Beth", age: 30 },
data2,
data,
])
await db.addIndex([["age"], ["name", "desc"]], "ppl")
await db.addIndex([["age"], ["name", "desc"], ["height"]], "ppl")
await db.addIndex([["age"], ["name", "desc"], ["height", "desc"]], "ppl")
await db.upsert(data4, "ppl", "John")
expect(await db.get("ppl", ["age"], ["name", "desc"])).to.eql([
data4,
data,
data2,
{ name: "Beth", age: 30 },
])
expect(
await db.get("ppl", ["age"], ["name", "in", ["Alice", "John"]])
).to.eql([data4, data2])
expect(await db.getIndexes("ppl")).to.eql([
[["name", "asc"]],
[["age", "asc"]],
[
["age", "asc"],
["name", "desc"],
],
[
["age", "asc"],
["name", "desc"],
["height", "asc"],
],
[
["age", "asc"],
["name", "desc"],
["height", "desc"],
],
[["height", "asc"]],
])
})
it("should link temporarily generated address", async () => {
const addr = wallet.getAddressString()
const { identity } = await db.createTempAddress(addr)
delete db.wallet
await db.set({ name: "Beth", age: 10 }, "ppl", "Beth", {
wallet: addr,
privateKey: identity.privateKey,
})
expect((await db.cget("ppl", "Beth")).setter).to.eql(addr)
await db.removeAddressLink(
{
address: identity.address,
},
{ wallet }
)
await db.set({ name: "Bob", age: 20 }, "ppl", "Bob", {
privateKey: identity.privateKey,
overwrite: true,
})
expect((await db.cget("ppl", "Bob")).setter).to.eql(
identity.address.toLowerCase()
)
})
it("should pre-process the new data with rules", async () => {
const rules = {
let: {
"resource.newData.age": 30,
},
"allow create": true,
}
await db.setRules(rules, "ppl")
await db.upsert({ name: "Bob" }, "ppl", "Bob")
expect((await db.get("ppl", "Bob")).age).to.eql(30)
await db.upsert({ name: "Bob" }, "ppl", "Bob")
})
it("should execute crons", async () => {
await db.set({ age: 3 }, "ppl", "Bob")
await db.addCron(
{
span: 2,
times: 2,
do: true,
jobs: [["upsert", [{ age: db.inc(1) }, "ppl", "Bob"]]],
},
"inc age"
)
expect((await db.get("ppl", "Bob")).age).to.eql(4)
while (true) {
await db.mineBlock()
if ((await db.get("ppl", "Bob")).age > 4) {
break
}
}
expect((await db.get("ppl", "Bob")).age).to.be.eql(5)
await db.removeCron("inc age")
expect((await db.getCrons()).crons).to.eql({})
})
it("should link temporarily generated address with internet identity", async () => {
const ii = Ed25519KeyIdentity.fromJSON(JSON.stringify(_ii))
const addr = ii.toJSON()[0]
const { identity } = await db.createTempAddressWithII(ii)
await db.set({ name: "Beth", age: 10 }, "ppl", "Beth", {
wallet: addr,
privateKey: identity.privateKey,
})
expect((await db.cget("ppl", "Beth")).setter).to.eql(addr)
await db.removeAddressLink(
{
address: identity.address,
},
{ ii }
)
await db.set({ name: "Bob", age: 20 }, "ppl", "Bob", {
privateKey: identity.privateKey,
overwrite: true,
})
expect((await db.cget("ppl", "Bob")).setter).to.eql(
identity.address.toLowerCase()
)
})
it("should add & get with internet identity", async () => {
const ii = Ed25519KeyIdentity.fromJSON(JSON.stringify(_ii))
const data = { name: "Bob", age: 20 }
const tx = await db.add(data, "ppl", { ii })
expect((await db.cget("ppl", (await db.getIds(tx))[0])).setter).to.eql(
ii.toJSON()[0]
)
})
it("should add & get with Arweave wallet", async () => {
const arweave_wallet = await db.arweave.wallets.generate()
const data = { name: "Bob", age: 20 }
const tx = await db.add(data, "ppl", { ar: arweave_wallet })
const addr = await db.arweave.wallets.jwkToAddress(arweave_wallet)
expect((await db.cget("ppl", (await db.getIds(tx))[0])).setter).to.eql(addr)
return
})
it("should link temporarily generated address with Arweave wallet", async () => {
const arweave_wallet = await db.arweave.wallets.generate()
let addr = await db.arweave.wallets.jwkToAddress(arweave_wallet)
const { identity } = await db.createTempAddressWithAR(arweave_wallet)
await db.set({ name: "Beth", age: 10 }, "ppl", "Beth", {
wallet: addr,
privateKey: identity.privateKey,
})
expect((await db.cget("ppl", "Beth")).setter).to.eql(addr)
await db.removeAddressLink(
{
address: identity.address,
},
{ ar: arweave_wallet }
)
await db.set({ name: "Bob", age: 20 }, "ppl", "Bob", {
privateKey: identity.privateKey,
overwrite: true,
})
expect((await db.cget("ppl", "Bob")).setter).to.eql(
identity.address.toLowerCase()
)
})
})