@flamingo-stack/openframe-frontend-core 0.0.598 → 0.0.599-2039.5716.1

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 (204) hide show
  1. package/dist/chat-protocol/index.cjs +8 -1
  2. package/dist/chat-protocol/index.cjs.map +1 -1
  3. package/dist/chat-protocol/index.d.ts +1 -0
  4. package/dist/chat-protocol/index.d.ts.map +1 -1
  5. package/dist/chat-protocol/index.js +7 -2
  6. package/dist/chat-protocol/index.js.map +1 -1
  7. package/dist/chat-protocol/nats-decoder.d.ts.map +1 -1
  8. package/dist/chat-protocol/wire-narrow.d.ts +17 -0
  9. package/dist/chat-protocol/wire-narrow.d.ts.map +1 -0
  10. package/dist/{chunk-VLJTJAHJ.cjs → chunk-2B4HSYAX.cjs} +25 -25
  11. package/dist/{chunk-VLJTJAHJ.cjs.map → chunk-2B4HSYAX.cjs.map} +1 -1
  12. package/dist/{chunk-UE7OFLLS.cjs → chunk-2GCBN73M.cjs} +14 -14
  13. package/dist/{chunk-UE7OFLLS.cjs.map → chunk-2GCBN73M.cjs.map} +1 -1
  14. package/dist/{chunk-STYNZ4QM.cjs → chunk-3IXDZI3R.cjs} +3 -3
  15. package/dist/{chunk-STYNZ4QM.cjs.map → chunk-3IXDZI3R.cjs.map} +1 -1
  16. package/dist/{chunk-DAPOBLAK.cjs → chunk-3RWSKUIN.cjs} +39 -39
  17. package/dist/{chunk-DAPOBLAK.cjs.map → chunk-3RWSKUIN.cjs.map} +1 -1
  18. package/dist/{chunk-VNSRNVPD.js → chunk-4HJYXP5O.js} +722 -406
  19. package/dist/chunk-4HJYXP5O.js.map +1 -0
  20. package/dist/{chunk-HWAYIK4U.cjs → chunk-4MOY5TQR.cjs} +22 -22
  21. package/dist/{chunk-HWAYIK4U.cjs.map → chunk-4MOY5TQR.cjs.map} +1 -1
  22. package/dist/{chunk-VIRTMRS4.js → chunk-4PBUEYAE.js} +7 -7
  23. package/dist/{chunk-TZPVRWPU.js → chunk-4TG5BWPE.js} +5 -5
  24. package/dist/{chunk-PTK3CINU.js → chunk-6JF65LRQ.js} +5 -5
  25. package/dist/{chunk-YWBAIFI2.cjs → chunk-6XISE63Q.cjs} +105 -105
  26. package/dist/{chunk-YWBAIFI2.cjs.map → chunk-6XISE63Q.cjs.map} +1 -1
  27. package/dist/{chunk-QD64Q4I2.cjs → chunk-A4HMGZ4L.cjs} +9 -9
  28. package/dist/{chunk-QD64Q4I2.cjs.map → chunk-A4HMGZ4L.cjs.map} +1 -1
  29. package/dist/{chunk-BK6CCQG6.js → chunk-AUCJ6I3R.js} +12 -12
  30. package/dist/{chunk-3WSS7C2V.cjs → chunk-BBBZ5QUB.cjs} +15 -15
  31. package/dist/{chunk-3WSS7C2V.cjs.map → chunk-BBBZ5QUB.cjs.map} +1 -1
  32. package/dist/{chunk-EVMHJB6X.js → chunk-BLUL2TE3.js} +4 -4
  33. package/dist/{chunk-GGQZBSFN.js → chunk-BVQEU5JB.js} +2 -2
  34. package/dist/{chunk-V2QK5W3O.cjs → chunk-BW644MGF.cjs} +3 -3
  35. package/dist/{chunk-V2QK5W3O.cjs.map → chunk-BW644MGF.cjs.map} +1 -1
  36. package/dist/{chunk-H64ZHSYR.cjs → chunk-CCWI3622.cjs} +4 -4
  37. package/dist/{chunk-H64ZHSYR.cjs.map → chunk-CCWI3622.cjs.map} +1 -1
  38. package/dist/{chunk-DI3SVVY5.cjs → chunk-COERKO4N.cjs} +85 -85
  39. package/dist/{chunk-DI3SVVY5.cjs.map → chunk-COERKO4N.cjs.map} +1 -1
  40. package/dist/{chunk-SE3PVN64.js → chunk-DZ44C6KB.js} +2 -2
  41. package/dist/{chunk-2SYX2Z7R.cjs → chunk-E27TMQRE.cjs} +34 -34
  42. package/dist/{chunk-2SYX2Z7R.cjs.map → chunk-E27TMQRE.cjs.map} +1 -1
  43. package/dist/{chunk-YH6IQTPI.cjs → chunk-EHAYNUOL.cjs} +3 -3
  44. package/dist/{chunk-YH6IQTPI.cjs.map → chunk-EHAYNUOL.cjs.map} +1 -1
  45. package/dist/{chunk-WZJOILPU.js → chunk-F2KBCLUG.js} +2 -2
  46. package/dist/{chunk-MYBXCWEI.js → chunk-FKBM6U43.js} +2 -2
  47. package/dist/{chunk-75YW6ET4.js → chunk-FNK4RZ25.js} +4 -4
  48. package/dist/{chunk-UG7JNQZY.cjs → chunk-GB5FPCUN.cjs} +37 -37
  49. package/dist/{chunk-UG7JNQZY.cjs.map → chunk-GB5FPCUN.cjs.map} +1 -1
  50. package/dist/{chunk-P24VPJTT.cjs → chunk-HJUXXWTM.cjs} +15 -15
  51. package/dist/{chunk-P24VPJTT.cjs.map → chunk-HJUXXWTM.cjs.map} +1 -1
  52. package/dist/{chunk-WPN545FW.js → chunk-ILJTXIEI.js} +12 -12
  53. package/dist/{chunk-YBPFIXV4.cjs → chunk-ITZDJYWI.cjs} +796 -480
  54. package/dist/chunk-ITZDJYWI.cjs.map +1 -0
  55. package/dist/{chunk-LRQCKSZC.js → chunk-IW746HJ7.js} +2 -2
  56. package/dist/{chunk-PX4IPM7Q.js → chunk-J62LLUDC.js} +3 -3
  57. package/dist/{chunk-M4YQUHOR.cjs → chunk-JJUV7Z4L.cjs} +15 -15
  58. package/dist/{chunk-M4YQUHOR.cjs.map → chunk-JJUV7Z4L.cjs.map} +1 -1
  59. package/dist/{chunk-GHYFVT52.js → chunk-JZF6EUMO.js} +5 -5
  60. package/dist/{chunk-JVZVVZOZ.cjs → chunk-KYGTR6EG.cjs} +9 -9
  61. package/dist/{chunk-JVZVVZOZ.cjs.map → chunk-KYGTR6EG.cjs.map} +1 -1
  62. package/dist/{chunk-QIKBISZM.cjs → chunk-L66UPFCQ.cjs} +242 -242
  63. package/dist/{chunk-QIKBISZM.cjs.map → chunk-L66UPFCQ.cjs.map} +1 -1
  64. package/dist/{chunk-NIAMPMKV.js → chunk-LJ4UG2SW.js} +4 -4
  65. package/dist/{chunk-WDWCQD75.js → chunk-MWQSHXYK.js} +9 -9
  66. package/dist/{chunk-BPVHDDNY.cjs → chunk-NLFST5II.cjs} +8 -8
  67. package/dist/{chunk-BPVHDDNY.cjs.map → chunk-NLFST5II.cjs.map} +1 -1
  68. package/dist/{chunk-SAEJGTCI.cjs → chunk-QPAPUIF3.cjs} +35 -35
  69. package/dist/{chunk-SAEJGTCI.cjs.map → chunk-QPAPUIF3.cjs.map} +1 -1
  70. package/dist/{chunk-HLPAVYK7.js → chunk-RSTEDWAT.js} +4 -4
  71. package/dist/{chunk-UAAXUFK6.js → chunk-RU3IKVY2.js} +3 -3
  72. package/dist/{chunk-EZM5DNII.js → chunk-S6WBBSII.js} +3 -3
  73. package/dist/{chunk-2JNCZDJR.js → chunk-TSCIVH7T.js} +2 -2
  74. package/dist/{chunk-RDTOMWDN.cjs → chunk-UPKZU6QU.cjs} +1 -1
  75. package/dist/chunk-UPKZU6QU.cjs.map +1 -0
  76. package/dist/{chunk-WCBRZCU4.cjs → chunk-UQEOAPAI.cjs} +9 -9
  77. package/dist/{chunk-WCBRZCU4.cjs.map → chunk-UQEOAPAI.cjs.map} +1 -1
  78. package/dist/{chunk-O4VZOA2Q.cjs → chunk-UXOOVP77.cjs} +3 -3
  79. package/dist/{chunk-O4VZOA2Q.cjs.map → chunk-UXOOVP77.cjs.map} +1 -1
  80. package/dist/{chunk-R4UV366K.js → chunk-UYLZAW6Y.js} +6 -6
  81. package/dist/{chunk-BZEWPW5A.cjs → chunk-VIBREYUK.cjs} +3 -3
  82. package/dist/{chunk-BZEWPW5A.cjs.map → chunk-VIBREYUK.cjs.map} +1 -1
  83. package/dist/{chunk-KHOZ52NR.js → chunk-XSMBLU44.js} +6 -6
  84. package/dist/{chunk-RYXIRVDC.js → chunk-YB6FI2G3.js} +1 -1
  85. package/dist/chunk-YB6FI2G3.js.map +1 -0
  86. package/dist/{chunk-XNPAVMPB.js → chunk-YG7DCXRD.js} +5 -5
  87. package/dist/components/case-studies/index.cjs +14 -14
  88. package/dist/components/case-studies/index.js +8 -8
  89. package/dist/components/chat/embeddable-chat.d.ts +10 -19
  90. package/dist/components/chat/embeddable-chat.d.ts.map +1 -1
  91. package/dist/components/chat/hooks/index.d.ts +1 -0
  92. package/dist/components/chat/hooks/index.d.ts.map +1 -1
  93. package/dist/components/chat/hooks/use-chat-history-hydration.d.ts +13 -3
  94. package/dist/components/chat/hooks/use-chat-history-hydration.d.ts.map +1 -1
  95. package/dist/components/chat/hooks/use-managed-dialog-list.d.ts +53 -0
  96. package/dist/components/chat/hooks/use-managed-dialog-list.d.ts.map +1 -0
  97. package/dist/components/chat/hooks/use-nats-chat-adapter.d.ts +2 -16
  98. package/dist/components/chat/hooks/use-nats-chat-adapter.d.ts.map +1 -1
  99. package/dist/components/chat/hooks/use-sse-chat-adapter.d.ts.map +1 -1
  100. package/dist/components/chat/hooks/use-unified-chat.d.ts.map +1 -1
  101. package/dist/components/chat/index.cjs +9 -5
  102. package/dist/components/chat/index.cjs.map +1 -1
  103. package/dist/components/chat/index.js +8 -4
  104. package/dist/components/chat/types/unified-chat-state.types.d.ts +72 -6
  105. package/dist/components/chat/types/unified-chat-state.types.d.ts.map +1 -1
  106. package/dist/components/chat/utils/chat-conversations-api.d.ts +29 -0
  107. package/dist/components/chat/utils/chat-conversations-api.d.ts.map +1 -0
  108. package/dist/components/chat/utils/index.d.ts +1 -0
  109. package/dist/components/chat/utils/index.d.ts.map +1 -1
  110. package/dist/components/contact/index.cjs +9 -9
  111. package/dist/components/contact/index.js +8 -8
  112. package/dist/components/docs/index.cjs +12 -12
  113. package/dist/components/docs/index.js +11 -11
  114. package/dist/components/embeds/index.cjs +9 -9
  115. package/dist/components/embeds/index.js +8 -8
  116. package/dist/components/faq/index.cjs +10 -10
  117. package/dist/components/faq/index.js +9 -9
  118. package/dist/components/features/index.cjs +8 -8
  119. package/dist/components/features/index.js +7 -7
  120. package/dist/components/help-center-pages/index.cjs +31 -31
  121. package/dist/components/help-center-pages/index.js +22 -22
  122. package/dist/components/index.cjs +70 -66
  123. package/dist/components/index.cjs.map +1 -1
  124. package/dist/components/index.js +26 -22
  125. package/dist/components/index.js.map +1 -1
  126. package/dist/components/meeting-scheduler/index.cjs +49 -49
  127. package/dist/components/meeting-scheduler/index.js +9 -9
  128. package/dist/components/navigation/index.cjs +11 -11
  129. package/dist/components/navigation/index.js +10 -10
  130. package/dist/components/onboarding-guides/index.cjs +12 -12
  131. package/dist/components/onboarding-guides/index.js +11 -11
  132. package/dist/components/related-content/index.cjs +7 -7
  133. package/dist/components/related-content/index.js +6 -6
  134. package/dist/components/tickets/index.cjs +13 -13
  135. package/dist/components/tickets/index.js +12 -12
  136. package/dist/components/ui/index.cjs +12 -8
  137. package/dist/components/ui/index.cjs.map +1 -1
  138. package/dist/components/ui/index.js +11 -7
  139. package/dist/contexts/chat-runtime-context.d.ts +12 -0
  140. package/dist/contexts/chat-runtime-context.d.ts.map +1 -1
  141. package/dist/contexts/index.cjs +2 -2
  142. package/dist/contexts/index.js +1 -1
  143. package/dist/hooks/index.cjs +5 -5
  144. package/dist/hooks/index.js +4 -4
  145. package/dist/index.cjs +16 -12
  146. package/dist/index.cjs.map +1 -1
  147. package/dist/index.js +15 -11
  148. package/dist/index.js.map +1 -1
  149. package/package.json +1 -1
  150. package/src/chat-protocol/index.ts +1 -0
  151. package/src/chat-protocol/nats-decoder.ts +1 -5
  152. package/src/chat-protocol/wire-narrow.ts +22 -0
  153. package/src/components/chat/.embeddable-chat.md +4 -2
  154. package/src/components/chat/embeddable-chat.tsx +136 -84
  155. package/src/components/chat/hooks/.index.md +1 -1
  156. package/src/components/chat/hooks/.use-managed-dialog-list.md +31 -0
  157. package/src/components/chat/hooks/.use-nats-chat-adapter.md +3 -1
  158. package/src/components/chat/hooks/.use-sse-chat-adapter.md +8 -1
  159. package/src/components/chat/hooks/.use-unified-chat.md +2 -2
  160. package/src/components/chat/hooks/__tests__/managed-dialog-list.test.ts +280 -0
  161. package/src/components/chat/hooks/__tests__/nats-adapter-managed-dialogs.test.ts +173 -0
  162. package/src/components/chat/hooks/__tests__/sse-managed-dialogs.test.ts +310 -0
  163. package/src/components/chat/hooks/index.ts +2 -0
  164. package/src/components/chat/hooks/use-chat-history-hydration.ts +67 -24
  165. package/src/components/chat/hooks/use-managed-dialog-list.ts +339 -0
  166. package/src/components/chat/hooks/use-nats-chat-adapter.ts +65 -150
  167. package/src/components/chat/hooks/use-sse-chat-adapter.ts +233 -59
  168. package/src/components/chat/hooks/use-unified-chat.ts +5 -0
  169. package/src/components/chat/types/.unified-chat-state.types.md +13 -1
  170. package/src/components/chat/types/unified-chat-state.types.ts +79 -6
  171. package/src/components/chat/utils/.chat-conversations-api.md +16 -0
  172. package/src/components/chat/utils/.index.md +1 -1
  173. package/src/components/chat/utils/__tests__/chat-conversations-api-golden.test.ts +105 -0
  174. package/src/components/chat/utils/chat-conversations-api.ts +83 -0
  175. package/src/components/chat/utils/index.ts +7 -0
  176. package/src/contexts/.chat-runtime-context.md +3 -1
  177. package/src/contexts/chat-runtime-context.tsx +12 -0
  178. package/dist/chunk-RDTOMWDN.cjs.map +0 -1
  179. package/dist/chunk-RYXIRVDC.js.map +0 -1
  180. package/dist/chunk-VNSRNVPD.js.map +0 -1
  181. package/dist/chunk-YBPFIXV4.cjs.map +0 -1
  182. /package/dist/{chunk-VIRTMRS4.js.map → chunk-4PBUEYAE.js.map} +0 -0
  183. /package/dist/{chunk-TZPVRWPU.js.map → chunk-4TG5BWPE.js.map} +0 -0
  184. /package/dist/{chunk-PTK3CINU.js.map → chunk-6JF65LRQ.js.map} +0 -0
  185. /package/dist/{chunk-BK6CCQG6.js.map → chunk-AUCJ6I3R.js.map} +0 -0
  186. /package/dist/{chunk-EVMHJB6X.js.map → chunk-BLUL2TE3.js.map} +0 -0
  187. /package/dist/{chunk-GGQZBSFN.js.map → chunk-BVQEU5JB.js.map} +0 -0
  188. /package/dist/{chunk-SE3PVN64.js.map → chunk-DZ44C6KB.js.map} +0 -0
  189. /package/dist/{chunk-WZJOILPU.js.map → chunk-F2KBCLUG.js.map} +0 -0
  190. /package/dist/{chunk-MYBXCWEI.js.map → chunk-FKBM6U43.js.map} +0 -0
  191. /package/dist/{chunk-75YW6ET4.js.map → chunk-FNK4RZ25.js.map} +0 -0
  192. /package/dist/{chunk-WPN545FW.js.map → chunk-ILJTXIEI.js.map} +0 -0
  193. /package/dist/{chunk-LRQCKSZC.js.map → chunk-IW746HJ7.js.map} +0 -0
  194. /package/dist/{chunk-PX4IPM7Q.js.map → chunk-J62LLUDC.js.map} +0 -0
  195. /package/dist/{chunk-GHYFVT52.js.map → chunk-JZF6EUMO.js.map} +0 -0
  196. /package/dist/{chunk-NIAMPMKV.js.map → chunk-LJ4UG2SW.js.map} +0 -0
  197. /package/dist/{chunk-WDWCQD75.js.map → chunk-MWQSHXYK.js.map} +0 -0
  198. /package/dist/{chunk-HLPAVYK7.js.map → chunk-RSTEDWAT.js.map} +0 -0
  199. /package/dist/{chunk-UAAXUFK6.js.map → chunk-RU3IKVY2.js.map} +0 -0
  200. /package/dist/{chunk-EZM5DNII.js.map → chunk-S6WBBSII.js.map} +0 -0
  201. /package/dist/{chunk-2JNCZDJR.js.map → chunk-TSCIVH7T.js.map} +0 -0
  202. /package/dist/{chunk-R4UV366K.js.map → chunk-UYLZAW6Y.js.map} +0 -0
  203. /package/dist/{chunk-KHOZ52NR.js.map → chunk-XSMBLU44.js.map} +0 -0
  204. /package/dist/{chunk-XNPAVMPB.js.map → chunk-YG7DCXRD.js.map} +0 -0
@@ -0,0 +1,280 @@
1
+ /**
2
+ * Unit tests for the shared `useManagedDialogList` hook — the parts the NATS
3
+ * characterization test cannot reach: `upsertDialogTop` dedupe,
4
+ * `bumpDialogToTop` ordering, append dedupe, out-of-order page-1 responses,
5
+ * the `onDialogRemoved` reasons, and the inert no-`fetchDialogs` shape.
6
+ */
7
+
8
+ import { renderHook, act, waitFor } from '@testing-library/react';
9
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
10
+ import { useManagedDialogList } from '../use-managed-dialog-list';
11
+
12
+ const page = (ids: string[], nextCursor: string | null = null) => ({
13
+ dialogs: ids.map(id => ({ id, title: `Chat ${id}` })),
14
+ nextCursor,
15
+ });
16
+
17
+ describe('useManagedDialogList', () => {
18
+ beforeEach(() => {
19
+ vi.spyOn(console, 'error').mockImplementation(() => {});
20
+ });
21
+ afterEach(() => {
22
+ vi.restoreAllMocks();
23
+ });
24
+
25
+ it('reports loading from the FIRST render when a load is coming', async () => {
26
+ let resolve: ((v: ReturnType<typeof page>) => void) | undefined;
27
+ const fetchDialogs = vi.fn(
28
+ () =>
29
+ new Promise<ReturnType<typeof page>>(r => {
30
+ resolve = r;
31
+ }),
32
+ );
33
+ const { result } = renderHook(() =>
34
+ useManagedDialogList({ autoLoad: true, pageSize: 20, logTag: '[t]', fetchDialogs }),
35
+ );
36
+ // Before the effect has even run: a consumer branching on "empty list" must
37
+ // not see a settled-and-empty state.
38
+ expect(result.current.isDialogsLoading).toBe(true);
39
+ expect(result.current.dialogs).toEqual([]);
40
+ await act(async () => {
41
+ resolve?.(page(['a']));
42
+ await Promise.resolve();
43
+ });
44
+ expect(result.current.isDialogsLoading).toBe(false);
45
+ expect(result.current.dialogs.map(d => d.id)).toEqual(['a']);
46
+ });
47
+
48
+ it('re-enters loading synchronously on a search change, including clearing it', async () => {
49
+ const resolvers: Array<(v: ReturnType<typeof page>) => void> = [];
50
+ const fetchDialogs = vi.fn(
51
+ () =>
52
+ new Promise<ReturnType<typeof page>>(r => {
53
+ resolvers.push(r);
54
+ }),
55
+ );
56
+ const { result, rerender } = renderHook(
57
+ ({ search }) => useManagedDialogList({ autoLoad: true, pageSize: 20, logTag: '[t]', fetchDialogs, search }),
58
+ { initialProps: { search: '' } },
59
+ );
60
+ await act(async () => {
61
+ resolvers[0](page(['a']));
62
+ await Promise.resolve();
63
+ });
64
+ expect(result.current.isDialogsLoading).toBe(false);
65
+
66
+ rerender({ search: 'zzz' });
67
+ expect(result.current.isDialogsLoading).toBe(true);
68
+ await act(async () => {
69
+ resolvers[1](page([]));
70
+ await Promise.resolve();
71
+ });
72
+ expect(result.current.isDialogsLoading).toBe(false);
73
+ expect(result.current.dialogs).toEqual([]);
74
+
75
+ // Clearing the term must ALSO read as loading again, not as "settled empty".
76
+ rerender({ search: '' });
77
+ expect(result.current.isDialogsLoading).toBe(true);
78
+ });
79
+
80
+ it('a REJECTED first page still settles — the list must not stick on a skeleton', async () => {
81
+ const fetchDialogs = vi.fn(() => Promise.reject(new Error('down')));
82
+ const { result } = renderHook(() =>
83
+ useManagedDialogList({ autoLoad: true, pageSize: 20, logTag: '[t]', fetchDialogs }),
84
+ );
85
+ expect(result.current.isDialogsLoading).toBe(true);
86
+ await waitFor(() => expect(result.current.dialogsError).toBe(true));
87
+ expect(result.current.isDialogsLoading).toBe(false);
88
+ });
89
+
90
+ it('a superseded response never settles the NEWER search term', async () => {
91
+ const resolvers: Array<(v: ReturnType<typeof page>) => void> = [];
92
+ const fetchDialogs = vi.fn(
93
+ () =>
94
+ new Promise<ReturnType<typeof page>>(r => {
95
+ resolvers.push(r);
96
+ }),
97
+ );
98
+ const { result, rerender } = renderHook(
99
+ ({ search }) => useManagedDialogList({ autoLoad: true, pageSize: 20, logTag: '[t]', fetchDialogs, search }),
100
+ { initialProps: { search: 'a' } },
101
+ );
102
+ await waitFor(() => expect(resolvers.length).toBe(1));
103
+ rerender({ search: 'ab' });
104
+ await waitFor(() => expect(resolvers.length).toBe(2));
105
+ await act(async () => {
106
+ resolvers[0](page([]));
107
+ await Promise.resolve();
108
+ });
109
+ expect(result.current.isDialogsLoading).toBe(true);
110
+ await act(async () => {
111
+ resolvers[1](page([]));
112
+ await Promise.resolve();
113
+ });
114
+ expect(result.current.isDialogsLoading).toBe(false);
115
+ });
116
+
117
+ it('re-arms when autoLoad turns on after mount (panel opened in the other mode)', async () => {
118
+ let resolve: ((v: ReturnType<typeof page>) => void) | undefined;
119
+ const fetchDialogs = vi.fn(
120
+ () =>
121
+ new Promise<ReturnType<typeof page>>(r => {
122
+ resolve = r;
123
+ }),
124
+ );
125
+ const { result, rerender } = renderHook(
126
+ ({ autoLoad }) => useManagedDialogList({ autoLoad, pageSize: 20, logTag: '[t]', fetchDialogs }),
127
+ { initialProps: { autoLoad: false } },
128
+ );
129
+ expect(result.current.isDialogsLoading).toBe(false);
130
+ expect(fetchDialogs).not.toHaveBeenCalled();
131
+
132
+ rerender({ autoLoad: true });
133
+ // Must NOT read as settled-and-empty for a frame before the effect fires.
134
+ expect(result.current.isDialogsLoading).toBe(true);
135
+ await waitFor(() => expect(fetchDialogs).toHaveBeenCalledTimes(1));
136
+ await act(async () => {
137
+ resolve?.(page(['a']));
138
+ await Promise.resolve();
139
+ });
140
+ expect(result.current.isDialogsLoading).toBe(false);
141
+ expect(result.current.dialogs.map(d => d.id)).toEqual(['a']);
142
+ });
143
+
144
+ it('an archive-style consumer (autoLoad false) is settled from the start', () => {
145
+ const { result } = renderHook(() =>
146
+ useManagedDialogList({
147
+ autoLoad: false,
148
+ pageSize: 20,
149
+ logTag: '[t]',
150
+ fetchDialogs: () => Promise.resolve(page([])),
151
+ }),
152
+ );
153
+ expect(result.current.isDialogsLoading).toBe(false);
154
+ });
155
+
156
+ it('is inert without fetchDialogs', () => {
157
+ const { result } = renderHook(() => useManagedDialogList({ autoLoad: true, pageSize: 20, logTag: '[t]' }));
158
+ expect(result.current.dialogs).toEqual([]);
159
+ expect(result.current.hasMoreDialogs).toBe(false);
160
+ expect(result.current.isDialogsLoading).toBe(false);
161
+ });
162
+
163
+ it('upsertDialogTop prepends + dedupes; bumpDialogToTop re-heads the row', async () => {
164
+ const fetchDialogs = vi.fn(() => Promise.resolve(page(['a', 'b'])));
165
+ const { result } = renderHook(() =>
166
+ useManagedDialogList({ autoLoad: true, pageSize: 20, logTag: '[t]', fetchDialogs }),
167
+ );
168
+ await waitFor(() => expect(result.current.dialogs.length).toBe(2));
169
+ act(() => result.current.upsertDialogTop({ id: 'b', title: 'B again' }));
170
+ expect(result.current.dialogs.map(d => [d.id, d.title])).toEqual([
171
+ ['b', 'B again'],
172
+ ['a', 'Chat a'],
173
+ ]);
174
+ act(() => result.current.bumpDialogToTop('a'));
175
+ expect(result.current.dialogs.map(d => d.id)).toEqual(['a', 'b']);
176
+ expect(result.current.dialogs[0].timestamp).toBeInstanceOf(Date);
177
+ // Unknown id is a no-op, not an insert.
178
+ act(() => result.current.bumpDialogToTop('nope'));
179
+ expect(result.current.dialogs.map(d => d.id)).toEqual(['a', 'b']);
180
+ });
181
+
182
+ it('drops a superseded page-1 response and dedupes appended pages', async () => {
183
+ const resolvers: Array<(v: ReturnType<typeof page>) => void> = [];
184
+ const fetchDialogs = vi.fn(() => new Promise<ReturnType<typeof page>>(resolve => resolvers.push(resolve)));
185
+ const { result, rerender } = renderHook(
186
+ ({ search }) => useManagedDialogList({ autoLoad: true, pageSize: 20, logTag: '[t]', fetchDialogs, search }),
187
+ { initialProps: { search: 'a' } },
188
+ );
189
+ await waitFor(() => expect(resolvers.length).toBe(1));
190
+ rerender({ search: 'ab' });
191
+ await waitFor(() => expect(resolvers.length).toBe(2));
192
+
193
+ // The NEWER request resolves first, then the stale one lands.
194
+ await act(async () => {
195
+ resolvers[1](page(['new'], 'cursor-new'));
196
+ await Promise.resolve();
197
+ });
198
+ await act(async () => {
199
+ resolvers[0](page(['stale'], 'cursor-stale'));
200
+ await Promise.resolve();
201
+ });
202
+ expect(result.current.dialogs.map(d => d.id)).toEqual(['new']);
203
+ expect(result.current.hasMoreDialogs).toBe(true);
204
+
205
+ // Appended pages drop ids already rendered (a row can move between pages).
206
+ await act(async () => {
207
+ const p = result.current.loadMoreDialogs();
208
+ resolvers[2](page(['new', 'later'], null));
209
+ await p;
210
+ });
211
+ expect(result.current.dialogs.map(d => d.id)).toEqual(['new', 'later']);
212
+ });
213
+
214
+ it('re-throws a failed archive/delete so a confirmation modal can stay open', async () => {
215
+ const { result } = renderHook(() =>
216
+ useManagedDialogList({
217
+ autoLoad: true,
218
+ pageSize: 20,
219
+ logTag: '[t]',
220
+ fetchDialogs: () => Promise.resolve(page(['a'])),
221
+ archiveDialog: () => Promise.reject(new Error('429')),
222
+ deleteDialog: () => Promise.reject(new Error('boom')),
223
+ }),
224
+ );
225
+ await waitFor(() => expect(result.current.dialogs.length).toBe(1));
226
+ await act(async () => {
227
+ await expect(result.current.archiveDialog('a')).rejects.toThrow('429');
228
+ await expect(result.current.deleteDialog('a')).rejects.toThrow('boom');
229
+ });
230
+ // The row survives a failed write.
231
+ expect(result.current.dialogs.map(d => d.id)).toEqual(['a']);
232
+ });
233
+
234
+ it('reports removal reasons AFTER the local removal', async () => {
235
+ const seen: Array<[string, string, number]> = [];
236
+ let latest: string[] = [];
237
+ const { result } = renderHook(() =>
238
+ useManagedDialogList({
239
+ autoLoad: true,
240
+ pageSize: 20,
241
+ logTag: '[t]',
242
+ fetchDialogs: () => Promise.resolve(page(['a', 'b'])),
243
+ archiveDialog: () => Promise.resolve(),
244
+ deleteDialog: () => Promise.resolve(),
245
+ onDialogRemoved: (id, reason) => seen.push([id, reason, latest.length]),
246
+ }),
247
+ );
248
+ await waitFor(() => expect(result.current.dialogs.length).toBe(2));
249
+ latest = result.current.dialogs.map(d => d.id);
250
+ await act(async () => {
251
+ await result.current.archiveDialog('a');
252
+ });
253
+ await act(async () => {
254
+ await result.current.deleteDialog('b');
255
+ });
256
+ expect(seen.map(([id, reason]) => [id, reason])).toEqual([
257
+ ['a', 'archived'],
258
+ ['b', 'deleted'],
259
+ ]);
260
+ expect(result.current.dialogs).toEqual([]);
261
+ });
262
+
263
+ it('mutations without their callback are no-ops', async () => {
264
+ const { result } = renderHook(() =>
265
+ useManagedDialogList({
266
+ autoLoad: true,
267
+ pageSize: 20,
268
+ logTag: '[t]',
269
+ fetchDialogs: () => Promise.resolve(page(['a'])),
270
+ }),
271
+ );
272
+ await waitFor(() => expect(result.current.dialogs.length).toBe(1));
273
+ await act(async () => {
274
+ await result.current.renameDialog('a', 'x');
275
+ await result.current.archiveDialog('a');
276
+ await result.current.deleteDialog('a');
277
+ });
278
+ expect(result.current.dialogs.map(d => [d.id, d.title])).toEqual([['a', 'Chat a']]);
279
+ });
280
+ });
@@ -0,0 +1,173 @@
1
+ /**
2
+ * CHARACTERIZATION TESTS — `useNatsChatAdapter` managed-dialog mode.
3
+ *
4
+ * Pins the dialog-list state machine (initial load, retry, pagination,
5
+ * optimistic rename/archive/delete) BEFORE that block moves into the shared
6
+ * `useManagedDialogList` hook consumed by both transport adapters. Every
7
+ * assertion here is observable host behaviour that must survive the
8
+ * extraction byte-for-byte: the wire params sent to `fetchDialogs`, the
9
+ * list contents after each mutation, and the active-id side effects.
10
+ */
11
+
12
+ import { renderHook, act, waitFor } from '@testing-library/react';
13
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
14
+ import type { DialogItem } from '../../types/component.types';
15
+ import { useNatsChatAdapter, type FetchDialogsParams, type UseNatsChatAdapterConfig } from '../use-nats-chat-adapter';
16
+
17
+ const page = (ids: string[], nextCursor: string | null = null) => ({
18
+ dialogs: ids.map<DialogItem>(id => ({ id, title: `Chat ${id}`, timestamp: new Date('2026-01-01T00:00:00Z') })),
19
+ nextCursor,
20
+ });
21
+
22
+ function makeConfig(overrides: Partial<UseNatsChatAdapterConfig> = {}): UseNatsChatAdapterConfig {
23
+ return {
24
+ getNatsWsUrl: () => null,
25
+ publishUserMessage: vi.fn(),
26
+ fetchChunks: vi.fn(() => Promise.resolve([])),
27
+ fetchDialogs: vi.fn((_p: FetchDialogsParams) => Promise.resolve(page(['a', 'b']))),
28
+ fetchDialogMessages: vi.fn(() => Promise.resolve({ messages: [], nextCursor: null })),
29
+ renameDialog: vi.fn(() => Promise.resolve()),
30
+ archiveDialog: vi.fn(() => Promise.resolve()),
31
+ deleteDialog: vi.fn(() => Promise.resolve()),
32
+ dialogsPageSize: 2,
33
+ ...overrides,
34
+ };
35
+ }
36
+
37
+ describe('useNatsChatAdapter — managed-dialog mode (characterization)', () => {
38
+ beforeEach(() => {
39
+ vi.spyOn(console, 'error').mockImplementation(() => {});
40
+ });
41
+ afterEach(() => {
42
+ vi.restoreAllMocks();
43
+ });
44
+
45
+ it('loads the first page only when active, with exactly {cursor, limit} (no search key)', async () => {
46
+ const config = makeConfig();
47
+ const { result, rerender } = renderHook(({ active }) => useNatsChatAdapter(config, { active }), {
48
+ initialProps: { active: false },
49
+ });
50
+ expect(config.fetchDialogs).not.toHaveBeenCalled();
51
+ expect(result.current.dialogs).toEqual([]);
52
+
53
+ rerender({ active: true });
54
+ await waitFor(() => expect(result.current.dialogs.map(d => d.id)).toEqual(['a', 'b']));
55
+ expect(config.fetchDialogs).toHaveBeenCalledTimes(1);
56
+ const params = (config.fetchDialogs as ReturnType<typeof vi.fn>).mock.calls[0][0] as FetchDialogsParams;
57
+ expect(params).toEqual({ cursor: undefined, limit: 2 });
58
+ expect(Object.keys(params)).toEqual(['cursor', 'limit']);
59
+ expect(result.current.dialogsError).toBe(false);
60
+ expect(result.current.isDialogsLoading).toBe(false);
61
+ });
62
+
63
+ it('first-page failure flags dialogsError and reloadDialogs retries', async () => {
64
+ const fetchDialogs = vi
65
+ .fn<(p: FetchDialogsParams) => Promise<ReturnType<typeof page>>>()
66
+ .mockRejectedValueOnce(new Error('boom'))
67
+ .mockResolvedValue(page(['a']));
68
+ const config = makeConfig({ fetchDialogs });
69
+ const { result } = renderHook(() => useNatsChatAdapter(config));
70
+ await waitFor(() => expect(result.current.dialogsError).toBe(true));
71
+ expect(result.current.dialogs).toEqual([]);
72
+
73
+ act(() => result.current.reloadDialogs());
74
+ await waitFor(() => expect(result.current.dialogs.map(d => d.id)).toEqual(['a']));
75
+ expect(result.current.dialogsError).toBe(false);
76
+ expect(fetchDialogs).toHaveBeenCalledTimes(2);
77
+ });
78
+
79
+ it('paginates with the server cursor; a pagination failure keeps the loaded rows', async () => {
80
+ const fetchDialogs = vi
81
+ .fn<(p: FetchDialogsParams) => Promise<ReturnType<typeof page>>>()
82
+ .mockResolvedValueOnce(page(['a', 'b'], 'c2'))
83
+ .mockRejectedValueOnce(new Error('page 2 down'))
84
+ .mockResolvedValueOnce(page(['c'], null));
85
+ const config = makeConfig({ fetchDialogs });
86
+ const { result } = renderHook(() => useNatsChatAdapter(config));
87
+ await waitFor(() => expect(result.current.hasMoreDialogs).toBe(true));
88
+
89
+ await act(async () => {
90
+ await result.current.loadMoreDialogs();
91
+ });
92
+ expect(fetchDialogs.mock.calls[1][0]).toEqual({ cursor: 'c2', limit: 2 });
93
+ expect(result.current.dialogs.map(d => d.id)).toEqual(['a', 'b']);
94
+ expect(result.current.dialogsError).toBe(false);
95
+ expect(result.current.hasMoreDialogs).toBe(true);
96
+
97
+ await act(async () => {
98
+ await result.current.loadMoreDialogs();
99
+ });
100
+ expect(result.current.dialogs.map(d => d.id)).toEqual(['a', 'b', 'c']);
101
+ expect(result.current.hasMoreDialogs).toBe(false);
102
+ });
103
+
104
+ it('rename is optimistic, rolls back AND surfaces the failure', async () => {
105
+ const renameDialog = vi
106
+ .fn<(id: string, title: string) => Promise<void>>()
107
+ .mockResolvedValueOnce(undefined)
108
+ .mockRejectedValueOnce(new Error('nope'));
109
+ const config = makeConfig({ renameDialog });
110
+ const { result } = renderHook(() => useNatsChatAdapter(config));
111
+ await waitFor(() => expect(result.current.dialogs.length).toBe(2));
112
+
113
+ await act(async () => {
114
+ await result.current.renameDialog('a', 'Renamed');
115
+ });
116
+ expect(result.current.dialogs.find(d => d.id === 'a')?.title).toBe('Renamed');
117
+
118
+ await act(async () => {
119
+ // SWALLOWED, unlike archive/delete: `useChatDialogManager` fires rename
120
+ // as `void renameDialog(...)`, so a rejection would only ever surface as
121
+ // an unhandled rejection. Pre-extraction NATS behaved the same way.
122
+ await expect(result.current.renameDialog('a', 'Will fail')).resolves.toBeUndefined();
123
+ });
124
+ expect(result.current.dialogs.find(d => d.id === 'a')?.title).toBe('Renamed');
125
+ expect(renameDialog).toHaveBeenCalledTimes(2);
126
+ });
127
+
128
+ it('archiving the ACTIVE dialog removes it and clears activeDialogId; delete removes too', async () => {
129
+ const config = makeConfig();
130
+ const { result } = renderHook(() => useNatsChatAdapter(config));
131
+ await waitFor(() => expect(result.current.dialogs.length).toBe(2));
132
+
133
+ act(() => result.current.selectDialog('a'));
134
+ expect(result.current.activeDialogId).toBe('a');
135
+
136
+ await act(async () => {
137
+ await result.current.archiveDialog('a');
138
+ });
139
+ expect(config.archiveDialog).toHaveBeenCalledWith('a');
140
+ expect(result.current.dialogs.map(d => d.id)).toEqual(['b']);
141
+ expect(result.current.activeDialogId).toBeNull();
142
+
143
+ await act(async () => {
144
+ await result.current.deleteDialog('b');
145
+ });
146
+ expect(config.deleteDialog).toHaveBeenCalledWith('b');
147
+ expect(result.current.dialogs).toEqual([]);
148
+ });
149
+
150
+ it('archive failure keeps the row and the active id, and surfaces the error', async () => {
151
+ const config = makeConfig({ archiveDialog: vi.fn(() => Promise.reject(new Error('down'))) });
152
+ const { result } = renderHook(() => useNatsChatAdapter(config));
153
+ await waitFor(() => expect(result.current.dialogs.length).toBe(2));
154
+ act(() => result.current.selectDialog('a'));
155
+ await act(async () => {
156
+ // Re-thrown (was swallowed before the shared-hook extraction) so a
157
+ // confirmation modal can stay open instead of closing on a write the
158
+ // server rejected.
159
+ await expect(result.current.archiveDialog('a')).rejects.toThrow('down');
160
+ });
161
+ expect(result.current.dialogs.map(d => d.id)).toEqual(['a', 'b']);
162
+ expect(result.current.activeDialogId).toBe('a');
163
+ });
164
+
165
+ it('bare-transport mode (no fetchDialogs) never lists and ignores selectDialog', () => {
166
+ const config = makeConfig({ fetchDialogs: undefined, dialogId: 'host-owned' });
167
+ const { result } = renderHook(() => useNatsChatAdapter(config));
168
+ act(() => result.current.selectDialog('other'));
169
+ expect(result.current.activeDialogId).toBe('host-owned');
170
+ expect(result.current.dialogs).toEqual([]);
171
+ expect(result.current.hasMoreDialogs).toBe(false);
172
+ });
173
+ });