@peers-app/peers-sdk 0.27.2 → 0.27.4

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 (260) hide show
  1. package/dist/contracts/contract-proxy.d.ts +7 -0
  2. package/dist/contracts/contract-proxy.js +19 -2
  3. package/dist/contracts/create-local-consumer.js +16 -1
  4. package/dist/contracts/index.d.ts +2 -0
  5. package/dist/contracts/index.js +2 -0
  6. package/dist/contracts/system/index.d.ts +2 -0
  7. package/dist/contracts/system/index.js +2 -0
  8. package/dist/contracts/system/invites-contract.js +2 -1
  9. package/dist/contracts/system/sync-check-contract.d.ts +97 -0
  10. package/dist/contracts/system/sync-check-contract.js +115 -0
  11. package/dist/contracts/tool-arguments.d.ts +15 -0
  12. package/dist/contracts/tool-arguments.js +41 -0
  13. package/dist/contracts/tool-catalog-service.js +2 -12
  14. package/dist/data/change-tracking.d.ts +40 -0
  15. package/dist/data/change-tracking.js +89 -0
  16. package/dist/data/orm/types.d.ts +24 -7
  17. package/dist/data/orm/types.js +97 -24
  18. package/dist/data/persistent-vars.d.ts +5 -0
  19. package/dist/data/persistent-vars.js +24 -6
  20. package/dist/define/manifest.js +6 -34
  21. package/dist/package-loader/package-loader.js +27 -22
  22. package/dist/system-ids.d.ts +5 -0
  23. package/dist/system-ids.js +6 -1
  24. package/package.json +4 -2
  25. package/dist/context/user-context-identity.test.d.ts +0 -1
  26. package/dist/context/user-context-identity.test.js +0 -220
  27. package/dist/contracts/__tests__/action-dispatch.test.d.ts +0 -1
  28. package/dist/contracts/__tests__/action-dispatch.test.js +0 -319
  29. package/dist/contracts/__tests__/also-implements.test.d.ts +0 -1
  30. package/dist/contracts/__tests__/also-implements.test.js +0 -110
  31. package/dist/contracts/__tests__/builder.test.d.ts +0 -1
  32. package/dist/contracts/__tests__/builder.test.js +0 -669
  33. package/dist/contracts/__tests__/contract-events.test.d.ts +0 -1
  34. package/dist/contracts/__tests__/contract-events.test.js +0 -199
  35. package/dist/contracts/__tests__/contract-observables.test.d.ts +0 -1
  36. package/dist/contracts/__tests__/contract-observables.test.js +0 -290
  37. package/dist/contracts/__tests__/contract-provider-router.test.d.ts +0 -1
  38. package/dist/contracts/__tests__/contract-provider-router.test.js +0 -315
  39. package/dist/contracts/__tests__/contract-proxy.test.d.ts +0 -1
  40. package/dist/contracts/__tests__/contract-proxy.test.js +0 -657
  41. package/dist/contracts/__tests__/contract-resolvers.test.d.ts +0 -1
  42. package/dist/contracts/__tests__/contract-resolvers.test.js +0 -758
  43. package/dist/contracts/__tests__/contract-subscription-lifecycle.test.d.ts +0 -1
  44. package/dist/contracts/__tests__/contract-subscription-lifecycle.test.js +0 -353
  45. package/dist/contracts/__tests__/contract-table-events.test.d.ts +0 -1
  46. package/dist/contracts/__tests__/contract-table-events.test.js +0 -209
  47. package/dist/contracts/__tests__/contract-transport.test.d.ts +0 -1
  48. package/dist/contracts/__tests__/contract-transport.test.js +0 -163
  49. package/dist/contracts/__tests__/extract.test.d.ts +0 -1
  50. package/dist/contracts/__tests__/extract.test.js +0 -145
  51. package/dist/contracts/__tests__/frozen-contracts.test.d.ts +0 -1
  52. package/dist/contracts/__tests__/frozen-contracts.test.js +0 -191
  53. package/dist/contracts/__tests__/generic-tool-dispatch.test.d.ts +0 -1
  54. package/dist/contracts/__tests__/generic-tool-dispatch.test.js +0 -130
  55. package/dist/contracts/__tests__/host-contract-client.test.d.ts +0 -1
  56. package/dist/contracts/__tests__/host-contract-client.test.js +0 -793
  57. package/dist/contracts/__tests__/host-contract-consumer.test.d.ts +0 -1
  58. package/dist/contracts/__tests__/host-contract-consumer.test.js +0 -163
  59. package/dist/contracts/__tests__/host-contract-transport.test.d.ts +0 -1
  60. package/dist/contracts/__tests__/host-contract-transport.test.js +0 -66
  61. package/dist/contracts/__tests__/integration.test.d.ts +0 -1
  62. package/dist/contracts/__tests__/integration.test.js +0 -348
  63. package/dist/contracts/__tests__/legacy-removal.test.d.ts +0 -1
  64. package/dist/contracts/__tests__/legacy-removal.test.js +0 -128
  65. package/dist/contracts/__tests__/persistent-registry.test.d.ts +0 -1
  66. package/dist/contracts/__tests__/persistent-registry.test.js +0 -109
  67. package/dist/contracts/__tests__/playground.test.d.ts +0 -1
  68. package/dist/contracts/__tests__/playground.test.js +0 -216
  69. package/dist/contracts/__tests__/registry.test.d.ts +0 -1
  70. package/dist/contracts/__tests__/registry.test.js +0 -238
  71. package/dist/contracts/__tests__/remote-contract-transport.test.d.ts +0 -1
  72. package/dist/contracts/__tests__/remote-contract-transport.test.js +0 -119
  73. package/dist/contracts/__tests__/system-contracts.test.d.ts +0 -1
  74. package/dist/contracts/__tests__/system-contracts.test.js +0 -247
  75. package/dist/contracts/__tests__/target-dispatch.d.ts +0 -11
  76. package/dist/contracts/__tests__/target-dispatch.js +0 -24
  77. package/dist/contracts/__tests__/tool-catalog-inventory.test.d.ts +0 -1
  78. package/dist/contracts/__tests__/tool-catalog-inventory.test.js +0 -44
  79. package/dist/contracts/__tests__/tool-catalog-service.test.d.ts +0 -1
  80. package/dist/contracts/__tests__/tool-catalog-service.test.js +0 -482
  81. package/dist/contracts/__tests__/tool-catalog.test.d.ts +0 -1
  82. package/dist/contracts/__tests__/tool-catalog.test.js +0 -1199
  83. package/dist/contracts/__tests__/validate.test.d.ts +0 -1
  84. package/dist/contracts/__tests__/validate.test.js +0 -991
  85. package/dist/contracts/__tests__/workflow-context.test.d.ts +0 -1
  86. package/dist/contracts/__tests__/workflow-context.test.js +0 -50
  87. package/dist/data/access-level.test.d.ts +0 -1
  88. package/dist/data/access-level.test.js +0 -53
  89. package/dist/data/change-tracking.test.d.ts +0 -1
  90. package/dist/data/change-tracking.test.js +0 -62
  91. package/dist/data/files/files.test.d.ts +0 -1
  92. package/dist/data/files/files.test.js +0 -770
  93. package/dist/data/group-members.test.d.ts +0 -1
  94. package/dist/data/group-members.test.js +0 -336
  95. package/dist/data/group-share.test.d.ts +0 -1
  96. package/dist/data/group-share.test.js +0 -110
  97. package/dist/data/groups.test.d.ts +0 -1
  98. package/dist/data/groups.test.js +0 -153
  99. package/dist/data/orm/cursor.test.d.ts +0 -1
  100. package/dist/data/orm/cursor.test.js +0 -314
  101. package/dist/data/orm/data-query.mongo.test.d.ts +0 -1
  102. package/dist/data/orm/data-query.mongo.test.js +0 -367
  103. package/dist/data/orm/data-query.sqlite.test.d.ts +0 -1
  104. package/dist/data/orm/data-query.sqlite.test.js +0 -365
  105. package/dist/data/orm/data-query.test.d.ts +0 -1
  106. package/dist/data/orm/data-query.test.js +0 -545
  107. package/dist/data/orm/dependency-injection.test.d.ts +0 -1
  108. package/dist/data/orm/dependency-injection.test.js +0 -171
  109. package/dist/data/orm/event-registry.test.d.ts +0 -1
  110. package/dist/data/orm/event-registry.test.js +0 -44
  111. package/dist/data/orm/multi-cursors.test.d.ts +0 -1
  112. package/dist/data/orm/multi-cursors.test.js +0 -463
  113. package/dist/data/orm/sql.data-source.test.d.ts +0 -1
  114. package/dist/data/orm/sql.data-source.test.js +0 -731
  115. package/dist/data/orm/table-container-events.test.d.ts +0 -1
  116. package/dist/data/orm/table-container-events.test.js +0 -101
  117. package/dist/data/orm/table-container.test.d.ts +0 -1
  118. package/dist/data/orm/table-container.test.js +0 -273
  119. package/dist/data/orm/table.event-source.test.d.ts +0 -1
  120. package/dist/data/orm/table.event-source.test.js +0 -331
  121. package/dist/data/orm/types.test.d.ts +0 -1
  122. package/dist/data/orm/types.test.js +0 -409
  123. package/dist/data/package-table-identity.test.d.ts +0 -1
  124. package/dist/data/package-table-identity.test.js +0 -46
  125. package/dist/data/package-version-permissions.test.d.ts +0 -1
  126. package/dist/data/package-version-permissions.test.js +0 -107
  127. package/dist/data/package-version-resolver.test.d.ts +0 -4
  128. package/dist/data/package-version-resolver.test.js +0 -543
  129. package/dist/data/package-versions.test.d.ts +0 -1
  130. package/dist/data/package-versions.test.js +0 -227
  131. package/dist/data/persistent-vars.conflict.test.d.ts +0 -1
  132. package/dist/data/persistent-vars.conflict.test.js +0 -126
  133. package/dist/data/persistent-vars.migration.test.d.ts +0 -1
  134. package/dist/data/persistent-vars.migration.test.js +0 -133
  135. package/dist/data/persistent-vars.package-scope.test.d.ts +0 -1
  136. package/dist/data/persistent-vars.package-scope.test.js +0 -27
  137. package/dist/data/sync-recovery-queue.test.d.ts +0 -1
  138. package/dist/data/sync-recovery-queue.test.js +0 -215
  139. package/dist/data/table-signing.test.d.ts +0 -1
  140. package/dist/data/table-signing.test.js +0 -118
  141. package/dist/data/tool-access.test.d.ts +0 -1
  142. package/dist/data/tool-access.test.js +0 -166
  143. package/dist/data/user-permissions.test.d.ts +0 -1
  144. package/dist/data/user-permissions.test.js +0 -305
  145. package/dist/data/user-trust-levels.test.d.ts +0 -1
  146. package/dist/data/user-trust-levels.test.js +0 -37
  147. package/dist/define/also-implements.test.d.ts +0 -1
  148. package/dist/define/also-implements.test.js +0 -123
  149. package/dist/define/declare-contract.test.d.ts +0 -1
  150. package/dist/define/declare-contract.test.js +0 -573
  151. package/dist/define/define.test.d.ts +0 -1
  152. package/dist/define/define.test.js +0 -1010
  153. package/dist/define/empty-fields.test.d.ts +0 -1
  154. package/dist/define/empty-fields.test.js +0 -55
  155. package/dist/define/events-observables.test.d.ts +0 -1
  156. package/dist/define/events-observables.test.js +0 -1013
  157. package/dist/define/serve-members.test.d.ts +0 -1
  158. package/dist/define/serve-members.test.js +0 -630
  159. package/dist/define/vars.test.d.ts +0 -1
  160. package/dist/define/vars.test.js +0 -39
  161. package/dist/device/binary-peer-connection-v2.test.d.ts +0 -1
  162. package/dist/device/binary-peer-connection-v2.test.js +0 -730
  163. package/dist/device/binary-peer-connection.test.d.ts +0 -1
  164. package/dist/device/binary-peer-connection.test.js +0 -184
  165. package/dist/device/connection.test.d.ts +0 -1
  166. package/dist/device/connection.test.js +0 -613
  167. package/dist/device/device-election.test.d.ts +0 -1
  168. package/dist/device/device-election.test.js +0 -516
  169. package/dist/device/device.test.d.ts +0 -1
  170. package/dist/device/device.test.js +0 -243
  171. package/dist/device/frame-cipher.test.d.ts +0 -1
  172. package/dist/device/frame-cipher.test.js +0 -219
  173. package/dist/device/get-trust-level-fn.test.d.ts +0 -1
  174. package/dist/device/get-trust-level-fn.test.js +0 -685
  175. package/dist/device/streamed-socket.test.d.ts +0 -1
  176. package/dist/device/streamed-socket.test.js +0 -216
  177. package/dist/device/tx-encoding.test.d.ts +0 -1
  178. package/dist/device/tx-encoding.test.js +0 -295
  179. package/dist/device-pairing/cpace.test.d.ts +0 -1
  180. package/dist/device-pairing/cpace.test.js +0 -116
  181. package/dist/device-pairing/device-pairing.test.d.ts +0 -1
  182. package/dist/device-pairing/device-pairing.test.js +0 -211
  183. package/dist/hlc.test.d.ts +0 -1
  184. package/dist/hlc.test.js +0 -45
  185. package/dist/host-credentials.test.d.ts +0 -1
  186. package/dist/host-credentials.test.js +0 -88
  187. package/dist/identity/account-email.test.d.ts +0 -1
  188. package/dist/identity/account-email.test.js +0 -59
  189. package/dist/identity/anchor-document.test.d.ts +0 -1
  190. package/dist/identity/anchor-document.test.js +0 -16
  191. package/dist/identity/anchor-resolution.test.d.ts +0 -1
  192. package/dist/identity/anchor-resolution.test.js +0 -130
  193. package/dist/identity/key-change.test.d.ts +0 -1
  194. package/dist/identity/key-change.test.js +0 -811
  195. package/dist/invites/group-approval.test.d.ts +0 -1
  196. package/dist/invites/group-approval.test.js +0 -223
  197. package/dist/invites/invite-token.test.d.ts +0 -1
  198. package/dist/invites/invite-token.test.js +0 -123
  199. package/dist/keys.test.d.ts +0 -1
  200. package/dist/keys.test.js +0 -336
  201. package/dist/logging/console-logger.test.d.ts +0 -1
  202. package/dist/logging/console-logger.test.js +0 -300
  203. package/dist/logging/console-logs.table.test.d.ts +0 -1
  204. package/dist/logging/console-logs.table.test.js +0 -156
  205. package/dist/observable.test.d.ts +0 -1
  206. package/dist/observable.test.js +0 -309
  207. package/dist/package-build/build-package.test.d.ts +0 -1
  208. package/dist/package-build/build-package.test.js +0 -204
  209. package/dist/package-build/contract-add.test.d.ts +0 -1
  210. package/dist/package-build/contract-add.test.js +0 -467
  211. package/dist/package-build/freeze-contracts.test.d.ts +0 -1
  212. package/dist/package-build/freeze-contracts.test.js +0 -355
  213. package/dist/package-installer/package-author-signing.test.d.ts +0 -1
  214. package/dist/package-installer/package-author-signing.test.js +0 -200
  215. package/dist/package-installer/package-cloner.test.d.ts +0 -1
  216. package/dist/package-installer/package-cloner.test.js +0 -278
  217. package/dist/package-installer/package-creator.test.d.ts +0 -1
  218. package/dist/package-installer/package-creator.test.js +0 -908
  219. package/dist/package-installer/package-dev-tools.test.d.ts +0 -1
  220. package/dist/package-installer/package-dev-tools.test.js +0 -64
  221. package/dist/package-installer/package-installer.test.d.ts +0 -1
  222. package/dist/package-installer/package-installer.test.js +0 -882
  223. package/dist/package-installer/package-propagation.test.d.ts +0 -1
  224. package/dist/package-installer/package-propagation.test.js +0 -1269
  225. package/dist/package-installer/package-publisher.test.d.ts +0 -1
  226. package/dist/package-installer/package-publisher.test.js +0 -153
  227. package/dist/package-installer/package-remote-checker.test.d.ts +0 -1
  228. package/dist/package-installer/package-remote-checker.test.js +0 -334
  229. package/dist/package-installer/package-seed-installer.test.d.ts +0 -1
  230. package/dist/package-installer/package-seed-installer.test.js +0 -123
  231. package/dist/package-installer/package-tarball.test.d.ts +0 -1
  232. package/dist/package-installer/package-tarball.test.js +0 -75
  233. package/dist/package-installer/peers-core-installer.test.d.ts +0 -1
  234. package/dist/package-installer/peers-core-installer.test.js +0 -49
  235. package/dist/package-installer/peers-core-trust-anchor.test.d.ts +0 -1
  236. package/dist/package-installer/peers-core-trust-anchor.test.js +0 -155
  237. package/dist/package-loader/contract-package-loader.test.d.ts +0 -1
  238. package/dist/package-loader/contract-package-loader.test.js +0 -274
  239. package/dist/package-loader/host-authority-loading.test.d.ts +0 -1
  240. package/dist/package-loader/host-authority-loading.test.js +0 -104
  241. package/dist/package-loader/host-dispatch-policy.test.d.ts +0 -1
  242. package/dist/package-loader/host-dispatch-policy.test.js +0 -56
  243. package/dist/package-loader/isolated-artifact.test.d.ts +0 -1
  244. package/dist/package-loader/isolated-artifact.test.js +0 -688
  245. package/dist/package-loader/isolated-persistent-var-gateway.test.d.ts +0 -1
  246. package/dist/package-loader/isolated-persistent-var-gateway.test.js +0 -56
  247. package/dist/package-loader/isolated-table-gateway.test.d.ts +0 -1
  248. package/dist/package-loader/isolated-table-gateway.test.js +0 -633
  249. package/dist/package-loader/package-loader.test.d.ts +0 -1
  250. package/dist/package-loader/package-loader.test.js +0 -1764
  251. package/dist/package-loader/resolve-consumed-contracts.test.d.ts +0 -1
  252. package/dist/package-loader/resolve-consumed-contracts.test.js +0 -166
  253. package/dist/serial-json.compat.test.d.ts +0 -1
  254. package/dist/serial-json.compat.test.js +0 -369
  255. package/dist/serial-json.test.d.ts +0 -1
  256. package/dist/serial-json.test.js +0 -401
  257. package/dist/testing/mock-host-contract.test.d.ts +0 -1
  258. package/dist/testing/mock-host-contract.test.js +0 -782
  259. package/dist/utils.test.d.ts +0 -1
  260. package/dist/utils.test.js +0 -232
@@ -1,353 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const observable_1 = require("../../observable");
4
- const field_type_1 = require("../../types/field-type");
5
- const utils_1 = require("../../utils");
6
- const contract_proxy_1 = require("../contract-proxy");
7
- const contract_transport_1 = require("../contract-transport");
8
- function deferred() {
9
- let resolve;
10
- const promise = new Promise((resolvePromise) => {
11
- resolve = resolvePromise;
12
- });
13
- return { promise, resolve };
14
- }
15
- function trackedEvent() {
16
- const handlers = new Set();
17
- const event = {
18
- subscribe: jest.fn((handler) => {
19
- handlers.add(handler);
20
- let active = true;
21
- return {
22
- unsubscribe: () => {
23
- if (!active) {
24
- return false;
25
- }
26
- active = false;
27
- return handlers.delete(handler);
28
- },
29
- };
30
- }),
31
- };
32
- return {
33
- event,
34
- activeCount: () => handlers.size,
35
- emit: async (value) => {
36
- for (const handler of [...handlers]) {
37
- await handler(value);
38
- }
39
- },
40
- };
41
- }
42
- function trackedObservable(initialValue) {
43
- const source = (0, observable_1.observable)(initialValue);
44
- const subscribe = source.subscribe;
45
- let activeCount = 0;
46
- source.subscribe = (handler) => {
47
- activeCount += 1;
48
- const subscription = subscribe(handler);
49
- let active = true;
50
- return {
51
- dispose: () => {
52
- if (!active) {
53
- return;
54
- }
55
- active = false;
56
- activeCount -= 1;
57
- subscription.dispose();
58
- },
59
- };
60
- };
61
- return { source, activeCount: () => activeCount };
62
- }
63
- async function settleTransport(emitSpy) {
64
- let previousCount = -1;
65
- while (previousCount !== emitSpy.mock.results.length) {
66
- previousCount = emitSpy.mock.results.length;
67
- const pending = emitSpy.mock.results
68
- .map((result) => result.value)
69
- .filter((value) => value instanceof Promise);
70
- await Promise.allSettled(pending);
71
- await Promise.resolve();
72
- }
73
- }
74
- const cleanups = [];
75
- afterEach(() => {
76
- for (const cleanup of cleanups.splice(0).reverse()) {
77
- cleanup();
78
- }
79
- jest.restoreAllMocks();
80
- });
81
- function makeHarness(opts = {}) {
82
- const contractId = (0, utils_1.newid)();
83
- const generic = trackedEvent();
84
- const tableChanged = trackedEvent();
85
- const count = trackedObservable(0);
86
- const list = jest.fn(async () => []);
87
- const run = jest.fn(async () => "ok");
88
- const contract = {
89
- contractId,
90
- version: 1,
91
- devTag: "dev",
92
- name: "Lifecycle Contract",
93
- description: "Exercises every subscription surface",
94
- tables: [
95
- {
96
- name: "Widgets",
97
- description: "Widgets",
98
- primaryKeyName: "id",
99
- fields: [{ name: "id", type: field_type_1.FieldType.id }],
100
- },
101
- ],
102
- tools: [
103
- {
104
- name: "run",
105
- usageDescription: "Run",
106
- inputFields: [],
107
- outputFields: [],
108
- },
109
- ],
110
- observables: [
111
- {
112
- name: "count",
113
- description: "Count",
114
- valueType: field_type_1.FieldType.number,
115
- writable: true,
116
- },
117
- ],
118
- events: [
119
- {
120
- name: "changed",
121
- description: "Changed",
122
- payloadFields: [{ name: "id", type: field_type_1.FieldType.id }],
123
- },
124
- ],
125
- };
126
- const resolution = {
127
- contractId,
128
- version: 1,
129
- tables: {
130
- Widgets: { dataChanged: tableChanged.event, list },
131
- },
132
- tools: {
133
- run: {
134
- tool: { toolId: (0, utils_1.newid)(), name: "run" },
135
- toolFn: run,
136
- },
137
- },
138
- observables: { count: count.source },
139
- observableWritability: { count: true },
140
- events: { changed: generic.event },
141
- };
142
- const [consumerTransport, providerTransport] = (0, contract_transport_1.inProcessTransportPair)();
143
- const emitSpy = jest.spyOn(consumerTransport, "emit");
144
- const session = (0, contract_proxy_1.createContractProviderSession)(resolution, providerTransport, {
145
- permissionCheck: opts.permissionCheck,
146
- });
147
- const consumer = (0, contract_proxy_1.createContractConsumer)(contract, consumerTransport);
148
- cleanups.push(() => {
149
- consumer.dispose();
150
- session.dispose();
151
- });
152
- return {
153
- consumer,
154
- session,
155
- generic,
156
- tableChanged,
157
- count,
158
- emitSpy,
159
- consumerTransport,
160
- providerTransport,
161
- contractId,
162
- };
163
- }
164
- describe("contract subscription lifecycle — consumer teardown", () => {
165
- it("detaches generic, table, and observable provider subscriptions", async () => {
166
- const { consumer, generic, tableChanged, count, emitSpy } = makeHarness();
167
- consumer.events.changed.subscribe(() => { });
168
- consumer.tables.Widgets.dataChanged.subscribe(() => { });
169
- await consumer.loadingPromise;
170
- expect(generic.activeCount()).toBe(1);
171
- expect(tableChanged.activeCount()).toBe(1);
172
- expect(count.activeCount()).toBe(1);
173
- consumer.dispose();
174
- await settleTransport(emitSpy);
175
- expect(generic.activeCount()).toBe(0);
176
- expect(tableChanged.activeCount()).toBe(0);
177
- expect(count.activeCount()).toBe(0);
178
- });
179
- it("is idempotent and sends each wire unsubscribe at most once", async () => {
180
- const { consumer, emitSpy } = makeHarness();
181
- consumer.events.changed.subscribe(() => { });
182
- await consumer.loadingPromise;
183
- consumer.dispose();
184
- consumer.dispose();
185
- await settleTransport(emitSpy);
186
- const unsubscribeCalls = emitSpy.mock.calls.filter(([channel, call]) => channel === contract_transport_1.CONTRACT_REQUEST_CHANNEL && call.operation === "unsubscribe");
187
- expect(unsubscribeCalls).toHaveLength(2);
188
- });
189
- it("returns false and emits nothing for a repeated event unsubscribe", async () => {
190
- const { consumer, emitSpy } = makeHarness();
191
- const subscription = consumer.events.changed.subscribe(() => { });
192
- await settleTransport(emitSpy);
193
- emitSpy.mockClear();
194
- expect(subscription.unsubscribe()).toBe(true);
195
- expect(subscription.unsubscribe()).toBe(false);
196
- await settleTransport(emitSpy);
197
- const unsubscribeCalls = emitSpy.mock.calls
198
- .map(([, call]) => call)
199
- .filter((call) => call.name === "changed" && call.operation === "unsubscribe");
200
- expect(unsubscribeCalls).toHaveLength(1);
201
- });
202
- it("rejects new remote operations and writes after disposal without emitting", async () => {
203
- const { consumer, emitSpy } = makeHarness();
204
- await consumer.loadingPromise;
205
- expect(consumer.observables.count()).toBe(0);
206
- consumer.dispose();
207
- await settleTransport(emitSpy);
208
- emitSpy.mockClear();
209
- await expect(consumer.tables.Widgets.list()).rejects.toThrow(/consumer .* disposed/i);
210
- await expect(consumer.tools.run()).rejects.toThrow(/consumer .* disposed/i);
211
- expect(() => consumer.events.changed.subscribe(() => { })).toThrow(/consumer .* disposed/i);
212
- expect(() => consumer.tables.Widgets.dataChanged.subscribe(() => { })).toThrow(/consumer .* disposed/i);
213
- expect(() => consumer.observables.count(1)).toThrow(/consumer .* disposed/i);
214
- expect(consumer.observables.count()).toBe(0);
215
- expect(emitSpy).not.toHaveBeenCalled();
216
- });
217
- });
218
- describe("contract subscription lifecycle — ordering and failures", () => {
219
- it("queues immediate unsubscribe behind a delayed subscribe acknowledgement", async () => {
220
- const permissionStarted = deferred();
221
- const permissionResult = deferred();
222
- const permissionCheck = async (call) => {
223
- if (call.operation === "subscribe") {
224
- permissionStarted.resolve();
225
- return permissionResult.promise;
226
- }
227
- return true;
228
- };
229
- const { consumer, generic, emitSpy } = makeHarness({ permissionCheck });
230
- const subscription = consumer.events.changed.subscribe(() => { });
231
- await permissionStarted.promise;
232
- expect(subscription.unsubscribe()).toBe(true);
233
- expect(generic.activeCount()).toBe(0);
234
- permissionResult.resolve(true);
235
- await settleTransport(emitSpy);
236
- expect(generic.activeCount()).toBe(0);
237
- const operations = emitSpy.mock.calls
238
- .filter(([channel]) => channel === contract_transport_1.CONTRACT_REQUEST_CHANNEL)
239
- .map(([, call]) => call.operation)
240
- .filter((operation) => operation === "subscribe" || operation === "unsubscribe");
241
- expect(operations).toEqual(["subscribe", "subscribe", "unsubscribe"]);
242
- });
243
- it("does not unsubscribe after a failed subscribe", async () => {
244
- const consoleError = jest.spyOn(console, "error").mockImplementation();
245
- const permissionStarted = deferred();
246
- const permissionResult = deferred();
247
- const permissionCheck = async (call) => {
248
- if (call.operation === "subscribe") {
249
- permissionStarted.resolve();
250
- return permissionResult.promise;
251
- }
252
- return true;
253
- };
254
- const { consumer, generic, emitSpy } = makeHarness({ permissionCheck });
255
- const subscription = consumer.events.changed.subscribe(() => { });
256
- await permissionStarted.promise;
257
- subscription.unsubscribe();
258
- consumer.dispose();
259
- permissionResult.resolve(false);
260
- await settleTransport(emitSpy);
261
- expect(generic.activeCount()).toBe(0);
262
- const changedCalls = emitSpy.mock.calls
263
- .map(([, call]) => call)
264
- .filter((call) => call.name === "changed");
265
- expect(changedCalls.map((call) => call.operation)).toEqual(["subscribe"]);
266
- expect(consoleError).toHaveBeenCalledWith("Contract subscribe failed", expect.objectContaining({ message: expect.stringMatching(/Permission denied/) }));
267
- });
268
- });
269
- describe("contract subscription lifecycle — provider protocol", () => {
270
- it("rejects duplicate subscription ids without corrupting the existing refcount", async () => {
271
- const { generic, consumerTransport, contractId } = makeHarness();
272
- const subscriptionId = (0, utils_1.newid)();
273
- const call = {
274
- contractId,
275
- version: 1,
276
- member: "event",
277
- name: "changed",
278
- operation: "subscribe",
279
- subscriptionId,
280
- args: [],
281
- };
282
- await consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, call);
283
- await expect(consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, call)).rejects.toThrow(/Duplicate contract subscriptionId/);
284
- expect(generic.activeCount()).toBe(1);
285
- await consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, {
286
- ...call,
287
- operation: "unsubscribe",
288
- subscriptionId: (0, utils_1.newid)(),
289
- });
290
- expect(generic.activeCount()).toBe(1);
291
- await consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, {
292
- ...call,
293
- operation: "unsubscribe",
294
- });
295
- expect(generic.activeCount()).toBe(0);
296
- });
297
- it("rejects missing ids and unsupported member/operation combinations", async () => {
298
- const { consumerTransport, contractId } = makeHarness();
299
- const base = {
300
- contractId,
301
- version: 1,
302
- name: "changed",
303
- args: [],
304
- };
305
- await expect(consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, {
306
- ...base,
307
- member: "event",
308
- operation: "subscribe",
309
- })).rejects.toThrow(/requires a subscriptionId/);
310
- await expect(consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, {
311
- ...base,
312
- member: "tool",
313
- operation: "subscribe",
314
- subscriptionId: (0, utils_1.newid)(),
315
- })).rejects.toThrow(/does not support subscriptions/);
316
- await expect(consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, {
317
- ...base,
318
- member: "event",
319
- operation: "get",
320
- subscriptionId: (0, utils_1.newid)(),
321
- })).rejects.toThrow(/Unsupported subscription operation/);
322
- await expect(consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, {
323
- ...base,
324
- member: "table",
325
- name: "Widgets",
326
- operation: "subscribe",
327
- event: "privateEvent",
328
- subscriptionId: (0, utils_1.newid)(),
329
- })).rejects.toThrow(/Unsupported table event/);
330
- });
331
- it("isolates a rejected notify so later subscribers still receive the payload", async () => {
332
- const consoleError = jest.spyOn(console, "error").mockImplementation();
333
- const { consumer, generic, emitSpy } = makeHarness();
334
- consumer.events.changed.subscribe(() => {
335
- throw new Error("first handler failed");
336
- });
337
- const received = [];
338
- consumer.events.changed.subscribe((value) => received.push(value));
339
- await settleTransport(emitSpy);
340
- await generic.emit({ id: "w1" });
341
- await Promise.resolve();
342
- expect(received).toEqual([{ id: "w1" }]);
343
- expect(consoleError).toHaveBeenCalledWith(expect.stringMatching(/Contract notify failed/), expect.objectContaining({ message: "first handler failed" }));
344
- });
345
- it("allows provider session disposal more than once", () => {
346
- const { session, generic } = makeHarness();
347
- expect(() => {
348
- session.dispose();
349
- session.dispose();
350
- }).not.toThrow();
351
- expect(generic.activeCount()).toBe(0);
352
- });
353
- });
@@ -1,209 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const event_registry_1 = require("../../data/orm/event-registry");
4
- const field_type_1 = require("../../types/field-type");
5
- const utils_1 = require("../../utils");
6
- const contract_proxy_1 = require("../contract-proxy");
7
- const contract_transport_1 = require("../contract-transport");
8
- /** Contract-member name of the single table under test. */
9
- const TABLE_NAME = "Widgets";
10
- /** Flush pending micro/macrotasks so fire-and-forget subscribe requests land. */
11
- const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
12
- /** Build a `dataChanged` payload matching the real `Table.dataChanged` shape. */
13
- const mkEvent = (id, op = "insert") => ({ dataObject: { id }, op, source: "local" });
14
- /** A minimal contract that declares exactly one table (no generic events). */
15
- function makeContract(contractId) {
16
- return {
17
- contractId,
18
- version: 1,
19
- devTag: "dev",
20
- name: "Table Events Test Contract",
21
- description: "For contract table dataChanged proxy tests",
22
- tables: [
23
- {
24
- name: TABLE_NAME,
25
- description: "Widgets",
26
- primaryKeyName: "id",
27
- fields: [{ name: "id", type: field_type_1.FieldType.id, description: "ID" }],
28
- },
29
- ],
30
- tools: [],
31
- observables: [],
32
- events: [],
33
- };
34
- }
35
- /**
36
- * Wire a consumer to a provider session over an in-process transport pair. The provider
37
- * "table" is a fake exposing a `save` method plus `dataChanged: emitter.event`, mirroring
38
- * the real {@link Table.dataChanged}. The emitter is built through an {@link EventRegistry}
39
- * scoped by a unique `dataContextId`, so the actual `${tableName}_DataChanged_${dataContextId}`
40
- * name path drives delivery (proves dataContext scoping) and never collides across tests.
41
- */
42
- function wire(opts = {}) {
43
- const contractId = (0, utils_1.newid)();
44
- const dataContextId = (0, utils_1.newid)();
45
- const registry = new event_registry_1.EventRegistry(dataContextId);
46
- const emitter = registry.getEmitter(`${TABLE_NAME}_DataChanged`);
47
- const save = jest.fn(async (record) => record);
48
- const table = { dataChanged: emitter.event, save };
49
- const resolution = {
50
- contractId,
51
- version: 1,
52
- // Fake table stands in for a real Table<Widget>; the session only touches
53
- // `dataChanged` (an Event) and dispatched method names.
54
- tables: { [TABLE_NAME]: table },
55
- };
56
- const [consumerTransport, providerTransport] = (0, contract_transport_1.inProcessTransportPair)();
57
- const session = (0, contract_proxy_1.createContractProviderSession)(resolution, providerTransport, {
58
- permissionCheck: opts.permissionCheck,
59
- context: opts.context,
60
- });
61
- const consumer = (0, contract_proxy_1.createContractConsumer)(makeContract(contractId), consumerTransport);
62
- return {
63
- consumer,
64
- session,
65
- emitter,
66
- save,
67
- contractId,
68
- consumerTransport,
69
- providerTransport,
70
- };
71
- }
72
- describe("contract table dataChanged — end-to-end", () => {
73
- it("delivers a dataChanged payload to a subscribed consumer", async () => {
74
- const { consumer, session, emitter } = wire();
75
- const received = [];
76
- consumer.tables[TABLE_NAME].dataChanged.subscribe((v) => received.push(v));
77
- await flush();
78
- await emitter.emit(mkEvent("w1"));
79
- expect(received).toEqual([mkEvent("w1")]);
80
- session.dispose();
81
- consumer.dispose();
82
- });
83
- });
84
- describe("contract table dataChanged — subscription gating", () => {
85
- it("pushes nothing on the notify channel when nobody is subscribed", async () => {
86
- const { emitter, providerTransport, session, consumer } = wire();
87
- const notifySpy = jest.spyOn(providerTransport, "emit");
88
- await emitter.emit(mkEvent("w0"));
89
- expect(notifySpy).not.toHaveBeenCalled();
90
- session.dispose();
91
- consumer.dispose();
92
- });
93
- });
94
- describe("contract table dataChanged — unsubscribe + refcount", () => {
95
- it("stops delivering after unsubscribe and detaches the live subscription", async () => {
96
- const { consumer, session, emitter, providerTransport } = wire();
97
- const originalSubscribe = emitter.event.subscribe;
98
- const liveUnsubscribe = jest.fn();
99
- jest.spyOn(emitter.event, "subscribe").mockImplementation((handler) => {
100
- const subscription = originalSubscribe(handler);
101
- return {
102
- unsubscribe: () => {
103
- liveUnsubscribe();
104
- return subscription.unsubscribe();
105
- },
106
- };
107
- });
108
- const received = [];
109
- const sub = consumer.tables[TABLE_NAME].dataChanged.subscribe((v) => received.push(v));
110
- await flush();
111
- await emitter.emit(mkEvent("a"));
112
- expect(received).toEqual([mkEvent("a")]);
113
- const notifySpy = jest.spyOn(providerTransport, "emit");
114
- sub.unsubscribe();
115
- await flush();
116
- // Refcount reached 0, so the provider detached its live handler: no further pushes.
117
- expect(liveUnsubscribe).toHaveBeenCalledTimes(1);
118
- await emitter.emit(mkEvent("b", "update"));
119
- expect(notifySpy).not.toHaveBeenCalled();
120
- expect(received).toEqual([mkEvent("a")]);
121
- session.dispose();
122
- consumer.dispose();
123
- });
124
- });
125
- describe("contract table dataChanged — fan-out", () => {
126
- it("delivers to multiple subscribers through a single live subscription", async () => {
127
- const { consumer, session, emitter, providerTransport } = wire();
128
- const liveSubscribeSpy = jest.spyOn(emitter.event, "subscribe");
129
- const a = [];
130
- const b = [];
131
- consumer.tables[TABLE_NAME].dataChanged.subscribe((v) => a.push(v));
132
- consumer.tables[TABLE_NAME].dataChanged.subscribe((v) => b.push(v));
133
- await flush();
134
- // One live subscription is shared across both consumer subscriptions (refcount by key).
135
- expect(liveSubscribeSpy).toHaveBeenCalledTimes(1);
136
- const notifySpy = jest.spyOn(providerTransport, "emit");
137
- await emitter.emit(mkEvent("x"));
138
- expect(a).toEqual([mkEvent("x")]);
139
- expect(b).toEqual([mkEvent("x")]);
140
- // One push per subscriptionId fanned out from the single emit.
141
- expect(notifySpy).toHaveBeenCalledTimes(2);
142
- session.dispose();
143
- consumer.dispose();
144
- });
145
- });
146
- describe("contract table dataChanged — permission gating", () => {
147
- it("rejects a denied subscribe and attaches no live subscription", async () => {
148
- const consoleError = jest.spyOn(console, "error").mockImplementation();
149
- const localUserId = (0, utils_1.newid)();
150
- const { providerTransport, emitter, session, consumer } = wire({
151
- permissionCheck: contract_proxy_1.sameUserContractPermissionCheck,
152
- context: { localUserId, callerUserId: (0, utils_1.newid)() },
153
- });
154
- const liveSubscribeSpy = jest.spyOn(emitter.event, "subscribe");
155
- const received = [];
156
- consumer.tables[TABLE_NAME].dataChanged.subscribe((value) => received.push(value));
157
- await flush();
158
- // No live subscription attached, so emitting the event pushes nothing.
159
- const notifySpy = jest.spyOn(providerTransport, "emit");
160
- await emitter.emit(mkEvent("z"));
161
- expect(notifySpy).not.toHaveBeenCalled();
162
- expect(liveSubscribeSpy).not.toHaveBeenCalled();
163
- expect(received).toEqual([]);
164
- expect(consoleError).toHaveBeenCalledWith("Contract subscribe failed", expect.objectContaining({ message: expect.stringMatching(/Permission denied/) }));
165
- session.dispose();
166
- consumer.dispose();
167
- });
168
- });
169
- describe("contract table dataChanged — methods coexist", () => {
170
- it("forwards table methods as calls alongside dataChanged subscriptions", async () => {
171
- const { consumer, session, save } = wire();
172
- const saved = await consumer.tables[TABLE_NAME].save({ id: "w9", label: "hi" });
173
- expect(save).toHaveBeenCalledWith({ id: "w9", label: "hi" });
174
- expect(saved).toEqual({ id: "w9", label: "hi" });
175
- session.dispose();
176
- consumer.dispose();
177
- });
178
- });
179
- describe("contract table dataChanged — teardown", () => {
180
- it("session.dispose() detaches live subscriptions and stops pushes", async () => {
181
- const { consumer, session, emitter, providerTransport } = wire();
182
- const received = [];
183
- consumer.tables[TABLE_NAME].dataChanged.subscribe((v) => received.push(v));
184
- await flush();
185
- await emitter.emit(mkEvent("1"));
186
- expect(received).toEqual([mkEvent("1")]);
187
- const notifySpy = jest.spyOn(providerTransport, "emit");
188
- session.dispose();
189
- await emitter.emit(mkEvent("2", "update"));
190
- expect(notifySpy).not.toHaveBeenCalled();
191
- expect(received).toEqual([mkEvent("1")]);
192
- consumer.dispose();
193
- });
194
- it("consumer.dispose() unsubscribes the provider and drops later pushes", async () => {
195
- const { consumer, session, emitter, providerTransport } = wire();
196
- const received = [];
197
- consumer.tables[TABLE_NAME].dataChanged.subscribe((v) => received.push(v));
198
- await flush();
199
- await emitter.emit(mkEvent("1"));
200
- expect(received).toEqual([mkEvent("1")]);
201
- const notifySpy = jest.spyOn(providerTransport, "emit");
202
- consumer.dispose();
203
- await flush();
204
- await emitter.emit(mkEvent("2", "update"));
205
- expect(notifySpy).not.toHaveBeenCalled();
206
- expect(received).toEqual([mkEvent("1")]);
207
- session.dispose();
208
- });
209
- });
@@ -1 +0,0 @@
1
- export {};