11import type { ThreadStructure } from "@t3tools/client-runtime/state/shell" ;
2+ import { scopedThreadKey } from "@t3tools/client-runtime/environment" ;
23import {
34 EnvironmentId ,
45 ChatAttachmentId ,
@@ -9,7 +10,7 @@ import {
910 ThreadId ,
1011 type ScopedThreadRef ,
1112} from "@t3tools/contracts" ;
12- import { useImperativeHandle , type RefObject } from "react" ;
13+ import { useImperativeHandle , useLayoutEffect , useRef , type RefObject } from "react" ;
1314import { describe , expect , it , vi } from "vite-plus/test" ;
1415import {
1516 ATTACHMENT_ONLY_BOOTSTRAP_PROMPT ,
@@ -30,6 +31,7 @@ import { deriveComposerSendState } from "../ChatView.logic";
3031import { ComposerPrimaryActions } from "./ComposerPrimaryActions" ;
3132import { AppAtomRegistryProvider , appAtomRegistry } from "../../rpc/atomRegistry" ;
3233import { useQueuedRunEdit } from "./useQueuedRunEdit" ;
34+ import type { ChatComposerHandle } from "./ChatComposer" ;
3335import { readAttachmentUpload , useAttachmentUploadStore } from "../../lib/attachmentUploadQueue" ;
3436
3537interface RunStatus {
@@ -128,23 +130,38 @@ function setRuns(ref: ScopedThreadRef, status: string) {
128130 appAtomRegistry . set ( mocks . threadRuns ( runsKey ( ref ) ) , [ { id : RUN , status } ] ) ;
129131}
130132
133+ interface ComposerRefs {
134+ readonly promptRef : RefObject < string > ;
135+ readonly composerRef : Parameters < typeof useQueuedRunEdit > [ 0 ] [ "composerRef" ] ;
136+ }
137+
131138function Harness ( props : {
132139 readonly threadRef : ScopedThreadRef ;
133140 readonly runStatus : string ;
134141 readonly hookRef : RefObject < HookResult | null > ;
135142 readonly messages : ReadonlyArray < unknown > ;
143+ readonly composer ?: ComposerRefs | undefined ;
136144} ) {
145+ const threadKey = scopedThreadKey ( props . threadRef ) ;
146+ const currentRouteThreadKeyRef = useRef < string | null > ( threadKey ) ;
147+ useLayoutEffect ( ( ) => {
148+ currentRouteThreadKeyRef . current = threadKey ;
149+ return ( ) => {
150+ currentRouteThreadKeyRef . current = null ;
151+ } ;
152+ } , [ threadKey ] ) ;
137153 const result = useQueuedRunEdit ( {
138154 environmentId : ENV ,
139155 routeThreadRef : props . threadRef ,
156+ currentRouteThreadKeyRef,
140157 threadDraftTarget : props . threadRef ,
141158 activeThreadId : props . threadRef . threadId ,
142159 serverProjection : projection (
143160 [ { id : RUN , status : props . runStatus , modelSelection : savedContent ( "" ) . modelSelection } ] ,
144161 props . messages ,
145162 ) ,
146- promptRef : { current : "" } ,
147- composerRef : { current : null } ,
163+ promptRef : props . composer ?. promptRef ?? { current : "" } ,
164+ composerRef : props . composer ?. composerRef ?? { current : null } ,
148165 } ) ;
149166 useImperativeHandle ( props . hookRef , ( ) => result ) ;
150167 const draft = useComposerThreadDraft ( result . draftTarget ) ;
@@ -179,10 +196,17 @@ function view(
179196 runStatus : string ,
180197 hook : { current : HookResult | null } ,
181198 messages : ReadonlyArray < unknown > = [ ] ,
199+ composer ?: ComposerRefs ,
182200) {
183201 return (
184202 < AppAtomRegistryProvider >
185- < Harness threadRef = { threadRef } runStatus = { runStatus } hookRef = { hook } messages = { messages } />
203+ < Harness
204+ threadRef = { threadRef }
205+ runStatus = { runStatus }
206+ hookRef = { hook }
207+ messages = { messages }
208+ composer = { composer }
209+ />
186210 </ AppAtomRegistryProvider >
187211 ) ;
188212}
@@ -232,6 +256,62 @@ const promptOf = (ref: ScopedThreadRef) =>
232256 useComposerDraftStore . getState ( ) . getComposerDraft ( ref ) ?. prompt ?? "" ;
233257
234258describe ( "useQueuedRunEdit" , ( ) => {
259+ it . each ( [
260+ { navigation : "switches before the reply" , resets : false , focuses : false } ,
261+ { navigation : "switches before the frame" , resets : true , focuses : false } ,
262+ { navigation : "stays on thread" , resets : true , focuses : true } ,
263+ ] ) ( "finishes a successful queued edit when the user $navigation" , async ( scenario ) => {
264+ const { hook } = setup ( ) ;
265+ const requested = Promise . withResolvers < void > ( ) ;
266+ const reply = Promise . withResolvers < { readonly _tag : "Success" } > ( ) ;
267+ mocks . editQueuedRun . mockImplementation ( ( ) => {
268+ requested . resolve ( ) ;
269+ return reply . promise ;
270+ } ) ;
271+ const focusAtEnd = vi . fn ( ) ;
272+ const resetCursorState = vi . fn ( ) ;
273+ const composer : ComposerRefs = {
274+ promptRef : { current : "changed" } ,
275+ composerRef : { current : null } ,
276+ } ;
277+ await using screen = await render ( view ( THREAD_A , "queued" , hook , [ ] , composer ) ) ;
278+ const frames : FrameRequestCallback [ ] = [ ] ;
279+ using _frame = vi . spyOn ( window , "requestAnimationFrame" ) . mockImplementation ( ( callback ) => {
280+ frames . push ( callback ) ;
281+ return 0 ;
282+ } ) ;
283+ await beginChangedEdit ( hook ) ;
284+ await act ( ( ) => {
285+ for ( const frame of frames . splice ( 0 ) ) frame ( 0 ) ;
286+ } ) ;
287+ composer . composerRef . current = {
288+ focusAtEnd,
289+ resetCursorState,
290+ } as unknown as ChatComposerHandle ;
291+
292+ const setThreadError = vi . fn ( ) ;
293+ const saved = requireHook ( hook ) . saveEdit ( savedContent ( "changed" ) , setThreadError ) ;
294+ await requested . promise ;
295+ const openOtherThread = async ( ) => {
296+ await screen . rerender ( view ( THREAD_B , "queued" , hook , [ ] , composer ) ) ;
297+ composer . promptRef . current = "other thread draft" ;
298+ } ;
299+ if ( scenario . navigation === "switches before the reply" ) await openOtherThread ( ) ;
300+ reply . resolve ( { _tag : "Success" } ) ;
301+ await act ( ( ) => saved ) ;
302+ expect ( setThreadError ) . toHaveBeenCalledExactlyOnceWith ( THREAD_A . threadId , null ) ;
303+ expect ( resetCursorState ) . toHaveBeenCalledTimes ( Number ( scenario . resets ) ) ;
304+
305+ if ( scenario . navigation === "switches before the frame" ) await openOtherThread ( ) ;
306+ await act ( ( ) => {
307+ for ( const frame of frames . splice ( 0 ) ) frame ( 0 ) ;
308+ } ) ;
309+ expect ( focusAtEnd ) . toHaveBeenCalledTimes ( Number ( scenario . focuses ) ) ;
310+ expect ( composer . promptRef . current ) . toBe (
311+ scenario . navigation === "stays on thread" ? "" : "other thread draft" ,
312+ ) ;
313+ } ) ;
314+
235315 it . each ( [ "" , "Ultrathink:\n" ] ) (
236316 "opens attachment-only edits without the placeholder and preserves effort on save (%j)" ,
237317 async ( prefix ) => {
0 commit comments