@coasys/ad4m 0.13.0-test-2 → 0.13.0-test-4
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/README.md +2 -25
- package/jest-setup.ts +1 -1
- package/lib/index.cjs +2805 -10556
- package/lib/index.js +2513 -10322
- package/lib/src/Ad4mClient.d.ts +7 -3
- package/lib/src/Ad4mClient.js +29 -13
- package/lib/src/agent/Agent.js +26 -272
- package/lib/src/agent/AgentClient.d.ts +10 -21
- package/lib/src/agent/AgentClient.js +75 -427
- package/lib/src/agent/AgentStatus.js +2 -37
- package/lib/src/ai/AIClient.d.ts +12 -4
- package/lib/src/ai/AIClient.js +81 -285
- package/lib/src/ai/AITypes.d.ts +55 -0
- package/lib/src/ai/AITypes.js +18 -0
- package/lib/src/ai/Tasks.js +10 -133
- package/lib/src/apiClient.d.ts +52 -0
- package/lib/src/apiClient.js +231 -0
- package/lib/src/expression/Expression.d.ts +1 -1
- package/lib/src/expression/Expression.js +13 -122
- package/lib/src/expression/ExpressionClient.d.ts +3 -3
- package/lib/src/expression/ExpressionClient.js +13 -76
- package/lib/src/expression/ExpressionRef.js +2 -25
- package/lib/src/generated/api/AIModelLoadingStatus.d.ts +7 -0
- package/lib/src/generated/api/AIModelLoadingStatus.js +2 -0
- package/lib/src/generated/api/AddLinkExpressionRequest.d.ts +6 -0
- package/lib/src/generated/api/AddLinkExpressionRequest.js +1 -0
- package/lib/src/generated/api/AddLinkRequest.d.ts +6 -0
- package/lib/src/generated/api/AddLinkRequest.js +1 -0
- package/lib/src/generated/api/AddLinksBulkRequest.d.ts +6 -0
- package/lib/src/generated/api/AddLinksBulkRequest.js +1 -0
- package/lib/src/generated/api/AddSdnaRequest.d.ts +6 -0
- package/lib/src/generated/api/AddSdnaRequest.js +2 -0
- package/lib/src/generated/api/ApplyTemplateRequest.d.ts +4 -0
- package/lib/src/generated/api/ApplyTemplateRequest.js +2 -0
- package/lib/src/generated/api/AuthInfoInput.d.ts +10 -0
- package/lib/src/generated/api/AuthInfoInput.js +1 -0
- package/lib/src/generated/api/CapabilityInput.d.ts +5 -0
- package/lib/src/generated/api/CapabilityInput.js +1 -0
- package/lib/src/generated/api/CommitBatchRequest.d.ts +3 -0
- package/lib/src/generated/api/CommitBatchRequest.js +2 -0
- package/lib/src/generated/api/ComputeLogEntry.d.ts +9 -0
- package/lib/src/generated/api/ComputeLogEntry.js +2 -0
- package/lib/src/generated/api/CreateExpressionRequest.d.ts +4 -0
- package/lib/src/generated/api/CreateExpressionRequest.js +2 -0
- package/lib/src/generated/api/CreatePerspectiveRequest.d.ts +3 -0
- package/lib/src/generated/api/CreatePerspectiveRequest.js +2 -0
- package/lib/src/generated/api/CreateSubjectRequest.d.ts +6 -0
- package/lib/src/generated/api/CreateSubjectRequest.js +2 -0
- package/lib/src/generated/api/EmbedRequest.d.ts +4 -0
- package/lib/src/generated/api/EmbedRequest.js +2 -0
- package/lib/src/generated/api/EntanglementProofInput.d.ts +6 -0
- package/lib/src/generated/api/EntanglementProofInput.js +2 -0
- package/lib/src/generated/api/ExecuteCommandsRequest.d.ts +4 -0
- package/lib/src/generated/api/ExecuteCommandsRequest.js +2 -0
- package/lib/src/generated/api/ExportRequest.d.ts +5 -0
- package/lib/src/generated/api/ExportRequest.js +2 -0
- package/lib/src/generated/api/ExpressionManyRequest.d.ts +3 -0
- package/lib/src/generated/api/ExpressionManyRequest.js +2 -0
- package/lib/src/generated/api/ExpressionProof.d.ts +4 -0
- package/lib/src/generated/api/ExpressionProof.js +2 -0
- package/lib/src/generated/api/ExpressionProofInput.d.ts +6 -0
- package/lib/src/generated/api/ExpressionProofInput.js +2 -0
- package/lib/src/generated/api/FriendSendMessageRequest.d.ts +3 -0
- package/lib/src/generated/api/FriendSendMessageRequest.js +2 -0
- package/lib/src/generated/api/FriendsListRequest.d.ts +3 -0
- package/lib/src/generated/api/FriendsListRequest.js +2 -0
- package/lib/src/generated/api/GenerateAgentRequest.d.ts +3 -0
- package/lib/src/generated/api/GenerateAgentRequest.js +2 -0
- package/lib/src/generated/api/GenerateJwtRequest.d.ts +4 -0
- package/lib/src/generated/api/GenerateJwtRequest.js +2 -0
- package/lib/src/generated/api/GetSubjectDataRequest.d.ts +4 -0
- package/lib/src/generated/api/GetSubjectDataRequest.js +2 -0
- package/lib/src/generated/api/ImportAgentRequest.d.ts +6 -0
- package/lib/src/generated/api/ImportAgentRequest.js +2 -0
- package/lib/src/generated/api/ImportRequest.d.ts +4 -0
- package/lib/src/generated/api/ImportRequest.js +2 -0
- package/lib/src/generated/api/JoinNeighbourhoodRequest.d.ts +3 -0
- package/lib/src/generated/api/JoinNeighbourhoodRequest.js +2 -0
- package/lib/src/generated/api/LanguageMetaInput.d.ts +6 -0
- package/lib/src/generated/api/LanguageMetaInput.js +2 -0
- package/lib/src/generated/api/Link.d.ts +5 -0
- package/lib/src/generated/api/Link.js +2 -0
- package/lib/src/generated/api/LinkExpression.d.ts +10 -0
- package/lib/src/generated/api/LinkExpression.js +1 -0
- package/lib/src/generated/api/LinkExpressionInput.d.ts +10 -0
- package/lib/src/generated/api/LinkExpressionInput.js +1 -0
- package/lib/src/generated/api/LinkInput.d.ts +5 -0
- package/lib/src/generated/api/LinkInput.js +2 -0
- package/lib/src/generated/api/LinkLanguageTemplatesRequest.d.ts +3 -0
- package/lib/src/generated/api/LinkLanguageTemplatesRequest.js +2 -0
- package/lib/src/generated/api/LinkMutations.d.ts +6 -0
- package/lib/src/generated/api/LinkMutations.js +1 -0
- package/lib/src/generated/api/LinkStatus.d.ts +1 -0
- package/lib/src/generated/api/LinkStatus.js +2 -0
- package/lib/src/generated/api/LockAgentRequest.d.ts +3 -0
- package/lib/src/generated/api/LockAgentRequest.js +2 -0
- package/lib/src/generated/api/ModelType.d.ts +1 -0
- package/lib/src/generated/api/ModelType.js +2 -0
- package/lib/src/generated/api/NotificationInput.d.ts +10 -0
- package/lib/src/generated/api/NotificationInput.js +2 -0
- package/lib/src/generated/api/OpenLinkRequest.d.ts +3 -0
- package/lib/src/generated/api/OpenLinkRequest.js +2 -0
- package/lib/src/generated/api/PermitCapabilityRequest.d.ts +3 -0
- package/lib/src/generated/api/PermitCapabilityRequest.js +2 -0
- package/lib/src/generated/api/PerspectiveInput.d.ts +4 -0
- package/lib/src/generated/api/PerspectiveInput.js +1 -0
- package/lib/src/generated/api/PromptRequest.d.ts +4 -0
- package/lib/src/generated/api/PromptRequest.js +2 -0
- package/lib/src/generated/api/PublishLanguageRequest.d.ts +5 -0
- package/lib/src/generated/api/PublishLanguageRequest.js +1 -0
- package/lib/src/generated/api/PublishNeighbourhoodRequest.d.ts +6 -0
- package/lib/src/generated/api/PublishNeighbourhoodRequest.js +1 -0
- package/lib/src/generated/api/QueryRequest.d.ts +7 -0
- package/lib/src/generated/api/QueryRequest.js +2 -0
- package/lib/src/generated/api/RemoveLinkRequest.d.ts +5 -0
- package/lib/src/generated/api/RemoveLinkRequest.js +1 -0
- package/lib/src/generated/api/RemoveLinksBulkRequest.d.ts +5 -0
- package/lib/src/generated/api/RemoveLinksBulkRequest.js +1 -0
- package/lib/src/generated/api/ResourceInput.d.ts +4 -0
- package/lib/src/generated/api/ResourceInput.js +2 -0
- package/lib/src/generated/api/SetDefaultModelRequest.d.ts +4 -0
- package/lib/src/generated/api/SetDefaultModelRequest.js +1 -0
- package/lib/src/generated/api/SetFreeHostingEnabledRequest.d.ts +3 -0
- package/lib/src/generated/api/SetFreeHostingEnabledRequest.js +2 -0
- package/lib/src/generated/api/SetMultiUserRequest.d.ts +3 -0
- package/lib/src/generated/api/SetMultiUserRequest.js +2 -0
- package/lib/src/generated/api/SetStatusRequest.d.ts +3 -0
- package/lib/src/generated/api/SetStatusRequest.js +2 -0
- package/lib/src/generated/api/SignMessageRequest.d.ts +3 -0
- package/lib/src/generated/api/SignMessageRequest.js +2 -0
- package/lib/src/generated/api/TrustedAgentsRequest.d.ts +3 -0
- package/lib/src/generated/api/TrustedAgentsRequest.js +2 -0
- package/lib/src/generated/api/UnlockAgentRequest.d.ts +4 -0
- package/lib/src/generated/api/UnlockAgentRequest.js +2 -0
- package/lib/src/generated/api/UpdateLinkRequest.d.ts +7 -0
- package/lib/src/generated/api/UpdateLinkRequest.js +1 -0
- package/lib/src/generated/api/UpdatePerspectiveRequest.d.ts +3 -0
- package/lib/src/generated/api/UpdatePerspectiveRequest.js +2 -0
- package/lib/src/generated/api/UserStatistics.d.ts +9 -0
- package/lib/src/generated/api/UserStatistics.js +2 -0
- package/lib/src/generated/api/VerificationRequestResult.d.ts +6 -0
- package/lib/src/generated/api/VerificationRequestResult.js +2 -0
- package/lib/src/generated/api/VerifySignatureRequest.d.ts +5 -0
- package/lib/src/generated/api/VerifySignatureRequest.js +2 -0
- package/lib/src/generated/api/WriteSettingsRequest.d.ts +3 -0
- package/lib/src/generated/api/WriteSettingsRequest.js +2 -0
- package/lib/src/generated/api/index.d.ts +42 -0
- package/lib/src/generated/api/index.js +7 -0
- package/lib/src/index.d.ts +6 -2
- package/lib/src/index.js +6 -2
- package/lib/src/jestSetup.d.ts +0 -1
- package/lib/src/jestSetup.js +1 -1
- package/lib/src/language/Icon.js +2 -21
- package/lib/src/language/Language.d.ts +26 -214
- package/lib/src/language/Language.js +32 -72
- package/lib/src/language/LanguageClient.d.ts +2 -2
- package/lib/src/language/LanguageClient.js +15 -105
- package/lib/src/language/LanguageHandle.js +2 -41
- package/lib/src/language/LanguageMeta.js +4 -75
- package/lib/src/language/LanguageRef.js +2 -25
- package/lib/src/links/Links.js +14 -111
- package/lib/src/model/Ad4mModel.d.ts +81 -38
- package/lib/src/model/Ad4mModel.js +651 -408
- package/lib/src/model/Ad4mModel.test.js +488 -750
- package/lib/src/model/ModelQueryBuilder.d.ts +28 -21
- package/lib/src/model/ModelQueryBuilder.js +160 -195
- package/lib/src/model/decorators.d.ts +22 -0
- package/lib/src/model/decorators.js +7 -0
- package/lib/src/model/hydration-batching.test.d.ts +7 -0
- package/lib/src/model/hydration-batching.test.js +456 -0
- package/lib/src/model/hydration.d.ts +8 -60
- package/lib/src/model/hydration.js +41 -541
- package/lib/src/model/index.d.ts +0 -4
- package/lib/src/model/index.js +0 -4
- package/lib/src/model/model-manifest.d.ts +41 -0
- package/lib/src/model/model-manifest.js +11 -0
- package/lib/src/model/query-sparql.d.ts +36 -22
- package/lib/src/model/query-sparql.js +188 -53
- package/lib/src/model/query-sparql.test.d.ts +1 -0
- package/lib/src/model/query-sparql.test.js +312 -0
- package/lib/src/model/query-utils.js +4 -0
- package/lib/src/model/shacl-gen.js +4 -0
- package/lib/src/model/subscription-delta.test.d.ts +24 -0
- package/lib/src/model/subscription-delta.test.js +180 -0
- package/lib/src/model/subscription-pool.js +48 -2
- package/lib/src/model/types.d.ts +58 -1
- package/lib/src/model/types.js +4 -1
- package/lib/src/neighbourhood/Neighbourhood.js +4 -32
- package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +3 -3
- package/lib/src/neighbourhood/NeighbourhoodClient.js +40 -203
- package/lib/src/perspectives/LinkQuery.js +2 -42
- package/lib/src/perspectives/Perspective.d.ts +1 -1
- package/lib/src/perspectives/Perspective.js +10 -52
- package/lib/src/perspectives/PerspectiveClient.d.ts +12 -15
- package/lib/src/perspectives/PerspectiveClient.js +168 -400
- package/lib/src/perspectives/PerspectiveClient.test.d.ts +1 -0
- package/lib/src/perspectives/PerspectiveClient.test.js +224 -0
- package/lib/src/perspectives/PerspectiveDiff.js +4 -31
- package/lib/src/perspectives/PerspectiveHandle.js +3 -43
- package/lib/src/perspectives/PerspectiveProxy.d.ts +95 -71
- package/lib/src/perspectives/PerspectiveProxy.js +288 -310
- package/lib/src/runtime/RuntimeClient.d.ts +6 -6
- package/lib/src/runtime/RuntimeClient.js +93 -467
- package/lib/src/runtime/RuntimeTypes.d.ts +101 -0
- package/lib/src/runtime/RuntimeTypes.js +45 -0
- package/lib/src/shacl/SHACLShape.d.ts +7 -0
- package/lib/src/shacl/SHACLShape.js +19 -0
- package/lib/src/shacl/SHACLShape.test.js +82 -0
- package/lib/src/typeDefs.js +3 -628
- package/package.json +9 -24
- package/rollup.config.js +0 -7
- package/src/Ad4mClient.test.ts +1182 -1288
- package/src/Ad4mClient.ts +30 -15
- package/src/agent/Agent.ts +0 -99
- package/src/agent/AgentClient.ts +112 -526
- package/src/agent/AgentStatus.ts +0 -12
- package/src/ai/AIClient.ts +96 -296
- package/src/ai/AITypes.ts +66 -0
- package/src/ai/Tasks.ts +0 -52
- package/src/apiClient.ts +283 -0
- package/src/expression/Expression.ts +3 -43
- package/src/expression/ExpressionClient.ts +14 -77
- package/src/expression/ExpressionRef.ts +0 -6
- package/src/generated/api/AIModelLoadingStatus.ts +3 -0
- package/src/generated/api/AddLinkExpressionRequest.ts +4 -0
- package/src/generated/api/AddLinkRequest.ts +4 -0
- package/src/generated/api/AddLinksBulkRequest.ts +4 -0
- package/src/generated/api/AddSdnaRequest.ts +3 -0
- package/src/generated/api/ApplyTemplateRequest.ts +3 -0
- package/src/generated/api/AuthInfoInput.ts +4 -0
- package/src/generated/api/CapabilityInput.ts +4 -0
- package/src/generated/api/CommitBatchRequest.ts +3 -0
- package/src/generated/api/ComputeLogEntry.ts +3 -0
- package/src/generated/api/CreateExpressionRequest.ts +3 -0
- package/src/generated/api/CreatePerspectiveRequest.ts +3 -0
- package/src/generated/api/CreateSubjectRequest.ts +3 -0
- package/src/generated/api/EmbedRequest.ts +3 -0
- package/src/generated/api/EntanglementProofInput.ts +3 -0
- package/src/generated/api/ExecuteCommandsRequest.ts +3 -0
- package/src/generated/api/ExportRequest.ts +3 -0
- package/src/generated/api/ExpressionManyRequest.ts +3 -0
- package/src/generated/api/ExpressionProof.ts +3 -0
- package/src/generated/api/ExpressionProofInput.ts +3 -0
- package/src/generated/api/FriendSendMessageRequest.ts +3 -0
- package/src/generated/api/FriendsListRequest.ts +3 -0
- package/src/generated/api/GenerateAgentRequest.ts +3 -0
- package/src/generated/api/GenerateJwtRequest.ts +3 -0
- package/src/generated/api/GetSubjectDataRequest.ts +3 -0
- package/src/generated/api/ImportAgentRequest.ts +3 -0
- package/src/generated/api/ImportRequest.ts +3 -0
- package/src/generated/api/JoinNeighbourhoodRequest.ts +3 -0
- package/src/generated/api/LanguageMetaInput.ts +3 -0
- package/src/generated/api/Link.ts +3 -0
- package/src/generated/api/LinkExpression.ts +6 -0
- package/src/generated/api/LinkExpressionInput.ts +6 -0
- package/src/generated/api/LinkInput.ts +3 -0
- package/src/generated/api/LinkLanguageTemplatesRequest.ts +3 -0
- package/src/generated/api/LinkMutations.ts +5 -0
- package/src/generated/api/LinkStatus.ts +3 -0
- package/src/generated/api/LockAgentRequest.ts +3 -0
- package/src/generated/api/ModelType.ts +3 -0
- package/src/generated/api/NotificationInput.ts +3 -0
- package/src/generated/api/OpenLinkRequest.ts +3 -0
- package/src/generated/api/PermitCapabilityRequest.ts +3 -0
- package/src/generated/api/PerspectiveInput.ts +4 -0
- package/src/generated/api/PromptRequest.ts +3 -0
- package/src/generated/api/PublishLanguageRequest.ts +4 -0
- package/src/generated/api/PublishNeighbourhoodRequest.ts +4 -0
- package/src/generated/api/QueryRequest.ts +6 -0
- package/src/generated/api/RemoveLinkRequest.ts +4 -0
- package/src/generated/api/RemoveLinksBulkRequest.ts +4 -0
- package/src/generated/api/ResourceInput.ts +3 -0
- package/src/generated/api/SetDefaultModelRequest.ts +4 -0
- package/src/generated/api/SetFreeHostingEnabledRequest.ts +3 -0
- package/src/generated/api/SetMultiUserRequest.ts +3 -0
- package/src/generated/api/SetStatusRequest.ts +3 -0
- package/src/generated/api/SignMessageRequest.ts +3 -0
- package/src/generated/api/TrustedAgentsRequest.ts +3 -0
- package/src/generated/api/UnlockAgentRequest.ts +3 -0
- package/src/generated/api/UpdateLinkRequest.ts +5 -0
- package/src/generated/api/UpdatePerspectiveRequest.ts +3 -0
- package/src/generated/api/UserStatistics.ts +3 -0
- package/src/generated/api/VerificationRequestResult.ts +3 -0
- package/src/generated/api/VerifySignatureRequest.ts +3 -0
- package/src/generated/api/WriteSettingsRequest.ts +3 -0
- package/src/generated/api/index.ts +49 -0
- package/src/index.ts +6 -2
- package/src/jestSetup.ts +1 -1
- package/src/language/Icon.ts +0 -4
- package/src/language/Language.ts +50 -292
- package/src/language/LanguageClient.ts +21 -132
- package/src/language/LanguageHandle.ts +0 -14
- package/src/language/LanguageMeta.ts +0 -29
- package/src/language/LanguageRef.ts +0 -7
- package/src/links/Links.ts +0 -37
- package/src/model/Ad4mModel.test.ts +827 -808
- package/src/model/Ad4mModel.ts +676 -457
- package/src/model/ModelQueryBuilder.ts +199 -217
- package/src/model/decorators.ts +29 -0
- package/src/model/hydration.ts +17 -572
- package/src/model/index.ts +0 -4
- package/src/model/query-sparql.test.ts +360 -0
- package/src/model/query-sparql.ts +203 -56
- package/src/model/query-utils.ts +4 -0
- package/src/model/shacl-gen.ts +5 -0
- package/src/model/types.ts +62 -1
- package/src/neighbourhood/Neighbourhood.ts +0 -9
- package/src/neighbourhood/NeighbourhoodClient.ts +42 -207
- package/src/neighbourhood/NeighbourhoodProxy.test.ts +34 -36
- package/src/perspectives/LinkQuery.ts +0 -15
- package/src/perspectives/Perspective.ts +1 -12
- package/src/perspectives/PerspectiveClient.test.ts +262 -0
- package/src/perspectives/PerspectiveClient.ts +227 -412
- package/src/perspectives/PerspectiveDiff.ts +0 -8
- package/src/perspectives/PerspectiveHandle.ts +1 -12
- package/src/perspectives/PerspectiveProxy.test.ts +41 -0
- package/src/perspectives/PerspectiveProxy.ts +325 -348
- package/src/runtime/RuntimeClient.ts +112 -474
- package/src/runtime/RuntimeTypes.ts +144 -0
- package/src/shacl/SHACLShape.test.ts +94 -0
- package/src/shacl/SHACLShape.ts +27 -0
- package/src/typeDefs.ts +3 -5
- package/tsconfig.json +3 -7
- package/lib/src/model/query-surreal.d.ts +0 -44
- package/lib/src/model/query-surreal.js +0 -249
- package/lib/src/model/surreal-utils.d.ts +0 -42
- package/lib/src/model/surreal-utils.js +0 -128
- package/shims/reflect-metadata.ts +0 -0
- package/shims/type-graphql.ts +0 -75
- package/src/PubSub.ts +0 -17
- package/src/agent/AgentResolver.ts +0 -263
- package/src/ai/AIResolver.ts +0 -372
- package/src/buildSchema.ts +0 -43
- package/src/expression/ExpressionResolver.ts +0 -64
- package/src/language/LanguageContext.ts +0 -37
- package/src/language/LanguageResolver.ts +0 -98
- package/src/model/Subject.ts +0 -159
- package/src/model/hydration-batch.ts +0 -205
- package/src/model/prolog-facts.ts +0 -145
- package/src/model/query-prolog.ts +0 -148
- package/src/model/query-sparql-batch.ts +0 -301
- package/src/model/subscription-pool.ts +0 -163
- package/src/neighbourhood/NeighbourhoodResolver.ts +0 -100
- package/src/perspectives/PerspectiveResolver.ts +0 -364
- package/src/runtime/RuntimeResolver.ts +0 -749
- package/src/unwrapApolloResult.ts +0 -16
- package/tsconfig-browser.json +0 -82
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { buildSPARQLOrderLimitOffset, buildSPARQLQuery, hasJsOnlyWhereFilters } from './query-sparql';
|
|
2
|
+
// Minimal stubs for ModelMetadata — buildSPARQLOrderLimitOffset only uses the query arg
|
|
3
|
+
const emptyMetadata = { properties: {}, relations: {} };
|
|
4
|
+
// Metadata with a required (non-literal) property and a literal-stored property
|
|
5
|
+
const richMetadata = {
|
|
6
|
+
properties: {
|
|
7
|
+
name: {
|
|
8
|
+
name: 'name',
|
|
9
|
+
predicate: 'flux://name',
|
|
10
|
+
required: true,
|
|
11
|
+
resolveLanguage: 'literal',
|
|
12
|
+
},
|
|
13
|
+
category: {
|
|
14
|
+
name: 'category',
|
|
15
|
+
predicate: 'flux://category',
|
|
16
|
+
required: true,
|
|
17
|
+
resolveLanguage: 'did:lang:abc', // non-literal → URI stored
|
|
18
|
+
},
|
|
19
|
+
description: {
|
|
20
|
+
name: 'description',
|
|
21
|
+
predicate: 'flux://description',
|
|
22
|
+
initial: 'literal://string:empty',
|
|
23
|
+
resolveLanguage: 'literal',
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
relations: {},
|
|
27
|
+
};
|
|
28
|
+
const emptyRelations = {};
|
|
29
|
+
describe('hasJsOnlyWhereFilters', () => {
|
|
30
|
+
it('returns false when no where clause', () => {
|
|
31
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, undefined)).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
it('returns false when where has literal-stored property with equality filter (pushed to SPARQL)', () => {
|
|
34
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, { name: 'Pasta' })).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
it('returns true when where has literal-stored property with comparison operator', () => {
|
|
37
|
+
// gt/lt/gte/lte/between/contains remain JS-only
|
|
38
|
+
const meta = { properties: { rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' } }, relations: {} };
|
|
39
|
+
expect(hasJsOnlyWhereFilters(meta, emptyRelations, { rating: { gt: 5 } })).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
it('returns false when where only has non-literal property filter', () => {
|
|
42
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, { category: 'some://uri' })).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
it('returns true when where has author filter', () => {
|
|
45
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, { author: 'did:key:abc' })).toBe(true);
|
|
46
|
+
});
|
|
47
|
+
it('returns true when where has timestamp filter', () => {
|
|
48
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, { timestamp: { gt: 100 } })).toBe(true);
|
|
49
|
+
});
|
|
50
|
+
it('returns true for belongsToOne reverse relation', () => {
|
|
51
|
+
const relMeta = { parent: { kind: 'belongsToOne', predicate: 'flux://parent' } };
|
|
52
|
+
const meta = { properties: { parent: { name: 'parent', predicate: 'flux://parent' } }, relations: {} };
|
|
53
|
+
expect(hasJsOnlyWhereFilters(meta, relMeta, { parent: 'some://id' })).toBe(true);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
describe('buildSPARQLQuery — SPARQL-level pagination via subquery', () => {
|
|
57
|
+
const modelClass = {};
|
|
58
|
+
it('includes LIMIT/OFFSET in a pagination subquery when query has them and no JS-only filters', () => {
|
|
59
|
+
const query = { limit: 10, offset: 0, where: { category: 'some://uri' } };
|
|
60
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
61
|
+
// SPARQL should contain LIMIT inside the pagination subquery
|
|
62
|
+
expect(sparql).toContain('LIMIT 10');
|
|
63
|
+
// The outer query should still exist
|
|
64
|
+
expect(sparql).toContain('SELECT ?source ?predicate ?target');
|
|
65
|
+
// Should have a subquery pattern
|
|
66
|
+
expect(sparql).toContain('SELECT DISTINCT ?source');
|
|
67
|
+
});
|
|
68
|
+
it('does NOT include LIMIT/OFFSET when JS-only where filters exist', () => {
|
|
69
|
+
// author is a JS-only filter
|
|
70
|
+
const query = { limit: 10, offset: 0, where: { author: 'did:key:abc' } };
|
|
71
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
72
|
+
expect(sparql).not.toContain('LIMIT');
|
|
73
|
+
expect(sparql).not.toContain('OFFSET');
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
describe('buildSPARQLOrderLimitOffset', () => {
|
|
77
|
+
it('always returns empty string (pagination uses subquery pattern instead)', () => {
|
|
78
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, {})).toBe('');
|
|
79
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, { limit: 50 })).toBe('');
|
|
80
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, { limit: 50, offset: 100 })).toBe('');
|
|
81
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, { order: { timestamp: 'DESC' } })).toBe('');
|
|
82
|
+
expect(buildSPARQLOrderLimitOffset(emptyMetadata, {
|
|
83
|
+
order: { timestamp: 'DESC' },
|
|
84
|
+
limit: 50,
|
|
85
|
+
offset: 100,
|
|
86
|
+
})).toBe('');
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
describe('buildSPARQLQuery — parse_literal push-down filters', () => {
|
|
90
|
+
const modelClass = {};
|
|
91
|
+
it('generates parse_literal FILTER for equality on literal property', () => {
|
|
92
|
+
const query = { where: { name: 'Alice' } };
|
|
93
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
94
|
+
expect(sparql).toContain('<ad4m://fn/parse_literal>');
|
|
95
|
+
expect(sparql).toContain('STR(<ad4m://fn/parse_literal>(?wTarget_name)) = "Alice"');
|
|
96
|
+
});
|
|
97
|
+
it('generates parse_literal IN FILTER for array on literal property', () => {
|
|
98
|
+
const query = { where: { name: ['Alice', 'Bob'] } };
|
|
99
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
100
|
+
expect(sparql).toContain('STR(<ad4m://fn/parse_literal>(?wTarget_name)) IN ("Alice", "Bob")');
|
|
101
|
+
});
|
|
102
|
+
it('generates parse_literal NOT FILTER for not condition', () => {
|
|
103
|
+
const query = { where: { name: { not: 'deleted' } } };
|
|
104
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
105
|
+
expect(sparql).toContain('STR(<ad4m://fn/parse_literal>(?wTarget_name)) != "deleted"');
|
|
106
|
+
});
|
|
107
|
+
it('does NOT use parse_literal for comparison operators (gt/lt)', () => {
|
|
108
|
+
const meta = {
|
|
109
|
+
properties: {
|
|
110
|
+
rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' },
|
|
111
|
+
},
|
|
112
|
+
relations: {},
|
|
113
|
+
};
|
|
114
|
+
const query = { where: { rating: { gt: 5 } } };
|
|
115
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
116
|
+
expect(sparql).not.toContain('parse_literal');
|
|
117
|
+
expect(sparql).toContain('?wTarget_cmp_rating');
|
|
118
|
+
});
|
|
119
|
+
it('does NOT use parse_literal for non-literal properties', () => {
|
|
120
|
+
const query = { where: { category: 'some://uri' } };
|
|
121
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
122
|
+
expect(sparql).not.toContain('parse_literal');
|
|
123
|
+
expect(sparql).toContain('<some://uri>');
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
describe('buildSPARQLQuery — structural correctness', () => {
|
|
127
|
+
const modelClass = {};
|
|
128
|
+
it('generates valid SPARQL with parent filter', () => {
|
|
129
|
+
const query = {
|
|
130
|
+
limit: 50,
|
|
131
|
+
offset: 0,
|
|
132
|
+
parent: { id: 'flux://channel-123', predicate: 'flux://has_message' },
|
|
133
|
+
where: { category: 'some://uri' },
|
|
134
|
+
};
|
|
135
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
136
|
+
expect(sparql).toContain('<flux://channel-123>');
|
|
137
|
+
// SPARQL-level pagination via subquery
|
|
138
|
+
expect(sparql).toContain('LIMIT 50');
|
|
139
|
+
expect(sparql).toContain('SELECT DISTINCT ?source');
|
|
140
|
+
});
|
|
141
|
+
it('does not allow injection through where clause IRI values', () => {
|
|
142
|
+
const query = {
|
|
143
|
+
where: { category: 'some://uri"> . } UNION { SELECT * WHERE { ?s ?p ?o' },
|
|
144
|
+
};
|
|
145
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
146
|
+
// Value should be wrapped in angle brackets as an IRI
|
|
147
|
+
expect(sparql).toContain('<some://uri');
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
describe('buildSPARQLQuery — NOT with array push-down', () => {
|
|
151
|
+
const modelClass = {};
|
|
152
|
+
it('generates NOT IN filter for NOT with array of values', () => {
|
|
153
|
+
const query = { where: { name: { not: ['deleted', 'archived'] } } };
|
|
154
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
155
|
+
// Should use NOT IN with parse_literal for literal properties
|
|
156
|
+
expect(sparql).toContain('NOT IN');
|
|
157
|
+
expect(sparql).toContain('"deleted"');
|
|
158
|
+
expect(sparql).toContain('"archived"');
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
describe('buildSPARQLQuery — mixed push-down + JS-only', () => {
|
|
162
|
+
const modelClass = {};
|
|
163
|
+
const mixedMeta = {
|
|
164
|
+
properties: {
|
|
165
|
+
name: { name: 'name', predicate: 'flux://name', required: true, resolveLanguage: 'literal' },
|
|
166
|
+
rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' },
|
|
167
|
+
},
|
|
168
|
+
relations: {},
|
|
169
|
+
};
|
|
170
|
+
it('pushes equality to SPARQL but keeps gt as JS-only', () => {
|
|
171
|
+
const query = { where: { name: 'Alice', rating: { gt: 5 } } };
|
|
172
|
+
const sparql = buildSPARQLQuery(mixedMeta, emptyRelations, query, modelClass);
|
|
173
|
+
// name equality should be pushed to SPARQL
|
|
174
|
+
expect(sparql).toContain('parse_literal');
|
|
175
|
+
expect(sparql).toContain('Alice');
|
|
176
|
+
// hasJsOnlyWhereFilters should return true because of rating.gt
|
|
177
|
+
expect(hasJsOnlyWhereFilters(mixedMeta, emptyRelations, query.where)).toBe(true);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
describe('buildSPARQLQuery — non-literal and flag properties', () => {
|
|
181
|
+
const modelClass = {};
|
|
182
|
+
it('does NOT use parse_literal for non-literal (resolveLanguage) properties', () => {
|
|
183
|
+
const meta = {
|
|
184
|
+
properties: {
|
|
185
|
+
avatar: { name: 'avatar', predicate: 'flux://avatar', required: true, resolveLanguage: 'did:lang:some-language' },
|
|
186
|
+
},
|
|
187
|
+
relations: {},
|
|
188
|
+
};
|
|
189
|
+
const query = { where: { avatar: 'https://example.com/pic.jpg' } };
|
|
190
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
191
|
+
expect(sparql).not.toContain('parse_literal');
|
|
192
|
+
});
|
|
193
|
+
it('does NOT use parse_literal for flag properties', () => {
|
|
194
|
+
const meta = {
|
|
195
|
+
properties: {
|
|
196
|
+
isPublic: { name: 'isPublic', predicate: 'flux://isPublic', flag: true, initial: 'flux://true' },
|
|
197
|
+
},
|
|
198
|
+
relations: {},
|
|
199
|
+
};
|
|
200
|
+
const query = { where: { isPublic: 'flux://true' } };
|
|
201
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
202
|
+
expect(sparql).not.toContain('parse_literal');
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
describe('buildSPARQLQuery — IRI correctness', () => {
|
|
206
|
+
const modelClass = {};
|
|
207
|
+
it('uses <ad4m://fn/parse_literal> IRI, not fn::parse_literal SurrealDB syntax', () => {
|
|
208
|
+
const query = { where: { name: 'test' } };
|
|
209
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
210
|
+
expect(sparql).toContain('<ad4m://fn/parse_literal>');
|
|
211
|
+
expect(sparql).not.toContain('fn::parse_literal');
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
// ──────────────────────────────────────────────────────────
|
|
215
|
+
// SPARQL-level pagination (LIMIT/OFFSET in generated queries)
|
|
216
|
+
// ──────────────────────────────────────────────────────────
|
|
217
|
+
describe('SPARQL-level pagination', () => {
|
|
218
|
+
const modelClass = {};
|
|
219
|
+
it('includes LIMIT in SPARQL when query specifies limit', () => {
|
|
220
|
+
const query = { limit: 30 };
|
|
221
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
222
|
+
expect(sparql).toContain('LIMIT 30');
|
|
223
|
+
expect(sparql).toContain('SELECT DISTINCT ?source');
|
|
224
|
+
});
|
|
225
|
+
it('includes OFFSET in SPARQL when query specifies offset > 0', () => {
|
|
226
|
+
const query = { limit: 20, offset: 40 };
|
|
227
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
228
|
+
expect(sparql).toContain('LIMIT 20');
|
|
229
|
+
expect(sparql).toContain('OFFSET 40');
|
|
230
|
+
});
|
|
231
|
+
it('does NOT include OFFSET when offset is 0', () => {
|
|
232
|
+
const query = { limit: 10, offset: 0 };
|
|
233
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
234
|
+
expect(sparql).toContain('LIMIT 10');
|
|
235
|
+
expect(sparql).not.toContain('OFFSET');
|
|
236
|
+
});
|
|
237
|
+
it('includes ORDER BY in subquery when query.order is specified', () => {
|
|
238
|
+
const query = { limit: 10, order: { name: 'DESC' } };
|
|
239
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
240
|
+
expect(sparql).toContain('ORDER BY DESC(');
|
|
241
|
+
expect(sparql).toContain('LIMIT 10');
|
|
242
|
+
});
|
|
243
|
+
it('does NOT default to ORDER BY timestamp when paginating without explicit order', () => {
|
|
244
|
+
const query = { limit: 30 };
|
|
245
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
246
|
+
expect(sparql).not.toContain('ORDER BY');
|
|
247
|
+
expect(sparql).not.toContain('pg_minTs');
|
|
248
|
+
});
|
|
249
|
+
it('does NOT push pagination to SPARQL when JS-only where filters exist (author)', () => {
|
|
250
|
+
const query = { limit: 10, where: { author: 'did:key:abc' } };
|
|
251
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
252
|
+
expect(sparql).not.toContain('LIMIT');
|
|
253
|
+
expect(sparql).not.toContain('OFFSET');
|
|
254
|
+
});
|
|
255
|
+
it('does NOT push pagination to SPARQL when JS-only where filters exist (gt operator)', () => {
|
|
256
|
+
const meta = {
|
|
257
|
+
properties: {
|
|
258
|
+
rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' },
|
|
259
|
+
},
|
|
260
|
+
relations: {},
|
|
261
|
+
};
|
|
262
|
+
const query = { limit: 10, where: { rating: { gt: 5 } } };
|
|
263
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
264
|
+
expect(sparql).not.toContain('LIMIT');
|
|
265
|
+
});
|
|
266
|
+
it('wraps pagination in a subquery (outer SELECT fetches all links for the page)', () => {
|
|
267
|
+
const query = { limit: 5, offset: 10 };
|
|
268
|
+
const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
|
|
269
|
+
expect(sparql).toContain('SELECT ?source ?predicate ?target ?author ?timestamp');
|
|
270
|
+
expect(sparql).toContain('SELECT DISTINCT ?source');
|
|
271
|
+
expect(sparql).toContain('LIMIT 5');
|
|
272
|
+
expect(sparql).toContain('OFFSET 10');
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
describe('buildSPARQLQuery — set-difference patterns', () => {
|
|
276
|
+
const modelClass = {};
|
|
277
|
+
// Subgroups are grandchildren of channels (channel → conversation → subgroup).
|
|
278
|
+
// Never assume direct child relationships in set-difference queries.
|
|
279
|
+
// A global EXISTS pattern (no parent scoping) should match items regardless of depth.
|
|
280
|
+
it('generates global EXISTS without parent scoping when no parent filter', () => {
|
|
281
|
+
const meta = {
|
|
282
|
+
properties: {
|
|
283
|
+
status: { name: 'status', predicate: 'flux://status', required: true, resolveLanguage: 'literal' },
|
|
284
|
+
},
|
|
285
|
+
relations: {},
|
|
286
|
+
};
|
|
287
|
+
const query = { where: { status: 'active' } };
|
|
288
|
+
const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
|
|
289
|
+
// Without a parent filter, the query should not scope to any specific parent
|
|
290
|
+
// This ensures grandchildren (items at any depth) are matched
|
|
291
|
+
expect(sparql).not.toContain('flux://has_child');
|
|
292
|
+
expect(sparql).toContain('SELECT ?source ?predicate ?target');
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
describe('buildSPARQLQuery — RDF 1.2 reifier patterns', () => {
|
|
296
|
+
const modelClass = {};
|
|
297
|
+
it('uses rdf:reifies instead of GRAPH for link metadata', () => {
|
|
298
|
+
const query = { limit: 10 };
|
|
299
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
300
|
+
expect(sparql).toContain('<http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies>');
|
|
301
|
+
expect(sparql).toContain('<http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies> <<(');
|
|
302
|
+
expect(sparql).toContain('?_reifier');
|
|
303
|
+
expect(sparql).not.toContain('GRAPH');
|
|
304
|
+
expect(sparql).not.toContain('?linkGraph');
|
|
305
|
+
});
|
|
306
|
+
it('binds author and timestamp on the reifier node', () => {
|
|
307
|
+
const query = {};
|
|
308
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
309
|
+
expect(sparql).toContain('?_reifier <ad4m://ontology/author> ?author');
|
|
310
|
+
expect(sparql).toContain('?_reifier <ad4m://ontology/timestamp> ?timestamp');
|
|
311
|
+
});
|
|
312
|
+
});
|
|
@@ -77,9 +77,13 @@ export function buildWhereCondition(predicate, condition, opts) {
|
|
|
77
77
|
if (ops.not !== undefined) {
|
|
78
78
|
if (Array.isArray(ops.not)) {
|
|
79
79
|
const formattedValues = ops.not.map((v) => formatValue(v)).join(', ');
|
|
80
|
+
// FILTER NOT EXISTS here is scoped to a specific predicate, not a global scan,
|
|
81
|
+
// so O(N²) behavior is bounded by the number of values for this property.
|
|
80
82
|
parts.push(`FILTER NOT EXISTS { ?target <${escapedPredicate}> ?_nval . FILTER(?_nval IN (${formattedValues})) }`);
|
|
81
83
|
}
|
|
82
84
|
else {
|
|
85
|
+
// FILTER NOT EXISTS here is scoped to a specific predicate, not a global scan,
|
|
86
|
+
// so O(N²) behavior is bounded by the number of values for this property.
|
|
83
87
|
parts.push(`FILTER NOT EXISTS { ?target <${escapedPredicate}> ${formatValue(ops.not)} }`);
|
|
84
88
|
}
|
|
85
89
|
}
|
|
@@ -101,6 +101,10 @@ export function buildSHACL(subjectName, target, properties, allRelationsMeta, co
|
|
|
101
101
|
if (propMeta.flag && propMeta.initial) {
|
|
102
102
|
propShape.hasValue = propMeta.initial;
|
|
103
103
|
}
|
|
104
|
+
// sh:in — allowed values (enum constraint)
|
|
105
|
+
if (propMeta.options && propMeta.options.length > 0) {
|
|
106
|
+
propShape.in = propMeta.options;
|
|
107
|
+
}
|
|
104
108
|
// AD4M-specific metadata
|
|
105
109
|
if (propMeta.local !== undefined) {
|
|
106
110
|
propShape.local = propMeta.local;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the delta fast-path logic used in subscription-pool.ts.
|
|
3
|
+
*
|
|
4
|
+
* The delta optimisation in pooledSubscribe's onResult handler:
|
|
5
|
+
* 1. Checks if new rows are a strict superset of previous rows
|
|
6
|
+
* 2. Extracts added rows (new triples not in prev set)
|
|
7
|
+
* 3. Verifies ALL added rows have source URIs not already in previous results
|
|
8
|
+
* 4. If condition 3 fails (existing source got new links), falls back to full rehydration
|
|
9
|
+
*
|
|
10
|
+
* These tests validate the detection logic in isolation.
|
|
11
|
+
*/
|
|
12
|
+
type Row = {
|
|
13
|
+
source: string;
|
|
14
|
+
predicate: string;
|
|
15
|
+
target: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Replicates the delta-path eligibility check from subscription-pool.ts
|
|
19
|
+
*/
|
|
20
|
+
declare function detectDeltaPath(prevArr: Row[], newArr: Row[]): {
|
|
21
|
+
useDelta: boolean;
|
|
22
|
+
addedRows: Row[];
|
|
23
|
+
reason?: string;
|
|
24
|
+
};
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the delta fast-path logic used in subscription-pool.ts.
|
|
3
|
+
*
|
|
4
|
+
* The delta optimisation in pooledSubscribe's onResult handler:
|
|
5
|
+
* 1. Checks if new rows are a strict superset of previous rows
|
|
6
|
+
* 2. Extracts added rows (new triples not in prev set)
|
|
7
|
+
* 3. Verifies ALL added rows have source URIs not already in previous results
|
|
8
|
+
* 4. If condition 3 fails (existing source got new links), falls back to full rehydration
|
|
9
|
+
*
|
|
10
|
+
* These tests validate the detection logic in isolation.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Replicates the delta-path eligibility check from subscription-pool.ts
|
|
14
|
+
*/
|
|
15
|
+
function detectDeltaPath(prevArr, newArr) {
|
|
16
|
+
if (newArr.length <= prevArr.length) {
|
|
17
|
+
return { useDelta: false, addedRows: [], reason: 'no growth' };
|
|
18
|
+
}
|
|
19
|
+
if (newArr.length - prevArr.length > 5) {
|
|
20
|
+
return { useDelta: false, addedRows: [], reason: 'delta too large' };
|
|
21
|
+
}
|
|
22
|
+
const prevSources = new Set(prevArr.map(r => `${r.source}|${r.predicate}|${r.target}`));
|
|
23
|
+
const addedRows = newArr.filter(r => !prevSources.has(`${r.source}|${r.predicate}|${r.target}`));
|
|
24
|
+
const newSources = new Set(newArr.map(r => `${r.source}|${r.predicate}|${r.target}`));
|
|
25
|
+
const removedRows = prevArr.filter(r => !newSources.has(`${r.source}|${r.predicate}|${r.target}`));
|
|
26
|
+
if (removedRows.length > 0) {
|
|
27
|
+
return { useDelta: false, addedRows, reason: 'rows removed' };
|
|
28
|
+
}
|
|
29
|
+
// Key check: all added rows must have NEW source URIs
|
|
30
|
+
const prevSourceIds = new Set(prevArr.map(r => r.source));
|
|
31
|
+
const allNewSources = addedRows.every(r => !prevSourceIds.has(r.source));
|
|
32
|
+
if (!allNewSources) {
|
|
33
|
+
return { useDelta: false, addedRows, reason: 'existing source got new links' };
|
|
34
|
+
}
|
|
35
|
+
return { useDelta: true, addedRows };
|
|
36
|
+
}
|
|
37
|
+
describe('delta fast-path detection', () => {
|
|
38
|
+
const prev = [
|
|
39
|
+
{ source: 'a', predicate: 'p1', target: 't1' },
|
|
40
|
+
{ source: 'a', predicate: 'p2', target: 't2' },
|
|
41
|
+
{ source: 'b', predicate: 'p1', target: 't3' },
|
|
42
|
+
];
|
|
43
|
+
it('appends genuinely new items via delta path', () => {
|
|
44
|
+
const next = [
|
|
45
|
+
...prev,
|
|
46
|
+
{ source: 'c', predicate: 'p1', target: 't4' },
|
|
47
|
+
];
|
|
48
|
+
const result = detectDeltaPath(prev, next);
|
|
49
|
+
expect(result.useDelta).toBe(true);
|
|
50
|
+
expect(result.addedRows).toHaveLength(1);
|
|
51
|
+
expect(result.addedRows[0].source).toBe('c');
|
|
52
|
+
});
|
|
53
|
+
it('falls back when existing source gets new links', () => {
|
|
54
|
+
const next = [
|
|
55
|
+
...prev,
|
|
56
|
+
{ source: 'a', predicate: 'p3', target: 't5' }, // source 'a' already exists
|
|
57
|
+
];
|
|
58
|
+
const result = detectDeltaPath(prev, next);
|
|
59
|
+
expect(result.useDelta).toBe(false);
|
|
60
|
+
expect(result.reason).toBe('existing source got new links');
|
|
61
|
+
});
|
|
62
|
+
it('falls back when rows are removed (even if new ones added)', () => {
|
|
63
|
+
const next = [
|
|
64
|
+
{ source: 'a', predicate: 'p1', target: 't1' },
|
|
65
|
+
{ source: 'a', predicate: 'p2', target: 't2' },
|
|
66
|
+
// source 'b' row removed, two new sources added so length grows
|
|
67
|
+
{ source: 'c', predicate: 'p1', target: 't4' },
|
|
68
|
+
{ source: 'd', predicate: 'p1', target: 't5' },
|
|
69
|
+
];
|
|
70
|
+
const result = detectDeltaPath(prev, next);
|
|
71
|
+
expect(result.useDelta).toBe(false);
|
|
72
|
+
expect(result.reason).toBe('rows removed');
|
|
73
|
+
});
|
|
74
|
+
it('falls back when delta is too large (>5 new rows)', () => {
|
|
75
|
+
const next = [
|
|
76
|
+
...prev,
|
|
77
|
+
...Array.from({ length: 6 }, (_, i) => ({
|
|
78
|
+
source: `new${i}`, predicate: 'p1', target: `t${10 + i}`,
|
|
79
|
+
})),
|
|
80
|
+
];
|
|
81
|
+
const result = detectDeltaPath(prev, next);
|
|
82
|
+
expect(result.useDelta).toBe(false);
|
|
83
|
+
expect(result.reason).toBe('delta too large');
|
|
84
|
+
});
|
|
85
|
+
it('falls back when no growth', () => {
|
|
86
|
+
const result = detectDeltaPath(prev, prev);
|
|
87
|
+
expect(result.useDelta).toBe(false);
|
|
88
|
+
expect(result.reason).toBe('no growth');
|
|
89
|
+
});
|
|
90
|
+
it('handles multiple new sources in one delta', () => {
|
|
91
|
+
const next = [
|
|
92
|
+
...prev,
|
|
93
|
+
{ source: 'c', predicate: 'p1', target: 't4' },
|
|
94
|
+
{ source: 'd', predicate: 'p1', target: 't5' },
|
|
95
|
+
];
|
|
96
|
+
const result = detectDeltaPath(prev, next);
|
|
97
|
+
expect(result.useDelta).toBe(true);
|
|
98
|
+
expect(result.addedRows).toHaveLength(2);
|
|
99
|
+
});
|
|
100
|
+
// ── Test #8: Rapid successive additions ──
|
|
101
|
+
it('handles 10 items added in quick succession without duplicates', () => {
|
|
102
|
+
let current = [...prev];
|
|
103
|
+
for (let i = 0; i < 10; i++) {
|
|
104
|
+
const next = [...current, { source: `rapid-${i}`, predicate: 'p1', target: `t-rapid-${i}` }];
|
|
105
|
+
const result = detectDeltaPath(current, next);
|
|
106
|
+
// Each individual addition should be eligible for delta
|
|
107
|
+
expect(result.useDelta).toBe(true);
|
|
108
|
+
expect(result.addedRows).toHaveLength(1);
|
|
109
|
+
expect(result.addedRows[0].source).toBe(`rapid-${i}`);
|
|
110
|
+
current = next;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
// ── Test #9: Addition + removal in same batch ──
|
|
114
|
+
it('falls back when one item added and another removed simultaneously', () => {
|
|
115
|
+
const next = [
|
|
116
|
+
{ source: 'a', predicate: 'p1', target: 't1' },
|
|
117
|
+
{ source: 'a', predicate: 'p2', target: 't2' },
|
|
118
|
+
// source 'b' removed, source 'c' added
|
|
119
|
+
{ source: 'c', predicate: 'p1', target: 't4' },
|
|
120
|
+
{ source: 'd', predicate: 'p1', target: 't5' },
|
|
121
|
+
];
|
|
122
|
+
const result = detectDeltaPath(prev, next);
|
|
123
|
+
expect(result.useDelta).toBe(false);
|
|
124
|
+
expect(result.reason).toBe('rows removed');
|
|
125
|
+
});
|
|
126
|
+
// ── Test #10: Delta with nested relations (detection only) ──
|
|
127
|
+
it('delta path detects new items correctly even when sources have complex URIs', () => {
|
|
128
|
+
const next = [
|
|
129
|
+
...prev,
|
|
130
|
+
{ source: 'flux://post/123/comment/456', predicate: 'flux://belongs_to', target: 'flux://post/123' },
|
|
131
|
+
];
|
|
132
|
+
const result = detectDeltaPath(prev, next);
|
|
133
|
+
expect(result.useDelta).toBe(true);
|
|
134
|
+
expect(result.addedRows).toHaveLength(1);
|
|
135
|
+
expect(result.addedRows[0].source).toBe('flux://post/123/comment/456');
|
|
136
|
+
});
|
|
137
|
+
// ── Test #11: Empty previous result + new items ──
|
|
138
|
+
it('handles empty previous array with new items arriving', () => {
|
|
139
|
+
const empty = [];
|
|
140
|
+
const next = [
|
|
141
|
+
{ source: 'x', predicate: 'p1', target: 't1' },
|
|
142
|
+
];
|
|
143
|
+
const result = detectDeltaPath(empty, next);
|
|
144
|
+
expect(result.useDelta).toBe(true);
|
|
145
|
+
expect(result.addedRows).toHaveLength(1);
|
|
146
|
+
});
|
|
147
|
+
it('handles empty previous with no new items', () => {
|
|
148
|
+
const result = detectDeltaPath([], []);
|
|
149
|
+
expect(result.useDelta).toBe(false);
|
|
150
|
+
expect(result.reason).toBe('no growth');
|
|
151
|
+
});
|
|
152
|
+
// ── Test #12: Source URI with special characters ──
|
|
153
|
+
it('handles URIs with encoded characters, fragments, and query params', () => {
|
|
154
|
+
const specialPrev = [
|
|
155
|
+
{ source: 'flux://item%20one', predicate: 'p1', target: 't1' },
|
|
156
|
+
{ source: 'flux://item#section', predicate: 'p2', target: 't2' },
|
|
157
|
+
{ source: 'flux://item?key=val&foo=bar', predicate: 'p3', target: 't3' },
|
|
158
|
+
];
|
|
159
|
+
const specialNext = [
|
|
160
|
+
...specialPrev,
|
|
161
|
+
{ source: 'flux://new%C3%A9item', predicate: 'p1', target: 't4' },
|
|
162
|
+
];
|
|
163
|
+
const result = detectDeltaPath(specialPrev, specialNext);
|
|
164
|
+
expect(result.useDelta).toBe(true);
|
|
165
|
+
expect(result.addedRows).toHaveLength(1);
|
|
166
|
+
expect(result.addedRows[0].source).toBe('flux://new%C3%A9item');
|
|
167
|
+
});
|
|
168
|
+
it('correctly identifies existing source with special chars getting new links', () => {
|
|
169
|
+
const specialPrev = [
|
|
170
|
+
{ source: 'flux://item#section', predicate: 'p1', target: 't1' },
|
|
171
|
+
];
|
|
172
|
+
const specialNext = [
|
|
173
|
+
...specialPrev,
|
|
174
|
+
{ source: 'flux://item#section', predicate: 'p2', target: 't2' },
|
|
175
|
+
];
|
|
176
|
+
const result = detectDeltaPath(specialPrev, specialNext);
|
|
177
|
+
expect(result.useDelta).toBe(false);
|
|
178
|
+
expect(result.reason).toBe('existing source got new links');
|
|
179
|
+
});
|
|
180
|
+
});
|
|
@@ -55,14 +55,60 @@ export async function pooledSubscribe(perspective, sparqlQuery, hydrate, onResul
|
|
|
55
55
|
refCount: 1,
|
|
56
56
|
};
|
|
57
57
|
pool.set(key, newEntry);
|
|
58
|
-
// Set up shared result handler
|
|
58
|
+
// Set up shared result handler with delta optimization
|
|
59
59
|
subscription.onResult(async (rawResult) => {
|
|
60
|
+
const prevRaw = newEntry.latestRaw;
|
|
60
61
|
newEntry.latestRaw = rawResult;
|
|
61
62
|
newEntry.hydrating = true;
|
|
62
63
|
try {
|
|
63
|
-
// Hydrate ONCE using the first subscriber's hydrate function
|
|
64
64
|
const firstSub = newEntry.callbacks.values().next().value;
|
|
65
65
|
if (firstSub) {
|
|
66
|
+
// Delta optimization: if the new result is strictly an addition
|
|
67
|
+
// (all previous rows still present + new rows), only hydrate the
|
|
68
|
+
// new rows and append them to the cached hydrated result.
|
|
69
|
+
const prevArr = Array.isArray(prevRaw) ? prevRaw : [];
|
|
70
|
+
const newArr = Array.isArray(rawResult) ? rawResult : [];
|
|
71
|
+
const prevHydrated = newEntry.latestHydrated;
|
|
72
|
+
if (prevHydrated &&
|
|
73
|
+
Array.isArray(prevHydrated) &&
|
|
74
|
+
newArr.length > prevArr.length &&
|
|
75
|
+
newArr.length - prevArr.length <= 5 // small delta — worth optimizing
|
|
76
|
+
) {
|
|
77
|
+
// Build a set of source URIs from previous results for quick lookup
|
|
78
|
+
const prevSources = new Set(prevArr.map((r) => `${r.source}|${r.predicate}|${r.target}`));
|
|
79
|
+
const addedRows = newArr.filter((r) => !prevSources.has(`${r.source}|${r.predicate}|${r.target}`));
|
|
80
|
+
// Check that no rows were removed (pure addition)
|
|
81
|
+
const newSources = new Set(newArr.map((r) => `${r.source}|${r.predicate}|${r.target}`));
|
|
82
|
+
const removedRows = prevArr.filter((r) => !newSources.has(`${r.source}|${r.predicate}|${r.target}`));
|
|
83
|
+
if (removedRows.length === 0 && addedRows.length > 0) {
|
|
84
|
+
// Verify ALL added rows have sources not in previous results.
|
|
85
|
+
// If any added row's source matches an existing source, it means
|
|
86
|
+
// an existing instance got new links — we need full rehydration.
|
|
87
|
+
const prevSourceIds = new Set(prevArr.map((r) => r.source));
|
|
88
|
+
const allNewSources = addedRows.every((r) => !prevSourceIds.has(r.source));
|
|
89
|
+
if (allNewSources) {
|
|
90
|
+
// Pure addition of new instances — hydrate only the new rows
|
|
91
|
+
try {
|
|
92
|
+
const deltaHydrated = await firstSub.hydrate(addedRows);
|
|
93
|
+
if (Array.isArray(deltaHydrated)) {
|
|
94
|
+
const merged = [...prevHydrated, ...deltaHydrated];
|
|
95
|
+
newEntry.latestHydrated = merged;
|
|
96
|
+
for (const cb of newEntry.callbacks) {
|
|
97
|
+
try {
|
|
98
|
+
cb.onResult(merged);
|
|
99
|
+
}
|
|
100
|
+
catch (e) { /* subscriber error */ }
|
|
101
|
+
}
|
|
102
|
+
return; // Delta path succeeded
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
// Delta hydration failed — fall through to full hydration
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// Full hydration fallback
|
|
66
112
|
const hydrated = await firstSub.hydrate(rawResult);
|
|
67
113
|
newEntry.latestHydrated = hydrated;
|
|
68
114
|
// Distribute to all callbacks
|