@@ -7,7 +7,9 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
77import { withEnvAsync } from "../test-utils/env.js" ;
88import {
99 COMPLETION_SHELLS ,
10+ formatCompletionReloadCommand ,
1011 resolveCompletionCachePath ,
12+ resolveCompletionProfileHint ,
1113 resolveCompletionProfilePath ,
1214 type CompletionShell ,
1315} from "./completion-runtime.js" ;
@@ -144,6 +146,7 @@ describe("completion-cli write-state", () => {
144146
145147 afterEach ( async ( ) => {
146148 restoreStderrWriteSpy ?.( ) ;
149+ vi . restoreAllMocks ( ) ;
147150 } ) ;
148151
149152 it . each ( COMPLETION_SHELLS ) (
@@ -291,25 +294,35 @@ describe("completion-cli write-state", () => {
291294 ) ;
292295
293296 it . each ( COMPLETION_SHELLS ) (
294- "installs cached %s completion without registering commands or plugins" ,
297+ "installs cached %s completion visibly and idempotently without registering commands or plugins" ,
295298 async ( shell ) => {
296299 const { registerCompletionCli } = await import ( "./completion-cli.js" ) ;
297300
298301 await withIsolatedCompletionState ( async ( ) => {
299302 const cachePath = resolveCompletionCachePath ( shell , "openclaw" ) ;
303+ const profilePath = resolveCompletionProfilePath ( shell ) ;
304+ const log = vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } ) ;
305+ vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } ) ;
300306 await fs . mkdir ( path . dirname ( cachePath ) , { recursive : true } ) ;
301307 await fs . writeFile ( cachePath , "# cached completion\n" , "utf8" ) ;
302308
303309 const program = new Command ( ) . name ( "openclaw" ) ;
304310 registerCompletionCli ( program ) ;
305- await program . parseAsync ( [ "completion" , "--shell" , shell , "--install" , "--yes" ] , {
306- from : "user" ,
307- } ) ;
311+ const args = [ "completion" , "--shell" , shell , "--install" , "--yes" ] ;
312+ await program . parseAsync ( args , { from : "user" } ) ;
308313
309- await expect ( fs . readFile ( resolveCompletionProfilePath ( shell ) , "utf8" ) ) . resolves . toContain (
310- cachePath ,
314+ const installedProfile = await fs . readFile ( profilePath , "utf8" ) ;
315+ expect ( installedProfile ) . toContain ( cachePath ) ;
316+ expect ( log ) . toHaveBeenCalledWith (
317+ `Completion installed. Restart your shell or run: ${ formatCompletionReloadCommand ( shell , resolveCompletionProfileHint ( shell ) ) } ` ,
311318 ) ;
312319 await expect ( fs . readFile ( cachePath , "utf8" ) ) . resolves . toBe ( "# cached completion\n" ) ;
320+
321+ log . mockClear ( ) ;
322+ await program . parseAsync ( args , { from : "user" } ) ;
323+
324+ expect ( log ) . toHaveBeenCalledWith ( `Completion already installed in ${ profilePath } ` ) ;
325+ await expect ( fs . readFile ( profilePath , "utf8" ) ) . resolves . toBe ( installedProfile ) ;
313326 expectCompletionInstallationToSkipRegistration ( ) ;
314327 } ) ;
315328 } ,
@@ -366,6 +379,8 @@ describe("completion-cli write-state", () => {
366379 const { registerCompletionCli } = await import ( "./completion-cli.js" ) ;
367380
368381 await withIsolatedCompletionState ( async ( ) => {
382+ const log = vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } ) ;
383+ vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } ) ;
369384 const program = new Command ( ) . name ( "openclaw" ) ;
370385 registerCompletionCli ( program ) ;
371386 await program . parseAsync (
@@ -378,6 +393,9 @@ describe("completion-cli write-state", () => {
378393 await expect ( fs . readFile ( resolveCompletionProfilePath ( "zsh" ) , "utf8" ) ) . resolves . toContain (
379394 cachePath ,
380395 ) ;
396+ expect ( log ) . toHaveBeenCalledWith (
397+ "Completion installed. Restart your shell or run: source ~/.zshrc" ,
398+ ) ;
381399 expect ( registerSubCliByNameMock . mock . calls ) . toEqual ( [
382400 [ program , "qa" , process . argv , { purpose : "completion" } ] ,
383401 ] ) ;
0 commit comments