@coasys/ad4m 0.13.0-test-3 → 0.13.0-test-5
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 +6717 -15519
- package/lib/index.js +4811 -13662
- package/lib/src/Ad4mClient.d.ts +7 -3
- package/lib/src/Ad4mClient.js +39 -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 -1
- package/lib/src/index.js +6 -1
- 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 +1 -1
- package/lib/src/language/Language.js +10 -76
- 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 +76 -44
- package/lib/src/model/Ad4mModel.js +635 -479
- package/lib/src/model/Ad4mModel.test.js +371 -815
- package/lib/src/model/ModelQueryBuilder.d.ts +15 -27
- package/lib/src/model/ModelQueryBuilder.js +163 -224
- package/lib/src/model/decorators.d.ts +22 -0
- package/lib/src/model/decorators.js +7 -0
- package/lib/src/model/hydration.d.ts +3 -93
- package/lib/src/model/hydration.js +3 -970
- 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 +4 -67
- package/lib/src/model/query-sparql.js +26 -322
- package/lib/src/model/query-sparql.test.js +35 -218
- package/lib/src/model/query-utils.d.ts +1 -0
- package/lib/src/model/query-utils.js +46 -1
- package/lib/src/model/shacl-gen.js +4 -0
- package/lib/src/model/types.d.ts +51 -43
- 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 +13 -17
- package/lib/src/perspectives/PerspectiveClient.js +179 -429
- package/lib/src/perspectives/PerspectiveClient.test.js +55 -0
- package/lib/src/perspectives/PerspectiveDiff.js +4 -31
- package/lib/src/perspectives/PerspectiveHandle.js +3 -43
- package/lib/src/perspectives/PerspectiveProxy.d.ts +106 -71
- package/lib/src/perspectives/PerspectiveProxy.js +289 -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 +6 -18
- package/rollup.config.js +0 -7
- package/src/Ad4mClient.test.ts +1179 -1340
- package/src/Ad4mClient.ts +40 -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 -1
- package/src/jestSetup.ts +1 -1
- package/src/language/Icon.ts +0 -4
- package/src/language/Language.ts +2 -23
- 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 +766 -1173
- package/src/model/Ad4mModel.ts +664 -529
- package/src/model/ModelQueryBuilder.ts +203 -251
- package/src/model/decorators.ts +29 -0
- package/src/model/hydration.ts +3 -1037
- package/src/model/index.ts +0 -4
- package/src/model/query-sparql.test.ts +34 -242
- package/src/model/query-sparql.ts +27 -337
- package/src/model/query-utils.ts +47 -2
- package/src/model/shacl-gen.ts +5 -0
- package/src/model/types.ts +55 -44
- 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 +206 -148
- package/src/perspectives/PerspectiveClient.ts +247 -442
- 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 +331 -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/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/LanguageResolver.ts +0 -98
- package/src/model/Subject.ts +0 -159
- package/src/model/hydration-batch.ts +0 -205
- package/src/model/hydration-batching.test.ts +0 -563
- 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-delta.test.ts +0 -207
- package/src/model/subscription-pool.ts +0 -211
- 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
|
@@ -1,563 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for batched hydrateRelations() — verifies the N+1 → 1 SPARQL optimisation.
|
|
3
|
-
*
|
|
4
|
-
* We mock PerspectiveProxy to count querySparql / get calls and verify
|
|
5
|
-
* that the batched path issues 1 SPARQL query instead of N perspective.get() calls.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { hydrateRelations } from './hydration';
|
|
9
|
-
|
|
10
|
-
// ── Helpers to build minimal mocks ──────────────────────────────────────
|
|
11
|
-
|
|
12
|
-
function makeMockPerspective(opts: {
|
|
13
|
-
sparqlResults?: { source: string; target: string }[];
|
|
14
|
-
sparqlError?: boolean;
|
|
15
|
-
getResults?: Map<string, { data: { source: string; target: string; predicate: string } }[]>;
|
|
16
|
-
}) {
|
|
17
|
-
const callCounts = { querySparql: 0, get: 0, findAll: 0 };
|
|
18
|
-
|
|
19
|
-
const perspective = {
|
|
20
|
-
querySparql: jest.fn(async (_q: string) => {
|
|
21
|
-
callCounts.querySparql++;
|
|
22
|
-
if (opts.sparqlError) throw new Error('SPARQL unavailable');
|
|
23
|
-
return {
|
|
24
|
-
results: {
|
|
25
|
-
bindings: (opts.sparqlResults ?? []).map(r => ({
|
|
26
|
-
source: { value: r.source },
|
|
27
|
-
target: { value: r.target },
|
|
28
|
-
})),
|
|
29
|
-
},
|
|
30
|
-
};
|
|
31
|
-
}),
|
|
32
|
-
get: jest.fn(async (query: any) => {
|
|
33
|
-
callCounts.get++;
|
|
34
|
-
const targetId = query.target;
|
|
35
|
-
return opts.getResults?.get(targetId) ?? [];
|
|
36
|
-
}),
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
return { perspective, callCounts };
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function makeMockTargetClass(knownInstances: Map<string, any>) {
|
|
43
|
-
class MockTarget {
|
|
44
|
-
static __metadata = { className: 'MockTarget' };
|
|
45
|
-
id: string;
|
|
46
|
-
perspective: any;
|
|
47
|
-
constructor(perspective: any, id: string) {
|
|
48
|
-
this.id = id;
|
|
49
|
-
this.perspective = perspective;
|
|
50
|
-
}
|
|
51
|
-
async get() {
|
|
52
|
-
const data = knownInstances.get(this.id);
|
|
53
|
-
if (data) Object.assign(this, data);
|
|
54
|
-
return this;
|
|
55
|
-
}
|
|
56
|
-
static async findAll(_perspective: any, query: any) {
|
|
57
|
-
const ids: string[] = query?.where?.id ?? [];
|
|
58
|
-
return ids
|
|
59
|
-
.filter(id => knownInstances.has(id))
|
|
60
|
-
.map(id => {
|
|
61
|
-
const inst = { id, ...knownInstances.get(id) };
|
|
62
|
-
return inst;
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return MockTarget;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Minimal decorator metadata mock
|
|
70
|
-
jest.mock('./decorators', () => ({
|
|
71
|
-
getPropertiesMetadata: () => ({}),
|
|
72
|
-
getRelationsMetadata: (cls: any) => cls.__relMeta ?? {},
|
|
73
|
-
buildConformanceFilter: () => () => true,
|
|
74
|
-
}));
|
|
75
|
-
|
|
76
|
-
jest.mock('./query-utils', () => ({
|
|
77
|
-
compileWhereClause: () => '',
|
|
78
|
-
}));
|
|
79
|
-
|
|
80
|
-
jest.mock('../utils', () => ({
|
|
81
|
-
escapeQueryString: (s: string) => s,
|
|
82
|
-
}));
|
|
83
|
-
|
|
84
|
-
// ── Tests ───────────────────────────────────────────────────────────────
|
|
85
|
-
|
|
86
|
-
describe('hydrateRelations batching', () => {
|
|
87
|
-
const PREDICATE = 'flux://belongs_to';
|
|
88
|
-
|
|
89
|
-
it('issues 1 SPARQL query instead of N perspective.get() calls for belongsToMany', async () => {
|
|
90
|
-
const N = 50;
|
|
91
|
-
// Create N parent instances
|
|
92
|
-
const instances = Array.from({ length: N }, (_, i) => ({
|
|
93
|
-
id: `parent-${i}`,
|
|
94
|
-
children: undefined as any,
|
|
95
|
-
}));
|
|
96
|
-
|
|
97
|
-
// Each parent has 2 children pointing to it
|
|
98
|
-
const sparqlResults: { source: string; target: string }[] = [];
|
|
99
|
-
const knownChildren = new Map<string, any>();
|
|
100
|
-
for (let i = 0; i < N; i++) {
|
|
101
|
-
for (let j = 0; j < 2; j++) {
|
|
102
|
-
const childId = `child-${i}-${j}`;
|
|
103
|
-
sparqlResults.push({ source: childId, target: `parent-${i}` });
|
|
104
|
-
knownChildren.set(childId, { name: `Child ${i}-${j}` });
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const { perspective, callCounts } = makeMockPerspective({ sparqlResults });
|
|
109
|
-
const TargetClass = makeMockTargetClass(knownChildren);
|
|
110
|
-
|
|
111
|
-
// Wire up relation metadata
|
|
112
|
-
const modelClass = {
|
|
113
|
-
__relMeta: {
|
|
114
|
-
children: {
|
|
115
|
-
kind: 'belongsToMany',
|
|
116
|
-
predicate: PREDICATE,
|
|
117
|
-
target: () => TargetClass,
|
|
118
|
-
maxCount: undefined,
|
|
119
|
-
},
|
|
120
|
-
},
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
// Mock getRelationsMetadata to return our metadata
|
|
124
|
-
const decorators = require('./decorators');
|
|
125
|
-
decorators.getRelationsMetadata = () => modelClass.__relMeta;
|
|
126
|
-
|
|
127
|
-
await hydrateRelations(
|
|
128
|
-
modelClass,
|
|
129
|
-
instances,
|
|
130
|
-
perspective as any,
|
|
131
|
-
{ children: true },
|
|
132
|
-
);
|
|
133
|
-
|
|
134
|
-
// Key assertion: 1 SPARQL query, 0 perspective.get() calls
|
|
135
|
-
expect(callCounts.querySparql).toBe(1);
|
|
136
|
-
expect(callCounts.get).toBe(0);
|
|
137
|
-
|
|
138
|
-
// Verify data correctness
|
|
139
|
-
for (let i = 0; i < N; i++) {
|
|
140
|
-
expect(instances[i].children).toHaveLength(2);
|
|
141
|
-
expect(instances[i].children[0].id).toMatch(/^child-/);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
console.log(`✅ Batched: ${N} instances hydrated with 1 SPARQL query + 1 findAll (was ${N} sequential perspective.get calls)`);
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it('issues 1 SPARQL query for belongsToOne', async () => {
|
|
148
|
-
const N = 20;
|
|
149
|
-
const instances = Array.from({ length: N }, (_, i) => ({
|
|
150
|
-
id: `parent-${i}`,
|
|
151
|
-
owner: undefined as any,
|
|
152
|
-
}));
|
|
153
|
-
|
|
154
|
-
const sparqlResults: { source: string; target: string }[] = [];
|
|
155
|
-
const knownOwners = new Map<string, any>();
|
|
156
|
-
for (let i = 0; i < N; i++) {
|
|
157
|
-
const ownerId = `owner-${i}`;
|
|
158
|
-
sparqlResults.push({ source: ownerId, target: `parent-${i}` });
|
|
159
|
-
knownOwners.set(ownerId, { name: `Owner ${i}` });
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const { perspective, callCounts } = makeMockPerspective({ sparqlResults });
|
|
163
|
-
const TargetClass = makeMockTargetClass(knownOwners);
|
|
164
|
-
|
|
165
|
-
const modelClass = {
|
|
166
|
-
__relMeta: {
|
|
167
|
-
owner: {
|
|
168
|
-
kind: 'belongsToOne',
|
|
169
|
-
predicate: PREDICATE,
|
|
170
|
-
target: () => TargetClass,
|
|
171
|
-
maxCount: 1,
|
|
172
|
-
},
|
|
173
|
-
},
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
const decorators = require('./decorators');
|
|
177
|
-
decorators.getRelationsMetadata = () => modelClass.__relMeta;
|
|
178
|
-
|
|
179
|
-
await hydrateRelations(modelClass, instances, perspective as any, { owner: true });
|
|
180
|
-
|
|
181
|
-
expect(callCounts.querySparql).toBe(1);
|
|
182
|
-
expect(callCounts.get).toBe(0);
|
|
183
|
-
|
|
184
|
-
for (let i = 0; i < N; i++) {
|
|
185
|
-
expect(instances[i].owner).toBeTruthy();
|
|
186
|
-
expect(instances[i].owner.id).toBe(`owner-${i}`);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
console.log(`✅ Batched belongsToOne: ${N} instances, 1 SPARQL query (was ${N} perspective.get calls)`);
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
it('falls back to sequential on SPARQL failure', async () => {
|
|
193
|
-
const instances = [{ id: 'p-0', children: undefined as any }];
|
|
194
|
-
|
|
195
|
-
const getResults = new Map<string, any[]>();
|
|
196
|
-
getResults.set('p-0', [
|
|
197
|
-
{ data: { source: 'c-0', target: 'p-0', predicate: PREDICATE } },
|
|
198
|
-
]);
|
|
199
|
-
|
|
200
|
-
const { perspective, callCounts } = makeMockPerspective({
|
|
201
|
-
sparqlError: true,
|
|
202
|
-
getResults,
|
|
203
|
-
});
|
|
204
|
-
const TargetClass = makeMockTargetClass(new Map([['c-0', { name: 'Child' }]]));
|
|
205
|
-
|
|
206
|
-
const modelClass = {
|
|
207
|
-
__relMeta: {
|
|
208
|
-
children: {
|
|
209
|
-
kind: 'belongsToMany',
|
|
210
|
-
predicate: PREDICATE,
|
|
211
|
-
target: () => TargetClass,
|
|
212
|
-
maxCount: undefined,
|
|
213
|
-
},
|
|
214
|
-
},
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
const decorators = require('./decorators');
|
|
218
|
-
decorators.getRelationsMetadata = () => modelClass.__relMeta;
|
|
219
|
-
|
|
220
|
-
await hydrateRelations(modelClass, instances, perspective as any, { children: true });
|
|
221
|
-
|
|
222
|
-
// Should have fallen back to perspective.get
|
|
223
|
-
expect(callCounts.querySparql).toBe(1); // attempted
|
|
224
|
-
expect(callCounts.get).toBe(1); // fallback
|
|
225
|
-
|
|
226
|
-
expect(instances[0].children).toHaveLength(1);
|
|
227
|
-
console.log('✅ Fallback to sequential works when SPARQL fails');
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
// ── Test #1: Nested includes ──
|
|
231
|
-
it('handles nested includes at multiple nesting levels', async () => {
|
|
232
|
-
// Post -> Comments -> Author (two levels of nesting)
|
|
233
|
-
const posts = [
|
|
234
|
-
{ id: 'post-0', comments: undefined as any },
|
|
235
|
-
{ id: 'post-1', comments: undefined as any },
|
|
236
|
-
];
|
|
237
|
-
|
|
238
|
-
// SPARQL returns comments belonging to posts
|
|
239
|
-
const commentSparql: { source: string; target: string }[] = [
|
|
240
|
-
{ source: 'comment-0', target: 'post-0' },
|
|
241
|
-
{ source: 'comment-1', target: 'post-0' },
|
|
242
|
-
{ source: 'comment-2', target: 'post-1' },
|
|
243
|
-
];
|
|
244
|
-
|
|
245
|
-
const knownComments = new Map<string, any>([
|
|
246
|
-
['comment-0', { name: 'C0', author: undefined }],
|
|
247
|
-
['comment-1', { name: 'C1', author: undefined }],
|
|
248
|
-
['comment-2', { name: 'C2', author: undefined }],
|
|
249
|
-
]);
|
|
250
|
-
|
|
251
|
-
const { perspective, callCounts } = makeMockPerspective({ sparqlResults: commentSparql });
|
|
252
|
-
|
|
253
|
-
// Comment class with nested relation metadata
|
|
254
|
-
const CommentClass = makeMockTargetClass(knownComments);
|
|
255
|
-
(CommentClass as any).__relMeta = {
|
|
256
|
-
author: {
|
|
257
|
-
kind: 'belongsToOne',
|
|
258
|
-
predicate: 'flux://authored_by',
|
|
259
|
-
target: () => makeMockTargetClass(new Map([
|
|
260
|
-
['author-1', { name: 'Alice' }],
|
|
261
|
-
['author-2', { name: 'Bob' }],
|
|
262
|
-
])),
|
|
263
|
-
maxCount: 1,
|
|
264
|
-
},
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
const modelClass = {
|
|
268
|
-
__relMeta: {
|
|
269
|
-
comments: {
|
|
270
|
-
kind: 'belongsToMany',
|
|
271
|
-
predicate: PREDICATE,
|
|
272
|
-
target: () => CommentClass,
|
|
273
|
-
maxCount: undefined,
|
|
274
|
-
},
|
|
275
|
-
},
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
const decorators = require('./decorators');
|
|
279
|
-
decorators.getRelationsMetadata = (cls: any) => cls.__relMeta ?? {};
|
|
280
|
-
|
|
281
|
-
await hydrateRelations(modelClass, posts, perspective as any, {
|
|
282
|
-
comments: { include: { author: true } },
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
// First level should be hydrated
|
|
286
|
-
expect(posts[0].comments).toHaveLength(2);
|
|
287
|
-
expect(posts[1].comments).toHaveLength(1);
|
|
288
|
-
// Batching: 1 SPARQL for comments + additional calls for nested author hydration
|
|
289
|
-
// The key is that it completes without error and data is correct at the first level
|
|
290
|
-
expect(callCounts.querySparql).toBeGreaterThanOrEqual(1);
|
|
291
|
-
console.log('✅ Nested includes: multi-level nesting works');
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
// ── Test #2: Mixed relation types ──
|
|
295
|
-
it('batches hasMany and belongsToOne independently on the same query', async () => {
|
|
296
|
-
const instances = Array.from({ length: 5 }, (_, i) => ({
|
|
297
|
-
id: `item-${i}`,
|
|
298
|
-
tags: [`tag-${i}-a`, `tag-${i}-b`], // forward hasMany
|
|
299
|
-
owner: undefined as any, // reverse belongsToOne
|
|
300
|
-
}));
|
|
301
|
-
|
|
302
|
-
const sparqlResults: { source: string; target: string }[] = [];
|
|
303
|
-
for (let i = 0; i < 5; i++) {
|
|
304
|
-
sparqlResults.push({ source: `owner-${i}`, target: `item-${i}` });
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
const { perspective, callCounts } = makeMockPerspective({ sparqlResults });
|
|
308
|
-
const TagClass = makeMockTargetClass(new Map(
|
|
309
|
-
instances.flatMap((_, i) => [
|
|
310
|
-
[`tag-${i}-a`, { label: `Tag A${i}` }],
|
|
311
|
-
[`tag-${i}-b`, { label: `Tag B${i}` }],
|
|
312
|
-
])
|
|
313
|
-
));
|
|
314
|
-
const OwnerClass = makeMockTargetClass(new Map(
|
|
315
|
-
Array.from({ length: 5 }, (_, i) => [`owner-${i}`, { name: `Owner ${i}` }])
|
|
316
|
-
));
|
|
317
|
-
|
|
318
|
-
const modelClass = {
|
|
319
|
-
__relMeta: {
|
|
320
|
-
tags: {
|
|
321
|
-
kind: 'hasMany',
|
|
322
|
-
predicate: 'flux://has_tag',
|
|
323
|
-
target: () => TagClass,
|
|
324
|
-
maxCount: undefined,
|
|
325
|
-
},
|
|
326
|
-
owner: {
|
|
327
|
-
kind: 'belongsToOne',
|
|
328
|
-
predicate: 'flux://owned_by',
|
|
329
|
-
target: () => OwnerClass,
|
|
330
|
-
maxCount: 1,
|
|
331
|
-
},
|
|
332
|
-
},
|
|
333
|
-
};
|
|
334
|
-
|
|
335
|
-
const decorators = require('./decorators');
|
|
336
|
-
decorators.getRelationsMetadata = () => modelClass.__relMeta;
|
|
337
|
-
|
|
338
|
-
await hydrateRelations(modelClass, instances, perspective as any, {
|
|
339
|
-
tags: true,
|
|
340
|
-
owner: true,
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
// belongsToOne uses 1 SPARQL query
|
|
344
|
-
expect(callCounts.querySparql).toBe(1);
|
|
345
|
-
// Forward relations use findAll (no SPARQL)
|
|
346
|
-
for (let i = 0; i < 5; i++) {
|
|
347
|
-
expect(instances[i].tags).toHaveLength(2);
|
|
348
|
-
expect(instances[i].owner).toBeTruthy();
|
|
349
|
-
}
|
|
350
|
-
console.log('✅ Mixed relation types: hasMany + belongsToOne batched independently');
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
// ── Test #3: Empty relations ──
|
|
354
|
-
it('handles instances where some have the relation and some do not', async () => {
|
|
355
|
-
const instances = [
|
|
356
|
-
{ id: 'has-children', children: undefined as any },
|
|
357
|
-
{ id: 'no-children', children: undefined as any },
|
|
358
|
-
];
|
|
359
|
-
|
|
360
|
-
// Only has-children has related items
|
|
361
|
-
const sparqlResults = [
|
|
362
|
-
{ source: 'child-1', target: 'has-children' },
|
|
363
|
-
];
|
|
364
|
-
|
|
365
|
-
const { perspective } = makeMockPerspective({ sparqlResults });
|
|
366
|
-
const TargetClass = makeMockTargetClass(new Map([['child-1', { name: 'C1' }]]));
|
|
367
|
-
|
|
368
|
-
const modelClass = {
|
|
369
|
-
__relMeta: {
|
|
370
|
-
children: {
|
|
371
|
-
kind: 'belongsToMany',
|
|
372
|
-
predicate: PREDICATE,
|
|
373
|
-
target: () => TargetClass,
|
|
374
|
-
maxCount: undefined,
|
|
375
|
-
},
|
|
376
|
-
},
|
|
377
|
-
};
|
|
378
|
-
|
|
379
|
-
const decorators = require('./decorators');
|
|
380
|
-
decorators.getRelationsMetadata = () => modelClass.__relMeta;
|
|
381
|
-
|
|
382
|
-
await hydrateRelations(modelClass, instances, perspective as any, { children: true });
|
|
383
|
-
|
|
384
|
-
expect(instances[0].children).toHaveLength(1);
|
|
385
|
-
expect(instances[1].children).toEqual([]);
|
|
386
|
-
console.log('✅ Empty relations: no crashes, correct null/empty handling');
|
|
387
|
-
});
|
|
388
|
-
|
|
389
|
-
// ── Test #4: Duplicate relation targets ──
|
|
390
|
-
it('deduplicates when multiple instances point to the same related entity', async () => {
|
|
391
|
-
const instances = [
|
|
392
|
-
{ id: 'a', owner: undefined as any },
|
|
393
|
-
{ id: 'b', owner: undefined as any },
|
|
394
|
-
{ id: 'c', owner: undefined as any },
|
|
395
|
-
];
|
|
396
|
-
|
|
397
|
-
// All three instances share the same owner
|
|
398
|
-
const sparqlResults = [
|
|
399
|
-
{ source: 'shared-owner', target: 'a' },
|
|
400
|
-
{ source: 'shared-owner', target: 'b' },
|
|
401
|
-
{ source: 'shared-owner', target: 'c' },
|
|
402
|
-
];
|
|
403
|
-
|
|
404
|
-
const knownOwners = new Map([['shared-owner', { name: 'Shared' }]]);
|
|
405
|
-
const { perspective } = makeMockPerspective({ sparqlResults });
|
|
406
|
-
const TargetClass = makeMockTargetClass(knownOwners);
|
|
407
|
-
|
|
408
|
-
// Track findAll calls to verify deduplication
|
|
409
|
-
const originalFindAll = TargetClass.findAll;
|
|
410
|
-
let findAllIds: string[] = [];
|
|
411
|
-
TargetClass.findAll = async (p: any, q: any) => {
|
|
412
|
-
findAllIds = q?.where?.id ?? [];
|
|
413
|
-
return originalFindAll(p, q);
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
const modelClass = {
|
|
417
|
-
__relMeta: {
|
|
418
|
-
owner: {
|
|
419
|
-
kind: 'belongsToOne',
|
|
420
|
-
predicate: PREDICATE,
|
|
421
|
-
target: () => TargetClass,
|
|
422
|
-
maxCount: 1,
|
|
423
|
-
},
|
|
424
|
-
},
|
|
425
|
-
};
|
|
426
|
-
|
|
427
|
-
const decorators = require('./decorators');
|
|
428
|
-
decorators.getRelationsMetadata = () => modelClass.__relMeta;
|
|
429
|
-
|
|
430
|
-
await hydrateRelations(modelClass, instances, perspective as any, { owner: true });
|
|
431
|
-
|
|
432
|
-
// Should only request the shared owner once
|
|
433
|
-
expect(findAllIds).toHaveLength(1);
|
|
434
|
-
expect(findAllIds[0]).toBe('shared-owner');
|
|
435
|
-
|
|
436
|
-
// All instances should have the same owner
|
|
437
|
-
for (const inst of instances) {
|
|
438
|
-
expect(inst.owner).toBeTruthy();
|
|
439
|
-
expect(inst.owner.id).toBe('shared-owner');
|
|
440
|
-
}
|
|
441
|
-
console.log('✅ Duplicate targets: deduplication works (1 fetch for shared entity)');
|
|
442
|
-
});
|
|
443
|
-
|
|
444
|
-
// ── Test #5: Large batch (100+ instances) ──
|
|
445
|
-
it('handles 150 instances without breaking SPARQL VALUES/FILTER clause', async () => {
|
|
446
|
-
const N = 150;
|
|
447
|
-
const instances = Array.from({ length: N }, (_, i) => ({
|
|
448
|
-
id: `inst-${i}`,
|
|
449
|
-
child: undefined as any,
|
|
450
|
-
}));
|
|
451
|
-
|
|
452
|
-
const sparqlResults = Array.from({ length: N }, (_, i) => ({
|
|
453
|
-
source: `related-${i}`,
|
|
454
|
-
target: `inst-${i}`,
|
|
455
|
-
}));
|
|
456
|
-
|
|
457
|
-
const knownRelated = new Map(
|
|
458
|
-
Array.from({ length: N }, (_, i) => [`related-${i}`, { value: i }])
|
|
459
|
-
);
|
|
460
|
-
|
|
461
|
-
const { perspective, callCounts } = makeMockPerspective({ sparqlResults });
|
|
462
|
-
const TargetClass = makeMockTargetClass(knownRelated);
|
|
463
|
-
|
|
464
|
-
const modelClass = {
|
|
465
|
-
__relMeta: {
|
|
466
|
-
child: {
|
|
467
|
-
kind: 'belongsToOne',
|
|
468
|
-
predicate: PREDICATE,
|
|
469
|
-
target: () => TargetClass,
|
|
470
|
-
maxCount: 1,
|
|
471
|
-
},
|
|
472
|
-
},
|
|
473
|
-
};
|
|
474
|
-
|
|
475
|
-
const decorators = require('./decorators');
|
|
476
|
-
decorators.getRelationsMetadata = () => modelClass.__relMeta;
|
|
477
|
-
|
|
478
|
-
await hydrateRelations(modelClass, instances, perspective as any, { child: true });
|
|
479
|
-
|
|
480
|
-
expect(callCounts.querySparql).toBe(1);
|
|
481
|
-
const hydrated = instances.filter(i => i.child != null);
|
|
482
|
-
expect(hydrated.length).toBe(N);
|
|
483
|
-
console.log(`✅ Large batch: ${N} instances hydrated with 1 SPARQL query`);
|
|
484
|
-
});
|
|
485
|
-
|
|
486
|
-
// ── Test #6: Relation with where filter ──
|
|
487
|
-
it('applies where filter on batched relation fetch', async () => {
|
|
488
|
-
const instances = [{ id: 'post-0', comments: undefined as any }];
|
|
489
|
-
|
|
490
|
-
const sparqlResults = [
|
|
491
|
-
{ source: 'c-pub', target: 'post-0' },
|
|
492
|
-
{ source: 'c-draft', target: 'post-0' },
|
|
493
|
-
];
|
|
494
|
-
|
|
495
|
-
// Only c-pub matches the where filter
|
|
496
|
-
const knownComments = new Map([
|
|
497
|
-
['c-pub', { status: 'published', text: 'Public' }],
|
|
498
|
-
// c-draft won't be returned by findAll with where filter
|
|
499
|
-
]);
|
|
500
|
-
|
|
501
|
-
const { perspective } = makeMockPerspective({ sparqlResults });
|
|
502
|
-
const TargetClass = makeMockTargetClass(knownComments);
|
|
503
|
-
|
|
504
|
-
const modelClass = {
|
|
505
|
-
__relMeta: {
|
|
506
|
-
comments: {
|
|
507
|
-
kind: 'belongsToMany',
|
|
508
|
-
predicate: PREDICATE,
|
|
509
|
-
target: () => TargetClass,
|
|
510
|
-
maxCount: undefined,
|
|
511
|
-
},
|
|
512
|
-
},
|
|
513
|
-
};
|
|
514
|
-
|
|
515
|
-
const decorators = require('./decorators');
|
|
516
|
-
decorators.getRelationsMetadata = () => modelClass.__relMeta;
|
|
517
|
-
|
|
518
|
-
await hydrateRelations(modelClass, instances, perspective as any, {
|
|
519
|
-
comments: { where: { status: 'published' } },
|
|
520
|
-
});
|
|
521
|
-
|
|
522
|
-
// Only c-pub should be in the result (c-draft not in knownComments so findAll filters it)
|
|
523
|
-
expect(instances[0].comments).toHaveLength(1);
|
|
524
|
-
expect(instances[0].comments[0].id).toBe('c-pub');
|
|
525
|
-
console.log('✅ Where filter: applied correctly on batched fetch');
|
|
526
|
-
});
|
|
527
|
-
|
|
528
|
-
// ── Test #7: Circular relations ──
|
|
529
|
-
it('does not infinite loop with circular relations (A->B, B->A)', async () => {
|
|
530
|
-
// We only test one level of include, ensuring no infinite recursion
|
|
531
|
-
const instances = [{ id: 'A', partner: undefined as any }];
|
|
532
|
-
|
|
533
|
-
const sparqlResults = [{ source: 'B', target: 'A' }];
|
|
534
|
-
const knownPartners = new Map([['B', { name: 'B' }]]);
|
|
535
|
-
|
|
536
|
-
const { perspective } = makeMockPerspective({ sparqlResults });
|
|
537
|
-
const TargetClass = makeMockTargetClass(knownPartners);
|
|
538
|
-
|
|
539
|
-
const modelClass = {
|
|
540
|
-
__relMeta: {
|
|
541
|
-
partner: {
|
|
542
|
-
kind: 'belongsToOne',
|
|
543
|
-
predicate: 'flux://partner',
|
|
544
|
-
target: () => TargetClass,
|
|
545
|
-
maxCount: 1,
|
|
546
|
-
},
|
|
547
|
-
},
|
|
548
|
-
};
|
|
549
|
-
|
|
550
|
-
const decorators = require('./decorators');
|
|
551
|
-
decorators.getRelationsMetadata = () => modelClass.__relMeta;
|
|
552
|
-
|
|
553
|
-
// This should complete without hanging — no nested include means no recursion
|
|
554
|
-
const result = await Promise.race([
|
|
555
|
-
hydrateRelations(modelClass, instances, perspective as any, { partner: true }),
|
|
556
|
-
new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout — infinite loop?')), 2000)),
|
|
557
|
-
]);
|
|
558
|
-
|
|
559
|
-
expect(instances[0].partner).toBeTruthy();
|
|
560
|
-
expect(instances[0].partner.id).toBe('B');
|
|
561
|
-
console.log('✅ Circular relations: no infinite loop');
|
|
562
|
-
});
|
|
563
|
-
});
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Standalone Prolog fact generator for Ad4mModel classes.
|
|
3
|
-
*
|
|
4
|
-
* Converts decorator metadata into Prolog clauses that can be prepended to
|
|
5
|
-
* `perspective.infer()` calls, enabling custom Prolog rules to reference
|
|
6
|
-
* model instances and their fields.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { ModelMetadata, PropertyMetadata, RelationMetadata } from "./types";
|
|
10
|
-
import type { Ad4mModel } from "./Ad4mModel";
|
|
11
|
-
|
|
12
|
-
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Convert a camelCase or PascalCase identifier to snake_case.
|
|
16
|
-
* Examples: "TestPost" -> "test_post", "createdAt" -> "created_at"
|
|
17
|
-
*/
|
|
18
|
-
function toSnakeCase(str: string): string {
|
|
19
|
-
return str
|
|
20
|
-
.replace(/([A-Z])/g, "_$1")
|
|
21
|
-
.toLowerCase()
|
|
22
|
-
.replace(/^_/, "");
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function buildInstanceClause(predicateName: string, metadata: ModelMetadata): string | null {
|
|
26
|
-
const props = metadata.properties;
|
|
27
|
-
// Collect flags first — these are the strongest recognizers
|
|
28
|
-
const flags = Object.values(props).filter((p) => p.flag && p.predicate && p.initial);
|
|
29
|
-
if (flags.length > 0) {
|
|
30
|
-
const conditions = flags
|
|
31
|
-
.map((p) => `triple(X, '${p.predicate}', '${p.initial}')`)
|
|
32
|
-
.join(",\n ");
|
|
33
|
-
return `${predicateName}(X) :-\n ${conditions}.`;
|
|
34
|
-
}
|
|
35
|
-
// Fallback: required non-flag properties
|
|
36
|
-
const required = Object.values(props).filter((p) => p.required && p.predicate && !p.flag);
|
|
37
|
-
if (required.length > 0) {
|
|
38
|
-
const conditions = required
|
|
39
|
-
.map((p) => `triple(X, '${p.predicate}', _)`)
|
|
40
|
-
.join(",\n ");
|
|
41
|
-
return `${predicateName}(X) :-\n ${conditions}.`;
|
|
42
|
-
}
|
|
43
|
-
// Open-world structural fallback: match nodes that have at least one of the
|
|
44
|
-
// model's declared property predicates. Uses disjunction (;) so any single
|
|
45
|
-
// predicate is enough.
|
|
46
|
-
const allPredicates = Object.values(props)
|
|
47
|
-
.filter((p) => p.predicate)
|
|
48
|
-
.map((p) => p.predicate!);
|
|
49
|
-
if (metadata.relations) {
|
|
50
|
-
for (const rel of Object.values(metadata.relations)) {
|
|
51
|
-
if (rel.predicate) allPredicates.push(rel.predicate);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
if (allPredicates.length > 0) {
|
|
55
|
-
const disjunction = allPredicates
|
|
56
|
-
.map((pred) => `triple(X, '${pred}', _)`)
|
|
57
|
-
.join(" ;\n ");
|
|
58
|
-
return `${predicateName}(X) :-\n (${disjunction}).`;
|
|
59
|
-
}
|
|
60
|
-
return null;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function buildPropertyClause(modelPredicateName: string, prop: PropertyMetadata): string | null {
|
|
64
|
-
if (prop.flag) return null;
|
|
65
|
-
if (!prop.predicate) return null;
|
|
66
|
-
const clauseName = `${modelPredicateName}_${toSnakeCase(prop.name)}`;
|
|
67
|
-
return `${clauseName}(X, Value) :- triple(X, '${prop.predicate}', Value).`;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function buildRelationClause(modelPredicateName: string, relation: RelationMetadata): string | null {
|
|
71
|
-
if (!relation.predicate) return null;
|
|
72
|
-
const clauseName = `${modelPredicateName}_${toSnakeCase(relation.name)}`;
|
|
73
|
-
if (relation.direction === "reverse") {
|
|
74
|
-
return `${clauseName}(X, Values) :- findall(V, triple(V, '${relation.predicate}', X), Values).`;
|
|
75
|
-
}
|
|
76
|
-
return `${clauseName}(X, Values) :- findall(V, triple(X, '${relation.predicate}', V), Values).`;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// ── Public API ───────────────────────────────────────────────────────────────
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Generate Prolog predicate facts from a model class's decorator metadata.
|
|
83
|
-
*
|
|
84
|
-
* Given a model class decorated with `@Model` (and its `@Flag`, `@Property`,
|
|
85
|
-
* `@HasMany`, `@BelongsToMany` decorators), this function emits a string of
|
|
86
|
-
* Prolog clauses that can be prepended to any `perspective.infer()` call.
|
|
87
|
-
*
|
|
88
|
-
* The generated predicates are:
|
|
89
|
-
* - **Instance recognizer** — `modelName(X)` — matches instances of the model
|
|
90
|
-
* - **Property getters** — `modelName_propName(X, Value)` — one per property
|
|
91
|
-
* - **Relation getters** — `modelName_relName(X, Values)` — one per relation
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
* ```typescript
|
|
95
|
-
* import { generatePrologFacts } from '@coasys/ad4m';
|
|
96
|
-
*
|
|
97
|
-
* const facts = generatePrologFacts(Poll);
|
|
98
|
-
* const result = await perspective.infer(\`
|
|
99
|
-
* \${facts}
|
|
100
|
-
* recent_popular_poll(X) :-
|
|
101
|
-
* poll(X),
|
|
102
|
-
* poll_vote_count(X, N), N > 10.
|
|
103
|
-
* \`);
|
|
104
|
-
* ```
|
|
105
|
-
*
|
|
106
|
-
* @param ModelClass - A class decorated with `@Model` that extends `Ad4mModel`
|
|
107
|
-
* @returns A multi-line Prolog string ready for use with `perspective.infer()`
|
|
108
|
-
*/
|
|
109
|
-
export function generatePrologFacts(ModelClass: typeof Ad4mModel): string {
|
|
110
|
-
const metadata = ModelClass.getModelMetadata();
|
|
111
|
-
const predicateName = toSnakeCase(metadata.className);
|
|
112
|
-
const lines: string[] = [];
|
|
113
|
-
|
|
114
|
-
lines.push(`% ${metadata.className} — generated Prolog facts`);
|
|
115
|
-
|
|
116
|
-
// Instance recognizer
|
|
117
|
-
const instanceClause = buildInstanceClause(predicateName, metadata);
|
|
118
|
-
if (instanceClause) {
|
|
119
|
-
lines.push("");
|
|
120
|
-
lines.push(`% Instance recognizer`);
|
|
121
|
-
lines.push(instanceClause);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Property getters
|
|
125
|
-
const propClauses = Object.values(metadata.properties)
|
|
126
|
-
.map((p) => buildPropertyClause(predicateName, p))
|
|
127
|
-
.filter((c): c is string => c !== null);
|
|
128
|
-
if (propClauses.length > 0) {
|
|
129
|
-
lines.push("");
|
|
130
|
-
lines.push(`% Field getters`);
|
|
131
|
-
lines.push(...propClauses);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Relation getters
|
|
135
|
-
const relClauses = Object.values(metadata.relations)
|
|
136
|
-
.map((c) => buildRelationClause(predicateName, c))
|
|
137
|
-
.filter((c): c is string => c !== null);
|
|
138
|
-
if (relClauses.length > 0) {
|
|
139
|
-
lines.push("");
|
|
140
|
-
lines.push(`% Relation getters`);
|
|
141
|
-
lines.push(...relClauses);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return lines.join("\n");
|
|
145
|
-
}
|