@@ -13,8 +13,9 @@ function suggestionTurn(text: string): TurnResult {
1313function createGenerator ( runTurn : ReturnType < typeof vi . fn > ) {
1414 const publish = vi . fn ( ) . mockResolvedValue ( undefined ) ;
1515 const threadStart = vi . fn ( ) . mockResolvedValue ( { thread : { id : "ephemeral" } } ) ;
16- const client = { threadStart, runTurn} as unknown as CodexAppServerClient ;
17- return { generator : new PromptSuggestionGenerator ( client , "/test/cwd" , publish ) , publish, threadStart, runTurn} ;
16+ const threadUnsubscribe = vi . fn ( ) . mockResolvedValue ( { status : "unsubscribed" } ) ;
17+ const client = { threadStart, runTurn, threadUnsubscribe} as unknown as CodexAppServerClient ;
18+ return { generator : new PromptSuggestionGenerator ( client , "/test/cwd" , publish ) , publish, threadStart, runTurn, threadUnsubscribe} ;
1819}
1920
2021async function flush ( ) {
@@ -29,12 +30,57 @@ describe("PromptSuggestionGenerator", () => {
2930 generator . onTurnCompleted ( "fix the bug" , "Fixed it." ) ;
3031 await vi . waitFor ( ( ) => expect ( publish ) . toHaveBeenCalledWith ( "Run the tests" ) ) ;
3132
32- expect ( threadStart ) . toHaveBeenCalledWith ( { cwd : "/test/cwd" , ephemeral : true } ) ;
33- expect ( runTurn . mock . calls [ 0 ] ! [ 0 ] ) . toMatchObject ( { threadId : "ephemeral" , model : TITLE_MODEL } ) ;
33+ expect ( threadStart ) . toHaveBeenCalledWith ( {
34+ cwd : "/test/cwd" ,
35+ ephemeral : true ,
36+ sandbox : "read-only" ,
37+ approvalPolicy : "never" ,
38+ baseInstructions : expect . stringContaining ( "Never call tools" ) ,
39+ config : { project_doc_max_bytes : 0 } ,
40+ } ) ;
41+ expect ( runTurn . mock . calls [ 0 ] ! [ 0 ] ) . toMatchObject ( { threadId : "ephemeral" , model : TITLE_MODEL , effort : "low" } ) ;
3442 expect ( runTurn . mock . calls [ 0 ] ! [ 0 ] . input [ 0 ] . text ) . toContain ( "fix the bug" ) ;
3543 expect ( runTurn . mock . calls [ 0 ] ! [ 0 ] . input [ 0 ] . text ) . toContain ( "Fixed it." ) ;
3644 } ) ;
3745
46+ /** The ephemeral thread is released after a successful turn, and a failing unsubscribe keeps the suggestion. */
47+ it ( "unsubscribes the thread after success" , async ( ) => {
48+ const { generator, publish, threadUnsubscribe} = createGenerator ( vi . fn ( ) . mockResolvedValue ( suggestionTurn ( "Go" ) ) ) ;
49+ threadUnsubscribe . mockRejectedValue ( new Error ( "gone" ) ) ;
50+
51+ generator . onTurnCompleted ( "a" , "b" ) ;
52+ await vi . waitFor ( ( ) => expect ( threadUnsubscribe ) . toHaveBeenCalled ( ) ) ;
53+
54+ expect ( threadUnsubscribe ) . toHaveBeenCalledWith ( { threadId : "ephemeral" } ) ;
55+ expect ( publish ) . toHaveBeenCalledWith ( "Go" ) ;
56+ } ) ;
57+
58+ /** The ephemeral thread is released when the turn fails, and a failing unsubscribe does not surface. */
59+ it ( "unsubscribes the thread after a runTurn failure" , async ( ) => {
60+ const { generator, publish, threadUnsubscribe} = createGenerator ( vi . fn ( ) . mockRejectedValue ( new Error ( "boom" ) ) ) ;
61+ threadUnsubscribe . mockRejectedValue ( new Error ( "gone" ) ) ;
62+
63+ generator . onTurnCompleted ( "a" , "b" ) ;
64+ await vi . waitFor ( ( ) => expect ( threadUnsubscribe ) . toHaveBeenCalledWith ( { threadId : "ephemeral" } ) ) ;
65+ await flush ( ) ;
66+
67+ expect ( publish ) . not . toHaveBeenCalled ( ) ;
68+ } ) ;
69+
70+ /** A stale generation is dropped, and its thread is still released. */
71+ it ( "unsubscribes the thread when the generation is stale" , async ( ) => {
72+ const turn = deferred < TurnResult > ( ) ;
73+ const { generator, publish, runTurn, threadUnsubscribe} = createGenerator ( vi . fn ( ) . mockReturnValue ( turn . promise ) ) ;
74+
75+ generator . onTurnCompleted ( "a" , "b" ) ;
76+ await vi . waitFor ( ( ) => expect ( runTurn ) . toHaveBeenCalled ( ) ) ;
77+ generator . invalidate ( ) ;
78+ turn . resolve ( suggestionTurn ( "Too late" ) ) ;
79+ await vi . waitFor ( ( ) => expect ( threadUnsubscribe ) . toHaveBeenCalledWith ( { threadId : "ephemeral" } ) ) ;
80+
81+ expect ( publish ) . not . toHaveBeenCalled ( ) ;
82+ } ) ;
83+
3884 /** Each side of the exchange is cut to 4000 characters before it reaches the model. */
3985 it ( "truncates long inputs" , async ( ) => {
4086 const { generator, publish, runTurn} = createGenerator ( vi . fn ( ) . mockResolvedValue ( suggestionTurn ( "Go on" ) ) ) ;
@@ -171,8 +217,9 @@ describe("PromptSuggestionGenerator", () => {
171217 const { generator} = createGenerator ( vi . fn ( ) ) ;
172218 const runTurn = vi . fn ( ) . mockResolvedValue ( suggestionTurn ( "Go" ) ) ;
173219 const threadStart = vi . fn ( ) . mockResolvedValue ( { thread : { id : "new" } } ) ;
220+ const threadUnsubscribe = vi . fn ( ) . mockResolvedValue ( { status : "unsubscribed" } ) ;
174221
175- generator . rebindAppServer ( { threadStart, runTurn} as unknown as CodexAppServerClient ) ;
222+ generator . rebindAppServer ( { threadStart, runTurn, threadUnsubscribe } as unknown as CodexAppServerClient ) ;
176223 generator . onTurnCompleted ( "a" , "b" ) ;
177224 await vi . waitFor ( ( ) => expect ( runTurn ) . toHaveBeenCalled ( ) ) ;
178225
0 commit comments