scripts: update test-shell-completion to use shared helpers

- Use `checkShellCompletionStatus` and `ensureCompletionCacheExists` from doctor-completion
- Display "Uses slow pattern" status in output
- Simulate doctor/update/onboard behavior for all completion scenarios
- Remove duplicated utility functions
This commit is contained in:
Shakker
2026-02-04 15:38:01 +00:00
committed by Shakker
parent 3e14192730
commit dae0e2b325

View File

@@ -24,25 +24,19 @@
*/ */
import { confirm, isCancel } from "@clack/prompts"; import { confirm, isCancel } from "@clack/prompts";
import { spawnSync } from "node:child_process";
import fs from "node:fs/promises";
import os from "node:os"; import os from "node:os";
import path from "node:path"; import path from "node:path";
import { installCompletion } from "../src/cli/completion-cli.js";
import { import {
installCompletion, checkShellCompletionStatus,
isCompletionInstalled, ensureCompletionCacheExists,
resolveShellFromEnv, } from "../src/commands/doctor-completion.js";
} from "../src/cli/completion-cli.js";
import { resolveStateDir } from "../src/config/paths.js";
import { stylePromptMessage } from "../src/terminal/prompt-style.js"; import { stylePromptMessage } from "../src/terminal/prompt-style.js";
import { theme } from "../src/terminal/theme.js"; import { theme } from "../src/terminal/theme.js";
const CLI_NAME = "openclaw"; const CLI_NAME = "openclaw";
const SUPPORTED_SHELLS = ["zsh", "bash", "fish", "powershell"] as const;
type SupportedShell = (typeof SUPPORTED_SHELLS)[number];
interface Options { interface Options {
shell?: string;
checkOnly: boolean; checkOnly: boolean;
force: boolean; force: boolean;
help: boolean; help: boolean;
@@ -55,12 +49,8 @@ function parseArgs(args: string[]): Options {
help: false, help: false,
}; };
for (let i = 0; i < args.length; i++) { for (const arg of args) {
const arg = args[i]; if (arg === "--check-only") {
if (arg === "--shell" && args[i + 1]) {
options.shell = args[i + 1];
i++;
} else if (arg === "--check-only") {
options.checkOnly = true; options.checkOnly = true;
} else if (arg === "--force") { } else if (arg === "--force") {
options.force = true; options.force = true;
@@ -76,9 +66,8 @@ function printHelp(): void {
console.log(` console.log(`
${theme.heading("Shell Completion Test Script")} ${theme.heading("Shell Completion Test Script")}
This script simulates the shell completion prompt that appears during This script simulates the shell completion checks that run during
\`openclaw update\`. Use it to verify the completion installation flow \`openclaw update\`, \`openclaw doctor\`, and \`openclaw onboard\`.
without running a full update.
${theme.heading("Usage (run from repo root):")} ${theme.heading("Usage (run from repo root):")}
node --import tsx scripts/test-shell-completion.ts [options] node --import tsx scripts/test-shell-completion.ts [options]
@@ -86,110 +75,35 @@ ${theme.heading("Usage (run from repo root):")}
bun scripts/test-shell-completion.ts [options] bun scripts/test-shell-completion.ts [options]
${theme.heading("Options:")} ${theme.heading("Options:")}
--shell <shell> Override shell detection (zsh, bash, fish, powershell)
--check-only Only check status, don't prompt to install --check-only Only check status, don't prompt to install
--force Skip the "already installed" check and prompt anyway --force Skip the "already installed" check and prompt anyway
--help, -h Show this help message --help, -h Show this help message
${theme.heading("Behavior:")}
- If profile has completion but no cache: auto-regenerates cache
- If no completion at all: prompts to install
- If both profile and cache exist: nothing to do
${theme.heading("Examples:")} ${theme.heading("Examples:")}
node --import tsx scripts/test-shell-completion.ts node --import tsx scripts/test-shell-completion.ts
node --import tsx scripts/test-shell-completion.ts --check-only node --import tsx scripts/test-shell-completion.ts --check-only
node --import tsx scripts/test-shell-completion.ts --shell bash
node --import tsx scripts/test-shell-completion.ts --force node --import tsx scripts/test-shell-completion.ts --force
`); `);
} }
function isSupportedShell(shell: string): shell is SupportedShell { function getShellProfilePath(shell: string): string {
return SUPPORTED_SHELLS.includes(shell as SupportedShell);
}
async function pathExists(targetPath: string): Promise<boolean> {
try {
await fs.access(targetPath);
return true;
} catch {
return false;
}
}
function resolveCompletionCacheExtension(shell: SupportedShell): string {
switch (shell) {
case "powershell":
return "ps1";
case "fish":
return "fish";
case "bash":
return "bash";
default:
return "zsh";
}
}
async function ensureCompletionCache(shell: SupportedShell): Promise<boolean> {
const stateDir = resolveStateDir(process.env, os.homedir);
const cacheDir = path.join(stateDir, "completions");
const ext = resolveCompletionCacheExtension(shell);
const cachePath = path.join(cacheDir, `${CLI_NAME}.${ext}`);
if (await pathExists(cachePath)) {
console.log(` Completion cache: ${theme.success("exists")} ${theme.muted(`(${cachePath})`)}`);
return true;
}
console.log(` Completion cache: ${theme.warn("missing")}`);
console.log(theme.muted(" Generating completion cache..."));
// Use the CLI to generate the cache (same approach as tryWriteCompletionCache in update-cli.ts)
const binPath = path.join(process.cwd(), "openclaw.mjs");
if (!(await pathExists(binPath))) {
console.log(theme.error(` Cannot find ${binPath}. Run from the repo root.`));
return false;
}
// Use the same runtime as the CLI
const runtime = process.execPath;
const result = spawnSync(runtime, [binPath, "completion", "--write-state"], {
cwd: process.cwd(),
env: process.env,
encoding: "utf-8",
// Windows needs shell: true for proper execution in some cases
shell: process.platform === "win32",
});
if (result.error) {
console.log(theme.error(` Failed to generate cache: ${String(result.error)}`));
return false;
}
if (result.status !== 0) {
const stderr = result.stderr?.trim();
console.log(
theme.error(
` Failed to generate cache (exit ${result.status})${stderr ? `: ${stderr}` : ""}`,
),
);
return false;
}
console.log(theme.success(" Completion cache generated."));
return true;
}
function getShellProfilePath(shell: SupportedShell): string {
const home = process.env.HOME || os.homedir(); const home = process.env.HOME || os.homedir();
switch (shell) { switch (shell) {
case "zsh": case "zsh":
return path.join(home, ".zshrc"); return path.join(home, ".zshrc");
case "bash": case "bash":
// Linux typically uses .bashrc, macOS uses .bash_profile
return process.platform === "darwin" return process.platform === "darwin"
? path.join(home, ".bash_profile") ? path.join(home, ".bash_profile")
: path.join(home, ".bashrc"); : path.join(home, ".bashrc");
case "fish": case "fish":
return path.join(home, ".config", "fish", "config.fish"); return path.join(home, ".config", "fish", "config.fish");
case "powershell": case "powershell":
// PowerShell profile location varies by platform
if (process.platform === "win32") { if (process.platform === "win32") {
return path.join( return path.join(
process.env.USERPROFILE || home, process.env.USERPROFILE || home,
@@ -199,6 +113,8 @@ function getShellProfilePath(shell: SupportedShell): string {
); );
} }
return path.join(home, ".config", "powershell", "Microsoft.PowerShell_profile.ps1"); return path.join(home, ".config", "powershell", "Microsoft.PowerShell_profile.ps1");
default:
return path.join(home, ".zshrc");
} }
} }
@@ -214,35 +130,21 @@ async function main() {
console.log(theme.heading("Shell Completion Test")); console.log(theme.heading("Shell Completion Test"));
console.log(""); console.log("");
// Determine shell // Get completion status using the same function used by doctor/update/onboard
let shell: SupportedShell; const status = await checkShellCompletionStatus(CLI_NAME);
if (options.shell) {
if (!isSupportedShell(options.shell)) {
console.log(theme.error(`Unsupported shell: ${options.shell}`));
console.log(theme.muted(`Supported shells: ${SUPPORTED_SHELLS.join(", ")}`));
process.exit(1);
}
shell = options.shell;
console.log(` Shell: ${theme.accent(shell)} ${theme.muted("(override)")}`);
} else {
shell = resolveShellFromEnv() as SupportedShell;
console.log(` Shell: ${theme.accent(shell)} ${theme.muted("(detected from $SHELL)")}`);
}
// Show platform info console.log(` Shell: ${theme.accent(status.shell)} ${theme.muted("(detected from $SHELL)")}`);
console.log(` Platform: ${theme.muted(process.platform)} ${theme.muted(`(${os.release()})`)}`); console.log(` Platform: ${theme.muted(process.platform)} ${theme.muted(`(${os.release()})`)}`);
console.log(` Profile: ${theme.muted(getShellProfilePath(shell))}`); console.log(` Profile: ${theme.muted(getShellProfilePath(status.shell))}`);
console.log(` Cache path: ${theme.muted(status.cachePath)}`);
console.log(""); console.log("");
console.log(
// Ensure completion cache exists ` Profile configured: ${status.profileInstalled ? theme.success("yes") : theme.warn("no")}`,
const cacheOk = await ensureCompletionCache(shell); );
if (!cacheOk) { console.log(` Cache exists: ${status.cacheExists ? theme.success("yes") : theme.warn("no")}`);
console.log(theme.warn(" Continuing without cache (will use dynamic completion)...")); console.log(
} ` Uses slow pattern: ${status.usesSlowPattern ? theme.error("yes (needs upgrade)") : theme.success("no")}`,
);
// Check if completion is installed in shell profile
const installed = await isCompletionInstalled(shell, CLI_NAME);
console.log(` Profile configured: ${installed ? theme.success("yes") : theme.warn("no")}`);
console.log(""); console.log("");
if (options.checkOnly) { if (options.checkOnly) {
@@ -250,8 +152,34 @@ async function main() {
return; return;
} }
if (installed && !options.force) { // Profile uses slow dynamic pattern - upgrade to cached version
console.log(theme.muted("Shell completion is already installed. To test the prompt:")); if (status.usesSlowPattern) {
console.log(theme.warn("Profile uses slow dynamic completion. Upgrading to cached version..."));
const cacheGenerated = await ensureCompletionCacheExists(CLI_NAME);
if (cacheGenerated) {
await installCompletion(status.shell, false, CLI_NAME);
console.log(theme.success("Upgraded to cached completion."));
} else {
console.log(theme.error("Failed to generate cache."));
}
return;
}
// Profile has completion but no cache - auto-fix
if (status.profileInstalled && !status.cacheExists) {
console.log(theme.warn("Profile has completion but cache is missing. Regenerating..."));
const cacheGenerated = await ensureCompletionCacheExists(CLI_NAME);
if (cacheGenerated) {
console.log(theme.success("Cache regenerated successfully."));
} else {
console.log(theme.error("Failed to regenerate cache."));
}
return;
}
// Both profile and cache exist - nothing to do
if (status.profileInstalled && status.cacheExists && !options.force) {
console.log(theme.muted("Shell completion is fully configured. To test the prompt:"));
console.log( console.log(
theme.muted(" 1. Remove the '# OpenClaw Completion' block from your shell profile"), theme.muted(" 1. Remove the '# OpenClaw Completion' block from your shell profile"),
); );
@@ -261,11 +189,11 @@ async function main() {
return; return;
} }
// Simulate the prompt from update-cli.ts // No profile configured - prompt to install
console.log(theme.heading("Shell completion")); console.log(theme.heading("Shell completion"));
const shouldInstall = await confirm({ const shouldInstall = await confirm({
message: stylePromptMessage(`Enable ${shell} shell completion for ${CLI_NAME}?`), message: stylePromptMessage(`Enable ${status.shell} shell completion for ${CLI_NAME}?`),
initialValue: true, initialValue: true,
}); });
@@ -274,8 +202,19 @@ async function main() {
return; return;
} }
// Install completion // Generate cache first (required for fast shell startup)
await installCompletion(shell, false, CLI_NAME); if (!status.cacheExists) {
console.log(theme.muted("Generating completion cache..."));
const cacheGenerated = await ensureCompletionCacheExists(CLI_NAME);
if (!cacheGenerated) {
console.log(theme.error("Failed to generate completion cache."));
return;
}
console.log(theme.success("Cache generated."));
}
// Install to shell profile
await installCompletion(status.shell, false, CLI_NAME);
} }
main().catch((err) => { main().catch((err) => {