@flamingo-stack/openframe-frontend-core 0.0.497 → 0.0.498-1506.4000.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 (285) hide show
  1. package/dist/chat-protocol/decode.d.ts +58 -0
  2. package/dist/chat-protocol/decode.d.ts.map +1 -0
  3. package/dist/chat-protocol/encode.d.ts +44 -0
  4. package/dist/chat-protocol/encode.d.ts.map +1 -0
  5. package/dist/chat-protocol/env-flag.d.ts +35 -0
  6. package/dist/chat-protocol/env-flag.d.ts.map +1 -0
  7. package/dist/chat-protocol/events.d.ts +207 -0
  8. package/dist/chat-protocol/events.d.ts.map +1 -0
  9. package/dist/chat-protocol/frames.d.ts +213 -0
  10. package/dist/chat-protocol/frames.d.ts.map +1 -0
  11. package/dist/chat-protocol/index.cjs +500 -0
  12. package/dist/chat-protocol/index.cjs.map +1 -0
  13. package/dist/chat-protocol/index.d.ts +18 -0
  14. package/dist/chat-protocol/index.d.ts.map +1 -0
  15. package/dist/chat-protocol/index.js +484 -0
  16. package/dist/chat-protocol/index.js.map +1 -0
  17. package/dist/chat-protocol/ip-normalize.d.ts +44 -0
  18. package/dist/chat-protocol/ip-normalize.d.ts.map +1 -0
  19. package/dist/chat-protocol/nats-decoder.d.ts +26 -0
  20. package/dist/chat-protocol/nats-decoder.d.ts.map +1 -0
  21. package/dist/{chunk-2DER2JSI.cjs → chunk-242K7LZY.cjs} +11 -11
  22. package/dist/{chunk-2DER2JSI.cjs.map → chunk-242K7LZY.cjs.map} +1 -1
  23. package/dist/{chunk-VBSS4TPV.cjs → chunk-2LYA3RBT.cjs} +37 -37
  24. package/dist/{chunk-VBSS4TPV.cjs.map → chunk-2LYA3RBT.cjs.map} +1 -1
  25. package/dist/{chunk-ZPUCCSYA.js → chunk-34C2JV2C.js} +2 -2
  26. package/dist/{chunk-GWVNRXJU.js → chunk-3VGRZ62V.js} +2 -2
  27. package/dist/{chunk-AAHAVML2.js → chunk-42NYDEHH.js} +2 -2
  28. package/dist/{chunk-ZEHA4IC7.js → chunk-42SMPKGY.js} +2 -2
  29. package/dist/{chunk-RTG5DJWM.cjs → chunk-44C23SSC.cjs} +5 -5
  30. package/dist/{chunk-RTG5DJWM.cjs.map → chunk-44C23SSC.cjs.map} +1 -1
  31. package/dist/{chunk-SL62IZ3I.cjs → chunk-4KKGHPNE.cjs} +9 -9
  32. package/dist/{chunk-SL62IZ3I.cjs.map → chunk-4KKGHPNE.cjs.map} +1 -1
  33. package/dist/{chunk-X2CYUCGL.cjs → chunk-5BQWVNRS.cjs} +62 -62
  34. package/dist/{chunk-X2CYUCGL.cjs.map → chunk-5BQWVNRS.cjs.map} +1 -1
  35. package/dist/{chunk-HRUXC3YN.js → chunk-5QWYIPMH.js} +2 -2
  36. package/dist/{chunk-NT3BKPEC.js → chunk-5ZY5JM4Z.js} +10 -10
  37. package/dist/chunk-5ZY5JM4Z.js.map +1 -0
  38. package/dist/{chunk-4C764VBX.cjs → chunk-62TM2RVZ.cjs} +26 -26
  39. package/dist/{chunk-4C764VBX.cjs.map → chunk-62TM2RVZ.cjs.map} +1 -1
  40. package/dist/{chunk-YBTYHIXA.js → chunk-632XJ2ZT.js} +6 -5
  41. package/dist/chunk-632XJ2ZT.js.map +1 -0
  42. package/dist/{chunk-DGDUOFNY.js → chunk-BUBQEZ5J.js} +2 -2
  43. package/dist/{chunk-ZAAIVSX6.js → chunk-D3LUKR2Y.js} +11964 -9714
  44. package/dist/chunk-D3LUKR2Y.js.map +1 -0
  45. package/dist/{chunk-5KSXMJXF.cjs → chunk-EO35UHFT.cjs} +60 -60
  46. package/dist/chunk-EO35UHFT.cjs.map +1 -0
  47. package/dist/{chunk-5YKUEZIA.cjs → chunk-EORFD5WB.cjs} +15 -15
  48. package/dist/{chunk-5YKUEZIA.cjs.map → chunk-EORFD5WB.cjs.map} +1 -1
  49. package/dist/{chunk-MEIMDHQ2.cjs → chunk-F7XPMG25.cjs} +28 -27
  50. package/dist/chunk-F7XPMG25.cjs.map +1 -0
  51. package/dist/{chunk-I74ADMZS.cjs → chunk-H6GFQ4AO.cjs} +7 -7
  52. package/dist/{chunk-I74ADMZS.cjs.map → chunk-H6GFQ4AO.cjs.map} +1 -1
  53. package/dist/{chunk-EOYFMWDT.cjs → chunk-J3NHQ4JB.cjs} +31 -31
  54. package/dist/{chunk-EOYFMWDT.cjs.map → chunk-J3NHQ4JB.cjs.map} +1 -1
  55. package/dist/{chunk-23QJPSZJ.cjs → chunk-SS3D3DHL.cjs} +7049 -4799
  56. package/dist/chunk-SS3D3DHL.cjs.map +1 -0
  57. package/dist/{chunk-6XQGPFIR.js → chunk-WNXIG6YY.js} +2 -2
  58. package/dist/{chunk-5INZDVGI.js → chunk-WZCQW4MW.js} +4 -4
  59. package/dist/{chunk-B3MRHZNZ.js → chunk-ZHC2BCXK.js} +4 -4
  60. package/dist/components/case-studies/index.cjs +8 -8
  61. package/dist/components/case-studies/index.js +2 -2
  62. package/dist/components/chat/chat-message-enhanced.d.ts.map +1 -1
  63. package/dist/components/chat/chat-message-list.d.ts.map +1 -1
  64. package/dist/components/chat/hooks/index.d.ts +0 -1
  65. package/dist/components/chat/hooks/index.d.ts.map +1 -1
  66. package/dist/components/chat/hooks/use-chat.d.ts.map +1 -1
  67. package/dist/components/chat/hooks/use-chunk-catchup.d.ts.map +1 -1
  68. package/dist/components/chat/hooks/use-nats-chat-adapter.d.ts +15 -48
  69. package/dist/components/chat/hooks/use-nats-chat-adapter.d.ts.map +1 -1
  70. package/dist/components/chat/hooks/use-sse-chat-adapter.d.ts +10 -57
  71. package/dist/components/chat/hooks/use-sse-chat-adapter.d.ts.map +1 -1
  72. package/dist/components/chat/index.cjs +10 -2
  73. package/dist/components/chat/index.cjs.map +1 -1
  74. package/dist/components/chat/index.d.ts +1 -0
  75. package/dist/components/chat/index.d.ts.map +1 -1
  76. package/dist/components/chat/index.js +21 -13
  77. package/dist/components/chat/stream/chat-dialog-store.d.ts +184 -0
  78. package/dist/components/chat/stream/chat-dialog-store.d.ts.map +1 -0
  79. package/dist/components/chat/stream/chat-stream-reducer.d.ts +367 -0
  80. package/dist/components/chat/stream/chat-stream-reducer.d.ts.map +1 -0
  81. package/dist/components/chat/stream/delta-batcher.d.ts +51 -0
  82. package/dist/components/chat/stream/delta-batcher.d.ts.map +1 -0
  83. package/dist/components/chat/stream/index.d.ts +17 -0
  84. package/dist/components/chat/stream/index.d.ts.map +1 -0
  85. package/dist/components/chat/stream/message-mutations.d.ts +77 -0
  86. package/dist/components/chat/stream/message-mutations.d.ts.map +1 -0
  87. package/dist/components/chat/stream/use-chat-stream-reducer.d.ts +22 -0
  88. package/dist/components/chat/stream/use-chat-stream-reducer.d.ts.map +1 -0
  89. package/dist/components/chat/types/api.types.d.ts +1 -81
  90. package/dist/components/chat/types/api.types.d.ts.map +1 -1
  91. package/dist/components/chat/types/processing.types.d.ts +2 -93
  92. package/dist/components/chat/types/processing.types.d.ts.map +1 -1
  93. package/dist/components/chat/utils/extract-incomplete-message-state.d.ts +34 -6
  94. package/dist/components/chat/utils/extract-incomplete-message-state.d.ts.map +1 -1
  95. package/dist/components/chat/utils/index.d.ts +2 -3
  96. package/dist/components/chat/utils/index.d.ts.map +1 -1
  97. package/dist/components/chat/utils/process-historical-messages.d.ts +59 -11
  98. package/dist/components/chat/utils/process-historical-messages.d.ts.map +1 -1
  99. package/dist/components/contact/index.cjs +3 -3
  100. package/dist/components/contact/index.js +2 -2
  101. package/dist/components/docs/index.cjs +5 -5
  102. package/dist/components/docs/index.js +4 -4
  103. package/dist/components/embeds/index.cjs +3 -3
  104. package/dist/components/embeds/index.js +2 -2
  105. package/dist/components/faq/index.cjs +3 -3
  106. package/dist/components/faq/index.js +2 -2
  107. package/dist/components/features/index.cjs +2 -2
  108. package/dist/components/features/index.js +1 -1
  109. package/dist/components/help-center-pages/index.cjs +20 -20
  110. package/dist/components/help-center-pages/index.js +11 -11
  111. package/dist/components/index.cjs +172 -130
  112. package/dist/components/index.cjs.map +1 -1
  113. package/dist/components/index.js +63 -21
  114. package/dist/components/index.js.map +1 -1
  115. package/dist/components/navigation/index.cjs +2 -2
  116. package/dist/components/navigation/index.js +1 -1
  117. package/dist/components/onboarding-guides/index.cjs +5 -5
  118. package/dist/components/onboarding-guides/index.js +4 -4
  119. package/dist/components/onboarding-guides/onboarding-guide-detail-view.d.ts.map +1 -1
  120. package/dist/components/related-content/index.cjs +3 -3
  121. package/dist/components/related-content/index.js +2 -2
  122. package/dist/components/shared/product-release/release-detail-page.d.ts.map +1 -1
  123. package/dist/components/tickets/index.cjs +5 -5
  124. package/dist/components/tickets/index.js +4 -4
  125. package/dist/components/ui/index.cjs +44 -2
  126. package/dist/components/ui/index.cjs.map +1 -1
  127. package/dist/components/ui/index.d.ts +1 -2
  128. package/dist/components/ui/index.d.ts.map +1 -1
  129. package/dist/components/ui/index.js +55 -13
  130. package/dist/components/ui/markdown/base-components.d.ts +51 -0
  131. package/dist/components/ui/markdown/base-components.d.ts.map +1 -0
  132. package/dist/components/ui/markdown/engine.d.ts +99 -0
  133. package/dist/components/ui/markdown/engine.d.ts.map +1 -0
  134. package/dist/components/ui/markdown/heading-ids.d.ts +65 -0
  135. package/dist/components/ui/markdown/heading-ids.d.ts.map +1 -0
  136. package/dist/components/ui/markdown/index.d.ts +18 -0
  137. package/dist/components/ui/markdown/index.d.ts.map +1 -0
  138. package/dist/components/ui/markdown/mermaid-diagram.d.ts +79 -0
  139. package/dist/components/ui/markdown/mermaid-diagram.d.ts.map +1 -0
  140. package/dist/components/ui/markdown/rich/embed-overrides.d.ts +14 -0
  141. package/dist/components/ui/markdown/rich/embed-overrides.d.ts.map +1 -0
  142. package/dist/components/ui/markdown/rich/rich-markdown-renderer.d.ts +40 -0
  143. package/dist/components/ui/markdown/rich/rich-markdown-renderer.d.ts.map +1 -0
  144. package/dist/components/ui/markdown/rich/shortcodes.d.ts +8 -0
  145. package/dist/components/ui/markdown/rich/shortcodes.d.ts.map +1 -0
  146. package/dist/components/ui/markdown/sanitize.d.ts +153 -0
  147. package/dist/components/ui/markdown/sanitize.d.ts.map +1 -0
  148. package/dist/components/ui/markdown/simple-markdown-renderer.d.ts +23 -0
  149. package/dist/components/ui/markdown/simple-markdown-renderer.d.ts.map +1 -0
  150. package/dist/components/ui/markdown/streaming.d.ts +78 -0
  151. package/dist/components/ui/markdown/streaming.d.ts.map +1 -0
  152. package/dist/components/ui/markdown/text-size.d.ts +34 -0
  153. package/dist/components/ui/markdown/text-size.d.ts.map +1 -0
  154. package/dist/index.cjs +60 -2
  155. package/dist/index.cjs.map +1 -1
  156. package/dist/index.js +71 -13
  157. package/dist/utils/index.cjs +169 -40
  158. package/dist/utils/index.cjs.map +1 -1
  159. package/dist/utils/index.d.ts +1 -0
  160. package/dist/utils/index.d.ts.map +1 -1
  161. package/dist/utils/index.js +162 -41
  162. package/dist/utils/index.js.map +1 -1
  163. package/dist/utils/markdown-fences.d.ts +42 -0
  164. package/dist/utils/markdown-fences.d.ts.map +1 -0
  165. package/dist/utils/markdown-heading-id.d.ts +85 -0
  166. package/dist/utils/markdown-heading-id.d.ts.map +1 -0
  167. package/dist/utils/markdown-section-extractor.d.ts.map +1 -1
  168. package/package.json +7 -1
  169. package/src/chat-protocol/__tests__/__snapshots__/nats-decoder-golden.test.ts.snap +317 -0
  170. package/src/chat-protocol/__tests__/chat-protocol.test.ts +516 -0
  171. package/src/chat-protocol/__tests__/env-flag.test.ts +52 -0
  172. package/src/chat-protocol/__tests__/ip-normalize.test.ts +136 -0
  173. package/src/chat-protocol/__tests__/nats-decoder-golden.test.ts +232 -0
  174. package/src/chat-protocol/decode.ts +278 -0
  175. package/src/chat-protocol/encode.ts +71 -0
  176. package/src/chat-protocol/env-flag.ts +39 -0
  177. package/src/chat-protocol/events.ts +245 -0
  178. package/src/chat-protocol/frames.ts +245 -0
  179. package/src/chat-protocol/index.ts +21 -0
  180. package/src/chat-protocol/ip-normalize.ts +146 -0
  181. package/src/chat-protocol/nats-decoder.ts +258 -0
  182. package/src/components/chat/__tests__/chat-message-list.test.tsx +381 -6
  183. package/src/components/chat/__tests__/chat-message-streaming-memo.test.tsx +207 -0
  184. package/src/components/chat/__tests__/chat-pending-turn.test.tsx +250 -0
  185. package/src/components/chat/chat-message-enhanced.tsx +122 -20
  186. package/src/components/chat/chat-message-list.tsx +409 -132
  187. package/src/components/chat/embeddable-chat.tsx +6 -0
  188. package/src/components/chat/hooks/.index.md +30 -33
  189. package/src/components/chat/hooks/.use-nats-chat-adapter.md +36 -55
  190. package/src/components/chat/hooks/__tests__/__snapshots__/conversation-id-persistence-golden.test.ts.snap +37 -0
  191. package/src/components/chat/hooks/__tests__/__snapshots__/sse-stream-golden.test.ts.snap +0 -0
  192. package/src/components/chat/hooks/__tests__/chunk-catchup-dialog-staleness.test.ts +92 -0
  193. package/src/components/chat/hooks/__tests__/conversation-id-persistence-golden.test.ts +438 -0
  194. package/src/components/chat/hooks/__tests__/sse-stream-golden.test.ts +318 -0
  195. package/src/components/chat/hooks/index.ts +0 -1
  196. package/src/components/chat/hooks/use-chat.ts +8 -0
  197. package/src/components/chat/hooks/use-chunk-catchup.ts +26 -1
  198. package/src/components/chat/hooks/use-nats-chat-adapter.ts +254 -825
  199. package/src/components/chat/hooks/use-sse-chat-adapter.ts +380 -684
  200. package/src/components/chat/index.ts +5 -0
  201. package/src/components/chat/stream/__tests__/__snapshots__/chat-stream-reducer-golden.test.ts.snap +940 -0
  202. package/src/components/chat/stream/__tests__/chat-dialog-store.test.ts +806 -0
  203. package/src/components/chat/stream/__tests__/chat-stream-reducer-golden.test.ts +467 -0
  204. package/src/components/chat/stream/__tests__/chat-stream-reducer.test.ts +1248 -0
  205. package/src/components/chat/stream/__tests__/delta-batcher.test.ts +255 -0
  206. package/src/components/chat/stream/__tests__/use-chat-stream-reducer.test.ts +105 -0
  207. package/src/components/chat/stream/chat-dialog-store.ts +536 -0
  208. package/src/components/chat/stream/chat-stream-reducer.ts +1876 -0
  209. package/src/components/chat/stream/delta-batcher.ts +168 -0
  210. package/src/components/chat/stream/index.ts +55 -0
  211. package/src/components/chat/stream/message-mutations.ts +340 -0
  212. package/src/components/chat/stream/use-chat-stream-reducer.ts +126 -0
  213. package/src/components/chat/thinking-display.tsx +1 -1
  214. package/src/components/chat/types/.api.types.md +39 -49
  215. package/src/components/chat/types/.processing.types.md +29 -58
  216. package/src/components/chat/types/api.types.ts +6 -73
  217. package/src/components/chat/types/processing.types.ts +11 -53
  218. package/src/components/chat/utils/.index.md +31 -40
  219. package/src/components/chat/utils/__tests__/__snapshots__/process-historical-messages-golden.test.ts.snap +420 -0
  220. package/src/components/chat/utils/__tests__/__snapshots__/segment-accumulator-golden.test.ts.snap +605 -0
  221. package/src/components/chat/utils/__tests__/process-historical-messages-approvals.test.ts +61 -0
  222. package/src/components/chat/utils/__tests__/process-historical-messages-golden.test.ts +394 -0
  223. package/src/components/chat/utils/__tests__/segment-accumulator-golden.test.ts +270 -0
  224. package/src/components/chat/utils/extract-incomplete-message-state.ts +63 -5
  225. package/src/components/chat/utils/index.ts +5 -8
  226. package/src/components/chat/utils/process-historical-messages.ts +358 -386
  227. package/src/components/onboarding-guides/onboarding-guide-detail-view.tsx +22 -4
  228. package/src/components/shared/legal-document/legal-document-page.tsx +1 -1
  229. package/src/components/shared/product-release/release-detail-page.tsx +19 -4
  230. package/src/components/ui/__tests__/__snapshots__/markdown-parity.test.tsx.snap +2420 -0
  231. package/src/components/ui/__tests__/markdown-parity.test.tsx +2540 -0
  232. package/src/components/ui/index.ts +1 -2
  233. package/src/components/ui/markdown/__tests__/mermaid-security.test.ts +176 -0
  234. package/src/components/ui/markdown/__tests__/mermaid-stale-render.test.tsx +151 -0
  235. package/src/components/ui/markdown/__tests__/sanitize-invariant.test.ts +354 -0
  236. package/src/components/ui/markdown/__tests__/sanitize-render.test.tsx +118 -0
  237. package/src/components/ui/markdown/__tests__/streaming.test.tsx +436 -0
  238. package/src/components/ui/markdown/base-components.tsx +360 -0
  239. package/src/components/ui/markdown/engine.tsx +315 -0
  240. package/src/components/ui/markdown/heading-ids.ts +239 -0
  241. package/src/components/ui/markdown/index.ts +49 -0
  242. package/src/components/ui/markdown/mermaid-diagram.tsx +323 -0
  243. package/src/components/ui/markdown/rich/embed-overrides.tsx +199 -0
  244. package/src/components/ui/markdown/rich/rich-markdown-renderer.tsx +184 -0
  245. package/src/components/ui/markdown/rich/shortcodes.ts +170 -0
  246. package/src/components/ui/markdown/sanitize.ts +3584 -0
  247. package/src/components/ui/markdown/simple-markdown-renderer.tsx +28 -0
  248. package/src/components/ui/markdown/streaming.ts +390 -0
  249. package/src/components/ui/markdown/text-size.ts +106 -0
  250. package/src/components/ui/release-changelog-section.tsx +1 -1
  251. package/src/components/ui/ticket-info-section.tsx +1 -1
  252. package/src/utils/index.ts +1 -0
  253. package/src/utils/markdown-fences.ts +127 -0
  254. package/src/utils/markdown-heading-id.ts +348 -0
  255. package/src/utils/markdown-section-extractor.ts +41 -56
  256. package/dist/chunk-23QJPSZJ.cjs.map +0 -1
  257. package/dist/chunk-5KSXMJXF.cjs.map +0 -1
  258. package/dist/chunk-MEIMDHQ2.cjs.map +0 -1
  259. package/dist/chunk-NT3BKPEC.js.map +0 -1
  260. package/dist/chunk-YBTYHIXA.js.map +0 -1
  261. package/dist/chunk-ZAAIVSX6.js.map +0 -1
  262. package/dist/components/chat/hooks/use-realtime-chunk-processor.d.ts +0 -6
  263. package/dist/components/chat/hooks/use-realtime-chunk-processor.d.ts.map +0 -1
  264. package/dist/components/chat/utils/chunk-parser.d.ts +0 -25
  265. package/dist/components/chat/utils/chunk-parser.d.ts.map +0 -1
  266. package/dist/components/ui/rich-markdown-renderer.d.ts +0 -34
  267. package/dist/components/ui/rich-markdown-renderer.d.ts.map +0 -1
  268. package/dist/components/ui/simple-markdown-renderer.d.ts +0 -73
  269. package/dist/components/ui/simple-markdown-renderer.d.ts.map +0 -1
  270. package/src/components/chat/hooks/.use-realtime-chunk-processor.md +0 -77
  271. package/src/components/chat/hooks/use-realtime-chunk-processor.ts +0 -489
  272. package/src/components/chat/utils/.chunk-parser.md +0 -62
  273. package/src/components/chat/utils/chunk-parser.ts +0 -262
  274. package/src/components/ui/.simple-markdown-renderer.md +0 -52
  275. package/src/components/ui/rich-markdown-renderer.tsx +0 -1223
  276. package/src/components/ui/simple-markdown-renderer.tsx +0 -964
  277. /package/dist/{chunk-ZPUCCSYA.js.map → chunk-34C2JV2C.js.map} +0 -0
  278. /package/dist/{chunk-GWVNRXJU.js.map → chunk-3VGRZ62V.js.map} +0 -0
  279. /package/dist/{chunk-AAHAVML2.js.map → chunk-42NYDEHH.js.map} +0 -0
  280. /package/dist/{chunk-ZEHA4IC7.js.map → chunk-42SMPKGY.js.map} +0 -0
  281. /package/dist/{chunk-HRUXC3YN.js.map → chunk-5QWYIPMH.js.map} +0 -0
  282. /package/dist/{chunk-DGDUOFNY.js.map → chunk-BUBQEZ5J.js.map} +0 -0
  283. /package/dist/{chunk-6XQGPFIR.js.map → chunk-WNXIG6YY.js.map} +0 -0
  284. /package/dist/{chunk-5INZDVGI.js.map → chunk-WZCQW4MW.js.map} +0 -0
  285. /package/dist/{chunk-B3MRHZNZ.js.map → chunk-ZHC2BCXK.js.map} +0 -0
@@ -1,15 +1,28 @@
1
- import { render } from '@testing-library/react'
1
+ import { act, render } from '@testing-library/react'
2
2
  import { beforeEach, describe, expect, it, vi } from 'vitest'
3
3
  import type { Message } from '../types/message.types'
4
4
 
5
5
  const scrollToBottom = vi.fn()
6
6
  const stopScroll = vi.fn()
7
7
 
8
+ /**
9
+ * `detachCount` records `ref(null)` invocations. React calls a ref callback with
10
+ * null and then with the element whenever the callback IDENTITY changes, and the
11
+ * real `use-stick-to-bottom` responds to the null by disconnecting its
12
+ * ResizeObserver and dropping its scroll/wheel listeners — so counting detaches
13
+ * is how a test can see the adapter-identity churn that a plain recording ref
14
+ * would silently absorb.
15
+ */
8
16
  function makeRef() {
9
17
  const fn = ((el: HTMLElement | null) => {
18
+ if (el === null) (fn as { detachCount: number }).detachCount += 1
10
19
  ;(fn as { current: HTMLElement | null }).current = el
11
- }) as ((el: HTMLElement | null) => void) & { current: HTMLElement | null }
20
+ }) as ((el: HTMLElement | null) => void) & {
21
+ current: HTMLElement | null
22
+ detachCount: number
23
+ }
12
24
  fn.current = null
25
+ fn.detachCount = 0
13
26
  return fn
14
27
  }
15
28
  const scrollRefMock = makeRef()
@@ -26,9 +39,16 @@ vi.mock('use-stick-to-bottom', () => ({
26
39
 
27
40
  // Keep the row renderer trivial — these tests exercise ChatMessageList's
28
41
  // scroll orchestration, not markdown rendering.
29
- vi.mock('../chat-message-enhanced', () => ({
30
- ChatMessageEnhanced: ({ content }: { content: string }) => <div>{content}</div>,
31
- }))
42
+ // Forwards the ref because the list registers each row element for the
43
+ // per-message `scrollAnchor: 'top'` path.
44
+ vi.mock('../chat-message-enhanced', async () => {
45
+ const { forwardRef } = await import('react')
46
+ return {
47
+ ChatMessageEnhanced: forwardRef<HTMLDivElement, { content: string }>(({ content }, ref) => (
48
+ <div ref={ref}>{content}</div>
49
+ )),
50
+ }
51
+ })
32
52
 
33
53
  import { ChatMessageList } from '../chat-message-list'
34
54
 
@@ -40,7 +60,36 @@ class ObserverStub {
40
60
  disconnect() {}
41
61
  }
42
62
  vi.stubGlobal('IntersectionObserver', ObserverStub)
43
- vi.stubGlobal('ResizeObserver', ObserverStub)
63
+
64
+ // Recording RO so the bottom-follow tests can drive a resize by hand.
65
+ const resizeCallbacks: Array<() => void> = []
66
+ class RecordingResizeObserver {
67
+ constructor(cb: () => void) {
68
+ resizeCallbacks.push(cb)
69
+ }
70
+ observe() {}
71
+ unobserve() {}
72
+ disconnect() {}
73
+ }
74
+ vi.stubGlobal('ResizeObserver', RecordingResizeObserver)
75
+ /** Fire every live ResizeObserver callback — same effect as any content
76
+ * or scroller-box size change in the real DOM. */
77
+ const fireResize = () => {
78
+ for (const cb of resizeCallbacks) cb()
79
+ }
80
+
81
+ /** jsdom has no layout, so every box metric reads 0. The jump-to-bottom
82
+ * button is driven off real geometry, so the tests supply it. */
83
+ const setGeometry = (geo: { scrollHeight: number; clientHeight: number; scrollTop: number }) => {
84
+ const el = scrollRefMock.current
85
+ if (!el) throw new Error('scroller not mounted')
86
+ for (const [key, value] of Object.entries(geo)) {
87
+ Object.defineProperty(el, key, { value, configurable: true, writable: true })
88
+ }
89
+ }
90
+
91
+ // jsdom has no layout, so `scrollIntoView` is undefined.
92
+ Element.prototype.scrollIntoView = vi.fn()
44
93
 
45
94
  const msg = (id: string, role: Message['role']): Message => ({
46
95
  id,
@@ -90,3 +139,329 @@ describe('ChatMessageList force-scroll decisions', () => {
90
139
  expect(scrollToBottom).not.toHaveBeenCalled()
91
140
  })
92
141
  })
142
+
143
+ // The library's own `isAtBottom` lock is lost silently when the scroller's
144
+ // box changes (source chips mounting below it) or when a card settling out
145
+ // of its skeleton produces a resize-driven scroll the library reads as a
146
+ // user gesture. The list therefore owns the INTENT and re-asserts on every
147
+ // geometry change until a real gesture releases it.
148
+ describe('ChatMessageList bottom-follow intent', () => {
149
+ beforeEach(() => {
150
+ scrollToBottom.mockClear()
151
+ resizeCallbacks.length = 0
152
+ })
153
+
154
+ const sendTurn = () => {
155
+ const initial = [msg('m1', 'user'), msg('m2', 'assistant')]
156
+ const view = render(<ChatMessageList dialogId="d1" messages={initial} />)
157
+ // A user send arms the follow intent for the whole turn.
158
+ view.rerender(<ChatMessageList dialogId="d1" messages={[...initial, msg('m3', 'user')]} />)
159
+ scrollToBottom.mockClear()
160
+ return view
161
+ }
162
+
163
+ it('re-asserts the bottom on geometry changes after the send', () => {
164
+ sendTurn()
165
+ fireResize()
166
+ expect(scrollToBottom).toHaveBeenCalled()
167
+ })
168
+
169
+ it('keeps re-asserting for late async growth (cards + covers resolving)', () => {
170
+ sendTurn()
171
+ fireResize()
172
+ fireResize()
173
+ fireResize()
174
+ expect(scrollToBottom).toHaveBeenCalledTimes(3)
175
+ })
176
+
177
+ it('releases the intent once the user scrolls up', () => {
178
+ sendTurn()
179
+ scrollRefMock.current?.dispatchEvent(
180
+ new WheelEvent('wheel', { deltaY: -120, bubbles: true }),
181
+ )
182
+ fireResize()
183
+ expect(scrollToBottom).not.toHaveBeenCalled()
184
+ })
185
+
186
+ it('ignores downward wheel (never an escape from the bottom)', () => {
187
+ sendTurn()
188
+ scrollRefMock.current?.dispatchEvent(
189
+ new WheelEvent('wheel', { deltaY: 120, bubbles: true }),
190
+ )
191
+ fireResize()
192
+ expect(scrollToBottom).toHaveBeenCalled()
193
+ })
194
+
195
+ it('does not follow when autoScroll is off (passive demo hosts)', () => {
196
+ const initial = [msg('m1', 'user'), msg('m2', 'assistant')]
197
+ const { rerender } = render(
198
+ <ChatMessageList dialogId="d1" messages={initial} autoScroll={false} />,
199
+ )
200
+ rerender(
201
+ <ChatMessageList dialogId="d1" messages={[...initial, msg('m3', 'user')]} autoScroll={false} />,
202
+ )
203
+ scrollToBottom.mockClear()
204
+ fireResize()
205
+ expect(scrollToBottom).not.toHaveBeenCalled()
206
+ })
207
+
208
+ it('hides jump-to-bottom while the thread is at the bottom', () => {
209
+ const view = sendTurn()
210
+ setGeometry({ scrollHeight: 1000, clientHeight: 500, scrollTop: 500 })
211
+ act(() => fireResize())
212
+ expect(view.queryByLabelText('Scroll to latest message')).toBeNull()
213
+ })
214
+
215
+ it('shows jump-to-bottom once the thread is away from the bottom', () => {
216
+ const view = sendTurn()
217
+ setGeometry({ scrollHeight: 20000, clientHeight: 500, scrollTop: 500 })
218
+ act(() => fireResize())
219
+ expect(view.queryByLabelText('Scroll to latest message')).not.toBeNull()
220
+ })
221
+
222
+ it('re-arms the intent when the user clicks jump-to-bottom', () => {
223
+ const view = sendTurn()
224
+ // User scrolls away: intent released, button appears.
225
+ scrollRefMock.current?.dispatchEvent(
226
+ new WheelEvent('wheel', { deltaY: -120, bubbles: true }),
227
+ )
228
+ setGeometry({ scrollHeight: 20000, clientHeight: 500, scrollTop: 500 })
229
+ act(() => fireResize())
230
+ expect(scrollToBottom).not.toHaveBeenCalled()
231
+
232
+ const button = view.getByLabelText('Scroll to latest message')
233
+ act(() => {
234
+ button.click()
235
+ })
236
+ expect(scrollToBottom).toHaveBeenCalled()
237
+
238
+ // …and the click re-armed the lock, so growth follows again.
239
+ scrollToBottom.mockClear()
240
+ act(() => fireResize())
241
+ expect(scrollToBottom).toHaveBeenCalled()
242
+ })
243
+
244
+ // A drag that ends OUTSIDE the window (button released off-screen, or the
245
+ // tab losing focus mid-drag) never delivers `pointerup`, so a naive
246
+ // `pointerDown` flag stays true forever. Afterwards the very thing this
247
+ // component exists for — a fetch-mode card settling from skeleton to a
248
+ // SHORTER height, which clamps scrollTop down — is misread as a scroll-up
249
+ // gesture and silently drops the lock.
250
+ const dragThenScrollDown = (endEvent: { target: 'window'; type: string }) => {
251
+ sendTurn()
252
+ // Park away from the bottom so the at-bottom re-arm below can't mask the
253
+ // result, and seed `lastScrollTop`.
254
+ setGeometry({ scrollHeight: 20000, clientHeight: 500, scrollTop: 4000 })
255
+ window.dispatchEvent(new Event('pointerdown'))
256
+ act(() => {
257
+ scrollRefMock.current?.dispatchEvent(new Event('scroll'))
258
+ })
259
+ // Drag ends where we can't see it.
260
+ if (endEvent.target === 'window') window.dispatchEvent(new Event(endEvent.type))
261
+ // Resize clamp: content shrank, scrollTop drops. NOT a gesture.
262
+ setGeometry({ scrollHeight: 20000, clientHeight: 500, scrollTop: 3800 })
263
+ act(() => {
264
+ scrollRefMock.current?.dispatchEvent(new Event('scroll'))
265
+ })
266
+ scrollToBottom.mockClear()
267
+ act(() => fireResize())
268
+ }
269
+
270
+ it('keeps the intent when a pointercancel ended the drag (resize clamp is not a gesture)', () => {
271
+ dragThenScrollDown({ target: 'window', type: 'pointercancel' })
272
+ expect(scrollToBottom).toHaveBeenCalled()
273
+ })
274
+
275
+ it('keeps the intent when the window lost focus mid-drag', () => {
276
+ dragThenScrollDown({ target: 'window', type: 'blur' })
277
+ expect(scrollToBottom).toHaveBeenCalled()
278
+ })
279
+
280
+ // Focus events do not bubble but they DO capture, so a capture-phase `blur`
281
+ // listener on `window` fires for EVERY element blur in the document — the
282
+ // classic focus-delegation idiom. Registering the drag-end `blur` that way
283
+ // made any focus change during a pointer hold (most commonly the chat input
284
+ // blurring as the user presses down on the scrollbar) clear `pointerDown`,
285
+ // which is exactly what re-opens the yank-back-mid-drag bug. Only a TRUE
286
+ // window blur (which targets `window` itself) may end the drag.
287
+ it('a descendant element losing focus does not end the drag', () => {
288
+ sendTurn()
289
+ setGeometry({ scrollHeight: 20000, clientHeight: 500, scrollTop: 4000 })
290
+ window.dispatchEvent(new Event('pointerdown'))
291
+ act(() => {
292
+ scrollRefMock.current?.dispatchEvent(new Event('scroll'))
293
+ })
294
+ // The chat input loses focus as the pointer goes down elsewhere.
295
+ const input = document.createElement('input')
296
+ document.body.appendChild(input)
297
+ input.focus()
298
+ input.blur()
299
+ input.remove()
300
+ // The drag is still live, so this held scroll-up IS a gesture.
301
+ setGeometry({ scrollHeight: 20000, clientHeight: 500, scrollTop: 3800 })
302
+ act(() => {
303
+ scrollRefMock.current?.dispatchEvent(new Event('scroll'))
304
+ })
305
+ scrollToBottom.mockClear()
306
+ act(() => fireResize())
307
+ expect(scrollToBottom).not.toHaveBeenCalled()
308
+ window.dispatchEvent(new Event('pointerup'))
309
+ })
310
+
311
+ it('still disarms on a genuine scrollbar drag upward', () => {
312
+ sendTurn()
313
+ setGeometry({ scrollHeight: 20000, clientHeight: 500, scrollTop: 4000 })
314
+ window.dispatchEvent(new Event('pointerdown'))
315
+ act(() => {
316
+ scrollRefMock.current?.dispatchEvent(new Event('scroll'))
317
+ })
318
+ setGeometry({ scrollHeight: 20000, clientHeight: 500, scrollTop: 3800 })
319
+ act(() => {
320
+ scrollRefMock.current?.dispatchEvent(new Event('scroll'))
321
+ })
322
+ scrollToBottom.mockClear()
323
+ act(() => fireResize())
324
+ expect(scrollToBottom).not.toHaveBeenCalled()
325
+ window.dispatchEvent(new Event('pointerup'))
326
+ })
327
+
328
+ // Scrolling back down to the live end by hand expresses exactly the intent
329
+ // the jump-to-bottom button does — the lock has to re-arm, or the thread
330
+ // renders at-bottom (button hidden) and then silently drifts away.
331
+ it('re-arms the intent when the user scrolls back to the bottom by hand', () => {
332
+ sendTurn()
333
+ scrollRefMock.current?.dispatchEvent(new WheelEvent('wheel', { deltaY: -120, bubbles: true }))
334
+ setGeometry({ scrollHeight: 20000, clientHeight: 500, scrollTop: 500 })
335
+ act(() => fireResize())
336
+ expect(scrollToBottom).not.toHaveBeenCalled()
337
+
338
+ // …and back down to the end.
339
+ setGeometry({ scrollHeight: 20000, clientHeight: 500, scrollTop: 19500 })
340
+ act(() => {
341
+ scrollRefMock.current?.dispatchEvent(new Event('scroll'))
342
+ })
343
+ scrollToBottom.mockClear()
344
+ act(() => fireResize())
345
+ expect(scrollToBottom).toHaveBeenCalled()
346
+ })
347
+
348
+ it('does not re-arm while the user is still parked above the bottom', () => {
349
+ sendTurn()
350
+ scrollRefMock.current?.dispatchEvent(new WheelEvent('wheel', { deltaY: -120, bubbles: true }))
351
+ setGeometry({ scrollHeight: 20000, clientHeight: 500, scrollTop: 8000 })
352
+ act(() => {
353
+ scrollRefMock.current?.dispatchEvent(new Event('scroll'))
354
+ })
355
+ scrollToBottom.mockClear()
356
+ act(() => fireResize())
357
+ expect(scrollToBottom).not.toHaveBeenCalled()
358
+ })
359
+
360
+ // A short scrollbar drag that starts at the live end and stays inside the
361
+ // 70px band disarms on the gesture and would immediately re-arm on the very
362
+ // same scroll event if the re-arm didn't wait for the drag to END — yanking
363
+ // the reader back down mid-drag, a miniature of the bug this block exists
364
+ // to prevent.
365
+ it('does not re-arm mid-drag when the drag stays inside the bottom band', () => {
366
+ sendTurn()
367
+ setGeometry({ scrollHeight: 20000, clientHeight: 500, scrollTop: 19500 })
368
+ act(() => {
369
+ scrollRefMock.current?.dispatchEvent(new Event('scroll'))
370
+ })
371
+ window.dispatchEvent(new Event('pointerdown'))
372
+ setGeometry({ scrollHeight: 20000, clientHeight: 500, scrollTop: 19460 })
373
+ act(() => {
374
+ scrollRefMock.current?.dispatchEvent(new Event('scroll'))
375
+ })
376
+ scrollToBottom.mockClear()
377
+ act(() => fireResize())
378
+ expect(scrollToBottom).not.toHaveBeenCalled()
379
+ window.dispatchEvent(new Event('pointerup'))
380
+ })
381
+
382
+ it('releases the intent for a top-anchored turn (it parks at the message top)', () => {
383
+ const initial = [msg('m1', 'user'), msg('m2', 'assistant')]
384
+ const { rerender } = render(<ChatMessageList dialogId="d1" messages={initial} />)
385
+ const sent = [...initial, msg('m3', 'user')]
386
+ rerender(<ChatMessageList dialogId="d1" messages={sent} />)
387
+ const anchored: Message = { ...msg('m4', 'assistant'), scrollAnchor: 'top' }
388
+ rerender(<ChatMessageList dialogId="d1" messages={[...sent, anchored]} />)
389
+ scrollToBottom.mockClear()
390
+ fireResize()
391
+ expect(scrollToBottom).not.toHaveBeenCalled()
392
+ })
393
+ })
394
+
395
+ /**
396
+ * The whole bottom-follow subsystem — ResizeObserver, wheel/touch/keyboard
397
+ * escape hatches, the jump-to-bottom button — is installed by ONE effect whose
398
+ * deps were all identity-stable (`autoScroll` plus three `[]`-memoized values
399
+ * from `useStickToBottom`). `isLoading` was missing, and on the loading branch
400
+ * the component returns the skeleton, so `scrollRef.current` is null and the
401
+ * effect bails.
402
+ *
403
+ * Net result for any consumer following this repo's own prescribed
404
+ * "skeleton during fetch" pattern: the effect ran once against null refs and
405
+ * NEVER re-ran, so nothing was ever observed, `atBottom` stayed at its initial
406
+ * `true`, the WCAG escape hatch could not render, and the follow lock was never
407
+ * re-asserted — while the observers/listeners bound on a later round trip kept
408
+ * detached nodes alive.
409
+ */
410
+ describe('ChatMessageList bottom-follow survives the loading skeleton', () => {
411
+ beforeEach(() => {
412
+ scrollToBottom.mockClear()
413
+ resizeCallbacks.length = 0
414
+ })
415
+
416
+ it('installs the follow observers when the skeleton is replaced by the list', () => {
417
+ const messages = [msg('m1', 'user'), msg('m2', 'assistant')]
418
+ const { rerender } = render(
419
+ <ChatMessageList dialogId="d1" messages={messages} isLoading />,
420
+ )
421
+ // Nothing to observe while the skeleton is up.
422
+ expect(resizeCallbacks).toHaveLength(0)
423
+
424
+ rerender(<ChatMessageList dialogId="d1" messages={messages} />)
425
+ expect(resizeCallbacks.length).toBeGreaterThan(0)
426
+ })
427
+
428
+ it('shows the jump-to-bottom escape hatch after a loading round trip', () => {
429
+ const messages = [msg('m1', 'user'), msg('m2', 'assistant')]
430
+ const view = render(<ChatMessageList dialogId="d1" messages={messages} />)
431
+ // A host loading another page / another dialog swaps the scroller out…
432
+ view.rerender(<ChatMessageList dialogId="d1" messages={messages} isLoading />)
433
+ view.rerender(<ChatMessageList dialogId="d1" messages={messages} />)
434
+
435
+ // …and the list must still notice it is scrolled away from the bottom.
436
+ setGeometry({ scrollHeight: 20000, clientHeight: 500, scrollTop: 0 })
437
+ act(() => {
438
+ scrollRefMock.current?.dispatchEvent(new Event('scroll'))
439
+ })
440
+ expect(view.queryByLabelText('Scroll to latest message')).not.toBeNull()
441
+ })
442
+
443
+ it('keeps the ref adapters identity-stable across re-renders', () => {
444
+ // The adapters are handed to `useStickToBottom`'s ref callbacks, and the
445
+ // library disconnects its ResizeObserver on `ref(null)` and builds a NEW
446
+ // one on re-attach. A fresh arrow per render therefore churned an observer
447
+ // (and two scroll listeners) on every committed frame — ~60/s while
448
+ // streaming — and reset the library's `previousHeight`, which is what it
449
+ // uses to choose between its `initial` and `resize` animations.
450
+ const messages = [msg('m1', 'user'), msg('m2', 'assistant')]
451
+ const view = render(<ChatMessageList dialogId="d1" messages={messages} />)
452
+ const detachesAfterMount = {
453
+ scroll: scrollRefMock.detachCount,
454
+ content: contentRefMock.detachCount,
455
+ }
456
+
457
+ for (let i = 0; i < 5; i++) {
458
+ view.rerender(<ChatMessageList dialogId="d1" messages={[...messages]} />)
459
+ }
460
+
461
+ // A memoized adapter is never re-invoked with null, so the library keeps its
462
+ // observer and listeners for the whole mount.
463
+ expect(scrollRefMock.detachCount).toBe(detachesAfterMount.scroll)
464
+ expect(contentRefMock.detachCount).toBe(detachesAfterMount.content)
465
+ expect(scrollRefMock.current).not.toBeNull()
466
+ })
467
+ })
@@ -0,0 +1,207 @@
1
+ /**
2
+ * REGRESSION: `ChatMessageEnhanced` must hand the markdown engine a
3
+ * REFERENTIALLY STABLE `componentOverrides` map across a streamed turn.
4
+ *
5
+ * The engine memoizes each COMPLETED block (`StreamingBlockRenderer`) on its
6
+ * `components` prop identity. `ChatMessageEnhanced` is the sole production
7
+ * caller, and it used to defeat that memo one level up: `cardComponentOverrides`
8
+ * listed `renderingPlan` in its deps, `renderingPlan` is memoized on `segments`,
9
+ * and the stream reducer replaces `segments` on EVERY text delta. So the
10
+ * override map got a new identity per token → the engine's `components` memo
11
+ * re-created → every completed block re-parsed on every token.
12
+ *
13
+ * The engine's own streaming test cannot catch this: it passes a module-scope
14
+ * stable override map. This test drives the real component with the real
15
+ * per-delta `content` churn a reducer produces, and asserts at the SEAM (the
16
+ * props the component hands the renderer) rather than through the engine's
17
+ * internals — the two are separately owned.
18
+ */
19
+
20
+ import React from 'react'
21
+ import { describe, it, expect, vi } from 'vitest'
22
+ import { render } from '@testing-library/react'
23
+ import type { MessageSegment } from '../types'
24
+ import type { ChatRef } from '../chat-ref.types'
25
+
26
+ /** Every `componentOverrides` / `additionalRemarkPlugins` identity the
27
+ * component handed the markdown renderer, in render order. */
28
+ const seenOverrides: unknown[] = []
29
+ const seenPlugins: unknown[] = []
30
+
31
+ vi.mock('../../ui/markdown/simple-markdown-renderer', () => ({
32
+ SimpleMarkdownRenderer: ({
33
+ content,
34
+ componentOverrides,
35
+ additionalRemarkPlugins,
36
+ }: {
37
+ content: string
38
+ componentOverrides?: unknown
39
+ additionalRemarkPlugins?: unknown
40
+ }) => {
41
+ seenOverrides.push(componentOverrides)
42
+ seenPlugins.push(additionalRemarkPlugins)
43
+ return <div data-testid="md">{content}</div>
44
+ },
45
+ }))
46
+
47
+ import { ChatMessageEnhanced } from '../chat-message-enhanced'
48
+
49
+ function NavLinkAnchor({ href, children }: React.AnchorHTMLAttributes<HTMLAnchorElement>) {
50
+ return <a href={href}>{children}</a>
51
+ }
52
+ const CHAT_REFS: Record<string, ChatRef> = {}
53
+ const renderMention = () => null
54
+
55
+ /** Fresh array per call — the reducer replaces `segments` on every delta. */
56
+ const segmentsFor = (tail: string): MessageSegment[] => [
57
+ { type: 'text', text: `First paragraph, already complete.\n\n${tail}` },
58
+ ]
59
+
60
+ describe('ChatMessageEnhanced — override identity across a streamed turn', () => {
61
+ it('keeps componentOverrides + remark plugins referentially stable per token', () => {
62
+ seenOverrides.length = 0
63
+ seenPlugins.length = 0
64
+
65
+ const props = {
66
+ role: 'assistant' as const,
67
+ isTyping: true,
68
+ chatRefs: CHAT_REFS,
69
+ renderMention,
70
+ NavLinkAnchor,
71
+ }
72
+
73
+ const view = render(<ChatMessageEnhanced {...props} content={segmentsFor('Tail')} />)
74
+ expect(seenOverrides.length).toBeGreaterThan(0)
75
+ const firstOverrides = seenOverrides[0]
76
+ const firstPlugins = seenPlugins[0]
77
+ expect(firstOverrides).toBeTruthy()
78
+
79
+ let tail = 'Tail'
80
+ for (let i = 0; i < 10; i += 1) {
81
+ tail += ` token${i}`
82
+ view.rerender(<ChatMessageEnhanced {...props} content={segmentsFor(tail)} />)
83
+ }
84
+
85
+ // ONE identity for the whole turn. Before the fix this produced a fresh
86
+ // object on each of the 11 renders.
87
+ expect(new Set(seenOverrides).size).toBe(1)
88
+ expect(seenOverrides.every((o) => o === firstOverrides)).toBe(true)
89
+ expect(new Set(seenPlugins).size).toBe(1)
90
+ expect(seenPlugins.every((p) => p === firstPlugins)).toBe(true)
91
+
92
+ // Sanity: the tail really did stream through.
93
+ expect(view.container.textContent).toContain('token9')
94
+ })
95
+
96
+ it('still resolves card markers through the CURRENT plan (ref-read, not closure)', () => {
97
+ seenOverrides.length = 0
98
+ const refs: Record<string, ChatRef> = {
99
+ 'blog:abc': { type: 'blog', id: 'abc', title: 'Resolved Title', url: null },
100
+ }
101
+ render(
102
+ <ChatMessageEnhanced
103
+ role="assistant"
104
+ content={[{ type: 'text', text: 'See [card://blog:abc] for more.' }]}
105
+ chatRefs={refs}
106
+ NavLinkAnchor={NavLinkAnchor}
107
+ />,
108
+ )
109
+ const overrides = seenOverrides[0] as { a: React.FC<{ href: string; children?: React.ReactNode }> }
110
+ const out = render(<>{overrides.a({ href: 'card://blog:abc', children: 'x' })}</>)
111
+ expect(out.container.textContent).toBe('Resolved Title')
112
+ })
113
+
114
+ /**
115
+ * REGRESSION (round 3): a ref that resolves AFTER the message finished
116
+ * streaming must still reach the pill. The override reads `chatRefs`
117
+ * through a ref, so its own closure is fine — but the markdown engine
118
+ * memoizes completed blocks on the override map's IDENTITY. With
119
+ * `chatRefs` excluded from the override memo's deps entirely, the map kept
120
+ * its old identity, the engine bailed, and the marker rendered its raw-id
121
+ * fallback permanently.
122
+ */
123
+ it('gives componentOverrides a NEW identity when the ref set grows (completed message)', () => {
124
+ seenOverrides.length = 0
125
+ const content: MessageSegment[] = [{ type: 'text', text: 'See [card://blog:late] for more.' }]
126
+ const emptyRefs: Record<string, ChatRef> = {}
127
+ const props = {
128
+ role: 'assistant' as const,
129
+ isTyping: false,
130
+ content,
131
+ NavLinkAnchor,
132
+ }
133
+
134
+ const view = render(<ChatMessageEnhanced {...props} chatRefs={emptyRefs} />)
135
+ const before = seenOverrides[seenOverrides.length - 1] as {
136
+ a: React.FC<{ href: string; children?: React.ReactNode }>
137
+ }
138
+ // Unresolved: the raw card id is surfaced so the breakage is visible.
139
+ expect(render(<>{before.a({ href: 'card://blog:late', children: 'x' })}</>).container.textContent)
140
+ .toBe('late')
141
+
142
+ // Same content, same everything — only the refs map gains the entry.
143
+ const resolvedRefs: Record<string, ChatRef> = {
144
+ 'blog:late': { type: 'blog', id: 'late', title: 'Late Title', url: null },
145
+ }
146
+ view.rerender(<ChatMessageEnhanced {...props} chatRefs={resolvedRefs} />)
147
+
148
+ const after = seenOverrides[seenOverrides.length - 1] as {
149
+ a: React.FC<{ href: string; children?: React.ReactNode }>
150
+ }
151
+ expect(after).not.toBe(before)
152
+ expect(render(<>{after.a({ href: 'card://blog:late', children: 'x' })}</>).container.textContent)
153
+ .toBe('Late Title')
154
+ })
155
+
156
+ /**
157
+ * REGRESSION (round 5): a later refs frame REPLACES the map for a send index
158
+ * with the SAME key set but richer values — the `title` going from the raw
159
+ * cardId to the resolved document title is the exact production case. A
160
+ * key-set-only token produced an identical string, so the override map kept
161
+ * its identity, the engine's completed-block memo bailed, and the pill stayed
162
+ * on its fallback forever. The token fingerprints VALUES now.
163
+ */
164
+ it('gives componentOverrides a NEW identity when a ref VALUE is enriched (same key set)', () => {
165
+ seenOverrides.length = 0
166
+ const content: MessageSegment[] = [{ type: 'text', text: 'See [card://blog:abc].' }]
167
+ const props = { role: 'assistant' as const, isTyping: false, content, NavLinkAnchor }
168
+
169
+ // First frame: the ref exists but its title is still the raw card id.
170
+ const stub: Record<string, ChatRef> = {
171
+ 'blog:abc': { type: 'blog', id: 'abc', title: 'abc', url: null },
172
+ }
173
+ const view = render(<ChatMessageEnhanced {...props} chatRefs={stub} />)
174
+ const before = seenOverrides[seenOverrides.length - 1] as {
175
+ a: React.FC<{ href: string; children?: React.ReactNode }>
176
+ }
177
+ expect(render(<>{before.a({ href: 'card://blog:abc', children: 'x' })}</>).container.textContent)
178
+ .toBe('abc')
179
+
180
+ // Second frame: SAME key set, enriched value.
181
+ const enriched: Record<string, ChatRef> = {
182
+ 'blog:abc': { type: 'blog', id: 'abc', title: 'Resolved Document Title', url: null },
183
+ }
184
+ view.rerender(<ChatMessageEnhanced {...props} chatRefs={enriched} />)
185
+
186
+ const after = seenOverrides[seenOverrides.length - 1] as {
187
+ a: React.FC<{ href: string; children?: React.ReactNode }>
188
+ }
189
+ expect(after).not.toBe(before)
190
+ expect(render(<>{after.a({ href: 'card://blog:abc', children: 'x' })}</>).container.textContent)
191
+ .toBe('Resolved Document Title')
192
+ })
193
+
194
+ /** …but a same-KEY refs object (new identity, same entries) must NOT churn
195
+ * the override map — that is the streaming-stability guarantee above. */
196
+ it('keeps the override identity when only the refs object identity changes', () => {
197
+ seenOverrides.length = 0
198
+ const content: MessageSegment[] = [{ type: 'text', text: 'See [card://blog:abc].' }]
199
+ const ref: ChatRef = { type: 'blog', id: 'abc', title: 'T', url: null }
200
+ const props = { role: 'assistant' as const, content, NavLinkAnchor }
201
+
202
+ const view = render(<ChatMessageEnhanced {...props} chatRefs={{ 'blog:abc': ref }} />)
203
+ view.rerender(<ChatMessageEnhanced {...props} chatRefs={{ 'blog:abc': ref }} />)
204
+
205
+ expect(new Set(seenOverrides).size).toBe(1)
206
+ })
207
+ })