@coasys/ad4m 0.13.0-test-interpretation-1 → 0.13.0-test-model-layer
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 +2409 -625
- package/lib/index.js +2406 -626
- package/lib/src/Literal.js +8 -0
- package/lib/src/ai/AIClient.js +6 -3
- package/lib/src/apiClient.d.ts +40 -4
- package/lib/src/apiClient.js +149 -9
- package/lib/src/generated/api/RunInterpretationRequest.d.ts +16 -0
- package/lib/src/index.d.ts +3 -1
- package/lib/src/index.js +2 -0
- package/lib/src/model/Ad4mModel.d.ts +34 -16
- package/lib/src/model/Ad4mModel.js +302 -63
- package/lib/src/model/Ad4mModel.test.js +480 -735
- package/lib/src/model/ModelQueryBuilder.js +2 -2
- package/lib/src/model/decorators.d.ts +199 -12
- package/lib/src/model/decorators.js +88 -32
- package/lib/src/model/json-schema.js +7 -21
- package/lib/src/model/model-manifest.d.ts +41 -0
- package/lib/src/model/model-manifest.js +11 -0
- package/lib/src/model/query-common.d.ts +2 -2
- package/lib/src/model/query-sparql.d.ts +62 -0
- package/lib/src/model/query-sparql.js +78 -15
- package/lib/src/model/query-sparql.test.js +92 -10
- package/lib/src/model/query-utils.d.ts +7 -2
- package/lib/src/model/query-utils.js +21 -11
- package/lib/src/model/shacl-gen.js +77 -15
- package/lib/src/model/types.d.ts +32 -8
- package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +33 -33
- package/lib/src/perspectives/AutoProcessor.d.ts +198 -2
- package/lib/src/perspectives/FlowInstance.d.ts +187 -0
- package/lib/src/perspectives/FlowInstance.js +336 -0
- package/lib/src/perspectives/FlowModels.d.ts +94 -0
- package/lib/src/perspectives/FlowModels.js +182 -0
- package/lib/src/perspectives/InterpretationModels.d.ts +41 -3
- package/lib/src/perspectives/InterpretationModels.js +60 -26
- package/lib/src/perspectives/PerspectiveClient.d.ts +88 -7
- package/lib/src/perspectives/PerspectiveClient.js +138 -11
- package/lib/src/perspectives/PerspectiveProxy.d.ts +150 -57
- package/lib/src/perspectives/PerspectiveProxy.js +411 -287
- package/lib/src/perspectives/SparqlBindings.d.ts +24 -3
- package/lib/src/perspectives/SparqlBindings.js +57 -6
- package/lib/src/shacl/SHACLFlow.d.ts +273 -47
- package/lib/src/shacl/SHACLFlow.js +397 -88
- package/lib/src/shacl/SHACLShape.d.ts +19 -3
- package/lib/src/shacl/SHACLShape.js +30 -1
- package/lib/src/shacl/SHACLShape.test.js +82 -0
- package/lib/src/shacl/index.d.ts +1 -1
- package/lib/src/shacl/index.js +1 -1
- package/package.json +3 -3
- package/src/Literal.ts +7 -0
- package/src/ai/AIClient.ts +6 -3
- package/src/apiClient.test.ts +227 -0
- package/src/apiClient.ts +185 -9
- package/src/generated/api/RunInterpretationRequest.ts +19 -3
- package/src/index.ts +3 -1
- package/src/model/Ad4mModel.test.ts +729 -22
- package/src/model/Ad4mModel.ts +305 -66
- package/src/model/ModelQueryBuilder.ts +2 -2
- package/src/model/decorators.ts +278 -44
- package/src/model/json-schema.ts +10 -25
- package/src/model/query-common.ts +2 -2
- package/src/model/query-sparql.test.ts +114 -13
- package/src/model/query-sparql.ts +83 -14
- package/src/model/query-utils.ts +22 -12
- package/src/model/relation-filtering.test.ts +70 -20
- package/src/model/shacl-gen.ts +78 -13
- package/src/model/shacl-roundtrip.test.ts +134 -2
- package/src/model/types.ts +30 -8
- package/src/perspectives/AutoProcessor.ts +204 -2
- package/src/perspectives/FlowInstance.ts +369 -0
- package/src/perspectives/FlowModels.ts +157 -0
- package/src/perspectives/InterpretationModels.ts +91 -21
- package/src/perspectives/PerspectiveClient.test.ts +67 -6
- package/src/perspectives/PerspectiveClient.ts +190 -14
- package/src/perspectives/PerspectiveProxy.test.ts +406 -0
- package/src/perspectives/PerspectiveProxy.ts +494 -308
- package/src/perspectives/SparqlBindings.test.ts +134 -0
- package/src/perspectives/SparqlBindings.ts +57 -6
- package/src/perspectives/shacl-rpc.test.ts +258 -0
- package/src/shacl/SHACLFlow.test.ts +762 -35
- package/src/shacl/SHACLFlow.ts +680 -112
- package/src/shacl/SHACLShape.test.ts +30 -4
- package/src/shacl/SHACLShape.ts +59 -6
- package/src/shacl/index.ts +1 -1
- package/lib/src/generated/rest/AIModelLoadingStatus.d.ts +0 -7
- package/lib/src/generated/rest/AIModelLoadingStatus.js +0 -2
- package/lib/src/generated/rest/AddAgentInfosRequest.d.ts +0 -3
- package/lib/src/generated/rest/AddAgentInfosRequest.js +0 -2
- package/lib/src/generated/rest/AddLinkExpressionRequest.d.ts +0 -6
- package/lib/src/generated/rest/AddLinkExpressionRequest.js +0 -1
- package/lib/src/generated/rest/AddLinkRequest.d.ts +0 -6
- package/lib/src/generated/rest/AddLinkRequest.js +0 -1
- package/lib/src/generated/rest/AddLinksBulkRequest.d.ts +0 -6
- package/lib/src/generated/rest/AddLinksBulkRequest.js +0 -1
- package/lib/src/generated/rest/AddSdnaRequest.d.ts +0 -6
- package/lib/src/generated/rest/AddSdnaRequest.js +0 -2
- package/lib/src/generated/rest/ApplyTemplateRequest.d.ts +0 -4
- package/lib/src/generated/rest/ApplyTemplateRequest.js +0 -2
- package/lib/src/generated/rest/AuthInfoInput.d.ts +0 -10
- package/lib/src/generated/rest/AuthInfoInput.js +0 -1
- package/lib/src/generated/rest/BroadcastRequest.d.ts +0 -6
- package/lib/src/generated/rest/BroadcastRequest.js +0 -1
- package/lib/src/generated/rest/CapabilityInput.d.ts +0 -5
- package/lib/src/generated/rest/CapabilityInput.js +0 -1
- package/lib/src/generated/rest/CommitBatchRequest.d.ts +0 -3
- package/lib/src/generated/rest/CommitBatchRequest.js +0 -2
- package/lib/src/generated/rest/ComputeLogEntry.d.ts +0 -9
- package/lib/src/generated/rest/ComputeLogEntry.js +0 -2
- package/lib/src/generated/rest/CreateExpressionRequest.d.ts +0 -4
- package/lib/src/generated/rest/CreateExpressionRequest.js +0 -2
- package/lib/src/generated/rest/CreatePerspectiveRequest.d.ts +0 -3
- package/lib/src/generated/rest/CreatePerspectiveRequest.js +0 -2
- package/lib/src/generated/rest/CreateSubjectRequest.d.ts +0 -6
- package/lib/src/generated/rest/CreateSubjectRequest.js +0 -2
- package/lib/src/generated/rest/CreateUserRequest.d.ts +0 -4
- package/lib/src/generated/rest/CreateUserRequest.js +0 -2
- package/lib/src/generated/rest/CreateUserResponse.d.ts +0 -5
- package/lib/src/generated/rest/CreateUserResponse.js +0 -2
- package/lib/src/generated/rest/DecoratedExpressionProof.d.ts +0 -6
- package/lib/src/generated/rest/DecoratedExpressionProof.js +0 -2
- package/lib/src/generated/rest/DecoratedLinkExpression.d.ts +0 -10
- package/lib/src/generated/rest/DecoratedLinkExpression.js +0 -1
- package/lib/src/generated/rest/DisposeQueryRequest.d.ts +0 -3
- package/lib/src/generated/rest/DisposeQueryRequest.js +0 -2
- package/lib/src/generated/rest/EmailTestRequest.d.ts +0 -6
- package/lib/src/generated/rest/EmailTestRequest.js +0 -2
- package/lib/src/generated/rest/EmbedRequest.d.ts +0 -4
- package/lib/src/generated/rest/EmbedRequest.js +0 -2
- package/lib/src/generated/rest/EntanglementProofInput.d.ts +0 -6
- package/lib/src/generated/rest/EntanglementProofInput.js +0 -2
- package/lib/src/generated/rest/EntanglementProofPreflightRequest.d.ts +0 -4
- package/lib/src/generated/rest/EntanglementProofPreflightRequest.js +0 -2
- package/lib/src/generated/rest/ExecuteCommandsRequest.d.ts +0 -4
- package/lib/src/generated/rest/ExecuteCommandsRequest.js +0 -2
- package/lib/src/generated/rest/ExportRequest.d.ts +0 -5
- package/lib/src/generated/rest/ExportRequest.js +0 -2
- package/lib/src/generated/rest/ExpressionManyRequest.d.ts +0 -3
- package/lib/src/generated/rest/ExpressionManyRequest.js +0 -2
- package/lib/src/generated/rest/ExpressionProof.d.ts +0 -4
- package/lib/src/generated/rest/ExpressionProof.js +0 -2
- package/lib/src/generated/rest/ExpressionProofInput.d.ts +0 -6
- package/lib/src/generated/rest/ExpressionProofInput.js +0 -2
- package/lib/src/generated/rest/FriendSendMessageRequest.d.ts +0 -3
- package/lib/src/generated/rest/FriendSendMessageRequest.js +0 -2
- package/lib/src/generated/rest/FriendsListRequest.d.ts +0 -3
- package/lib/src/generated/rest/FriendsListRequest.js +0 -2
- package/lib/src/generated/rest/GenerateAgentRequest.d.ts +0 -3
- package/lib/src/generated/rest/GenerateAgentRequest.js +0 -2
- package/lib/src/generated/rest/GenerateJwtRequest.d.ts +0 -4
- package/lib/src/generated/rest/GenerateJwtRequest.js +0 -2
- package/lib/src/generated/rest/GetSubjectDataRequest.d.ts +0 -4
- package/lib/src/generated/rest/GetSubjectDataRequest.js +0 -2
- package/lib/src/generated/rest/HostingInfoResponse.d.ts +0 -5
- package/lib/src/generated/rest/HostingInfoResponse.js +0 -2
- package/lib/src/generated/rest/HostingWalletResponse.d.ts +0 -4
- package/lib/src/generated/rest/HostingWalletResponse.js +0 -2
- package/lib/src/generated/rest/ImportAgentRequest.d.ts +0 -6
- package/lib/src/generated/rest/ImportAgentRequest.js +0 -2
- package/lib/src/generated/rest/ImportRequest.d.ts +0 -4
- package/lib/src/generated/rest/ImportRequest.js +0 -2
- package/lib/src/generated/rest/JoinNeighbourhoodRequest.d.ts +0 -3
- package/lib/src/generated/rest/JoinNeighbourhoodRequest.js +0 -2
- package/lib/src/generated/rest/KeepAliveQueryRequest.d.ts +0 -3
- package/lib/src/generated/rest/KeepAliveQueryRequest.js +0 -2
- package/lib/src/generated/rest/LanguageMetaInput.d.ts +0 -6
- package/lib/src/generated/rest/LanguageMetaInput.js +0 -2
- package/lib/src/generated/rest/Link.d.ts +0 -5
- package/lib/src/generated/rest/Link.js +0 -2
- package/lib/src/generated/rest/LinkExpression.d.ts +0 -10
- package/lib/src/generated/rest/LinkExpression.js +0 -1
- package/lib/src/generated/rest/LinkExpressionInput.d.ts +0 -10
- package/lib/src/generated/rest/LinkExpressionInput.js +0 -1
- package/lib/src/generated/rest/LinkInput.d.ts +0 -5
- package/lib/src/generated/rest/LinkInput.js +0 -2
- package/lib/src/generated/rest/LinkLanguageTemplatesRequest.d.ts +0 -3
- package/lib/src/generated/rest/LinkLanguageTemplatesRequest.js +0 -2
- package/lib/src/generated/rest/LinkMutationRequest.d.ts +0 -12
- package/lib/src/generated/rest/LinkMutationRequest.js +0 -1
- package/lib/src/generated/rest/LinkMutationResponse.d.ts +0 -9
- package/lib/src/generated/rest/LinkMutationResponse.js +0 -1
- package/lib/src/generated/rest/LinkMutations.d.ts +0 -6
- package/lib/src/generated/rest/LinkMutations.js +0 -1
- package/lib/src/generated/rest/LinkMutationsRequest.d.ts +0 -5
- package/lib/src/generated/rest/LinkMutationsRequest.js +0 -1
- package/lib/src/generated/rest/LinkStatus.d.ts +0 -1
- package/lib/src/generated/rest/LinkStatus.js +0 -2
- package/lib/src/generated/rest/LinkUpdateInput.d.ts +0 -5
- package/lib/src/generated/rest/LinkUpdateInput.js +0 -1
- package/lib/src/generated/rest/LockAgentRequest.d.ts +0 -3
- package/lib/src/generated/rest/LockAgentRequest.js +0 -2
- package/lib/src/generated/rest/LoginUserRequest.d.ts +0 -5
- package/lib/src/generated/rest/LoginUserRequest.js +0 -2
- package/lib/src/generated/rest/ModelType.d.ts +0 -1
- package/lib/src/generated/rest/ModelType.js +0 -2
- package/lib/src/generated/rest/NotificationGrantRequest.d.ts +0 -3
- package/lib/src/generated/rest/NotificationGrantRequest.js +0 -2
- package/lib/src/generated/rest/NotificationInput.d.ts +0 -10
- package/lib/src/generated/rest/NotificationInput.js +0 -2
- package/lib/src/generated/rest/OpenLinkRequest.d.ts +0 -3
- package/lib/src/generated/rest/OpenLinkRequest.js +0 -2
- package/lib/src/generated/rest/PermitCapabilityRequest.d.ts +0 -3
- package/lib/src/generated/rest/PermitCapabilityRequest.js +0 -2
- package/lib/src/generated/rest/PerspectiveInput.d.ts +0 -4
- package/lib/src/generated/rest/PerspectiveInput.js +0 -1
- package/lib/src/generated/rest/PromptRequest.d.ts +0 -4
- package/lib/src/generated/rest/PromptRequest.js +0 -2
- package/lib/src/generated/rest/PublicPerspectiveInput.d.ts +0 -4
- package/lib/src/generated/rest/PublicPerspectiveInput.js +0 -1
- package/lib/src/generated/rest/PublishLanguageRequest.d.ts +0 -5
- package/lib/src/generated/rest/PublishLanguageRequest.js +0 -1
- package/lib/src/generated/rest/PublishNeighbourhoodRequest.d.ts +0 -6
- package/lib/src/generated/rest/PublishNeighbourhoodRequest.js +0 -1
- package/lib/src/generated/rest/QueryRequest.d.ts +0 -7
- package/lib/src/generated/rest/QueryRequest.js +0 -2
- package/lib/src/generated/rest/RemoveLinkRequest.d.ts +0 -5
- package/lib/src/generated/rest/RemoveLinkRequest.js +0 -1
- package/lib/src/generated/rest/RemoveLinksBulkRequest.d.ts +0 -5
- package/lib/src/generated/rest/RemoveLinksBulkRequest.js +0 -1
- package/lib/src/generated/rest/RequestCapabilityRequest.d.ts +0 -4
- package/lib/src/generated/rest/RequestCapabilityRequest.js +0 -1
- package/lib/src/generated/rest/RequestPaymentRequest.d.ts +0 -3
- package/lib/src/generated/rest/RequestPaymentRequest.js +0 -2
- package/lib/src/generated/rest/RequestVerificationRequest.d.ts +0 -4
- package/lib/src/generated/rest/RequestVerificationRequest.js +0 -2
- package/lib/src/generated/rest/ResourceInput.d.ts +0 -4
- package/lib/src/generated/rest/ResourceInput.js +0 -2
- package/lib/src/generated/rest/SetDefaultModelRequest.d.ts +0 -4
- package/lib/src/generated/rest/SetDefaultModelRequest.js +0 -1
- package/lib/src/generated/rest/SetFreeHostingEnabledRequest.d.ts +0 -3
- package/lib/src/generated/rest/SetFreeHostingEnabledRequest.js +0 -2
- package/lib/src/generated/rest/SetHotWalletAddressRequest.d.ts +0 -3
- package/lib/src/generated/rest/SetHotWalletAddressRequest.js +0 -2
- package/lib/src/generated/rest/SetMultiUserRequest.d.ts +0 -3
- package/lib/src/generated/rest/SetMultiUserRequest.js +0 -2
- package/lib/src/generated/rest/SetOnlineStatusRequest.d.ts +0 -5
- package/lib/src/generated/rest/SetOnlineStatusRequest.js +0 -1
- package/lib/src/generated/rest/SetStatusRequest.d.ts +0 -3
- package/lib/src/generated/rest/SetStatusRequest.js +0 -2
- package/lib/src/generated/rest/SetUserFreeAccessRequest.d.ts +0 -4
- package/lib/src/generated/rest/SetUserFreeAccessRequest.js +0 -2
- package/lib/src/generated/rest/SignMessageRequest.d.ts +0 -3
- package/lib/src/generated/rest/SignMessageRequest.js +0 -2
- package/lib/src/generated/rest/SignalRequest.d.ts +0 -6
- package/lib/src/generated/rest/SignalRequest.js +0 -1
- package/lib/src/generated/rest/SubscribeQueryRequest.d.ts +0 -3
- package/lib/src/generated/rest/SubscribeQueryRequest.js +0 -2
- package/lib/src/generated/rest/SubscribeQueryResponse.d.ts +0 -4
- package/lib/src/generated/rest/SubscribeQueryResponse.js +0 -2
- package/lib/src/generated/rest/TrustedAgentsRequest.d.ts +0 -3
- package/lib/src/generated/rest/TrustedAgentsRequest.js +0 -2
- package/lib/src/generated/rest/UnlockAgentRequest.d.ts +0 -4
- package/lib/src/generated/rest/UnlockAgentRequest.js +0 -2
- package/lib/src/generated/rest/UpdateLinkRequest.d.ts +0 -7
- package/lib/src/generated/rest/UpdateLinkRequest.js +0 -1
- package/lib/src/generated/rest/UpdatePerspectiveRequest.d.ts +0 -3
- package/lib/src/generated/rest/UpdatePerspectiveRequest.js +0 -2
- package/lib/src/generated/rest/UpdateProfileRequest.d.ts +0 -5
- package/lib/src/generated/rest/UpdateProfileRequest.js +0 -1
- package/lib/src/generated/rest/UserStatistics.d.ts +0 -9
- package/lib/src/generated/rest/UserStatistics.js +0 -2
- package/lib/src/generated/rest/VerificationRequestResult.d.ts +0 -6
- package/lib/src/generated/rest/VerificationRequestResult.js +0 -2
- package/lib/src/generated/rest/VerifyEmailRequest.d.ts +0 -6
- package/lib/src/generated/rest/VerifyEmailRequest.js +0 -2
- package/lib/src/generated/rest/VerifySignatureRequest.d.ts +0 -5
- package/lib/src/generated/rest/VerifySignatureRequest.js +0 -2
- package/lib/src/generated/rest/WriteSettingsRequest.d.ts +0 -3
- package/lib/src/generated/rest/WriteSettingsRequest.js +0 -2
- package/lib/src/generated/rest/index.d.ts +0 -73
- package/lib/src/generated/rest/index.js +0 -8
- package/lib/src/generated/rest/routes.d.ts +0 -587
- package/lib/src/generated/rest/routes.js +0 -3
- 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/lib/src/perspectives/PerspectiveProxy.test.d.ts +0 -1
- package/lib/src/perspectives/PerspectiveProxy.test.js +0 -34
- package/lib/src/restClient.d.ts +0 -52
- package/lib/src/restClient.js +0 -227
|
@@ -84,6 +84,101 @@ describe('PerspectiveProxy.dispose', () => {
|
|
|
84
84
|
});
|
|
85
85
|
});
|
|
86
86
|
|
|
87
|
+
describe('ApiClient.onReconnect', () => {
|
|
88
|
+
// Injected WebSocket implementation (ApiClient constructor arg 3) so these
|
|
89
|
+
// tests drive the REAL onopen/onclose lifecycle instead of poking at
|
|
90
|
+
// private fields. Each call returns a fresh class with its own instance
|
|
91
|
+
// list.
|
|
92
|
+
function makeFakeWebSocketImpl() {
|
|
93
|
+
class FakeWebSocket {
|
|
94
|
+
static instances: FakeWebSocket[] = [];
|
|
95
|
+
url: string;
|
|
96
|
+
readyState = 0;
|
|
97
|
+
onopen: (() => void) | null = null;
|
|
98
|
+
onmessage: ((event: any) => void) | null = null;
|
|
99
|
+
onerror: ((e: any) => void) | null = null;
|
|
100
|
+
onclose: (() => void) | null = null;
|
|
101
|
+
constructor(url: string) {
|
|
102
|
+
this.url = url;
|
|
103
|
+
FakeWebSocket.instances.push(this);
|
|
104
|
+
}
|
|
105
|
+
send(_data: any) {}
|
|
106
|
+
close() { this.readyState = 3; }
|
|
107
|
+
/** Test helper: simulate the server accepting the connection. */
|
|
108
|
+
open() { this.readyState = 1; this.onopen?.(); }
|
|
109
|
+
/** Test helper: simulate the connection dropping. */
|
|
110
|
+
drop() { this.readyState = 3; this.onclose?.(); }
|
|
111
|
+
}
|
|
112
|
+
return FakeWebSocket;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
it('fires reconnect callbacks only on reconnect, not first connect', () => {
|
|
116
|
+
const { ApiClient } = require('../apiClient');
|
|
117
|
+
const FakeWs = makeFakeWebSocketImpl();
|
|
118
|
+
const client = new ApiClient('http://localhost:12000', undefined, FakeWs as any);
|
|
119
|
+
const reconnectCb = jest.fn();
|
|
120
|
+
client.onReconnect(reconnectCb);
|
|
121
|
+
|
|
122
|
+
// First connection: onopen must NOT fire the reconnect callback
|
|
123
|
+
client.connect();
|
|
124
|
+
FakeWs.instances[0].open();
|
|
125
|
+
expect(reconnectCb).not.toHaveBeenCalled();
|
|
126
|
+
|
|
127
|
+
// Drop and reconnect: the second onopen must fire it exactly once
|
|
128
|
+
FakeWs.instances[0].drop();
|
|
129
|
+
client.connect();
|
|
130
|
+
FakeWs.instances[1].open();
|
|
131
|
+
expect(reconnectCb).toHaveBeenCalledTimes(1);
|
|
132
|
+
|
|
133
|
+
client.closeAll();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('unsubscribed callbacks do not fire on reconnect', () => {
|
|
137
|
+
const { ApiClient } = require('../apiClient');
|
|
138
|
+
const FakeWs = makeFakeWebSocketImpl();
|
|
139
|
+
const client = new ApiClient('http://localhost:12000', undefined, FakeWs as any);
|
|
140
|
+
const reconnectCb = jest.fn();
|
|
141
|
+
const unsub = client.onReconnect(reconnectCb);
|
|
142
|
+
|
|
143
|
+
client.connect();
|
|
144
|
+
FakeWs.instances[0].open();
|
|
145
|
+
unsub();
|
|
146
|
+
|
|
147
|
+
FakeWs.instances[0].drop();
|
|
148
|
+
client.connect();
|
|
149
|
+
FakeWs.instances[1].open();
|
|
150
|
+
expect(reconnectCb).not.toHaveBeenCalled();
|
|
151
|
+
|
|
152
|
+
client.closeAll();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('closeAll resets the first-connect gate for client reuse', () => {
|
|
156
|
+
const { ApiClient } = require('../apiClient');
|
|
157
|
+
const FakeWs = makeFakeWebSocketImpl();
|
|
158
|
+
const client = new ApiClient('http://localhost:12000', undefined, FakeWs as any);
|
|
159
|
+
|
|
160
|
+
client.connect();
|
|
161
|
+
FakeWs.instances[0].open();
|
|
162
|
+
client.closeAll();
|
|
163
|
+
|
|
164
|
+
// Reuse after closeAll: the first open of the NEW connection is an
|
|
165
|
+
// initial connect again, not a reconnect.
|
|
166
|
+
const reconnectCb = jest.fn();
|
|
167
|
+
client.onReconnect(reconnectCb);
|
|
168
|
+
client.connect();
|
|
169
|
+
FakeWs.instances[1].open();
|
|
170
|
+
expect(reconnectCb).not.toHaveBeenCalled();
|
|
171
|
+
|
|
172
|
+
// …but a genuine reconnect within the new lifecycle still fires.
|
|
173
|
+
FakeWs.instances[1].drop();
|
|
174
|
+
client.connect();
|
|
175
|
+
FakeWs.instances[2].open();
|
|
176
|
+
expect(reconnectCb).toHaveBeenCalledTimes(1);
|
|
177
|
+
|
|
178
|
+
client.closeAll();
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
87
182
|
describe('QuerySubscriptionProxy', () => {
|
|
88
183
|
it('treats the initial subscribeQuery() result as a completed initialization', async () => {
|
|
89
184
|
const initialResult = [
|
|
@@ -122,4 +217,315 @@ describe('QuerySubscriptionProxy', () => {
|
|
|
122
217
|
subscription.dispose();
|
|
123
218
|
expect(unsubscribe).toHaveBeenCalled();
|
|
124
219
|
});
|
|
220
|
+
|
|
221
|
+
it('re-subscribes immediately when onReconnect fires', async () => {
|
|
222
|
+
let reconnectCallback: (() => void) | undefined;
|
|
223
|
+
const initialUnsubscribe = jest.fn();
|
|
224
|
+
const reconnectUnsubscribe = jest.fn();
|
|
225
|
+
let subscribeToUpdatesCall = 0;
|
|
226
|
+
const unsubReconnect = jest.fn();
|
|
227
|
+
const mockClient = {
|
|
228
|
+
subscribeQuery: jest.fn().mockResolvedValue({
|
|
229
|
+
subscriptionId: 'sub-1',
|
|
230
|
+
result: [{ s: 'a', p: 'b', o: 'c' }],
|
|
231
|
+
}),
|
|
232
|
+
subscribeToQueryUpdates: jest.fn(() => {
|
|
233
|
+
subscribeToUpdatesCall++;
|
|
234
|
+
return subscribeToUpdatesCall === 1 ? initialUnsubscribe : reconnectUnsubscribe;
|
|
235
|
+
}),
|
|
236
|
+
keepAliveQuery: jest.fn().mockResolvedValue(true),
|
|
237
|
+
disposeQuerySubscription: jest.fn().mockResolvedValue(true),
|
|
238
|
+
onReconnect: jest.fn((cb: () => void) => {
|
|
239
|
+
reconnectCallback = cb;
|
|
240
|
+
return unsubReconnect;
|
|
241
|
+
}),
|
|
242
|
+
} as any;
|
|
243
|
+
|
|
244
|
+
const subscription = new QuerySubscriptionProxy('perspective-1', 'SELECT ?x WHERE { ?x ?p ?o }', mockClient);
|
|
245
|
+
await subscription.subscribe();
|
|
246
|
+
|
|
247
|
+
// onReconnect should have been registered
|
|
248
|
+
expect(mockClient.onReconnect).toHaveBeenCalledTimes(1);
|
|
249
|
+
expect(reconnectCallback).toBeDefined();
|
|
250
|
+
|
|
251
|
+
// Reset call counts to isolate the reconnect re-subscribe — but do NOT
|
|
252
|
+
// reset the monotonic counter: the mock must keep returning DISTINCT
|
|
253
|
+
// unsubscribers so the assertions below can tell "old callback disposed"
|
|
254
|
+
// apart from "new callback disposed" (they'd be the same jest.fn if the
|
|
255
|
+
// counter restarted at 1).
|
|
256
|
+
mockClient.subscribeQuery.mockClear();
|
|
257
|
+
mockClient.subscribeToQueryUpdates.mockClear();
|
|
258
|
+
|
|
259
|
+
// Return a fresh subscriptionId on reconnect
|
|
260
|
+
mockClient.subscribeQuery.mockResolvedValue({
|
|
261
|
+
subscriptionId: 'sub-2',
|
|
262
|
+
result: [{ s: 'x', p: 'y', o: 'z' }],
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
// Simulate reconnect
|
|
266
|
+
reconnectCallback!();
|
|
267
|
+
// Allow the async swap to settle
|
|
268
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
269
|
+
|
|
270
|
+
// Should have re-established the server-side subscription with a fresh ID
|
|
271
|
+
// and swapped the client-side callback in place — NEW callback registered
|
|
272
|
+
// BEFORE the old one is disposed (so the WS never dips to 0 subscribers).
|
|
273
|
+
expect(mockClient.subscribeQuery).toHaveBeenCalledWith('perspective-1', 'SELECT ?x WHERE { ?x ?p ?o }');
|
|
274
|
+
expect(mockClient.subscribeToQueryUpdates).toHaveBeenCalledWith('sub-2', expect.any(Function));
|
|
275
|
+
// The OLD callback was disposed, the NEW one was not.
|
|
276
|
+
expect(initialUnsubscribe).toHaveBeenCalledTimes(1);
|
|
277
|
+
expect(reconnectUnsubscribe).not.toHaveBeenCalled();
|
|
278
|
+
|
|
279
|
+
subscription.dispose();
|
|
280
|
+
// dispose() tears down the callback that is live at that point — the
|
|
281
|
+
// reconnect-registered one.
|
|
282
|
+
expect(reconnectUnsubscribe).toHaveBeenCalledTimes(1);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('cleans up reconnect listener on dispose', async () => {
|
|
286
|
+
const unsubReconnect = jest.fn();
|
|
287
|
+
const mockClient = {
|
|
288
|
+
subscribeQuery: jest.fn().mockResolvedValue({
|
|
289
|
+
subscriptionId: 'sub-1',
|
|
290
|
+
result: [],
|
|
291
|
+
}),
|
|
292
|
+
subscribeToQueryUpdates: jest.fn().mockReturnValue(jest.fn()),
|
|
293
|
+
keepAliveQuery: jest.fn().mockResolvedValue(true),
|
|
294
|
+
disposeQuerySubscription: jest.fn().mockResolvedValue(true),
|
|
295
|
+
onReconnect: jest.fn(() => unsubReconnect),
|
|
296
|
+
} as any;
|
|
297
|
+
|
|
298
|
+
const subscription = new QuerySubscriptionProxy('p-1', 'SELECT ?x WHERE { ?x ?p ?o }', mockClient);
|
|
299
|
+
await subscription.subscribe();
|
|
300
|
+
|
|
301
|
+
expect(unsubReconnect).not.toHaveBeenCalled();
|
|
302
|
+
subscription.dispose();
|
|
303
|
+
expect(unsubReconnect).toHaveBeenCalledTimes(1);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
// Regression for CodeRabbit review on PR #899 AND for the follow-on
|
|
307
|
+
// integration-tests-mcp failure the first fix exposed.
|
|
308
|
+
//
|
|
309
|
+
// CodeRabbit warned about the "final-subscriber" loop: when this query
|
|
310
|
+
// owns the LAST `_wsCallbacks` entry in ApiClient, calling `#unsubscribe`
|
|
311
|
+
// inside a reconnect-driven `subscribe()` closes the WebSocket
|
|
312
|
+
// (ApiClient.subscribe()'s deleter: "if no more callbacks and no pending
|
|
313
|
+
// calls, close the socket"). The subsequent `subscribeQuery` reopens the
|
|
314
|
+
// socket, whose fresh `onopen` fires every registered reconnect callback
|
|
315
|
+
// → recursion.
|
|
316
|
+
//
|
|
317
|
+
// The current fix avoids the loop AND the collateral damage by NOT
|
|
318
|
+
// calling `subscribe()` from the reconnect handler at all. Instead it
|
|
319
|
+
// swap-in-place: get a new server-side subscription ID via
|
|
320
|
+
// `subscribeQuery`, register the new client-side callback FIRST, then
|
|
321
|
+
// dispose the old callback. `_wsCallbacks.size` never dips to 0 across
|
|
322
|
+
// the swap, so the socket stays open, no `onopen` re-fires, no
|
|
323
|
+
// cross-proxy RPCs die with 503 (the mcp-http.test.ts "should fire
|
|
324
|
+
// onWake when mention uses agent DID" failure).
|
|
325
|
+
//
|
|
326
|
+
// This test models the invariant the swap guarantees.
|
|
327
|
+
it('reconnect handler swaps subscriptions without dropping to zero WS callbacks (regression)', async () => {
|
|
328
|
+
// Real Set so we can observe the swap ordering.
|
|
329
|
+
const reconnectCallbacks = new Set<() => void>();
|
|
330
|
+
// Count of live client-side callback subscriptions (proxy analogue of
|
|
331
|
+
// ApiClient._wsCallbacks.size). Bumped by subscribeToQueryUpdates, and
|
|
332
|
+
// decremented by the returned unsubscribe. If this ever drops to 0
|
|
333
|
+
// during the swap, ApiClient would close the socket — which is
|
|
334
|
+
// exactly the loop CodeRabbit flagged.
|
|
335
|
+
let liveCallbacks = 0;
|
|
336
|
+
let minLiveDuringSwap = Number.POSITIVE_INFINITY;
|
|
337
|
+
let subscribeQueryCount = 0;
|
|
338
|
+
let subscribeToUpdatesCount = 0;
|
|
339
|
+
|
|
340
|
+
const mockClient = {
|
|
341
|
+
subscribeQuery: jest.fn(async (_uuid: string, _query: string) => {
|
|
342
|
+
subscribeQueryCount++;
|
|
343
|
+
return {
|
|
344
|
+
subscriptionId: `sub-${subscribeQueryCount}`,
|
|
345
|
+
result: [{ s: 'a', p: 'b', o: 'c' }],
|
|
346
|
+
};
|
|
347
|
+
}),
|
|
348
|
+
subscribeToQueryUpdates: jest.fn(() => {
|
|
349
|
+
subscribeToUpdatesCount++;
|
|
350
|
+
liveCallbacks++;
|
|
351
|
+
// Sample the invariant after each mutation.
|
|
352
|
+
if (liveCallbacks < minLiveDuringSwap) minLiveDuringSwap = liveCallbacks;
|
|
353
|
+
return jest.fn(() => {
|
|
354
|
+
liveCallbacks--;
|
|
355
|
+
if (liveCallbacks < minLiveDuringSwap) minLiveDuringSwap = liveCallbacks;
|
|
356
|
+
});
|
|
357
|
+
}),
|
|
358
|
+
keepAliveQuery: jest.fn().mockResolvedValue(true),
|
|
359
|
+
disposeQuerySubscription: jest.fn().mockResolvedValue(true),
|
|
360
|
+
onReconnect: jest.fn((cb: () => void) => {
|
|
361
|
+
reconnectCallbacks.add(cb);
|
|
362
|
+
return () => { reconnectCallbacks.delete(cb); };
|
|
363
|
+
}),
|
|
364
|
+
} as any;
|
|
365
|
+
|
|
366
|
+
const subscription = new QuerySubscriptionProxy(
|
|
367
|
+
'perspective-1',
|
|
368
|
+
'SELECT ?x WHERE { ?x ?p ?o }',
|
|
369
|
+
mockClient,
|
|
370
|
+
);
|
|
371
|
+
|
|
372
|
+
// Initial subscribe — one server subscription, one live callback.
|
|
373
|
+
await subscription.subscribe();
|
|
374
|
+
expect(subscribeQueryCount).toBe(1);
|
|
375
|
+
expect(subscribeToUpdatesCount).toBe(1);
|
|
376
|
+
expect(liveCallbacks).toBe(1);
|
|
377
|
+
expect(reconnectCallbacks.size).toBe(1);
|
|
378
|
+
// Reset the low-water mark to the post-init steady state so we only
|
|
379
|
+
// measure what the reconnect handler does.
|
|
380
|
+
minLiveDuringSwap = liveCallbacks;
|
|
381
|
+
|
|
382
|
+
// Fire one genuine reconnect. The handler should:
|
|
383
|
+
// (a) call subscribeQuery (new server-side sub id: sub-2)
|
|
384
|
+
// (b) call subscribeToQueryUpdates for sub-2 → liveCallbacks 1 → 2
|
|
385
|
+
// (c) THEN invoke the old unsubscribe → liveCallbacks 2 → 1
|
|
386
|
+
// At no point should liveCallbacks reach 0. And the fresh listener
|
|
387
|
+
// installed at the top of the initial subscribe() must NOT itself
|
|
388
|
+
// re-enter subscribe() from this reconnect.
|
|
389
|
+
for (const cb of Array.from(reconnectCallbacks)) cb();
|
|
390
|
+
// Let the async swap settle.
|
|
391
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
392
|
+
|
|
393
|
+
expect(subscribeQueryCount).toBe(2);
|
|
394
|
+
expect(subscribeToUpdatesCount).toBe(2);
|
|
395
|
+
// The critical assertion: the swap never let the callback set go to 0.
|
|
396
|
+
// A zero here would mean the socket got closed → reopened → onopen
|
|
397
|
+
// fires reconnect callbacks again → the loop CodeRabbit warned about.
|
|
398
|
+
expect(minLiveDuringSwap).toBeGreaterThanOrEqual(1);
|
|
399
|
+
// One reconnect fired → exactly one server-side re-establish. NOT a
|
|
400
|
+
// recursive cascade.
|
|
401
|
+
expect(reconnectCallbacks.size).toBe(1);
|
|
402
|
+
expect(liveCallbacks).toBe(1);
|
|
403
|
+
|
|
404
|
+
subscription.dispose();
|
|
405
|
+
// Dispose drops both the live callback and the reconnect listener.
|
|
406
|
+
expect(liveCallbacks).toBe(0);
|
|
407
|
+
expect(reconnectCallbacks.size).toBe(0);
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
// Regression for the review-blocker on PR #899: a failed full resubscribe
|
|
411
|
+
// (the keepalive / init-timeout retry path) used to remove the reconnect
|
|
412
|
+
// listener at the top of subscribe() and only re-register on success —
|
|
413
|
+
// one subscribeQuery failure during a network flap left the proxy
|
|
414
|
+
// permanently dead (keepalive loop stops itself on resubscribe failure,
|
|
415
|
+
// no listener left to retry). The catch block must install a recovery
|
|
416
|
+
// listener so the next reconnect retries the full subscribe.
|
|
417
|
+
it('keeps reconnect recovery alive after a failed resubscribe attempt', async () => {
|
|
418
|
+
let reconnectCallback: (() => void) | undefined;
|
|
419
|
+
const mockClient = {
|
|
420
|
+
subscribeQuery: jest.fn()
|
|
421
|
+
.mockResolvedValueOnce({ subscriptionId: 'sub-1', result: [] }) // initial subscribe OK
|
|
422
|
+
.mockRejectedValueOnce(new Error('flap')) // resubscribe attempt fails
|
|
423
|
+
.mockResolvedValueOnce({ subscriptionId: 'sub-2', result: [] }), // recovery succeeds
|
|
424
|
+
subscribeToQueryUpdates: jest.fn().mockReturnValue(jest.fn()),
|
|
425
|
+
keepAliveQuery: jest.fn().mockResolvedValue(true),
|
|
426
|
+
disposeQuerySubscription: jest.fn().mockResolvedValue(true),
|
|
427
|
+
onReconnect: jest.fn((cb: () => void) => {
|
|
428
|
+
reconnectCallback = cb;
|
|
429
|
+
return jest.fn();
|
|
430
|
+
}),
|
|
431
|
+
} as any;
|
|
432
|
+
|
|
433
|
+
const subscription = new QuerySubscriptionProxy('p-1', 'SELECT ?x WHERE { ?x ?p ?o }', mockClient);
|
|
434
|
+
await subscription.subscribe();
|
|
435
|
+
expect(subscription.id).toBe('sub-1');
|
|
436
|
+
|
|
437
|
+
// A retry-subscribe fails (this is what the keepalive path does when
|
|
438
|
+
// the server subscription died).
|
|
439
|
+
await expect(subscription.subscribe()).rejects.toThrow('flap');
|
|
440
|
+
|
|
441
|
+
// The failure must have installed a recovery listener — total listener
|
|
442
|
+
// registrations: initial subscribe + failure recovery.
|
|
443
|
+
expect(mockClient.onReconnect).toHaveBeenCalledTimes(2);
|
|
444
|
+
expect(reconnectCallback).toBeDefined();
|
|
445
|
+
|
|
446
|
+
// A reconnect arrives → the recovery listener runs a full subscribe()
|
|
447
|
+
// and the proxy comes back to life on a fresh server subscription.
|
|
448
|
+
reconnectCallback!();
|
|
449
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
450
|
+
|
|
451
|
+
expect(mockClient.subscribeQuery).toHaveBeenCalledTimes(3);
|
|
452
|
+
expect(subscription.id).toBe('sub-2');
|
|
453
|
+
|
|
454
|
+
subscription.dispose();
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
// Regression for the concurrent-writers race flagged in the PR #899
|
|
458
|
+
// review: the reconnect swap handler, the keepalive-retry subscribe() and
|
|
459
|
+
// the init-timeout retry subscribe() all write `#unsubscribe` /
|
|
460
|
+
// `#subscriptionId`. Without the generation guard, a swap handler parked
|
|
461
|
+
// on its subscribeQuery while a full subscribe() ran could register a
|
|
462
|
+
// client-side callback that the subscribe() then overwrote WITHOUT
|
|
463
|
+
// disposing — leaking the callback in ApiClient._wsCallbacks for the
|
|
464
|
+
// client lifetime (holding the socket open and firing into a dead
|
|
465
|
+
// subscription). The stale writer must back out and release its
|
|
466
|
+
// server-side subscription instead.
|
|
467
|
+
it('a reconnect swap superseded by a concurrent resubscribe backs out without leaking callbacks', async () => {
|
|
468
|
+
let reconnectCallback: (() => void) | undefined;
|
|
469
|
+
// Deferred subscribeQuery responses so the test controls interleaving.
|
|
470
|
+
const deferreds: Array<{ resolve: (v: any) => void }> = [];
|
|
471
|
+
let subCounter = 0;
|
|
472
|
+
let liveCallbacks = 0;
|
|
473
|
+
const disposedServerSubs: string[] = [];
|
|
474
|
+
|
|
475
|
+
const mockClient = {
|
|
476
|
+
subscribeQuery: jest.fn(() => new Promise((resolve) => {
|
|
477
|
+
deferreds.push({ resolve });
|
|
478
|
+
})),
|
|
479
|
+
subscribeToQueryUpdates: jest.fn(() => {
|
|
480
|
+
liveCallbacks++;
|
|
481
|
+
return jest.fn(() => { liveCallbacks--; });
|
|
482
|
+
}),
|
|
483
|
+
keepAliveQuery: jest.fn().mockResolvedValue(true),
|
|
484
|
+
disposeQuerySubscription: jest.fn((_uuid: string, subId: string) => {
|
|
485
|
+
disposedServerSubs.push(subId);
|
|
486
|
+
return Promise.resolve(true);
|
|
487
|
+
}),
|
|
488
|
+
onReconnect: jest.fn((cb: () => void) => {
|
|
489
|
+
reconnectCallback = cb;
|
|
490
|
+
return jest.fn();
|
|
491
|
+
}),
|
|
492
|
+
} as any;
|
|
493
|
+
|
|
494
|
+
const subscription = new QuerySubscriptionProxy('p-1', 'SELECT ?x WHERE { ?x ?p ?o }', mockClient);
|
|
495
|
+
|
|
496
|
+
// Initial subscribe.
|
|
497
|
+
const initial = subscription.subscribe();
|
|
498
|
+
deferreds[0].resolve({ subscriptionId: `sub-${++subCounter}`, result: [] });
|
|
499
|
+
await initial;
|
|
500
|
+
expect(liveCallbacks).toBe(1);
|
|
501
|
+
|
|
502
|
+
// 1. Reconnect fires; the swap handler parks on its subscribeQuery.
|
|
503
|
+
reconnectCallback!();
|
|
504
|
+
expect(deferreds.length).toBe(2);
|
|
505
|
+
|
|
506
|
+
// 2. While the handler is parked, a full resubscribe runs (keepalive
|
|
507
|
+
// retry analogue) and parks on ITS subscribeQuery.
|
|
508
|
+
const retry = subscription.subscribe();
|
|
509
|
+
expect(deferreds.length).toBe(3);
|
|
510
|
+
|
|
511
|
+
// 3. The handler's subscribeQuery resolves FIRST — the handler is now
|
|
512
|
+
// stale (the full subscribe superseded it) and must back out.
|
|
513
|
+
deferreds[1].resolve({ subscriptionId: 'sub-stale', result: [] });
|
|
514
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
515
|
+
|
|
516
|
+
// 4. The full subscribe's subscribeQuery resolves and completes.
|
|
517
|
+
deferreds[2].resolve({ subscriptionId: `sub-${++subCounter}`, result: [] });
|
|
518
|
+
await retry;
|
|
519
|
+
|
|
520
|
+
// Exactly ONE live client-side callback — the stale handler must not
|
|
521
|
+
// have left an orphaned one behind (nor disposed the winner's).
|
|
522
|
+
expect(liveCallbacks).toBe(1);
|
|
523
|
+
// The winner's subscription is active…
|
|
524
|
+
expect(subscription.id).toBe('sub-2');
|
|
525
|
+
// …and the stale handler released its orphaned server-side subscription.
|
|
526
|
+
expect(disposedServerSubs).toContain('sub-stale');
|
|
527
|
+
|
|
528
|
+
subscription.dispose();
|
|
529
|
+
expect(liveCallbacks).toBe(0);
|
|
530
|
+
});
|
|
125
531
|
});
|