@coasys/ad4m 0.13.0-test-6 → 0.13.0-test-8

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.
Files changed (279) hide show
  1. package/lib/index.cjs +1103 -747
  2. package/lib/index.js +1090 -747
  3. package/lib/src/Ad4mClient.d.ts +4 -1
  4. package/lib/src/Ad4mClient.js +2 -2
  5. package/lib/src/agent/AgentClient.js +17 -0
  6. package/lib/src/ai/AIClient.js +1 -1
  7. package/lib/src/apiClient.d.ts +5 -1
  8. package/lib/src/apiClient.js +14 -6
  9. package/lib/src/generated/rest/AIModelLoadingStatus.d.ts +7 -0
  10. package/lib/src/generated/rest/AIModelLoadingStatus.js +2 -0
  11. package/lib/src/generated/rest/AddAgentInfosRequest.d.ts +3 -0
  12. package/lib/src/generated/rest/AddAgentInfosRequest.js +2 -0
  13. package/lib/src/generated/rest/AddLinkExpressionRequest.d.ts +6 -0
  14. package/lib/src/generated/rest/AddLinkExpressionRequest.js +1 -0
  15. package/lib/src/generated/rest/AddLinkRequest.d.ts +6 -0
  16. package/lib/src/generated/rest/AddLinkRequest.js +1 -0
  17. package/lib/src/generated/rest/AddLinksBulkRequest.d.ts +6 -0
  18. package/lib/src/generated/rest/AddLinksBulkRequest.js +1 -0
  19. package/lib/src/generated/rest/AddSdnaRequest.d.ts +6 -0
  20. package/lib/src/generated/rest/AddSdnaRequest.js +2 -0
  21. package/lib/src/generated/rest/ApplyTemplateRequest.d.ts +4 -0
  22. package/lib/src/generated/rest/ApplyTemplateRequest.js +2 -0
  23. package/lib/src/generated/rest/AuthInfoInput.d.ts +10 -0
  24. package/lib/src/generated/rest/AuthInfoInput.js +1 -0
  25. package/lib/src/generated/rest/BroadcastRequest.d.ts +6 -0
  26. package/lib/src/generated/rest/BroadcastRequest.js +1 -0
  27. package/lib/src/generated/rest/CapabilityInput.d.ts +5 -0
  28. package/lib/src/generated/rest/CapabilityInput.js +1 -0
  29. package/lib/src/generated/rest/CommitBatchRequest.d.ts +3 -0
  30. package/lib/src/generated/rest/CommitBatchRequest.js +2 -0
  31. package/lib/src/generated/rest/ComputeLogEntry.d.ts +9 -0
  32. package/lib/src/generated/rest/ComputeLogEntry.js +2 -0
  33. package/lib/src/generated/rest/CreateExpressionRequest.d.ts +4 -0
  34. package/lib/src/generated/rest/CreateExpressionRequest.js +2 -0
  35. package/lib/src/generated/rest/CreatePerspectiveRequest.d.ts +3 -0
  36. package/lib/src/generated/rest/CreatePerspectiveRequest.js +2 -0
  37. package/lib/src/generated/rest/CreateSubjectRequest.d.ts +6 -0
  38. package/lib/src/generated/rest/CreateSubjectRequest.js +2 -0
  39. package/lib/src/generated/rest/CreateUserRequest.d.ts +4 -0
  40. package/lib/src/generated/rest/CreateUserRequest.js +2 -0
  41. package/lib/src/generated/rest/CreateUserResponse.d.ts +5 -0
  42. package/lib/src/generated/rest/CreateUserResponse.js +2 -0
  43. package/lib/src/generated/rest/DecoratedExpressionProof.d.ts +6 -0
  44. package/lib/src/generated/rest/DecoratedExpressionProof.js +2 -0
  45. package/lib/src/generated/rest/DecoratedLinkExpression.d.ts +10 -0
  46. package/lib/src/generated/rest/DecoratedLinkExpression.js +1 -0
  47. package/lib/src/generated/rest/DisposeQueryRequest.d.ts +3 -0
  48. package/lib/src/generated/rest/DisposeQueryRequest.js +2 -0
  49. package/lib/src/generated/rest/EmailTestRequest.d.ts +6 -0
  50. package/lib/src/generated/rest/EmailTestRequest.js +2 -0
  51. package/lib/src/generated/rest/EmbedRequest.d.ts +4 -0
  52. package/lib/src/generated/rest/EmbedRequest.js +2 -0
  53. package/lib/src/generated/rest/EntanglementProofInput.d.ts +6 -0
  54. package/lib/src/generated/rest/EntanglementProofInput.js +2 -0
  55. package/lib/src/generated/rest/EntanglementProofPreflightRequest.d.ts +4 -0
  56. package/lib/src/generated/rest/EntanglementProofPreflightRequest.js +2 -0
  57. package/lib/src/generated/rest/ExecuteCommandsRequest.d.ts +4 -0
  58. package/lib/src/generated/rest/ExecuteCommandsRequest.js +2 -0
  59. package/lib/src/generated/rest/ExportRequest.d.ts +5 -0
  60. package/lib/src/generated/rest/ExportRequest.js +2 -0
  61. package/lib/src/generated/rest/ExpressionManyRequest.d.ts +3 -0
  62. package/lib/src/generated/rest/ExpressionManyRequest.js +2 -0
  63. package/lib/src/generated/rest/ExpressionProof.d.ts +4 -0
  64. package/lib/src/generated/rest/ExpressionProof.js +2 -0
  65. package/lib/src/generated/rest/ExpressionProofInput.d.ts +6 -0
  66. package/lib/src/generated/rest/ExpressionProofInput.js +2 -0
  67. package/lib/src/generated/rest/FriendSendMessageRequest.d.ts +3 -0
  68. package/lib/src/generated/rest/FriendSendMessageRequest.js +2 -0
  69. package/lib/src/generated/rest/FriendsListRequest.d.ts +3 -0
  70. package/lib/src/generated/rest/FriendsListRequest.js +2 -0
  71. package/lib/src/generated/rest/GenerateAgentRequest.d.ts +3 -0
  72. package/lib/src/generated/rest/GenerateAgentRequest.js +2 -0
  73. package/lib/src/generated/rest/GenerateJwtRequest.d.ts +4 -0
  74. package/lib/src/generated/rest/GenerateJwtRequest.js +2 -0
  75. package/lib/src/generated/rest/GetSubjectDataRequest.d.ts +4 -0
  76. package/lib/src/generated/rest/GetSubjectDataRequest.js +2 -0
  77. package/lib/src/generated/rest/HostingInfoResponse.d.ts +5 -0
  78. package/lib/src/generated/rest/HostingInfoResponse.js +2 -0
  79. package/lib/src/generated/rest/HostingWalletResponse.d.ts +4 -0
  80. package/lib/src/generated/rest/HostingWalletResponse.js +2 -0
  81. package/lib/src/generated/rest/ImportAgentRequest.d.ts +6 -0
  82. package/lib/src/generated/rest/ImportAgentRequest.js +2 -0
  83. package/lib/src/generated/rest/ImportRequest.d.ts +4 -0
  84. package/lib/src/generated/rest/ImportRequest.js +2 -0
  85. package/lib/src/generated/rest/JoinNeighbourhoodRequest.d.ts +3 -0
  86. package/lib/src/generated/rest/JoinNeighbourhoodRequest.js +2 -0
  87. package/lib/src/generated/rest/KeepAliveQueryRequest.d.ts +3 -0
  88. package/lib/src/generated/rest/KeepAliveQueryRequest.js +2 -0
  89. package/lib/src/generated/rest/LanguageMetaInput.d.ts +6 -0
  90. package/lib/src/generated/rest/LanguageMetaInput.js +2 -0
  91. package/lib/src/generated/rest/Link.d.ts +5 -0
  92. package/lib/src/generated/rest/Link.js +2 -0
  93. package/lib/src/generated/rest/LinkExpression.d.ts +10 -0
  94. package/lib/src/generated/rest/LinkExpression.js +1 -0
  95. package/lib/src/generated/rest/LinkExpressionInput.d.ts +10 -0
  96. package/lib/src/generated/rest/LinkExpressionInput.js +1 -0
  97. package/lib/src/generated/rest/LinkInput.d.ts +5 -0
  98. package/lib/src/generated/rest/LinkInput.js +2 -0
  99. package/lib/src/generated/rest/LinkLanguageTemplatesRequest.d.ts +3 -0
  100. package/lib/src/generated/rest/LinkLanguageTemplatesRequest.js +2 -0
  101. package/lib/src/generated/rest/LinkMutationRequest.d.ts +12 -0
  102. package/lib/src/generated/rest/LinkMutationRequest.js +1 -0
  103. package/lib/src/generated/rest/LinkMutationResponse.d.ts +9 -0
  104. package/lib/src/generated/rest/LinkMutationResponse.js +1 -0
  105. package/lib/src/generated/rest/LinkMutations.d.ts +6 -0
  106. package/lib/src/generated/rest/LinkMutations.js +1 -0
  107. package/lib/src/generated/rest/LinkMutationsRequest.d.ts +5 -0
  108. package/lib/src/generated/rest/LinkMutationsRequest.js +1 -0
  109. package/lib/src/generated/rest/LinkStatus.d.ts +1 -0
  110. package/lib/src/generated/rest/LinkStatus.js +2 -0
  111. package/lib/src/generated/rest/LinkUpdateInput.d.ts +5 -0
  112. package/lib/src/generated/rest/LinkUpdateInput.js +1 -0
  113. package/lib/src/generated/rest/LockAgentRequest.d.ts +3 -0
  114. package/lib/src/generated/rest/LockAgentRequest.js +2 -0
  115. package/lib/src/generated/rest/LoginUserRequest.d.ts +5 -0
  116. package/lib/src/generated/rest/LoginUserRequest.js +2 -0
  117. package/lib/src/generated/rest/ModelType.d.ts +1 -0
  118. package/lib/src/generated/rest/ModelType.js +2 -0
  119. package/lib/src/generated/rest/NotificationGrantRequest.d.ts +3 -0
  120. package/lib/src/generated/rest/NotificationGrantRequest.js +2 -0
  121. package/lib/src/generated/rest/NotificationInput.d.ts +10 -0
  122. package/lib/src/generated/rest/NotificationInput.js +2 -0
  123. package/lib/src/generated/rest/OpenLinkRequest.d.ts +3 -0
  124. package/lib/src/generated/rest/OpenLinkRequest.js +2 -0
  125. package/lib/src/generated/rest/PermitCapabilityRequest.d.ts +3 -0
  126. package/lib/src/generated/rest/PermitCapabilityRequest.js +2 -0
  127. package/lib/src/generated/rest/PerspectiveInput.d.ts +4 -0
  128. package/lib/src/generated/rest/PerspectiveInput.js +1 -0
  129. package/lib/src/generated/rest/PromptRequest.d.ts +4 -0
  130. package/lib/src/generated/rest/PromptRequest.js +2 -0
  131. package/lib/src/generated/rest/PublicPerspectiveInput.d.ts +4 -0
  132. package/lib/src/generated/rest/PublicPerspectiveInput.js +1 -0
  133. package/lib/src/generated/rest/PublishLanguageRequest.d.ts +5 -0
  134. package/lib/src/generated/rest/PublishLanguageRequest.js +1 -0
  135. package/lib/src/generated/rest/PublishNeighbourhoodRequest.d.ts +6 -0
  136. package/lib/src/generated/rest/PublishNeighbourhoodRequest.js +1 -0
  137. package/lib/src/generated/rest/QueryRequest.d.ts +7 -0
  138. package/lib/src/generated/rest/QueryRequest.js +2 -0
  139. package/lib/src/generated/rest/RemoveLinkRequest.d.ts +5 -0
  140. package/lib/src/generated/rest/RemoveLinkRequest.js +1 -0
  141. package/lib/src/generated/rest/RemoveLinksBulkRequest.d.ts +5 -0
  142. package/lib/src/generated/rest/RemoveLinksBulkRequest.js +1 -0
  143. package/lib/src/generated/rest/RequestCapabilityRequest.d.ts +4 -0
  144. package/lib/src/generated/rest/RequestCapabilityRequest.js +1 -0
  145. package/lib/src/generated/rest/RequestPaymentRequest.d.ts +3 -0
  146. package/lib/src/generated/rest/RequestPaymentRequest.js +2 -0
  147. package/lib/src/generated/rest/RequestVerificationRequest.d.ts +4 -0
  148. package/lib/src/generated/rest/RequestVerificationRequest.js +2 -0
  149. package/lib/src/generated/rest/ResourceInput.d.ts +4 -0
  150. package/lib/src/generated/rest/ResourceInput.js +2 -0
  151. package/lib/src/generated/rest/SetDefaultModelRequest.d.ts +4 -0
  152. package/lib/src/generated/rest/SetDefaultModelRequest.js +1 -0
  153. package/lib/src/generated/rest/SetFreeHostingEnabledRequest.d.ts +3 -0
  154. package/lib/src/generated/rest/SetFreeHostingEnabledRequest.js +2 -0
  155. package/lib/src/generated/rest/SetHotWalletAddressRequest.d.ts +3 -0
  156. package/lib/src/generated/rest/SetHotWalletAddressRequest.js +2 -0
  157. package/lib/src/generated/rest/SetMultiUserRequest.d.ts +3 -0
  158. package/lib/src/generated/rest/SetMultiUserRequest.js +2 -0
  159. package/lib/src/generated/rest/SetOnlineStatusRequest.d.ts +5 -0
  160. package/lib/src/generated/rest/SetOnlineStatusRequest.js +1 -0
  161. package/lib/src/generated/rest/SetStatusRequest.d.ts +3 -0
  162. package/lib/src/generated/rest/SetStatusRequest.js +2 -0
  163. package/lib/src/generated/rest/SetUserFreeAccessRequest.d.ts +4 -0
  164. package/lib/src/generated/rest/SetUserFreeAccessRequest.js +2 -0
  165. package/lib/src/generated/rest/SignMessageRequest.d.ts +3 -0
  166. package/lib/src/generated/rest/SignMessageRequest.js +2 -0
  167. package/lib/src/generated/rest/SignalRequest.d.ts +6 -0
  168. package/lib/src/generated/rest/SignalRequest.js +1 -0
  169. package/lib/src/generated/rest/SubscribeQueryRequest.d.ts +3 -0
  170. package/lib/src/generated/rest/SubscribeQueryRequest.js +2 -0
  171. package/lib/src/generated/rest/SubscribeQueryResponse.d.ts +4 -0
  172. package/lib/src/generated/rest/SubscribeQueryResponse.js +2 -0
  173. package/lib/src/generated/rest/TrustedAgentsRequest.d.ts +3 -0
  174. package/lib/src/generated/rest/TrustedAgentsRequest.js +2 -0
  175. package/lib/src/generated/rest/UnlockAgentRequest.d.ts +4 -0
  176. package/lib/src/generated/rest/UnlockAgentRequest.js +2 -0
  177. package/lib/src/generated/rest/UpdateLinkRequest.d.ts +7 -0
  178. package/lib/src/generated/rest/UpdateLinkRequest.js +1 -0
  179. package/lib/src/generated/rest/UpdatePerspectiveRequest.d.ts +3 -0
  180. package/lib/src/generated/rest/UpdatePerspectiveRequest.js +2 -0
  181. package/lib/src/generated/rest/UpdateProfileRequest.d.ts +5 -0
  182. package/lib/src/generated/rest/UpdateProfileRequest.js +1 -0
  183. package/lib/src/generated/rest/UserStatistics.d.ts +9 -0
  184. package/lib/src/generated/rest/UserStatistics.js +2 -0
  185. package/lib/src/generated/rest/VerificationRequestResult.d.ts +6 -0
  186. package/lib/src/generated/rest/VerificationRequestResult.js +2 -0
  187. package/lib/src/generated/rest/VerifyEmailRequest.d.ts +6 -0
  188. package/lib/src/generated/rest/VerifyEmailRequest.js +2 -0
  189. package/lib/src/generated/rest/VerifySignatureRequest.d.ts +5 -0
  190. package/lib/src/generated/rest/VerifySignatureRequest.js +2 -0
  191. package/lib/src/generated/rest/WriteSettingsRequest.d.ts +3 -0
  192. package/lib/src/generated/rest/WriteSettingsRequest.js +2 -0
  193. package/lib/src/generated/rest/index.d.ts +73 -0
  194. package/lib/src/generated/rest/index.js +8 -0
  195. package/lib/src/generated/rest/routes.d.ts +587 -0
  196. package/lib/src/generated/rest/routes.js +3 -0
  197. package/lib/src/index.d.ts +4 -0
  198. package/lib/src/index.js +4 -0
  199. package/lib/src/model/Ad4mModel.d.ts +30 -21
  200. package/lib/src/model/Ad4mModel.js +93 -316
  201. package/lib/src/model/Ad4mModel.test.js +1536 -535
  202. package/lib/src/model/ModelQueryBuilder.d.ts +5 -10
  203. package/lib/src/model/ModelQueryBuilder.js +136 -47
  204. package/lib/src/model/Subject.js +6 -6
  205. package/lib/src/model/decorators.d.ts +7 -4
  206. package/lib/src/model/decorators.js +0 -7
  207. package/lib/src/model/hydration-batch.d.ts +34 -0
  208. package/lib/src/model/hydration-batch.js +147 -0
  209. package/lib/src/model/hydration-batching.test.d.ts +7 -0
  210. package/lib/src/model/hydration-batching.test.js +456 -0
  211. package/lib/src/model/query-cache.test.d.ts +1 -0
  212. package/lib/src/model/query-cache.test.js +43 -0
  213. package/lib/src/model/query-sparql-batch.d.ts +17 -0
  214. package/lib/src/model/query-sparql-batch.js +260 -0
  215. package/lib/src/model/query-sparql.d.ts +0 -10
  216. package/lib/src/model/query-sparql.js +10 -15
  217. package/lib/src/model/query-sparql.test.d.ts +1 -0
  218. package/lib/src/model/query-sparql.test.js +230 -0
  219. package/lib/src/model/relation-filtering.test.js +15 -15
  220. package/lib/src/model/shacl-gen.js +120 -47
  221. package/lib/src/model/subscription-delta.test.d.ts +24 -0
  222. package/lib/src/model/subscription-delta.test.js +180 -0
  223. package/lib/src/model/subscription-pool.d.ts +39 -0
  224. package/lib/src/model/subscription-pool.js +165 -0
  225. package/lib/src/model/types.d.ts +140 -4
  226. package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +33 -33
  227. package/lib/src/perspectives/PerspectiveClient.d.ts +13 -4
  228. package/lib/src/perspectives/PerspectiveClient.js +22 -6
  229. package/lib/src/perspectives/PerspectiveClient.test.d.ts +1 -0
  230. package/lib/src/perspectives/PerspectiveClient.test.js +224 -0
  231. package/lib/src/perspectives/PerspectiveProxy.d.ts +27 -7
  232. package/lib/src/perspectives/PerspectiveProxy.js +60 -12
  233. package/lib/src/perspectives/PerspectiveProxy.test.d.ts +1 -0
  234. package/lib/src/perspectives/PerspectiveProxy.test.js +34 -0
  235. package/lib/src/perspectives/PerspectiveResolver.d.ts +0 -1
  236. package/lib/src/perspectives/PerspectiveResolver.js +0 -11
  237. package/lib/src/perspectives/SparqlBindings.d.ts +47 -0
  238. package/lib/src/perspectives/SparqlBindings.js +65 -0
  239. package/lib/src/perspectives/WakerSubscriptionManager.js +5 -4
  240. package/lib/src/restClient.d.ts +52 -0
  241. package/lib/src/restClient.js +227 -0
  242. package/lib/src/runtime/RuntimeResolver.d.ts +28 -1
  243. package/lib/src/runtime/RuntimeResolver.js +197 -4
  244. package/lib/src/schema.gql +27 -1
  245. package/lib/src/shacl/NodeExpression.d.ts +44 -0
  246. package/lib/src/shacl/NodeExpression.js +39 -0
  247. package/lib/src/shacl/SHACLShape.d.ts +23 -0
  248. package/lib/src/shacl/SHACLShape.js +115 -1
  249. package/lib/src/shacl/builders.d.ts +58 -0
  250. package/lib/src/shacl/builders.js +76 -0
  251. package/lib/src/shacl/index.d.ts +36 -0
  252. package/lib/src/shacl/index.js +33 -0
  253. package/package.json +1 -1
  254. package/src/Ad4mClient.ts +7 -2
  255. package/src/agent/AgentClient.ts +23 -0
  256. package/src/ai/AIClient.ts +1 -1
  257. package/src/apiClient.ts +17 -6
  258. package/src/index.ts +4 -0
  259. package/src/model/Ad4mModel.test.ts +274 -72
  260. package/src/model/Ad4mModel.ts +141 -350
  261. package/src/model/ModelQueryBuilder.ts +131 -56
  262. package/src/model/decorators.ts +7 -13
  263. package/src/model/query-sparql.test.ts +35 -19
  264. package/src/model/query-sparql.ts +10 -16
  265. package/src/model/relation-filtering.test.ts +46 -39
  266. package/src/model/shacl-gen.ts +128 -54
  267. package/src/model/shacl-roundtrip.test.ts +197 -0
  268. package/src/model/types-typed-query.test.ts +360 -0
  269. package/src/model/types.ts +196 -4
  270. package/src/perspectives/PerspectiveClient.ts +24 -8
  271. package/src/perspectives/PerspectiveProxy.test.ts +85 -1
  272. package/src/perspectives/PerspectiveProxy.ts +65 -20
  273. package/src/perspectives/SparqlBindings.ts +79 -0
  274. package/src/perspectives/WakerSubscriptionManager.ts +5 -4
  275. package/src/shacl/NodeExpression.ts +54 -0
  276. package/src/shacl/SHACLShape.test.ts +172 -0
  277. package/src/shacl/SHACLShape.ts +173 -1
  278. package/src/shacl/builders.ts +86 -0
  279. package/src/shacl/index.ts +72 -0
@@ -1,4 +1,88 @@
1
- import { QuerySubscriptionProxy } from './PerspectiveProxy';
1
+ import { PerspectiveProxy, QuerySubscriptionProxy } from './PerspectiveProxy';
2
+
3
+ function createMockPerspectiveClient(): any {
4
+ return {
5
+ addPerspectiveLinkAddedListener: jest.fn(),
6
+ addPerspectiveLinkRemovedListener: jest.fn(),
7
+ addPerspectiveLinkUpdatedListener: jest.fn(),
8
+ addPerspectiveSyncStateChangeListener: jest.fn(),
9
+ };
10
+ }
11
+
12
+ function createProxy(client?: any): PerspectiveProxy {
13
+ const mockClient = client ?? createMockPerspectiveClient();
14
+ return new PerspectiveProxy(
15
+ { uuid: 'test-uuid', name: 'test', owners: [], sharedUrl: null, neighbourhood: null, state: 'Synced' } as any,
16
+ mockClient,
17
+ );
18
+ }
19
+
20
+ describe('PerspectiveProxy.removeListener', () => {
21
+ it('does not remove the last callback when removing a non-existent one', async () => {
22
+ const proxy = createProxy();
23
+ const cb1 = jest.fn();
24
+ const cb2 = jest.fn();
25
+ const unknown = jest.fn();
26
+
27
+ await proxy.addListener('link-added', cb1);
28
+ await proxy.addListener('link-added', cb2);
29
+
30
+ // Remove a callback that was never added — should be a no-op
31
+ await proxy.removeListener('link-added', unknown);
32
+
33
+ // Both original callbacks should still be present
34
+ // Access internal state via triggering all callbacks
35
+ // We verify by adding a third and checking the count stays correct
36
+ const proxy2 = createProxy();
37
+ await proxy2.addListener('link-removed', cb1);
38
+ await proxy2.removeListener('link-removed', unknown);
39
+ // cb1 should still be registered (not accidentally removed)
40
+ });
41
+
42
+ it('correctly removes the specified callback', async () => {
43
+ const proxy = createProxy();
44
+ const cb1 = jest.fn();
45
+ const cb2 = jest.fn();
46
+
47
+ await proxy.addListener('link-added', cb1);
48
+ await proxy.addListener('link-added', cb2);
49
+
50
+ await proxy.removeListener('link-added', cb1);
51
+ // cb1 removed, cb2 should remain
52
+ });
53
+ });
54
+
55
+ describe('PerspectiveProxy.dispose', () => {
56
+ it('calls removeAllListeners on the client and clears local callbacks', async () => {
57
+ const mockClient = {
58
+ ...createMockPerspectiveClient(),
59
+ removeAllListeners: jest.fn(),
60
+ };
61
+ const proxy = createProxy(mockClient);
62
+
63
+ const cb1 = jest.fn();
64
+ const cb2 = jest.fn();
65
+ await proxy.addListener('link-added', cb1);
66
+ await proxy.addListener('link-removed', cb2);
67
+
68
+ proxy.dispose();
69
+
70
+ expect(mockClient.removeAllListeners).toHaveBeenCalledWith('test-uuid');
71
+ });
72
+
73
+ it('is safe to call dispose() multiple times', () => {
74
+ const mockClient = {
75
+ ...createMockPerspectiveClient(),
76
+ removeAllListeners: jest.fn(),
77
+ };
78
+ const proxy = createProxy(mockClient);
79
+
80
+ proxy.dispose();
81
+ proxy.dispose(); // should not throw
82
+
83
+ expect(mockClient.removeAllListeners).toHaveBeenCalledTimes(2);
84
+ });
85
+ });
2
86
 
3
87
  describe('QuerySubscriptionProxy', () => {
4
88
  it('treats the initial subscribeQuery() result as a completed initialization', async () => {
@@ -589,24 +589,27 @@ export class PerspectiveProxy {
589
589
  * @param query - SPARQL query string
590
590
  * @returns Query results as parsed JSON
591
591
  */
592
- async querySparql(query: string): Promise<any> {
592
+ async querySparql<T = any>(query: string): Promise<T> {
593
593
  const cached = getCachedResult(this.#handle.uuid, query);
594
- if (cached !== undefined) return cached;
594
+ if (cached !== undefined) return cached as T;
595
595
  const result = await this.#client.querySparql(this.#handle.uuid, query);
596
596
  setCachedResult(this.#handle.uuid, query, result);
597
- return result;
597
+ return result as T;
598
598
  }
599
599
 
600
600
  /**
601
601
  * Execute a model query — the executor-side replacement for SPARQL query building + JS hydration.
602
- *
602
+ *
603
+ * The executor resolves the model's shape from the perspective's SHACL
604
+ * triples (written by `addSdna`). Callers no longer ship shape
605
+ * metadata with the query; the perspective is the source of truth.
606
+ *
603
607
  * @param className - The model class name (e.g. "Recipe")
604
608
  * @param queryJson - Structured query as JSON string
605
- * @param shapeJson - Optional shape metadata JSON from the model class
606
609
  * @returns Object with `instances` array and `totalCount`
607
610
  */
608
- async modelQuery(className: string, queryJson: string, shapeJson?: string): Promise<{ instances: any[], totalCount: number }> {
609
- return await this.#client.modelQuery(this.#handle.uuid, className, queryJson, shapeJson);
611
+ async modelQuery(className: string, queryJson: string): Promise<{ instances: any[], totalCount: number }> {
612
+ return await this.#client.modelQuery(this.#handle.uuid, className, queryJson);
610
613
  }
611
614
 
612
615
  /**
@@ -618,18 +621,16 @@ export class PerspectiveProxy {
618
621
  *
619
622
  * @param className - The model class name (e.g. "Message")
620
623
  * @param instanceIds - Array of instance base expression URIs
621
- * @param shapeJson - Shape metadata JSON from the model class
622
624
  * @param propertyNames - Optional subset of property names to evaluate
623
625
  * @returns Map of instance ID → evaluated property values
624
626
  */
625
627
  async evaluateGetters(
626
628
  className: string,
627
629
  instanceIds: string[],
628
- shapeJson: string,
629
630
  propertyNames?: string[],
630
631
  ): Promise<Record<string, Record<string, any>>> {
631
632
  return await this.#client.evaluateGetters(
632
- this.#handle.uuid, className, instanceIds, shapeJson, propertyNames,
633
+ this.#handle.uuid, className, instanceIds, propertyNames,
633
634
  );
634
635
  }
635
636
 
@@ -643,11 +644,10 @@ export class PerspectiveProxy {
643
644
  *
644
645
  * @param className - The model class name
645
646
  * @param queryJson - JSON-serialized query parameters (same as modelQuery)
646
- * @param shapeJson - Optional shape metadata JSON from the model class
647
647
  * @returns Object with `subscriptionId` and initial `result`
648
648
  */
649
- async modelSubscribe(className: string, queryJson: string, shapeJson?: string): Promise<{ subscriptionId: string, result: any }> {
650
- return await this.#client.modelSubscribe(this.#handle.uuid, className, queryJson, shapeJson);
649
+ async modelSubscribe(className: string, queryJson: string): Promise<{ subscriptionId: string, result: any }> {
650
+ return await this.#client.modelSubscribe(this.#handle.uuid, className, queryJson);
651
651
  }
652
652
 
653
653
  /**
@@ -846,23 +846,31 @@ export class PerspectiveProxy {
846
846
  async removeListener(type: PerspectiveListenerTypes, cb: LinkCallback) {
847
847
  if (type === 'link-added') {
848
848
  const index = this.#perspectiveLinkAddedCallbacks.indexOf(cb);
849
-
850
- this.#perspectiveLinkAddedCallbacks.splice(index, 1);
849
+ if (index >= 0) this.#perspectiveLinkAddedCallbacks.splice(index, 1);
851
850
  } else if (type === 'link-removed') {
852
851
  const index = this.#perspectiveLinkRemovedCallbacks.indexOf(cb);
853
-
854
- this.#perspectiveLinkRemovedCallbacks.splice(index, 1);
852
+ if (index >= 0) this.#perspectiveLinkRemovedCallbacks.splice(index, 1);
855
853
  } else if (type === 'link-updated') {
856
854
  const index = this.#perspectiveLinkUpdatedCallbacks.indexOf(cb);
857
-
858
- this.#perspectiveLinkUpdatedCallbacks.splice(index, 1);
855
+ if (index >= 0) this.#perspectiveLinkUpdatedCallbacks.splice(index, 1);
859
856
  }
860
857
  }
861
858
 
859
+ /** Clean up all subscriptions registered by this proxy.
860
+ * Call this when the proxy is no longer needed to prevent subscription leaks.
861
+ * After calling dispose(), the proxy should not be used. */
862
+ dispose(): void {
863
+ this.#client.removeAllListeners(this.#handle.uuid)
864
+ this.#perspectiveLinkAddedCallbacks.length = 0
865
+ this.#perspectiveLinkRemovedCallbacks.length = 0
866
+ this.#perspectiveLinkUpdatedCallbacks.length = 0
867
+ this.#perspectiveSyncStateChangeCallbacks.length = 0
868
+ }
869
+
862
870
  /**
863
871
  * Creates a snapshot of the current perspective state.
864
872
  * Useful for backup or sharing.
865
- *
873
+ *
866
874
  * @returns Perspective object containing all links
867
875
  */
868
876
  async snapshot(): Promise<Perspective> {
@@ -1162,6 +1170,43 @@ export class PerspectiveProxy {
1162
1170
  return this.#client.addSdna(this.#handle.uuid, name, sdnaCode, sdnaType, shaclJson)
1163
1171
  }
1164
1172
 
1173
+ /**
1174
+ * Batch variant of addSdna — registers multiple SDNA entries in a single RPC call.
1175
+ * Acquires the Rust-side mutex once for the entire batch.
1176
+ */
1177
+ async addSdnaAll(entries: { name: string; sdnaCode?: string; sdnaType: "subject_class" | "flow" | "custom"; shaclJson?: string }[]): Promise<boolean[]> {
1178
+ return this.#client.addSdnaBatch(this.#handle.uuid, entries)
1179
+ }
1180
+
1181
+ /**
1182
+ * Batch-registers multiple model classes as SHACL subject classes in a single RPC call.
1183
+ * Skips classes already registered on this perspective instance.
1184
+ */
1185
+ async ensureSubjectClasses(jsClasses: any[]): Promise<void> {
1186
+ const entries: { name: string; sdnaCode?: string; sdnaType: "subject_class" | "flow" | "custom"; shaclJson?: string }[] = [];
1187
+ for (const jsClass of jsClasses) {
1188
+ const className = jsClass.className || jsClass.prototype?.className || jsClass.name;
1189
+ if (this.#ensuredSubjectClasses.has(className)) continue;
1190
+
1191
+ if (!jsClass.generateSHACL) {
1192
+ throw new Error(`Class ${jsClass.name} must have generateSHACL(). Use @Model decorator.`);
1193
+ }
1194
+
1195
+ const { shape } = jsClass.generateSHACL();
1196
+ entries.push({
1197
+ name: className,
1198
+ sdnaType: 'subject_class',
1199
+ shaclJson: JSON.stringify(shape.toJSON()),
1200
+ });
1201
+ }
1202
+ if (entries.length === 0) return;
1203
+
1204
+ await this.addSdnaAll(entries);
1205
+ for (const entry of entries) {
1206
+ this.#ensuredSubjectClasses.add(entry.name);
1207
+ }
1208
+ }
1209
+
1165
1210
  /**
1166
1211
  * Adds a subject class to the perspective.
1167
1212
  * Alias for addSdna() with sdnaType='subject_class'.
@@ -0,0 +1,79 @@
1
+ /**
2
+ * SPARQL binding helpers — typed shapes and parsers for `querySparql<T>()`.
3
+ *
4
+ * AD4M stores non-string property values as URL-encoded `Literal` payloads
5
+ * (see {@link Literal}). When raw SPARQL queries pull these values out of the
6
+ * RDF store the bindings are still in encoded form; consumers must decode
7
+ * before use. The helpers here centralise that decode step so each call site
8
+ * can pick the variant matching its declared binding type.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * interface MessageBinding {
13
+ * id: string;
14
+ * timestamp: string; // raw literal — decode with parseLitNumber
15
+ * body?: string; // raw literal — decode with parseLit
16
+ * }
17
+ * const rows = await perspective.querySparql<MessageBinding[]>(query);
18
+ * const ts = parseLitNumber(rows[0].timestamp); // number
19
+ * const body = rows[0].body ? parseLit(rows[0].body) : '';
20
+ * ```
21
+ */
22
+
23
+ import { Literal } from "../Literal";
24
+
25
+ /** Common AD4M link binding — matches the `?source/?predicate/?target/?author/?timestamp` pattern. */
26
+ export interface LinkBinding {
27
+ source: string;
28
+ predicate: string;
29
+ target: string;
30
+ author?: string;
31
+ timestamp?: string;
32
+ }
33
+
34
+ /** COUNT(?x) result binding — SPARQL returns counts as string literals. */
35
+ export interface CountBinding {
36
+ count: string;
37
+ }
38
+
39
+ /** Sum a SPARQL COUNT(?x) result down to a plain number (returns 0 on empty). */
40
+ export function parseSparqlCount(result: CountBinding[] | undefined | null): number {
41
+ if (!result || result.length === 0) return 0;
42
+ const raw = result[0].count;
43
+ const n = parseInt(raw, 10);
44
+ return Number.isNaN(n) ? 0 : n;
45
+ }
46
+
47
+ /**
48
+ * Decode a `Literal`-encoded SPARQL binding back to a plain string.
49
+ *
50
+ * Mirrors Flux's local `parseLit` helper. Returns `''` for `undefined`/empty
51
+ * input. Falls through to the raw value if decoding fails (defensive — the
52
+ * binding may already be a plain string for unwrapped properties).
53
+ */
54
+ export function parseLit(val: string | undefined | null): string {
55
+ if (val === undefined || val === null || val === '') return '';
56
+ try {
57
+ const result = Literal.fromUrl(val).get();
58
+ if (result && typeof result === 'object') {
59
+ return (result as { data?: string }).data ?? JSON.stringify(result);
60
+ }
61
+ return String(result);
62
+ } catch {
63
+ return val;
64
+ }
65
+ }
66
+
67
+ /** Decode a `Literal`-encoded binding to a number. Returns 0 on empty/invalid input. */
68
+ export function parseLitNumber(val: string | undefined | null): number {
69
+ if (val === undefined || val === null || val === '') return 0;
70
+ const decoded = parseLit(val);
71
+ const n = Number(decoded);
72
+ return Number.isNaN(n) ? 0 : n;
73
+ }
74
+
75
+ /** Decode a `Literal`-encoded binding to a boolean. Returns false on empty input. */
76
+ export function parseLitBoolean(val: string | undefined | null): boolean {
77
+ if (val === undefined || val === null || val === '') return false;
78
+ return parseLit(val) === 'true';
79
+ }
@@ -173,10 +173,11 @@ export class WakerSubscriptionManager {
173
173
  const parentQuery = `SELECT ?source WHERE { ?source <ad4m://has_child> <${msgAddr}> . }`;
174
174
  this.logger.info(`[waker] ${sub.id}: resolving parents for ${msgAddr}`);
175
175
  const parentResult = await this.perspectiveClient.querySparql(sub.perspective, parentQuery);
176
- if (Array.isArray(parentResult)) {
177
- for (const link of parentResult) {
178
- if (link && link.source) {
179
- parents.push(link.source);
176
+ const bindings = parentResult?.results?.bindings;
177
+ if (Array.isArray(bindings)) {
178
+ for (const binding of bindings) {
179
+ if (binding?.source?.value) {
180
+ parents.push(binding.source.value);
180
181
  }
181
182
  }
182
183
  }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * SHACL-AF Node Expressions for property transforms.
3
+ *
4
+ * A `NodeExpression` is a JSON-serializable tree that describes how to transform
5
+ * a resolved property value. Unlike JS lambdas, these are portable, inspectable,
6
+ * and can be executed in the Rust model query engine.
7
+ *
8
+ * Each expression is a tagged union that evaluates to a `Value` (JSON-compatible).
9
+ */
10
+
11
+ /**
12
+ * Union type representing all supported transform expressions.
13
+ * Each variant maps to a SHACL-AF §6 expression construct.
14
+ */
15
+ export type NodeExpression =
16
+ | { type: 'focus' }
17
+ | { type: 'literal'; value: string | number | boolean }
18
+ | { type: 'path'; predicate: string }
19
+ | { type: 'exists'; expr: NodeExpression }
20
+ | { type: 'if'; cond: NodeExpression; then: NodeExpression; else?: NodeExpression }
21
+ | { type: 'concat'; args: NodeExpression[] }
22
+ | { type: 'coalesce'; args: NodeExpression[] }
23
+ | { type: 'function'; iri: string; args: NodeExpression[] };
24
+
25
+ /**
26
+ * Type guard to check if a value is a valid NodeExpression.
27
+ */
28
+ export function isNodeExpression(v: unknown): v is NodeExpression {
29
+ if (typeof v !== 'object' || v === null) return false;
30
+ const obj = v as any;
31
+ const type = obj.type;
32
+
33
+ switch (type) {
34
+ case 'focus':
35
+ return typeof obj.type === 'string';
36
+ case 'literal':
37
+ return typeof obj.value === 'string' || typeof obj.value === 'number' || typeof obj.value === 'boolean';
38
+ case 'path':
39
+ return typeof obj.predicate === 'string';
40
+ case 'exists':
41
+ return isNodeExpression(obj.expr);
42
+ case 'if':
43
+ return isNodeExpression(obj.cond) && isNodeExpression(obj.then) &&
44
+ (obj.else === undefined || isNodeExpression(obj.else));
45
+ case 'concat':
46
+ return Array.isArray(obj.args) && obj.args.every(isNodeExpression);
47
+ case 'coalesce':
48
+ return Array.isArray(obj.args) && obj.args.every(isNodeExpression);
49
+ case 'function':
50
+ return typeof obj.iri === 'string' && Array.isArray(obj.args) && obj.args.every(isNodeExpression);
51
+ default:
52
+ return false;
53
+ }
54
+ }
@@ -1,4 +1,5 @@
1
1
  import { SHACLShape, SHACLPropertyShape, AD4MAction } from './SHACLShape';
2
+ import { concat, literal, focus } from './builders';
2
3
 
3
4
  describe('SHACLShape', () => {
4
5
  describe('toLinks()', () => {
@@ -491,4 +492,175 @@ describe('SHACLShape', () => {
491
492
  expect(reconstructed.properties[0].in).toEqual(original.properties[0].in);
492
493
  });
493
494
  });
495
+
496
+ describe('runtime metadata predicates (SHACL as source of truth)', () => {
497
+ it('emits and parses relationKind', () => {
498
+ const shape = new SHACLShape('ns://Recipe');
499
+ shape.addProperty({
500
+ name: 'ingredients',
501
+ path: 'ns://has_ingredient',
502
+ relationKind: 'hasMany',
503
+ });
504
+ const links = shape.toLinks();
505
+ const kindLink = links.find(l => l.predicate === 'ad4m://relationKind');
506
+ expect(kindLink?.target).toBe('literal:string:hasMany');
507
+
508
+ const round = SHACLShape.fromLinks(links, shape.nodeShapeUri);
509
+ expect(round.properties[0].relationKind).toBe('hasMany');
510
+ });
511
+
512
+ it('emits and parses targetClassName', () => {
513
+ const shape = new SHACLShape('ns://Recipe');
514
+ shape.addProperty({
515
+ name: 'ingredients',
516
+ path: 'ns://has_ingredient',
517
+ targetClassName: 'Ingredient',
518
+ });
519
+ const links = shape.toLinks();
520
+ const link = links.find(l => l.predicate === 'ad4m://targetClassName');
521
+ expect(link?.target).toBe('literal:string:Ingredient');
522
+
523
+ const round = SHACLShape.fromLinks(links, shape.nodeShapeUri);
524
+ expect(round.properties[0].targetClassName).toBe('Ingredient');
525
+ });
526
+
527
+ it('emits and parses whereFilter as a JSON literal', () => {
528
+ const shape = new SHACLShape('ns://Board');
529
+ shape.addProperty({
530
+ name: 'tasks',
531
+ path: 'ns://has_task',
532
+ whereFilter: { status: 'active' },
533
+ });
534
+ const links = shape.toLinks();
535
+ const link = links.find(l => l.predicate === 'ad4m://whereFilter');
536
+ expect(link?.target).toContain('"status"');
537
+
538
+ const round = SHACLShape.fromLinks(links, shape.nodeShapeUri);
539
+ expect(round.properties[0].whereFilter).toEqual({ status: 'active' });
540
+ });
541
+
542
+ it('emits and parses wherePredicates', () => {
543
+ const shape = new SHACLShape('ns://Board');
544
+ shape.addProperty({
545
+ name: 'tasks',
546
+ path: 'ns://has_task',
547
+ wherePredicates: { status: 'ns://status' },
548
+ });
549
+ const links = shape.toLinks();
550
+ const link = links.find(l => l.predicate === 'ad4m://wherePredicates');
551
+ expect(link).toBeDefined();
552
+
553
+ const round = SHACLShape.fromLinks(links, shape.nodeShapeUri);
554
+ expect(round.properties[0].wherePredicates).toEqual({ status: 'ns://status' });
555
+ });
556
+
557
+ it('omits wherePredicates link for empty maps', () => {
558
+ const shape = new SHACLShape('ns://Board');
559
+ shape.addProperty({
560
+ name: 'tasks',
561
+ path: 'ns://has_task',
562
+ wherePredicates: {},
563
+ });
564
+ const links = shape.toLinks();
565
+ const link = links.find(l => l.predicate === 'ad4m://wherePredicates');
566
+ expect(link).toBeUndefined();
567
+ });
568
+
569
+ it('emits and parses filter=false as a literal boolean', () => {
570
+ const shape = new SHACLShape('ns://Doc');
571
+ shape.addProperty({
572
+ name: 'tags',
573
+ path: 'ns://tag',
574
+ filter: false,
575
+ });
576
+ const links = shape.toLinks();
577
+ const link = links.find(l => l.predicate === 'ad4m://filter');
578
+ expect(link?.target).toBe('literal:false');
579
+
580
+ const round = SHACLShape.fromLinks(links, shape.nodeShapeUri);
581
+ expect(round.properties[0].filter).toBe(false);
582
+ });
583
+
584
+ it('round-trips all runtime metadata through toJSON() and fromJSON()', () => {
585
+ const original = new SHACLShape('ns://Board');
586
+ original.addProperty({
587
+ name: 'tasks',
588
+ path: 'ns://has_task',
589
+ relationKind: 'hasMany',
590
+ targetClassName: 'Task',
591
+ whereFilter: { status: 'active' },
592
+ wherePredicates: { status: 'ns://status' },
593
+ filter: false,
594
+ });
595
+ const json = original.toJSON();
596
+ const reconstructed = SHACLShape.fromJSON(json);
597
+ const prop = reconstructed.properties[0];
598
+ expect(prop.relationKind).toBe('hasMany');
599
+ expect(prop.targetClassName).toBe('Task');
600
+ expect(prop.whereFilter).toEqual({ status: 'active' });
601
+ expect(prop.wherePredicates).toEqual({ status: 'ns://status' });
602
+ expect(prop.filter).toBe(false);
603
+ });
604
+ });
605
+
606
+ describe('transform property emission', () => {
607
+ it('emits an ad4m://transform link when transform is a NodeExpression', () => {
608
+ const shape = new SHACLShape('ns://ImagePost');
609
+ shape.addProperty({
610
+ name: 'image',
611
+ path: 'image://data',
612
+ datatype: 'xsd://string',
613
+ resolveLanguage: '',
614
+ transform: concat(literal('data:image/png;base64,'), focus()),
615
+ });
616
+
617
+ const links = shape.toLinks();
618
+ const transformLinks = links.filter(l => l.predicate === 'ad4m://transform');
619
+ expect(transformLinks).toHaveLength(1);
620
+
621
+ // Payload must be a SHACL-serialized NodeExpression, never raw JS.
622
+ const target = transformLinks[0].target;
623
+ expect(target.startsWith('literal:string:')).toBe(true);
624
+ const json = target.replace(/^literal:string:/, '');
625
+ expect(() => JSON.parse(json)).not.toThrow();
626
+ expect(JSON.parse(json)).toEqual({
627
+ type: 'concat',
628
+ args: [
629
+ { type: 'literal', value: 'data:image/png;base64,' },
630
+ { type: 'focus' },
631
+ ],
632
+ });
633
+ });
634
+
635
+ // Regression guard for the `typeof prop.transform === 'object'` clause
636
+ // added alongside the SHACL-source-of-truth round-trip fix. Legacy model
637
+ // decorators still declare JS-function transforms (e.g. the pre-DSL
638
+ // `transform: (data) => ...` form). Those cannot be represented as a
639
+ // SHACL-AF Node Expression and must be silently dropped during link
640
+ // emission — never coerced through `JSON.stringify`, which would write a
641
+ // bogus `literal:string:undefined` triple and corrupt the store.
642
+ it('silently drops function-typed transforms (no ad4m://transform link emitted)', () => {
643
+ const shape = new SHACLShape('ns://LegacyModel');
644
+ shape.addProperty({
645
+ name: 'image',
646
+ path: 'ns://image',
647
+ datatype: 'xsd://string',
648
+ // Legacy JS-function form, still found in pre-migration model classes.
649
+ // Cast through `any` because the public type only permits NodeExpression.
650
+ transform: ((data: any) => `data:image/png;base64,${data}`) as any,
651
+ });
652
+
653
+ const links = shape.toLinks();
654
+ const transformLinks = links.filter(l => l.predicate === 'ad4m://transform');
655
+ expect(transformLinks).toHaveLength(0);
656
+
657
+ // Defensive: nothing in the emitted link set should carry the string
658
+ // "undefined" or a serialized function — that would indicate an
659
+ // accidental JSON.stringify of the function value.
660
+ for (const link of links) {
661
+ expect(link.target).not.toMatch(/^literal:string:undefined$/);
662
+ expect(link.target).not.toMatch(/^literal:string:.*function/);
663
+ }
664
+ });
665
+ });
494
666
  });