@@ -2329,6 +2329,117 @@ describe("buildStatusReply subagent summary", () => {
23292329 expect ( normalizeTestText ( text ) ) . toContain ( "Runtime: OpenAI Codex" ) ;
23302330 } ) ;
23312331} ) ;
2332+
2333+ describe ( "buildStatusReply error handling" , ( ) => {
2334+ afterEach ( ( ) => {
2335+ vi . doUnmock ( "../../logger.js" ) ;
2336+ vi . doUnmock ( "../../status/status-text.js" ) ;
2337+ vi . resetModules ( ) ;
2338+ vi . restoreAllMocks ( ) ;
2339+ } ) ;
2340+
2341+ async function runStatusReply ( fn : typeof buildStatusReply ) {
2342+ const commandParams = buildCommandTestParams ( "/status" , baseCfg ) ;
2343+ return await fn ( {
2344+ cfg : baseCfg ,
2345+ command : commandParams . command ,
2346+ sessionEntry : commandParams . sessionEntry ,
2347+ sessionKey : commandParams . sessionKey ,
2348+ parentSessionKey : commandParams . sessionKey ,
2349+ sessionScope : commandParams . sessionScope ,
2350+ storePath : commandParams . storePath ,
2351+ provider : "anthropic" ,
2352+ model : "claude-opus-4-6" ,
2353+ contextTokens : 0 ,
2354+ resolvedThinkLevel : commandParams . resolvedThinkLevel ,
2355+ resolvedFastMode : false ,
2356+ resolvedVerboseLevel : commandParams . resolvedVerboseLevel ,
2357+ resolvedReasoningLevel : commandParams . resolvedReasoningLevel ,
2358+ resolvedElevatedLevel : commandParams . resolvedElevatedLevel ,
2359+ resolveDefaultThinkingLevel : commandParams . resolveDefaultThinkingLevel ,
2360+ isGroup : commandParams . isGroup ,
2361+ defaultGroupActivation : commandParams . defaultGroupActivation ,
2362+ modelAuthOverride : "api-key" ,
2363+ activeModelAuthOverride : "api-key" ,
2364+ } ) ;
2365+ }
2366+
2367+ it ( "delivers a fixed generic reply and logs details when status rendering throws" , async ( ) => {
2368+ // commands-status re-exports buildStatusText, so the mock must keep that
2369+ // binding while prod calls buildStatusReplyParts. logError stays mocked so
2370+ // containment diagnostics never leak into test stderr.
2371+ vi . doMock ( "../../logger.js" , async ( importOriginal ) => ( {
2372+ ...( await importOriginal < object > ( ) ) ,
2373+ logError : vi . fn ( ) ,
2374+ } ) ) ;
2375+ vi . doMock ( "../../status/status-text.js" , ( ) => ( {
2376+ buildStatusReplyParts : vi . fn ( ( ) => Promise . reject ( new Error ( "Unexpected rendering error" ) ) ) ,
2377+ buildStatusText : vi . fn ( ( ) => Promise . reject ( new Error ( "Unexpected rendering error" ) ) ) ,
2378+ } ) ) ;
2379+
2380+ vi . resetModules ( ) ;
2381+ const { buildStatusReply : freshBuildStatusReply } = await import ( "./commands-status.js" ) ;
2382+ const { logError } = await import ( "../../logger.js" ) ;
2383+ const reply = await runStatusReply ( freshBuildStatusReply ) ;
2384+
2385+ // Exact object equality also pins that no stale presentation or internal
2386+ // error text reaches the channel; diagnostics belong to the log sink only.
2387+ expect ( reply ) . toEqual ( { text : "⚠️ Status: error rendering response" } ) ;
2388+ expect ( logError ) . toHaveBeenCalledWith ( expect . stringContaining ( "Unexpected rendering error" ) ) ;
2389+ } ) ;
2390+
2391+ it ( "keeps the structured rich payload on the success path" , async ( ) => {
2392+ const presentation = {
2393+ title : "Status" ,
2394+ tone : "info" as const ,
2395+ blocks : [ { type : "text" as const , text : "plain status" } , { type : "divider" as const } ] ,
2396+ } ;
2397+ vi . doMock ( "../../status/status-text.js" , ( ) => ( {
2398+ buildStatusReplyParts : vi . fn ( ( ) => Promise . resolve ( { text : "plain status" , presentation } ) ) ,
2399+ buildStatusText : vi . fn ( ( ) => Promise . resolve ( "plain status" ) ) ,
2400+ } ) ) ;
2401+
2402+ vi . resetModules ( ) ;
2403+ const { buildStatusReply : freshBuildStatusReply } = await import ( "./commands-status.js" ) ;
2404+ const reply = await runStatusReply ( freshBuildStatusReply ) ;
2405+
2406+ expect ( reply ) . toMatchObject ( {
2407+ text : "plain status" ,
2408+ presentation,
2409+ presentationTextMode : "fallback" ,
2410+ } ) ;
2411+ } ) ;
2412+
2413+ it ( "returns a generic reply and logs details when plugin health collection fails" , async ( ) => {
2414+ vi . doMock ( "../../logger.js" , async ( importOriginal ) => ( {
2415+ ...( await importOriginal < object > ( ) ) ,
2416+ logError : vi . fn ( ) ,
2417+ } ) ) ;
2418+
2419+ vi . resetModules ( ) ;
2420+ const { buildStatusPluginsReply : freshBuildStatusPluginsReply } =
2421+ await import ( "./commands-status.js" ) ;
2422+ const { logError } = await import ( "../../logger.js" ) ;
2423+ pluginHealthRuntimeMock . collectInstalledPluginHealthSnapshot . mockRejectedValueOnce (
2424+ new Error ( "Cannot find module 'internal/path'" ) ,
2425+ ) ;
2426+
2427+ const commandParams = buildCommandTestParams ( "/status plugins" , {
2428+ ...baseCfg ,
2429+ commands : { text : true , plugins : true } ,
2430+ } ) ;
2431+ const reply = await freshBuildStatusPluginsReply ( {
2432+ cfg : commandParams . cfg ,
2433+ command : commandParams . command ,
2434+ workspaceDir : commandParams . workspaceDir ,
2435+ } ) ;
2436+
2437+ expect ( reply ?. text ) . toBe ( "⚠️ Plugins: health unavailable" ) ;
2438+ expect ( logError ) . toHaveBeenCalledWith (
2439+ expect . stringContaining ( "Cannot find module 'internal/path'" ) ,
2440+ ) ;
2441+ } ) ;
2442+ } ) ;
23322443/* oxlint-disable max-lines -- TODO: split this grandfathered oversized file. */
23332444
23342445async function buildKiraStatusReply ( cfg : OpenClawConfig ) {
0 commit comments