@@ -2,11 +2,15 @@ import fs from "node:fs/promises";
22import os from "node:os" ;
33import path from "node:path" ;
44import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
5+ import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js" ;
56import type { UpdateRunResult } from "../../infra/update-runner.js" ;
67import { defaultRuntime } from "../../runtime.js" ;
78
9+ const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
810const mocks = vi . hoisted ( ( ) => ( {
11+ checkCompletionStatus : vi . fn ( ) ,
912 completePluginUpdate : vi . fn ( ) ,
13+ ensureCompletionCache : vi . fn ( ) ,
1014 leaseActive : false ,
1115 loadPluginRecords : vi . fn ( ) ,
1216 markSentinelFailure : vi . fn ( async ( ) => undefined ) ,
@@ -24,8 +28,6 @@ const mocks = vi.hoisted(() => ({
2428 typeof import ( "./update-command-service.js" ) . revalidateManagedGatewayServiceAfterUpdate
2529 > ( ) ,
2630 restoreWindowsAutoStart : vi . fn ( async ( ) => true ) ,
27- tryInstallCompletion : vi . fn ( async ( ) => undefined ) ,
28- tryWriteCompletionCache : vi . fn ( async ( ) => undefined ) ,
2931 updatePlugins : vi . fn ( ) ,
3032 writeSentinel : vi . fn ( async ( ) => undefined ) ,
3133} ) ) ;
@@ -43,6 +45,11 @@ vi.mock("../../daemon/service.js", async (importOriginal) => ({
4345 ...( await importOriginal < typeof import ( "../../daemon/service.js" ) > ( ) ) ,
4446 readGatewayServiceState : mocks . readServiceState ,
4547} ) ) ;
48+ vi . mock ( "../../commands/doctor-completion.js" , async ( importOriginal ) => ( {
49+ ...( await importOriginal < typeof import ( "../../commands/doctor-completion.js" ) > ( ) ) ,
50+ checkShellCompletionStatus : mocks . checkCompletionStatus ,
51+ ensureCompletionCacheExists : mocks . ensureCompletionCache ,
52+ } ) ) ;
4653vi . mock ( "../../plugins/plugin-lifecycle-lease.js" , ( ) => ( {
4754 withPluginLifecycleLease : async ( _params : unknown , callback : ( ) => unknown ) => {
4855 mocks . leaseActive = true ;
@@ -72,10 +79,6 @@ vi.mock("./update-command-fresh-doctor.js", () => ({
7279vi . mock ( "./update-command-plugins.js" , ( ) => ( {
7380 updatePluginsAfterCoreUpdate : mocks . updatePlugins ,
7481} ) ) ;
75- vi . mock ( "./shared.js" , async ( importOriginal ) => ( {
76- ...( await importOriginal < typeof import ( "./shared.js" ) > ( ) ) ,
77- tryWriteCompletionCache : mocks . tryWriteCompletionCache ,
78- } ) ) ;
7982vi . mock ( "./restart-helper.js" , ( ) => ( {
8083 prepareRestartScript : mocks . prepareRestartScript ,
8184} ) ) ;
@@ -85,7 +88,6 @@ vi.mock("./update-command-service.js", async (importOriginal) => ({
8588 maybeRestartServiceAfterFailedMutableUpdate : mocks . restart ,
8689 revalidateManagedGatewayServiceAfterUpdate : mocks . revalidateService ,
8790 restoreWindowsTaskAutoStartOrExit : mocks . restoreWindowsAutoStart ,
88- tryInstallShellCompletion : mocks . tryInstallCompletion ,
8991} ) ) ;
9092vi . mock ( "./update-command-post-core.js" , async ( importOriginal ) => ( {
9193 ...( await importOriginal < typeof import ( "./update-command-post-core.js" ) > ( ) ) ,
@@ -98,6 +100,15 @@ import { finishUpdate } from "./update-command-post-update.js";
98100import { resolveUpdatedGatewayRestartPort } from "./update-command-service.js" ;
99101
100102type FinishUpdateParams = Parameters < typeof finishUpdate > [ 0 ] ;
103+ const stdinIsTTYDescriptor = Object . getOwnPropertyDescriptor ( process . stdin , "isTTY" ) ;
104+
105+ afterEach ( ( ) => {
106+ if ( stdinIsTTYDescriptor ) {
107+ Object . defineProperty ( process . stdin , "isTTY" , stdinIsTTYDescriptor ) ;
108+ } else {
109+ Reflect . deleteProperty ( process . stdin , "isTTY" ) ;
110+ }
111+ } ) ;
101112
102113const validConfigSnapshot = {
103114 valid : true ,
@@ -129,6 +140,7 @@ async function finishSuccessfulPackageSwitch(params: {
129140 previousRoot : string ;
130141 packageRoot : string ;
131142 restartEnvironment ?: NodeJS . ProcessEnv ;
143+ json ?: boolean ;
132144 sealed ?: boolean ;
133145 updateMode ?: UpdateRunResult [ "mode" ] ;
134146 stoppedForUpdate ?: boolean ;
@@ -157,7 +169,7 @@ async function finishSuccessfulPackageSwitch(params: {
157169 channel : params . updateMode === "git" ? "dev" : "stable" ,
158170 downgradeRisk : true ,
159171 shouldRestart : Boolean ( params . restartEnvironment ) ,
160- opts : { } ,
172+ opts : { json : params . json } ,
161173 showProgress : false ,
162174 controlPlaneUpdateSentinelMeta : { } ,
163175 preUpdatePluginInstallRecords : { } ,
@@ -292,6 +304,135 @@ describe("successful update finalization ordering", () => {
292304 vi . spyOn ( defaultRuntime , "log" ) . mockImplementation ( ( ) => undefined ) ;
293305 } ) ;
294306
307+ it ( "restarts after completion status inspection fails" , async ( ) => {
308+ Object . defineProperty ( process . stdin , "isTTY" , { configurable : true , value : true } ) ;
309+ mocks . checkCompletionStatus . mockRejectedValueOnce (
310+ Object . assign ( new Error ( "EACCES: completion profile read denied" ) , { code : "EACCES" } ) ,
311+ ) ;
312+
313+ let failure : unknown ;
314+ try {
315+ await finishSuccessfulPackageSwitch ( {
316+ previousRoot : "/tmp/openclaw-update" ,
317+ packageRoot : "/tmp/openclaw-update" ,
318+ restartEnvironment : process . env ,
319+ } ) ;
320+ } catch ( err ) {
321+ failure = err ;
322+ }
323+
324+ const output = vi . mocked ( defaultRuntime . log ) . mock . calls . flat ( ) . map ( String ) . join ( "\n" ) ;
325+ expect . soft ( failure ) . toBeUndefined ( ) ;
326+ expect . soft ( output ) . toContain ( "Shell completion refresh failed" ) ;
327+ expect . soft ( mocks . restartService ) . toHaveBeenCalledOnce ( ) ;
328+ expect ( mocks . restartService . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
329+ mocks . checkCompletionStatus . mock . invocationCallOrder [ 0 ] ?? Number . POSITIVE_INFINITY ,
330+ ) ;
331+ } ) ;
332+
333+ it ( "restarts when completion cache refresh reports failure" , async ( ) => {
334+ const root = tempDirs . make ( "openclaw-completion-failure-" ) ;
335+ await fs . writeFile (
336+ path . join ( root , "openclaw.mjs" ) ,
337+ 'process.stderr.write("injected completion cache failure"); process.exit(1);' ,
338+ ) ;
339+
340+ await finishSuccessfulPackageSwitch ( {
341+ previousRoot : root ,
342+ packageRoot : root ,
343+ restartEnvironment : process . env ,
344+ } ) ;
345+
346+ const logCalls = vi . mocked ( defaultRuntime . log ) . mock . calls ;
347+ const warningIndex = logCalls . findIndex ( ( call ) =>
348+ call . some ( ( value ) => String ( value ) . includes ( "Completion cache update failed" ) ) ,
349+ ) ;
350+ expect ( warningIndex ) . toBeGreaterThanOrEqual ( 0 ) ;
351+ expect ( mocks . restartService . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
352+ vi . mocked ( defaultRuntime . log ) . mock . invocationCallOrder [ warningIndex ] ??
353+ Number . POSITIVE_INFINITY ,
354+ ) ;
355+ expect ( logCalls [ warningIndex ] ?. join ( " " ) ) . toContain ( "openclaw completion --write-state" ) ;
356+ } ) ;
357+
358+ it ( "restarts when shell completion cache generation returns false" , async ( ) => {
359+ Object . defineProperty ( process . stdin , "isTTY" , { configurable : true , value : true } ) ;
360+ mocks . checkCompletionStatus . mockResolvedValueOnce ( {
361+ shell : "zsh" ,
362+ profileInstalled : true ,
363+ cacheExists : true ,
364+ cachePath : "/tmp/openclaw-completion.zsh" ,
365+ usesSlowPattern : true ,
366+ } ) ;
367+ mocks . ensureCompletionCache . mockResolvedValueOnce ( false ) ;
368+
369+ await finishSuccessfulPackageSwitch ( {
370+ previousRoot : "/tmp/openclaw-update" ,
371+ packageRoot : "/tmp/openclaw-update" ,
372+ restartEnvironment : process . env ,
373+ } ) ;
374+
375+ const output = vi . mocked ( defaultRuntime . log ) . mock . calls . flat ( ) . map ( String ) . join ( "\n" ) ;
376+ expect ( output ) . toContain ( "completion cache generation failed" ) ;
377+ expect ( output ) . toContain ( "openclaw completion --write-state --install" ) ;
378+ expect ( mocks . restartService ) . toHaveBeenCalledOnce ( ) ;
379+ expect ( mocks . restartService . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
380+ mocks . ensureCompletionCache . mock . invocationCallOrder [ 0 ] ?? Number . POSITIVE_INFINITY ,
381+ ) ;
382+ } ) ;
383+
384+ it ( "keeps JSON completion cache failures silent and restarts" , async ( ) => {
385+ const root = tempDirs . make ( "openclaw-json-completion-failure-" ) ;
386+ await fs . writeFile ( path . join ( root , "openclaw.mjs" ) , "process.exit(1);" ) ;
387+ Object . defineProperty ( process . stdin , "isTTY" , { configurable : true , value : true } ) ;
388+
389+ await finishSuccessfulPackageSwitch ( {
390+ previousRoot : root ,
391+ packageRoot : root ,
392+ restartEnvironment : process . env ,
393+ json : true ,
394+ } ) ;
395+
396+ expect ( defaultRuntime . error ) . not . toHaveBeenCalled ( ) ;
397+ expect ( mocks . checkCompletionStatus ) . not . toHaveBeenCalled ( ) ;
398+ expect ( mocks . ensureCompletionCache ) . not . toHaveBeenCalled ( ) ;
399+ expect ( mocks . restartService ) . toHaveBeenCalledOnce ( ) ;
400+ } ) ;
401+
402+ it ( "skips interactive completion in non-TTY mode" , async ( ) => {
403+ Object . defineProperty ( process . stdin , "isTTY" , { configurable : true , value : false } ) ;
404+
405+ await finishSuccessfulPackageSwitch ( {
406+ previousRoot : "/tmp/openclaw-update" ,
407+ packageRoot : "/tmp/openclaw-update" ,
408+ restartEnvironment : process . env ,
409+ } ) ;
410+
411+ expect ( mocks . checkCompletionStatus ) . not . toHaveBeenCalled ( ) ;
412+ expect ( mocks . ensureCompletionCache ) . not . toHaveBeenCalled ( ) ;
413+ expect ( mocks . restartService ) . toHaveBeenCalledOnce ( ) ;
414+ } ) ;
415+
416+ it ( "keeps an unhealthy restart blocking before completion refresh" , async ( ) => {
417+ Object . defineProperty ( process . stdin , "isTTY" , { configurable : true , value : true } ) ;
418+ mocks . restartService . mockResolvedValueOnce ( false ) ;
419+ mocks . checkCompletionStatus . mockRejectedValueOnce (
420+ new Error ( "completion status should not run after failed restart health" ) ,
421+ ) ;
422+
423+ await finishSuccessfulPackageSwitch ( {
424+ previousRoot : "/tmp/openclaw-update" ,
425+ packageRoot : "/tmp/openclaw-update" ,
426+ restartEnvironment : process . env ,
427+ } ) ;
428+
429+ expect ( mocks . markSentinelFailure ) . toHaveBeenCalledWith (
430+ expect . objectContaining ( { reason : "restart-unhealthy" } ) ,
431+ ) ;
432+ expect ( defaultRuntime . exit ) . toHaveBeenCalledWith ( 1 ) ;
433+ expect ( mocks . checkCompletionStatus ) . not . toHaveBeenCalled ( ) ;
434+ } ) ;
435+
295436 it ( "retires the wrapper before persisting and printing success" , async ( ) => {
296437 const home = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-finalize-order-" ) ) ;
297438 const previousRoot = path . join ( home , "old-root" ) ;
0 commit comments