Skip to content

The agent that grows with you

test(desktop): pin the draft-text swap to the pre-paint layout phase

Brooklyn Nicholsone6121060c505
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx
index 60f1988aa3e..0f148de708f 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx
@@ -41,11 +41,13 @@ vi.mock('@assistant-ui/react', () => ({
interface ProbeHarnessProps {
activeQueueSessionKey: string | null
onLayoutSnapshot: (attachments: ComposerAttachment[]) => void
+ /** Optional pre-paint text probe: called with the composer's mirrored text in the layout phase. */
+ onTextSnapshot?: (text: string) => void
sessionId: string
}
-function ProbeHarness({ activeQueueSessionKey, onLayoutSnapshot, sessionId }: ProbeHarnessProps) {
- useComposerDraft({
+function ProbeHarness({ activeQueueSessionKey, onLayoutSnapshot, onTextSnapshot, sessionId }: ProbeHarnessProps) {
+ const { draftRef } = useComposerDraft({
activeQueueSessionKey,
focusKey: null,
inputDisabled: false,
@@ -60,6 +62,7 @@ function ProbeHarness({ activeQueueSessionKey, onLayoutSnapshot, sessionId }: Pr
// performs at render time — observes the OUTGOING session's attachments.
useLayoutEffect(() => {
onLayoutSnapshot(mainComposerScope.$attachments.get())
+ onTextSnapshot?.(draftRef.current)
})
return null
@@ -115,6 +118,60 @@ describe('useComposerDraft — attachment scope stays coherent with the committe
expect(snapshots[0]).toEqual([])
})
+ it("swaps the draft TEXT before paint: the layout phase of session B never observes A's draft (#66662 review)", () => {
+ // The attachment-scope test above pins the layout-phase guarantee for
+ // chips; this pins it for the TEXT. The review on #62586 (carried through
+ // #128476) observed that an assertion made after flushSync returns proves
+ // the eventual swap but not that the previous draft could not be painted
+ // first: a passive useEffect restore would let the browser paint session
+ // B's view with session A's text still loaded. The deterministic pre-paint
+ // probe is a useLayoutEffect that runs synchronously after the DOM commit —
+ // exactly the last moment before the browser may paint. If the restore
+ // lived in a passive effect, the probe would observe A's text here.
+ stashSessionDraft('session-A', 'draft typed in session A', [])
+ stashSessionDraft('session-B', 'draft typed in session B', [])
+
+ const textSnapshots: string[] = []
+
+ const { rerender } = render(
+ <ProbeHarness
+ activeQueueSessionKey="session-A"
+ onLayoutSnapshot={() => undefined}
+ onTextSnapshot={t => textSnapshots.push(t)}
+ sessionId="session-A"
+ />
+ )
+
+ // Mount restored A's draft — the seeded precondition, same as the
+ // attachment test.
+ expect(mockComposerApi.setText).toHaveBeenCalledWith('draft typed in session A')
+
+ mockComposerApi.setText.mockClear()
+ textSnapshots.length = 0
+
+ act(() => {
+ rerender(
+ <ProbeHarness
+ activeQueueSessionKey="session-B"
+ onLayoutSnapshot={() => undefined}
+ onTextSnapshot={t => textSnapshots.push(t)}
+ sessionId="session-B"
+ />
+ )
+ })
+
+ // Layout phase of B's switch render: the draft must already be B's. A
+ // passive (useEffect) restore leaves A's text in place at this instant —
+ // the browser would paint it first.
+ expect(textSnapshots[0]).toBe('draft typed in session B')
+
+ // The restore also reached the composer core in the same commit.
+ expect(mockComposerApi.setText).toHaveBeenCalledWith('draft typed in session B')
+
+ clearSessionDraft('session-A')
+ clearSessionDraft('session-B')
+ })
+
it('carries a pre-session draft onto the session the fresh chat is re-homed to, before its runtime id is known', () => {
const preSessionAttachment: ComposerAttachment = { id: 'file:new', kind: 'file', label: 'new.txt' }
stashSessionDraft(null, 'do not lose this draft', [preSessionAttachment])