@xyo-network/diviner-payload-generic 5.1.21 → 5.1.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +24 -21
- package/dist/neutral/spec/Diviner.indexdb.spec.d.ts +0 -2
- package/dist/neutral/spec/Diviner.indexdb.spec.d.ts.map +0 -1
- package/dist/neutral/spec/Diviner.spec.d.ts +0 -2
- package/dist/neutral/spec/Diviner.spec.d.ts.map +0 -1
- package/src/spec/Diviner.indexdb.spec.ts +0 -274
- package/src/spec/Diviner.spec.ts +0 -251
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xyo-network/diviner-payload-generic",
|
|
3
|
-
"version": "5.1.
|
|
3
|
+
"version": "5.1.23",
|
|
4
4
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
5
5
|
"homepage": "https://xyo.network",
|
|
6
6
|
"bugs": {
|
|
@@ -30,35 +30,38 @@
|
|
|
30
30
|
"types": "dist/neutral/index.d.ts",
|
|
31
31
|
"files": [
|
|
32
32
|
"dist",
|
|
33
|
-
"src"
|
|
33
|
+
"src",
|
|
34
|
+
"!**/*.bench.*",
|
|
35
|
+
"!**/*.spec.*",
|
|
36
|
+
"!**/*.test.*"
|
|
34
37
|
],
|
|
35
38
|
"dependencies": {
|
|
36
|
-
"@xylabs/array": "~5.0.
|
|
37
|
-
"@xylabs/assert": "~5.0.
|
|
38
|
-
"@xylabs/events": "~5.0.
|
|
39
|
-
"@xylabs/forget": "~5.0.
|
|
40
|
-
"@xylabs/hex": "~5.0.
|
|
41
|
-
"@xyo-network/archivist-model": "~5.1.
|
|
42
|
-
"@xyo-network/diviner-model": "~5.1.
|
|
43
|
-
"@xyo-network/diviner-payload-abstract": "~5.1.
|
|
44
|
-
"@xyo-network/diviner-payload-model": "~5.1.
|
|
45
|
-
"@xyo-network/module-model": "~5.1.
|
|
46
|
-
"@xyo-network/payload-builder": "~5.1.
|
|
47
|
-
"@xyo-network/payload-model": "~5.1.
|
|
39
|
+
"@xylabs/array": "~5.0.33",
|
|
40
|
+
"@xylabs/assert": "~5.0.33",
|
|
41
|
+
"@xylabs/events": "~5.0.33",
|
|
42
|
+
"@xylabs/forget": "~5.0.33",
|
|
43
|
+
"@xylabs/hex": "~5.0.33",
|
|
44
|
+
"@xyo-network/archivist-model": "~5.1.23",
|
|
45
|
+
"@xyo-network/diviner-model": "~5.1.23",
|
|
46
|
+
"@xyo-network/diviner-payload-abstract": "~5.1.23",
|
|
47
|
+
"@xyo-network/diviner-payload-model": "~5.1.23",
|
|
48
|
+
"@xyo-network/module-model": "~5.1.23",
|
|
49
|
+
"@xyo-network/payload-builder": "~5.1.23",
|
|
50
|
+
"@xyo-network/payload-model": "~5.1.23",
|
|
48
51
|
"async-mutex": "~0.5.0"
|
|
49
52
|
},
|
|
50
53
|
"devDependencies": {
|
|
51
|
-
"@xylabs/delay": "~5.0.
|
|
54
|
+
"@xylabs/delay": "~5.0.33",
|
|
52
55
|
"@xylabs/ts-scripts-yarn3": "~7.2.8",
|
|
53
56
|
"@xylabs/tsconfig": "~7.2.8",
|
|
54
|
-
"@xylabs/vitest-extended": "~5.0.
|
|
55
|
-
"@xyo-network/archivist-indexeddb": "~5.1.
|
|
56
|
-
"@xyo-network/archivist-memory": "~5.1.
|
|
57
|
-
"@xyo-network/node-memory": "~5.1.
|
|
58
|
-
"@xyo-network/payload-builder": "~5.1.
|
|
57
|
+
"@xylabs/vitest-extended": "~5.0.33",
|
|
58
|
+
"@xyo-network/archivist-indexeddb": "~5.1.23",
|
|
59
|
+
"@xyo-network/archivist-memory": "~5.1.23",
|
|
60
|
+
"@xyo-network/node-memory": "~5.1.23",
|
|
61
|
+
"@xyo-network/payload-builder": "~5.1.23",
|
|
59
62
|
"fake-indexeddb": "~6.2.5",
|
|
60
63
|
"typescript": "~5.9.3",
|
|
61
|
-
"vitest": "~4.0.
|
|
64
|
+
"vitest": "~4.0.9"
|
|
62
65
|
},
|
|
63
66
|
"publishConfig": {
|
|
64
67
|
"access": "public"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Diviner.indexdb.spec.d.ts","sourceRoot":"","sources":["../../../src/spec/Diviner.indexdb.spec.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Diviner.spec.d.ts","sourceRoot":"","sources":["../../../src/spec/Diviner.spec.ts"],"names":[],"mappings":"AAEA,OAAO,yBAAyB,CAAA"}
|
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
import '@xylabs/vitest-extended'
|
|
2
|
-
|
|
3
|
-
import { delay } from '@xylabs/delay'
|
|
4
|
-
import { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'
|
|
5
|
-
import type { PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'
|
|
6
|
-
import { PayloadDivinerQuerySchema } from '@xyo-network/diviner-payload-model'
|
|
7
|
-
import { MemoryNode } from '@xyo-network/node-memory'
|
|
8
|
-
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
9
|
-
import type { Payload, WithStorageMeta } from '@xyo-network/payload-model'
|
|
10
|
-
import {
|
|
11
|
-
IDBCursor,
|
|
12
|
-
IDBCursorWithValue,
|
|
13
|
-
IDBDatabase,
|
|
14
|
-
IDBFactory,
|
|
15
|
-
IDBIndex,
|
|
16
|
-
IDBKeyRange,
|
|
17
|
-
IDBObjectStore,
|
|
18
|
-
IDBOpenDBRequest,
|
|
19
|
-
IDBRequest,
|
|
20
|
-
IDBTransaction,
|
|
21
|
-
IDBVersionChangeEvent,
|
|
22
|
-
indexedDB,
|
|
23
|
-
} from 'fake-indexeddb'
|
|
24
|
-
import {
|
|
25
|
-
beforeAll, describe, expect, it,
|
|
26
|
-
} from 'vitest'
|
|
27
|
-
|
|
28
|
-
import { GenericPayloadDiviner, GenericPayloadDivinerConfigSchema } from '../Diviner.ts'
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @group module
|
|
32
|
-
* @group diviner
|
|
33
|
-
*/
|
|
34
|
-
|
|
35
|
-
describe('GenericPayloadDiviner', () => {
|
|
36
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
-
const globalThisAny = globalThis as any
|
|
38
|
-
|
|
39
|
-
// Augment window with prototypes to ensure instance of comparisons work
|
|
40
|
-
globalThisAny.IDBCursor = IDBCursor
|
|
41
|
-
globalThisAny.IDBCursorWithValue = IDBCursorWithValue
|
|
42
|
-
globalThisAny.IDBDatabase = IDBDatabase
|
|
43
|
-
globalThisAny.IDBFactory = IDBFactory
|
|
44
|
-
globalThisAny.IDBIndex = IDBIndex
|
|
45
|
-
globalThisAny.IDBKeyRange = IDBKeyRange
|
|
46
|
-
globalThisAny.IDBObjectStore = IDBObjectStore
|
|
47
|
-
globalThisAny.IDBOpenDBRequest = IDBOpenDBRequest
|
|
48
|
-
globalThisAny.IDBRequest = IDBRequest
|
|
49
|
-
globalThisAny.IDBTransaction = IDBTransaction
|
|
50
|
-
globalThisAny.IDBVersionChangeEvent = IDBVersionChangeEvent
|
|
51
|
-
globalThisAny.indexedDB = indexedDB
|
|
52
|
-
|
|
53
|
-
let archivist: IndexedDbArchivist
|
|
54
|
-
let sut: GenericPayloadDiviner
|
|
55
|
-
let node: MemoryNode
|
|
56
|
-
let payloadA: Payload<{ schema: string; url: string }>
|
|
57
|
-
let payloadB: Payload<{ foo: string[]; schema: string }>
|
|
58
|
-
let payloadC: Payload<{ foo: string[]; schema: string }>
|
|
59
|
-
let payloadD: Payload<{ foo: string[]; schema: string }>
|
|
60
|
-
let insertedPayloads: WithStorageMeta<Payload>[]
|
|
61
|
-
|
|
62
|
-
beforeAll(async () => {
|
|
63
|
-
payloadA = {
|
|
64
|
-
schema: 'network.xyo.test',
|
|
65
|
-
url: 'https://xyo.network',
|
|
66
|
-
}
|
|
67
|
-
payloadB = {
|
|
68
|
-
foo: ['bar', 'baz'],
|
|
69
|
-
schema: 'network.xyo.debug',
|
|
70
|
-
}
|
|
71
|
-
payloadC = {
|
|
72
|
-
foo: ['one', 'two'],
|
|
73
|
-
schema: 'network.xyo.debug',
|
|
74
|
-
}
|
|
75
|
-
payloadD = {
|
|
76
|
-
foo: ['aaa', 'bbb'],
|
|
77
|
-
schema: 'network.xyo.debug',
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
archivist = await IndexedDbArchivist.create({
|
|
81
|
-
account: 'random',
|
|
82
|
-
config: { name: 'test', schema: IndexedDbArchivist.defaultConfigSchema },
|
|
83
|
-
})
|
|
84
|
-
const [insertedPayloadA] = await archivist.insert([payloadA])
|
|
85
|
-
await delay(1)
|
|
86
|
-
const [insertedPayloadB] = await archivist.insert([payloadB])
|
|
87
|
-
await delay(1)
|
|
88
|
-
const [insertedPayloadC] = await archivist.insert([payloadC])
|
|
89
|
-
await delay(1)
|
|
90
|
-
const [insertedPayloadD] = await archivist.insert([payloadD])
|
|
91
|
-
await delay(1)
|
|
92
|
-
insertedPayloads = [insertedPayloadA, insertedPayloadB, insertedPayloadC, insertedPayloadD]
|
|
93
|
-
const all = await archivist.all()
|
|
94
|
-
console.log(all)
|
|
95
|
-
sut = await GenericPayloadDiviner.create({
|
|
96
|
-
account: 'random',
|
|
97
|
-
config: {
|
|
98
|
-
archivist: archivist.address,
|
|
99
|
-
schema: GenericPayloadDivinerConfigSchema,
|
|
100
|
-
},
|
|
101
|
-
})
|
|
102
|
-
node = await MemoryNode.create({
|
|
103
|
-
account: 'random',
|
|
104
|
-
config: { schema: MemoryNode.defaultConfigSchema },
|
|
105
|
-
})
|
|
106
|
-
const modules = [archivist, sut]
|
|
107
|
-
await node.start()
|
|
108
|
-
await Promise.all(
|
|
109
|
-
modules.map(async (mod) => {
|
|
110
|
-
await node.register(mod)
|
|
111
|
-
await node.attach(mod.address, true)
|
|
112
|
-
}),
|
|
113
|
-
)
|
|
114
|
-
await sut.start()
|
|
115
|
-
})
|
|
116
|
-
describe('with filter for', () => {
|
|
117
|
-
describe('schema', () => {
|
|
118
|
-
describe('single', () => {
|
|
119
|
-
it.each(['network.xyo.test', 'network.xyo.debug'])('only returns payloads of that schema', async (schema) => {
|
|
120
|
-
const schemas = [schema]
|
|
121
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
122
|
-
.fields({ schemas })
|
|
123
|
-
.build()
|
|
124
|
-
const results = await sut.divine([query])
|
|
125
|
-
expect(results.length).toBeGreaterThan(0)
|
|
126
|
-
expect(results.every(result => result.schema === schema)).toBe(true)
|
|
127
|
-
})
|
|
128
|
-
it('only return single payload of that schema', async () => {
|
|
129
|
-
const schemas = ['network.xyo.debug']
|
|
130
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
131
|
-
.fields({ limit: 1, schemas })
|
|
132
|
-
.build()
|
|
133
|
-
const results = await sut.divine([query])
|
|
134
|
-
expect(results.length).toBe(1)
|
|
135
|
-
expect(await PayloadBuilder.dataHash(results[0])).toBe(await PayloadBuilder.dataHash(payloadD))
|
|
136
|
-
expect(results.every(result => result.schema === 'network.xyo.debug')).toBe(true)
|
|
137
|
-
})
|
|
138
|
-
it('only return single payload of that schema (desc)', async () => {
|
|
139
|
-
const schemas = ['network.xyo.debug']
|
|
140
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
141
|
-
.fields({
|
|
142
|
-
limit: 1, order: 'desc', schemas,
|
|
143
|
-
})
|
|
144
|
-
.build()
|
|
145
|
-
const results = await sut.divine([query])
|
|
146
|
-
expect(results.length).toBe(1)
|
|
147
|
-
expect(await PayloadBuilder.dataHash(results[0])).toBe(await PayloadBuilder.dataHash(payloadD))
|
|
148
|
-
expect(results.every(result => result.schema === 'network.xyo.debug')).toBe(true)
|
|
149
|
-
})
|
|
150
|
-
it('only return single payload of that schema (asc)', async () => {
|
|
151
|
-
const schemas = ['network.xyo.debug']
|
|
152
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
153
|
-
.fields({
|
|
154
|
-
limit: 1, order: 'asc', schemas,
|
|
155
|
-
})
|
|
156
|
-
.build()
|
|
157
|
-
const results = await sut.divine([query])
|
|
158
|
-
expect(results.length).toBe(1)
|
|
159
|
-
expect(await PayloadBuilder.dataHash(results[0])).toBe(await PayloadBuilder.dataHash(payloadB))
|
|
160
|
-
expect(results.every(result => result.schema === 'network.xyo.debug')).toBe(true)
|
|
161
|
-
})
|
|
162
|
-
})
|
|
163
|
-
describe('multiple', () => {
|
|
164
|
-
it('only returns payloads of that schema', async () => {
|
|
165
|
-
const schemas = ['network.xyo.test', 'network.xyo.debug']
|
|
166
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
167
|
-
.fields({ schemas })
|
|
168
|
-
.build()
|
|
169
|
-
const results = await sut.divine([query])
|
|
170
|
-
expect(results.length).toBeGreaterThan(0)
|
|
171
|
-
expect(results.every(result => schemas.includes(result.schema))).toBe(true)
|
|
172
|
-
})
|
|
173
|
-
})
|
|
174
|
-
describe('paging', () => {
|
|
175
|
-
it('test paging with multiple calls (asc)', async () => {
|
|
176
|
-
const schemas = ['network.xyo.test', 'network.xyo.debug']
|
|
177
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
178
|
-
.fields({
|
|
179
|
-
limit: 2, order: 'asc', schemas,
|
|
180
|
-
})
|
|
181
|
-
.build()
|
|
182
|
-
const results = await sut.divine([query])
|
|
183
|
-
const resultSequences = results.map(result => result._sequence)
|
|
184
|
-
expect(PayloadBuilder.omitStorageMeta(results)).toStrictEqual([payloadA, payloadB])
|
|
185
|
-
expect(results.length).toBe(2)
|
|
186
|
-
expect(resultSequences[0]).toBe(insertedPayloads[0]._sequence)
|
|
187
|
-
expect(resultSequences[1]).toBe(insertedPayloads[1]._sequence)
|
|
188
|
-
|
|
189
|
-
const cursor = resultSequences[1]
|
|
190
|
-
const query2 = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
191
|
-
.fields({
|
|
192
|
-
limit: 2, cursor, order: 'asc', schemas,
|
|
193
|
-
})
|
|
194
|
-
.build()
|
|
195
|
-
const results2 = await sut.divine([query2])
|
|
196
|
-
const resultSequences2 = results2.map(result => result._sequence)
|
|
197
|
-
expect(results2.length).toBe(2)
|
|
198
|
-
expect(resultSequences2[0]).toBe(insertedPayloads[2]._sequence)
|
|
199
|
-
expect(resultSequences2[1]).toBe(insertedPayloads[3]._sequence)
|
|
200
|
-
const cursor2 = resultSequences2[1]
|
|
201
|
-
|
|
202
|
-
const query3 = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
203
|
-
.fields({
|
|
204
|
-
limit: 2, cursor: cursor2, order: 'asc', schemas,
|
|
205
|
-
})
|
|
206
|
-
.build()
|
|
207
|
-
const results3 = await sut.divine([query3])
|
|
208
|
-
expect(results3).toBeArrayOfSize(0)
|
|
209
|
-
})
|
|
210
|
-
it('test paging with multiple calls (desc)', async () => {
|
|
211
|
-
const schemas = ['network.xyo.test', 'network.xyo.debug']
|
|
212
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
213
|
-
.fields({
|
|
214
|
-
limit: 2, order: 'desc', schemas,
|
|
215
|
-
})
|
|
216
|
-
.build()
|
|
217
|
-
const results = await sut.divine([query])
|
|
218
|
-
const resultSequences = results.map(result => result._sequence)
|
|
219
|
-
expect(results.length).toBe(2)
|
|
220
|
-
expect(resultSequences[0]).toBe(insertedPayloads[3]._sequence)
|
|
221
|
-
expect(resultSequences[1]).toBe(insertedPayloads[2]._sequence)
|
|
222
|
-
|
|
223
|
-
const cursor = resultSequences[1]
|
|
224
|
-
const query2 = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
225
|
-
.fields({
|
|
226
|
-
limit: 2, cursor, order: 'desc', schemas,
|
|
227
|
-
})
|
|
228
|
-
.build()
|
|
229
|
-
const results2 = await sut.divine([query2])
|
|
230
|
-
const resultSequences2 = results2.map(result => result._sequence)
|
|
231
|
-
expect(results2.length).toBe(2)
|
|
232
|
-
expect(resultSequences2[0]).toBe(insertedPayloads[1]._sequence)
|
|
233
|
-
expect(resultSequences2[1]).toBe(insertedPayloads[0]._sequence)
|
|
234
|
-
const cursor2 = resultSequences2[1]
|
|
235
|
-
|
|
236
|
-
const query3 = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
237
|
-
.fields({
|
|
238
|
-
limit: 2, cursor: cursor2, order: 'desc', schemas,
|
|
239
|
-
})
|
|
240
|
-
.build()
|
|
241
|
-
const results3 = await sut.divine([query3])
|
|
242
|
-
expect(results3).toBeArrayOfSize(0)
|
|
243
|
-
})
|
|
244
|
-
})
|
|
245
|
-
})
|
|
246
|
-
describe('custom field', () => {
|
|
247
|
-
describe('property', () => {
|
|
248
|
-
it('only returns payloads with that property', async () => {
|
|
249
|
-
type WithUrl = { url?: string }
|
|
250
|
-
const url = payloadA.url
|
|
251
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload<{ url?: string }>>({ schema: PayloadDivinerQuerySchema })
|
|
252
|
-
.fields({ url })
|
|
253
|
-
.build()
|
|
254
|
-
const results = await sut.divine([query])
|
|
255
|
-
expect(results.length).toBeGreaterThan(0)
|
|
256
|
-
expect(results.every(result => (result as WithUrl)?.url === url)).toBe(true)
|
|
257
|
-
})
|
|
258
|
-
})
|
|
259
|
-
describe('array', () => {
|
|
260
|
-
const cases: string[][] = [['bar'], ['baz'], ['bar', 'baz']]
|
|
261
|
-
it.each(cases)('only returns payloads that have an array containing all the values supplied', async (...foo) => {
|
|
262
|
-
type WithFoo = { foo?: string[] }
|
|
263
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload<{ foo?: string[] }>>({ schema: PayloadDivinerQuerySchema })
|
|
264
|
-
.fields({ foo })
|
|
265
|
-
.build()
|
|
266
|
-
const results = await sut.divine([query])
|
|
267
|
-
expect(results.length).toBeGreaterThan(0)
|
|
268
|
-
// eslint-disable-next-line max-nested-callbacks
|
|
269
|
-
expect(results.every(result => foo.every(v => (result as unknown as WithFoo)?.foo?.includes(v)))).toBe(true)
|
|
270
|
-
})
|
|
271
|
-
})
|
|
272
|
-
})
|
|
273
|
-
})
|
|
274
|
-
})
|
package/src/spec/Diviner.spec.ts
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
/* eslint-disable max-nested-callbacks */
|
|
2
|
-
|
|
3
|
-
import '@xylabs/vitest-extended'
|
|
4
|
-
|
|
5
|
-
import { delay } from '@xylabs/delay'
|
|
6
|
-
import { MemoryArchivist } from '@xyo-network/archivist-memory'
|
|
7
|
-
import type { PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'
|
|
8
|
-
import { PayloadDivinerQuerySchema } from '@xyo-network/diviner-payload-model'
|
|
9
|
-
import { MemoryNode } from '@xyo-network/node-memory'
|
|
10
|
-
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
11
|
-
import type { Payload, WithStorageMeta } from '@xyo-network/payload-model'
|
|
12
|
-
import {
|
|
13
|
-
beforeAll, describe, expect, it,
|
|
14
|
-
} from 'vitest'
|
|
15
|
-
|
|
16
|
-
import { GenericPayloadDiviner, GenericPayloadDivinerConfigSchema } from '../Diviner.ts'
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* @group module
|
|
20
|
-
* @group diviner
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
describe('GenericPayloadDiviner', () => {
|
|
24
|
-
let archivist: MemoryArchivist
|
|
25
|
-
let sut: GenericPayloadDiviner
|
|
26
|
-
let node: MemoryNode
|
|
27
|
-
const payloadA: Payload<{ schema: string; url: string }> = {
|
|
28
|
-
schema: 'network.xyo.test',
|
|
29
|
-
url: 'https://xyo.network',
|
|
30
|
-
}
|
|
31
|
-
const payloadB: Payload<{ foo: string[]; schema: string }> = {
|
|
32
|
-
foo: ['bar', 'baz'],
|
|
33
|
-
schema: 'network.xyo.debug',
|
|
34
|
-
}
|
|
35
|
-
const payloadC: Payload<{ foo: string[]; schema: string }> = {
|
|
36
|
-
foo: ['one', 'two'],
|
|
37
|
-
schema: 'network.xyo.debug',
|
|
38
|
-
}
|
|
39
|
-
const payloadD: Payload<{ foo: string[]; schema: string }> = {
|
|
40
|
-
foo: ['aaa', 'bbb'],
|
|
41
|
-
schema: 'network.xyo.debug',
|
|
42
|
-
}
|
|
43
|
-
let insertedPayloads: WithStorageMeta<Payload>[] = []
|
|
44
|
-
beforeAll(async () => {
|
|
45
|
-
archivist = await MemoryArchivist.create({
|
|
46
|
-
account: 'random',
|
|
47
|
-
config: { name: 'test', schema: MemoryArchivist.defaultConfigSchema },
|
|
48
|
-
})
|
|
49
|
-
for (const payload of [payloadA, payloadB, payloadC, payloadD]) {
|
|
50
|
-
await delay(2)
|
|
51
|
-
const [insertedPayload] = await archivist.insert([payload])
|
|
52
|
-
insertedPayloads.push(insertedPayload)
|
|
53
|
-
}
|
|
54
|
-
sut = await GenericPayloadDiviner.create({
|
|
55
|
-
account: 'random',
|
|
56
|
-
config: {
|
|
57
|
-
archivist: archivist.address,
|
|
58
|
-
schema: GenericPayloadDivinerConfigSchema,
|
|
59
|
-
},
|
|
60
|
-
})
|
|
61
|
-
node = await MemoryNode.create({
|
|
62
|
-
account: 'random',
|
|
63
|
-
config: { schema: MemoryNode.defaultConfigSchema },
|
|
64
|
-
})
|
|
65
|
-
const modules = [archivist, sut]
|
|
66
|
-
await node.start()
|
|
67
|
-
await Promise.all(
|
|
68
|
-
modules.map(async (mod) => {
|
|
69
|
-
await node.register(mod)
|
|
70
|
-
await node.attach(mod.address, true)
|
|
71
|
-
}),
|
|
72
|
-
)
|
|
73
|
-
await sut.start()
|
|
74
|
-
})
|
|
75
|
-
describe('with filter for', () => {
|
|
76
|
-
describe('schema', () => {
|
|
77
|
-
describe('single', () => {
|
|
78
|
-
it.each(['network.xyo.test', 'network.xyo.debug'])('only returns payloads of that schema', async (schema) => {
|
|
79
|
-
const schemas = [schema]
|
|
80
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema }).fields({ schemas }).build()
|
|
81
|
-
const results = await sut.divine([query])
|
|
82
|
-
expect(results.length).toBeGreaterThan(0)
|
|
83
|
-
expect(results.every(result => result.schema === schema)).toBe(true)
|
|
84
|
-
})
|
|
85
|
-
it('only return single payload of that schema', async () => {
|
|
86
|
-
const schemas = ['network.xyo.debug']
|
|
87
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
88
|
-
.fields({ limit: 1, schemas })
|
|
89
|
-
.build()
|
|
90
|
-
const results = await sut.divine([query])
|
|
91
|
-
expect(results.length).toBe(1)
|
|
92
|
-
expect(await PayloadBuilder.dataHash(results[0])).toBe(await PayloadBuilder.dataHash(insertedPayloads[3]))
|
|
93
|
-
expect(results.every(result => result.schema === 'network.xyo.debug')).toBe(true)
|
|
94
|
-
})
|
|
95
|
-
it('only return single payload of that schema (desc)', async () => {
|
|
96
|
-
const schemas = ['network.xyo.debug']
|
|
97
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
98
|
-
.fields({
|
|
99
|
-
limit: 1, order: 'desc', schemas,
|
|
100
|
-
})
|
|
101
|
-
.build()
|
|
102
|
-
const results = await sut.divine([query])
|
|
103
|
-
expect(results.length).toBe(1)
|
|
104
|
-
expect(PayloadBuilder.omitStorageMeta(results[0])).toStrictEqual(payloadD)
|
|
105
|
-
expect(await PayloadBuilder.dataHash(results[0])).toBe(await PayloadBuilder.dataHash(insertedPayloads[3]))
|
|
106
|
-
expect(results.every(result => result.schema === 'network.xyo.debug')).toBe(true)
|
|
107
|
-
})
|
|
108
|
-
it('only return single payload of that schema (asc)', async () => {
|
|
109
|
-
const schemas = ['network.xyo.debug']
|
|
110
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
111
|
-
.fields({
|
|
112
|
-
limit: 1, order: 'asc', schemas,
|
|
113
|
-
})
|
|
114
|
-
.build()
|
|
115
|
-
const results = await sut.divine([query])
|
|
116
|
-
expect(results.length).toBe(1)
|
|
117
|
-
expect(PayloadBuilder.omitStorageMeta(results[0])).toStrictEqual(payloadB)
|
|
118
|
-
expect(await PayloadBuilder.dataHash(results[0])).toBe(await PayloadBuilder.dataHash(payloadB))
|
|
119
|
-
expect(results.every(result => result.schema === 'network.xyo.debug')).toBe(true)
|
|
120
|
-
})
|
|
121
|
-
})
|
|
122
|
-
describe('multiple', () => {
|
|
123
|
-
it('only returns payloads of that schema', async () => {
|
|
124
|
-
const schemas = ['network.xyo.test', 'network.xyo.debug']
|
|
125
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
126
|
-
.fields({ schemas })
|
|
127
|
-
.build()
|
|
128
|
-
const results = await sut.divine([query])
|
|
129
|
-
expect(results.length).toBeGreaterThan(0)
|
|
130
|
-
expect(results.every(result => schemas.includes(result.schema))).toBe(true)
|
|
131
|
-
})
|
|
132
|
-
})
|
|
133
|
-
describe('paging', () => {
|
|
134
|
-
it('test paging with multiple calls (asc)', async () => {
|
|
135
|
-
const schemas = ['network.xyo.test', 'network.xyo.debug']
|
|
136
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
137
|
-
.fields({
|
|
138
|
-
limit: 2, order: 'asc', schemas,
|
|
139
|
-
})
|
|
140
|
-
.build()
|
|
141
|
-
const results = await sut.divine([query])
|
|
142
|
-
console.warn('results', results)
|
|
143
|
-
const resultSequences = results.map(result => result._sequence)
|
|
144
|
-
expect(PayloadBuilder.omitStorageMeta(results)).toStrictEqual([payloadA, payloadB])
|
|
145
|
-
expect(results.length).toBe(2)
|
|
146
|
-
expect(resultSequences[0]).toBe(insertedPayloads[0]._sequence)
|
|
147
|
-
expect(resultSequences[1]).toBe(insertedPayloads[1]._sequence)
|
|
148
|
-
|
|
149
|
-
const cursor = resultSequences[1]
|
|
150
|
-
const query2 = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
151
|
-
.fields({
|
|
152
|
-
limit: 2, cursor, order: 'asc', schemas,
|
|
153
|
-
})
|
|
154
|
-
.build()
|
|
155
|
-
const results2 = await sut.divine([query2])
|
|
156
|
-
const resultSequences2 = results2.map(result => result._sequence)
|
|
157
|
-
expect(results2.length).toBe(2)
|
|
158
|
-
expect(resultSequences2[0]).toBe(insertedPayloads[2]._sequence)
|
|
159
|
-
expect(resultSequences2[1]).toBe(insertedPayloads[3]._sequence)
|
|
160
|
-
const cursor2 = resultSequences2[1]
|
|
161
|
-
|
|
162
|
-
const query3 = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
163
|
-
.fields({
|
|
164
|
-
limit: 2, cursor: cursor2, order: 'asc', schemas,
|
|
165
|
-
})
|
|
166
|
-
.build()
|
|
167
|
-
const results3 = await sut.divine([query3])
|
|
168
|
-
expect(results3).toBeArrayOfSize(0)
|
|
169
|
-
})
|
|
170
|
-
it('test paging with multiple calls (desc)', async () => {
|
|
171
|
-
const schemas = ['network.xyo.test', 'network.xyo.debug']
|
|
172
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
173
|
-
.fields({
|
|
174
|
-
limit: 2, order: 'desc', schemas,
|
|
175
|
-
})
|
|
176
|
-
.build()
|
|
177
|
-
const results = await sut.divine([query])
|
|
178
|
-
const resultSequences = results.map(result => result._sequence)
|
|
179
|
-
expect(results.length).toBe(2)
|
|
180
|
-
expect(resultSequences[0]).toBe(insertedPayloads[3]._sequence)
|
|
181
|
-
expect(resultSequences[1]).toBe(insertedPayloads[2]._sequence)
|
|
182
|
-
|
|
183
|
-
const cursor = resultSequences[1]
|
|
184
|
-
const query2 = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
185
|
-
.fields({
|
|
186
|
-
limit: 2, cursor, order: 'desc', schemas,
|
|
187
|
-
})
|
|
188
|
-
.build()
|
|
189
|
-
const results2 = await sut.divine([query2])
|
|
190
|
-
const resultSequences2 = results2.map(result => result._sequence)
|
|
191
|
-
expect(results2.length).toBe(2)
|
|
192
|
-
expect(resultSequences2[0]).toBe(insertedPayloads[1]._sequence)
|
|
193
|
-
expect(resultSequences2[1]).toBe(insertedPayloads[0]._sequence)
|
|
194
|
-
const cursor2 = resultSequences2[1]
|
|
195
|
-
|
|
196
|
-
const query3 = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
197
|
-
.fields({
|
|
198
|
-
limit: 2, cursor: cursor2, order: 'desc', schemas,
|
|
199
|
-
})
|
|
200
|
-
.build()
|
|
201
|
-
const results3 = await sut.divine([query3])
|
|
202
|
-
expect(results3).toBeArrayOfSize(0)
|
|
203
|
-
})
|
|
204
|
-
})
|
|
205
|
-
})
|
|
206
|
-
describe('custom field', () => {
|
|
207
|
-
describe('property', () => {
|
|
208
|
-
it('only returns payloads with that property', async () => {
|
|
209
|
-
type WithUrl = { url?: string }
|
|
210
|
-
const url = payloadA.url
|
|
211
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload & WithUrl>({ schema: PayloadDivinerQuerySchema })
|
|
212
|
-
.fields({ url })
|
|
213
|
-
.build()
|
|
214
|
-
const results = await sut.divine([query])
|
|
215
|
-
expect(results.length).toBeGreaterThan(0)
|
|
216
|
-
expect(results.every(result => (result as WithUrl)?.url === url)).toBe(true)
|
|
217
|
-
})
|
|
218
|
-
})
|
|
219
|
-
describe('array', () => {
|
|
220
|
-
const cases: string[][] = [['bar'], ['baz'], ['bar', 'baz']]
|
|
221
|
-
it.each(cases)('only returns payloads that have an array containing all the values supplied', async (...foo) => {
|
|
222
|
-
type WithFoo = { foo?: string[] }
|
|
223
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload & WithFoo>({ schema: PayloadDivinerQuerySchema })
|
|
224
|
-
.fields({ foo })
|
|
225
|
-
.build()
|
|
226
|
-
const results = await sut.divine([query])
|
|
227
|
-
expect(results.length).toBeGreaterThan(0)
|
|
228
|
-
expect(results.every(result => foo.every(v => (result as unknown as WithFoo)?.foo?.includes(v)))).toBe(true)
|
|
229
|
-
})
|
|
230
|
-
})
|
|
231
|
-
})
|
|
232
|
-
})
|
|
233
|
-
describe('on events', () => {
|
|
234
|
-
it ('should listen to clear event', async () => {
|
|
235
|
-
// clear the memory archivist before we test
|
|
236
|
-
await archivist.clear()
|
|
237
|
-
// insert a single payload
|
|
238
|
-
const payload: Payload = { schema: 'network.xyo.test' }
|
|
239
|
-
await archivist.insert([payload])
|
|
240
|
-
// build query for a single schema
|
|
241
|
-
const query = new PayloadBuilder<PayloadDivinerQueryPayload>({ schema: PayloadDivinerQuerySchema })
|
|
242
|
-
.fields({ schemas: ['network.xyo.test'] })
|
|
243
|
-
.build()
|
|
244
|
-
// divine the payload
|
|
245
|
-
const results = await sut.divine([query])
|
|
246
|
-
// should be one but has two because of some payload inserted before cleared the archivist
|
|
247
|
-
// but not the diviner cache
|
|
248
|
-
expect(results).toBeArrayOfSize(1)
|
|
249
|
-
})
|
|
250
|
-
})
|
|
251
|
-
})
|