@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.
Files changed (279) hide show
  1. package/lib/index.cjs +2409 -625
  2. package/lib/index.js +2406 -626
  3. package/lib/src/Literal.js +8 -0
  4. package/lib/src/ai/AIClient.js +6 -3
  5. package/lib/src/apiClient.d.ts +40 -4
  6. package/lib/src/apiClient.js +149 -9
  7. package/lib/src/generated/api/RunInterpretationRequest.d.ts +16 -0
  8. package/lib/src/index.d.ts +3 -1
  9. package/lib/src/index.js +2 -0
  10. package/lib/src/model/Ad4mModel.d.ts +34 -16
  11. package/lib/src/model/Ad4mModel.js +302 -63
  12. package/lib/src/model/Ad4mModel.test.js +480 -735
  13. package/lib/src/model/ModelQueryBuilder.js +2 -2
  14. package/lib/src/model/decorators.d.ts +199 -12
  15. package/lib/src/model/decorators.js +88 -32
  16. package/lib/src/model/json-schema.js +7 -21
  17. package/lib/src/model/model-manifest.d.ts +41 -0
  18. package/lib/src/model/model-manifest.js +11 -0
  19. package/lib/src/model/query-common.d.ts +2 -2
  20. package/lib/src/model/query-sparql.d.ts +62 -0
  21. package/lib/src/model/query-sparql.js +78 -15
  22. package/lib/src/model/query-sparql.test.js +92 -10
  23. package/lib/src/model/query-utils.d.ts +7 -2
  24. package/lib/src/model/query-utils.js +21 -11
  25. package/lib/src/model/shacl-gen.js +77 -15
  26. package/lib/src/model/types.d.ts +32 -8
  27. package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +33 -33
  28. package/lib/src/perspectives/AutoProcessor.d.ts +198 -2
  29. package/lib/src/perspectives/FlowInstance.d.ts +187 -0
  30. package/lib/src/perspectives/FlowInstance.js +336 -0
  31. package/lib/src/perspectives/FlowModels.d.ts +94 -0
  32. package/lib/src/perspectives/FlowModels.js +182 -0
  33. package/lib/src/perspectives/InterpretationModels.d.ts +41 -3
  34. package/lib/src/perspectives/InterpretationModels.js +60 -26
  35. package/lib/src/perspectives/PerspectiveClient.d.ts +88 -7
  36. package/lib/src/perspectives/PerspectiveClient.js +138 -11
  37. package/lib/src/perspectives/PerspectiveProxy.d.ts +150 -57
  38. package/lib/src/perspectives/PerspectiveProxy.js +411 -287
  39. package/lib/src/perspectives/SparqlBindings.d.ts +24 -3
  40. package/lib/src/perspectives/SparqlBindings.js +57 -6
  41. package/lib/src/shacl/SHACLFlow.d.ts +273 -47
  42. package/lib/src/shacl/SHACLFlow.js +397 -88
  43. package/lib/src/shacl/SHACLShape.d.ts +19 -3
  44. package/lib/src/shacl/SHACLShape.js +30 -1
  45. package/lib/src/shacl/SHACLShape.test.js +82 -0
  46. package/lib/src/shacl/index.d.ts +1 -1
  47. package/lib/src/shacl/index.js +1 -1
  48. package/package.json +3 -3
  49. package/src/Literal.ts +7 -0
  50. package/src/ai/AIClient.ts +6 -3
  51. package/src/apiClient.test.ts +227 -0
  52. package/src/apiClient.ts +185 -9
  53. package/src/generated/api/RunInterpretationRequest.ts +19 -3
  54. package/src/index.ts +3 -1
  55. package/src/model/Ad4mModel.test.ts +729 -22
  56. package/src/model/Ad4mModel.ts +305 -66
  57. package/src/model/ModelQueryBuilder.ts +2 -2
  58. package/src/model/decorators.ts +278 -44
  59. package/src/model/json-schema.ts +10 -25
  60. package/src/model/query-common.ts +2 -2
  61. package/src/model/query-sparql.test.ts +114 -13
  62. package/src/model/query-sparql.ts +83 -14
  63. package/src/model/query-utils.ts +22 -12
  64. package/src/model/relation-filtering.test.ts +70 -20
  65. package/src/model/shacl-gen.ts +78 -13
  66. package/src/model/shacl-roundtrip.test.ts +134 -2
  67. package/src/model/types.ts +30 -8
  68. package/src/perspectives/AutoProcessor.ts +204 -2
  69. package/src/perspectives/FlowInstance.ts +369 -0
  70. package/src/perspectives/FlowModels.ts +157 -0
  71. package/src/perspectives/InterpretationModels.ts +91 -21
  72. package/src/perspectives/PerspectiveClient.test.ts +67 -6
  73. package/src/perspectives/PerspectiveClient.ts +190 -14
  74. package/src/perspectives/PerspectiveProxy.test.ts +406 -0
  75. package/src/perspectives/PerspectiveProxy.ts +494 -308
  76. package/src/perspectives/SparqlBindings.test.ts +134 -0
  77. package/src/perspectives/SparqlBindings.ts +57 -6
  78. package/src/perspectives/shacl-rpc.test.ts +258 -0
  79. package/src/shacl/SHACLFlow.test.ts +762 -35
  80. package/src/shacl/SHACLFlow.ts +680 -112
  81. package/src/shacl/SHACLShape.test.ts +30 -4
  82. package/src/shacl/SHACLShape.ts +59 -6
  83. package/src/shacl/index.ts +1 -1
  84. package/lib/src/generated/rest/AIModelLoadingStatus.d.ts +0 -7
  85. package/lib/src/generated/rest/AIModelLoadingStatus.js +0 -2
  86. package/lib/src/generated/rest/AddAgentInfosRequest.d.ts +0 -3
  87. package/lib/src/generated/rest/AddAgentInfosRequest.js +0 -2
  88. package/lib/src/generated/rest/AddLinkExpressionRequest.d.ts +0 -6
  89. package/lib/src/generated/rest/AddLinkExpressionRequest.js +0 -1
  90. package/lib/src/generated/rest/AddLinkRequest.d.ts +0 -6
  91. package/lib/src/generated/rest/AddLinkRequest.js +0 -1
  92. package/lib/src/generated/rest/AddLinksBulkRequest.d.ts +0 -6
  93. package/lib/src/generated/rest/AddLinksBulkRequest.js +0 -1
  94. package/lib/src/generated/rest/AddSdnaRequest.d.ts +0 -6
  95. package/lib/src/generated/rest/AddSdnaRequest.js +0 -2
  96. package/lib/src/generated/rest/ApplyTemplateRequest.d.ts +0 -4
  97. package/lib/src/generated/rest/ApplyTemplateRequest.js +0 -2
  98. package/lib/src/generated/rest/AuthInfoInput.d.ts +0 -10
  99. package/lib/src/generated/rest/AuthInfoInput.js +0 -1
  100. package/lib/src/generated/rest/BroadcastRequest.d.ts +0 -6
  101. package/lib/src/generated/rest/BroadcastRequest.js +0 -1
  102. package/lib/src/generated/rest/CapabilityInput.d.ts +0 -5
  103. package/lib/src/generated/rest/CapabilityInput.js +0 -1
  104. package/lib/src/generated/rest/CommitBatchRequest.d.ts +0 -3
  105. package/lib/src/generated/rest/CommitBatchRequest.js +0 -2
  106. package/lib/src/generated/rest/ComputeLogEntry.d.ts +0 -9
  107. package/lib/src/generated/rest/ComputeLogEntry.js +0 -2
  108. package/lib/src/generated/rest/CreateExpressionRequest.d.ts +0 -4
  109. package/lib/src/generated/rest/CreateExpressionRequest.js +0 -2
  110. package/lib/src/generated/rest/CreatePerspectiveRequest.d.ts +0 -3
  111. package/lib/src/generated/rest/CreatePerspectiveRequest.js +0 -2
  112. package/lib/src/generated/rest/CreateSubjectRequest.d.ts +0 -6
  113. package/lib/src/generated/rest/CreateSubjectRequest.js +0 -2
  114. package/lib/src/generated/rest/CreateUserRequest.d.ts +0 -4
  115. package/lib/src/generated/rest/CreateUserRequest.js +0 -2
  116. package/lib/src/generated/rest/CreateUserResponse.d.ts +0 -5
  117. package/lib/src/generated/rest/CreateUserResponse.js +0 -2
  118. package/lib/src/generated/rest/DecoratedExpressionProof.d.ts +0 -6
  119. package/lib/src/generated/rest/DecoratedExpressionProof.js +0 -2
  120. package/lib/src/generated/rest/DecoratedLinkExpression.d.ts +0 -10
  121. package/lib/src/generated/rest/DecoratedLinkExpression.js +0 -1
  122. package/lib/src/generated/rest/DisposeQueryRequest.d.ts +0 -3
  123. package/lib/src/generated/rest/DisposeQueryRequest.js +0 -2
  124. package/lib/src/generated/rest/EmailTestRequest.d.ts +0 -6
  125. package/lib/src/generated/rest/EmailTestRequest.js +0 -2
  126. package/lib/src/generated/rest/EmbedRequest.d.ts +0 -4
  127. package/lib/src/generated/rest/EmbedRequest.js +0 -2
  128. package/lib/src/generated/rest/EntanglementProofInput.d.ts +0 -6
  129. package/lib/src/generated/rest/EntanglementProofInput.js +0 -2
  130. package/lib/src/generated/rest/EntanglementProofPreflightRequest.d.ts +0 -4
  131. package/lib/src/generated/rest/EntanglementProofPreflightRequest.js +0 -2
  132. package/lib/src/generated/rest/ExecuteCommandsRequest.d.ts +0 -4
  133. package/lib/src/generated/rest/ExecuteCommandsRequest.js +0 -2
  134. package/lib/src/generated/rest/ExportRequest.d.ts +0 -5
  135. package/lib/src/generated/rest/ExportRequest.js +0 -2
  136. package/lib/src/generated/rest/ExpressionManyRequest.d.ts +0 -3
  137. package/lib/src/generated/rest/ExpressionManyRequest.js +0 -2
  138. package/lib/src/generated/rest/ExpressionProof.d.ts +0 -4
  139. package/lib/src/generated/rest/ExpressionProof.js +0 -2
  140. package/lib/src/generated/rest/ExpressionProofInput.d.ts +0 -6
  141. package/lib/src/generated/rest/ExpressionProofInput.js +0 -2
  142. package/lib/src/generated/rest/FriendSendMessageRequest.d.ts +0 -3
  143. package/lib/src/generated/rest/FriendSendMessageRequest.js +0 -2
  144. package/lib/src/generated/rest/FriendsListRequest.d.ts +0 -3
  145. package/lib/src/generated/rest/FriendsListRequest.js +0 -2
  146. package/lib/src/generated/rest/GenerateAgentRequest.d.ts +0 -3
  147. package/lib/src/generated/rest/GenerateAgentRequest.js +0 -2
  148. package/lib/src/generated/rest/GenerateJwtRequest.d.ts +0 -4
  149. package/lib/src/generated/rest/GenerateJwtRequest.js +0 -2
  150. package/lib/src/generated/rest/GetSubjectDataRequest.d.ts +0 -4
  151. package/lib/src/generated/rest/GetSubjectDataRequest.js +0 -2
  152. package/lib/src/generated/rest/HostingInfoResponse.d.ts +0 -5
  153. package/lib/src/generated/rest/HostingInfoResponse.js +0 -2
  154. package/lib/src/generated/rest/HostingWalletResponse.d.ts +0 -4
  155. package/lib/src/generated/rest/HostingWalletResponse.js +0 -2
  156. package/lib/src/generated/rest/ImportAgentRequest.d.ts +0 -6
  157. package/lib/src/generated/rest/ImportAgentRequest.js +0 -2
  158. package/lib/src/generated/rest/ImportRequest.d.ts +0 -4
  159. package/lib/src/generated/rest/ImportRequest.js +0 -2
  160. package/lib/src/generated/rest/JoinNeighbourhoodRequest.d.ts +0 -3
  161. package/lib/src/generated/rest/JoinNeighbourhoodRequest.js +0 -2
  162. package/lib/src/generated/rest/KeepAliveQueryRequest.d.ts +0 -3
  163. package/lib/src/generated/rest/KeepAliveQueryRequest.js +0 -2
  164. package/lib/src/generated/rest/LanguageMetaInput.d.ts +0 -6
  165. package/lib/src/generated/rest/LanguageMetaInput.js +0 -2
  166. package/lib/src/generated/rest/Link.d.ts +0 -5
  167. package/lib/src/generated/rest/Link.js +0 -2
  168. package/lib/src/generated/rest/LinkExpression.d.ts +0 -10
  169. package/lib/src/generated/rest/LinkExpression.js +0 -1
  170. package/lib/src/generated/rest/LinkExpressionInput.d.ts +0 -10
  171. package/lib/src/generated/rest/LinkExpressionInput.js +0 -1
  172. package/lib/src/generated/rest/LinkInput.d.ts +0 -5
  173. package/lib/src/generated/rest/LinkInput.js +0 -2
  174. package/lib/src/generated/rest/LinkLanguageTemplatesRequest.d.ts +0 -3
  175. package/lib/src/generated/rest/LinkLanguageTemplatesRequest.js +0 -2
  176. package/lib/src/generated/rest/LinkMutationRequest.d.ts +0 -12
  177. package/lib/src/generated/rest/LinkMutationRequest.js +0 -1
  178. package/lib/src/generated/rest/LinkMutationResponse.d.ts +0 -9
  179. package/lib/src/generated/rest/LinkMutationResponse.js +0 -1
  180. package/lib/src/generated/rest/LinkMutations.d.ts +0 -6
  181. package/lib/src/generated/rest/LinkMutations.js +0 -1
  182. package/lib/src/generated/rest/LinkMutationsRequest.d.ts +0 -5
  183. package/lib/src/generated/rest/LinkMutationsRequest.js +0 -1
  184. package/lib/src/generated/rest/LinkStatus.d.ts +0 -1
  185. package/lib/src/generated/rest/LinkStatus.js +0 -2
  186. package/lib/src/generated/rest/LinkUpdateInput.d.ts +0 -5
  187. package/lib/src/generated/rest/LinkUpdateInput.js +0 -1
  188. package/lib/src/generated/rest/LockAgentRequest.d.ts +0 -3
  189. package/lib/src/generated/rest/LockAgentRequest.js +0 -2
  190. package/lib/src/generated/rest/LoginUserRequest.d.ts +0 -5
  191. package/lib/src/generated/rest/LoginUserRequest.js +0 -2
  192. package/lib/src/generated/rest/ModelType.d.ts +0 -1
  193. package/lib/src/generated/rest/ModelType.js +0 -2
  194. package/lib/src/generated/rest/NotificationGrantRequest.d.ts +0 -3
  195. package/lib/src/generated/rest/NotificationGrantRequest.js +0 -2
  196. package/lib/src/generated/rest/NotificationInput.d.ts +0 -10
  197. package/lib/src/generated/rest/NotificationInput.js +0 -2
  198. package/lib/src/generated/rest/OpenLinkRequest.d.ts +0 -3
  199. package/lib/src/generated/rest/OpenLinkRequest.js +0 -2
  200. package/lib/src/generated/rest/PermitCapabilityRequest.d.ts +0 -3
  201. package/lib/src/generated/rest/PermitCapabilityRequest.js +0 -2
  202. package/lib/src/generated/rest/PerspectiveInput.d.ts +0 -4
  203. package/lib/src/generated/rest/PerspectiveInput.js +0 -1
  204. package/lib/src/generated/rest/PromptRequest.d.ts +0 -4
  205. package/lib/src/generated/rest/PromptRequest.js +0 -2
  206. package/lib/src/generated/rest/PublicPerspectiveInput.d.ts +0 -4
  207. package/lib/src/generated/rest/PublicPerspectiveInput.js +0 -1
  208. package/lib/src/generated/rest/PublishLanguageRequest.d.ts +0 -5
  209. package/lib/src/generated/rest/PublishLanguageRequest.js +0 -1
  210. package/lib/src/generated/rest/PublishNeighbourhoodRequest.d.ts +0 -6
  211. package/lib/src/generated/rest/PublishNeighbourhoodRequest.js +0 -1
  212. package/lib/src/generated/rest/QueryRequest.d.ts +0 -7
  213. package/lib/src/generated/rest/QueryRequest.js +0 -2
  214. package/lib/src/generated/rest/RemoveLinkRequest.d.ts +0 -5
  215. package/lib/src/generated/rest/RemoveLinkRequest.js +0 -1
  216. package/lib/src/generated/rest/RemoveLinksBulkRequest.d.ts +0 -5
  217. package/lib/src/generated/rest/RemoveLinksBulkRequest.js +0 -1
  218. package/lib/src/generated/rest/RequestCapabilityRequest.d.ts +0 -4
  219. package/lib/src/generated/rest/RequestCapabilityRequest.js +0 -1
  220. package/lib/src/generated/rest/RequestPaymentRequest.d.ts +0 -3
  221. package/lib/src/generated/rest/RequestPaymentRequest.js +0 -2
  222. package/lib/src/generated/rest/RequestVerificationRequest.d.ts +0 -4
  223. package/lib/src/generated/rest/RequestVerificationRequest.js +0 -2
  224. package/lib/src/generated/rest/ResourceInput.d.ts +0 -4
  225. package/lib/src/generated/rest/ResourceInput.js +0 -2
  226. package/lib/src/generated/rest/SetDefaultModelRequest.d.ts +0 -4
  227. package/lib/src/generated/rest/SetDefaultModelRequest.js +0 -1
  228. package/lib/src/generated/rest/SetFreeHostingEnabledRequest.d.ts +0 -3
  229. package/lib/src/generated/rest/SetFreeHostingEnabledRequest.js +0 -2
  230. package/lib/src/generated/rest/SetHotWalletAddressRequest.d.ts +0 -3
  231. package/lib/src/generated/rest/SetHotWalletAddressRequest.js +0 -2
  232. package/lib/src/generated/rest/SetMultiUserRequest.d.ts +0 -3
  233. package/lib/src/generated/rest/SetMultiUserRequest.js +0 -2
  234. package/lib/src/generated/rest/SetOnlineStatusRequest.d.ts +0 -5
  235. package/lib/src/generated/rest/SetOnlineStatusRequest.js +0 -1
  236. package/lib/src/generated/rest/SetStatusRequest.d.ts +0 -3
  237. package/lib/src/generated/rest/SetStatusRequest.js +0 -2
  238. package/lib/src/generated/rest/SetUserFreeAccessRequest.d.ts +0 -4
  239. package/lib/src/generated/rest/SetUserFreeAccessRequest.js +0 -2
  240. package/lib/src/generated/rest/SignMessageRequest.d.ts +0 -3
  241. package/lib/src/generated/rest/SignMessageRequest.js +0 -2
  242. package/lib/src/generated/rest/SignalRequest.d.ts +0 -6
  243. package/lib/src/generated/rest/SignalRequest.js +0 -1
  244. package/lib/src/generated/rest/SubscribeQueryRequest.d.ts +0 -3
  245. package/lib/src/generated/rest/SubscribeQueryRequest.js +0 -2
  246. package/lib/src/generated/rest/SubscribeQueryResponse.d.ts +0 -4
  247. package/lib/src/generated/rest/SubscribeQueryResponse.js +0 -2
  248. package/lib/src/generated/rest/TrustedAgentsRequest.d.ts +0 -3
  249. package/lib/src/generated/rest/TrustedAgentsRequest.js +0 -2
  250. package/lib/src/generated/rest/UnlockAgentRequest.d.ts +0 -4
  251. package/lib/src/generated/rest/UnlockAgentRequest.js +0 -2
  252. package/lib/src/generated/rest/UpdateLinkRequest.d.ts +0 -7
  253. package/lib/src/generated/rest/UpdateLinkRequest.js +0 -1
  254. package/lib/src/generated/rest/UpdatePerspectiveRequest.d.ts +0 -3
  255. package/lib/src/generated/rest/UpdatePerspectiveRequest.js +0 -2
  256. package/lib/src/generated/rest/UpdateProfileRequest.d.ts +0 -5
  257. package/lib/src/generated/rest/UpdateProfileRequest.js +0 -1
  258. package/lib/src/generated/rest/UserStatistics.d.ts +0 -9
  259. package/lib/src/generated/rest/UserStatistics.js +0 -2
  260. package/lib/src/generated/rest/VerificationRequestResult.d.ts +0 -6
  261. package/lib/src/generated/rest/VerificationRequestResult.js +0 -2
  262. package/lib/src/generated/rest/VerifyEmailRequest.d.ts +0 -6
  263. package/lib/src/generated/rest/VerifyEmailRequest.js +0 -2
  264. package/lib/src/generated/rest/VerifySignatureRequest.d.ts +0 -5
  265. package/lib/src/generated/rest/VerifySignatureRequest.js +0 -2
  266. package/lib/src/generated/rest/WriteSettingsRequest.d.ts +0 -3
  267. package/lib/src/generated/rest/WriteSettingsRequest.js +0 -2
  268. package/lib/src/generated/rest/index.d.ts +0 -73
  269. package/lib/src/generated/rest/index.js +0 -8
  270. package/lib/src/generated/rest/routes.d.ts +0 -587
  271. package/lib/src/generated/rest/routes.js +0 -3
  272. package/lib/src/model/query-surreal.d.ts +0 -44
  273. package/lib/src/model/query-surreal.js +0 -249
  274. package/lib/src/model/surreal-utils.d.ts +0 -42
  275. package/lib/src/model/surreal-utils.js +0 -128
  276. package/lib/src/perspectives/PerspectiveProxy.test.d.ts +0 -1
  277. package/lib/src/perspectives/PerspectiveProxy.test.js +0 -34
  278. package/lib/src/restClient.d.ts +0 -52
  279. package/lib/src/restClient.js +0 -227
@@ -0,0 +1,134 @@
1
+ import { Literal } from '../Literal';
2
+ import { parseLit, parseLitNumber, parseLitBoolean, parseSparqlCount } from './SparqlBindings';
3
+
4
+ describe('parseLit', () => {
5
+ it('returns empty string for undefined/null/empty', () => {
6
+ expect(parseLit(undefined)).toBe('');
7
+ expect(parseLit(null)).toBe('');
8
+ expect(parseLit('')).toBe('');
9
+ });
10
+
11
+ it('returns the raw value unchanged when Literal.fromUrl throws (non-literal URI)', () => {
12
+ expect(parseLit('just a string')).toBe('just a string');
13
+ expect(parseLit('http://example.com/resource')).toBe('http://example.com/resource');
14
+ });
15
+
16
+ it('decodes literal:string: primitives', () => {
17
+ expect(parseLit(Literal.from('hello').toUrl())).toBe('hello');
18
+ expect(parseLit(Literal.from('hello world').toUrl())).toBe('hello world');
19
+ });
20
+
21
+ it('decodes literal:number: and literal:boolean: primitives to their string form', () => {
22
+ expect(parseLit(Literal.from(42).toUrl())).toBe('42');
23
+ expect(parseLit(Literal.from(true).toUrl())).toBe('true');
24
+ expect(parseLit(Literal.from(false).toUrl())).toBe('false');
25
+ });
26
+
27
+ // Signed-envelope literals are produced by the `literal` language on write
28
+ // for any property declared with `resolveLanguage: 'literal'` — see
29
+ // rust-executor/src/languages/mod.rs (`expression_create` literal branch,
30
+ // which calls `create_signed_expression` before `literal_encode`).
31
+ // Consumers expect `.data` on read, not the envelope JSON.
32
+ it("extracts `.data` from signed-envelope literals (resolveLanguage: 'literal')", () => {
33
+ const envelope = {
34
+ author: 'did:key:z6MkTest',
35
+ timestamp: '2026-08-19T14:00:00.000Z',
36
+ data: '<p>hello</p>',
37
+ proof: { key: 'did:key:z6MkTest#z6MkTest', signature: 'deadbeef' },
38
+ };
39
+ expect(parseLit(Literal.from(envelope).toUrl())).toBe('<p>hello</p>');
40
+ });
41
+
42
+ it('extracts `.data` when the envelope only carries a `data` field', () => {
43
+ expect(parseLit(Literal.from({ data: 'extracted' }).toUrl())).toBe('extracted');
44
+ });
45
+
46
+ it('falls back to JSON.stringify for objects without a string `.data`', () => {
47
+ expect(parseLit(Literal.from({ foo: 'bar' }).toUrl())).toBe(JSON.stringify({ foo: 'bar' }));
48
+ });
49
+
50
+ it('falls back to JSON.stringify when `.data` is a non-string value', () => {
51
+ const payload = { data: { nested: 'object' } };
52
+ expect(parseLit(Literal.from(payload).toUrl())).toBe(JSON.stringify(payload));
53
+ });
54
+
55
+ // Some storage backends / query paths surface signed envelopes for
56
+ // `resolveLanguage: 'literal'` properties as a bare JSON string rather
57
+ // than a `literal:json:*` URL — the Flux Synergy view hit this when
58
+ // Message.body arrived as `{"author":..,"timestamp":..,"data":"hi","proof":..}`
59
+ // straight out of a SPARQL binding. parseLit must still unwrap `.data`
60
+ // in that shape or the raw envelope leaks into the chat UI.
61
+ it('extracts .data from a plain-JSON signed envelope (no literal: URL wrapper)', () => {
62
+ const envelope = JSON.stringify({
63
+ author: 'did:key:z6MkTest',
64
+ timestamp: '2026-08-19T14:00:00.000Z',
65
+ data: '<p>hello</p>',
66
+ proof: { key: 'did:key:z6MkTest#z6MkTest', signature: 'deadbeef' },
67
+ });
68
+ expect(parseLit(envelope)).toBe('<p>hello</p>');
69
+ });
70
+
71
+ it('leaves plain-JSON objects without a string .data unchanged', () => {
72
+ // Not an envelope — preserve original JSON so downstream code sees
73
+ // exactly what was stored.
74
+ const raw = JSON.stringify({ foo: 'bar' });
75
+ expect(parseLit(raw)).toBe(raw);
76
+ });
77
+
78
+ it('leaves plain text values that happen to start with `{` unchanged', () => {
79
+ expect(parseLit('{ not really json')).toBe('{ not really json');
80
+ });
81
+ });
82
+
83
+ describe('parseLitNumber', () => {
84
+ it('returns 0 for empty input', () => {
85
+ expect(parseLitNumber(undefined)).toBe(0);
86
+ expect(parseLitNumber(null)).toBe(0);
87
+ expect(parseLitNumber('')).toBe(0);
88
+ });
89
+
90
+ it('decodes literal:number: bindings', () => {
91
+ expect(parseLitNumber(Literal.from(42).toUrl())).toBe(42);
92
+ expect(parseLitNumber(Literal.from(3.14).toUrl())).toBe(3.14);
93
+ });
94
+
95
+ it('returns 0 for non-numeric decoded values', () => {
96
+ expect(parseLitNumber(Literal.from('not a number').toUrl())).toBe(0);
97
+ });
98
+ });
99
+
100
+ describe('parseLitBoolean', () => {
101
+ it('returns false for empty input', () => {
102
+ expect(parseLitBoolean(undefined)).toBe(false);
103
+ expect(parseLitBoolean(null)).toBe(false);
104
+ expect(parseLitBoolean('')).toBe(false);
105
+ });
106
+
107
+ it('decodes literal:boolean: bindings', () => {
108
+ expect(parseLitBoolean(Literal.from(true).toUrl())).toBe(true);
109
+ expect(parseLitBoolean(Literal.from(false).toUrl())).toBe(false);
110
+ });
111
+
112
+ it('returns false for any non-"true" decoded value', () => {
113
+ expect(parseLitBoolean(Literal.from('yes').toUrl())).toBe(false);
114
+ expect(parseLitBoolean(Literal.from(1).toUrl())).toBe(false);
115
+ });
116
+ });
117
+
118
+ describe('parseSparqlCount', () => {
119
+ it('returns 0 for undefined/null/empty result arrays', () => {
120
+ expect(parseSparqlCount(undefined)).toBe(0);
121
+ expect(parseSparqlCount(null)).toBe(0);
122
+ expect(parseSparqlCount([])).toBe(0);
123
+ });
124
+
125
+ it('parses the first row `count` value', () => {
126
+ expect(parseSparqlCount([{ count: '5' }])).toBe(5);
127
+ expect(parseSparqlCount([{ count: '0' }])).toBe(0);
128
+ expect(parseSparqlCount([{ count: '123' }, { count: '999' }])).toBe(123);
129
+ });
130
+
131
+ it('returns 0 for non-integer count values', () => {
132
+ expect(parseSparqlCount([{ count: 'not-a-number' }])).toBe(0);
133
+ });
134
+ });
@@ -47,21 +47,72 @@ export function parseSparqlCount(result: CountBinding[] | undefined | null): num
47
47
  /**
48
48
  * Decode a `Literal`-encoded SPARQL binding back to a plain string.
49
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).
50
+ * Returns `''` for `undefined`/empty input. Falls through to the raw value
51
+ * if decoding fails — bindings against properties resolved through a custom
52
+ * language are already raw URIs and should pass through unchanged.
53
+ *
54
+ * Signed-envelope literals
55
+ * ------------------------
56
+ * Properties declared with `resolveLanguage: 'literal'` are written through
57
+ * the built-in `literal` language, which wraps the value in a signed
58
+ * expression envelope (`{ author, timestamp, data, proof }`) and encodes
59
+ * the whole envelope as a `literal:json:` URL — see
60
+ * `rust-executor/src/languages/mod.rs::expression_create` (literal branch).
61
+ * On read, `parseLit` unwraps `.data` from that envelope so callers get the
62
+ * original scalar value instead of the envelope JSON.
63
+ *
64
+ * The Channel V refactor moved most scalar properties to deterministic
65
+ * typed XSD literals which no longer round-trip through this envelope — for
66
+ * those properties the SPARQL binding is already the lexical form and this
67
+ * helper decodes to the primitive. Signed-envelope literals still exist
68
+ * for any property that opts back into `resolveLanguage: 'literal'` (e.g.
69
+ * per-message provenance for chat bodies), which is why the `.data` unwrap
70
+ * is preserved here.
71
+ *
72
+ * Non-envelope JSON objects (`literal:json:` payloads that are not signed
73
+ * envelopes) are JSON-stringified for display.
53
74
  */
54
75
  export function parseLit(val: string | undefined | null): string {
55
76
  if (val === undefined || val === null || val === '') return '';
77
+
78
+ // Fast-path 1: `literal:*` URLs from properties written through the built-in
79
+ // `literal` language. On Channel V typed-XSD-literal properties, these
80
+ // decode straight to a primitive; on `resolveLanguage: 'literal'` properties
81
+ // (per-message provenance in chat/synergy) they decode to a signed-envelope
82
+ // object we unwrap via `.data`.
56
83
  try {
57
84
  const result = Literal.fromUrl(val).get();
58
- if (result && typeof result === 'object') {
59
- return (result as { data?: string }).data ?? JSON.stringify(result);
85
+ if (typeof result === 'object' && result !== null) {
86
+ const data = (result as { data?: unknown }).data;
87
+ if (typeof data === 'string') return data;
88
+ return JSON.stringify(result);
60
89
  }
61
90
  return String(result);
62
91
  } catch {
63
- return val;
92
+ // fall through to plain-JSON envelope detection
64
93
  }
94
+
95
+ // Fast-path 2: envelope stored/returned as a bare JSON string rather than a
96
+ // `literal:json:*` URL. Depending on the storage backend and query path,
97
+ // SPARQL bindings for `resolveLanguage: 'literal'` properties can surface
98
+ // the envelope object directly as its JSON serialisation (Oxigraph typed
99
+ // xsd:string of the JSON, or a Channel V pass-through) instead of the URL
100
+ // form. Detect that shape by parsing the string and unwrapping `.data`
101
+ // when it looks like a signed envelope; anything else passes through
102
+ // unchanged so plain text (e.g. "just a string") is not corrupted.
103
+ if (val.length >= 2 && val.charCodeAt(0) === 0x7b /* '{' */) {
104
+ try {
105
+ const parsed = JSON.parse(val);
106
+ if (parsed && typeof parsed === 'object') {
107
+ const data = (parsed as { data?: unknown }).data;
108
+ if (typeof data === 'string') return data;
109
+ }
110
+ } catch {
111
+ // not JSON — fall through and return the raw value
112
+ }
113
+ }
114
+
115
+ return val;
65
116
  }
66
117
 
67
118
  /** Decode a `Literal`-encoded binding to a number. Returns 0 on empty/invalid input. */
@@ -0,0 +1,258 @@
1
+ import { PerspectiveProxy } from './PerspectiveProxy';
2
+ import { SHACLShape } from '../shacl/SHACLShape';
3
+
4
+ // ── Helpers ──────────────────────────────────────────────────────────────────
5
+
6
+ function createMockClient(overrides: Record<string, jest.Mock> = {}): any {
7
+ return {
8
+ addPerspectiveLinkAddedListener: jest.fn(),
9
+ addPerspectiveLinkRemovedListener: jest.fn(),
10
+ addPerspectiveLinkUpdatedListener: jest.fn(),
11
+ addPerspectiveSyncStateChangeListener: jest.fn(),
12
+ getShaclNames: jest.fn().mockResolvedValue([]),
13
+ // Client contract (post r3897752023): getShaclTargetClass returns
14
+ // `undefined` on "not found", not `null`. Mock the same shape.
15
+ getShaclTargetClass: jest.fn().mockResolvedValue(undefined),
16
+ getShacl: jest.fn().mockResolvedValue(null),
17
+ getAllShacl: jest.fn().mockResolvedValue([]),
18
+ ...overrides,
19
+ };
20
+ }
21
+
22
+ function createProxy(client?: any): PerspectiveProxy {
23
+ const mockClient = client ?? createMockClient();
24
+ return new PerspectiveProxy(
25
+ {
26
+ uuid: 'test-uuid',
27
+ name: 'test',
28
+ owners: [],
29
+ sharedUrl: null,
30
+ neighbourhood: null,
31
+ state: 'Synced',
32
+ } as any,
33
+ mockClient,
34
+ );
35
+ }
36
+
37
+ /** Build the link triples that SHACLShape.fromLinks expects for a minimal shape. */
38
+ function buildShapeLinks(
39
+ shapeUri: string,
40
+ targetClass: string,
41
+ properties: Array<{ name: string; path: string; datatype?: string }>,
42
+ ): Array<{ source: string; predicate: string; target: string }> {
43
+ const links: Array<{ source: string; predicate: string; target: string }> = [
44
+ { source: shapeUri, predicate: 'sh://targetClass', target: targetClass },
45
+ ];
46
+ for (const prop of properties) {
47
+ const propUri = `${shapeUri}.${prop.name}`;
48
+ links.push({ source: shapeUri, predicate: 'sh://property', target: propUri });
49
+ links.push({ source: propUri, predicate: 'sh://path', target: prop.path });
50
+ if (prop.datatype) {
51
+ links.push({ source: propUri, predicate: 'sh://datatype', target: prop.datatype });
52
+ }
53
+ }
54
+ return links;
55
+ }
56
+
57
+ // ── Tests ────────────────────────────────────────────────────────────────────
58
+
59
+ describe('PerspectiveProxy SHACL RPC delegation', () => {
60
+ describe('getShaclNames', () => {
61
+ it('delegates to PerspectiveClient.getShaclNames with the perspective UUID', async () => {
62
+ const client = createMockClient({
63
+ getShaclNames: jest.fn().mockResolvedValue(['Message', 'Channel']),
64
+ });
65
+ const proxy = createProxy(client);
66
+
67
+ const names = await proxy.getShaclNames();
68
+
69
+ expect(client.getShaclNames).toHaveBeenCalledWith('test-uuid');
70
+ expect(names).toEqual(['Message', 'Channel']);
71
+ });
72
+
73
+ it('returns an empty array when no shapes exist', async () => {
74
+ const proxy = createProxy();
75
+ const names = await proxy.getShaclNames();
76
+ expect(names).toEqual([]);
77
+ });
78
+ });
79
+
80
+ describe('getShaclTargetClass', () => {
81
+ it('delegates to PerspectiveClient and returns the target class', async () => {
82
+ const client = createMockClient({
83
+ getShaclTargetClass: jest.fn().mockResolvedValue('flux://Message'),
84
+ });
85
+ const proxy = createProxy(client);
86
+
87
+ const tc = await proxy.getShaclTargetClass('Message');
88
+
89
+ expect(client.getShaclTargetClass).toHaveBeenCalledWith('test-uuid', 'Message');
90
+ expect(tc).toBe('flux://Message');
91
+ });
92
+
93
+ it('returns undefined when the shape does not exist', async () => {
94
+ const client = createMockClient({
95
+ // Post r3897752023 the client itself returns undefined on "not
96
+ // found", so the proxy passthrough must preserve that.
97
+ getShaclTargetClass: jest.fn().mockResolvedValue(undefined),
98
+ });
99
+ const proxy = createProxy(client);
100
+
101
+ const tc = await proxy.getShaclTargetClass('NonExistent');
102
+ expect(tc).toBeUndefined();
103
+ });
104
+ });
105
+
106
+ describe('getShacl', () => {
107
+ it('delegates to PerspectiveClient and reconstructs a SHACLShape', async () => {
108
+ const shapeUri = 'flux://MessageShape';
109
+ const links = buildShapeLinks(shapeUri, 'flux://Message', [
110
+ { name: 'body', path: 'flux://body', datatype: 'xsd:string' },
111
+ { name: 'timestamp', path: 'flux://timestamp', datatype: 'xsd:dateTime' },
112
+ ]);
113
+
114
+ const client = createMockClient({
115
+ getShacl: jest.fn().mockResolvedValue({ shapeUri, links }),
116
+ });
117
+ const proxy = createProxy(client);
118
+
119
+ const shape = await proxy.getShacl('Message');
120
+
121
+ expect(client.getShacl).toHaveBeenCalledWith('test-uuid', 'Message');
122
+ expect(shape).not.toBeNull();
123
+ expect(shape!.nodeShapeUri).toBe(shapeUri);
124
+ expect(shape!.targetClass).toBe('flux://Message');
125
+ expect(shape!.properties.length).toBe(2);
126
+ expect(shape!.properties.map(p => p.name).sort()).toEqual(['body', 'timestamp']);
127
+ });
128
+
129
+ it('returns null when the shape does not exist', async () => {
130
+ const proxy = createProxy();
131
+ const shape = await proxy.getShacl('NonExistent');
132
+ expect(shape).toBeNull();
133
+ });
134
+ });
135
+
136
+ describe('getAllShacl', () => {
137
+ it('delegates to PerspectiveClient and reconstructs all shapes', async () => {
138
+ const msgLinks = buildShapeLinks('flux://MessageShape', 'flux://Message', [
139
+ { name: 'body', path: 'flux://body' },
140
+ ]);
141
+ const chanLinks = buildShapeLinks('flux://ChannelShape', 'flux://Channel', [
142
+ { name: 'name', path: 'flux://name' },
143
+ ]);
144
+
145
+ const client = createMockClient({
146
+ getAllShacl: jest.fn().mockResolvedValue([
147
+ { name: 'Message', shapeUri: 'flux://MessageShape', links: msgLinks },
148
+ { name: 'Channel', shapeUri: 'flux://ChannelShape', links: chanLinks },
149
+ ]),
150
+ });
151
+ const proxy = createProxy(client);
152
+
153
+ const shapes = await proxy.getAllShacl();
154
+
155
+ expect(client.getAllShacl).toHaveBeenCalledWith('test-uuid');
156
+ expect(shapes.length).toBe(2);
157
+ expect(shapes[0].name).toBe('Message');
158
+ expect(shapes[0].shape.targetClass).toBe('flux://Message');
159
+ expect(shapes[1].name).toBe('Channel');
160
+ expect(shapes[1].shape.targetClass).toBe('flux://Channel');
161
+ });
162
+
163
+ it('returns an empty array when no shapes exist', async () => {
164
+ const proxy = createProxy();
165
+ const shapes = await proxy.getAllShacl();
166
+ expect(shapes).toEqual([]);
167
+ });
168
+
169
+ it('passes every entry through when SHACLShape.fromLinks succeeds', async () => {
170
+ // Named for what it actually asserts: `SHACLShape.fromLinks` always
171
+ // returns a shape (even for the empty-links case — an empty shape
172
+ // with no targetClass is still a shape), so the proxy's `s !== null`
173
+ // filter is only reachable if `fromLinks` itself is mocked to return
174
+ // null. The original name ("filters out shapes that fail
175
+ // reconstruction") described a case the production path can't
176
+ // produce; that was dead coverage.
177
+ const client = createMockClient({
178
+ getAllShacl: jest.fn().mockResolvedValue([
179
+ { name: 'Good', shapeUri: 'app://GoodShape', links: buildShapeLinks('app://GoodShape', 'app://Good', [{ name: 'x', path: 'app://x' }]) },
180
+ { name: 'Empty', shapeUri: 'app://EmptyShape', links: [] },
181
+ ]),
182
+ });
183
+ const proxy = createProxy(client);
184
+
185
+ const shapes = await proxy.getAllShacl();
186
+
187
+ expect(shapes.length).toBe(2);
188
+ expect(shapes.map(s => s.name).sort()).toEqual(['Empty', 'Good']);
189
+ });
190
+
191
+ it('drops entries when SHACLShape.fromLinks returns null', async () => {
192
+ // Exercises the actual filter in `PerspectiveProxy.getAllShacl`:
193
+ // `SHACLShape.fromLinks` doesn't currently return null for any
194
+ // input, but the filter is defensive against a future change (or a
195
+ // subclass override), so we pin it by mocking `fromLinks` to
196
+ // simulate that case for one of two entries.
197
+ const goodLinks = buildShapeLinks('app://GoodShape', 'app://Good', [{ name: 'x', path: 'app://x' }]);
198
+ const badLinks = buildShapeLinks('app://BadShape', 'app://Bad', [{ name: 'y', path: 'app://y' }]);
199
+
200
+ const client = createMockClient({
201
+ getAllShacl: jest.fn().mockResolvedValue([
202
+ { name: 'Good', shapeUri: 'app://GoodShape', links: goodLinks },
203
+ { name: 'Bad', shapeUri: 'app://BadShape', links: badLinks },
204
+ ]),
205
+ });
206
+ const proxy = createProxy(client);
207
+
208
+ const fromLinksSpy = jest
209
+ .spyOn(SHACLShape, 'fromLinks')
210
+ .mockImplementation((_links: any, shapeUri: string) => {
211
+ if (shapeUri === 'app://BadShape') return null as unknown as SHACLShape;
212
+ const shape = new SHACLShape(shapeUri, 'app://Good');
213
+ return shape;
214
+ });
215
+
216
+ try {
217
+ const shapes = await proxy.getAllShacl();
218
+ expect(shapes.length).toBe(1);
219
+ expect(shapes[0].name).toBe('Good');
220
+ } finally {
221
+ fromLinksSpy.mockRestore();
222
+ }
223
+ });
224
+ });
225
+ });
226
+
227
+ describe('SHACLShape.fromLinks round-trip with RPC link format', () => {
228
+ it('reconstructs property details from simplified link triples', () => {
229
+ const shapeUri = 'recipe://RecipeShape';
230
+ const links = [
231
+ { source: shapeUri, predicate: 'sh://targetClass', target: 'recipe://Recipe' },
232
+ { source: shapeUri, predicate: 'sh://property', target: `${shapeUri}.title` },
233
+ { source: `${shapeUri}.title`, predicate: 'sh://path', target: 'recipe://title' },
234
+ { source: `${shapeUri}.title`, predicate: 'sh://datatype', target: 'xsd:string' },
235
+ { source: `${shapeUri}.title`, predicate: 'sh://minCount', target: 'literal:number:1' },
236
+ { source: `${shapeUri}.title`, predicate: 'sh://maxCount', target: 'literal:number:1' },
237
+ { source: shapeUri, predicate: 'sh://property', target: `${shapeUri}.servings` },
238
+ { source: `${shapeUri}.servings`, predicate: 'sh://path', target: 'recipe://servings' },
239
+ { source: `${shapeUri}.servings`, predicate: 'sh://datatype', target: 'xsd:integer' },
240
+ ];
241
+
242
+ const shape = SHACLShape.fromLinks(links as any, shapeUri);
243
+
244
+ expect(shape.nodeShapeUri).toBe(shapeUri);
245
+ expect(shape.targetClass).toBe('recipe://Recipe');
246
+ expect(shape.properties.length).toBe(2);
247
+
248
+ const title = shape.properties.find(p => p.name === 'title')!;
249
+ expect(title.path).toBe('recipe://title');
250
+ expect(title.datatype).toBe('xsd:string');
251
+ expect(title.minCount).toBe(1);
252
+ expect(title.maxCount).toBe(1);
253
+
254
+ const servings = shape.properties.find(p => p.name === 'servings')!;
255
+ expect(servings.path).toBe('recipe://servings');
256
+ expect(servings.datatype).toBe('xsd:integer');
257
+ });
258
+ });