11import { afterEach , beforeAll , beforeEach , describe , expect , it , vi } from "vitest" ;
2+ import {
3+ onInternalDiagnosticEvent ,
4+ type DiagnosticEventPayload ,
5+ } from "../../../infra/diagnostic-events.js" ;
6+ import type { createOpenClawCodingTools } from "../../agent-tools.js" ;
27import type { ToolSearchCatalogRef } from "../../tool-search.js" ;
38import {
49 cleanupTempPaths ,
@@ -58,27 +63,40 @@ describe("runEmbeddedAttempt tool-search catalog cleanup", () => {
5863 mode : "code-mode" ,
5964 tools : { codeMode : { enabled : true } } ,
6065 cancel : false ,
66+ timeout : false ,
6167 } ,
6268 {
6369 mode : "tool-search-tools" ,
6470 tools : { toolSearch : { enabled : true , mode : "tools" } } ,
6571 cancel : false ,
72+ timeout : false ,
6673 } ,
6774 {
6875 mode : "tool-search-directory" ,
6976 tools : { toolSearch : { enabled : true , mode : "directory" } } ,
7077 cancel : false ,
78+ timeout : false ,
7179 } ,
7280 {
7381 mode : "cancelled-code-mode" ,
7482 tools : { codeMode : { enabled : true } } ,
7583 cancel : true ,
84+ timeout : false ,
85+ } ,
86+ {
87+ mode : "timed-out-code-mode" ,
88+ tools : { codeMode : { enabled : true } } ,
89+ cancel : true ,
90+ timeout : true ,
7691 } ,
7792 ] as const ) (
7893 "clears the $mode run catalog when preparation fails or is cancelled" ,
79- async ( { mode, tools, cancel } ) => {
94+ async ( { mode, tools, cancel, timeout } ) => {
8095 const runId = `run-catalog-diagnostics-${ mode } ` ;
8196 const diagnosticsError = new Error ( `failed ${ mode } tool diagnostics` ) ;
97+ if ( timeout ) {
98+ diagnosticsError . name = "TimeoutError" ;
99+ }
82100 const abortController = new AbortController ( ) ;
83101 let catalogRef : ToolSearchCatalogRef | undefined ;
84102 const logDiagnostics = vi . fn ( ( ) => {
@@ -92,7 +110,16 @@ describe("runEmbeddedAttempt tool-search catalog cleanup", () => {
92110 throw diagnosticsError ;
93111 }
94112 } ) ;
95- hoisted . createOpenClawCodingToolsMock . mockImplementation ( ( ) => catalogProbeTools ( ) ) ;
113+ const cleanup = vi . fn ( async ( _reason : string ) => { } ) ;
114+ hoisted . createOpenClawCodingToolsMock . mockImplementation ( ( options ) => {
115+ const toolOptions = options as NonNullable < Parameters < typeof createOpenClawCodingTools > [ 0 ] > ;
116+ toolOptions . registerRunCleanup ?.( cleanup ) ;
117+ return catalogProbeTools ( ) ;
118+ } ) ;
119+ const events : DiagnosticEventPayload [ ] = [ ] ;
120+ const unsubscribe = onInternalDiagnosticEvent ( ( event ) => events . push ( event ) , {
121+ include : [ "run.completed" ] ,
122+ } ) ;
96123
97124 const attempt = createContextEngineAttemptRunner ( {
98125 contextEngine : createContextEngineBootstrapAndAssemble ( ) ,
@@ -112,7 +139,30 @@ describe("runEmbeddedAttempt tool-search catalog cleanup", () => {
112139 } ,
113140 } ) ;
114141
115- await expect ( attempt ) . rejects . toBe ( diagnosticsError ) ;
142+ try {
143+ if ( timeout ) {
144+ await expect ( attempt ) . rejects . toMatchObject ( {
145+ cause : diagnosticsError ,
146+ terminalOutcome : { status : "timeout" } ,
147+ } ) ;
148+ } else {
149+ await expect ( attempt ) . rejects . toBe ( diagnosticsError ) ;
150+ }
151+ } finally {
152+ unsubscribe ( ) ;
153+ }
154+ expect ( cleanup ) . toHaveBeenCalledExactlyOnceWith (
155+ timeout ? "timeout" : cancel ? "cancel" : "completion" ,
156+ ) ;
157+ if ( timeout ) {
158+ expect ( events ) . toContainEqual (
159+ expect . objectContaining ( {
160+ type : "run.completed" ,
161+ runId,
162+ outcome : "aborted" ,
163+ } ) ,
164+ ) ;
165+ }
116166 expect ( logDiagnostics ) . toHaveBeenCalledOnce ( ) ;
117167 expect ( catalogRef ) . toBeDefined ( ) ;
118168 expect ( catalogRef ?. current ) . toBeUndefined ( ) ;
0 commit comments