@xyo-network/diviner-temporal-indexing-memory 5.1.22 → 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 +40 -37
- package/dist/browser/DivinerQueryToIndexQueryDiviner/spec/Diviner.spec.d.ts +0 -2
- package/dist/browser/DivinerQueryToIndexQueryDiviner/spec/Diviner.spec.d.ts.map +0 -1
- package/dist/browser/IndexCandidateToIndexDiviner/spec/Diviner.spec.d.ts +0 -2
- package/dist/browser/IndexCandidateToIndexDiviner/spec/Diviner.spec.d.ts.map +0 -1
- package/dist/browser/IndexQueryResponseToDivinerQueryResponseDiviner/spec/Diviner.spec.d.ts +0 -2
- package/dist/browser/IndexQueryResponseToDivinerQueryResponseDiviner/spec/Diviner.spec.d.ts.map +0 -1
- package/dist/browser/StateToIndexCandidateDiviner/spec/Diviner.spec.d.ts +0 -2
- package/dist/browser/StateToIndexCandidateDiviner/spec/Diviner.spec.d.ts.map +0 -1
- package/dist/browser/spec/Diviner.Multiple.spec.d.ts +0 -2
- package/dist/browser/spec/Diviner.Multiple.spec.d.ts.map +0 -1
- package/dist/browser/spec/Diviner.spec.d.ts +0 -2
- package/dist/browser/spec/Diviner.spec.d.ts.map +0 -1
- package/dist/neutral/DivinerQueryToIndexQueryDiviner/spec/Diviner.spec.d.ts +0 -2
- package/dist/neutral/DivinerQueryToIndexQueryDiviner/spec/Diviner.spec.d.ts.map +0 -1
- package/dist/neutral/IndexCandidateToIndexDiviner/spec/Diviner.spec.d.ts +0 -2
- package/dist/neutral/IndexCandidateToIndexDiviner/spec/Diviner.spec.d.ts.map +0 -1
- package/dist/neutral/IndexQueryResponseToDivinerQueryResponseDiviner/spec/Diviner.spec.d.ts +0 -2
- package/dist/neutral/IndexQueryResponseToDivinerQueryResponseDiviner/spec/Diviner.spec.d.ts.map +0 -1
- package/dist/neutral/StateToIndexCandidateDiviner/spec/Diviner.spec.d.ts +0 -2
- package/dist/neutral/StateToIndexCandidateDiviner/spec/Diviner.spec.d.ts.map +0 -1
- package/dist/neutral/spec/Diviner.Multiple.spec.d.ts +0 -2
- package/dist/neutral/spec/Diviner.Multiple.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/dist/node/DivinerQueryToIndexQueryDiviner/spec/Diviner.spec.d.ts +0 -2
- package/dist/node/DivinerQueryToIndexQueryDiviner/spec/Diviner.spec.d.ts.map +0 -1
- package/dist/node/IndexCandidateToIndexDiviner/spec/Diviner.spec.d.ts +0 -2
- package/dist/node/IndexCandidateToIndexDiviner/spec/Diviner.spec.d.ts.map +0 -1
- package/dist/node/IndexQueryResponseToDivinerQueryResponseDiviner/spec/Diviner.spec.d.ts +0 -2
- package/dist/node/IndexQueryResponseToDivinerQueryResponseDiviner/spec/Diviner.spec.d.ts.map +0 -1
- package/dist/node/StateToIndexCandidateDiviner/spec/Diviner.spec.d.ts +0 -2
- package/dist/node/StateToIndexCandidateDiviner/spec/Diviner.spec.d.ts.map +0 -1
- package/dist/node/spec/Diviner.Multiple.spec.d.ts +0 -2
- package/dist/node/spec/Diviner.Multiple.spec.d.ts.map +0 -1
- package/dist/node/spec/Diviner.spec.d.ts +0 -2
- package/dist/node/spec/Diviner.spec.d.ts.map +0 -1
- package/src/DivinerQueryToIndexQueryDiviner/spec/Diviner.spec.ts +0 -244
- package/src/IndexCandidateToIndexDiviner/spec/Diviner.spec.ts +0 -247
- package/src/IndexQueryResponseToDivinerQueryResponseDiviner/spec/Diviner.spec.ts +0 -103
- package/src/StateToIndexCandidateDiviner/spec/Diviner.spec.ts +0 -167
- package/src/spec/Diviner.Multiple.spec.ts +0 -214
- package/src/spec/Diviner.spec.ts +0 -225
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
import '@xylabs/vitest-extended'
|
|
2
|
-
|
|
3
|
-
import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'
|
|
4
|
-
import type { BoundWitness } from '@xyo-network/boundwitness-model'
|
|
5
|
-
import type { SchemaToJsonPathTransformExpressionsDictionary } from '@xyo-network/diviner-jsonpath-model'
|
|
6
|
-
import { isTemporalIndexingDivinerResultIndex } from '@xyo-network/diviner-temporal-indexing-model'
|
|
7
|
-
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
8
|
-
import type { Payload } from '@xyo-network/payload-model'
|
|
9
|
-
import type { TimeStamp } from '@xyo-network/witness-timestamp'
|
|
10
|
-
import { TimestampSchema } from '@xyo-network/witness-timestamp'
|
|
11
|
-
import {
|
|
12
|
-
beforeAll,
|
|
13
|
-
describe, expect, it,
|
|
14
|
-
} from 'vitest'
|
|
15
|
-
|
|
16
|
-
import { TemporalIndexingDivinerIndexCandidateToIndexDiviner } from '../Diviner.ts'
|
|
17
|
-
|
|
18
|
-
type ImageThumbnail = Payload<{
|
|
19
|
-
http?: {
|
|
20
|
-
ipAddress?: string
|
|
21
|
-
status?: number
|
|
22
|
-
}
|
|
23
|
-
// schema: 'network.xyo.image.thumbnail'
|
|
24
|
-
sourceUrl: string
|
|
25
|
-
url?: string
|
|
26
|
-
}>
|
|
27
|
-
|
|
28
|
-
describe('TemporalIndexCandidateToImageThumbnailIndexDiviner', () => {
|
|
29
|
-
describe('divine', () => {
|
|
30
|
-
const timestampA = 1_234_567_890
|
|
31
|
-
const timestampPayloadA: TimeStamp = { schema: TimestampSchema, timestamp: timestampA }
|
|
32
|
-
const imageThumbnailPayloadA: ImageThumbnail = {
|
|
33
|
-
http: { status: 200 },
|
|
34
|
-
schema: 'network.xyo.image.thumbnail',
|
|
35
|
-
sourceUrl: 'https://xyo.network',
|
|
36
|
-
url: 'data',
|
|
37
|
-
}
|
|
38
|
-
const timestampB = 1_234_567_891
|
|
39
|
-
const timestampPayloadB: TimeStamp = { schema: TimestampSchema, timestamp: timestampB }
|
|
40
|
-
const imageThumbnailPayloadB: ImageThumbnail = {
|
|
41
|
-
http: { status: 500 },
|
|
42
|
-
schema: 'network.xyo.image.thumbnail',
|
|
43
|
-
sourceUrl: 'https://xyo.network',
|
|
44
|
-
}
|
|
45
|
-
const timestampC = 1_234_567_892
|
|
46
|
-
const timestampPayloadC: TimeStamp = { schema: TimestampSchema, timestamp: timestampC }
|
|
47
|
-
const imageThumbnailPayloadC: ImageThumbnail = {
|
|
48
|
-
|
|
49
|
-
http: { ipAddress: '192.169.1.1' },
|
|
50
|
-
schema: 'network.xyo.image.thumbnail',
|
|
51
|
-
sourceUrl: 'https://www.google.com',
|
|
52
|
-
}
|
|
53
|
-
describe('with single schema transform', () => {
|
|
54
|
-
const validateSingleResult = async (
|
|
55
|
-
input: [boundWitness: BoundWitness, timestamp: TimeStamp, thumbnail: ImageThumbnail],
|
|
56
|
-
result: Payload[],
|
|
57
|
-
) => {
|
|
58
|
-
const [boundWitness, timestamp, thumbnail] = input
|
|
59
|
-
const payloadDictionary = await PayloadBuilder.toHashMap([boundWitness, timestamp, thumbnail])
|
|
60
|
-
expect(result).toBeArrayOfSize(1)
|
|
61
|
-
expect(result.filter(isTemporalIndexingDivinerResultIndex)).toBeArrayOfSize(1)
|
|
62
|
-
const index = result.find(isTemporalIndexingDivinerResultIndex)
|
|
63
|
-
// eslint-disable-next-line sonarjs/no-alphabetical-sort
|
|
64
|
-
expect(index?.$sources.toSorted()).toEqual(Object.keys(payloadDictionary).toSorted())
|
|
65
|
-
expect(index?.timestamp).toBe(timestamp.timestamp)
|
|
66
|
-
expect((index as { url?: string })?.url).toBe(thumbnail.sourceUrl)
|
|
67
|
-
expect((index as { status?: number })?.status).toBe(thumbnail.http?.status)
|
|
68
|
-
}
|
|
69
|
-
beforeAll(async () => {
|
|
70
|
-
diviner = await TemporalIndexingDivinerIndexCandidateToIndexDiviner.create({
|
|
71
|
-
account: 'random',
|
|
72
|
-
config: {
|
|
73
|
-
schema: TemporalIndexingDivinerIndexCandidateToIndexDiviner.defaultConfigSchema,
|
|
74
|
-
schemaTransforms,
|
|
75
|
-
},
|
|
76
|
-
})
|
|
77
|
-
})
|
|
78
|
-
const schemaTransforms: SchemaToJsonPathTransformExpressionsDictionary = {
|
|
79
|
-
'network.xyo.image.thumbnail': [
|
|
80
|
-
{ destinationField: 'url', sourcePathExpression: '$.sourceUrl' },
|
|
81
|
-
{ destinationField: 'status', sourcePathExpression: '$.http.status' },
|
|
82
|
-
],
|
|
83
|
-
'network.xyo.timestamp': [{ destinationField: 'timestamp', sourcePathExpression: '$.timestamp' }],
|
|
84
|
-
}
|
|
85
|
-
let diviner: TemporalIndexingDivinerIndexCandidateToIndexDiviner
|
|
86
|
-
|
|
87
|
-
const cases: [TimeStamp, ImageThumbnail][] = [
|
|
88
|
-
[timestampPayloadA, imageThumbnailPayloadA],
|
|
89
|
-
[timestampPayloadB, imageThumbnailPayloadB],
|
|
90
|
-
[timestampPayloadC, imageThumbnailPayloadC],
|
|
91
|
-
]
|
|
92
|
-
describe('with single result', () => {
|
|
93
|
-
it.each(cases)('transforms single result', async (timestamp, thumbnail) => {
|
|
94
|
-
const [boundWitness] = await new BoundWitnessBuilder().payloads([timestamp, thumbnail]).build()
|
|
95
|
-
const result = await diviner.divine([boundWitness, timestamp, thumbnail])
|
|
96
|
-
await validateSingleResult([boundWitness, timestamp, thumbnail], result)
|
|
97
|
-
})
|
|
98
|
-
it('transforms BW with multiple results inside', async () => {
|
|
99
|
-
const payloads = cases.flat()
|
|
100
|
-
const [boundWitness] = await new BoundWitnessBuilder().payloads(payloads).build()
|
|
101
|
-
const results = await diviner.divine([boundWitness, ...payloads])
|
|
102
|
-
expect(results).toBeArrayOfSize(Math.pow(cases.length, cases[0].length))
|
|
103
|
-
let resultIndex = 0
|
|
104
|
-
for (let i = 0; i < cases.length; i++) {
|
|
105
|
-
const thumbnail = cases[i][1]
|
|
106
|
-
// eslint-disable-next-line unicorn/no-for-loop
|
|
107
|
-
for (let j = 0; j < cases.length; j++) {
|
|
108
|
-
const timestamp = cases[j][0]
|
|
109
|
-
const result = results[resultIndex]
|
|
110
|
-
await validateSingleResult([boundWitness, timestamp, thumbnail], [result])
|
|
111
|
-
resultIndex++
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
})
|
|
115
|
-
it.each(cases)('handles sparse inputs', async (thumbnail, timestamp) => {
|
|
116
|
-
const [boundWitness] = await new BoundWitnessBuilder().payloads([timestamp, thumbnail]).build()
|
|
117
|
-
expect(await diviner.divine([thumbnail, timestamp])).toBeArrayOfSize(0)
|
|
118
|
-
expect(await diviner.divine([boundWitness, timestamp])).toBeArrayOfSize(0)
|
|
119
|
-
expect(await diviner.divine([boundWitness, thumbnail])).toBeArrayOfSize(0)
|
|
120
|
-
})
|
|
121
|
-
})
|
|
122
|
-
describe('with multiple results', () => {
|
|
123
|
-
it('transforms multiple results', async () => {
|
|
124
|
-
const data: [BoundWitness, TimeStamp, ImageThumbnail][] = await Promise.all(
|
|
125
|
-
cases.map(async (payloads) => {
|
|
126
|
-
const [bw] = await new BoundWitnessBuilder().payloads(payloads).build()
|
|
127
|
-
return [bw, ...payloads]
|
|
128
|
-
}),
|
|
129
|
-
)
|
|
130
|
-
const results = await diviner.divine(data.flat())
|
|
131
|
-
expect(results).toBeArrayOfSize(cases.length)
|
|
132
|
-
|
|
133
|
-
await Promise.all(
|
|
134
|
-
data.map(async (input, i) => {
|
|
135
|
-
const result = results[i]
|
|
136
|
-
await validateSingleResult(input, [result])
|
|
137
|
-
}),
|
|
138
|
-
)
|
|
139
|
-
})
|
|
140
|
-
it('handles sparse inputs', async () => {
|
|
141
|
-
const [bw] = await new BoundWitnessBuilder().payloads(cases[0]).build()
|
|
142
|
-
const results = await diviner.divine([bw, ...cases.flat()])
|
|
143
|
-
expect(results).toBeArrayOfSize(1)
|
|
144
|
-
await validateSingleResult([bw, ...cases[0]], results)
|
|
145
|
-
})
|
|
146
|
-
it('handles missing inputs', async () => {
|
|
147
|
-
const [bw] = await new BoundWitnessBuilder().payloads([...cases[0]]).build()
|
|
148
|
-
const results = await diviner.divine([bw, ...cases[0].slice(0, -1)])
|
|
149
|
-
expect(results).toBeArrayOfSize(0)
|
|
150
|
-
})
|
|
151
|
-
})
|
|
152
|
-
})
|
|
153
|
-
describe('with multiple schema transforms', () => {
|
|
154
|
-
const validateMultiResult = async (
|
|
155
|
-
input: [boundWitness: BoundWitness, timestamp: TimeStamp, thumbnail: ImageThumbnail, payload: Payload],
|
|
156
|
-
result: Payload[],
|
|
157
|
-
) => {
|
|
158
|
-
const [boundWitness, timestamp, thumbnail, payload] = input
|
|
159
|
-
const payloadDictionary = await PayloadBuilder.toHashMap([boundWitness, timestamp, thumbnail, payload])
|
|
160
|
-
expect(result).toBeArrayOfSize(1)
|
|
161
|
-
expect(result.filter(isTemporalIndexingDivinerResultIndex)).toBeArrayOfSize(1)
|
|
162
|
-
const index = result.find(isTemporalIndexingDivinerResultIndex)
|
|
163
|
-
// eslint-disable-next-line sonarjs/no-alphabetical-sort
|
|
164
|
-
expect(index?.$sources.toSorted()).toEqual(Object.keys(payloadDictionary).toSorted())
|
|
165
|
-
expect(index?.timestamp).toBe(timestamp.timestamp)
|
|
166
|
-
expect((index as { url?: string })?.url).toBe((payload as { sourceUrl?: string }).sourceUrl)
|
|
167
|
-
expect((index as { status?: number })?.status).toBe(thumbnail.http?.status)
|
|
168
|
-
}
|
|
169
|
-
beforeAll(async () => {
|
|
170
|
-
diviner = await TemporalIndexingDivinerIndexCandidateToIndexDiviner.create({
|
|
171
|
-
account: 'random',
|
|
172
|
-
config: {
|
|
173
|
-
schema: TemporalIndexingDivinerIndexCandidateToIndexDiviner.defaultConfigSchema,
|
|
174
|
-
schemaTransforms,
|
|
175
|
-
},
|
|
176
|
-
})
|
|
177
|
-
})
|
|
178
|
-
const schemaTransforms: SchemaToJsonPathTransformExpressionsDictionary = {
|
|
179
|
-
'network.xyo.image.thumbnail': [{ destinationField: 'status', sourcePathExpression: '$.http.status' }],
|
|
180
|
-
'network.xyo.image.thumbnail.other': [{ destinationField: 'url', sourcePathExpression: '$.sourceUrl' }],
|
|
181
|
-
'network.xyo.timestamp': [{ destinationField: 'timestamp', sourcePathExpression: '$.timestamp' }],
|
|
182
|
-
}
|
|
183
|
-
let diviner: TemporalIndexingDivinerIndexCandidateToIndexDiviner
|
|
184
|
-
|
|
185
|
-
const cases: [TimeStamp, ImageThumbnail, Payload<{ sourceUrl: string }>][] = [
|
|
186
|
-
[
|
|
187
|
-
timestampPayloadA,
|
|
188
|
-
{ ...imageThumbnailPayloadA, sourceUrl: '' },
|
|
189
|
-
{ schema: 'network.xyo.image.thumbnail.other', sourceUrl: imageThumbnailPayloadA.sourceUrl },
|
|
190
|
-
],
|
|
191
|
-
[
|
|
192
|
-
timestampPayloadB,
|
|
193
|
-
{ ...imageThumbnailPayloadB, sourceUrl: '' },
|
|
194
|
-
{ schema: 'network.xyo.image.thumbnail.other', sourceUrl: imageThumbnailPayloadB.sourceUrl },
|
|
195
|
-
],
|
|
196
|
-
[
|
|
197
|
-
timestampPayloadC,
|
|
198
|
-
{ ...imageThumbnailPayloadC, sourceUrl: '' },
|
|
199
|
-
{ schema: 'network.xyo.image.thumbnail.other', sourceUrl: imageThumbnailPayloadC.sourceUrl },
|
|
200
|
-
],
|
|
201
|
-
]
|
|
202
|
-
describe('with single result', () => {
|
|
203
|
-
it.each(cases)('transforms single result', async (timestamp, thumbnail, payload) => {
|
|
204
|
-
const [boundWitness] = await new BoundWitnessBuilder().payloads([timestamp, thumbnail, payload]).build()
|
|
205
|
-
const result = await diviner.divine([boundWitness, timestamp, thumbnail, payload])
|
|
206
|
-
await validateMultiResult([boundWitness, timestamp, thumbnail, payload], result)
|
|
207
|
-
})
|
|
208
|
-
it.each(cases)('handles sparse inputs', async (thumbnail, timestamp, payload) => {
|
|
209
|
-
const [boundWitness] = await new BoundWitnessBuilder().payloads([timestamp, thumbnail, payload]).build()
|
|
210
|
-
expect(await diviner.divine([thumbnail, timestamp])).toBeArrayOfSize(0)
|
|
211
|
-
expect(await diviner.divine([boundWitness, timestamp])).toBeArrayOfSize(0)
|
|
212
|
-
expect(await diviner.divine([boundWitness, thumbnail])).toBeArrayOfSize(0)
|
|
213
|
-
})
|
|
214
|
-
})
|
|
215
|
-
describe('with multiple results', () => {
|
|
216
|
-
it('transforms multiple results', async () => {
|
|
217
|
-
const data: [BoundWitness, TimeStamp, ImageThumbnail, Payload][] = await Promise.all(
|
|
218
|
-
cases.map(async (payloads) => {
|
|
219
|
-
const [bw] = await new BoundWitnessBuilder().payloads(payloads).build()
|
|
220
|
-
return [bw, ...payloads]
|
|
221
|
-
}),
|
|
222
|
-
)
|
|
223
|
-
const results = await diviner.divine(data.flat())
|
|
224
|
-
expect(results).toBeArrayOfSize(cases.length)
|
|
225
|
-
|
|
226
|
-
await Promise.all(
|
|
227
|
-
data.map(async (input, i) => {
|
|
228
|
-
const result = results[i]
|
|
229
|
-
await validateMultiResult(input, [result])
|
|
230
|
-
}),
|
|
231
|
-
)
|
|
232
|
-
})
|
|
233
|
-
it('handles sparse inputs', async () => {
|
|
234
|
-
const [bw] = await new BoundWitnessBuilder().payloads(cases[0]).build()
|
|
235
|
-
const results = await diviner.divine([bw, ...cases.flat()])
|
|
236
|
-
expect(results).toBeArrayOfSize(1)
|
|
237
|
-
await validateMultiResult([bw, ...cases[0]], results)
|
|
238
|
-
})
|
|
239
|
-
it('handles missing inputs', async () => {
|
|
240
|
-
const [bw] = await new BoundWitnessBuilder().payloads([...cases[0]]).build()
|
|
241
|
-
const results = await diviner.divine([bw, ...cases[0].slice(0, -1)])
|
|
242
|
-
expect(results).toBeArrayOfSize(0)
|
|
243
|
-
})
|
|
244
|
-
})
|
|
245
|
-
})
|
|
246
|
-
})
|
|
247
|
-
})
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import '@xylabs/vitest-extended'
|
|
2
|
-
|
|
3
|
-
import type { PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'
|
|
4
|
-
import { PayloadDivinerQuerySchema } from '@xyo-network/diviner-payload-model'
|
|
5
|
-
import type { Payload } from '@xyo-network/payload-model'
|
|
6
|
-
import {
|
|
7
|
-
beforeAll,
|
|
8
|
-
describe, expect, it,
|
|
9
|
-
} from 'vitest'
|
|
10
|
-
|
|
11
|
-
import { TemporalIndexingDivinerIndexQueryResponseToDivinerQueryResponseDiviner } from '../Diviner.ts'
|
|
12
|
-
|
|
13
|
-
type QueryType = Payload<PayloadDivinerQueryPayload & Payload<{ status?: number; success?: boolean; url: string }>>
|
|
14
|
-
|
|
15
|
-
describe('TemporalIndexingDivinerIndexQueryResponseToDivinerQueryResponseDiviner', () => {
|
|
16
|
-
const url = 'https://xyo.network'
|
|
17
|
-
const queries: QueryType[] = [
|
|
18
|
-
{
|
|
19
|
-
schema: PayloadDivinerQuerySchema,
|
|
20
|
-
url,
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
schema: PayloadDivinerQuerySchema,
|
|
24
|
-
url,
|
|
25
|
-
},
|
|
26
|
-
]
|
|
27
|
-
const indexes = [
|
|
28
|
-
[
|
|
29
|
-
{
|
|
30
|
-
schema: 'TODO',
|
|
31
|
-
sources: [],
|
|
32
|
-
status: 200,
|
|
33
|
-
success: true,
|
|
34
|
-
timestamp: 1_234_567_890,
|
|
35
|
-
url,
|
|
36
|
-
},
|
|
37
|
-
],
|
|
38
|
-
[
|
|
39
|
-
{
|
|
40
|
-
schema: 'TODO',
|
|
41
|
-
sources: [],
|
|
42
|
-
status: 200,
|
|
43
|
-
success: true,
|
|
44
|
-
timestamp: 1_234_567_891,
|
|
45
|
-
url,
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
schema: 'TODO',
|
|
49
|
-
sources: [],
|
|
50
|
-
status: 500,
|
|
51
|
-
success: false,
|
|
52
|
-
timestamp: 1_234_567_892,
|
|
53
|
-
url,
|
|
54
|
-
},
|
|
55
|
-
],
|
|
56
|
-
]
|
|
57
|
-
let diviner: TemporalIndexingDivinerIndexQueryResponseToDivinerQueryResponseDiviner
|
|
58
|
-
beforeAll(async () => {
|
|
59
|
-
diviner = await TemporalIndexingDivinerIndexQueryResponseToDivinerQueryResponseDiviner.create({ account: 'random' })
|
|
60
|
-
})
|
|
61
|
-
const cases: [QueryType, Payload[]][] = queries.map((query, i) => [query, indexes[i]])
|
|
62
|
-
describe('divine', () => {
|
|
63
|
-
describe('with single url in index result', () => {
|
|
64
|
-
it.each(cases)('transforms single url index results', async (imageThumbnailDivinerQuery, imageThumbnailResultIndex) => {
|
|
65
|
-
const results = await diviner.divine([imageThumbnailDivinerQuery, ...imageThumbnailResultIndex])
|
|
66
|
-
expect(results).toBeArrayOfSize(imageThumbnailResultIndex.length)
|
|
67
|
-
expect(results).toBeArrayOfSize(imageThumbnailResultIndex.length)
|
|
68
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, unicorn/no-array-for-each
|
|
69
|
-
results.forEach((result: any, i) => {
|
|
70
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
71
|
-
const index = imageThumbnailResultIndex[i] as any
|
|
72
|
-
expect(result.url).toBe(imageThumbnailDivinerQuery.url)
|
|
73
|
-
expect(result.success).toBe(index.success)
|
|
74
|
-
expect(result.timestamp).toBe(index.timestamp)
|
|
75
|
-
expect(result.status).toBe(index.status)
|
|
76
|
-
expect(result.schema).toBe('TODO')
|
|
77
|
-
})
|
|
78
|
-
})
|
|
79
|
-
})
|
|
80
|
-
describe('with multiple urls in index result', () => {
|
|
81
|
-
it('transforms multiple url index results', async () => {
|
|
82
|
-
const indexesLength = indexes.flat().length
|
|
83
|
-
const results = await diviner.divine([...queries, ...indexes.flat()])
|
|
84
|
-
expect(results).toBeArrayOfSize(indexesLength)
|
|
85
|
-
expect(results).toBeArrayOfSize(indexesLength)
|
|
86
|
-
let resultsIterator = 0
|
|
87
|
-
for (const [i, { url }] of queries.entries()) {
|
|
88
|
-
const indexSet = indexes[i]
|
|
89
|
-
for (const index of indexSet) {
|
|
90
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
91
|
-
const result = results[resultsIterator] as any
|
|
92
|
-
expect(result.url).toBe(url)
|
|
93
|
-
expect(result.success).toBe(index.success)
|
|
94
|
-
expect(result.timestamp).toBe(index.timestamp)
|
|
95
|
-
expect(result.status).toBe(index.status)
|
|
96
|
-
expect(result.schema).toBe('TODO')
|
|
97
|
-
resultsIterator = ++resultsIterator
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
})
|
|
101
|
-
})
|
|
102
|
-
})
|
|
103
|
-
})
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
import '@xylabs/vitest-extended'
|
|
2
|
-
|
|
3
|
-
import { filterAs } from '@xylabs/array'
|
|
4
|
-
import { assertEx } from '@xylabs/assert'
|
|
5
|
-
import { delay } from '@xylabs/delay'
|
|
6
|
-
import type { MemoryArchivist } from '@xyo-network/archivist-memory'
|
|
7
|
-
import { asArchivistInstance } from '@xyo-network/archivist-model'
|
|
8
|
-
import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'
|
|
9
|
-
import { isBoundWitness } from '@xyo-network/boundwitness-model'
|
|
10
|
-
import type { IndexingDivinerState } from '@xyo-network/diviner-indexing-model'
|
|
11
|
-
import { asDivinerInstance } from '@xyo-network/diviner-model'
|
|
12
|
-
import type { PackageManifestPayload } from '@xyo-network/manifest'
|
|
13
|
-
import { ManifestWrapper } from '@xyo-network/manifest'
|
|
14
|
-
import { ModuleFactoryLocator } from '@xyo-network/module-factory-locator'
|
|
15
|
-
import type { ModuleState } from '@xyo-network/module-model'
|
|
16
|
-
import { isModuleState, ModuleStateSchema } from '@xyo-network/module-model'
|
|
17
|
-
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
18
|
-
import type { Payload, WithStorageMeta } from '@xyo-network/payload-model'
|
|
19
|
-
import { asOptionalStorageMeta } from '@xyo-network/payload-model'
|
|
20
|
-
import { HDWallet } from '@xyo-network/wallet'
|
|
21
|
-
import type { TimeStamp } from '@xyo-network/witness-timestamp'
|
|
22
|
-
import { isTimestamp, TimestampSchema } from '@xyo-network/witness-timestamp'
|
|
23
|
-
import {
|
|
24
|
-
beforeAll, describe, expect, it,
|
|
25
|
-
} from 'vitest'
|
|
26
|
-
|
|
27
|
-
import { TemporalIndexingDivinerStateToIndexCandidateDiviner } from '../Diviner.ts'
|
|
28
|
-
import TemporalStateToIndexCandidateDivinerManifest from './TemporalStateToIndexCandidateDiviner.json' with { type: 'json' }
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @group slow
|
|
32
|
-
*/
|
|
33
|
-
describe('TemporalStateToIndexCandidateDiviner', () => {
|
|
34
|
-
const sourceUrl = 'https://placekitten.com/200/300'
|
|
35
|
-
const thumbnailHttpSuccess = {
|
|
36
|
-
http: { status: 200 },
|
|
37
|
-
schema: 'network.xyo.image.thumbnail',
|
|
38
|
-
sourceHash: '7f39363514d9d9b958a5a993edeba35cb44f912c7072ed9ddd628728ac0fd681',
|
|
39
|
-
sourceUrl,
|
|
40
|
-
url: 'data:image/png;base64,===',
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const thumbnailHttpFail = {
|
|
44
|
-
http: {
|
|
45
|
-
|
|
46
|
-
ipAddress: '104.17.96.13',
|
|
47
|
-
status: 429,
|
|
48
|
-
},
|
|
49
|
-
schema: 'network.xyo.image.thumbnail',
|
|
50
|
-
sourceUrl,
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const thumbnailCodeFail = {
|
|
54
|
-
http: { code: 'FAILED' },
|
|
55
|
-
schema: 'network.xyo.image.thumbnail',
|
|
56
|
-
sourceUrl,
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const thumbnailWitnessFail = {
|
|
60
|
-
|
|
61
|
-
http: { ipAddress: '104.17.96.13' },
|
|
62
|
-
schema: 'network.xyo.image.thumbnail',
|
|
63
|
-
sourceUrl,
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
let testCases: WithStorageMeta<Payload>[][] = []
|
|
67
|
-
let archivist: MemoryArchivist
|
|
68
|
-
let sut: TemporalIndexingDivinerStateToIndexCandidateDiviner
|
|
69
|
-
|
|
70
|
-
beforeAll(async () => {
|
|
71
|
-
const wallet = await HDWallet.random()
|
|
72
|
-
const locator = new ModuleFactoryLocator()
|
|
73
|
-
locator.register(TemporalIndexingDivinerStateToIndexCandidateDiviner.factory())
|
|
74
|
-
const manifest = TemporalStateToIndexCandidateDivinerManifest as PackageManifestPayload
|
|
75
|
-
const manifestWrapper = new ManifestWrapper(manifest, wallet, locator)
|
|
76
|
-
const node = await manifestWrapper.loadNodeFromIndex(0)
|
|
77
|
-
await node.start()
|
|
78
|
-
|
|
79
|
-
const privateModules = manifest.nodes[0].modules?.private ?? []
|
|
80
|
-
const publicModules = manifest.nodes[0].modules?.public ?? []
|
|
81
|
-
const mods = await node.resolve('*')
|
|
82
|
-
expect(mods.length).toBe(privateModules.length + publicModules.length + 1)
|
|
83
|
-
|
|
84
|
-
// Insert previously witnessed payloads into thumbnail archivist
|
|
85
|
-
const httpSuccessTimestamp: TimeStamp = { schema: TimestampSchema, timestamp: 1 }
|
|
86
|
-
const [httpSuccessBoundWitness, httpSuccessPayloads] = await new BoundWitnessBuilder()
|
|
87
|
-
.payloads([thumbnailHttpSuccess, httpSuccessTimestamp])
|
|
88
|
-
.build()
|
|
89
|
-
const httpFailTimestamp: TimeStamp = { schema: TimestampSchema, timestamp: 2 }
|
|
90
|
-
const [httpFailBoundWitness, httpFailPayloads] = await (new BoundWitnessBuilder().payloads([thumbnailHttpFail, httpFailTimestamp])).build()
|
|
91
|
-
|
|
92
|
-
const witnessFailTimestamp: TimeStamp = { schema: TimestampSchema, timestamp: 3 }
|
|
93
|
-
const [witnessFailBoundWitness, witnessFailPayloads] = await new BoundWitnessBuilder()
|
|
94
|
-
.payloads([thumbnailWitnessFail, witnessFailTimestamp])
|
|
95
|
-
.build()
|
|
96
|
-
|
|
97
|
-
const codeFailTimestamp: TimeStamp = { schema: TimestampSchema, timestamp: 4 }
|
|
98
|
-
const [codeFailBoundWitness, codeFailPayloads] = await (new BoundWitnessBuilder().payloads([thumbnailCodeFail, codeFailTimestamp])).build()
|
|
99
|
-
|
|
100
|
-
archivist = assertEx(asArchivistInstance<MemoryArchivist>(await node.resolve('ImageThumbnailArchivist')))
|
|
101
|
-
const testCasesToCreate = [
|
|
102
|
-
[httpSuccessBoundWitness, ...httpSuccessPayloads],
|
|
103
|
-
[httpFailBoundWitness, ...httpFailPayloads],
|
|
104
|
-
[witnessFailBoundWitness, ...witnessFailPayloads],
|
|
105
|
-
[codeFailBoundWitness, ...codeFailPayloads],
|
|
106
|
-
]
|
|
107
|
-
|
|
108
|
-
for (const [bw, ...payloads] of testCasesToCreate) {
|
|
109
|
-
const createdTestCase = []
|
|
110
|
-
for (const payload of [bw, ...payloads]) {
|
|
111
|
-
await delay(2)
|
|
112
|
-
const [signedPayload] = await archivist.insert([payload])
|
|
113
|
-
createdTestCase.push(signedPayload)
|
|
114
|
-
}
|
|
115
|
-
testCases.push(createdTestCase)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
sut = assertEx(
|
|
119
|
-
asDivinerInstance(await node.resolve('TemporalStateToIndexCandidateDiviner')),
|
|
120
|
-
) as TemporalIndexingDivinerStateToIndexCandidateDiviner
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
describe('divine', () => {
|
|
124
|
-
describe('with no previous state', () => {
|
|
125
|
-
it('returns next state and batch results', async () => {
|
|
126
|
-
const results = await sut.divine()
|
|
127
|
-
expect(results.length).toBe(testCases.flat().length + 1)
|
|
128
|
-
const state = results.find(isModuleState<IndexingDivinerState>)
|
|
129
|
-
expect(state).toBeDefined()
|
|
130
|
-
const last = filterAs(results, asOptionalStorageMeta)
|
|
131
|
-
.map(p => p as WithStorageMeta<Payload>)
|
|
132
|
-
.toSorted(PayloadBuilder.compareStorageMeta)
|
|
133
|
-
.at(-1)
|
|
134
|
-
expect(last).toBeDefined()
|
|
135
|
-
expect(state?.state.cursor).toBe(last?._sequence)
|
|
136
|
-
})
|
|
137
|
-
})
|
|
138
|
-
describe('with previous state', () => {
|
|
139
|
-
it.each([1, 2, 3])('returns next state and batch results', async (batch) => {
|
|
140
|
-
const all = (await archivist.all()).toSorted(PayloadBuilder.compareStorageMeta)
|
|
141
|
-
const batchOffset = all.at((3 * batch) - 1)
|
|
142
|
-
expect(batchOffset).toBeDefined()
|
|
143
|
-
// Test across all offsets
|
|
144
|
-
const cursor = assertEx(batchOffset)._sequence
|
|
145
|
-
const lastState: ModuleState<IndexingDivinerState> = { schema: ModuleStateSchema, state: { cursor } }
|
|
146
|
-
const results = await sut.divine([lastState])
|
|
147
|
-
|
|
148
|
-
// Validate expected results length
|
|
149
|
-
// [BW, ImageThumbnail, TimeStamp] + 1 [ModuleState]
|
|
150
|
-
const expectedResults = testCases.slice(batch).flat().length + 1
|
|
151
|
-
expect(results.length).toBe(expectedResults)
|
|
152
|
-
|
|
153
|
-
// Validate expected state
|
|
154
|
-
const nextState = results.find(isModuleState<IndexingDivinerState>)
|
|
155
|
-
expect(nextState).toBeDefined()
|
|
156
|
-
expect(nextState?.state?.cursor).toBeDefined()
|
|
157
|
-
expect(nextState?.state.cursor).toBe(testCases?.at(-1)?.at(-1)?._sequence)
|
|
158
|
-
|
|
159
|
-
// Validate expected individual results
|
|
160
|
-
const bws = results.filter(isBoundWitness)
|
|
161
|
-
expect(bws.length).toBeGreaterThan(0)
|
|
162
|
-
const timestamps = results.filter(isTimestamp)
|
|
163
|
-
expect(timestamps.length).toBeGreaterThan(0)
|
|
164
|
-
})
|
|
165
|
-
})
|
|
166
|
-
})
|
|
167
|
-
})
|