@coasys/ad4m 0.13.0-test-8-profile-cache-fix → 0.13.0-test-9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.cjs +136 -18
- package/lib/index.js +136 -18
- package/lib/src/generated/rest/AIModelLoadingStatus.d.ts +7 -0
- package/lib/src/generated/rest/AIModelLoadingStatus.js +2 -0
- package/lib/src/generated/rest/AddAgentInfosRequest.d.ts +3 -0
- package/lib/src/generated/rest/AddAgentInfosRequest.js +2 -0
- package/lib/src/generated/rest/AddLinkExpressionRequest.d.ts +6 -0
- package/lib/src/generated/rest/AddLinkExpressionRequest.js +1 -0
- package/lib/src/generated/rest/AddLinkRequest.d.ts +6 -0
- package/lib/src/generated/rest/AddLinkRequest.js +1 -0
- package/lib/src/generated/rest/AddLinksBulkRequest.d.ts +6 -0
- package/lib/src/generated/rest/AddLinksBulkRequest.js +1 -0
- package/lib/src/generated/rest/AddSdnaRequest.d.ts +6 -0
- package/lib/src/generated/rest/AddSdnaRequest.js +2 -0
- package/lib/src/generated/rest/ApplyTemplateRequest.d.ts +4 -0
- package/lib/src/generated/rest/ApplyTemplateRequest.js +2 -0
- package/lib/src/generated/rest/AuthInfoInput.d.ts +10 -0
- package/lib/src/generated/rest/AuthInfoInput.js +1 -0
- package/lib/src/generated/rest/BroadcastRequest.d.ts +6 -0
- package/lib/src/generated/rest/BroadcastRequest.js +1 -0
- package/lib/src/generated/rest/CapabilityInput.d.ts +5 -0
- package/lib/src/generated/rest/CapabilityInput.js +1 -0
- package/lib/src/generated/rest/CommitBatchRequest.d.ts +3 -0
- package/lib/src/generated/rest/CommitBatchRequest.js +2 -0
- package/lib/src/generated/rest/ComputeLogEntry.d.ts +9 -0
- package/lib/src/generated/rest/ComputeLogEntry.js +2 -0
- package/lib/src/generated/rest/CreateExpressionRequest.d.ts +4 -0
- package/lib/src/generated/rest/CreateExpressionRequest.js +2 -0
- package/lib/src/generated/rest/CreatePerspectiveRequest.d.ts +3 -0
- package/lib/src/generated/rest/CreatePerspectiveRequest.js +2 -0
- package/lib/src/generated/rest/CreateSubjectRequest.d.ts +6 -0
- package/lib/src/generated/rest/CreateSubjectRequest.js +2 -0
- package/lib/src/generated/rest/CreateUserRequest.d.ts +4 -0
- package/lib/src/generated/rest/CreateUserRequest.js +2 -0
- package/lib/src/generated/rest/CreateUserResponse.d.ts +5 -0
- package/lib/src/generated/rest/CreateUserResponse.js +2 -0
- package/lib/src/generated/rest/DecoratedExpressionProof.d.ts +6 -0
- package/lib/src/generated/rest/DecoratedExpressionProof.js +2 -0
- package/lib/src/generated/rest/DecoratedLinkExpression.d.ts +10 -0
- package/lib/src/generated/rest/DecoratedLinkExpression.js +1 -0
- package/lib/src/generated/rest/DisposeQueryRequest.d.ts +3 -0
- package/lib/src/generated/rest/DisposeQueryRequest.js +2 -0
- package/lib/src/generated/rest/EmailTestRequest.d.ts +6 -0
- package/lib/src/generated/rest/EmailTestRequest.js +2 -0
- package/lib/src/generated/rest/EmbedRequest.d.ts +4 -0
- package/lib/src/generated/rest/EmbedRequest.js +2 -0
- package/lib/src/generated/rest/EntanglementProofInput.d.ts +6 -0
- package/lib/src/generated/rest/EntanglementProofInput.js +2 -0
- package/lib/src/generated/rest/EntanglementProofPreflightRequest.d.ts +4 -0
- package/lib/src/generated/rest/EntanglementProofPreflightRequest.js +2 -0
- package/lib/src/generated/rest/ExecuteCommandsRequest.d.ts +4 -0
- package/lib/src/generated/rest/ExecuteCommandsRequest.js +2 -0
- package/lib/src/generated/rest/ExportRequest.d.ts +5 -0
- package/lib/src/generated/rest/ExportRequest.js +2 -0
- package/lib/src/generated/rest/ExpressionManyRequest.d.ts +3 -0
- package/lib/src/generated/rest/ExpressionManyRequest.js +2 -0
- package/lib/src/generated/rest/ExpressionProof.d.ts +4 -0
- package/lib/src/generated/rest/ExpressionProof.js +2 -0
- package/lib/src/generated/rest/ExpressionProofInput.d.ts +6 -0
- package/lib/src/generated/rest/ExpressionProofInput.js +2 -0
- package/lib/src/generated/rest/FriendSendMessageRequest.d.ts +3 -0
- package/lib/src/generated/rest/FriendSendMessageRequest.js +2 -0
- package/lib/src/generated/rest/FriendsListRequest.d.ts +3 -0
- package/lib/src/generated/rest/FriendsListRequest.js +2 -0
- package/lib/src/generated/rest/GenerateAgentRequest.d.ts +3 -0
- package/lib/src/generated/rest/GenerateAgentRequest.js +2 -0
- package/lib/src/generated/rest/GenerateJwtRequest.d.ts +4 -0
- package/lib/src/generated/rest/GenerateJwtRequest.js +2 -0
- package/lib/src/generated/rest/GetSubjectDataRequest.d.ts +4 -0
- package/lib/src/generated/rest/GetSubjectDataRequest.js +2 -0
- package/lib/src/generated/rest/HostingInfoResponse.d.ts +5 -0
- package/lib/src/generated/rest/HostingInfoResponse.js +2 -0
- package/lib/src/generated/rest/HostingWalletResponse.d.ts +4 -0
- package/lib/src/generated/rest/HostingWalletResponse.js +2 -0
- package/lib/src/generated/rest/ImportAgentRequest.d.ts +6 -0
- package/lib/src/generated/rest/ImportAgentRequest.js +2 -0
- package/lib/src/generated/rest/ImportRequest.d.ts +4 -0
- package/lib/src/generated/rest/ImportRequest.js +2 -0
- package/lib/src/generated/rest/JoinNeighbourhoodRequest.d.ts +3 -0
- package/lib/src/generated/rest/JoinNeighbourhoodRequest.js +2 -0
- package/lib/src/generated/rest/KeepAliveQueryRequest.d.ts +3 -0
- package/lib/src/generated/rest/KeepAliveQueryRequest.js +2 -0
- package/lib/src/generated/rest/LanguageMetaInput.d.ts +6 -0
- package/lib/src/generated/rest/LanguageMetaInput.js +2 -0
- package/lib/src/generated/rest/Link.d.ts +5 -0
- package/lib/src/generated/rest/Link.js +2 -0
- package/lib/src/generated/rest/LinkExpression.d.ts +10 -0
- package/lib/src/generated/rest/LinkExpression.js +1 -0
- package/lib/src/generated/rest/LinkExpressionInput.d.ts +10 -0
- package/lib/src/generated/rest/LinkExpressionInput.js +1 -0
- package/lib/src/generated/rest/LinkInput.d.ts +5 -0
- package/lib/src/generated/rest/LinkInput.js +2 -0
- package/lib/src/generated/rest/LinkLanguageTemplatesRequest.d.ts +3 -0
- package/lib/src/generated/rest/LinkLanguageTemplatesRequest.js +2 -0
- package/lib/src/generated/rest/LinkMutationRequest.d.ts +12 -0
- package/lib/src/generated/rest/LinkMutationRequest.js +1 -0
- package/lib/src/generated/rest/LinkMutationResponse.d.ts +9 -0
- package/lib/src/generated/rest/LinkMutationResponse.js +1 -0
- package/lib/src/generated/rest/LinkMutations.d.ts +6 -0
- package/lib/src/generated/rest/LinkMutations.js +1 -0
- package/lib/src/generated/rest/LinkMutationsRequest.d.ts +5 -0
- package/lib/src/generated/rest/LinkMutationsRequest.js +1 -0
- package/lib/src/generated/rest/LinkStatus.d.ts +1 -0
- package/lib/src/generated/rest/LinkStatus.js +2 -0
- package/lib/src/generated/rest/LinkUpdateInput.d.ts +5 -0
- package/lib/src/generated/rest/LinkUpdateInput.js +1 -0
- package/lib/src/generated/rest/LockAgentRequest.d.ts +3 -0
- package/lib/src/generated/rest/LockAgentRequest.js +2 -0
- package/lib/src/generated/rest/LoginUserRequest.d.ts +5 -0
- package/lib/src/generated/rest/LoginUserRequest.js +2 -0
- package/lib/src/generated/rest/ModelType.d.ts +1 -0
- package/lib/src/generated/rest/ModelType.js +2 -0
- package/lib/src/generated/rest/NotificationGrantRequest.d.ts +3 -0
- package/lib/src/generated/rest/NotificationGrantRequest.js +2 -0
- package/lib/src/generated/rest/NotificationInput.d.ts +10 -0
- package/lib/src/generated/rest/NotificationInput.js +2 -0
- package/lib/src/generated/rest/OpenLinkRequest.d.ts +3 -0
- package/lib/src/generated/rest/OpenLinkRequest.js +2 -0
- package/lib/src/generated/rest/PermitCapabilityRequest.d.ts +3 -0
- package/lib/src/generated/rest/PermitCapabilityRequest.js +2 -0
- package/lib/src/generated/rest/PerspectiveInput.d.ts +4 -0
- package/lib/src/generated/rest/PerspectiveInput.js +1 -0
- package/lib/src/generated/rest/PromptRequest.d.ts +4 -0
- package/lib/src/generated/rest/PromptRequest.js +2 -0
- package/lib/src/generated/rest/PublicPerspectiveInput.d.ts +4 -0
- package/lib/src/generated/rest/PublicPerspectiveInput.js +1 -0
- package/lib/src/generated/rest/PublishLanguageRequest.d.ts +5 -0
- package/lib/src/generated/rest/PublishLanguageRequest.js +1 -0
- package/lib/src/generated/rest/PublishNeighbourhoodRequest.d.ts +6 -0
- package/lib/src/generated/rest/PublishNeighbourhoodRequest.js +1 -0
- package/lib/src/generated/rest/QueryRequest.d.ts +7 -0
- package/lib/src/generated/rest/QueryRequest.js +2 -0
- package/lib/src/generated/rest/RemoveLinkRequest.d.ts +5 -0
- package/lib/src/generated/rest/RemoveLinkRequest.js +1 -0
- package/lib/src/generated/rest/RemoveLinksBulkRequest.d.ts +5 -0
- package/lib/src/generated/rest/RemoveLinksBulkRequest.js +1 -0
- package/lib/src/generated/rest/RequestCapabilityRequest.d.ts +4 -0
- package/lib/src/generated/rest/RequestCapabilityRequest.js +1 -0
- package/lib/src/generated/rest/RequestPaymentRequest.d.ts +3 -0
- package/lib/src/generated/rest/RequestPaymentRequest.js +2 -0
- package/lib/src/generated/rest/RequestVerificationRequest.d.ts +4 -0
- package/lib/src/generated/rest/RequestVerificationRequest.js +2 -0
- package/lib/src/generated/rest/ResourceInput.d.ts +4 -0
- package/lib/src/generated/rest/ResourceInput.js +2 -0
- package/lib/src/generated/rest/SetDefaultModelRequest.d.ts +4 -0
- package/lib/src/generated/rest/SetDefaultModelRequest.js +1 -0
- package/lib/src/generated/rest/SetFreeHostingEnabledRequest.d.ts +3 -0
- package/lib/src/generated/rest/SetFreeHostingEnabledRequest.js +2 -0
- package/lib/src/generated/rest/SetHotWalletAddressRequest.d.ts +3 -0
- package/lib/src/generated/rest/SetHotWalletAddressRequest.js +2 -0
- package/lib/src/generated/rest/SetMultiUserRequest.d.ts +3 -0
- package/lib/src/generated/rest/SetMultiUserRequest.js +2 -0
- package/lib/src/generated/rest/SetOnlineStatusRequest.d.ts +5 -0
- package/lib/src/generated/rest/SetOnlineStatusRequest.js +1 -0
- package/lib/src/generated/rest/SetStatusRequest.d.ts +3 -0
- package/lib/src/generated/rest/SetStatusRequest.js +2 -0
- package/lib/src/generated/rest/SetUserFreeAccessRequest.d.ts +4 -0
- package/lib/src/generated/rest/SetUserFreeAccessRequest.js +2 -0
- package/lib/src/generated/rest/SignMessageRequest.d.ts +3 -0
- package/lib/src/generated/rest/SignMessageRequest.js +2 -0
- package/lib/src/generated/rest/SignalRequest.d.ts +6 -0
- package/lib/src/generated/rest/SignalRequest.js +1 -0
- package/lib/src/generated/rest/SubscribeQueryRequest.d.ts +3 -0
- package/lib/src/generated/rest/SubscribeQueryRequest.js +2 -0
- package/lib/src/generated/rest/SubscribeQueryResponse.d.ts +4 -0
- package/lib/src/generated/rest/SubscribeQueryResponse.js +2 -0
- package/lib/src/generated/rest/TrustedAgentsRequest.d.ts +3 -0
- package/lib/src/generated/rest/TrustedAgentsRequest.js +2 -0
- package/lib/src/generated/rest/UnlockAgentRequest.d.ts +4 -0
- package/lib/src/generated/rest/UnlockAgentRequest.js +2 -0
- package/lib/src/generated/rest/UpdateLinkRequest.d.ts +7 -0
- package/lib/src/generated/rest/UpdateLinkRequest.js +1 -0
- package/lib/src/generated/rest/UpdatePerspectiveRequest.d.ts +3 -0
- package/lib/src/generated/rest/UpdatePerspectiveRequest.js +2 -0
- package/lib/src/generated/rest/UpdateProfileRequest.d.ts +5 -0
- package/lib/src/generated/rest/UpdateProfileRequest.js +1 -0
- package/lib/src/generated/rest/UserStatistics.d.ts +9 -0
- package/lib/src/generated/rest/UserStatistics.js +2 -0
- package/lib/src/generated/rest/VerificationRequestResult.d.ts +6 -0
- package/lib/src/generated/rest/VerificationRequestResult.js +2 -0
- package/lib/src/generated/rest/VerifyEmailRequest.d.ts +6 -0
- package/lib/src/generated/rest/VerifyEmailRequest.js +2 -0
- package/lib/src/generated/rest/VerifySignatureRequest.d.ts +5 -0
- package/lib/src/generated/rest/VerifySignatureRequest.js +2 -0
- package/lib/src/generated/rest/WriteSettingsRequest.d.ts +3 -0
- package/lib/src/generated/rest/WriteSettingsRequest.js +2 -0
- package/lib/src/generated/rest/index.d.ts +73 -0
- package/lib/src/generated/rest/index.js +8 -0
- package/lib/src/generated/rest/routes.d.ts +587 -0
- package/lib/src/generated/rest/routes.js +3 -0
- package/lib/src/model/Ad4mModel.test.js +735 -480
- package/lib/src/model/decorators.d.ts +20 -1
- package/lib/src/model/decorators.js +33 -2
- package/lib/src/model/query-sparql.d.ts +1 -0
- package/lib/src/model/query-sparql.js +13 -0
- package/lib/src/model/query-sparql.test.js +10 -92
- package/lib/src/model/query-surreal.d.ts +44 -0
- package/lib/src/model/query-surreal.js +249 -0
- package/lib/src/model/shacl-gen.d.ts +15 -1
- package/lib/src/model/shacl-gen.js +23 -1
- package/lib/src/model/surreal-utils.d.ts +42 -0
- package/lib/src/model/surreal-utils.js +128 -0
- package/lib/src/model/types.d.ts +14 -3
- package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +33 -33
- package/lib/src/perspectives/PerspectiveProxy.d.ts +17 -0
- package/lib/src/perspectives/PerspectiveProxy.js +67 -15
- package/lib/src/perspectives/PerspectiveProxy.test.d.ts +1 -0
- package/lib/src/perspectives/PerspectiveProxy.test.js +34 -0
- package/lib/src/restClient.d.ts +52 -0
- package/lib/src/restClient.js +227 -0
- package/lib/src/shacl/SHACLShape.test.js +0 -82
- package/package.json +11 -8
- package/src/model/Ad4mModel.test.ts +103 -0
- package/src/model/decorators.ts +37 -3
- package/src/model/query-sparql.test.ts +148 -0
- package/src/model/query-sparql.ts +15 -0
- package/src/model/shacl-gen.ts +23 -0
- package/src/model/types.ts +28 -6
- package/src/perspectives/PerspectiveProxy.ts +73 -20
- package/lib/src/model/model-manifest.d.ts +0 -41
- package/lib/src/model/model-manifest.js +0 -11
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/** Error thrown by RestClient when an RPC call fails. */
|
|
2
|
+
export class RestError extends Error {
|
|
3
|
+
constructor(status, body) {
|
|
4
|
+
super(`RPC error ${status}: ${body}`);
|
|
5
|
+
this.name = 'RestError';
|
|
6
|
+
this.status = status;
|
|
7
|
+
this.body = body;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/** Default RPC call timeout in milliseconds (30 seconds). */
|
|
11
|
+
const DEFAULT_TIMEOUT_MS = 30000;
|
|
12
|
+
/** Maximum reconnect delay in ms. */
|
|
13
|
+
const MAX_RECONNECT_DELAY_MS = 30000;
|
|
14
|
+
/** Initial reconnect delay in ms. */
|
|
15
|
+
const INITIAL_RECONNECT_DELAY_MS = 500;
|
|
16
|
+
/** Counter for generating unique request IDs. */
|
|
17
|
+
let _idCounter = 0;
|
|
18
|
+
function nextId() {
|
|
19
|
+
return String(++_idCounter);
|
|
20
|
+
}
|
|
21
|
+
export class RestClient {
|
|
22
|
+
constructor(baseUrl, token) {
|
|
23
|
+
// ── WebSocket RPC core ──────────────────────────────────────────────────
|
|
24
|
+
this._ws = null;
|
|
25
|
+
this._wsCallbacks = new Set();
|
|
26
|
+
this._pendingCalls = new Map();
|
|
27
|
+
this._wsReady = null;
|
|
28
|
+
this._wsReadyResolve = null;
|
|
29
|
+
this._wsReconnectTimer = null;
|
|
30
|
+
this._wsReconnectDelay = INITIAL_RECONNECT_DELAY_MS;
|
|
31
|
+
this._wsClosed = false;
|
|
32
|
+
this._wsPingTimer = null;
|
|
33
|
+
this.baseUrl = baseUrl;
|
|
34
|
+
this.token = token;
|
|
35
|
+
}
|
|
36
|
+
getBaseUrl() { return this.baseUrl; }
|
|
37
|
+
getToken() { return this.token; }
|
|
38
|
+
setToken(token) {
|
|
39
|
+
this.token = token;
|
|
40
|
+
}
|
|
41
|
+
_getWsUrl() {
|
|
42
|
+
const wsBase = this.baseUrl
|
|
43
|
+
.replace(/^http:\/\//, 'ws://')
|
|
44
|
+
.replace(/^https:\/\//, 'wss://');
|
|
45
|
+
const tokenParam = this.token ? `token=${encodeURIComponent(this.token)}` : '';
|
|
46
|
+
const path = '/api/v1/ws';
|
|
47
|
+
return tokenParam ? `${wsBase}${path}?${tokenParam}` : `${wsBase}${path}`;
|
|
48
|
+
}
|
|
49
|
+
_ensureWs() {
|
|
50
|
+
if (this._ws && (this._ws.readyState === WebSocket.OPEN || this._ws.readyState === WebSocket.CONNECTING)) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
this._wsClosed = false;
|
|
54
|
+
this._wsReady = new Promise((resolve) => {
|
|
55
|
+
this._wsReadyResolve = resolve;
|
|
56
|
+
});
|
|
57
|
+
const url = this._getWsUrl();
|
|
58
|
+
const ws = new WebSocket(url);
|
|
59
|
+
this._ws = ws;
|
|
60
|
+
ws.onopen = () => {
|
|
61
|
+
this._wsReconnectDelay = INITIAL_RECONNECT_DELAY_MS;
|
|
62
|
+
if (this._wsReadyResolve) {
|
|
63
|
+
this._wsReadyResolve();
|
|
64
|
+
this._wsReadyResolve = null;
|
|
65
|
+
}
|
|
66
|
+
this._startPing();
|
|
67
|
+
};
|
|
68
|
+
ws.onmessage = (event) => {
|
|
69
|
+
let parsed;
|
|
70
|
+
try {
|
|
71
|
+
parsed = JSON.parse(event.data);
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
console.error('Error parsing WebSocket data:', e);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
// Ignore pong messages
|
|
78
|
+
if (parsed.type === 'pong')
|
|
79
|
+
return;
|
|
80
|
+
// Check if this is a response to a pending RPC call (has an id field)
|
|
81
|
+
const id = parsed.id;
|
|
82
|
+
if (id && this._pendingCalls.has(id)) {
|
|
83
|
+
const pending = this._pendingCalls.get(id);
|
|
84
|
+
this._pendingCalls.delete(id);
|
|
85
|
+
clearTimeout(pending.timer);
|
|
86
|
+
if (parsed.error) {
|
|
87
|
+
const err = parsed.error;
|
|
88
|
+
pending.reject(new RestError(err.code ?? 500, err.message ?? 'Unknown error'));
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
pending.resolve(parsed.result);
|
|
92
|
+
}
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
// Server-push event (no id, or id not in pending) → route to subscribers
|
|
96
|
+
for (const cb of this._wsCallbacks)
|
|
97
|
+
cb(parsed);
|
|
98
|
+
};
|
|
99
|
+
ws.onerror = (e) => {
|
|
100
|
+
console.error('WebSocket error:', e);
|
|
101
|
+
};
|
|
102
|
+
ws.onclose = () => {
|
|
103
|
+
this._stopPing();
|
|
104
|
+
this._ws = null;
|
|
105
|
+
// Reset the readiness promise so future calls reconnect
|
|
106
|
+
this._wsReady = null;
|
|
107
|
+
// Reject all pending calls
|
|
108
|
+
for (const [id, pending] of this._pendingCalls) {
|
|
109
|
+
clearTimeout(pending.timer);
|
|
110
|
+
pending.reject(new RestError(503, 'WebSocket connection closed'));
|
|
111
|
+
}
|
|
112
|
+
this._pendingCalls.clear();
|
|
113
|
+
if (!this._wsClosed && (this._wsCallbacks.size > 0 || this._pendingCalls.size > 0)) {
|
|
114
|
+
this._scheduleReconnect();
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
_startPing() {
|
|
119
|
+
this._stopPing();
|
|
120
|
+
this._wsPingTimer = setInterval(() => {
|
|
121
|
+
if (this._ws && this._ws.readyState === WebSocket.OPEN) {
|
|
122
|
+
this._ws.send(JSON.stringify({ type: 'ping' }));
|
|
123
|
+
}
|
|
124
|
+
}, 30000);
|
|
125
|
+
}
|
|
126
|
+
_stopPing() {
|
|
127
|
+
if (this._wsPingTimer) {
|
|
128
|
+
clearInterval(this._wsPingTimer);
|
|
129
|
+
this._wsPingTimer = null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
_scheduleReconnect() {
|
|
133
|
+
if (this._wsReconnectTimer)
|
|
134
|
+
return;
|
|
135
|
+
const delay = this._wsReconnectDelay;
|
|
136
|
+
this._wsReconnectDelay = Math.min(this._wsReconnectDelay * 2, MAX_RECONNECT_DELAY_MS);
|
|
137
|
+
this._wsReconnectTimer = setTimeout(() => {
|
|
138
|
+
this._wsReconnectTimer = null;
|
|
139
|
+
if (!this._wsClosed) {
|
|
140
|
+
this._ensureWs();
|
|
141
|
+
}
|
|
142
|
+
}, delay);
|
|
143
|
+
}
|
|
144
|
+
/** Ensure WS is connected and ready. Lazy — connects on first use. */
|
|
145
|
+
async _ready() {
|
|
146
|
+
this._ensureWs();
|
|
147
|
+
if (this._wsReady)
|
|
148
|
+
await this._wsReady;
|
|
149
|
+
}
|
|
150
|
+
// ── RPC call method ─────────────────────────────────────────────────────
|
|
151
|
+
/**
|
|
152
|
+
* Send an RPC call over the WebSocket connection.
|
|
153
|
+
* @param type - The operation type (e.g. 'agent.get', 'perspective.all')
|
|
154
|
+
* @param params - Optional parameters to include in the message
|
|
155
|
+
* @returns Promise that resolves with the result from the server
|
|
156
|
+
*/
|
|
157
|
+
async call(type, params) {
|
|
158
|
+
await this._ready();
|
|
159
|
+
const id = nextId();
|
|
160
|
+
// Put params under a "params" key to avoid collision with
|
|
161
|
+
// protocol fields "id" and "type" (e.g. params might contain
|
|
162
|
+
// { id: modelId } or { type: "db" }).
|
|
163
|
+
const message = { id, type, params: params || {} };
|
|
164
|
+
return new Promise((resolve, reject) => {
|
|
165
|
+
const timer = setTimeout(() => {
|
|
166
|
+
this._pendingCalls.delete(id);
|
|
167
|
+
reject(new RestError(408, `RPC call '${type}' timed out after ${DEFAULT_TIMEOUT_MS}ms`));
|
|
168
|
+
}, DEFAULT_TIMEOUT_MS);
|
|
169
|
+
this._pendingCalls.set(id, {
|
|
170
|
+
resolve: resolve,
|
|
171
|
+
reject,
|
|
172
|
+
timer,
|
|
173
|
+
});
|
|
174
|
+
if (this._ws && this._ws.readyState === WebSocket.OPEN) {
|
|
175
|
+
this._ws.send(JSON.stringify(message));
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
this._pendingCalls.delete(id);
|
|
179
|
+
clearTimeout(timer);
|
|
180
|
+
reject(new RestError(503, 'WebSocket not connected'));
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
// ── Event subscriptions (same interface as before) ──────────────────────
|
|
185
|
+
subscribe(path, callback) {
|
|
186
|
+
this._wsCallbacks.add(callback);
|
|
187
|
+
this._ensureWs();
|
|
188
|
+
return () => {
|
|
189
|
+
this._wsCallbacks.delete(callback);
|
|
190
|
+
if (this._wsCallbacks.size === 0 && this._pendingCalls.size === 0) {
|
|
191
|
+
this._closeWs();
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/** Wait until the WebSocket connection is established. */
|
|
196
|
+
async waitForSubscription(path) {
|
|
197
|
+
await this._ready();
|
|
198
|
+
}
|
|
199
|
+
// ── Explicit connection ─────────────────────────────────────────────────
|
|
200
|
+
/** Explicitly connect the WebSocket (optional — connection is lazy). */
|
|
201
|
+
connect() {
|
|
202
|
+
this._ensureWs();
|
|
203
|
+
}
|
|
204
|
+
_closeWs() {
|
|
205
|
+
this._stopPing();
|
|
206
|
+
if (this._wsReconnectTimer) {
|
|
207
|
+
clearTimeout(this._wsReconnectTimer);
|
|
208
|
+
this._wsReconnectTimer = null;
|
|
209
|
+
}
|
|
210
|
+
if (this._ws) {
|
|
211
|
+
this._wsClosed = true;
|
|
212
|
+
this._ws.close();
|
|
213
|
+
this._ws = null;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/** Close all open WebSocket connections and reject pending calls. */
|
|
217
|
+
closeAll() {
|
|
218
|
+
// Reject pending calls
|
|
219
|
+
for (const [id, pending] of this._pendingCalls) {
|
|
220
|
+
clearTimeout(pending.timer);
|
|
221
|
+
pending.reject(new RestError(503, 'Client closed'));
|
|
222
|
+
}
|
|
223
|
+
this._pendingCalls.clear();
|
|
224
|
+
this._closeWs();
|
|
225
|
+
this._wsCallbacks.clear();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -333,86 +333,4 @@ describe('SHACLShape', () => {
|
|
|
333
333
|
expect(prop.remover).toEqual(original.properties[0].remover);
|
|
334
334
|
});
|
|
335
335
|
});
|
|
336
|
-
describe('sh:in round-trip', () => {
|
|
337
|
-
it('preserves sh:in enum values through toLinks() → fromLinks()', () => {
|
|
338
|
-
const original = new SHACLShape('test://Model');
|
|
339
|
-
original.addProperty({
|
|
340
|
-
name: 'status',
|
|
341
|
-
path: 'test://status',
|
|
342
|
-
datatype: 'xsd:string',
|
|
343
|
-
in: [
|
|
344
|
-
{ value: 'active', label: 'Active' },
|
|
345
|
-
{ value: 'inactive', label: 'Inactive' },
|
|
346
|
-
{ value: 'archived' },
|
|
347
|
-
],
|
|
348
|
-
});
|
|
349
|
-
const links = original.toLinks();
|
|
350
|
-
// Verify sh:in link was created
|
|
351
|
-
const inLink = links.find(l => l.predicate === 'sh://in');
|
|
352
|
-
expect(inLink).toBeDefined();
|
|
353
|
-
expect(inLink.target).toContain('literal:string:');
|
|
354
|
-
// Reconstruct and verify
|
|
355
|
-
const reconstructed = SHACLShape.fromLinks(links, 'test://ModelShape');
|
|
356
|
-
expect(reconstructed.properties[0].in).toBeDefined();
|
|
357
|
-
expect(reconstructed.properties[0].in).toEqual([
|
|
358
|
-
{ value: 'active', label: 'Active' },
|
|
359
|
-
{ value: 'inactive', label: 'Inactive' },
|
|
360
|
-
{ value: 'archived' },
|
|
361
|
-
]);
|
|
362
|
-
});
|
|
363
|
-
it('handles sh:in with special characters in values', () => {
|
|
364
|
-
const original = new SHACLShape('test://Model');
|
|
365
|
-
original.addProperty({
|
|
366
|
-
name: 'category',
|
|
367
|
-
path: 'test://category',
|
|
368
|
-
in: [
|
|
369
|
-
{ value: 'food & drink', label: 'Food & Drink' },
|
|
370
|
-
{ value: 'arts/crafts' },
|
|
371
|
-
],
|
|
372
|
-
});
|
|
373
|
-
const links = original.toLinks();
|
|
374
|
-
const reconstructed = SHACLShape.fromLinks(links, 'test://ModelShape');
|
|
375
|
-
expect(reconstructed.properties[0].in).toEqual(original.properties[0].in);
|
|
376
|
-
});
|
|
377
|
-
it('handles sh:in with empty array (no link created)', () => {
|
|
378
|
-
const original = new SHACLShape('test://Model');
|
|
379
|
-
original.addProperty({
|
|
380
|
-
name: 'field',
|
|
381
|
-
path: 'test://field',
|
|
382
|
-
in: [],
|
|
383
|
-
});
|
|
384
|
-
const links = original.toLinks();
|
|
385
|
-
const inLink = links.find(l => l.predicate === 'sh://in');
|
|
386
|
-
expect(inLink).toBeUndefined();
|
|
387
|
-
});
|
|
388
|
-
it('handles sh:in with literal://string: prefix variant', () => {
|
|
389
|
-
// fromLinks should handle both `literal:string:` and `literal://string:` prefixes
|
|
390
|
-
const links = [
|
|
391
|
-
{ source: 'test://ModelShape', predicate: 'sh://targetClass', target: 'test://Model' },
|
|
392
|
-
{ source: 'test://ModelShape', predicate: 'sh://property', target: 'test://Model.status' },
|
|
393
|
-
{ source: 'test://Model.status', predicate: 'sh://path', target: 'test://status' },
|
|
394
|
-
{ source: 'test://Model.status', predicate: 'sh://in', target: 'literal://string:[{"value":"a"},{"value":"b"}]' },
|
|
395
|
-
];
|
|
396
|
-
const reconstructed = SHACLShape.fromLinks(links, 'test://ModelShape');
|
|
397
|
-
expect(reconstructed.properties[0].in).toEqual([
|
|
398
|
-
{ value: 'a' },
|
|
399
|
-
{ value: 'b' },
|
|
400
|
-
]);
|
|
401
|
-
});
|
|
402
|
-
it('preserves sh:in through toJSON() → fromJSON()', () => {
|
|
403
|
-
const original = new SHACLShape('test://Model');
|
|
404
|
-
original.addProperty({
|
|
405
|
-
name: 'priority',
|
|
406
|
-
path: 'test://priority',
|
|
407
|
-
in: [
|
|
408
|
-
{ value: 'high', label: 'High' },
|
|
409
|
-
{ value: 'medium', label: 'Medium' },
|
|
410
|
-
{ value: 'low', label: 'Low' },
|
|
411
|
-
],
|
|
412
|
-
});
|
|
413
|
-
const json = original.toJSON();
|
|
414
|
-
const reconstructed = SHACLShape.fromJSON(json);
|
|
415
|
-
expect(reconstructed.properties[0].in).toEqual(original.properties[0].in);
|
|
416
|
-
});
|
|
417
|
-
});
|
|
418
336
|
});
|
package/package.json
CHANGED
|
@@ -5,6 +5,12 @@
|
|
|
5
5
|
"module": "lib/index.js",
|
|
6
6
|
"types": "lib/src/index.d.ts",
|
|
7
7
|
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "patch-package && tsc && pnpm run bundle",
|
|
10
|
+
"bundle": "rollup -c rollup.config.js",
|
|
11
|
+
"test": "jest --forceExit",
|
|
12
|
+
"generate:api-types": "cd ../rust-executor && TS_RS_EXPORT_DIR=../core/src/generated/api/ cargo test --lib 2>/dev/null && echo \"API types generated to core/src/generated/api/\""
|
|
13
|
+
},
|
|
8
14
|
"repository": {
|
|
9
15
|
"type": "git",
|
|
10
16
|
"url": "git+https://github.com/coasys/ad4m.git"
|
|
@@ -48,11 +54,8 @@
|
|
|
48
54
|
"ws": "8.13.0"
|
|
49
55
|
},
|
|
50
56
|
"patchedDependencies": {},
|
|
51
|
-
"
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
"generate:api-types": "cd ../rust-executor && TS_RS_EXPORT_DIR=../core/src/generated/api/ cargo test --lib 2>/dev/null && echo \"API types generated to core/src/generated/api/\""
|
|
57
|
-
}
|
|
58
|
-
}
|
|
57
|
+
"pnpm": {
|
|
58
|
+
"patchedDependencies": {}
|
|
59
|
+
},
|
|
60
|
+
"version": "0.13.0-test-9"
|
|
61
|
+
}
|
|
@@ -1725,6 +1725,109 @@ describe("SHACL Memoisation", () => {
|
|
|
1725
1725
|
});
|
|
1726
1726
|
});
|
|
1727
1727
|
|
|
1728
|
+
describe("SHACL recursion — self-referential relations", () => {
|
|
1729
|
+
// Reported as a Maximum-call-stack-size-exceeded overflow when Flux's
|
|
1730
|
+
// Channel model has `@HasMany(() => Channel) childChannels`. The
|
|
1731
|
+
// memoised SHACL builder used to recurse forever because the cache
|
|
1732
|
+
// entry wasn't populated until buildSHACL returned. The fix seeds
|
|
1733
|
+
// the cache with the in-progress shape; these tests pin the contract.
|
|
1734
|
+
|
|
1735
|
+
it("should generate SHACL for a directly self-referential model without overflowing the stack", () => {
|
|
1736
|
+
@Model({ name: "RecursiveChannel" })
|
|
1737
|
+
class RecursiveChannel extends Ad4mModel {
|
|
1738
|
+
@Property({ through: "channel://name", resolveLanguage: "literal" })
|
|
1739
|
+
name: string = "";
|
|
1740
|
+
|
|
1741
|
+
@HasMany(() => RecursiveChannel, { through: "channel://child" })
|
|
1742
|
+
childChannels: string[] = [];
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
let shacl: any;
|
|
1746
|
+
expect(() => {
|
|
1747
|
+
shacl = (RecursiveChannel as any).generateSHACL();
|
|
1748
|
+
}).not.toThrow();
|
|
1749
|
+
|
|
1750
|
+
expect(shacl).toBeDefined();
|
|
1751
|
+
expect(shacl.name).toBe("RecursiveChannel");
|
|
1752
|
+
expect(shacl.shape).toBeDefined();
|
|
1753
|
+
expect(shacl.shape.nodeShapeUri).toBe("channel://RecursiveChannelShape");
|
|
1754
|
+
|
|
1755
|
+
// The child relation must reference the same class's shape.
|
|
1756
|
+
const childRel = (shacl.shape.properties as any[]).find((p) => p.name === "childChannels");
|
|
1757
|
+
expect(childRel).toBeDefined();
|
|
1758
|
+
expect(childRel.class).toBe("channel://RecursiveChannelShape");
|
|
1759
|
+
});
|
|
1760
|
+
|
|
1761
|
+
it("should memoise the self-referential shape (single shared instance)", () => {
|
|
1762
|
+
@Model({ name: "MemoSelfRef" })
|
|
1763
|
+
class MemoSelfRef extends Ad4mModel {
|
|
1764
|
+
@Property({ through: "memo://name" })
|
|
1765
|
+
name: string = "";
|
|
1766
|
+
|
|
1767
|
+
@HasMany(() => MemoSelfRef, { through: "memo://child" })
|
|
1768
|
+
children: string[] = [];
|
|
1769
|
+
}
|
|
1770
|
+
|
|
1771
|
+
const a = (MemoSelfRef as any).generateSHACL();
|
|
1772
|
+
const b = (MemoSelfRef as any).generateSHACL();
|
|
1773
|
+
expect(a).toBe(b);
|
|
1774
|
+
});
|
|
1775
|
+
|
|
1776
|
+
it("should handle mutually-recursive relations between two classes (A → B → A)", () => {
|
|
1777
|
+
@Model({ name: "MutualA" })
|
|
1778
|
+
class MutualA extends Ad4mModel {
|
|
1779
|
+
@Property({ through: "mut://aName" })
|
|
1780
|
+
aName: string = "";
|
|
1781
|
+
|
|
1782
|
+
@HasMany(() => MutualB, { through: "mut://aToB" })
|
|
1783
|
+
bs: string[] = [];
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
@Model({ name: "MutualB" })
|
|
1787
|
+
class MutualB extends Ad4mModel {
|
|
1788
|
+
@Property({ through: "mut://bName" })
|
|
1789
|
+
bName: string = "";
|
|
1790
|
+
|
|
1791
|
+
@HasMany(() => MutualA, { through: "mut://bToA" })
|
|
1792
|
+
as: string[] = [];
|
|
1793
|
+
}
|
|
1794
|
+
|
|
1795
|
+
let shaclA: any;
|
|
1796
|
+
let shaclB: any;
|
|
1797
|
+
expect(() => {
|
|
1798
|
+
shaclA = (MutualA as any).generateSHACL();
|
|
1799
|
+
shaclB = (MutualB as any).generateSHACL();
|
|
1800
|
+
}).not.toThrow();
|
|
1801
|
+
|
|
1802
|
+
expect(shaclA.name).toBe("MutualA");
|
|
1803
|
+
expect(shaclB.name).toBe("MutualB");
|
|
1804
|
+
});
|
|
1805
|
+
|
|
1806
|
+
it("should handle a three-cycle (A → B → C → A)", () => {
|
|
1807
|
+
@Model({ name: "CycleA" })
|
|
1808
|
+
class CycleA extends Ad4mModel {
|
|
1809
|
+
@HasMany(() => CycleB, { through: "cyc://aToB" })
|
|
1810
|
+
bs: string[] = [];
|
|
1811
|
+
}
|
|
1812
|
+
|
|
1813
|
+
@Model({ name: "CycleB" })
|
|
1814
|
+
class CycleB extends Ad4mModel {
|
|
1815
|
+
@HasMany(() => CycleC, { through: "cyc://bToC" })
|
|
1816
|
+
cs: string[] = [];
|
|
1817
|
+
}
|
|
1818
|
+
|
|
1819
|
+
@Model({ name: "CycleC" })
|
|
1820
|
+
class CycleC extends Ad4mModel {
|
|
1821
|
+
@HasMany(() => CycleA, { through: "cyc://cToA" })
|
|
1822
|
+
as: string[] = [];
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
expect(() => (CycleA as any).generateSHACL()).not.toThrow();
|
|
1826
|
+
expect(() => (CycleB as any).generateSHACL()).not.toThrow();
|
|
1827
|
+
expect(() => (CycleC as any).generateSHACL()).not.toThrow();
|
|
1828
|
+
});
|
|
1829
|
+
});
|
|
1830
|
+
|
|
1728
1831
|
describe("Lazy Conformance Filters", () => {
|
|
1729
1832
|
it("should defer conformance filter resolution until property access", () => {
|
|
1730
1833
|
@Model({ name: "LazyTarget2" })
|
package/src/model/decorators.ts
CHANGED
|
@@ -115,11 +115,44 @@ export function getRelationsMetadata(ctor: Function): Record<string, RelationMet
|
|
|
115
115
|
|
|
116
116
|
/**
|
|
117
117
|
* Get or compute memoised SHACL for a class. Used by @Model decorator.
|
|
118
|
+
*
|
|
119
|
+
* The `compute` callback receives a `seed(partial)` function: calling
|
|
120
|
+
* it stores `partial` in the cache *before* `compute` returns, so any
|
|
121
|
+
* re-entrant `getMemoizedSHACL(target, …)` call from inside `compute`
|
|
122
|
+
* observes the in-progress shape instead of re-entering `compute`
|
|
123
|
+
* infinitely.
|
|
124
|
+
*
|
|
125
|
+
* This matters for self-referential models — e.g.
|
|
126
|
+
*
|
|
127
|
+
* @Model({ name: "Channel" })
|
|
128
|
+
* class Channel extends Ad4mModel {
|
|
129
|
+
* @HasMany({ predicate: "…/child", target: () => Channel })
|
|
130
|
+
* childChannels!: Channel[];
|
|
131
|
+
* }
|
|
132
|
+
*
|
|
133
|
+
* Without the seed, building the SHACL shape for `Channel` invokes
|
|
134
|
+
* `Channel.generateSHACL()` while walking the `childChannels`
|
|
135
|
+
* relation's target (shacl-gen.ts ~line 327), which re-enters this
|
|
136
|
+
* function with an empty cache, blowing the stack.
|
|
118
137
|
*/
|
|
119
|
-
export function getMemoizedSHACL(
|
|
138
|
+
export function getMemoizedSHACL(
|
|
139
|
+
target: Function,
|
|
140
|
+
compute: (seed: (partial: any) => void) => any,
|
|
141
|
+
): any {
|
|
120
142
|
const cached = shaclCache.get(target);
|
|
121
143
|
if (cached) return cached;
|
|
122
|
-
const
|
|
144
|
+
const seed = (partial: any) => {
|
|
145
|
+
shaclCache.set(target, partial);
|
|
146
|
+
};
|
|
147
|
+
let result: any;
|
|
148
|
+
try {
|
|
149
|
+
result = compute(seed);
|
|
150
|
+
} catch (e) {
|
|
151
|
+
// If compute throws after seeding, evict the half-built entry
|
|
152
|
+
// so a later retry can rebuild from scratch.
|
|
153
|
+
shaclCache.delete(target);
|
|
154
|
+
throw e;
|
|
155
|
+
}
|
|
123
156
|
shaclCache.set(target, result);
|
|
124
157
|
return result;
|
|
125
158
|
}
|
|
@@ -544,12 +577,13 @@ export function Model(opts: ModelConfig) {
|
|
|
544
577
|
}
|
|
545
578
|
|
|
546
579
|
target.generateSHACL = function() {
|
|
547
|
-
return getMemoizedSHACL(target, () => buildSHACL(
|
|
580
|
+
return getMemoizedSHACL(target, (seed) => buildSHACL(
|
|
548
581
|
opts.name,
|
|
549
582
|
target,
|
|
550
583
|
getPropertiesMetadata(target),
|
|
551
584
|
getRelationsMetadata(target),
|
|
552
585
|
buildConformanceFilter,
|
|
586
|
+
seed,
|
|
553
587
|
));
|
|
554
588
|
}
|
|
555
589
|
|
|
@@ -374,3 +374,151 @@ describe('buildSPARQLQuery — RDF 1.2 reifier patterns', () => {
|
|
|
374
374
|
});
|
|
375
375
|
|
|
376
376
|
});
|
|
377
|
+
|
|
378
|
+
// ──────────────────────────────────────────────────────────
|
|
379
|
+
// OR / AND / NOT logical combinators
|
|
380
|
+
// ──────────────────────────────────────────────────────────
|
|
381
|
+
|
|
382
|
+
describe('hasJsOnlyWhereFilters — OR/AND/NOT combinators', () => {
|
|
383
|
+
// buildSPARQLWhereFilters never emits a SPARQL translation for OR/AND/NOT
|
|
384
|
+
// regardless of what their branches contain (see the comment there), so
|
|
385
|
+
// hasJsOnlyWhereFilters must treat their mere presence as JS-only —
|
|
386
|
+
// otherwise SPARQL-level LIMIT/OFFSET would run before the combinator
|
|
387
|
+
// filter itself is ever applied.
|
|
388
|
+
|
|
389
|
+
it('returns true for OR even when every branch is SPARQL-pushable', () => {
|
|
390
|
+
const where = {
|
|
391
|
+
OR: [
|
|
392
|
+
{ category: 'some://uri' },
|
|
393
|
+
{ category: 'other://uri' },
|
|
394
|
+
],
|
|
395
|
+
};
|
|
396
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, where)).toBe(true);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it('returns true for OR when a branch contains a JS-only filter', () => {
|
|
400
|
+
const where = {
|
|
401
|
+
OR: [
|
|
402
|
+
{ category: 'some://uri' },
|
|
403
|
+
{ author: 'did:key:abc' },
|
|
404
|
+
],
|
|
405
|
+
};
|
|
406
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, where)).toBe(true);
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it('returns true for OR when a branch contains a contains operator', () => {
|
|
410
|
+
const where = {
|
|
411
|
+
OR: [
|
|
412
|
+
{ name: { contains: 'foo' } },
|
|
413
|
+
{ description: { contains: 'foo' } },
|
|
414
|
+
],
|
|
415
|
+
};
|
|
416
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, where)).toBe(true);
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it('returns true for AND even when every branch is SPARQL-pushable', () => {
|
|
420
|
+
const where = {
|
|
421
|
+
AND: [
|
|
422
|
+
{ category: 'some://uri' },
|
|
423
|
+
{ category: 'other://uri' },
|
|
424
|
+
],
|
|
425
|
+
};
|
|
426
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, where)).toBe(true);
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
it('returns true for AND when a branch contains a JS-only filter', () => {
|
|
430
|
+
const where = {
|
|
431
|
+
AND: [
|
|
432
|
+
{ category: 'some://uri' },
|
|
433
|
+
{ name: { gt: 5 } },
|
|
434
|
+
],
|
|
435
|
+
};
|
|
436
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, where)).toBe(true);
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it('returns true for NOT even when the negated clause is SPARQL-pushable', () => {
|
|
440
|
+
const where = { NOT: { category: 'some://uri' } };
|
|
441
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, where)).toBe(true);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
it('returns true for NOT when the negated clause contains a JS-only filter', () => {
|
|
445
|
+
const where = { NOT: { author: 'did:key:abc' } };
|
|
446
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, where)).toBe(true);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
it('returns true for nested OR inside OR', () => {
|
|
450
|
+
const where = {
|
|
451
|
+
OR: [
|
|
452
|
+
{ OR: [{ author: 'did:key:abc' }] },
|
|
453
|
+
{ category: 'some://uri' },
|
|
454
|
+
],
|
|
455
|
+
};
|
|
456
|
+
expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, where)).toBe(true);
|
|
457
|
+
});
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
describe('buildSPARQLQuery — OR/AND/NOT not emitted as SPARQL (handled Rust-side)', () => {
|
|
461
|
+
const modelClass: any = {};
|
|
462
|
+
|
|
463
|
+
it('does not emit OR key as a SPARQL triple pattern', () => {
|
|
464
|
+
const query = {
|
|
465
|
+
where: {
|
|
466
|
+
OR: [
|
|
467
|
+
{ name: 'Alice' },
|
|
468
|
+
{ name: 'Bob' },
|
|
469
|
+
],
|
|
470
|
+
},
|
|
471
|
+
};
|
|
472
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
473
|
+
// OR should not appear as a predicate in the SPARQL
|
|
474
|
+
expect(sparql).not.toContain('"OR"');
|
|
475
|
+
expect(sparql).not.toContain('<OR>');
|
|
476
|
+
// Valid SPARQL structure still present
|
|
477
|
+
expect(sparql).toContain('SELECT ?source ?predicate ?target');
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
it('does not emit AND or NOT as SPARQL patterns', () => {
|
|
481
|
+
const query = {
|
|
482
|
+
where: {
|
|
483
|
+
AND: [{ category: 'some://uri' }],
|
|
484
|
+
NOT: { category: 'other://uri' },
|
|
485
|
+
},
|
|
486
|
+
};
|
|
487
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
488
|
+
expect(sparql).not.toContain('"AND"');
|
|
489
|
+
expect(sparql).not.toContain('"NOT"');
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
it('does NOT include LIMIT when OR contains JS-only filters', () => {
|
|
493
|
+
// OR with contains → hasJsOnlyWhereFilters true → no SPARQL pagination
|
|
494
|
+
const query = {
|
|
495
|
+
limit: 10,
|
|
496
|
+
where: {
|
|
497
|
+
OR: [
|
|
498
|
+
{ name: { contains: 'foo' } },
|
|
499
|
+
{ description: { contains: 'foo' } },
|
|
500
|
+
],
|
|
501
|
+
},
|
|
502
|
+
};
|
|
503
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
504
|
+
expect(sparql).not.toContain('LIMIT');
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
it('does NOT include LIMIT when OR branches are all SPARQL-pushable', () => {
|
|
508
|
+
// Even though both OR branches are simple non-literal equality, the OR
|
|
509
|
+
// combinator itself is never translated into a SPARQL FILTER (see
|
|
510
|
+
// buildSPARQLWhereFilters), so pushing LIMIT down would cap the
|
|
511
|
+
// candidate set before the combinator is applied.
|
|
512
|
+
const query = {
|
|
513
|
+
limit: 5,
|
|
514
|
+
where: {
|
|
515
|
+
OR: [
|
|
516
|
+
{ category: 'some://uri' },
|
|
517
|
+
{ category: 'other://uri' },
|
|
518
|
+
],
|
|
519
|
+
},
|
|
520
|
+
};
|
|
521
|
+
const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
|
|
522
|
+
expect(sparql).not.toContain('LIMIT');
|
|
523
|
+
});
|
|
524
|
+
});
|