@coasys/ad4m 0.13.1-staging.13 → 0.13.1-staging.14
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/lib/index.cjs +548 -477
- package/lib/index.js +548 -473
- package/lib/src/agent/AgentClient.d.ts +9 -8
- package/lib/src/agent/AgentClient.js +46 -67
- package/lib/src/cache/KeyedCache.d.ts +51 -0
- package/lib/src/cache/KeyedCache.js +142 -0
- package/lib/src/expression/ExpressionClient.d.ts +14 -3
- package/lib/src/expression/ExpressionClient.js +49 -111
- package/lib/src/generated/api/ExpressionLookupError.d.ts +7 -0
- package/lib/src/generated/api/ExpressionLookupError.js +2 -0
- package/lib/src/generated/api/ExpressionManyEntry.d.ts +6 -0
- package/lib/src/generated/api/ExpressionManyEntry.js +1 -0
- package/lib/src/generated/api/FlowFiredEntry.d.ts +11 -0
- package/lib/src/generated/api/FlowFiredEntry.js +2 -0
- package/lib/src/generated/api/PerspectiveFlowFiredParams.d.ts +3 -3
- package/lib/src/generated/api/RpcMethods.d.ts +4 -2
- package/lib/src/model/Ad4mModel.d.ts +26 -2
- package/lib/src/model/Ad4mModel.js +125 -82
- package/lib/src/model/ModelQueryBuilder.d.ts +3 -0
- package/lib/src/model/ModelQueryBuilder.js +42 -14
- package/lib/src/model/index.d.ts +0 -1
- package/lib/src/model/index.js +0 -1
- package/lib/src/model/query-common.d.ts +8 -0
- package/lib/src/model/query-common.js +12 -0
- package/lib/src/model/types.d.ts +12 -3
- package/lib/src/perspectives/FlowModels.js +1 -1
- package/lib/src/perspectives/LiveQuery.d.ts +1 -1
- package/lib/src/perspectives/PerspectiveClient.d.ts +17 -6
- package/lib/src/perspectives/PerspectiveClient.js +83 -44
- package/lib/src/perspectives/PerspectiveProxy.d.ts +19 -10
- package/lib/src/perspectives/PerspectiveProxy.js +31 -69
- package/package.json +2 -2
- package/src/Ad4mClient.test.ts +17 -6
- package/src/agent/AgentClient.ts +46 -74
- package/src/cache/KeyedCache.test.ts +162 -0
- package/src/cache/KeyedCache.ts +179 -0
- package/src/cache/cache.test.ts +294 -71
- package/src/expression/ExpressionClient.ts +46 -131
- package/src/generated/api/ExpressionLookupError.ts +6 -0
- package/src/generated/api/ExpressionManyEntry.ts +8 -0
- package/src/generated/api/FlowFiredEntry.ts +11 -0
- package/src/generated/api/PerspectiveFlowFiredParams.ts +3 -3
- package/src/generated/api/RpcMethods.ts +4 -2
- package/src/model/Ad4mModel.test.ts +237 -43
- package/src/model/Ad4mModel.ts +132 -98
- package/src/model/ModelQueryBuilder.ts +41 -15
- package/src/model/count-result-type.test.ts +72 -0
- package/src/model/index.ts +0 -1
- package/src/model/query-common.ts +13 -0
- package/src/model/relation-filtering.test.ts +3 -5
- package/src/model/types.ts +9 -3
- package/src/model/write-round-trips.test.ts +30 -6
- package/src/perspectives/FlowModels.ts +1 -1
- package/src/perspectives/LiveQuery.ts +1 -1
- package/src/perspectives/PerspectiveClient.test.ts +287 -0
- package/src/perspectives/PerspectiveClient.ts +101 -66
- package/src/perspectives/PerspectiveProxy.test.ts +6 -14
- package/src/perspectives/PerspectiveProxy.ts +33 -67
- package/src/perspectives/TextContent.test.ts +0 -16
- package/src/perspectives/shacl-rpc.test.ts +1 -1
- package/lib/src/model/query-cache.d.ts +0 -30
- package/lib/src/model/query-cache.js +0 -66
- package/src/model/query-cache.test.ts +0 -54
- package/src/model/query-cache.ts +0 -79
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Ad4mModel } from "./Ad4mModel";
|
|
2
2
|
import { HasMany, Model, Property } from "./decorators";
|
|
3
3
|
import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
|
|
4
|
-
import { clearQueryCache } from "./query-cache";
|
|
5
4
|
|
|
6
5
|
// Every RPC that save() and delete() send, counted on a client double that
|
|
7
6
|
// keeps the registered classes' SHACL links like the executor does.
|
|
@@ -113,8 +112,6 @@ async function createNote(proxy: PerspectiveProxy) {
|
|
|
113
112
|
}
|
|
114
113
|
|
|
115
114
|
describe("RPCs per write", () => {
|
|
116
|
-
beforeEach(() => clearQueryCache());
|
|
117
|
-
|
|
118
115
|
it("create of a decorated model sends no class-list lookup", async () => {
|
|
119
116
|
const { proxy, calls } = countingProxy([RoundTripNote]);
|
|
120
117
|
await createNote(proxy);
|
|
@@ -187,6 +184,36 @@ describe("RPCs per write", () => {
|
|
|
187
184
|
expect(commands[1]).toContain("collectionSetter");
|
|
188
185
|
});
|
|
189
186
|
|
|
187
|
+
it("create still clears an empty relation whose predicate an adder envelope in the same save wrote to", async () => {
|
|
188
|
+
// `tags` writes through `{action: 'adder'}` before `labels` (same predicate)
|
|
189
|
+
// is reached, so `labels: []` has links to clear.
|
|
190
|
+
const { proxy, sent } = countingProxy([RoundTripShared]);
|
|
191
|
+
const shared = new RoundTripShared(proxy);
|
|
192
|
+
shared.title = "standup";
|
|
193
|
+
shared.tags = { action: "adder", value: ["rt://tag/a"] } as any;
|
|
194
|
+
shared.labels = [];
|
|
195
|
+
await shared.save();
|
|
196
|
+
expect(commandsSent(sent).some((c) => c.includes("collectionSetter") && c.includes("rt://tag"))).toBe(true);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("create still clears an empty relation whose predicate a scalar relation write in the same save wrote to", async () => {
|
|
200
|
+
// A scalar on `tags` is written as a one-member relation before `labels`
|
|
201
|
+
// (same predicate) is reached, so `labels: []` has a link to clear. The
|
|
202
|
+
// scalar's own write is a collectionSetter on rt://tag too, so look for
|
|
203
|
+
// the clear: the one with no values.
|
|
204
|
+
const { proxy, sent } = countingProxy([RoundTripShared]);
|
|
205
|
+
const shared = new RoundTripShared(proxy);
|
|
206
|
+
shared.title = "standup";
|
|
207
|
+
shared.tags = "rt://tag/a" as any;
|
|
208
|
+
shared.labels = [];
|
|
209
|
+
await shared.save();
|
|
210
|
+
const clears = sent.filter(
|
|
211
|
+
([method, , commands, , params]) =>
|
|
212
|
+
method === "executeCommands" && commands.includes("collectionSetter") && commands.includes("rt://tag") && params === "[]",
|
|
213
|
+
);
|
|
214
|
+
expect(clears).toHaveLength(1);
|
|
215
|
+
});
|
|
216
|
+
|
|
190
217
|
it("update of an existing record with [] still clears the relation", async () => {
|
|
191
218
|
const { proxy, calls, sent } = countingProxy([RoundTripCall]);
|
|
192
219
|
const call = new RoundTripCall(proxy, "rt://call/1");
|
|
@@ -210,8 +237,6 @@ describe("RPCs per write", () => {
|
|
|
210
237
|
});
|
|
211
238
|
|
|
212
239
|
describe("class name a write resolves", () => {
|
|
213
|
-
beforeEach(() => clearQueryCache());
|
|
214
|
-
|
|
215
240
|
@Model({ name: "RoundTripDerivedNote" })
|
|
216
241
|
class RoundTripDerivedNote extends RoundTripNote {
|
|
217
242
|
@Property({ through: "rt://body" })
|
|
@@ -301,7 +326,6 @@ const rtt = Number(process.env.SDK_RTT_MS || 0);
|
|
|
301
326
|
const ms: number[] = [];
|
|
302
327
|
let rpcs = 0;
|
|
303
328
|
for (let i = 0; i < runs; i++) {
|
|
304
|
-
clearQueryCache();
|
|
305
329
|
const { proxy, calls } = countingProxy([RoundTripNote, RoundTripReadable, RoundTripCall], rtt);
|
|
306
330
|
const start = performance.now();
|
|
307
331
|
await write(proxy);
|
|
@@ -161,7 +161,7 @@ export class FlowTransitionProposal extends Ad4mModel {
|
|
|
161
161
|
|
|
162
162
|
// Whether this proposal settled an edge is not on the record: the
|
|
163
163
|
// executor keeps that per agent, outside the graph (#1424). Ask
|
|
164
|
-
// `PerspectiveProxy.flowFired(
|
|
164
|
+
// `PerspectiveProxy.flowFired(instances)` for the proposals your replica
|
|
165
165
|
// recorded as fired, or listen with `onFlowStateChanged`.
|
|
166
166
|
|
|
167
167
|
// "When was this proposal written?" is answered by `Ad4mModel`'s built-in
|
|
@@ -10,7 +10,7 @@ export interface Subscribed {
|
|
|
10
10
|
/**
|
|
11
11
|
* A `query-subscription-update` event: the change from the previous revision.
|
|
12
12
|
* Model results carry `ids` (the new order), `upsert` (new or changed
|
|
13
|
-
* instances) and `totalCount`; query results carry `added` / `removed` rows;
|
|
13
|
+
* instances) and, when the query set `count: true`, `totalCount`; query results carry `added` / `removed` rows;
|
|
14
14
|
* a result that could not be diffed comes whole as `result`.
|
|
15
15
|
*/
|
|
16
16
|
export interface QueryUpdate {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { PerspectiveClient } from "./PerspectiveClient"
|
|
2
2
|
import { PerspectiveHandle, PerspectiveState } from "./PerspectiveHandle"
|
|
3
|
+
import { PerspectiveProxy } from "./PerspectiveProxy"
|
|
3
4
|
import { RpcError } from "../apiClient"
|
|
4
5
|
import { LinkQuery } from "./LinkQuery"
|
|
5
6
|
|
|
@@ -244,6 +245,292 @@ describe('PerspectiveClient RPC operations', () => {
|
|
|
244
245
|
})
|
|
245
246
|
})
|
|
246
247
|
|
|
248
|
+
describe('PerspectiveProxy.querySparql shares in-flight calls', () => {
|
|
249
|
+
const QUERY = 'SELECT ?s WHERE { ?s ?p ?o }'
|
|
250
|
+
|
|
251
|
+
function sparqlCalls() {
|
|
252
|
+
return mockCall.mock.calls.filter(([method]) => method === 'perspective.querySparql').length
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function makeProxy(client = new PerspectiveClient('http://localhost:12000', 'token'), uuid = 'uuid-s') {
|
|
256
|
+
return new PerspectiveProxy(makeHandle(uuid, 'S'), client)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
beforeEach(() => {
|
|
260
|
+
mockCall.mockReset().mockResolvedValue(JSON.stringify([{ s: 'a' }]))
|
|
261
|
+
mockOn.mockReset().mockReturnValue(() => {})
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
it('two concurrent identical calls make one RPC and return equal results', async () => {
|
|
265
|
+
const proxy = makeProxy()
|
|
266
|
+
const [a, b] = await Promise.all([proxy.querySparql(QUERY), proxy.querySparql(QUERY)])
|
|
267
|
+
expect(sparqlCalls()).toBe(1)
|
|
268
|
+
expect(a).toEqual([{ s: 'a' }])
|
|
269
|
+
expect(b).toEqual(a)
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
it('shares across proxies of the same perspective on one client', async () => {
|
|
273
|
+
const client = new PerspectiveClient('http://localhost:12000', 'token')
|
|
274
|
+
await Promise.all([makeProxy(client).querySparql(QUERY), makeProxy(client).querySparql(QUERY)])
|
|
275
|
+
expect(sparqlCalls()).toBe(1)
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
it('gives each caller its own result object', async () => {
|
|
279
|
+
const proxy = makeProxy()
|
|
280
|
+
const [a, b] = await Promise.all([proxy.querySparql(QUERY), proxy.querySparql(QUERY)])
|
|
281
|
+
expect(a).not.toBe(b)
|
|
282
|
+
a[0].s = 'changed'
|
|
283
|
+
expect(b).toEqual([{ s: 'a' }])
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
it('a call after the first settled makes a new RPC and sees the new result', async () => {
|
|
287
|
+
const proxy = makeProxy()
|
|
288
|
+
await proxy.querySparql(QUERY)
|
|
289
|
+
mockCall.mockResolvedValue(JSON.stringify([{ s: 'b' }]))
|
|
290
|
+
expect(await proxy.querySparql(QUERY)).toEqual([{ s: 'b' }])
|
|
291
|
+
expect(sparqlCalls()).toBe(2)
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
it('does not share between perspectives, queries or clients', async () => {
|
|
295
|
+
const client = new PerspectiveClient('http://localhost:12000', 'token')
|
|
296
|
+
await Promise.all([
|
|
297
|
+
makeProxy(client, 'p1').querySparql(QUERY),
|
|
298
|
+
makeProxy(client, 'p2').querySparql(QUERY),
|
|
299
|
+
makeProxy(client, 'p1').querySparql(QUERY + ' LIMIT 1'),
|
|
300
|
+
makeProxy(undefined, 'p1').querySparql(QUERY),
|
|
301
|
+
])
|
|
302
|
+
expect(sparqlCalls()).toBe(4)
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
it('a failed call rejects every sharer, and the next call retries', async () => {
|
|
306
|
+
const proxy = makeProxy()
|
|
307
|
+
mockCall.mockRejectedValueOnce(new Error('boom'))
|
|
308
|
+
const results = await Promise.allSettled([proxy.querySparql(QUERY), proxy.querySparql(QUERY)])
|
|
309
|
+
expect(results.map(r => r.status)).toEqual(['rejected', 'rejected'])
|
|
310
|
+
expect(await proxy.querySparql(QUERY)).toEqual([{ s: 'a' }])
|
|
311
|
+
expect(sparqlCalls()).toBe(2)
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
it('a call with an AbortSignal makes its own RPC, so aborting it cannot fail another caller', async () => {
|
|
315
|
+
const proxy = makeProxy()
|
|
316
|
+
const controller = new AbortController()
|
|
317
|
+
await Promise.all([proxy.querySparql(QUERY), proxy.querySparql(QUERY, { signal: controller.signal })])
|
|
318
|
+
expect(sparqlCalls()).toBe(2)
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
// A query sent before a write answers with the state before it, so the
|
|
322
|
+
// same query started after `await proxy.add(...)` must not join it.
|
|
323
|
+
function executorAnsweringAtSendTime() {
|
|
324
|
+
let rows = [{ s: 'old' }]
|
|
325
|
+
const pending: Array<() => void> = []
|
|
326
|
+
mockCall.mockImplementation((method: string) => {
|
|
327
|
+
if (method === 'perspective.querySparql') {
|
|
328
|
+
const answer = JSON.stringify(rows)
|
|
329
|
+
return new Promise(resolve => pending.push(() => resolve(answer)))
|
|
330
|
+
}
|
|
331
|
+
rows = [{ s: 'old' }, { s: 'new' }]
|
|
332
|
+
const added = { author: 'did:test', timestamp: '2026-01-01T00:00:00Z', data: { source: 'a', target: 'new' }, proof: { key: 'k', signature: 's' } }
|
|
333
|
+
const replies: Record<string, unknown> = {
|
|
334
|
+
'perspective.addLinks': [added],
|
|
335
|
+
'perspective.removeLinks': [added],
|
|
336
|
+
'perspective.linkMutations': { additions: [added], removals: [] },
|
|
337
|
+
'perspective.commitBatch': { additions: [added], removals: [] },
|
|
338
|
+
'perspective.addSdna': true,
|
|
339
|
+
'perspective.rejectFlowProposal': { retractedLinks: 1 },
|
|
340
|
+
}
|
|
341
|
+
return Promise.resolve(method in replies ? replies[method] : added)
|
|
342
|
+
})
|
|
343
|
+
return { settleQueries: () => pending.splice(0).forEach(resolve => resolve()) }
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
it('a call started after a write makes its own RPC and reads the write back', async () => {
|
|
347
|
+
const executor = executorAnsweringAtSendTime()
|
|
348
|
+
const proxy = makeProxy()
|
|
349
|
+
const before = proxy.querySparql(QUERY)
|
|
350
|
+
await proxy.add({ source: 'a', target: 'new' })
|
|
351
|
+
const after = proxy.querySparql(QUERY)
|
|
352
|
+
executor.settleQueries()
|
|
353
|
+
expect(await before).toEqual([{ s: 'old' }])
|
|
354
|
+
expect(await after).toEqual([{ s: 'old' }, { s: 'new' }])
|
|
355
|
+
expect(sparqlCalls()).toBe(2)
|
|
356
|
+
})
|
|
357
|
+
|
|
358
|
+
it('calls waiting before a write keep sharing their RPC', async () => {
|
|
359
|
+
const executor = executorAnsweringAtSendTime()
|
|
360
|
+
const proxy = makeProxy()
|
|
361
|
+
const first = proxy.querySparql(QUERY)
|
|
362
|
+
const second = proxy.querySparql(QUERY)
|
|
363
|
+
await proxy.add({ source: 'a', target: 'new' })
|
|
364
|
+
executor.settleQueries()
|
|
365
|
+
expect(await first).toEqual([{ s: 'old' }])
|
|
366
|
+
expect(await second).toEqual([{ s: 'old' }])
|
|
367
|
+
expect(sparqlCalls()).toBe(1)
|
|
368
|
+
})
|
|
369
|
+
|
|
370
|
+
it('a write to another perspective does not split the sharing', async () => {
|
|
371
|
+
const executor = executorAnsweringAtSendTime()
|
|
372
|
+
const client = new PerspectiveClient('http://localhost:12000', 'token')
|
|
373
|
+
const first = makeProxy(client, 'p1').querySparql(QUERY)
|
|
374
|
+
await makeProxy(client, 'p2').add({ source: 'a', target: 'new' })
|
|
375
|
+
const second = makeProxy(client, 'p1').querySparql(QUERY)
|
|
376
|
+
executor.settleQueries()
|
|
377
|
+
await Promise.all([first, second])
|
|
378
|
+
expect(sparqlCalls()).toBe(1)
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
it('every write path of the client starts a new generation', async () => {
|
|
382
|
+
const client = new PerspectiveClient('http://localhost:12000', 'token')
|
|
383
|
+
const link = { source: 'a', target: 'new' }
|
|
384
|
+
const stored = { author: 'did:test', timestamp: '2026-01-01T00:00:00Z', data: link, proof: { key: 'k', signature: 's' } } as any
|
|
385
|
+
const writes: Array<() => Promise<unknown>> = [
|
|
386
|
+
() => client.addLink('u', link),
|
|
387
|
+
() => client.addLinks('u', [link]),
|
|
388
|
+
() => client.removeLinks('u', [stored]),
|
|
389
|
+
() => client.linkMutations('u', { additions: [link], removals: [] }),
|
|
390
|
+
() => client.addLinkExpression('u', stored),
|
|
391
|
+
() => client.updateLink('u', stored, link),
|
|
392
|
+
() => client.removeLink('u', stored),
|
|
393
|
+
() => client.executeCommands('u', '[]', 'expr', '[]'),
|
|
394
|
+
() => client.createSubject('u', 'C', 'expr'),
|
|
395
|
+
() => client.updateSubject('u', 'C', 'expr', '{}'),
|
|
396
|
+
() => client.removeSubject('u', 'C', 'expr'),
|
|
397
|
+
() => client.commitBatch('u', 'b'),
|
|
398
|
+
// Executor methods whose handlers write links themselves (SHACL
|
|
399
|
+
// and SDNA links, overlays, flow proposals, votes, fired marks,
|
|
400
|
+
// receipts, processor configs).
|
|
401
|
+
() => client.addSdna('u', 'C', undefined, 'subject_class', '{}'),
|
|
402
|
+
() => client.addSdnaBatch('u', [{ name: 'C', sdnaType: 'subject_class', shaclJson: '{}' }]),
|
|
403
|
+
() => client.runInterpretation('u', [], 'base://'),
|
|
404
|
+
() => client.runInterpretationWithHarness('u', [], 'base://', 5),
|
|
405
|
+
() => client.runAgent('u', [], { writeTools: ['add_link'] }),
|
|
406
|
+
() => client.addAutoProcessor('u', { processorId: 'p' } as any),
|
|
407
|
+
() => client.removeAutoProcessor('u', 'p'),
|
|
408
|
+
() => client.acceptInterpretation('u', 'base://1'),
|
|
409
|
+
() => client.rejectInterpretation('u', 'base://1'),
|
|
410
|
+
() => client.proposeFlowTransition('u', 'flow://1', 'done'),
|
|
411
|
+
() => client.acceptFlowProposal('u', 'proposal://1'),
|
|
412
|
+
() => client.rejectFlowProposal('u', 'proposal://1'),
|
|
413
|
+
() => client.mintFlowReceipt('u', 'flow://1'),
|
|
414
|
+
// The validated instance writes.
|
|
415
|
+
() => client.instanceCreate('u', { className: 'C', properties: {} } as any),
|
|
416
|
+
() => client.instanceUpdate('u', { className: 'C', baseUri: 'expr', properties: {} } as any),
|
|
417
|
+
() => client.instanceRemove('u', { className: 'C', baseUri: 'expr' } as any),
|
|
418
|
+
() => client.instanceAddToCollection('u', { className: 'C', baseUri: 'expr', collection: 'c', itemUri: 'i' } as any),
|
|
419
|
+
() => client.instanceRemoveFromCollection('u', { className: 'C', baseUri: 'expr', collection: 'c', itemUri: 'i' } as any),
|
|
420
|
+
]
|
|
421
|
+
for (const write of writes) {
|
|
422
|
+
const executor = executorAnsweringAtSendTime()
|
|
423
|
+
mockCall.mockClear()
|
|
424
|
+
const before = client.querySparql('u', QUERY)
|
|
425
|
+
await write()
|
|
426
|
+
const after = client.querySparql('u', QUERY)
|
|
427
|
+
executor.settleQueries()
|
|
428
|
+
expect([write.toString(), await after]).toEqual([write.toString(), [{ s: 'old' }, { s: 'new' }]])
|
|
429
|
+
await before
|
|
430
|
+
expect([write.toString(), sparqlCalls()]).toEqual([write.toString(), 2])
|
|
431
|
+
}
|
|
432
|
+
})
|
|
433
|
+
|
|
434
|
+
it('a write that fails still starts a new generation', async () => {
|
|
435
|
+
const executor = executorAnsweringAtSendTime()
|
|
436
|
+
const client = new PerspectiveClient('http://localhost:12000', 'token')
|
|
437
|
+
const before = client.querySparql('u', QUERY)
|
|
438
|
+
// The executor may have written part of it before failing.
|
|
439
|
+
mockCall.mockImplementationOnce(() => Promise.reject(new Error('write failed')))
|
|
440
|
+
await expect(client.addLinks('u', [{ source: 'a', target: 'new' }])).rejects.toThrow('write failed')
|
|
441
|
+
const after = client.querySparql('u', QUERY)
|
|
442
|
+
executor.settleQueries()
|
|
443
|
+
await Promise.all([before, after])
|
|
444
|
+
expect(sparqlCalls()).toBe(2)
|
|
445
|
+
})
|
|
446
|
+
|
|
447
|
+
it('the generation moves when the write settles, not when it starts', async () => {
|
|
448
|
+
let rows = [{ s: 'old' }]
|
|
449
|
+
const pendingQueries: Array<() => void> = []
|
|
450
|
+
let settleWrite = () => {}
|
|
451
|
+
const added = { author: 'did:test', timestamp: '2026-01-01T00:00:00Z', data: { source: 'a', target: 'new' }, proof: { key: 'k', signature: 's' } }
|
|
452
|
+
mockCall.mockImplementation((method: string) => {
|
|
453
|
+
if (method === 'perspective.querySparql') {
|
|
454
|
+
const answer = JSON.stringify(rows)
|
|
455
|
+
return new Promise(resolve => pendingQueries.push(() => resolve(answer)))
|
|
456
|
+
}
|
|
457
|
+
return new Promise(resolve => {
|
|
458
|
+
settleWrite = () => { rows = [{ s: 'old' }, { s: 'new' }]; resolve([added]) }
|
|
459
|
+
})
|
|
460
|
+
})
|
|
461
|
+
const client = new PerspectiveClient('http://localhost:12000', 'token')
|
|
462
|
+
const write = client.addLinks('u', [{ source: 'a', target: 'new' }])
|
|
463
|
+
// Sent while the write is still running: may answer before it lands.
|
|
464
|
+
const during = client.querySparql('u', QUERY)
|
|
465
|
+
settleWrite()
|
|
466
|
+
await write
|
|
467
|
+
const after = client.querySparql('u', QUERY)
|
|
468
|
+
pendingQueries.splice(0).forEach(resolve => resolve())
|
|
469
|
+
expect(await during).toEqual([{ s: 'old' }])
|
|
470
|
+
expect(await after).toEqual([{ s: 'old' }, { s: 'new' }])
|
|
471
|
+
expect(sparqlCalls()).toBe(2)
|
|
472
|
+
})
|
|
473
|
+
|
|
474
|
+
it('a read-only agent session keeps the sharing', async () => {
|
|
475
|
+
const executor = executorAnsweringAtSendTime()
|
|
476
|
+
const client = new PerspectiveClient('http://localhost:12000', 'token')
|
|
477
|
+
const first = client.querySparql('u', QUERY)
|
|
478
|
+
await client.runAgent('u', [])
|
|
479
|
+
const second = client.querySparql('u', QUERY)
|
|
480
|
+
executor.settleQueries()
|
|
481
|
+
await Promise.all([first, second])
|
|
482
|
+
expect(sparqlCalls()).toBe(1)
|
|
483
|
+
})
|
|
484
|
+
})
|
|
485
|
+
|
|
486
|
+
describe('PerspectiveProxy.getAllShacl reads shapes in one RPC', () => {
|
|
487
|
+
const shape = (name: string) => ({
|
|
488
|
+
name,
|
|
489
|
+
shapeUri: `app://${name}Shape`,
|
|
490
|
+
links: [{ source: `app://${name}Shape`, predicate: 'sh://targetClass', target: `app://${name}` }],
|
|
491
|
+
})
|
|
492
|
+
|
|
493
|
+
beforeEach(() => {
|
|
494
|
+
mockCall.mockReset()
|
|
495
|
+
mockOn.mockReset().mockReturnValue(() => {})
|
|
496
|
+
})
|
|
497
|
+
|
|
498
|
+
function proxy() {
|
|
499
|
+
return new PerspectiveProxy(makeHandle('uuid-shacl', 'S'), new PerspectiveClient('http://localhost:12000', 'token'))
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
it('getAllShacl() makes one perspective.getAllShacl RPC with no name filter', async () => {
|
|
503
|
+
mockCall.mockResolvedValue([shape('A'), shape('B')])
|
|
504
|
+
const shapes = await proxy().getAllShacl()
|
|
505
|
+
expect(mockCall.mock.calls).toEqual([['perspective.getAllShacl', { uuid: 'uuid-shacl' }]])
|
|
506
|
+
expect(shapes.map(s => [s.name, s.shape.targetClass])).toEqual([['A', 'app://A'], ['B', 'app://B']])
|
|
507
|
+
})
|
|
508
|
+
|
|
509
|
+
it('getAllShacl({ names }) sends the names in the same single RPC', async () => {
|
|
510
|
+
mockCall.mockResolvedValue([shape('B')])
|
|
511
|
+
const shapes = await proxy().getAllShacl({ names: ['B', 'Missing'] })
|
|
512
|
+
expect(mockCall.mock.calls).toEqual([['perspective.getAllShacl', { uuid: 'uuid-shacl', names: ['B', 'Missing'] }]])
|
|
513
|
+
expect(shapes.map(s => s.name)).toEqual(['B'])
|
|
514
|
+
})
|
|
515
|
+
})
|
|
516
|
+
|
|
517
|
+
describe('PerspectiveProxy.flowFired reads many instances in one RPC (P9)', () => {
|
|
518
|
+
beforeEach(() => {
|
|
519
|
+
mockCall.mockReset()
|
|
520
|
+
mockOn.mockReset().mockReturnValue(() => {})
|
|
521
|
+
})
|
|
522
|
+
|
|
523
|
+
it('the fired marks of 20 runs take one perspective.flowFired RPC', async () => {
|
|
524
|
+
const runs = Array.from({ length: 20 }, (_, i) => `flow://run/${i}`)
|
|
525
|
+
const entries = runs.map((instance, i) => ({ instance, fired: i % 2 ? [`${instance}/p`] : [] }))
|
|
526
|
+
mockCall.mockResolvedValue(entries)
|
|
527
|
+
const proxy = new PerspectiveProxy(makeHandle('uuid-flow', 'F'), new PerspectiveClient('http://localhost:12000', 'token'))
|
|
528
|
+
|
|
529
|
+
expect(await proxy.flowFired(runs)).toEqual(entries)
|
|
530
|
+
expect(mockCall.mock.calls).toEqual([['perspective.flowFired', { uuid: 'uuid-flow', instances: runs }]])
|
|
531
|
+
})
|
|
532
|
+
})
|
|
533
|
+
|
|
247
534
|
/**
|
|
248
535
|
* Unit tests for PerspectiveProxy utility functions.
|
|
249
536
|
*/
|