@jsb188/graphql 1.1.11 → 1.1.12

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 (165) hide show
  1. package/audience.d.ts +343 -0
  2. package/cache/config.ts +58 -24
  3. package/cache/index.chat-activity.test.ts +119 -0
  4. package/cache/index.chat.test.ts +92 -0
  5. package/cache/index.helpers.ts +96 -0
  6. package/cache/index.regression.test.ts +149 -0
  7. package/cache/index.test.ts +370 -42
  8. package/cache/index.ts +326 -799
  9. package/cache/optimistic-ui-runtime.ts +46 -0
  10. package/cache/optimistic-ui.ts +401 -0
  11. package/cache/persist.ts +98 -19
  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} +5 -2
  16. package/client/index.ts +3 -2
  17. package/client/request.helpers.ts +14 -0
  18. package/client/request.ts +130 -215
  19. package/deno.lock +17 -0
  20. package/deno.test.json +2 -0
  21. package/gql/fragments/agentMemoryFragments.ts +5 -1
  22. package/gql/fragments/agentNoteFragments.ts +150 -0
  23. package/gql/fragments/aiChatFragments.ts +36 -10
  24. package/gql/fragments/audienceFragments.ts +141 -0
  25. package/gql/fragments/campaignFragments.ts +415 -126
  26. package/gql/fragments/dataTableFragments.ts +48 -80
  27. package/gql/fragments/emailFragments.ts +28 -23
  28. package/gql/fragments/fieldDocFragments.ts +222 -40
  29. package/gql/fragments/inboundContactFragments.ts +2 -0
  30. package/gql/fragments/logFragments.ts +93 -2
  31. package/gql/fragments/organizationFragments.ts +10 -0
  32. package/gql/fragments/qrCodeFragments.ts +29 -0
  33. package/gql/fragments/scheduleFragments.ts +652 -140
  34. package/gql/fragments/sheetFragments.ts +84 -373
  35. package/gql/fragments/storageFragments.ts +25 -1
  36. package/gql/fragments/workflowFragments.ts +13 -1
  37. package/gql/mutations/agentMemoryMutations.ts +32 -0
  38. package/gql/mutations/agentNoteMutations.ts +62 -0
  39. package/gql/mutations/aiChatMutations.ts +36 -4
  40. package/gql/mutations/audienceMutations.ts +71 -0
  41. package/gql/mutations/authMutations.ts +45 -1
  42. package/gql/mutations/billingMutations.ts +24 -0
  43. package/gql/mutations/campaignMutations.ts +170 -127
  44. package/gql/mutations/contactMutations.ts +23 -0
  45. package/gql/mutations/dataTableMutations.ts +18 -75
  46. package/gql/mutations/fieldDocMutations.ts +162 -13
  47. package/gql/mutations/index.ts +8 -5
  48. package/gql/mutations/organizationMutations.ts +6 -0
  49. package/gql/mutations/qrCodeMutations.ts +20 -0
  50. package/gql/mutations/scheduleMutations.ts +1052 -354
  51. package/gql/mutations/sheetMutations.ts +62 -118
  52. package/gql/mutations/storageMutations.ts +0 -1
  53. package/gql/mutations/workflowMutations.ts +18 -0
  54. package/gql/queries/agentMemoryQueries.ts +6 -0
  55. package/gql/queries/agentNoteQueries.ts +89 -0
  56. package/gql/queries/aiChatQueries.ts +56 -4
  57. package/gql/queries/audienceQueries.ts +128 -0
  58. package/gql/queries/authQueries.ts +6 -0
  59. package/gql/queries/billingQueries.ts +95 -6
  60. package/gql/queries/campaignQueries.ts +132 -23
  61. package/gql/queries/dataTableQueries.ts +24 -65
  62. package/gql/queries/emailQueries.ts +18 -61
  63. package/gql/queries/emailReplyReportQueries.ts +76 -0
  64. package/gql/queries/fieldDocQueries.ts +100 -92
  65. package/gql/queries/inboundContactQueries.ts +4 -0
  66. package/gql/queries/index.ts +3 -1
  67. package/gql/queries/logQueries.ts +62 -7
  68. package/gql/queries/organizationQueries.ts +13 -0
  69. package/gql/queries/qrCodeQueries.ts +20 -0
  70. package/gql/queries/scheduleQueries.ts +633 -257
  71. package/gql/queries/sheetQueries.ts +59 -152
  72. package/gql/queries/workflowQueries.ts +10 -4
  73. package/hooks/index.helpers.test.ts +50 -0
  74. package/hooks/index.helpers.ts +52 -0
  75. package/hooks/index.ts +863 -685
  76. package/hooks/use-agentMemory-mtn.ts +29 -31
  77. package/hooks/use-agentMemory-qry.ts +15 -3
  78. package/hooks/use-agentNote-mtn.ts +82 -0
  79. package/hooks/use-agentNote-qry.ts +190 -0
  80. package/hooks/use-agentNote-recovery.helpers.test.ts +68 -0
  81. package/hooks/use-agentNote-recovery.helpers.ts +58 -0
  82. package/hooks/use-agentNote-recovery.ts +72 -0
  83. package/hooks/use-aiChat-mtn.ts +96 -112
  84. package/hooks/use-aiChat-qry.helpers.test.ts +63 -0
  85. package/hooks/use-aiChat-qry.helpers.ts +124 -0
  86. package/hooks/use-aiChat-qry.ts +229 -383
  87. package/hooks/use-aiChat-stream-mtn.ts +14 -0
  88. package/hooks/use-audience-mtn.ts +194 -0
  89. package/hooks/use-audience-qry.helpers.test.ts +80 -0
  90. package/hooks/use-audience-qry.helpers.ts +63 -0
  91. package/hooks/use-audience-qry.ts +330 -0
  92. package/hooks/use-auth-mtn.ts +30 -1
  93. package/hooks/use-billing-mtn.ts +31 -0
  94. package/hooks/use-billing-qry.ts +76 -0
  95. package/hooks/use-campaign-generation-recovery.ts +53 -0
  96. package/hooks/use-campaign-mtn.ts +224 -165
  97. package/hooks/use-campaign-qry.helpers.ts +40 -0
  98. package/hooks/use-campaign-qry.ts +631 -54
  99. package/hooks/use-dataTable-mtn.ts +23 -115
  100. package/hooks/use-dataTable-qry.ts +66 -916
  101. package/hooks/use-email-qry.helpers.ts +37 -0
  102. package/hooks/use-email-qry.ts +33 -178
  103. package/hooks/use-emailCampaignReplyReport-qry.ts +27 -0
  104. package/hooks/use-fieldDoc-mtn.ts +244 -32
  105. package/hooks/use-fieldDoc-qry.helpers.test.ts +65 -0
  106. package/hooks/use-fieldDoc-qry.helpers.ts +49 -0
  107. package/hooks/use-fieldDoc-qry.ts +134 -102
  108. package/hooks/use-inboundContact-qry.ts +7 -9
  109. package/hooks/use-log-mtn.ts +25 -12
  110. package/hooks/use-log-qry.ts +99 -46
  111. package/hooks/use-organization-qry.ts +16 -60
  112. package/hooks/use-publicFormEmail-mtn.ts +27 -0
  113. package/hooks/use-qrCode-mtn.ts +40 -0
  114. package/hooks/use-qrCode-qry.ts +78 -0
  115. package/hooks/use-schedule-mtn.ts +506 -333
  116. package/hooks/use-schedule-qry.ts +682 -101
  117. package/hooks/use-schedule-reactive.ts +486 -0
  118. package/hooks/use-sheet-mtn.ts +91 -199
  119. package/hooks/use-sheet-qry.ts +84 -238
  120. package/hooks/use-storage-qry.ts +84 -61
  121. package/hooks/use-workflow-mtn.ts +39 -22
  122. package/hooks/use-workflow-qry.ts +40 -61
  123. package/libs/ai-chat-outgoing.helpers.ts +153 -0
  124. package/libs/ai-chat-outgoing.ts +11 -0
  125. package/libs/ai-chat-outgoing.types.ts +25 -0
  126. package/libs/ai-chat-run-realtime.ts +20 -0
  127. package/libs/ai-chat-submission-realtime.ts +150 -0
  128. package/libs/fragment-realtime-sequence.ts +55 -0
  129. package/libs/fragment-recovery.test.ts +326 -0
  130. package/libs/fragment-recovery.ts +325 -0
  131. package/libs/open-sheet.ts +138 -0
  132. package/libs/orchestrator-realtime.helpers.test.ts +229 -0
  133. package/libs/orchestrator-realtime.helpers.ts +317 -0
  134. package/libs/orchestrator-realtime.ts +433 -0
  135. package/libs/request-cache-regression.test.ts +122 -0
  136. package/{client → libs}/request-managed-session.test.ts +1 -1
  137. package/libs/request-no-store.test.ts +419 -0
  138. package/libs/sheet-command-batch.ts +44 -0
  139. package/libs/sheet-view-realtime-router.test.ts +214 -0
  140. package/libs/sheet-view-realtime-router.ts +99 -0
  141. package/loaders/campaign.test.ts +186 -0
  142. package/loaders/campaign.ts +56 -83
  143. package/loaders/emailPreference.ts +75 -0
  144. package/loaders/rest.ts +12 -0
  145. package/package.json +42 -17
  146. package/schedule.d.ts +1790 -0
  147. package/sse.helpers.ts +19 -0
  148. package/sse.ts +26 -115
  149. package/tests/agent-note-detail-cache.test.ts +120 -0
  150. package/tests/agent-note-search.test.ts +444 -0
  151. package/types.d.ts +46 -25
  152. package/client/data-table-realtime-subscriptions.test.ts +0 -740
  153. package/client/data-table-realtime-subscriptions.ts +0 -955
  154. package/client/data-table-realtime.test.ts +0 -792
  155. package/client/data-table-realtime.ts +0 -1218
  156. package/client/fragment-realtime-sequence.ts +0 -52
  157. package/client/open-sheet.ts +0 -137
  158. package/client/request-no-store.test.ts +0 -157
  159. package/gql/fragments/agentDocFragments.ts +0 -46
  160. package/gql/fragments/billingFragments.ts +0 -14
  161. package/gql/mutations/agentDocMutations.ts +0 -177
  162. package/gql/queries/agentDocQueries.ts +0 -120
  163. package/hooks/use-agentDoc-mtn.ts +0 -285
  164. package/hooks/use-agentDoc-qry.ts +0 -208
  165. /package/{client → libs}/sse-ticket.test.ts +0 -0
@@ -1,19 +1,20 @@
1
- import { makeVariablesKey } from '@jsb188/app/utils/logic.ts';
2
- import { makeQueryResetTarget } from '@jsb188/app/utils/queryReset.ts';
1
+ import { makeGraphQLVariablesKey } from '@jsb188/app/utils/logic.ts';
2
+ import { getQueryResetTargetId, makeQueryResetTarget } from '@jsb188/app/utils/queryReset.ts';
3
3
  import {
4
4
  addFragmentToCache,
5
5
  clearGraphQLClientCache,
6
6
  fetchCachedData,
7
- loadDataFromCache,
8
- loadFragment,
9
7
  getNextQueryLastUpdatedCount,
10
8
  getQueryRefreshTime,
9
+ loadDataFromCache,
10
+ loadFragment,
11
11
  resetQuery,
12
12
  resetQueryByTarget,
13
13
  setDataToCache,
14
14
  updateFragment,
15
15
  updateQuery,
16
16
  } from './index.ts';
17
+ import { beginOptimisticUITransaction } from './optimistic-ui.ts';
17
18
 
18
19
  if (typeof (console as any).dev !== 'function') {
19
20
  (console as any).dev = () => {};
@@ -147,6 +148,58 @@ function resetCacheState() {
147
148
  clearGraphQLClientCache();
148
149
  }
149
150
 
151
+ /**
152
+ * Verify a shared realtime revision cannot produce a mixed editor session
153
+ * whose core is newer than its editor body.
154
+ */
155
+
156
+ Deno.test('Editor sessions retain one atomic query snapshot', () => {
157
+ resetCacheState();
158
+
159
+ // Use a cacheable test-only root name because production edit sessions are
160
+ // intentionally uncached. The nested shape mirrors campaignTemplateEditSession:
161
+ // an inline core selection (no fragment spread) plus the editor body.
162
+ const cachedSessionQuery = makeQuery('cachedCampaignTemplateEditSession', [
163
+ makeField('organizationId'),
164
+ makeField('template', [
165
+ makeField('id'),
166
+ makeField('organizationId'),
167
+ makeField('editVersion'),
168
+ makeField('title'),
169
+ makeField('updatedAt'),
170
+ ]),
171
+ makeField('editor', [makeField('content')]),
172
+ makeField('editable'),
173
+ ]);
174
+ const session = {
175
+ organizationId: 'organization-1',
176
+ template: {
177
+ id: 'campaign-template-1',
178
+ organizationId: 'organization-1',
179
+ editVersion: 1,
180
+ title: 'Original title',
181
+ updatedAt: '2026-08-06T00:00:00.000Z',
182
+ },
183
+ editor: { content: '{"version":1,"type":"doc","content":[]}' },
184
+ editable: true,
185
+ };
186
+
187
+ setDataToCache(
188
+ { cachedCampaignTemplateEditSession: session },
189
+ cachedSessionQuery,
190
+ );
191
+ addFragmentToCache('$campaignTemplateFragment:campaign-template-1', {
192
+ id: 'campaign-template-1',
193
+ editVersion: 2,
194
+ });
195
+
196
+ assertDeepEqual(
197
+ fetchCachedData(cachedSessionQuery, makeGraphQLVariablesKey(undefined)),
198
+ { cachedCampaignTemplateEditSession: session },
199
+ 'edit-session core and editor body should remain the original atomic snapshot',
200
+ );
201
+ });
202
+
150
203
  Deno.test('loadFragment rebuilds nested list items without inner fragments', () => {
151
204
  resetCacheState();
152
205
 
@@ -214,11 +267,7 @@ Deno.test('loadDataFromCache preserves mixed list items with fragment refs and i
214
267
 
215
268
  setDataToCache(requestData, gqlQuery);
216
269
 
217
- const cachedData = loadDataFromCache(
218
- {},
219
- gqlQuery,
220
- makeVariablesKey(undefined),
221
- );
270
+ const cachedData = loadDataFromCache({}, gqlQuery, makeGraphQLVariablesKey(undefined));
222
271
 
223
272
  assertDeepEqual(
224
273
  cachedData,
@@ -279,7 +328,7 @@ Deno.test('loadFragment uses newest spread list after same fragment id mutation'
279
328
  'fragment cache should rebuild same-id Sheet with newest nested tabs spread list',
280
329
  );
281
330
  assertDeepEqual(
282
- loadDataFromCache({}, sheetQuery, makeVariablesKey(sheetVariables)),
331
+ loadDataFromCache({}, sheetQuery, makeGraphQLVariablesKey(sheetVariables)),
283
332
  { workbookSnapshot: updatedSheet },
284
333
  'query cache should rebuild same-id Sheet with newest nested tabs spread list',
285
334
  );
@@ -294,7 +343,6 @@ Deno.test('SheetView fragment patches preserve null state fields and clear volat
294
343
  axisMoveOps: [{ commandId: 'move-1' }],
295
344
  id: 'sheet-view-1',
296
345
  originClientId: 'old-client',
297
- ranges: [{ id: 'range-1' }],
298
346
  sheetTab: { id: 'tab-1', title: 'Tab 1' },
299
347
  structureOps: [{ index: 2, operation: 'INSERT_ROW' }],
300
348
  },
@@ -309,7 +357,6 @@ Deno.test('SheetView fragment patches preserve null state fields and clear volat
309
357
  axisMoveOps: null,
310
358
  id: 'sheet-view-1',
311
359
  originClientId: null,
312
- ranges: null,
313
360
  sheetTab: null,
314
361
  structureOps: null,
315
362
  },
@@ -325,7 +372,6 @@ Deno.test('SheetView fragment patches preserve null state fields and clear volat
325
372
  axisMoveOps: null,
326
373
  id: 'sheet-view-1',
327
374
  originClientId: null,
328
- ranges: [{ id: 'range-1' }],
329
375
  sheetTab: { id: 'tab-1', title: 'Tab 1' },
330
376
  structureOps: null,
331
377
  },
@@ -355,13 +401,13 @@ Deno.test('updateFragment callback receives cleaned optimistic array fields', ()
355
401
  id: 'temp-tab-1',
356
402
  sheetId: 'sheet-optimistic-callback',
357
403
  name: 'saving',
358
- __optimisticUI: true,
404
+ __optimisticPlaceholder: true,
359
405
  };
360
406
  const nextOptimisticTab = {
361
407
  id: 'temp-tab-2',
362
408
  sheetId: 'sheet-optimistic-callback',
363
409
  name: 'still_saving',
364
- __optimisticUI: true,
410
+ __optimisticPlaceholder: true,
365
411
  };
366
412
  let callbackTabs: any[] = [];
367
413
 
@@ -395,7 +441,7 @@ Deno.test('updateFragment callback receives cleaned optimistic array fields', ()
395
441
  );
396
442
  });
397
443
 
398
- Deno.test('updateFragment direct object preserves current optimistic array objects', () => {
444
+ Deno.test('updateFragment direct object preserves current optimistic placeholders', () => {
399
445
  resetCacheState();
400
446
 
401
447
  const sheetQuery = makeQuery('workbookSnapshot', [
@@ -417,7 +463,7 @@ Deno.test('updateFragment direct object preserves current optimistic array objec
417
463
  id: 'temp-tab',
418
464
  sheetId: 'sheet-optimistic-direct',
419
465
  name: 'saving',
420
- __optimisticUI: true,
466
+ __optimisticPlaceholder: true,
421
467
  };
422
468
 
423
469
  setDataToCache({ workbookSnapshot: sheet }, sheetQuery);
@@ -442,7 +488,7 @@ Deno.test('updateFragment direct object preserves current optimistic array objec
442
488
  );
443
489
  });
444
490
 
445
- Deno.test('authoritative fragment writes remove or replace optimistic array objects by id', () => {
491
+ Deno.test('authoritative fragment writes remove or replace optimistic placeholders by id', () => {
446
492
  resetCacheState();
447
493
 
448
494
  const sheetQuery = makeQuery('workbookSnapshot', [
@@ -465,13 +511,13 @@ Deno.test('authoritative fragment writes remove or replace optimistic array obje
465
511
  id: 'tab-2',
466
512
  sheetId: 'sheet-optimistic-authoritative',
467
513
  name: 'saving_tab_2',
468
- __optimisticUI: true,
514
+ __optimisticPlaceholder: true,
469
515
  };
470
516
  const optimisticUnmatchedTab = {
471
517
  id: 'temp-tab',
472
518
  sheetId: 'sheet-optimistic-authoritative',
473
519
  name: 'saving_temp',
474
- __optimisticUI: true,
520
+ __optimisticPlaceholder: true,
475
521
  };
476
522
  const realSheet = {
477
523
  ...sheet,
@@ -508,12 +554,299 @@ Deno.test('authoritative fragment writes remove or replace optimistic array obje
508
554
  'real spread-backed tabs should replace matching optimistic tabs and remove unmatched optimistic tabs',
509
555
  );
510
556
  assertDeepEqual(
511
- loadDataFromCache({}, sheetQuery, makeVariablesKey(sheetVariables)),
557
+ loadDataFromCache({}, sheetQuery, makeGraphQLVariablesKey(sheetVariables)),
512
558
  { workbookSnapshot: realSheet },
513
559
  'query cache should also return authoritative tabs without optimistic duplicates',
514
560
  );
515
561
  });
516
562
 
563
+ Deno.test('optimistic UI updates multiple fragments and rolls them back together', () => {
564
+ resetCacheState();
565
+
566
+ const logFragmentKey = '$logEntryFragment:optimistic-log';
567
+ const summaryFragmentKey = '$logSummaryFragment:optimistic-summary';
568
+ addFragmentToCache(logFragmentKey, {
569
+ flagColor: null,
570
+ id: 'optimistic-log',
571
+ summary: 'Original',
572
+ });
573
+ addFragmentToCache(summaryFragmentKey, {
574
+ flaggedCount: 0,
575
+ id: 'optimistic-summary',
576
+ });
577
+ const observerUpdates: any[] = [];
578
+ const transaction = beginOptimisticUITransaction(
579
+ [
580
+ {
581
+ action: 'update',
582
+ fragmentKey: logFragmentKey,
583
+ update: { flagColor: 'RED' },
584
+ },
585
+ {
586
+ action: 'update',
587
+ fragmentKey: summaryFragmentKey,
588
+ update: (summary: any) => ({
589
+ flaggedCount: summary.flaggedCount + 1,
590
+ }),
591
+ },
592
+ ],
593
+ (update) => observerUpdates.push(update),
594
+ );
595
+
596
+ assertDeepEqual(
597
+ loadFragment(logFragmentKey),
598
+ {
599
+ flagColor: 'RED',
600
+ id: 'optimistic-log',
601
+ summary: 'Original',
602
+ },
603
+ 'optimistic UI should patch the first fragment immediately',
604
+ );
605
+ assertDeepEqual(
606
+ loadFragment(summaryFragmentKey),
607
+ { flaggedCount: 1, id: 'optimistic-summary' },
608
+ 'optimistic UI should patch the second fragment immediately',
609
+ );
610
+ assertDeepEqual(
611
+ observerUpdates,
612
+ [{ fragmentIds: [logFragmentKey, summaryFragmentKey] }],
613
+ 'optimistic UI should notify all affected fragments together',
614
+ );
615
+
616
+ transaction.transaction?.rollback();
617
+
618
+ assertDeepEqual(
619
+ loadFragment(logFragmentKey),
620
+ {
621
+ flagColor: null,
622
+ id: 'optimistic-log',
623
+ summary: 'Original',
624
+ },
625
+ 'rollback should restore the first fragment',
626
+ );
627
+ assertDeepEqual(
628
+ loadFragment(summaryFragmentKey),
629
+ { flaggedCount: 0, id: 'optimistic-summary' },
630
+ 'rollback should restore the second fragment',
631
+ );
632
+ assertDeepEqual(
633
+ observerUpdates,
634
+ [
635
+ { fragmentIds: [logFragmentKey, summaryFragmentKey] },
636
+ { fragmentIds: [logFragmentKey, summaryFragmentKey] },
637
+ ],
638
+ 'rollback should notify restored fragments together',
639
+ );
640
+ });
641
+
642
+ Deno.test('optimistic UI creates marked placeholders without mutating caller data', () => {
643
+ resetCacheState();
644
+
645
+ const sheetFragmentKey = '$sheetFragment:optimistic-create';
646
+ const placeholderData = {
647
+ id: 'temporary-tab',
648
+ name: 'Saving',
649
+ };
650
+ addFragmentToCache(sheetFragmentKey, {
651
+ id: 'optimistic-create',
652
+ tabs: [{ id: 'tab-1', name: 'First' }],
653
+ });
654
+ const transaction = beginOptimisticUITransaction(
655
+ {
656
+ action: 'create-placeholder',
657
+ parentFragmentKey: sheetFragmentKey,
658
+ field: 'tabs',
659
+ data: placeholderData,
660
+ position: 'end',
661
+ },
662
+ () => undefined,
663
+ );
664
+ const optimisticTabs = loadFragment(sheetFragmentKey)?.tabs;
665
+
666
+ assertDeepEqual(
667
+ {
668
+ callerData: placeholderData,
669
+ placeholder: {
670
+ id: optimisticTabs?.[1]?.id,
671
+ isOptimisticPlaceholder: optimisticTabs?.[1]?.__optimisticPlaceholder,
672
+ name: optimisticTabs?.[1]?.name,
673
+ transactionTokenType:
674
+ typeof optimisticTabs?.[1]?.__optimisticTransactionId,
675
+ },
676
+ },
677
+ {
678
+ callerData: { id: 'temporary-tab', name: 'Saving' },
679
+ placeholder: {
680
+ id: 'temporary-tab',
681
+ isOptimisticPlaceholder: true,
682
+ name: 'Saving',
683
+ transactionTokenType: 'string',
684
+ },
685
+ },
686
+ 'placeholder creation should add internal markers only to cached data',
687
+ );
688
+
689
+ addFragmentToCache(sheetFragmentKey, {
690
+ id: 'optimistic-create',
691
+ tabs: [
692
+ { id: 'tab-1', name: 'First' },
693
+ { id: 'tab-2', name: 'Created' },
694
+ ],
695
+ });
696
+ transaction.transaction?.commit();
697
+
698
+ assertDeepEqual(
699
+ loadFragment(sheetFragmentKey)?.tabs,
700
+ [
701
+ { id: 'tab-1', name: 'First' },
702
+ { id: 'tab-2', name: 'Created' },
703
+ ],
704
+ 'authoritative collection data should survive placeholder settlement',
705
+ );
706
+ });
707
+
708
+ Deno.test('optimistic UI rollback preserves newer authoritative values', () => {
709
+ resetCacheState();
710
+
711
+ const fragmentKey = '$logEntryFragment:optimistic-authority';
712
+ addFragmentToCache(fragmentKey, {
713
+ flagColor: null,
714
+ id: 'optimistic-authority',
715
+ });
716
+ const transaction = beginOptimisticUITransaction(
717
+ {
718
+ action: 'update',
719
+ fragmentKey,
720
+ update: { flagColor: 'RED' },
721
+ },
722
+ () => undefined,
723
+ );
724
+
725
+ addFragmentToCache(fragmentKey, {
726
+ flagColor: 'BLUE',
727
+ id: 'optimistic-authority',
728
+ });
729
+ transaction.transaction?.rollback();
730
+
731
+ assertDeepEqual(
732
+ loadFragment(fragmentKey),
733
+ { flagColor: 'BLUE', id: 'optimistic-authority' },
734
+ 'a late rollback should not overwrite a newer authoritative value',
735
+ );
736
+ });
737
+
738
+ Deno.test('optimistic UI rejects a second transaction for the same fragment', () => {
739
+ resetCacheState();
740
+
741
+ const fragmentKey = '$logEntryFragment:optimistic-conflict';
742
+ addFragmentToCache(fragmentKey, {
743
+ flagColor: null,
744
+ id: 'optimistic-conflict',
745
+ });
746
+ const firstTransaction = beginOptimisticUITransaction(
747
+ {
748
+ action: 'update',
749
+ fragmentKey,
750
+ update: { flagColor: 'RED' },
751
+ },
752
+ () => undefined,
753
+ );
754
+ const secondTransaction = beginOptimisticUITransaction(
755
+ {
756
+ action: 'update',
757
+ fragmentKey,
758
+ update: { flagColor: 'BLUE' },
759
+ },
760
+ () => undefined,
761
+ );
762
+
763
+ assertDeepEqual(
764
+ {
765
+ flagColor: loadFragment(fragmentKey)?.flagColor,
766
+ firstStatus: firstTransaction.status,
767
+ secondStatus: secondTransaction.status,
768
+ },
769
+ {
770
+ flagColor: 'RED',
771
+ firstStatus: 'applied',
772
+ secondStatus: 'conflict',
773
+ },
774
+ 'a same-fragment transaction should be rejected before it can overlap',
775
+ );
776
+
777
+ firstTransaction.transaction?.rollback();
778
+ });
779
+
780
+ Deno.test('optimistic UI rolls back collection updates before removing placeholders', () => {
781
+ resetCacheState();
782
+
783
+ const fragmentKey = '$sheetFragment:optimistic-mixed-collection';
784
+ addFragmentToCache(fragmentKey, {
785
+ id: 'optimistic-mixed-collection',
786
+ tabs: [{ id: 'tab-1', name: 'First' }],
787
+ });
788
+ const transaction = beginOptimisticUITransaction(
789
+ [
790
+ {
791
+ action: 'update',
792
+ fragmentKey,
793
+ update: (sheet: any) => ({
794
+ tabs: [...sheet.tabs, { id: 'tab-2', name: 'Second' }],
795
+ }),
796
+ },
797
+ {
798
+ action: 'create-placeholder',
799
+ parentFragmentKey: fragmentKey,
800
+ field: 'tabs',
801
+ data: { id: 'temporary-tab', name: 'Saving' },
802
+ },
803
+ ],
804
+ () => undefined,
805
+ );
806
+
807
+ transaction.transaction?.rollback();
808
+
809
+ assertDeepEqual(
810
+ loadFragment(fragmentKey)?.tabs,
811
+ [{ id: 'tab-1', name: 'First' }],
812
+ 'rollback should restore a collection update after removing its placeholder',
813
+ );
814
+ });
815
+
816
+ Deno.test('optimistic placeholder creation requires an existing collection', () => {
817
+ resetCacheState();
818
+
819
+ const fragmentKey = '$sheetFragment:optimistic-missing-collection';
820
+ addFragmentToCache(fragmentKey, {
821
+ id: 'optimistic-missing-collection',
822
+ tabs: null,
823
+ });
824
+ const transaction = beginOptimisticUITransaction(
825
+ {
826
+ action: 'create-placeholder',
827
+ parentFragmentKey: fragmentKey,
828
+ field: 'tabs',
829
+ data: { id: 'temporary-tab', name: 'Saving' },
830
+ },
831
+ () => undefined,
832
+ );
833
+
834
+ assertDeepEqual(
835
+ {
836
+ fragment: loadFragment(fragmentKey),
837
+ status: transaction.status,
838
+ },
839
+ {
840
+ fragment: {
841
+ id: 'optimistic-missing-collection',
842
+ tabs: null,
843
+ },
844
+ status: 'noop',
845
+ },
846
+ 'placeholder creation should be a no-op when the collection is not cached',
847
+ );
848
+ });
849
+
517
850
  Deno.test('fetchCachedData rejects cached query data missing newly selected nested fields', () => {
518
851
  resetCacheState();
519
852
 
@@ -543,11 +876,7 @@ Deno.test('fetchCachedData rejects cached query data missing newly selected nest
543
876
 
544
877
  setDataToCache(requestData, oldQuery);
545
878
 
546
- const cachedData = fetchCachedData(
547
- newQuery,
548
- makeVariablesKey(undefined),
549
- () => {},
550
- );
879
+ const cachedData = fetchCachedData(newQuery, makeGraphQLVariablesKey(undefined), () => {});
551
880
 
552
881
  assertDeepEqual(
553
882
  cachedData,
@@ -600,11 +929,7 @@ Deno.test('fetchCachedData ignores nonmatching inline fragments for union data',
600
929
 
601
930
  setDataToCache(requestData, gqlQuery);
602
931
 
603
- const cachedData = fetchCachedData(
604
- gqlQuery,
605
- makeVariablesKey(undefined),
606
- () => {},
607
- );
932
+ const cachedData = fetchCachedData(gqlQuery, makeGraphQLVariablesKey(undefined), () => {});
608
933
 
609
934
  assertDeepEqual(
610
935
  cachedData,
@@ -616,11 +941,8 @@ Deno.test('fetchCachedData ignores nonmatching inline fragments for union data',
616
941
  Deno.test('resetQuery reset-only clears matching query cache and notifies observers without force refetch', () => {
617
942
  resetCacheState();
618
943
 
619
- const gqlQuery = makeQuery('logEntries', [
620
- makeField('id'),
621
- makeField('status'),
622
- ]);
623
- const variablesKey = makeVariablesKey({
944
+ const gqlQuery = makeQuery('logEntries', [makeField('id'), makeField('status')]);
945
+ const variablesKey = makeGraphQLVariablesKey({
624
946
  organizationId: 'org-1',
625
947
  });
626
948
  const requestData = {
@@ -713,8 +1035,8 @@ Deno.test('structured reset force-refetch marks only matching scoped query varia
713
1035
  sheetId: 'sheet-1',
714
1036
  sheetTabId: 'tab-2',
715
1037
  };
716
- const matchingVariablesKey = makeVariablesKey(matchingVariables);
717
- const otherTabVariablesKey = makeVariablesKey(otherTabVariables);
1038
+ const matchingVariablesKey = makeGraphQLVariablesKey(matchingVariables);
1039
+ const otherTabVariablesKey = makeGraphQLVariablesKey(otherTabVariables);
718
1040
  const observerArgs: any[] = [];
719
1041
 
720
1042
  setDataToCache(
@@ -752,7 +1074,13 @@ Deno.test('structured reset force-refetch marks only matching scoped query varia
752
1074
  observerArgs,
753
1075
  [
754
1076
  {
755
- queryId: `#tabScopedSnapshot:${matchingVariablesKey}`,
1077
+ queryId: getQueryResetTargetId(
1078
+ makeQueryResetTarget('tabScopedSnapshot', matchingVariables),
1079
+ ),
1080
+ queryTarget: makeQueryResetTarget(
1081
+ 'tabScopedSnapshot',
1082
+ matchingVariables,
1083
+ ),
756
1084
  forceRefetch: true,
757
1085
  },
758
1086
  ],
@@ -792,7 +1120,7 @@ Deno.test('root field aliases share the operation response cache key', () => {
792
1120
  );
793
1121
 
794
1122
  assertDeepEqual(
795
- fetchCachedData(gqlQuery, makeVariablesKey(variables)),
1123
+ fetchCachedData(gqlQuery, makeGraphQLVariablesKey(variables)),
796
1124
  {
797
1125
  rowsSnapshotAlias: {
798
1126
  dataTableId: 'table-1',
@@ -856,7 +1184,7 @@ Deno.test('appended query list survives a stale flat fragment spread copy', () =
856
1184
  // mutation and the realtime QUERY_APPEND event
857
1185
  addFragmentToCache('$aiChatMessageFragment:msg-3', appendedMessage);
858
1186
  updateQuery(
859
- `#aiChat:${makeVariablesKey(chatVariables)}`,
1187
+ `#aiChat:${makeGraphQLVariablesKey(chatVariables)}`,
860
1188
  (currentData: any) => ({
861
1189
  ...currentData,
862
1190
  messages: {
@@ -868,7 +1196,7 @@ Deno.test('appended query list survives a stale flat fragment spread copy', () =
868
1196
  );
869
1197
 
870
1198
  assertDeepEqual(
871
- loadDataFromCache({}, aiChatQuery, makeVariablesKey(chatVariables)),
1199
+ loadDataFromCache({}, aiChatQuery, makeGraphQLVariablesKey(chatVariables)),
872
1200
  {
873
1201
  aiChat: {
874
1202
  id: 'chat-1',