1+ import { EnvironmentId , ProjectId , type ScopedProjectRef } from "@t3tools/contracts" ;
12import { describe , expect , it , vi } from "vite-plus/test" ;
23
34const testState = vi . hoisted ( ( ) => {
@@ -13,13 +14,19 @@ const testState = vi.hoisted(() => {
1314 let storedDraft : StoredDraft | null = null ;
1415 let prompts : Record < string , string > = { } ;
1516 let navigationFailure : Error | null = null ;
17+ let completeNavigation : ( ( ) => void ) | null = null ;
1618 const router = {
1719 state : {
1820 location : { href : "/" } ,
1921 matches : [ { params : { } } ] ,
2022 } ,
2123 navigate : vi . fn ( async ( request : { readonly params : { readonly draftId : string } } ) => {
2224 if ( navigationFailure !== null ) throw navigationFailure ;
25+ if ( completeNavigation !== null ) {
26+ await new Promise < void > ( ( resolve ) => {
27+ completeNavigation = resolve ;
28+ } ) ;
29+ }
2330 router . state . location . href = `/draft/${ request . params . draftId } ` ;
2431 } ) ,
2532 } ;
@@ -73,6 +80,7 @@ const testState = vi.hoisted(() => {
7380 : { ...nextStoredDraft , logicalProjectKey : "remote-project" } ;
7481 prompts = { } ;
7582 navigationFailure = null ;
83+ completeNavigation = null ;
7684 router . state . location . href = "/" ;
7785 router . navigate . mockClear ( ) ;
7886 draftStore . setLogicalProjectDraftThreadId . mockClear ( ) ;
@@ -82,6 +90,13 @@ const testState = vi.hoisted(() => {
8290 } ) ;
8391 } ,
8492 router,
93+ holdNavigation ( ) {
94+ completeNavigation = ( ) => undefined ;
95+ } ,
96+ releaseNavigation ( ) {
97+ completeNavigation ?.( ) ;
98+ completeNavigation = null ;
99+ } ,
85100 setNavigationFailure ( error : Error | null ) {
86101 navigationFailure = error ;
87102 } ,
@@ -96,7 +111,11 @@ vi.mock("@t3tools/client-runtime/environment", () => ({
96111 scopeProjectRef : ( environmentId : string , projectId : string ) => ( { environmentId, projectId } ) ,
97112 scopeThreadRef : ( environmentId : string , threadId : string ) => ( { environmentId, threadId } ) ,
98113} ) ) ;
99- vi . mock ( "@t3tools/contracts" , ( ) => ( { DEFAULT_RUNTIME_MODE : "default" } ) ) ;
114+ vi . mock ( "@t3tools/contracts" , ( ) => ( {
115+ DEFAULT_RUNTIME_MODE : "default" ,
116+ EnvironmentId : { make : ( value : string ) => value } ,
117+ ProjectId : { make : ( value : string ) => value } ,
118+ } ) ) ;
100119vi . mock ( "@t3tools/shared/threadEnvMode" , ( ) => ( {
101120 resolveDefaultThreadEnvMode : ( input : {
102121 readonly projectFile : "local" | "worktree" | null ;
@@ -252,4 +271,30 @@ describe("useNewThreadHandler", () => {
252271 expect ( testState . draftStore . setPrompt ) . toHaveBeenCalledOnce ( ) ;
253272 expect ( testState . router . navigate ) . toHaveBeenCalledTimes ( 2 ) ;
254273 } ) ;
274+
275+ it ( "does not deliver a raced handover into another invocation's draft" , async ( ) => {
276+ testState . reset ( null ) ;
277+ testState . holdNavigation ( ) ;
278+ const openThread = useNewThreadHandler ( ) ;
279+ const projectRef = {
280+ environmentId : EnvironmentId . make ( "environment-ssh" ) ,
281+ projectId : ProjectId . make ( "project-remote" ) ,
282+ } satisfies ScopedProjectRef ;
283+ const ordinaryOpen = openThread ( projectRef ) ;
284+ const onHandoverDraftReady = vi . fn ( ) ;
285+ const handoverOpen = openThread ( projectRef , {
286+ initialPrompt : "Generated handover" ,
287+ onDraftReady : onHandoverDraftReady ,
288+ } ) ;
289+
290+ testState . completeProjectFileRead ( null ) ;
291+
292+ await expect ( handoverOpen ) . resolves . toBeNull ( ) ;
293+ expect ( testState . draftStore . setPrompt ) . not . toHaveBeenCalled ( ) ;
294+ expect ( onHandoverDraftReady ) . not . toHaveBeenCalled ( ) ;
295+ expect ( testState . router . navigate ) . toHaveBeenCalledOnce ( ) ;
296+
297+ testState . releaseNavigation ( ) ;
298+ await expect ( ordinaryOpen ) . resolves . toMatchObject ( { draftId : "draft-delayed" } ) ;
299+ } ) ;
255300} ) ;
0 commit comments