@jsb188/graphql 1.1.11 → 1.1.13

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 (174) hide show
  1. package/LICENSE +21 -0
  2. package/audience.d.ts +205 -0
  3. package/cache/config.ts +65 -28
  4. package/cache/index.helpers.ts +96 -0
  5. package/cache/index.ts +364 -808
  6. package/cache/mounted-queries.ts +33 -0
  7. package/cache/optimistic-ui-runtime.ts +46 -0
  8. package/cache/optimistic-ui.ts +401 -0
  9. package/cache/persist.ts +107 -22
  10. package/cache/realtime-fence.ts +47 -0
  11. package/cache/realtime-live.ts +17 -0
  12. package/cache/store.ts +22 -64
  13. package/cache/value.helpers.ts +17 -0
  14. package/{drafts/store.ts → client/draft-persistence-indexed-db.ts} +19 -3
  15. package/{drafts/index.ts → client/draft-persistence.ts} +13 -39
  16. package/client/index.ts +4 -2
  17. package/client/request.helpers.ts +14 -0
  18. package/client/request.ts +122 -232
  19. package/client/storage-asset.ts +58 -0
  20. package/gql/fragments/accountAuthFragments.ts +0 -4
  21. package/gql/fragments/agentMemoryFragments.ts +5 -1
  22. package/gql/fragments/agentNoteFragments.ts +156 -0
  23. package/gql/fragments/aiChatFragments.ts +35 -11
  24. package/gql/fragments/aiChatLinkFragments.ts +8 -0
  25. package/gql/fragments/audienceFragments.ts +76 -0
  26. package/gql/fragments/dataTableFragments.ts +48 -80
  27. package/gql/fragments/emailFragments.ts +49 -21
  28. package/gql/fragments/emailOutreachFragments.ts +281 -0
  29. package/gql/fragments/fieldDocFragments.ts +222 -40
  30. package/gql/fragments/inboundContactFragments.ts +2 -0
  31. package/gql/fragments/libraryFragments.ts +120 -0
  32. package/gql/fragments/logFragments.ts +93 -2
  33. package/gql/fragments/organizationFragments.ts +12 -0
  34. package/gql/fragments/qrCodeFragments.ts +29 -0
  35. package/gql/fragments/scheduleFragments.ts +574 -145
  36. package/gql/fragments/sheetFragments.ts +86 -424
  37. package/gql/fragments/storageFragments.ts +25 -1
  38. package/gql/fragments/workWaitFragments.ts +13 -0
  39. package/gql/fragments/workflowFragments.ts +32 -4
  40. package/gql/mutations/agentMemoryMutations.ts +32 -0
  41. package/gql/mutations/agentNoteMutations.ts +64 -0
  42. package/gql/mutations/aiChatLinkMutations.ts +13 -0
  43. package/gql/mutations/aiChatMutations.ts +34 -4
  44. package/gql/mutations/audienceMutations.ts +58 -0
  45. package/gql/mutations/authMutations.ts +24 -28
  46. package/gql/mutations/billingMutations.ts +24 -0
  47. package/gql/mutations/contactMutations.ts +23 -0
  48. package/gql/mutations/dataTableMutations.ts +18 -75
  49. package/gql/mutations/emailMutations.ts +22 -0
  50. package/gql/mutations/emailOutreachMutations.ts +139 -0
  51. package/gql/mutations/fieldDocMutations.ts +162 -13
  52. package/gql/mutations/index.ts +10 -5
  53. package/gql/mutations/organizationMutations.ts +6 -0
  54. package/gql/mutations/qrCodeMutations.ts +20 -0
  55. package/gql/mutations/scheduleMutations.ts +389 -417
  56. package/gql/mutations/sheetMutations.ts +62 -118
  57. package/gql/mutations/storageMutations.ts +1 -1
  58. package/gql/mutations/workflowMutations.ts +94 -0
  59. package/gql/queries/agentMemoryQueries.ts +12 -2
  60. package/gql/queries/agentNoteQueries.ts +86 -0
  61. package/gql/queries/aiChatQueries.ts +63 -12
  62. package/gql/queries/audienceQueries.ts +104 -0
  63. package/gql/queries/authQueries.ts +20 -4
  64. package/gql/queries/billingQueries.ts +114 -9
  65. package/gql/queries/dataTableQueries.ts +43 -75
  66. package/gql/queries/emailOutreachQueries.ts +282 -0
  67. package/gql/queries/emailQueries.ts +48 -64
  68. package/gql/queries/emailReplyReportQueries.ts +127 -0
  69. package/gql/queries/fieldDocQueries.ts +128 -98
  70. package/gql/queries/inboundContactQueries.ts +15 -4
  71. package/gql/queries/index.ts +5 -3
  72. package/gql/queries/libraryQueries.ts +112 -0
  73. package/gql/queries/logQueries.ts +86 -12
  74. package/gql/queries/organizationQueries.ts +44 -12
  75. package/gql/queries/qrCodeQueries.ts +27 -0
  76. package/gql/queries/scheduleQueries.ts +401 -278
  77. package/gql/queries/sheetQueries.ts +94 -165
  78. package/gql/queries/storageQueries.ts +11 -4
  79. package/gql/queries/workflowQueries.ts +61 -12
  80. package/gql/reactivity.ts +98 -0
  81. package/gql/recordReads.ts +171 -0
  82. package/hooks/index.helpers.ts +52 -0
  83. package/hooks/index.ts +920 -715
  84. package/hooks/use-agentMemory-mtn.ts +29 -31
  85. package/hooks/use-agentMemory-qry.ts +15 -3
  86. package/hooks/use-agentNote-mtn.ts +89 -0
  87. package/hooks/use-agentNote-qry.ts +162 -0
  88. package/hooks/use-agentNote-recovery.helpers.ts +58 -0
  89. package/hooks/use-agentNote-recovery.ts +57 -0
  90. package/hooks/use-aiChat-mtn.ts +104 -115
  91. package/hooks/use-aiChat-qry.helpers.ts +124 -0
  92. package/hooks/use-aiChat-qry.ts +254 -387
  93. package/hooks/use-aiChat-stream-mtn.ts +14 -0
  94. package/hooks/use-audience-mtn.ts +170 -0
  95. package/hooks/use-audience-qry.helpers.ts +39 -0
  96. package/hooks/use-audience-qry.ts +245 -0
  97. package/hooks/use-auth-mtn.ts +30 -1
  98. package/hooks/use-billing-mtn.ts +31 -0
  99. package/hooks/use-billing-qry.ts +76 -0
  100. package/hooks/use-dataTable-mtn.ts +23 -115
  101. package/hooks/use-dataTable-qry.ts +69 -914
  102. package/hooks/use-email-mtn.ts +26 -0
  103. package/hooks/use-email-qry.helpers.ts +37 -0
  104. package/hooks/use-email-qry.ts +44 -174
  105. package/hooks/use-emailOutreach-mtn.ts +211 -0
  106. package/hooks/use-emailOutreach-qry.helpers.ts +25 -0
  107. package/hooks/use-emailOutreach-qry.ts +367 -0
  108. package/hooks/use-emailOutreachReplyReport-qry.ts +46 -0
  109. package/hooks/use-fieldDoc-mtn.ts +244 -32
  110. package/hooks/use-fieldDoc-qry.helpers.ts +49 -0
  111. package/hooks/use-fieldDoc-qry.ts +134 -102
  112. package/hooks/use-inboundContact-qry.ts +7 -9
  113. package/hooks/use-library-qry.ts +233 -0
  114. package/hooks/use-log-mtn.ts +25 -12
  115. package/hooks/use-log-qry.ts +99 -46
  116. package/hooks/use-organization-qry.ts +16 -60
  117. package/hooks/use-qrCode-mtn.ts +40 -0
  118. package/hooks/use-qrCode-qry.ts +78 -0
  119. package/hooks/use-schedule-mtn.ts +231 -348
  120. package/hooks/use-schedule-qry.ts +208 -139
  121. package/hooks/use-schedule-reactive.ts +218 -0
  122. package/hooks/use-sheet-mtn.ts +91 -199
  123. package/hooks/use-sheet-qry.ts +88 -237
  124. package/hooks/use-storage-qry.ts +84 -61
  125. package/hooks/use-workflow-mtn.ts +84 -23
  126. package/hooks/use-workflow-qry.ts +60 -61
  127. package/libs/ai-chat-link-events.ts +78 -0
  128. package/libs/ai-chat-outgoing.helpers.ts +153 -0
  129. package/libs/ai-chat-outgoing.ts +11 -0
  130. package/libs/ai-chat-outgoing.types.ts +25 -0
  131. package/libs/ai-chat-run-realtime.ts +20 -0
  132. package/libs/ai-chat-submission-realtime.ts +150 -0
  133. package/libs/data-changes.helpers.ts +161 -0
  134. package/libs/data-changes.ts +382 -0
  135. package/libs/fragment-realtime-sequence.ts +55 -0
  136. package/libs/open-sheet.ts +138 -0
  137. package/libs/record-read.ts +327 -0
  138. package/libs/sheet-command-batch.ts +45 -0
  139. package/libs/sheet-realtime-packet.ts +53 -0
  140. package/libs/sheet-view-realtime-router.ts +99 -0
  141. package/loaders/emailPreference.ts +69 -0
  142. package/loaders/rest.ts +12 -0
  143. package/package.json +77 -25
  144. package/schedule.d.ts +1183 -0
  145. package/sse.helpers.ts +19 -0
  146. package/sse.ts +26 -115
  147. package/types.d.ts +48 -25
  148. package/cache/index.test.ts +0 -881
  149. package/client/data-table-realtime-subscriptions.test.ts +0 -740
  150. package/client/data-table-realtime-subscriptions.ts +0 -955
  151. package/client/data-table-realtime.test.ts +0 -792
  152. package/client/data-table-realtime.ts +0 -1218
  153. package/client/fragment-realtime-sequence.ts +0 -52
  154. package/client/open-sheet.ts +0 -137
  155. package/client/request-managed-session.test.ts +0 -574
  156. package/client/request-no-store.test.ts +0 -157
  157. package/client/sse-ticket.test.ts +0 -26
  158. package/deno.lock +0 -170
  159. package/deno.test.json +0 -10
  160. package/gql/fragments/agentDocFragments.ts +0 -46
  161. package/gql/fragments/billingFragments.ts +0 -14
  162. package/gql/fragments/campaignFragments.ts +0 -277
  163. package/gql/mutations/agentDocMutations.ts +0 -177
  164. package/gql/mutations/campaignMutations.ts +0 -215
  165. package/gql/queries/agentDocQueries.ts +0 -120
  166. package/gql/queries/appQueries.ts +0 -14
  167. package/gql/queries/campaignQueries.ts +0 -127
  168. package/hooks/use-agentDoc-mtn.ts +0 -285
  169. package/hooks/use-agentDoc-qry.ts +0 -208
  170. package/hooks/use-app-qry.ts +0 -22
  171. package/hooks/use-campaign-mtn.ts +0 -377
  172. package/hooks/use-campaign-qry.ts +0 -257
  173. package/loaders/campaign.ts +0 -137
  174. package/tsconfig.json +0 -13
@@ -1,740 +0,0 @@
1
- import WS_ACTIONS from '@jsb188/app/constants/wsActions.ts';
2
- import { DATA_TABLE_REALTIME_MAX_PAYLOAD_BYTES } from '@jsb188/mday/constants/dataTable.ts';
3
- import {
4
- buildDataTableRealtimeWatchTargets,
5
- DATA_TABLE_REALTIME_SUBSCRIPTION_MAX,
6
- DataTableRealtimeSubscriptionRegistry,
7
- dataTableRealtimeWatchPayloadIsWithinLimit,
8
- getDataTableRealtimeFragmentValue,
9
- getDataTableRealtimeQuerySubscriptions,
10
- normalizeDataTableRealtimeWatchAcknowledgement,
11
- normalizeDataTableRealtimeWatchTargets,
12
- resolveDataTableRealtimeQueryResult,
13
- } from './data-table-realtime-subscriptions.ts';
14
-
15
- /*
16
- * Assert JSON-compatible registry output without requiring a remote test dependency.
17
- */
18
- function assertEquals(actual: unknown, expected: unknown) {
19
- if (JSON.stringify(actual) !== JSON.stringify(expected)) {
20
- throw new Error(`Values differ: ${JSON.stringify(actual)}`);
21
- }
22
- }
23
-
24
- /*
25
- * Build one JWT-shaped value whose payload is used only as an untrusted client
26
- * scheduling hint. No test or production client treats this as authorization.
27
- */
28
- function createRealtimeCapabilityHint(expiresAtSeconds: number) {
29
- return `header.${btoa(JSON.stringify({ exp: expiresAtSeconds }))}.signature`;
30
- }
31
-
32
- /*
33
- * Build one boundary-sized exact-set subscription for packet-budget tests.
34
- */
35
- function buildBoundaryDataTableSubscriptions(tokenLength: number) {
36
- return [
37
- {
38
- dataTableId: 'table-a',
39
- lastSeq: '0',
40
- organizationId: 'org-a',
41
- realtimeToken: 'a'.repeat(tokenLength),
42
- },
43
- ];
44
- }
45
-
46
- /*
47
- * Return the encoded watch packet size with or without the transport field.
48
- */
49
- function getDataTableWatchPacketBytes(
50
- tokenLength: number,
51
- includeWatchId: boolean,
52
- ) {
53
- return (
54
- 2 +
55
- new TextEncoder().encode(
56
- JSON.stringify([
57
- WS_ACTIONS.data_table_watch,
58
- {
59
- tables: buildBoundaryDataTableSubscriptions(tokenLength),
60
- ...(includeWatchId ? { watchId: 'w'.repeat(200) } : {}),
61
- },
62
- ]),
63
- ).byteLength
64
- );
65
- }
66
-
67
- Deno.test('the singular DataTable query requests its realtime capability', async () => {
68
- const querySource = await Deno.readTextFile(
69
- new URL('../gql/queries/dataTableQueries.ts', import.meta.url),
70
- );
71
- const singularQuerySource = querySource.slice(
72
- querySource.indexOf('export const dataTableQry'),
73
- querySource.indexOf('export const dataTableMetadataQry'),
74
- );
75
-
76
- if (!/\.\.\.dataTableFragment\s+realtimeToken/.test(singularQuerySource)) {
77
- throw new Error(
78
- 'The direct DataTable route cannot join realtime rooms without a capability.',
79
- );
80
- }
81
- });
82
-
83
- Deno.test('DataTable capability rotation cannot be shadowed by recovered fragment metadata', () => {
84
- const cachedDataTable = {
85
- id: 'table-a',
86
- realtimeToken: 'token-old',
87
- title: 'Recovered title',
88
- };
89
- const queryDataTable = {
90
- id: 'table-a',
91
- realtimeToken: 'token-new',
92
- title: 'Older query title',
93
- };
94
-
95
- assertEquals(getDataTableRealtimeFragmentValue(cachedDataTable), {
96
- id: 'table-a',
97
- title: 'Recovered title',
98
- });
99
- assertEquals(
100
- resolveDataTableRealtimeQueryResult(queryDataTable, cachedDataTable),
101
- {
102
- id: 'table-a',
103
- title: 'Recovered title',
104
- realtimeToken: 'token-new',
105
- },
106
- );
107
- assertEquals(resolveDataTableRealtimeQueryResult(null, cachedDataTable), {
108
- id: 'table-a',
109
- title: 'Recovered title',
110
- });
111
- });
112
-
113
- Deno.test('DataTable realtime subscriptions batch, ref-count, and reconnect exactly', () => {
114
- const registry = new DataTableRealtimeSubscriptionRegistry();
115
- const events: Array<{ eventName: string; tables: unknown[] }> = [];
116
- registry.setTransport((eventName, payload) => {
117
- events.push({ eventName, tables: payload.tables });
118
- });
119
- const unregisterA1 = registry.register([
120
- {
121
- organizationId: 'org-a',
122
- dataTableId: 'table-a',
123
- realtimeToken: 'token-a',
124
- },
125
- ]);
126
- const unregisterA2 = registry.register([
127
- {
128
- organizationId: 'org-a',
129
- dataTableId: 'table-a',
130
- realtimeToken: 'token-a',
131
- },
132
- ]);
133
- const unregisterB = registry.register([
134
- {
135
- organizationId: 'org-a',
136
- dataTableId: 'table-b',
137
- realtimeToken: 'token-b',
138
- },
139
- ]);
140
- registry.flushNow();
141
-
142
- assertEquals(events, [
143
- {
144
- eventName: WS_ACTIONS.data_table_watch,
145
- tables: [
146
- {
147
- dataTableId: 'table-a',
148
- lastSeq: '0',
149
- organizationId: 'org-a',
150
- realtimeToken: 'token-a',
151
- },
152
- {
153
- dataTableId: 'table-b',
154
- lastSeq: '0',
155
- organizationId: 'org-a',
156
- realtimeToken: 'token-b',
157
- },
158
- ],
159
- },
160
- ]);
161
-
162
- unregisterA1();
163
- registry.flushNow();
164
- assertEquals(events.length, 1);
165
- unregisterA2();
166
- registry.flushNow();
167
- assertEquals(events.slice(1), [
168
- {
169
- eventName: WS_ACTIONS.data_table_watch,
170
- tables: [
171
- {
172
- dataTableId: 'table-b',
173
- lastSeq: '0',
174
- organizationId: 'org-a',
175
- realtimeToken: 'token-b',
176
- },
177
- ],
178
- },
179
- ]);
180
-
181
- events.length = 0;
182
- registry.sync();
183
- registry.flushNow();
184
- assertEquals(events, [
185
- {
186
- eventName: WS_ACTIONS.data_table_watch,
187
- tables: [
188
- {
189
- dataTableId: 'table-b',
190
- lastSeq: '0',
191
- organizationId: 'org-a',
192
- realtimeToken: 'token-b',
193
- },
194
- ],
195
- },
196
- ]);
197
-
198
- events.length = 0;
199
- unregisterB();
200
- registry.flushNow();
201
- assertEquals(events, [
202
- {
203
- eventName: WS_ACTIONS.data_table_unwatch,
204
- tables: [],
205
- },
206
- ]);
207
- });
208
-
209
- Deno.test('DataTable direct cutover leaves every room when replacement intent empties before acknowledgement', () => {
210
- const registry = new DataTableRealtimeSubscriptionRegistry();
211
- const events: Array<{ eventName: string; tables: unknown[] }> = [];
212
- registry.setTransport((eventName, payload) => {
213
- events.push({ eventName, tables: payload.tables });
214
- });
215
- const unregisterA = registry.register([
216
- {
217
- organizationId: 'org-a',
218
- dataTableId: 'table-a',
219
- realtimeToken: 'token-a',
220
- },
221
- ]);
222
- registry.flushNow();
223
-
224
- unregisterA();
225
- const unregisterB = registry.register([
226
- {
227
- organizationId: 'org-a',
228
- dataTableId: 'table-b',
229
- realtimeToken: 'token-b',
230
- },
231
- ]);
232
- registry.flushNow();
233
- unregisterB();
234
- registry.flushNow();
235
-
236
- assertEquals(events.slice(1), [
237
- {
238
- eventName: WS_ACTIONS.data_table_watch,
239
- tables: [
240
- {
241
- dataTableId: 'table-b',
242
- lastSeq: '0',
243
- organizationId: 'org-a',
244
- realtimeToken: 'token-b',
245
- },
246
- ],
247
- },
248
- {
249
- eventName: WS_ACTIONS.data_table_unwatch,
250
- tables: [],
251
- },
252
- ]);
253
- });
254
-
255
- Deno.test('DataTable watch cursors are resolved at each actual subscription flush', () => {
256
- let cursor = '4';
257
- const registry = new DataTableRealtimeSubscriptionRegistry(() => cursor);
258
- const events: unknown[] = [];
259
- registry.setTransport((eventName, payload) => {
260
- events.push({ eventName, tables: payload.tables });
261
- });
262
- registry.register([
263
- {
264
- dataTableId: 'table-a',
265
- organizationId: 'org-a',
266
- realtimeToken: 'token-a',
267
- },
268
- ]);
269
- registry.flushNow();
270
- cursor = '5';
271
- registry.flushNow();
272
- registry.sync();
273
- registry.flushNow();
274
-
275
- assertEquals(events, [
276
- {
277
- eventName: WS_ACTIONS.data_table_watch,
278
- tables: [
279
- {
280
- dataTableId: 'table-a',
281
- lastSeq: '4',
282
- organizationId: 'org-a',
283
- realtimeToken: 'token-a',
284
- },
285
- ],
286
- },
287
- {
288
- eventName: WS_ACTIONS.data_table_watch,
289
- tables: [
290
- {
291
- dataTableId: 'table-a',
292
- lastSeq: '5',
293
- organizationId: 'org-a',
294
- realtimeToken: 'token-a',
295
- },
296
- ],
297
- },
298
- ]);
299
- });
300
-
301
- Deno.test('DataTable direct-cutover watch targets require exact canonical cursors', () => {
302
- const target = {
303
- dataTableId: 'table-a',
304
- lastSeq: '9007199254740993',
305
- organizationId: 'org-a',
306
- realtimeToken: 'token-a',
307
- };
308
- assertEquals(normalizeDataTableRealtimeWatchTargets([target]), [target]);
309
- assertEquals(
310
- buildDataTableRealtimeWatchTargets(
311
- [
312
- {
313
- dataTableId: 'table-a',
314
- organizationId: 'org-a',
315
- realtimeToken: 'token-a',
316
- },
317
- ],
318
- () => null,
319
- ),
320
- [
321
- {
322
- dataTableId: 'table-a',
323
- lastSeq: '0',
324
- organizationId: 'org-a',
325
- realtimeToken: 'token-a',
326
- },
327
- ],
328
- );
329
- assertEquals(
330
- normalizeDataTableRealtimeWatchTargets([{ ...target, lastSeq: '01' }]),
331
- null,
332
- );
333
- assertEquals(normalizeDataTableRealtimeWatchTargets([target, target]), null);
334
- assertEquals(
335
- normalizeDataTableRealtimeWatchTargets([{ ...target, legacy: true }]),
336
- null,
337
- );
338
- });
339
-
340
- Deno.test('DataTable watch acknowledgements accept only exact convergence statuses', () => {
341
- const acknowledgement = {
342
- errorCode: null,
343
- status: 'OK',
344
- tables: [
345
- {
346
- currentSeq: '12',
347
- dataTableId: 'table-a',
348
- organizationId: 'org-a',
349
- syncStatus: 'SNAPSHOT_REQUIRED',
350
- },
351
- {
352
- currentSeq: '4',
353
- dataTableId: 'table-b',
354
- organizationId: 'org-a',
355
- syncStatus: 'UP_TO_DATE',
356
- },
357
- ],
358
- watchId: 'watch-a',
359
- };
360
- assertEquals(
361
- normalizeDataTableRealtimeWatchAcknowledgement(acknowledgement),
362
- acknowledgement,
363
- );
364
- assertEquals(
365
- normalizeDataTableRealtimeWatchAcknowledgement({
366
- errorCode: 'AUTHORIZATION_FAILED',
367
- status: 'ERROR',
368
- tables: [],
369
- watchId: 'watch-b',
370
- }),
371
- {
372
- errorCode: 'AUTHORIZATION_FAILED',
373
- status: 'ERROR',
374
- tables: [],
375
- watchId: 'watch-b',
376
- },
377
- );
378
- assertEquals(
379
- normalizeDataTableRealtimeWatchAcknowledgement({
380
- ...acknowledgement,
381
- tables: [acknowledgement.tables[0], acknowledgement.tables[0]],
382
- }),
383
- null,
384
- );
385
- assertEquals(
386
- normalizeDataTableRealtimeWatchAcknowledgement({
387
- ...acknowledgement,
388
- tables: [
389
- {
390
- ...acknowledgement.tables[0],
391
- currentSeq: '01',
392
- },
393
- ],
394
- }),
395
- null,
396
- );
397
- assertEquals(
398
- normalizeDataTableRealtimeWatchAcknowledgement({
399
- ...acknowledgement,
400
- tables: [
401
- {
402
- ...acknowledgement.tables[0],
403
- syncStatus: 'REPLAY',
404
- },
405
- ],
406
- }),
407
- null,
408
- );
409
- assertEquals(
410
- normalizeDataTableRealtimeWatchAcknowledgement({
411
- ...acknowledgement,
412
- legacy: true,
413
- }),
414
- null,
415
- );
416
- });
417
-
418
- Deno.test('DataTable room readiness follows only exact correlated scope replacement', () => {
419
- const registry = new DataTableRealtimeSubscriptionRegistry();
420
- const tableA = { dataTableId: 'table-a', organizationId: 'org-a' };
421
- const tableB = { dataTableId: 'table-b', organizationId: 'org-a' };
422
- const unregisterA = registry.register([
423
- { ...tableA, realtimeToken: 'token-a' },
424
- ]);
425
- const unregisterB = registry.register([
426
- { ...tableB, realtimeToken: 'token-b' },
427
- ]);
428
- const observedA: boolean[] = [];
429
- const observedB: boolean[] = [];
430
- const unsubscribeA = registry.subscribeRoomReadiness(tableA, () => {
431
- observedA.push(registry.getRoomReadiness(tableA));
432
- });
433
- const unsubscribeB = registry.subscribeRoomReadiness(tableB, () => {
434
- observedB.push(registry.getRoomReadiness(tableB));
435
- });
436
-
437
- registry.replaceRoomReadiness([tableA, tableB]);
438
- assertEquals(registry.getRoomReadiness(tableA), true);
439
- assertEquals(registry.getRoomReadiness(tableB), true);
440
- registry.replaceRoomReadiness([tableB]);
441
- assertEquals(registry.getRoomReadiness(tableA), false);
442
- assertEquals(registry.getRoomReadiness(tableB), true);
443
- registry.replaceRoomReadiness([tableB]);
444
- assertEquals(observedA, [true, false]);
445
- assertEquals(observedB, [true]);
446
-
447
- registry.clearRoomReadiness();
448
- assertEquals(registry.getRoomReadiness(tableB), false);
449
- assertEquals(observedB, [true, false]);
450
- unregisterA();
451
- unregisterB();
452
- unsubscribeA();
453
- unsubscribeB();
454
- });
455
-
456
- Deno.test('DataTable membership intent revisions invalidate async ACK work immediately', () => {
457
- const registry = new DataTableRealtimeSubscriptionRegistry();
458
- const table = {
459
- dataTableId: 'table-a',
460
- organizationId: 'org-a',
461
- realtimeToken: 'token-a',
462
- };
463
- const unregister = registry.register([table]);
464
- const registeredRevision = registry.getMembershipIntentRevision();
465
-
466
- registry.setTransport(() => undefined);
467
- const connectedRevision = registry.getMembershipIntentRevision();
468
- registry.sync();
469
- const synchronizedRevision = registry.getMembershipIntentRevision();
470
- const unregisterRotated = registry.register([
471
- { ...table, realtimeToken: 'token-b' },
472
- ]);
473
- const rotatedRevision = registry.getMembershipIntentRevision();
474
- unregister();
475
- unregisterRotated();
476
- registry.setTransport(null);
477
-
478
- if (
479
- registeredRevision >= connectedRevision ||
480
- connectedRevision >= synchronizedRevision ||
481
- synchronizedRevision >= rotatedRevision
482
- ) {
483
- throw new Error(
484
- 'Each transport, sync, and token boundary must invalidate older async acknowledgements.',
485
- );
486
- }
487
- });
488
-
489
- Deno.test('DataTable readiness fails closed at pending sync, capability, unregister, and transport boundaries', () => {
490
- const registry = new DataTableRealtimeSubscriptionRegistry();
491
- const table = { dataTableId: 'table-a', organizationId: 'org-a' };
492
- registry.setTransport(() => {});
493
- const unregisterOld = registry.register([
494
- { ...table, realtimeToken: 'token-old' },
495
- ]);
496
- registry.flushNow();
497
- registry.replaceRoomReadiness([table]);
498
- assertEquals(registry.getRoomReadiness(table), true);
499
-
500
- registry.sync();
501
- assertEquals(registry.getRoomReadiness(table), false);
502
- registry.flushNow();
503
- registry.replaceRoomReadiness([table]);
504
- const unregisterNew = registry.register([
505
- { ...table, realtimeToken: 'token-new' },
506
- ]);
507
- assertEquals(registry.getRoomReadiness(table), false);
508
-
509
- registry.replaceRoomReadiness([table]);
510
- unregisterOld();
511
- assertEquals(registry.getRoomReadiness(table), true);
512
- unregisterNew();
513
- assertEquals(registry.getRoomReadiness(table), false);
514
- registry.setTransport(null);
515
- });
516
-
517
- Deno.test('singular DataTable query subscriptions require a signed capability and honor skip', () => {
518
- assertEquals(
519
- getDataTableRealtimeQuerySubscriptions('org-a', 'table-a', 'token-a'),
520
- [
521
- {
522
- organizationId: 'org-a',
523
- dataTableId: 'table-a',
524
- realtimeToken: 'token-a',
525
- },
526
- ],
527
- );
528
- assertEquals(
529
- getDataTableRealtimeQuerySubscriptions('org-a', 'table-a', 'token-a', true),
530
- [],
531
- );
532
- assertEquals(getDataTableRealtimeQuerySubscriptions('org-a', 'table-a'), []);
533
- assertEquals(
534
- getDataTableRealtimeQuerySubscriptions(null, 'table-a', 'token-a'),
535
- [],
536
- );
537
- });
538
-
539
- Deno.test('zero-subscription connect and focus sync emit no watch', () => {
540
- const registry = new DataTableRealtimeSubscriptionRegistry();
541
- const events: unknown[] = [];
542
- registry.setTransport((eventName, payload) =>
543
- events.push({ eventName, payload }),
544
- );
545
- registry.flushNow();
546
- registry.sync();
547
- registry.flushNow();
548
- registry.setTransport(null);
549
- assertEquals(events, []);
550
- });
551
-
552
- Deno.test('detaching a live transport leaves every joined DataTable room', () => {
553
- const registry = new DataTableRealtimeSubscriptionRegistry();
554
- const events: unknown[] = [];
555
- registry.setTransport((eventName, payload) =>
556
- events.push({ eventName, payload }),
557
- );
558
- registry.register([
559
- {
560
- dataTableId: 'table-a',
561
- organizationId: 'org-a',
562
- realtimeToken: 'token-a',
563
- },
564
- ]);
565
- registry.flushNow();
566
- events.length = 0;
567
-
568
- registry.setTransport(null);
569
-
570
- assertEquals(events, [
571
- {
572
- eventName: WS_ACTIONS.data_table_unwatch,
573
- payload: { tables: [] },
574
- },
575
- ]);
576
- });
577
-
578
- Deno.test('replacing a capability immediately replaces the exact DataTable room set', () => {
579
- const registry = new DataTableRealtimeSubscriptionRegistry();
580
- const events: Array<{ eventName: string; tables: unknown[] }> = [];
581
- registry.setTransport((eventName, payload) => {
582
- events.push({ eventName, tables: payload.tables });
583
- });
584
- const unregisterOld = registry.register([
585
- {
586
- dataTableId: 'table-a',
587
- organizationId: 'org-a',
588
- realtimeToken: 'token-old',
589
- },
590
- ]);
591
- registry.flushNow();
592
- events.length = 0;
593
-
594
- unregisterOld();
595
- const unregisterNew = registry.register([
596
- {
597
- dataTableId: 'table-a',
598
- organizationId: 'org-a',
599
- realtimeToken: 'token-new',
600
- },
601
- ]);
602
- registry.flushNow();
603
-
604
- assertEquals(events, [
605
- {
606
- eventName: WS_ACTIONS.data_table_watch,
607
- tables: [
608
- {
609
- dataTableId: 'table-a',
610
- lastSeq: '0',
611
- organizationId: 'org-a',
612
- realtimeToken: 'token-new',
613
- },
614
- ],
615
- },
616
- ]);
617
- unregisterNew();
618
- registry.flushNow();
619
- registry.setTransport(null);
620
- });
621
-
622
- Deno.test('DataTable capabilities renew before expiry and stop retrying after token replacement', async () => {
623
- const registry = new DataTableRealtimeSubscriptionRegistry();
624
- const nowSeconds = Math.floor(Date.now() / 1000);
625
- registry.setTransport(() => {});
626
- let unregister = registry.register([
627
- {
628
- dataTableId: 'table-a',
629
- organizationId: 'org-a',
630
- realtimeToken: createRealtimeCapabilityHint(nowSeconds + 30),
631
- },
632
- ]);
633
- let renewalCount = 0;
634
- const stopRenewal = registry.startCapabilityRenewal(
635
- () => {
636
- renewalCount += 1;
637
- unregister();
638
- unregister = registry.register([
639
- {
640
- dataTableId: 'table-a',
641
- organizationId: 'org-a',
642
- realtimeToken: createRealtimeCapabilityHint(nowSeconds + 3_600),
643
- },
644
- ]);
645
- },
646
- { leadMs: 60_000, retryMs: 1 },
647
- );
648
-
649
- await new Promise((resolve) => setTimeout(resolve, 5));
650
- assertEquals(renewalCount, 1);
651
- stopRenewal();
652
- unregister();
653
- registry.setTransport(null);
654
- });
655
-
656
- Deno.test('oversized exact DataTable subscription sets are surfaced without partial joins', () => {
657
- const registry = new DataTableRealtimeSubscriptionRegistry();
658
- const events: unknown[] = [];
659
- registry.setTransport((eventName, payload) => {
660
- events.push({ eventName, tableCount: payload.tables.length });
661
- });
662
- registry.register(
663
- Array.from(
664
- { length: DATA_TABLE_REALTIME_SUBSCRIPTION_MAX + 1 },
665
- (_value, index) => ({
666
- dataTableId: `table-${index}`,
667
- organizationId: 'org-a',
668
- realtimeToken: `token-${index}`,
669
- }),
670
- ),
671
- );
672
- registry.flushNow();
673
-
674
- assertEquals(events, [
675
- {
676
- eventName: WS_ACTIONS.data_table_watch,
677
- tableCount: DATA_TABLE_REALTIME_SUBSCRIPTION_MAX + 1,
678
- },
679
- ]);
680
- });
681
-
682
- Deno.test('exact DataTable watches count UTF-8 bytes and suppress oversized retries', () => {
683
- const ascii = [
684
- {
685
- dataTableId: 'table-a',
686
- lastSeq: '0',
687
- organizationId: 'org-a',
688
- realtimeToken: 'a'.repeat(22_000),
689
- },
690
- ];
691
- const unicode = [
692
- {
693
- dataTableId: 'table-a',
694
- lastSeq: '0',
695
- organizationId: 'org-a',
696
- realtimeToken: '界'.repeat(22_000),
697
- },
698
- ];
699
- assertEquals(dataTableRealtimeWatchPayloadIsWithinLimit(ascii), true);
700
- assertEquals(dataTableRealtimeWatchPayloadIsWithinLimit(unicode), false);
701
-
702
- const registry = new DataTableRealtimeSubscriptionRegistry();
703
- const events: unknown[] = [];
704
- registry.setTransport((eventName, payload) => {
705
- events.push({ eventName, payload });
706
- });
707
- registry.register(unicode);
708
- registry.flushNow();
709
- registry.sync();
710
- registry.flushNow();
711
- assertEquals(events, []);
712
- });
713
-
714
- Deno.test('exact DataTable watch budget reserves the transport-added maximum watch id', () => {
715
- const fullEnvelopeBaseBytes = getDataTableWatchPacketBytes(0, true);
716
- const exactTokenLength =
717
- DATA_TABLE_REALTIME_MAX_PAYLOAD_BYTES - fullEnvelopeBaseBytes;
718
-
719
- assertEquals(
720
- getDataTableWatchPacketBytes(exactTokenLength, true),
721
- DATA_TABLE_REALTIME_MAX_PAYLOAD_BYTES,
722
- );
723
- assertEquals(
724
- dataTableRealtimeWatchPayloadIsWithinLimit(
725
- buildBoundaryDataTableSubscriptions(exactTokenLength),
726
- ),
727
- true,
728
- );
729
- assertEquals(
730
- getDataTableWatchPacketBytes(exactTokenLength + 1, false) <=
731
- DATA_TABLE_REALTIME_MAX_PAYLOAD_BYTES,
732
- true,
733
- );
734
- assertEquals(
735
- dataTableRealtimeWatchPayloadIsWithinLimit(
736
- buildBoundaryDataTableSubscriptions(exactTokenLength + 1),
737
- ),
738
- false,
739
- );
740
- });