From 377a56525cd40544513f0a037faadc5fd7082e79 Mon Sep 17 00:00:00 2001 From: noam-home Date: Thu, 6 Nov 2025 14:03:59 +0200 Subject: [PATCH 1/6] shell completions for zsh --- README.md | 66 +++ .../design.md | 525 ++++++++++++++++++ .../proposal.md | 29 + .../specs/cli-completion/spec.md | 300 ++++++++++ .../2025-11-06-add-shell-completions/tasks.md | 81 +++ openspec/specs/cli-completion/spec.md | 284 ++++++++++ package.json | 3 + scripts/postinstall.js | 143 +++++ scripts/test-postinstall.sh | 57 ++ src/cli/index.ts | 49 ++ src/commands/completion.ts | 209 +++++++ src/core/completions/command-registry.ts | 324 +++++++++++ src/core/completions/completion-provider.ts | 128 +++++ src/core/completions/factory.ts | 72 +++ .../completions/generators/zsh-generator.ts | 325 +++++++++++ .../completions/installers/zsh-installer.ts | 235 ++++++++ src/core/completions/types.ts | 90 +++ src/utils/shell-detection.ts | 48 ++ test/commands/completion.test.ts | 269 +++++++++ .../completions/completion-provider.test.ts | 288 ++++++++++ .../generators/zsh-generator.test.ts | 381 +++++++++++++ .../installers/zsh-installer.test.ts | 311 +++++++++++ test/utils/shell-detection.test.ts | 158 ++++++ 23 files changed, 4375 insertions(+) create mode 100644 openspec/changes/archive/2025-11-06-add-shell-completions/design.md create mode 100644 openspec/changes/archive/2025-11-06-add-shell-completions/proposal.md create mode 100644 openspec/changes/archive/2025-11-06-add-shell-completions/specs/cli-completion/spec.md create mode 100644 openspec/changes/archive/2025-11-06-add-shell-completions/tasks.md create mode 100644 openspec/specs/cli-completion/spec.md create mode 100644 scripts/postinstall.js create mode 100755 scripts/test-postinstall.sh create mode 100644 src/commands/completion.ts create mode 100644 src/core/completions/command-registry.ts create mode 100644 src/core/completions/completion-provider.ts create mode 100644 src/core/completions/factory.ts create mode 100644 src/core/completions/generators/zsh-generator.ts create mode 100644 src/core/completions/installers/zsh-installer.ts create mode 100644 src/core/completions/types.ts create mode 100644 src/utils/shell-detection.ts create mode 100644 test/commands/completion.test.ts create mode 100644 test/core/completions/completion-provider.test.ts create mode 100644 test/core/completions/generators/zsh-generator.test.ts create mode 100644 test/core/completions/installers/zsh-installer.test.ts create mode 100644 test/utils/shell-detection.test.ts diff --git a/README.md b/README.md index 91b68a75..7fc10338 100644 --- a/README.md +++ b/README.md @@ -230,6 +230,72 @@ openspec validate # Check spec formatting and structure openspec archive [--yes|-y] # Move a completed change into archive/ (non-interactive with --yes) ``` +## Shell Completions + +OpenSpec provides shell tab completions for faster command entry. Completions are automatically installed during `npm install` for supported shells. + +### Supported Shells + +- **Zsh** (including Oh My Zsh) - fully supported with dynamic change/spec ID completion +- **Bash** - coming soon +- **Fish** - coming soon + +### Auto-Install Behavior + +When you install OpenSpec via `npm install -g @fission-ai/openspec`, the completions are automatically installed for your shell if: +- Your shell is detected and supported (currently Zsh) +- You're not in a CI environment (`CI=true`) +- You haven't opted out with `OPENSPEC_NO_COMPLETIONS=1` + +**Oh My Zsh users**: Completions are installed to `~/.oh-my-zsh/completions/_openspec` and activate automatically after restarting your shell. + +**Standard Zsh users**: Completions are installed to `~/.zsh/completions/_openspec`. You'll need to add the completions directory to your `fpath` in `~/.zshrc`: + +```bash +# Add completions directory to fpath +fpath=(~/.zsh/completions $fpath) + +# Initialize completion system +autoload -Uz compinit +compinit +``` + +Then restart your shell with `exec zsh`. + +### Manual Installation + +If auto-install was skipped or you want to reinstall: + +```bash +# Install for your current shell (auto-detected) +openspec completion install + +# Install for a specific shell +openspec completion install zsh + +# View the completion script +openspec completion generate zsh +``` + +### Disabling Auto-Install + +To prevent automatic installation during `npm install`: + +```bash +# Set environment variable before installing +OPENSPEC_NO_COMPLETIONS=1 npm install -g @fission-ai/openspec +``` + +### Uninstalling Completions + +```bash +# Uninstall completions for your current shell +openspec completion uninstall + +# Uninstall for a specific shell +openspec completion uninstall zsh +``` + ## Example: How AI Creates OpenSpec Files When you ask your AI assistant to "add two-factor authentication", it creates: diff --git a/openspec/changes/archive/2025-11-06-add-shell-completions/design.md b/openspec/changes/archive/2025-11-06-add-shell-completions/design.md new file mode 100644 index 00000000..4dac3e5d --- /dev/null +++ b/openspec/changes/archive/2025-11-06-add-shell-completions/design.md @@ -0,0 +1,525 @@ +# Shell Completions Design + +## Overview + +This design establishes a plugin-based architecture for shell completions that prioritizes clean TypeScript patterns, scalability, and maintainability. The system separates concerns between shell-specific generation logic, dynamic completion data providers, and installation automation. + +**Scope:** This proposal implements **Zsh completion only** (with Oh My Zsh priority). The architecture is designed to support bash, fish, and PowerShell in future proposals. + +## Native Shell Completion Behaviors + +**Design Philosophy:** We integrate with each shell's native completion system rather than attempting to customize or unify behaviors. This ensures familiar UX for users and reduces maintenance complexity. + +**Note:** While all four shell behaviors are documented below for architectural reference, **only Zsh is implemented in this proposal**. Bash, Fish, and PowerShell are documented to guide future implementations. + +### Bash Completion Behavior + +**Interaction Pattern:** +- **Single TAB:** Completes if only one match exists, otherwise does nothing +- **Double TAB (TAB TAB):** Displays all possible completions as a list +- **Type more characters + TAB:** Narrows matches and completes or shows refined list + +**OpenSpec Integration:** +```bash +# After installing: openspec completion install bash +openspec val # Completes to "openspec validate" +openspec validate # Shows: --all --changes --specs --strict --json [change-ids] [spec-ids] +openspec show add- # Shows all changes starting with "add-" +``` + +**Implementation:** Uses bash-completion framework with `_init_completion`, `compgen`, and `COMPREPLY` array. + +### Zsh Completion Behavior (with Oh My Zsh) + +**Interaction Pattern:** +- **Single TAB:** Shows interactive menu with all matches immediately +- **TAB / Arrow Keys:** Navigate through completion options +- **Enter:** Selects highlighted option +- **Ctrl+C / Esc:** Cancels completion menu + +**OpenSpec Integration:** +```zsh +# After installing: openspec completion install zsh +openspec val # Shows menu with "validate" and "view" highlighted +openspec show # Shows menu with all change IDs and spec IDs, categorized +``` + +**Implementation:** Uses Zsh completion system with `_arguments`, `_describe`, and `compadd` built-ins. Oh My Zsh provides enhanced menu styling automatically. + +### Fish Completion Behavior + +**Interaction Pattern:** +- **As-you-type:** Gray suggestions appear automatically in real-time +- **Right Arrow / Ctrl+F:** Accepts the suggestion +- **TAB:** Shows menu with all matches if multiple exist +- **TAB again:** Cycles through options or navigates menu +- **Enter:** Accepts current selection + +**OpenSpec Integration:** +```fish +# After installing: openspec completion install fish +openspec val # Gray suggestion shows "validate" immediately +openspec show a # Real-time suggestions for changes starting with "a" +openspec # Shows all commands with descriptions in paged menu +``` + +**Implementation:** Uses Fish's declarative `complete -c` syntax. Completions are auto-loaded from `~/.config/fish/completions/`. + +### PowerShell Completion Behavior + +**Interaction Pattern:** +- **TAB:** Cycles forward through completions one at a time (inline replacement) +- **Shift+TAB:** Cycles backward through completions +- **Ctrl+Space:** Shows IntelliSense-style menu (PSReadLine v2.2+) +- **Arrow Keys:** Navigate menu if shown + +**OpenSpec Integration:** +```powershell +# After installing: openspec completion install powershell +openspec val # Cycles: validate → view → validate +openspec show # Cycles through change IDs one by one +openspec # Shows IntelliSense menu with all commands +``` + +**Implementation:** Uses `Register-ArgumentCompleter` with custom script block that returns `[System.Management.Automation.CompletionResult]` objects. + +### Comparison Table + +| Shell | Trigger | Display Style | Navigation | Selection | +|-------------|-----------------|------------------------|----------------------|----------------| +| Bash | TAB TAB | List (printed once) | Type more + TAB | Auto-complete | +| Zsh | TAB | Interactive menu | TAB/Arrows | Enter | +| Fish | TAB/Auto | Real-time + menu | TAB/Arrows | Enter/Right | +| PowerShell | TAB | Inline cycling | TAB/Shift+TAB | Stop cycling | + +**Key Insight:** Each shell's completion UX reflects its design philosophy. We respect these conventions rather than forcing uniformity. + +## Architectural Principles + +### 1. Plugin-Based Generator System + +Each shell has unique completion syntax and conventions. Rather than creating a monolithic generator with branching logic, we use a plugin pattern where each shell implements a common interface: + +```typescript +interface CompletionGenerator { + generate(): string; + getInstallPath(): string; + getConfigFile(): string; +} +``` + +**Benefits:** +- New shells can be added without modifying existing generators +- Shell-specific logic is isolated and testable +- Type safety ensures all generators implement required methods +- Easy to maintain and understand (single responsibility per generator) + +**Implementation Classes:** +- `ZshCompletionGenerator` - Uses Zsh's `_arguments` and `_describe` functions +- `BashCompletionGenerator` - Uses `_init_completion` and `compgen` built-ins +- `FishCompletionGenerator` - Uses `complete -c` declarative syntax +- `PowerShellCompletionGenerator` - Uses `Register-ArgumentCompleter` cmdlet + +### 2. Centralized Command Registry + +Shell completions must stay synchronized with actual CLI commands. To avoid duplication and drift, we maintain a single source of truth: + +```typescript +type CommandDefinition = { + name: string; + description: string; + flags: FlagDefinition[]; + acceptsChangeId: boolean; + acceptsSpecId: boolean; + subcommands?: CommandDefinition[]; +}; + +const COMMAND_REGISTRY: CommandDefinition[] = [ + { + name: 'init', + description: 'Initialize OpenSpec in your project', + flags: [ + { name: '--tools', description: 'Configure AI tools non-interactively', hasValue: true } + ], + acceptsChangeId: false, + acceptsSpecId: false + }, + // ... all other commands +]; +``` + +**Benefits:** +- All generators consume the same command definitions +- Adding a new command automatically propagates to all shells +- Flag changes only need to be made in one place +- Type safety prevents typos and missing fields +- Easier to test (mock the registry) + +**TypeScript Sugar:** +- Use `const` assertions for readonly registry +- Leverage discriminated unions for command types +- Use `satisfies` operator to ensure registry matches interface + +### 3. Dynamic Completion Provider + +Change and spec IDs are project-specific and discovered at runtime. A dedicated provider encapsulates this logic: + +```typescript +class CompletionProvider { + private changeCache: { ids: string[]; timestamp: number } | null = null; + private specCache: { ids: string[]; timestamp: number } | null = null; + private readonly CACHE_TTL_MS = 2000; + + async getChangeIds(): Promise { + if (this.changeCache && Date.now() - this.changeCache.timestamp < this.CACHE_TTL_MS) { + return this.changeCache.ids; + } + + const ids = await discoverActiveChangeIds(); + this.changeCache = { ids, timestamp: Date.now() }; + return ids; + } + + async getSpecIds(): Promise { + // Similar caching logic + } + + isOpenSpecProject(): boolean { + // Check for openspec/ directory + } +} +``` + +**Benefits:** +- Caching reduces file system overhead during rapid tab completion +- Encapsulates project detection logic +- Easy to test with mocked file system +- Shared across all shell generators + +**Design Decisions:** +- 2-second cache TTL balances freshness with performance +- Cache per-process (not persistent) to avoid stale data across sessions +- Graceful degradation when outside OpenSpec projects + +### 4. Separate Installation Logic + +Installation involves shell configuration file manipulation, which differs from generation. We separate this concern: + +```typescript +interface CompletionInstaller { + install(): Promise; + uninstall(): Promise; + isInstalled(): Promise; +} +``` + +**Shell-Specific Installers:** +- `ZshInstaller` - Handles both Oh My Zsh (custom completions) and standard Zsh (fpath) +- `BashInstaller` - Detects completion directories and sources from `.bashrc` +- `FishInstaller` - Writes to `~/.config/fish/completions/` (auto-loaded) +- `PowerShellInstaller` - Appends to PowerShell profile + +**Benefits:** +- Installation logic doesn't pollute generator code +- Can test installation without generating completion scripts +- Easier to handle edge cases (missing directories, permissions, already installed) + +### 5. Type-Safe Shell Detection + +We use TypeScript's literal types and type guards for shell detection: + +```typescript +type SupportedShell = 'bash' | 'zsh' | 'fish' | 'powershell'; + +function detectShell(): SupportedShell { + const shellPath = process.env.SHELL || ''; + const shellName = path.basename(shellPath).toLowerCase(); + + // PowerShell normalization + if (shellName === 'pwsh' || shellName === 'powershell') { + return 'powershell'; + } + + const supported: SupportedShell[] = ['bash', 'zsh', 'fish', 'powershell']; + if (supported.includes(shellName as SupportedShell)) { + return shellName as SupportedShell; + } + + throw new Error(`Shell '${shellName}' is not supported. Supported: ${supported.join(', ')}`); +} +``` + +**Benefits:** +- Compile-time type checking prevents invalid shell names +- Easy to add new shells (add to union type) +- Type narrowing works in switch statements +- Clear error messages for unsupported shells + +### 6. Factory Pattern for Instantiation + +A factory function selects the appropriate generator/installer based on shell type: + +```typescript +function createGenerator(shell: SupportedShell, provider: CompletionProvider): CompletionGenerator { + switch (shell) { + case 'bash': return new BashCompletionGenerator(COMMAND_REGISTRY, provider); + case 'zsh': return new ZshCompletionGenerator(COMMAND_REGISTRY, provider); + case 'fish': return new FishCompletionGenerator(COMMAND_REGISTRY, provider); + case 'powershell': return new PowerShellCompletionGenerator(COMMAND_REGISTRY, provider); + } +} +``` + +**Benefits:** +- Single point of instantiation +- Type safety ensures exhaustive switch (TypeScript error if shell type missing) +- Easy to inject dependencies (registry, provider) + +## Command Structure + +**This Proposal (Zsh-only):** +``` +openspec completion +├── zsh # Generate Zsh completion script +├── install [shell] # Install Zsh completion (auto-detects or explicit zsh) +└── uninstall [shell] # Remove Zsh completion (auto-detects or explicit zsh) +``` + +**Future (after follow-up proposals):** +``` +openspec completion +├── bash # Generate Bash completion script (future) +├── zsh # Generate Zsh completion script (this proposal) +├── fish # Generate Fish completion script (future) +├── powershell # Generate PowerShell completion script (future) +├── install [shell] # Install completion (auto-detects or explicit shell) +└── uninstall [shell] # Remove completion (auto-detects or explicit shell) +``` + +## File Organization + +**This Proposal (Zsh-only):** +``` +src/ +├── commands/ +│ └── completion.ts # CLI command registration (zsh, install, uninstall) +├── core/ +│ └── completions/ +│ ├── types.ts # Interfaces: CompletionGenerator, CommandDefinition, etc. +│ ├── command-registry.ts # Single source of truth for OpenSpec commands +│ ├── completion-provider.ts # Dynamic change/spec ID discovery with caching +│ ├── factory.ts # Factory for instantiating Zsh generator/installer +│ ├── generators/ +│ │ └── zsh-generator.ts # Zsh completion script generator +│ └── installers/ +│ └── zsh-installer.ts # Handles Oh My Zsh + standard Zsh installation +└── utils/ + └── shell-detection.ts # Shell detection (returns 'zsh' or throws) +``` + +**Future additions (bash, fish, powershell):** +- `generators/bash-generator.ts`, `fish-generator.ts`, `powershell-generator.ts` +- `installers/bash-installer.ts`, `fish-installer.ts`, `powershell-installer.ts` +- Update `shell-detection.ts` to support additional shell types + +## Oh My Zsh Priority + +Zsh implementation prioritizes Oh My Zsh because: +1. **Popularity** - Oh My Zsh is the most popular Zsh configuration framework +2. **Convention** - Has standard completion directory (`~/.oh-my-zsh/custom/completions/`) +3. **Detection** - Easy to detect via `$ZSH` environment variable +4. **Fallback** - Standard Zsh support provides compatibility when Oh My Zsh isn't installed + +**Installation Strategy:** +```typescript +if (isOhMyZshInstalled()) { + // Install to ~/.oh-my-zsh/custom/completions/_openspec + // Automatically loaded by Oh My Zsh +} else { + // Install to ~/.zsh/completions/_openspec + // Update ~/.zshrc with fpath and compinit if needed +} +``` + +## Caching Strategy + +Dynamic completions cache results for 2 seconds to balance freshness with performance: + +**Why 2 seconds?** +- Typical tab completion sessions last < 2 seconds +- Prevents repeated file system scans during rapid tabbing +- Short enough to feel "live" when changes/specs are added +- Automatic per-process expiration (no stale data across sessions) + +**Implementation:** +```typescript +private changeCache: { ids: string[]; timestamp: number } | null = null; +private readonly CACHE_TTL_MS = 2000; + +if (this.changeCache && Date.now() - this.changeCache.timestamp < this.CACHE_TTL_MS) { + return this.changeCache.ids; // Use cached +} +// Refresh cache +``` + +## Error Handling Philosophy + +Completions should degrade gracefully rather than break workflows: + +1. **Unsupported shell** - Clear error with list of supported shells +2. **Not in OpenSpec project** - Skip dynamic completions, only offer static commands +3. **Permission errors** - Suggest alternative installation methods +4. **Missing config directories** - Auto-create with user notification +5. **Already installed** - Offer to reinstall/update +6. **Not installed (during uninstall)** - Exit gracefully with informational message + +## Testing Strategy + +Each component is independently testable: + +1. **Unit Tests** + - Shell detection with mocked `$SHELL` environment variable + - Generator output verification (regex pattern matching) + - Completion provider caching behavior + - Command registry structure validation + +2. **Integration Tests** + - Installation to temporary test directories + - Configuration file modifications + - End-to-end command flow (generate → install → verify) + +3. **Manual Testing** + - Real shell environments (Oh My Zsh, Bash, Fish, PowerShell) + - Tab completion behavior in OpenSpec projects + - Dynamic change/spec ID suggestions + - Installation/uninstallation workflows + +## TypeScript Sugar Patterns + +### 1. Const Assertions for Immutable Data +```typescript +const COMMAND_REGISTRY = [ + { name: 'init', ... }, + { name: 'list', ... } +] as const; +``` + +### 2. Discriminated Unions for Command Types +```typescript +type Command = + | { type: 'simple'; name: string } + | { type: 'with-subcommands'; name: string; subcommands: Command[] }; +``` + +### 3. Template Literal Types for Strings +```typescript +type ShellConfigFile = `~/.${SupportedShell}rc` | `~/.${SupportedShell}_profile`; +``` + +### 4. Satisfies Operator for Type Validation +```typescript +const config = { + shell: 'zsh', + path: '~/.zshrc' +} satisfies ShellConfig; +``` + +### 5. Optional Chaining and Nullish Coalescing +```typescript +const path = process.env.ZSH ?? `${os.homedir()}/.oh-my-zsh`; +``` + +### 6. Async/Await with Promise.all for Parallel Operations +```typescript +const [changes, specs] = await Promise.all([ + provider.getChangeIds(), + provider.getSpecIds() +]); +``` + +## Scalability Considerations + +### Adding a New Shell + +1. Define shell in `SupportedShell` union type +2. Create generator class implementing `CompletionGenerator` +3. Create installer class implementing `CompletionInstaller` +4. Add cases to factory functions +5. Add command registration in CLI +6. Write tests + +**TypeScript will enforce** that all switch statements are updated (exhaustiveness checking). + +### Adding a New Command + +1. Add to `COMMAND_REGISTRY` with appropriate metadata +2. All generators automatically include it +3. Update tests to verify new command appears + +### Changing Completion Behavior + +Dynamic completion logic is centralized in `CompletionProvider`, making behavior changes trivial without touching shell-specific code. + +## Trade-offs and Decisions + +### Decision: Separate Generators vs. Template Engine + +**Chosen:** Separate generator classes per shell + +**Alternative:** Template engine with shell-specific templates + +**Rationale:** +- Shell completion syntax is fundamentally different (not just text substitution) +- Type safety is better with classes than templates +- Logic complexity (caching, dynamic completions) doesn't fit template paradigm +- Easier to debug and test dedicated classes + +### Decision: 2-Second Cache TTL + +**Chosen:** 2-second cache + +**Alternatives:** No cache (slow), longer cache (stale), persistent cache (complex) + +**Rationale:** +- Balances performance with freshness +- Matches typical user interaction patterns +- Simple implementation (no invalidation complexity) +- Automatic cleanup on process exit + +### Decision: Oh My Zsh Detection + +**Chosen:** Check `$ZSH` env var first, then `~/.oh-my-zsh/` directory + +**Rationale:** +- `$ZSH` is set by Oh My Zsh initialization (reliable) +- Directory check is fallback for non-interactive scenarios +- Standard Zsh serves as ultimate fallback + +### Decision: Installation Automation vs. Manual Instructions + +**Chosen:** Automated installation with install/uninstall commands + +**Alternative:** Generate script and provide manual installation instructions + +**Rationale:** +- Better user experience (one command vs. multiple manual steps) +- Reduces errors from manual configuration +- Aligns with user expectations for modern CLI tools +- Still supports manual workflow via script generation to stdout + +## Future Enhancements + +1. **Contextual Flag Completion** - Suggest only valid flags for current command +2. **Fuzzy Matching** - Allow partial matching for change/spec IDs +3. **Rich Descriptions** - Include "why" section in completion suggestions (shell-dependent) +4. **Completion Stats** - Track completion usage for analytics +5. **Custom Completion Hooks** - Allow projects to extend completions +6. **MCP Integration** - Provide completions via Model Context Protocol + +## References + +- [Bash Programmable Completion](https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html) +- [Zsh Completion System](https://zsh.sourceforge.io/Doc/Release/Completion-System.html) +- [Fish Completions](https://fishshell.com/docs/current/completions.html) +- [PowerShell Argument Completers](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/register-argumentcompleter) +- [Oh My Zsh Custom Completions](https://github.com/ohmyzsh/ohmyzsh/wiki/Customization#adding-custom-completions) diff --git a/openspec/changes/archive/2025-11-06-add-shell-completions/proposal.md b/openspec/changes/archive/2025-11-06-add-shell-completions/proposal.md new file mode 100644 index 00000000..198b0bd7 --- /dev/null +++ b/openspec/changes/archive/2025-11-06-add-shell-completions/proposal.md @@ -0,0 +1,29 @@ +# Add Shell Completions + +## Why + +OpenSpec CLI commands lack shell completion, forcing users to remember all commands, subcommands, flags, and change/spec IDs manually. This creates friction during daily use and slows developer workflows. Shell completions are a standard expectation for modern CLI tools and significantly improve user experience through: +- Faster command discovery via tab completion +- Reduced cognitive load by removing memorization requirements +- Fewer typos through validated suggestions +- Professional polish expected of production-grade tools + +## What Changes + +This change adds shell completion support for the OpenSpec CLI, starting with **Zsh (including Oh My Zsh)** and establishing a scalable architecture for future shells (bash, fish, PowerShell). The implementation provides: + +1. **New `openspec completion` command** with Zsh generation and installation/uninstallation capabilities +2. **Native Zsh integration** that respects standard Zsh tab completion behavior (single-TAB menu navigation) +3. **Dynamic completion providers** that discover active changes and specs from the current project +4. **Plugin-based architecture** using TypeScript interfaces for easy extension to additional shells in future proposals +5. **Installation automation** for Oh My Zsh (priority) and standard Zsh configurations +6. **Context-aware suggestions** that only activate within OpenSpec-enabled projects + +The architecture emphasizes clean TypeScript patterns, composable generators, separation of concerns between shell-specific logic and shared completion data providers, and integration with native shell completion systems. Other shells (bash, fish, PowerShell) are architecturally documented but not implemented in this proposal—they will be added in follow-up changes. + +## Deltas + +### Delta: New CLI completion specification +- **Spec:** cli-completion +- **Operation:** ADDED +- **Description:** Defines requirements for the new `openspec completion` command including generation, installation, and shell-specific behaviors for Oh My Zsh, bash, fish, and PowerShell. \ No newline at end of file diff --git a/openspec/changes/archive/2025-11-06-add-shell-completions/specs/cli-completion/spec.md b/openspec/changes/archive/2025-11-06-add-shell-completions/specs/cli-completion/spec.md new file mode 100644 index 00000000..a5318992 --- /dev/null +++ b/openspec/changes/archive/2025-11-06-add-shell-completions/specs/cli-completion/spec.md @@ -0,0 +1,300 @@ +# CLI Completion Specification + +## Purpose + +The `openspec completion` command SHALL provide shell completion functionality for all OpenSpec CLI commands, flags, and dynamic values (change IDs, spec IDs), with support for Zsh (including Oh My Zsh) and a scalable architecture ready for future shells (bash, fish, PowerShell). The completion system SHALL integrate with Zsh's native completion behavior rather than attempting to customize the user experience. + +## ADDED Requirements + +### Requirement: Native Shell Behavior Integration + +The completion system SHALL respect and integrate with Zsh's native completion patterns and user interaction model. + +#### Scenario: Zsh native completion + +- **WHEN** generating Zsh completion scripts +- **THEN** use Zsh completion system with `_arguments`, `_describe`, and `compadd` +- **AND** completions SHALL trigger on single TAB (standard Zsh behavior) +- **AND** display as an interactive menu that users navigate with TAB/arrow keys +- **AND** support Oh My Zsh's enhanced menu styling automatically + +#### Scenario: No custom UX patterns + +- **WHEN** implementing Zsh completion +- **THEN** do NOT attempt to customize completion trigger behavior +- **AND** do NOT override Zsh-specific navigation patterns +- **AND** ensure completions feel native to experienced Zsh users + +### Requirement: Command Structure + +The completion command SHALL follow a subcommand pattern for generating and managing completion scripts. + +#### Scenario: Available subcommands + +- **WHEN** user executes `openspec completion --help` +- **THEN** display available subcommands: + - `zsh` - Generate Zsh completion script + - `install [shell]` - Install completion for Zsh (auto-detects or requires explicit shell) + - `uninstall [shell]` - Remove completion for Zsh (auto-detects or requires explicit shell) + +### Requirement: Shell Detection + +The completion system SHALL automatically detect the user's current shell environment. + +#### Scenario: Detecting Zsh from environment + +- **WHEN** no shell is explicitly specified +- **THEN** read the `$SHELL` environment variable +- **AND** extract the shell name from the path (e.g., `/bin/zsh` → `zsh`) +- **AND** validate the shell is `zsh` +- **AND** throw an error if the shell is not `zsh`, with message indicating only Zsh is currently supported + +#### Scenario: Non-Zsh shell detection + +- **WHEN** shell path indicates bash, fish, powershell, or other non-Zsh shell +- **THEN** throw error: "Shell '' is not supported yet. Currently supported: zsh" + +### Requirement: Completion Generation + +The completion command SHALL generate Zsh completion scripts on demand. + +#### Scenario: Generating Zsh completion + +- **WHEN** user executes `openspec completion zsh` +- **THEN** output a complete Zsh completion script to stdout +- **AND** include completions for all commands: init, list, show, validate, archive, view, update, change, spec, completion +- **AND** include all command-specific flags and options +- **AND** use Zsh's `_arguments` and `_describe` built-in functions +- **AND** support dynamic completion for change and spec IDs + +### Requirement: Dynamic Completions + +The completion system SHALL provide context-aware dynamic completions for project-specific values. + +#### Scenario: Completing change IDs + +- **WHEN** completing arguments for commands that accept change names (show, validate, archive) +- **THEN** discover active changes from `openspec/changes/` directory +- **AND** exclude archived changes in `openspec/changes/archive/` +- **AND** return change IDs as completion suggestions +- **AND** only provide suggestions when inside an OpenSpec-enabled project + +#### Scenario: Completing spec IDs + +- **WHEN** completing arguments for commands that accept spec names (show, validate) +- **THEN** discover specs from `openspec/specs/` directory +- **AND** return spec IDs as completion suggestions +- **AND** only provide suggestions when inside an OpenSpec-enabled project + +#### Scenario: Completion caching + +- **WHEN** dynamic completions are requested +- **THEN** cache discovered change and spec IDs for 2 seconds +- **AND** reuse cached values for subsequent requests within cache window +- **AND** automatically refresh cache after expiration + +#### Scenario: Project detection + +- **WHEN** user requests completions outside an OpenSpec project +- **THEN** skip dynamic change/spec ID completions +- **AND** only suggest static commands and flags + +### Requirement: Installation Automation + +The completion command SHALL automatically install completion scripts into shell configuration files. + +#### Scenario: Installing for Oh My Zsh + +- **WHEN** user executes `openspec completion install zsh` +- **THEN** detect if Oh My Zsh is installed by checking for `$ZSH` environment variable or `~/.oh-my-zsh/` directory +- **AND** create custom completions directory at `~/.oh-my-zsh/custom/completions/` if it doesn't exist +- **AND** write completion script to `~/.oh-my-zsh/custom/completions/_openspec` +- **AND** ensure `~/.oh-my-zsh/custom/completions` is in `$fpath` by updating `~/.zshrc` if needed +- **AND** display success message with instruction to run `exec zsh` or restart terminal + +#### Scenario: Installing for standard Zsh + +- **WHEN** user executes `openspec completion install zsh` and Oh My Zsh is not detected +- **THEN** create completions directory at `~/.zsh/completions/` if it doesn't exist +- **AND** write completion script to `~/.zsh/completions/_openspec` +- **AND** add `fpath=(~/.zsh/completions $fpath)` to `~/.zshrc` if not already present +- **AND** add `autoload -Uz compinit && compinit` to `~/.zshrc` if not already present +- **AND** display success message with instruction to run `exec zsh` or restart terminal + +#### Scenario: Auto-detecting Zsh for installation + +- **WHEN** user executes `openspec completion install` without specifying a shell +- **THEN** detect current shell using shell detection logic +- **AND** install completion if detected shell is Zsh +- **AND** throw error if detected shell is not Zsh +- **AND** display which shell was detected + +#### Scenario: Already installed + +- **WHEN** completion is already installed for the target shell +- **THEN** display message indicating completion is already installed +- **AND** offer to reinstall/update by overwriting existing files +- **AND** exit with code 0 + +### Requirement: Uninstallation + +The completion command SHALL remove installed completion scripts and configuration. + +#### Scenario: Uninstalling Oh My Zsh completion + +- **WHEN** user executes `openspec completion uninstall zsh` +- **THEN** remove `~/.oh-my-zsh/custom/completions/_openspec` if Oh My Zsh is detected +- **AND** remove `~/.zsh/completions/_openspec` if standard Zsh setup is detected +- **AND** optionally remove fpath modifications from `~/.zshrc` (with confirmation) +- **AND** display success message + +#### Scenario: Auto-detecting Zsh for uninstallation + +- **WHEN** user executes `openspec completion uninstall` without specifying a shell +- **THEN** detect current shell and uninstall completion if shell is Zsh +- **AND** throw error if detected shell is not Zsh + +#### Scenario: Not installed + +- **WHEN** attempting to uninstall completion that isn't installed +- **THEN** display message indicating completion is not installed +- **AND** exit with code 0 + +### Requirement: Architecture Patterns + +The completion implementation SHALL follow clean architecture principles with TypeScript best practices. + +#### Scenario: Shell-specific generators + +- **WHEN** implementing completion generators +- **THEN** create `ZshCompletionGenerator` class for Zsh +- **AND** implement a common `CompletionGenerator` interface with methods: + - `generate(): string` - Returns complete shell script + - `getInstallPath(): string` - Returns target installation path + - `getConfigFile(): string` - Returns shell configuration file path +- **AND** design interface to be extensible for future shells (bash, fish, powershell) + +#### Scenario: Dynamic completion providers + +- **WHEN** implementing dynamic completions +- **THEN** create a `CompletionProvider` class that encapsulates project discovery logic +- **AND** implement methods: + - `getChangeIds(): Promise` - Discovers active change IDs + - `getSpecIds(): Promise` - Discovers spec IDs + - `isOpenSpecProject(): boolean` - Checks if current directory is OpenSpec-enabled +- **AND** implement caching with 2-second TTL using class properties + +#### Scenario: Command registry + +- **WHEN** defining completable commands +- **THEN** create a centralized `CommandDefinition` type with properties: + - `name: string` - Command name + - `description: string` - Help text + - `flags: FlagDefinition[]` - Available flags + - `acceptsChangeId: boolean` - Whether command takes change ID argument + - `acceptsSpecId: boolean` - Whether command takes spec ID argument + - `subcommands?: CommandDefinition[]` - Nested subcommands +- **AND** export a `COMMAND_REGISTRY` constant with all command definitions +- **AND** generators consume this registry to ensure consistency + +#### Scenario: Type-safe shell detection + +- **WHEN** implementing shell detection +- **THEN** define a `SupportedShell` type as literal type: `'zsh'` +- **AND** implement `detectShell()` function that returns 'zsh' or throws error +- **AND** design type to be extensible (e.g., future: `'bash' | 'zsh' | 'fish' | 'powershell'`) + +### Requirement: Error Handling + +The completion command SHALL provide clear error messages for common failure scenarios. + +#### Scenario: Unsupported shell + +- **WHEN** user requests completion for unsupported shell (bash, fish, powershell, etc.) +- **THEN** display error message: "Shell '' is not supported yet. Currently supported: zsh" +- **AND** exit with code 1 + +#### Scenario: Permission errors during installation + +- **WHEN** installation fails due to file permission issues +- **THEN** display clear error message indicating permission problem +- **AND** suggest using appropriate permissions or alternative installation method +- **AND** exit with code 1 + +#### Scenario: Missing shell configuration directory + +- **WHEN** expected shell configuration directory doesn't exist +- **THEN** create the directory automatically (with user notification) +- **AND** proceed with installation + +#### Scenario: Shell not detected + +- **WHEN** `openspec completion install` cannot detect current shell or detects non-Zsh shell +- **THEN** display error: "Could not detect Zsh. Please specify explicitly: openspec completion install zsh" +- **AND** exit with code 1 + +### Requirement: Output Format + +The completion command SHALL provide machine-parseable and human-readable output. + +#### Scenario: Script generation output + +- **WHEN** generating completion script to stdout +- **THEN** output only the completion script content (no extra messages) +- **AND** allow redirection to files: `openspec completion zsh > /path/to/_openspec` + +#### Scenario: Installation success output + +- **WHEN** installation completes successfully +- **THEN** display formatted success message with: + - Checkmark indicator + - Installation location + - Next steps (shell reload instructions) +- **AND** use colors when terminal supports it (unless `--no-color` is set) + +#### Scenario: Verbose installation output + +- **WHEN** user provides `--verbose` flag during installation +- **THEN** display detailed steps: + - Shell detection result + - Target file paths + - Configuration modifications + - File creation confirmations + +### Requirement: Testing Support + +The completion implementation SHALL be testable with unit and integration tests. + +#### Scenario: Mock shell environment + +- **WHEN** writing tests for shell detection +- **THEN** allow overriding `$SHELL` environment variable +- **AND** use dependency injection for file system operations + +#### Scenario: Generator output verification + +- **WHEN** testing completion generators +- **THEN** verify generated scripts contain expected patterns +- **AND** test that command registry is properly consumed +- **AND** ensure dynamic completion placeholders are present + +#### Scenario: Installation simulation + +- **WHEN** testing installation logic +- **THEN** use temporary test directories instead of actual home directories +- **AND** verify file creation without modifying real shell configurations +- **AND** test path resolution logic independently + +## Not in Scope + +The following shells are **architecturally documented but not implemented** in this proposal. They will be added in future proposals: + +- **Bash completion** - Will use bash-completion framework with `_init_completion`, `compgen`, and `COMPREPLY` +- **Fish completion** - Will use Fish's declarative `complete -c` syntax +- **PowerShell completion** - Will use `Register-ArgumentCompleter` with completion result objects + +The plugin-based architecture (CompletionGenerator interface, command registry, dynamic providers) is designed to make adding these shells straightforward in follow-up changes. + +## Why + +Shell completions are essential for professional CLI tools and significantly improve developer experience by reducing friction, errors, and cognitive load during daily workflows. diff --git a/openspec/changes/archive/2025-11-06-add-shell-completions/tasks.md b/openspec/changes/archive/2025-11-06-add-shell-completions/tasks.md new file mode 100644 index 00000000..84589567 --- /dev/null +++ b/openspec/changes/archive/2025-11-06-add-shell-completions/tasks.md @@ -0,0 +1,81 @@ +# Implementation Tasks + +## Phase 1: Foundation & Architecture + +- [x] Create `src/utils/shell-detection.ts` with `SupportedShell` type and `detectShell()` function +- [x] Create `src/core/completions/types.ts` with interfaces: `CompletionGenerator`, `CommandDefinition`, `FlagDefinition` +- [x] Create `src/core/completions/command-registry.ts` with `COMMAND_REGISTRY` constant defining all OpenSpec commands, flags, and metadata +- [x] Create `src/core/completions/completion-provider.ts` with `CompletionProvider` class for dynamic change/spec ID discovery with 2-second caching +- [x] Write tests for shell detection (`test/utils/shell-detection.test.ts`) +- [x] Write tests for completion provider (`test/core/completions/completion-provider.test.ts`) + +## Phase 2: Zsh Completion (Oh My Zsh Priority) + +- [x] Create `src/core/completions/generators/zsh-generator.ts` implementing `CompletionGenerator` interface +- [x] Implement Zsh script generation using `_arguments` and `_describe` patterns +- [x] Add dynamic completion logic for change/spec IDs using completion provider +- [x] Test Zsh generator output (`test/core/completions/generators/zsh-generator.test.ts`) +- [x] Create `src/core/completions/installers/zsh-installer.ts` with Oh My Zsh and standard Zsh support +- [x] Implement Oh My Zsh detection (`$ZSH` env var or `~/.oh-my-zsh/` directory) +- [x] Implement installation to `~/.oh-my-zsh/custom/completions/_openspec` for Oh My Zsh +- [x] Implement fallback installation to `~/.zsh/completions/_openspec` with `fpath` updates +- [x] Test Zsh installer logic with mocked file system (`test/core/completions/installers/zsh-installer.test.ts`) + +## Phase 3: CLI Command Implementation + +- [x] Create `src/commands/completion.ts` with `CompletionCommand` class +- [x] Register `completion` command in `src/cli/index.ts` with subcommands: generate, install, uninstall +- [x] Implement `generateSubcommand()` that outputs Zsh script to stdout +- [x] Implement `installSubcommand(shell?: 'zsh')` with auto-detection for Zsh-only +- [x] Implement `uninstallSubcommand(shell?: 'zsh')` for removing Zsh completions +- [x] Add `--verbose` flag support for detailed installation output +- [x] Add error handling with clear messages: "Shell '' is not supported yet. Currently supported: zsh" +- [x] Test completion command integration (`test/commands/completion.test.ts`) + +## Phase 4: Integration & Polish + +- [x] Create factory pattern in `src/core/completions/factory.ts` to instantiate Zsh generator/installer (extensible for future shells) +- [x] Add `completion` command to command registry for self-referential completion +- [x] Implement dynamic completion helper functions in Zsh generator (`_openspec_complete_changes`, `_openspec_complete_specs`, `_openspec_complete_items`) +- [x] Add 'shell' positional type for completion command arguments +- [x] Test completion generation with dynamic helpers +- [x] Test completion install/uninstall flow +- [x] Verify all tests pass (97 completion tests, 340 total tests) +- [x] Implement auto-install via npm postinstall script +- [x] Add safety checks (CI detection, opt-out flag) +- [x] Handle Oh My Zsh vs standard Zsh installation paths +- [x] Add test script for postinstall validation +- [x] Document auto-install behavior and opt-out in README +- [ ] Manually test Zsh completion in Oh My Zsh environment (install, test tab completion, uninstall) +- [ ] Manually test Zsh completion in standard Zsh environment +- [ ] Test dynamic change/spec ID completion in real OpenSpec projects +- [ ] Verify completion cache behavior (2-second TTL) +- [ ] Test behavior outside OpenSpec projects (should skip dynamic completions) +- [x] Update `openspec --help` output to include completion command (automatically done via Commander) + +## Phase 5: Edge Cases & Error Handling + +- [ ] Test and handle permission errors during installation +- [ ] Test and handle missing shell configuration directories (auto-create with notification) +- [ ] Test "already installed" detection and reinstall flow +- [ ] Test "not installed" detection during uninstall +- [ ] Verify `--no-color` flag is respected in completion command output +- [ ] Test shell detection failure scenarios with helpful error messages +- [ ] Ensure graceful handling when `$SHELL` is unset or invalid +- [ ] Test non-Zsh shells get clear "not supported yet" error messages +- [ ] Test generator output can be redirected to files without corruption + +## Dependencies + +- Phase 2 depends on Phase 1 (foundation must exist first) +- Phase 3 depends on Phase 2 (CLI needs Zsh generator working) +- Phase 4 depends on Phase 3 (integration requires CLI + Zsh implementation) +- Phase 5 depends on Phase 4 (edge case testing after core functionality works) + +## Future Work (Not in This Proposal) + +- **Bash completions** - Create bash-generator.ts and bash-installer.ts in follow-up proposal +- **Fish completions** - Create fish-generator.ts and fish-installer.ts in follow-up proposal +- **PowerShell completions** - Create powershell-generator.ts and powershell-installer.ts in follow-up proposal + +The architecture is designed to make adding these shells straightforward by implementing the `CompletionGenerator` interface. diff --git a/openspec/specs/cli-completion/spec.md b/openspec/specs/cli-completion/spec.md new file mode 100644 index 00000000..c467321e --- /dev/null +++ b/openspec/specs/cli-completion/spec.md @@ -0,0 +1,284 @@ +# cli-completion Specification + +## Purpose +TBD - created by archiving change add-shell-completions. Update Purpose after archive. +## Requirements +### Requirement: Native Shell Behavior Integration + +The completion system SHALL respect and integrate with Zsh's native completion patterns and user interaction model. + +#### Scenario: Zsh native completion + +- **WHEN** generating Zsh completion scripts +- **THEN** use Zsh completion system with `_arguments`, `_describe`, and `compadd` +- **AND** completions SHALL trigger on single TAB (standard Zsh behavior) +- **AND** display as an interactive menu that users navigate with TAB/arrow keys +- **AND** support Oh My Zsh's enhanced menu styling automatically + +#### Scenario: No custom UX patterns + +- **WHEN** implementing Zsh completion +- **THEN** do NOT attempt to customize completion trigger behavior +- **AND** do NOT override Zsh-specific navigation patterns +- **AND** ensure completions feel native to experienced Zsh users + +### Requirement: Command Structure + +The completion command SHALL follow a subcommand pattern for generating and managing completion scripts. + +#### Scenario: Available subcommands + +- **WHEN** user executes `openspec completion --help` +- **THEN** display available subcommands: + - `zsh` - Generate Zsh completion script + - `install [shell]` - Install completion for Zsh (auto-detects or requires explicit shell) + - `uninstall [shell]` - Remove completion for Zsh (auto-detects or requires explicit shell) + +### Requirement: Shell Detection + +The completion system SHALL automatically detect the user's current shell environment. + +#### Scenario: Detecting Zsh from environment + +- **WHEN** no shell is explicitly specified +- **THEN** read the `$SHELL` environment variable +- **AND** extract the shell name from the path (e.g., `/bin/zsh` → `zsh`) +- **AND** validate the shell is `zsh` +- **AND** throw an error if the shell is not `zsh`, with message indicating only Zsh is currently supported + +#### Scenario: Non-Zsh shell detection + +- **WHEN** shell path indicates bash, fish, powershell, or other non-Zsh shell +- **THEN** throw error: "Shell '' is not supported yet. Currently supported: zsh" + +### Requirement: Completion Generation + +The completion command SHALL generate Zsh completion scripts on demand. + +#### Scenario: Generating Zsh completion + +- **WHEN** user executes `openspec completion zsh` +- **THEN** output a complete Zsh completion script to stdout +- **AND** include completions for all commands: init, list, show, validate, archive, view, update, change, spec, completion +- **AND** include all command-specific flags and options +- **AND** use Zsh's `_arguments` and `_describe` built-in functions +- **AND** support dynamic completion for change and spec IDs + +### Requirement: Dynamic Completions + +The completion system SHALL provide context-aware dynamic completions for project-specific values. + +#### Scenario: Completing change IDs + +- **WHEN** completing arguments for commands that accept change names (show, validate, archive) +- **THEN** discover active changes from `openspec/changes/` directory +- **AND** exclude archived changes in `openspec/changes/archive/` +- **AND** return change IDs as completion suggestions +- **AND** only provide suggestions when inside an OpenSpec-enabled project + +#### Scenario: Completing spec IDs + +- **WHEN** completing arguments for commands that accept spec names (show, validate) +- **THEN** discover specs from `openspec/specs/` directory +- **AND** return spec IDs as completion suggestions +- **AND** only provide suggestions when inside an OpenSpec-enabled project + +#### Scenario: Completion caching + +- **WHEN** dynamic completions are requested +- **THEN** cache discovered change and spec IDs for 2 seconds +- **AND** reuse cached values for subsequent requests within cache window +- **AND** automatically refresh cache after expiration + +#### Scenario: Project detection + +- **WHEN** user requests completions outside an OpenSpec project +- **THEN** skip dynamic change/spec ID completions +- **AND** only suggest static commands and flags + +### Requirement: Installation Automation + +The completion command SHALL automatically install completion scripts into shell configuration files. + +#### Scenario: Installing for Oh My Zsh + +- **WHEN** user executes `openspec completion install zsh` +- **THEN** detect if Oh My Zsh is installed by checking for `$ZSH` environment variable or `~/.oh-my-zsh/` directory +- **AND** create custom completions directory at `~/.oh-my-zsh/custom/completions/` if it doesn't exist +- **AND** write completion script to `~/.oh-my-zsh/custom/completions/_openspec` +- **AND** ensure `~/.oh-my-zsh/custom/completions` is in `$fpath` by updating `~/.zshrc` if needed +- **AND** display success message with instruction to run `exec zsh` or restart terminal + +#### Scenario: Installing for standard Zsh + +- **WHEN** user executes `openspec completion install zsh` and Oh My Zsh is not detected +- **THEN** create completions directory at `~/.zsh/completions/` if it doesn't exist +- **AND** write completion script to `~/.zsh/completions/_openspec` +- **AND** add `fpath=(~/.zsh/completions $fpath)` to `~/.zshrc` if not already present +- **AND** add `autoload -Uz compinit && compinit` to `~/.zshrc` if not already present +- **AND** display success message with instruction to run `exec zsh` or restart terminal + +#### Scenario: Auto-detecting Zsh for installation + +- **WHEN** user executes `openspec completion install` without specifying a shell +- **THEN** detect current shell using shell detection logic +- **AND** install completion if detected shell is Zsh +- **AND** throw error if detected shell is not Zsh +- **AND** display which shell was detected + +#### Scenario: Already installed + +- **WHEN** completion is already installed for the target shell +- **THEN** display message indicating completion is already installed +- **AND** offer to reinstall/update by overwriting existing files +- **AND** exit with code 0 + +### Requirement: Uninstallation + +The completion command SHALL remove installed completion scripts and configuration. + +#### Scenario: Uninstalling Oh My Zsh completion + +- **WHEN** user executes `openspec completion uninstall zsh` +- **THEN** remove `~/.oh-my-zsh/custom/completions/_openspec` if Oh My Zsh is detected +- **AND** remove `~/.zsh/completions/_openspec` if standard Zsh setup is detected +- **AND** optionally remove fpath modifications from `~/.zshrc` (with confirmation) +- **AND** display success message + +#### Scenario: Auto-detecting Zsh for uninstallation + +- **WHEN** user executes `openspec completion uninstall` without specifying a shell +- **THEN** detect current shell and uninstall completion if shell is Zsh +- **AND** throw error if detected shell is not Zsh + +#### Scenario: Not installed + +- **WHEN** attempting to uninstall completion that isn't installed +- **THEN** display message indicating completion is not installed +- **AND** exit with code 0 + +### Requirement: Architecture Patterns + +The completion implementation SHALL follow clean architecture principles with TypeScript best practices. + +#### Scenario: Shell-specific generators + +- **WHEN** implementing completion generators +- **THEN** create `ZshCompletionGenerator` class for Zsh +- **AND** implement a common `CompletionGenerator` interface with methods: + - `generate(): string` - Returns complete shell script + - `getInstallPath(): string` - Returns target installation path + - `getConfigFile(): string` - Returns shell configuration file path +- **AND** design interface to be extensible for future shells (bash, fish, powershell) + +#### Scenario: Dynamic completion providers + +- **WHEN** implementing dynamic completions +- **THEN** create a `CompletionProvider` class that encapsulates project discovery logic +- **AND** implement methods: + - `getChangeIds(): Promise` - Discovers active change IDs + - `getSpecIds(): Promise` - Discovers spec IDs + - `isOpenSpecProject(): boolean` - Checks if current directory is OpenSpec-enabled +- **AND** implement caching with 2-second TTL using class properties + +#### Scenario: Command registry + +- **WHEN** defining completable commands +- **THEN** create a centralized `CommandDefinition` type with properties: + - `name: string` - Command name + - `description: string` - Help text + - `flags: FlagDefinition[]` - Available flags + - `acceptsChangeId: boolean` - Whether command takes change ID argument + - `acceptsSpecId: boolean` - Whether command takes spec ID argument + - `subcommands?: CommandDefinition[]` - Nested subcommands +- **AND** export a `COMMAND_REGISTRY` constant with all command definitions +- **AND** generators consume this registry to ensure consistency + +#### Scenario: Type-safe shell detection + +- **WHEN** implementing shell detection +- **THEN** define a `SupportedShell` type as literal type: `'zsh'` +- **AND** implement `detectShell()` function that returns 'zsh' or throws error +- **AND** design type to be extensible (e.g., future: `'bash' | 'zsh' | 'fish' | 'powershell'`) + +### Requirement: Error Handling + +The completion command SHALL provide clear error messages for common failure scenarios. + +#### Scenario: Unsupported shell + +- **WHEN** user requests completion for unsupported shell (bash, fish, powershell, etc.) +- **THEN** display error message: "Shell '' is not supported yet. Currently supported: zsh" +- **AND** exit with code 1 + +#### Scenario: Permission errors during installation + +- **WHEN** installation fails due to file permission issues +- **THEN** display clear error message indicating permission problem +- **AND** suggest using appropriate permissions or alternative installation method +- **AND** exit with code 1 + +#### Scenario: Missing shell configuration directory + +- **WHEN** expected shell configuration directory doesn't exist +- **THEN** create the directory automatically (with user notification) +- **AND** proceed with installation + +#### Scenario: Shell not detected + +- **WHEN** `openspec completion install` cannot detect current shell or detects non-Zsh shell +- **THEN** display error: "Could not detect Zsh. Please specify explicitly: openspec completion install zsh" +- **AND** exit with code 1 + +### Requirement: Output Format + +The completion command SHALL provide machine-parseable and human-readable output. + +#### Scenario: Script generation output + +- **WHEN** generating completion script to stdout +- **THEN** output only the completion script content (no extra messages) +- **AND** allow redirection to files: `openspec completion zsh > /path/to/_openspec` + +#### Scenario: Installation success output + +- **WHEN** installation completes successfully +- **THEN** display formatted success message with: + - Checkmark indicator + - Installation location + - Next steps (shell reload instructions) +- **AND** use colors when terminal supports it (unless `--no-color` is set) + +#### Scenario: Verbose installation output + +- **WHEN** user provides `--verbose` flag during installation +- **THEN** display detailed steps: + - Shell detection result + - Target file paths + - Configuration modifications + - File creation confirmations + +### Requirement: Testing Support + +The completion implementation SHALL be testable with unit and integration tests. + +#### Scenario: Mock shell environment + +- **WHEN** writing tests for shell detection +- **THEN** allow overriding `$SHELL` environment variable +- **AND** use dependency injection for file system operations + +#### Scenario: Generator output verification + +- **WHEN** testing completion generators +- **THEN** verify generated scripts contain expected patterns +- **AND** test that command registry is properly consumed +- **AND** ensure dynamic completion placeholders are present + +#### Scenario: Installation simulation + +- **WHEN** testing installation logic +- **THEN** use temporary test directories instead of actual home directories +- **AND** verify file creation without modifying real shell configurations +- **AND** test path resolution logic independently + diff --git a/package.json b/package.json index e68a1634..a3e9d945 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "files": [ "dist", "bin", + "scripts", "!dist/**/*.test.js", "!dist/**/__tests__", "!dist/**/*.map" @@ -44,8 +45,10 @@ "test:watch": "vitest", "test:ui": "vitest --ui", "test:coverage": "vitest --coverage", + "test:postinstall": "node scripts/postinstall.js", "prepare": "pnpm run build", "prepublishOnly": "pnpm run build", + "postinstall": "node scripts/postinstall.js", "check:pack-version": "node scripts/pack-version-check.mjs", "release": "pnpm run release:ci", "release:ci": "pnpm run check:pack-version && pnpm exec changeset publish", diff --git a/scripts/postinstall.js b/scripts/postinstall.js new file mode 100644 index 00000000..43b92d76 --- /dev/null +++ b/scripts/postinstall.js @@ -0,0 +1,143 @@ +#!/usr/bin/env node + +/** + * Postinstall script for auto-installing shell completions + * + * This script runs automatically after npm install unless: + * - CI=true environment variable is set + * - OPENSPEC_NO_COMPLETIONS=1 environment variable is set + * - dist/ directory doesn't exist (dev setup scenario) + * + * The script never fails npm install - all errors are caught and handled gracefully. + */ + +import { promises as fs } from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +/** + * Check if we should skip installation + */ +function shouldSkipInstallation() { + // Skip in CI environments + if (process.env.CI === 'true' || process.env.CI === '1') { + return { skip: true, reason: 'CI environment detected' }; + } + + // Skip if user opted out + if (process.env.OPENSPEC_NO_COMPLETIONS === '1') { + return { skip: true, reason: 'OPENSPEC_NO_COMPLETIONS=1 set' }; + } + + return { skip: false }; +} + +/** + * Check if dist/ directory exists + */ +async function distExists() { + const distPath = path.join(__dirname, '..', 'dist'); + try { + const stat = await fs.stat(distPath); + return stat.isDirectory(); + } catch { + return false; + } +} + +/** + * Detect the user's shell + */ +async function detectShell() { + try { + const { detectShell } = await import('../dist/utils/shell-detection.js'); + return detectShell(); + } catch (error) { + // Fail silently if detection module doesn't exist + return undefined; + } +} + +/** + * Install completions for the detected shell + */ +async function installCompletions(shell) { + try { + const { CompletionFactory } = await import('../dist/core/completions/factory.js'); + const { COMMAND_REGISTRY } = await import('../dist/core/completions/command-registry.js'); + + // Check if shell is supported + if (!CompletionFactory.isSupported(shell)) { + console.log(`\nTip: Run 'openspec completion install' for shell completions`); + return; + } + + // Generate completion script + const generator = CompletionFactory.createGenerator(shell); + const script = generator.generate(COMMAND_REGISTRY); + + // Install completion script + const installer = CompletionFactory.createInstaller(shell); + const result = await installer.install(script); + + if (result.success) { + // Show success message based on installation type + if (result.isOhMyZsh) { + console.log(`✓ Shell completions installed`); + console.log(` Restart shell: exec zsh`); + } else { + console.log(`✓ Shell completions installed to ~/.zsh/completions/`); + console.log(` Add to ~/.zshrc: fpath=(~/.zsh/completions $fpath)`); + console.log(` Then: exec zsh`); + } + } else { + // Installation failed, show tip for manual install + console.log(`\nTip: Run 'openspec completion install' for shell completions`); + } + } catch (error) { + // Fail gracefully - show tip for manual install + console.log(`\nTip: Run 'openspec completion install' for shell completions`); + } +} + +/** + * Main function + */ +async function main() { + try { + // Check if we should skip + const skipCheck = shouldSkipInstallation(); + if (skipCheck.skip) { + // Silent skip - no output + return; + } + + // Check if dist/ exists (skip silently if not - expected during dev setup) + if (!(await distExists())) { + return; + } + + // Detect shell + const shell = await detectShell(); + if (!shell) { + console.log(`\nTip: Run 'openspec completion install' for shell completions`); + return; + } + + // Install completions + await installCompletions(shell); + } catch (error) { + // Fail gracefully - never break npm install + // Show tip for manual install + console.log(`\nTip: Run 'openspec completion install' for shell completions`); + } +} + +// Run main and handle any unhandled errors +main().catch(() => { + // Silent failure - never break npm install + process.exit(0); +}); diff --git a/scripts/test-postinstall.sh b/scripts/test-postinstall.sh new file mode 100755 index 00000000..97b0ab5b --- /dev/null +++ b/scripts/test-postinstall.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# Test script for postinstall.js +# Tests different scenarios: normal install, CI, opt-out + +set -e + +echo "======================================" +echo "Testing OpenSpec Postinstall Script" +echo "======================================" +echo "" + +# Save original environment +ORIGINAL_CI="${CI:-}" +ORIGINAL_OPENSPEC_NO_COMPLETIONS="${OPENSPEC_NO_COMPLETIONS:-}" + +# Test 1: Normal install +echo "Test 1: Normal install (should attempt to install completions)" +echo "--------------------------------------" +unset CI +unset OPENSPEC_NO_COMPLETIONS +node scripts/postinstall.js +echo "" + +# Test 2: CI environment (should skip silently) +echo "Test 2: CI=true (should skip silently)" +echo "--------------------------------------" +export CI=true +node scripts/postinstall.js +echo "[No output expected - skipped due to CI]" +echo "" + +# Test 3: Opt-out flag (should skip silently) +echo "Test 3: OPENSPEC_NO_COMPLETIONS=1 (should skip silently)" +echo "--------------------------------------" +unset CI +export OPENSPEC_NO_COMPLETIONS=1 +node scripts/postinstall.js +echo "[No output expected - skipped due to opt-out]" +echo "" + +# Restore original environment +if [ -n "$ORIGINAL_CI" ]; then + export CI="$ORIGINAL_CI" +else + unset CI +fi + +if [ -n "$ORIGINAL_OPENSPEC_NO_COMPLETIONS" ]; then + export OPENSPEC_NO_COMPLETIONS="$ORIGINAL_OPENSPEC_NO_COMPLETIONS" +else + unset OPENSPEC_NO_COMPLETIONS +fi + +echo "======================================" +echo "All tests completed successfully!" +echo "======================================" diff --git a/src/cli/index.ts b/src/cli/index.ts index 780ba1d8..cc450682 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -13,6 +13,7 @@ import { registerSpecCommand } from '../commands/spec.js'; import { ChangeCommand } from '../commands/change.js'; import { ValidateCommand } from '../commands/validate.js'; import { ShowCommand } from '../commands/show.js'; +import { CompletionCommand } from '../commands/completion.js'; const program = new Command(); const require = createRequire(import.meta.url); @@ -250,4 +251,52 @@ program } }); +// Completion command with subcommands +const completionCmd = program + .command('completion') + .description('Manage shell completions for OpenSpec CLI'); + +completionCmd + .command('generate [shell]') + .description('Generate completion script for a shell (outputs to stdout)') + .action(async (shell?: string) => { + try { + const completionCommand = new CompletionCommand(); + await completionCommand.generate({ shell }); + } catch (error) { + console.log(); + ora().fail(`Error: ${(error as Error).message}`); + process.exit(1); + } + }); + +completionCmd + .command('install [shell]') + .description('Install completion script for a shell') + .option('--verbose', 'Show detailed installation output') + .action(async (shell?: string, options?: { verbose?: boolean }) => { + try { + const completionCommand = new CompletionCommand(); + await completionCommand.install({ shell, verbose: options?.verbose }); + } catch (error) { + console.log(); + ora().fail(`Error: ${(error as Error).message}`); + process.exit(1); + } + }); + +completionCmd + .command('uninstall [shell]') + .description('Uninstall completion script for a shell') + .action(async (shell?: string) => { + try { + const completionCommand = new CompletionCommand(); + await completionCommand.uninstall({ shell }); + } catch (error) { + console.log(); + ora().fail(`Error: ${(error as Error).message}`); + process.exit(1); + } + }); + program.parse(); diff --git a/src/commands/completion.ts b/src/commands/completion.ts new file mode 100644 index 00000000..8ed90b1c --- /dev/null +++ b/src/commands/completion.ts @@ -0,0 +1,209 @@ +import ora from 'ora'; +import { CompletionFactory } from '../core/completions/factory.js'; +import { COMMAND_REGISTRY } from '../core/completions/command-registry.js'; +import { detectShell, SupportedShell } from '../utils/shell-detection.js'; + +interface GenerateOptions { + shell?: string; +} + +interface InstallOptions { + shell?: string; + verbose?: boolean; +} + +interface UninstallOptions { + shell?: string; +} + +/** + * Command for managing shell completions for OpenSpec CLI + */ +export class CompletionCommand { + /** + * Generate completion script and output to stdout + * + * @param options - Options for generation (shell type) + */ + async generate(options: GenerateOptions = {}): Promise { + const shell = this.normalizeShell(options.shell); + + if (!shell) { + const detected = detectShell(); + if (detected && CompletionFactory.isSupported(detected)) { + // Use detected shell + await this.generateForShell(detected); + return; + } + + // No shell specified and cannot auto-detect + console.error('Error: Could not auto-detect shell. Please specify shell explicitly.'); + console.error('Usage: openspec completion generate '); + console.error(`Currently supported: ${CompletionFactory.getSupportedShells().join(', ')}`); + process.exitCode = 1; + return; + } + + if (!CompletionFactory.isSupported(shell)) { + console.error(`Error: Shell '${shell}' is not supported yet. Currently supported: ${CompletionFactory.getSupportedShells().join(', ')}`); + process.exitCode = 1; + return; + } + + await this.generateForShell(shell); + } + + /** + * Install completion script to the appropriate location + * + * @param options - Options for installation (shell type, verbose output) + */ + async install(options: InstallOptions = {}): Promise { + const shell = this.normalizeShell(options.shell); + + if (!shell) { + const detected = detectShell(); + if (detected && CompletionFactory.isSupported(detected)) { + // Use detected shell + await this.installForShell(detected, options.verbose || false); + return; + } + + // No shell specified and cannot auto-detect + console.error('Error: Could not auto-detect shell. Please specify shell explicitly.'); + console.error('Usage: openspec completion install [shell]'); + console.error(`Currently supported: ${CompletionFactory.getSupportedShells().join(', ')}`); + process.exitCode = 1; + return; + } + + if (!CompletionFactory.isSupported(shell)) { + console.error(`Error: Shell '${shell}' is not supported yet. Currently supported: ${CompletionFactory.getSupportedShells().join(', ')}`); + process.exitCode = 1; + return; + } + + await this.installForShell(shell, options.verbose || false); + } + + /** + * Uninstall completion script from the installation location + * + * @param options - Options for uninstallation (shell type) + */ + async uninstall(options: UninstallOptions = {}): Promise { + const shell = this.normalizeShell(options.shell); + + if (!shell) { + const detected = detectShell(); + if (detected && CompletionFactory.isSupported(detected)) { + // Use detected shell + await this.uninstallForShell(detected); + return; + } + + // No shell specified and cannot auto-detect + console.error('Error: Could not auto-detect shell. Please specify shell explicitly.'); + console.error('Usage: openspec completion uninstall [shell]'); + console.error(`Currently supported: ${CompletionFactory.getSupportedShells().join(', ')}`); + process.exitCode = 1; + return; + } + + if (!CompletionFactory.isSupported(shell)) { + console.error(`Error: Shell '${shell}' is not supported yet. Currently supported: ${CompletionFactory.getSupportedShells().join(', ')}`); + process.exitCode = 1; + return; + } + + await this.uninstallForShell(shell); + } + + /** + * Generate completion script for a specific shell + */ + private async generateForShell(shell: SupportedShell): Promise { + const generator = CompletionFactory.createGenerator(shell); + const script = generator.generate(COMMAND_REGISTRY); + console.log(script); + } + + /** + * Install completion script for a specific shell + */ + private async installForShell(shell: SupportedShell, verbose: boolean): Promise { + const generator = CompletionFactory.createGenerator(shell); + const installer = CompletionFactory.createInstaller(shell); + + const spinner = ora(`Installing ${shell} completion script...`).start(); + + try { + // Generate the completion script + const script = generator.generate(COMMAND_REGISTRY); + + // Install it + const result = await installer.install(script); + + spinner.stop(); + + if (result.success) { + console.log(`✓ ${result.message}`); + + if (verbose && result.installedPath) { + console.log(` Installed to: ${result.installedPath}`); + if (result.backupPath) { + console.log(` Backup created: ${result.backupPath}`); + } + } + + // Print instructions + if (result.instructions && result.instructions.length > 0) { + console.log(''); + for (const instruction of result.instructions) { + console.log(instruction); + } + } + } else { + console.error(`✗ ${result.message}`); + process.exitCode = 1; + } + } catch (error) { + spinner.stop(); + console.error(`✗ Failed to install completion script: ${error instanceof Error ? error.message : String(error)}`); + process.exitCode = 1; + } + } + + /** + * Uninstall completion script for a specific shell + */ + private async uninstallForShell(shell: SupportedShell): Promise { + const installer = CompletionFactory.createInstaller(shell); + + const spinner = ora(`Uninstalling ${shell} completion script...`).start(); + + try { + const result = await installer.uninstall(); + + spinner.stop(); + + if (result.success) { + console.log(`✓ ${result.message}`); + } else { + console.error(`✗ ${result.message}`); + process.exitCode = 1; + } + } catch (error) { + spinner.stop(); + console.error(`✗ Failed to uninstall completion script: ${error instanceof Error ? error.message : String(error)}`); + process.exitCode = 1; + } + } + + /** + * Normalize shell parameter to lowercase + */ + private normalizeShell(shell?: string): string | undefined { + return shell?.toLowerCase(); + } +} diff --git a/src/core/completions/command-registry.ts b/src/core/completions/command-registry.ts new file mode 100644 index 00000000..9466b005 --- /dev/null +++ b/src/core/completions/command-registry.ts @@ -0,0 +1,324 @@ +import { CommandDefinition } from './types.js'; + +/** + * Registry of all OpenSpec CLI commands with their flags and metadata. + * This registry is used to generate shell completion scripts. + */ +export const COMMAND_REGISTRY: CommandDefinition[] = [ + { + name: 'init', + description: 'Initialize OpenSpec in your project', + acceptsPositional: true, + positionalType: 'path', + flags: [ + { + name: 'tools', + description: 'Configure AI tools non-interactively (e.g., "all", "none", or comma-separated tool IDs)', + takesValue: true, + }, + ], + }, + { + name: 'update', + description: 'Update OpenSpec instruction files', + acceptsPositional: true, + positionalType: 'path', + flags: [], + }, + { + name: 'list', + description: 'List items (changes by default, or specs with --specs)', + flags: [ + { + name: 'specs', + description: 'List specs instead of changes', + }, + { + name: 'changes', + description: 'List changes explicitly (default)', + }, + ], + }, + { + name: 'view', + description: 'Display an interactive dashboard of specs and changes', + flags: [], + }, + { + name: 'validate', + description: 'Validate changes and specs', + acceptsPositional: true, + positionalType: 'change-or-spec-id', + flags: [ + { + name: 'all', + description: 'Validate all changes and specs', + }, + { + name: 'changes', + description: 'Validate all changes', + }, + { + name: 'specs', + description: 'Validate all specs', + }, + { + name: 'type', + description: 'Specify item type when ambiguous', + takesValue: true, + values: ['change', 'spec'], + }, + { + name: 'strict', + description: 'Enable strict validation mode', + }, + { + name: 'json', + description: 'Output validation results as JSON', + }, + { + name: 'concurrency', + description: 'Max concurrent validations (defaults to env OPENSPEC_CONCURRENCY or 6)', + takesValue: true, + }, + { + name: 'no-interactive', + description: 'Disable interactive prompts', + }, + ], + }, + { + name: 'show', + description: 'Show a change or spec', + acceptsPositional: true, + positionalType: 'change-or-spec-id', + flags: [ + { + name: 'json', + description: 'Output as JSON', + }, + { + name: 'type', + description: 'Specify item type when ambiguous', + takesValue: true, + values: ['change', 'spec'], + }, + { + name: 'no-interactive', + description: 'Disable interactive prompts', + }, + { + name: 'deltas-only', + description: 'Show only deltas (JSON only, change-specific)', + }, + { + name: 'requirements-only', + description: 'Alias for --deltas-only (deprecated, change-specific)', + }, + { + name: 'requirements', + description: 'Show only requirements, exclude scenarios (JSON only, spec-specific)', + }, + { + name: 'no-scenarios', + description: 'Exclude scenario content (JSON only, spec-specific)', + }, + { + name: 'requirement', + short: 'r', + description: 'Show specific requirement by ID (JSON only, spec-specific)', + takesValue: true, + }, + ], + }, + { + name: 'archive', + description: 'Archive a completed change and update main specs', + acceptsPositional: true, + positionalType: 'change-id', + flags: [ + { + name: 'yes', + short: 'y', + description: 'Skip confirmation prompts', + }, + { + name: 'skip-specs', + description: 'Skip spec update operations', + }, + { + name: 'no-validate', + description: 'Skip validation (not recommended)', + }, + ], + }, + { + name: 'change', + description: 'Manage OpenSpec change proposals (deprecated)', + flags: [], + subcommands: [ + { + name: 'show', + description: 'Show a change proposal', + acceptsPositional: true, + positionalType: 'change-id', + flags: [ + { + name: 'json', + description: 'Output as JSON', + }, + { + name: 'deltas-only', + description: 'Show only deltas (JSON only)', + }, + { + name: 'requirements-only', + description: 'Alias for --deltas-only (deprecated)', + }, + { + name: 'no-interactive', + description: 'Disable interactive prompts', + }, + ], + }, + { + name: 'list', + description: 'List all active changes (deprecated)', + flags: [ + { + name: 'json', + description: 'Output as JSON', + }, + { + name: 'long', + description: 'Show id and title with counts', + }, + ], + }, + { + name: 'validate', + description: 'Validate a change proposal', + acceptsPositional: true, + positionalType: 'change-id', + flags: [ + { + name: 'strict', + description: 'Enable strict validation mode', + }, + { + name: 'json', + description: 'Output validation report as JSON', + }, + { + name: 'no-interactive', + description: 'Disable interactive prompts', + }, + ], + }, + ], + }, + { + name: 'spec', + description: 'Manage OpenSpec specifications', + flags: [], + subcommands: [ + { + name: 'show', + description: 'Show a specification', + acceptsPositional: true, + positionalType: 'spec-id', + flags: [ + { + name: 'json', + description: 'Output as JSON', + }, + { + name: 'requirements', + description: 'Show only requirements, exclude scenarios (JSON only)', + }, + { + name: 'no-scenarios', + description: 'Exclude scenario content (JSON only)', + }, + { + name: 'requirement', + short: 'r', + description: 'Show specific requirement by ID (JSON only)', + takesValue: true, + }, + { + name: 'no-interactive', + description: 'Disable interactive prompts', + }, + ], + }, + { + name: 'list', + description: 'List all specifications', + flags: [ + { + name: 'json', + description: 'Output as JSON', + }, + { + name: 'long', + description: 'Show id and title with counts', + }, + ], + }, + { + name: 'validate', + description: 'Validate a specification', + acceptsPositional: true, + positionalType: 'spec-id', + flags: [ + { + name: 'strict', + description: 'Enable strict validation mode', + }, + { + name: 'json', + description: 'Output validation report as JSON', + }, + { + name: 'no-interactive', + description: 'Disable interactive prompts', + }, + ], + }, + ], + }, + { + name: 'completion', + description: 'Manage shell completions for OpenSpec CLI', + flags: [], + subcommands: [ + { + name: 'generate', + description: 'Generate completion script for a shell (outputs to stdout)', + acceptsPositional: true, + positionalType: 'shell', + flags: [], + }, + { + name: 'install', + description: 'Install completion script for a shell', + acceptsPositional: true, + positionalType: 'shell', + flags: [ + { + name: 'verbose', + description: 'Show detailed installation output', + }, + ], + }, + { + name: 'uninstall', + description: 'Uninstall completion script for a shell', + acceptsPositional: true, + positionalType: 'shell', + flags: [], + }, + ], + }, +]; diff --git a/src/core/completions/completion-provider.ts b/src/core/completions/completion-provider.ts new file mode 100644 index 00000000..b798ffe5 --- /dev/null +++ b/src/core/completions/completion-provider.ts @@ -0,0 +1,128 @@ +import { getActiveChangeIds, getSpecIds } from '../../utils/item-discovery.js'; + +/** + * Cache entry for completion data + */ +interface CacheEntry { + data: T; + timestamp: number; +} + +/** + * Provides dynamic completion suggestions for OpenSpec items (changes and specs). + * Implements a 2-second cache to avoid excessive file system operations during + * tab completion. + */ +export class CompletionProvider { + private readonly cacheTTL: number; + private changeCache: CacheEntry | null = null; + private specCache: CacheEntry | null = null; + + /** + * Creates a new completion provider + * + * @param cacheTTLMs - Cache time-to-live in milliseconds (default: 2000ms) + * @param projectRoot - Project root directory (default: process.cwd()) + */ + constructor( + private readonly cacheTTLMs: number = 2000, + private readonly projectRoot: string = process.cwd() + ) { + this.cacheTTL = cacheTTLMs; + } + + /** + * Get all active change IDs for completion + * + * @returns Array of change IDs + */ + async getChangeIds(): Promise { + const now = Date.now(); + + // Check if cache is valid + if (this.changeCache && now - this.changeCache.timestamp < this.cacheTTL) { + return this.changeCache.data; + } + + // Fetch fresh data + const changeIds = await getActiveChangeIds(this.projectRoot); + + // Update cache + this.changeCache = { + data: changeIds, + timestamp: now, + }; + + return changeIds; + } + + /** + * Get all spec IDs for completion + * + * @returns Array of spec IDs + */ + async getSpecIds(): Promise { + const now = Date.now(); + + // Check if cache is valid + if (this.specCache && now - this.specCache.timestamp < this.cacheTTL) { + return this.specCache.data; + } + + // Fetch fresh data + const specIds = await getSpecIds(this.projectRoot); + + // Update cache + this.specCache = { + data: specIds, + timestamp: now, + }; + + return specIds; + } + + /** + * Get both change and spec IDs for completion + * + * @returns Object with changeIds and specIds arrays + */ + async getAllIds(): Promise<{ changeIds: string[]; specIds: string[] }> { + const [changeIds, specIds] = await Promise.all([ + this.getChangeIds(), + this.getSpecIds(), + ]); + + return { changeIds, specIds }; + } + + /** + * Clear all cached data + */ + clearCache(): void { + this.changeCache = null; + this.specCache = null; + } + + /** + * Get cache statistics for debugging + * + * @returns Cache status information + */ + getCacheStats(): { + changeCache: { valid: boolean; age?: number }; + specCache: { valid: boolean; age?: number }; + } { + const now = Date.now(); + + return { + changeCache: { + valid: this.changeCache !== null && now - this.changeCache.timestamp < this.cacheTTL, + age: this.changeCache ? now - this.changeCache.timestamp : undefined, + }, + specCache: { + valid: this.specCache !== null && now - this.specCache.timestamp < this.cacheTTL, + age: this.specCache ? now - this.specCache.timestamp : undefined, + }, + }; + } +} diff --git a/src/core/completions/factory.ts b/src/core/completions/factory.ts new file mode 100644 index 00000000..add3d497 --- /dev/null +++ b/src/core/completions/factory.ts @@ -0,0 +1,72 @@ +import { CompletionGenerator } from './types.js'; +import { ZshGenerator } from './generators/zsh-generator.js'; +import { ZshInstaller, InstallationResult } from './installers/zsh-installer.js'; +import { SupportedShell } from '../../utils/shell-detection.js'; + +/** + * Interface for completion installers + */ +export interface CompletionInstaller { + install(script: string): Promise; + uninstall(): Promise<{ success: boolean; message: string }>; +} + +// Re-export InstallationResult for convenience +export type { InstallationResult }; + +/** + * Factory for creating completion generators and installers + * This design makes it easy to add support for additional shells + */ +export class CompletionFactory { + /** + * Create a completion generator for the specified shell + * + * @param shell - The target shell + * @returns CompletionGenerator instance + * @throws Error if shell is not supported + */ + static createGenerator(shell: SupportedShell): CompletionGenerator { + switch (shell) { + case 'zsh': + return new ZshGenerator(); + default: + throw new Error(`Unsupported shell: ${shell}`); + } + } + + /** + * Create a completion installer for the specified shell + * + * @param shell - The target shell + * @returns CompletionInstaller instance + * @throws Error if shell is not supported + */ + static createInstaller(shell: SupportedShell): CompletionInstaller { + switch (shell) { + case 'zsh': + return new ZshInstaller(); + default: + throw new Error(`Unsupported shell: ${shell}`); + } + } + + /** + * Check if a shell is supported + * + * @param shell - The shell to check + * @returns true if the shell is supported + */ + static isSupported(shell: string): shell is SupportedShell { + return shell === 'zsh'; + } + + /** + * Get list of all supported shells + * + * @returns Array of supported shell names + */ + static getSupportedShells(): SupportedShell[] { + return ['zsh']; + } +} diff --git a/src/core/completions/generators/zsh-generator.ts b/src/core/completions/generators/zsh-generator.ts new file mode 100644 index 00000000..c413266a --- /dev/null +++ b/src/core/completions/generators/zsh-generator.ts @@ -0,0 +1,325 @@ +import { CompletionGenerator, CommandDefinition, FlagDefinition } from '../types.js'; + +/** + * Generates Zsh completion scripts for the OpenSpec CLI. + * Follows Zsh completion system conventions using the _openspec function. + */ +export class ZshGenerator implements CompletionGenerator { + readonly shell = 'zsh' as const; + + /** + * Generate a Zsh completion script + * + * @param commands - Command definitions to generate completions for + * @returns Zsh completion script as a string + */ + generate(commands: CommandDefinition[]): string { + const script: string[] = []; + + // Header comment + script.push('#compdef openspec'); + script.push(''); + script.push('# Zsh completion script for OpenSpec CLI'); + script.push('# Auto-generated - do not edit manually'); + script.push(''); + + // Main completion function + script.push('_openspec() {'); + script.push(' local context state line'); + script.push(' typeset -A opt_args'); + script.push(''); + + // Generate main command argument specification + script.push(' local -a commands'); + script.push(' commands=('); + for (const cmd of commands) { + const escapedDesc = this.escapeDescription(cmd.description); + script.push(` '${cmd.name}:${escapedDesc}'`); + } + script.push(' )'); + script.push(''); + + // Main _arguments call + script.push(' _arguments -C \\'); + script.push(' "1: :->command" \\'); + script.push(' "*::arg:->args"'); + script.push(''); + + // Command dispatch logic + script.push(' case $state in'); + script.push(' command)'); + script.push(' _describe "openspec command" commands'); + script.push(' ;;'); + script.push(' args)'); + script.push(' case $line[1] in'); + + // Generate completion for each command + for (const cmd of commands) { + script.push(` ${cmd.name})`); + script.push(` _openspec_${this.sanitizeFunctionName(cmd.name)}`); + script.push(' ;;'); + } + + script.push(' esac'); + script.push(' ;;'); + script.push(' esac'); + script.push('}'); + script.push(''); + + // Generate individual command completion functions + for (const cmd of commands) { + script.push(...this.generateCommandFunction(cmd)); + script.push(''); + } + + // Add dynamic completion helper functions + script.push(...this.generateDynamicCompletionHelpers()); + + // Register the completion function + script.push('_openspec "$@"'); + script.push(''); + + return script.join('\n'); + } + + /** + * Generate dynamic completion helper functions for change and spec IDs + */ + private generateDynamicCompletionHelpers(): string[] { + const lines: string[] = []; + + // Helper function for completing change IDs + lines.push('# Dynamic completion helpers'); + lines.push('_openspec_complete_changes() {'); + lines.push(' local -a changes'); + lines.push(' # Use openspec list to get available changes'); + lines.push(' changes=(${(f)"$(openspec list --changes 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"})'); + lines.push(' _describe "change" changes'); + lines.push('}'); + lines.push(''); + + // Helper function for completing spec IDs + lines.push('_openspec_complete_specs() {'); + lines.push(' local -a specs'); + lines.push(' # Use openspec list to get available specs'); + lines.push(' specs=(${(f)"$(openspec list --specs 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"})'); + lines.push(' _describe "spec" specs'); + lines.push('}'); + lines.push(''); + + // Helper function for completing both changes and specs + lines.push('_openspec_complete_items() {'); + lines.push(' local -a items'); + lines.push(' # Get both changes and specs'); + lines.push(' items=('); + lines.push(' ${(f)"$(openspec list --changes 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"} \\'); + lines.push(' ${(f)"$(openspec list --specs 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"}'); + lines.push(' )'); + lines.push(' _describe "item" items'); + lines.push('}'); + lines.push(''); + + return lines; + } + + /** + * Generate completion function for a specific command + */ + private generateCommandFunction(cmd: CommandDefinition): string[] { + const funcName = `_openspec_${this.sanitizeFunctionName(cmd.name)}`; + const lines: string[] = []; + + lines.push(`${funcName}() {`); + + // If command has subcommands, handle them + if (cmd.subcommands && cmd.subcommands.length > 0) { + lines.push(' local context state line'); + lines.push(' typeset -A opt_args'); + lines.push(''); + lines.push(' local -a subcommands'); + lines.push(' subcommands=('); + + for (const subcmd of cmd.subcommands) { + const escapedDesc = this.escapeDescription(subcmd.description); + lines.push(` '${subcmd.name}:${escapedDesc}'`); + } + + lines.push(' )'); + lines.push(''); + lines.push(' _arguments -C \\'); + + // Add command flags + for (const flag of cmd.flags) { + lines.push(' ' + this.generateFlagSpec(flag) + ' \\'); + } + + lines.push(' "1: :->subcommand" \\'); + lines.push(' "*::arg:->args"'); + lines.push(''); + lines.push(' case $state in'); + lines.push(' subcommand)'); + lines.push(' _describe "subcommand" subcommands'); + lines.push(' ;;'); + lines.push(' args)'); + lines.push(' case $line[1] in'); + + for (const subcmd of cmd.subcommands) { + lines.push(` ${subcmd.name})`); + lines.push(` _openspec_${this.sanitizeFunctionName(cmd.name)}_${this.sanitizeFunctionName(subcmd.name)}`); + lines.push(' ;;'); + } + + lines.push(' esac'); + lines.push(' ;;'); + lines.push(' esac'); + } else { + // Command without subcommands + lines.push(' _arguments \\'); + + // Add flags + for (const flag of cmd.flags) { + lines.push(' ' + this.generateFlagSpec(flag) + ' \\'); + } + + // Add positional argument completion + if (cmd.acceptsPositional) { + const positionalSpec = this.generatePositionalSpec(cmd.positionalType); + lines.push(' ' + positionalSpec); + } else { + // Remove trailing backslash from last flag + if (lines[lines.length - 1].endsWith(' \\')) { + lines[lines.length - 1] = lines[lines.length - 1].slice(0, -2); + } + } + } + + lines.push('}'); + + // Generate subcommand functions if they exist + if (cmd.subcommands) { + for (const subcmd of cmd.subcommands) { + lines.push(''); + lines.push(...this.generateSubcommandFunction(cmd.name, subcmd)); + } + } + + return lines; + } + + /** + * Generate completion function for a subcommand + */ + private generateSubcommandFunction(parentName: string, subcmd: CommandDefinition): string[] { + const funcName = `_openspec_${this.sanitizeFunctionName(parentName)}_${this.sanitizeFunctionName(subcmd.name)}`; + const lines: string[] = []; + + lines.push(`${funcName}() {`); + lines.push(' _arguments \\'); + + // Add flags + for (const flag of subcmd.flags) { + lines.push(' ' + this.generateFlagSpec(flag) + ' \\'); + } + + // Add positional argument completion + if (subcmd.acceptsPositional) { + const positionalSpec = this.generatePositionalSpec(subcmd.positionalType); + lines.push(' ' + positionalSpec); + } else { + // Remove trailing backslash from last flag + if (lines[lines.length - 1].endsWith(' \\')) { + lines[lines.length - 1] = lines[lines.length - 1].slice(0, -2); + } + } + + lines.push('}'); + + return lines; + } + + /** + * Generate flag specification for _arguments + */ + private generateFlagSpec(flag: FlagDefinition): string { + const parts: string[] = []; + + // Handle mutually exclusive short and long forms + if (flag.short) { + parts.push(`'(-${flag.short} --${flag.name})'{-${flag.short},--${flag.name}}'`); + } else { + parts.push(`'--${flag.name}`); + } + + // Add description + const escapedDesc = this.escapeDescription(flag.description); + parts.push(`[${escapedDesc}]`); + + // Add value completion if flag takes a value + if (flag.takesValue) { + if (flag.values && flag.values.length > 0) { + // Provide specific value completions + const valueList = flag.values.map(v => this.escapeValue(v)).join(' '); + parts.push(`:value:(${valueList})`); + } else { + // Generic value placeholder + parts.push(':value:'); + } + } + + // Close the quote + if (!flag.short) { + parts.push("'"); + } + + return parts.join(''); + } + + /** + * Generate positional argument specification + */ + private generatePositionalSpec(positionalType?: string): string { + switch (positionalType) { + case 'change-id': + return "'*: :_openspec_complete_changes'"; + case 'spec-id': + return "'*: :_openspec_complete_specs'"; + case 'change-or-spec-id': + return "'*: :_openspec_complete_items'"; + case 'path': + return "'*:path:_files'"; + case 'shell': + return "'*:shell:(zsh)'"; + default: + return "'*: :_default'"; + } + } + + /** + * Escape special characters in descriptions + */ + private escapeDescription(desc: string): string { + return desc + .replace(/\\/g, '\\\\') + .replace(/'/g, "\\'") + .replace(/\[/g, '\\[') + .replace(/]/g, '\\]') + .replace(/:/g, '\\:'); + } + + /** + * Escape special characters in values + */ + private escapeValue(value: string): string { + return value + .replace(/\\/g, '\\\\') + .replace(/'/g, "\\'") + .replace(/ /g, '\\ '); + } + + /** + * Sanitize command names for use in function names + */ + private sanitizeFunctionName(name: string): string { + return name.replace(/-/g, '_'); + } +} diff --git a/src/core/completions/installers/zsh-installer.ts b/src/core/completions/installers/zsh-installer.ts new file mode 100644 index 00000000..ef0ad38c --- /dev/null +++ b/src/core/completions/installers/zsh-installer.ts @@ -0,0 +1,235 @@ +import { promises as fs } from 'fs'; +import path from 'path'; +import os from 'os'; + +/** + * Installation result information + */ +export interface InstallationResult { + success: boolean; + installedPath?: string; + backupPath?: string; + isOhMyZsh: boolean; + message: string; + instructions?: string[]; +} + +/** + * Installer for Zsh completion scripts. + * Supports both Oh My Zsh and standard Zsh configurations. + */ +export class ZshInstaller { + private readonly homeDir: string; + + constructor(homeDir: string = os.homedir()) { + this.homeDir = homeDir; + } + + /** + * Check if Oh My Zsh is installed + * + * @returns true if Oh My Zsh directory exists + */ + async isOhMyZshInstalled(): Promise { + const ohMyZshPath = path.join(this.homeDir, '.oh-my-zsh'); + + try { + const stat = await fs.stat(ohMyZshPath); + return stat.isDirectory(); + } catch { + return false; + } + } + + /** + * Get the appropriate installation path for the completion script + * + * @returns Object with installation path and whether it's Oh My Zsh + */ + async getInstallationPath(): Promise<{ path: string; isOhMyZsh: boolean }> { + const isOhMyZsh = await this.isOhMyZshInstalled(); + + if (isOhMyZsh) { + // Oh My Zsh custom completions directory + return { + path: path.join(this.homeDir, '.oh-my-zsh', 'completions', '_openspec'), + isOhMyZsh: true, + }; + } else { + // Standard Zsh completions directory + return { + path: path.join(this.homeDir, '.zsh', 'completions', '_openspec'), + isOhMyZsh: false, + }; + } + } + + /** + * Backup an existing completion file if it exists + * + * @param targetPath - Path to the file to backup + * @returns Path to the backup file, or undefined if no backup was needed + */ + async backupExistingFile(targetPath: string): Promise { + try { + await fs.access(targetPath); + // File exists, create a backup + const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); + const backupPath = `${targetPath}.backup-${timestamp}`; + await fs.copyFile(targetPath, backupPath); + return backupPath; + } catch { + // File doesn't exist, no backup needed + return undefined; + } + } + + /** + * Install the completion script + * + * @param completionScript - The completion script content to install + * @returns Installation result with status and instructions + */ + async install(completionScript: string): Promise { + try { + const { path: targetPath, isOhMyZsh } = await this.getInstallationPath(); + + // Ensure the directory exists + const targetDir = path.dirname(targetPath); + await fs.mkdir(targetDir, { recursive: true }); + + // Backup existing file if present + const backupPath = await this.backupExistingFile(targetPath); + + // Write the completion script + await fs.writeFile(targetPath, completionScript, 'utf-8'); + + // Generate instructions + const instructions = this.generateInstructions(isOhMyZsh, targetPath); + + return { + success: true, + installedPath: targetPath, + backupPath, + isOhMyZsh, + message: isOhMyZsh + ? 'Completion script installed successfully for Oh My Zsh' + : 'Completion script installed successfully for Zsh', + instructions, + }; + } catch (error) { + return { + success: false, + isOhMyZsh: false, + message: `Failed to install completion script: ${error instanceof Error ? error.message : String(error)}`, + }; + } + } + + /** + * Generate user instructions for enabling completions + * + * @param isOhMyZsh - Whether Oh My Zsh is being used + * @param installedPath - Path where the script was installed + * @returns Array of instruction strings + */ + private generateInstructions(isOhMyZsh: boolean, installedPath: string): string[] { + if (isOhMyZsh) { + return [ + 'Completion script installed to Oh My Zsh completions directory.', + 'Restart your shell or run: exec zsh', + 'Completions should activate automatically.', + ]; + } else { + const completionsDir = path.dirname(installedPath); + const zshrcPath = path.join(this.homeDir, '.zshrc'); + + return [ + 'Completion script installed to ~/.zsh/completions/', + '', + 'To enable completions, add the following to your ~/.zshrc file:', + '', + ` # Add completions directory to fpath`, + ` fpath=(${completionsDir} $fpath)`, + '', + ' # Initialize completion system', + ' autoload -Uz compinit', + ' compinit', + '', + 'Then restart your shell or run: exec zsh', + '', + `Check if these lines already exist in ${zshrcPath} before adding.`, + ]; + } + } + + /** + * Uninstall the completion script + * + * @returns true if uninstalled successfully, false otherwise + */ + async uninstall(): Promise<{ success: boolean; message: string }> { + try { + const { path: targetPath } = await this.getInstallationPath(); + + try { + await fs.access(targetPath); + await fs.unlink(targetPath); + return { + success: true, + message: `Completion script removed from ${targetPath}`, + }; + } catch { + return { + success: false, + message: 'Completion script is not installed', + }; + } + } catch (error) { + return { + success: false, + message: `Failed to uninstall completion script: ${error instanceof Error ? error.message : String(error)}`, + }; + } + } + + /** + * Check if completion script is currently installed + * + * @returns true if the completion script exists + */ + async isInstalled(): Promise { + try { + const { path: targetPath } = await this.getInstallationPath(); + await fs.access(targetPath); + return true; + } catch { + return false; + } + } + + /** + * Get information about the current installation + * + * @returns Installation status information + */ + async getInstallationInfo(): Promise<{ + installed: boolean; + path?: string; + isOhMyZsh?: boolean; + }> { + const installed = await this.isInstalled(); + + if (!installed) { + return { installed: false }; + } + + const { path: targetPath, isOhMyZsh } = await this.getInstallationPath(); + + return { + installed: true, + path: targetPath, + isOhMyZsh, + }; + } +} diff --git a/src/core/completions/types.ts b/src/core/completions/types.ts new file mode 100644 index 00000000..fef908b6 --- /dev/null +++ b/src/core/completions/types.ts @@ -0,0 +1,90 @@ +import { SupportedShell } from '../../utils/shell-detection.js'; + +/** + * Definition of a command-line flag/option + */ +export interface FlagDefinition { + /** + * Flag name without dashes (e.g., "json", "strict", "no-interactive") + */ + name: string; + + /** + * Short flag name without dash (e.g., "y" for "-y") + */ + short?: string; + + /** + * Human-readable description of what the flag does + */ + description: string; + + /** + * Whether the flag takes an argument value + */ + takesValue?: boolean; + + /** + * Possible values for the flag (for completion suggestions) + */ + values?: string[]; +} + +/** + * Definition of a CLI command + */ +export interface CommandDefinition { + /** + * Command name (e.g., "init", "validate", "show") + */ + name: string; + + /** + * Human-readable description of the command + */ + description: string; + + /** + * Flags/options supported by this command + */ + flags: FlagDefinition[]; + + /** + * Subcommands (e.g., "change show", "spec validate") + */ + subcommands?: CommandDefinition[]; + + /** + * Whether this command accepts a positional argument (e.g., item name, path) + */ + acceptsPositional?: boolean; + + /** + * Type of positional argument for dynamic completion + * - 'change-id': Complete with active change IDs + * - 'spec-id': Complete with spec IDs + * - 'change-or-spec-id': Complete with both changes and specs + * - 'path': Complete with file paths + * - 'shell': Complete with supported shell names + * - undefined: No specific completion + */ + positionalType?: 'change-id' | 'spec-id' | 'change-or-spec-id' | 'path' | 'shell'; +} + +/** + * Interface for shell-specific completion script generators + */ +export interface CompletionGenerator { + /** + * The shell type this generator targets + */ + readonly shell: SupportedShell; + + /** + * Generate the completion script content + * + * @param commands - Command definitions to generate completions for + * @returns The shell-specific completion script as a string + */ + generate(commands: CommandDefinition[]): string; +} diff --git a/src/utils/shell-detection.ts b/src/utils/shell-detection.ts new file mode 100644 index 00000000..3415a77a --- /dev/null +++ b/src/utils/shell-detection.ts @@ -0,0 +1,48 @@ +/** + * Supported shell types for completion generation + */ +export type SupportedShell = 'zsh' | 'bash' | 'fish' | 'powershell'; + +/** + * Detects the current user's shell based on environment variables + * + * @returns The detected shell type, or undefined if the shell is not supported or cannot be detected + */ +export function detectShell(): SupportedShell | undefined { + // Try SHELL environment variable first (Unix-like systems) + const shellPath = process.env.SHELL; + + if (shellPath) { + const shellName = shellPath.toLowerCase(); + + if (shellName.includes('zsh')) { + return 'zsh'; + } + if (shellName.includes('bash')) { + return 'bash'; + } + if (shellName.includes('fish')) { + return 'fish'; + } + } + + // Check for PowerShell on Windows + // PSModulePath is a reliable PowerShell-specific environment variable + if (process.env.PSModulePath || process.platform === 'win32') { + // On Windows, check if we're in PowerShell or cmd + const comspec = process.env.COMSPEC?.toLowerCase(); + + // If PSModulePath exists, we're definitely in PowerShell + if (process.env.PSModulePath) { + return 'powershell'; + } + + // On Windows without PSModulePath, we might be in cmd.exe + // For now, we don't support cmd.exe, so return undefined + if (comspec?.includes('cmd.exe')) { + return undefined; + } + } + + return undefined; +} diff --git a/test/commands/completion.test.ts b/test/commands/completion.test.ts new file mode 100644 index 00000000..d474daf1 --- /dev/null +++ b/test/commands/completion.test.ts @@ -0,0 +1,269 @@ +import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'; +import { CompletionCommand } from '../../src/commands/completion.js'; +import * as shellDetection from '../../src/utils/shell-detection.js'; + +// Mock the shell detection module +vi.mock('../../src/utils/shell-detection.js', () => ({ + detectShell: vi.fn(), +})); + +// Mock the ZshInstaller +vi.mock('../../src/core/completions/installers/zsh-installer.js', () => ({ + ZshInstaller: vi.fn().mockImplementation(() => ({ + install: vi.fn().mockResolvedValue({ + success: true, + installedPath: '/home/user/.oh-my-zsh/completions/_openspec', + isOhMyZsh: true, + message: 'Completion script installed successfully for Oh My Zsh', + instructions: [ + 'Completion script installed to Oh My Zsh completions directory.', + 'Restart your shell or run: exec zsh', + 'Completions should activate automatically.', + ], + }), + uninstall: vi.fn().mockResolvedValue({ + success: true, + message: 'Completion script removed from /home/user/.oh-my-zsh/completions/_openspec', + }), + })), +})); + +describe('CompletionCommand', () => { + let command: CompletionCommand; + let consoleLogSpy: any; + let consoleErrorSpy: any; + + beforeEach(() => { + command = new CompletionCommand(); + consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); + consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); + process.exitCode = 0; + }); + + afterEach(() => { + consoleLogSpy.mockRestore(); + consoleErrorSpy.mockRestore(); + vi.clearAllMocks(); + }); + + describe('generate subcommand', () => { + it('should generate Zsh completion script to stdout', async () => { + await command.generate({ shell: 'zsh' }); + + expect(consoleLogSpy).toHaveBeenCalled(); + const output = consoleLogSpy.mock.calls[0][0]; + expect(output).toContain('#compdef openspec'); + expect(output).toContain('_openspec() {'); + }); + + it('should auto-detect Zsh shell when no shell specified', async () => { + vi.mocked(shellDetection.detectShell).mockReturnValue('zsh'); + + await command.generate({}); + + expect(consoleLogSpy).toHaveBeenCalled(); + const output = consoleLogSpy.mock.calls[0][0]; + expect(output).toContain('#compdef openspec'); + }); + + it('should show error when shell cannot be auto-detected', async () => { + vi.mocked(shellDetection.detectShell).mockReturnValue(undefined); + + await command.generate({}); + + expect(consoleErrorSpy).toHaveBeenCalledWith( + 'Error: Could not auto-detect shell. Please specify shell explicitly.' + ); + expect(process.exitCode).toBe(1); + }); + + it('should show error for unsupported shell', async () => { + await command.generate({ shell: 'bash' }); + + expect(consoleErrorSpy).toHaveBeenCalledWith( + "Error: Shell 'bash' is not supported yet. Currently supported: zsh" + ); + expect(process.exitCode).toBe(1); + }); + + it('should handle shell parameter case-insensitively', async () => { + await command.generate({ shell: 'ZSH' }); + + expect(consoleLogSpy).toHaveBeenCalled(); + const output = consoleLogSpy.mock.calls[0][0]; + expect(output).toContain('#compdef openspec'); + }); + }); + + describe('install subcommand', () => { + it('should install Zsh completion script', async () => { + await command.install({ shell: 'zsh' }); + + expect(consoleLogSpy).toHaveBeenCalledWith( + expect.stringContaining('Completion script installed successfully') + ); + expect(process.exitCode).toBe(0); + }); + + it('should show verbose output when --verbose flag is provided', async () => { + await command.install({ shell: 'zsh', verbose: true }); + + expect(consoleLogSpy).toHaveBeenCalledWith( + expect.stringContaining('Installed to:') + ); + }); + + it('should auto-detect Zsh shell when no shell specified', async () => { + vi.mocked(shellDetection.detectShell).mockReturnValue('zsh'); + + await command.install({}); + + expect(consoleLogSpy).toHaveBeenCalledWith( + expect.stringContaining('Completion script installed successfully') + ); + }); + + it('should show error when shell cannot be auto-detected', async () => { + vi.mocked(shellDetection.detectShell).mockReturnValue(undefined); + + await command.install({}); + + expect(consoleErrorSpy).toHaveBeenCalledWith( + 'Error: Could not auto-detect shell. Please specify shell explicitly.' + ); + expect(process.exitCode).toBe(1); + }); + + it('should show error for unsupported shell', async () => { + await command.install({ shell: 'fish' }); + + expect(consoleErrorSpy).toHaveBeenCalledWith( + "Error: Shell 'fish' is not supported yet. Currently supported: zsh" + ); + expect(process.exitCode).toBe(1); + }); + + it('should display installation instructions', async () => { + await command.install({ shell: 'zsh' }); + + expect(consoleLogSpy).toHaveBeenCalledWith( + expect.stringContaining('Restart your shell or run: exec zsh') + ); + }); + }); + + describe('uninstall subcommand', () => { + it('should uninstall Zsh completion script', async () => { + await command.uninstall({ shell: 'zsh' }); + + expect(consoleLogSpy).toHaveBeenCalledWith( + expect.stringContaining('Completion script removed') + ); + expect(process.exitCode).toBe(0); + }); + + it('should auto-detect Zsh shell when no shell specified', async () => { + vi.mocked(shellDetection.detectShell).mockReturnValue('zsh'); + + await command.uninstall({}); + + expect(consoleLogSpy).toHaveBeenCalledWith( + expect.stringContaining('Completion script removed') + ); + }); + + it('should show error when shell cannot be auto-detected', async () => { + vi.mocked(shellDetection.detectShell).mockReturnValue(undefined); + + await command.uninstall({}); + + expect(consoleErrorSpy).toHaveBeenCalledWith( + 'Error: Could not auto-detect shell. Please specify shell explicitly.' + ); + expect(process.exitCode).toBe(1); + }); + + it('should show error for unsupported shell', async () => { + await command.uninstall({ shell: 'powershell' }); + + expect(consoleErrorSpy).toHaveBeenCalledWith( + "Error: Shell 'powershell' is not supported yet. Currently supported: zsh" + ); + process.exitCode = 1; + }); + }); + + describe('error handling', () => { + it('should handle installation failures gracefully', async () => { + const { ZshInstaller } = await import('../../src/core/completions/installers/zsh-installer.js'); + vi.mocked(ZshInstaller).mockImplementationOnce(() => ({ + install: vi.fn().mockResolvedValue({ + success: false, + isOhMyZsh: false, + message: 'Permission denied', + }), + uninstall: vi.fn(), + isInstalled: vi.fn(), + getInstallationInfo: vi.fn(), + isOhMyZshInstalled: vi.fn(), + getInstallationPath: vi.fn(), + backupExistingFile: vi.fn(), + } as any)); + + const cmd = new CompletionCommand(); + await cmd.install({ shell: 'zsh' }); + + expect(consoleErrorSpy).toHaveBeenCalledWith( + expect.stringContaining('Permission denied') + ); + expect(process.exitCode).toBe(1); + }); + + it('should handle uninstallation failures gracefully', async () => { + const { ZshInstaller } = await import('../../src/core/completions/installers/zsh-installer.js'); + vi.mocked(ZshInstaller).mockImplementationOnce(() => ({ + install: vi.fn(), + uninstall: vi.fn().mockResolvedValue({ + success: false, + message: 'Completion script is not installed', + }), + isInstalled: vi.fn(), + getInstallationInfo: vi.fn(), + isOhMyZshInstalled: vi.fn(), + getInstallationPath: vi.fn(), + backupExistingFile: vi.fn(), + } as any)); + + const cmd = new CompletionCommand(); + await cmd.uninstall({ shell: 'zsh' }); + + expect(consoleErrorSpy).toHaveBeenCalledWith( + expect.stringContaining('Completion script is not installed') + ); + expect(process.exitCode).toBe(1); + }); + }); + + describe('shell detection integration', () => { + it('should fallback to error when detected shell is unsupported', async () => { + vi.mocked(shellDetection.detectShell).mockReturnValue('bash'); + + await command.generate({}); + + expect(consoleErrorSpy).toHaveBeenCalledWith( + expect.stringContaining('Could not auto-detect shell') + ); + expect(process.exitCode).toBe(1); + }); + + it('should respect explicit shell parameter over auto-detection', async () => { + vi.mocked(shellDetection.detectShell).mockReturnValue('bash'); + + await command.generate({ shell: 'zsh' }); + + expect(consoleLogSpy).toHaveBeenCalled(); + const output = consoleLogSpy.mock.calls[0][0]; + expect(output).toContain('#compdef openspec'); + }); + }); +}); diff --git a/test/core/completions/completion-provider.test.ts b/test/core/completions/completion-provider.test.ts new file mode 100644 index 00000000..2af6dc24 --- /dev/null +++ b/test/core/completions/completion-provider.test.ts @@ -0,0 +1,288 @@ +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; +import { promises as fs } from 'fs'; +import path from 'path'; +import os from 'os'; +import { randomUUID } from 'crypto'; +import { CompletionProvider } from '../../../src/core/completions/completion-provider.js'; + +describe('CompletionProvider', () => { + let testDir: string; + let provider: CompletionProvider; + + beforeEach(async () => { + testDir = path.join(os.tmpdir(), `openspec-test-${randomUUID()}`); + await fs.mkdir(testDir, { recursive: true }); + provider = new CompletionProvider(2000, testDir); + }); + + afterEach(async () => { + await fs.rm(testDir, { recursive: true, force: true }); + }); + + describe('getChangeIds', () => { + it('should return empty array when no changes exist', async () => { + const changeIds = await provider.getChangeIds(); + expect(changeIds).toEqual([]); + }); + + it('should return active change IDs', async () => { + // Create openspec/changes directory structure + const changesDir = path.join(testDir, 'openspec', 'changes'); + await fs.mkdir(changesDir, { recursive: true }); + + // Create some changes + await fs.mkdir(path.join(changesDir, 'change-1'), { recursive: true }); + await fs.writeFile(path.join(changesDir, 'change-1', 'proposal.md'), '# Change 1'); + + await fs.mkdir(path.join(changesDir, 'change-2'), { recursive: true }); + await fs.writeFile(path.join(changesDir, 'change-2', 'proposal.md'), '# Change 2'); + + const changeIds = await provider.getChangeIds(); + expect(changeIds).toEqual(['change-1', 'change-2']); + }); + + it('should exclude archive directory', async () => { + const changesDir = path.join(testDir, 'openspec', 'changes'); + await fs.mkdir(changesDir, { recursive: true }); + + // Create active change + await fs.mkdir(path.join(changesDir, 'active-change'), { recursive: true }); + await fs.writeFile(path.join(changesDir, 'active-change', 'proposal.md'), '# Active'); + + // Create archived change + await fs.mkdir(path.join(changesDir, 'archive', 'old-change'), { recursive: true }); + await fs.writeFile(path.join(changesDir, 'archive', 'old-change', 'proposal.md'), '# Old'); + + const changeIds = await provider.getChangeIds(); + expect(changeIds).toEqual(['active-change']); + }); + + it('should cache results for the TTL duration', async () => { + const changesDir = path.join(testDir, 'openspec', 'changes'); + await fs.mkdir(changesDir, { recursive: true }); + + await fs.mkdir(path.join(changesDir, 'change-1'), { recursive: true }); + await fs.writeFile(path.join(changesDir, 'change-1', 'proposal.md'), '# Change 1'); + + // First call + const firstResult = await provider.getChangeIds(); + expect(firstResult).toEqual(['change-1']); + + // Add another change + await fs.mkdir(path.join(changesDir, 'change-2'), { recursive: true }); + await fs.writeFile(path.join(changesDir, 'change-2', 'proposal.md'), '# Change 2'); + + // Second call should return cached result (still only change-1) + const secondResult = await provider.getChangeIds(); + expect(secondResult).toEqual(['change-1']); + }); + + it('should refresh cache after TTL expires', async () => { + // Use a very short TTL for testing + const shortTTLProvider = new CompletionProvider(50, testDir); + + const changesDir = path.join(testDir, 'openspec', 'changes'); + await fs.mkdir(changesDir, { recursive: true }); + + await fs.mkdir(path.join(changesDir, 'change-1'), { recursive: true }); + await fs.writeFile(path.join(changesDir, 'change-1', 'proposal.md'), '# Change 1'); + + // First call + const firstResult = await shortTTLProvider.getChangeIds(); + expect(firstResult).toEqual(['change-1']); + + // Add another change + await fs.mkdir(path.join(changesDir, 'change-2'), { recursive: true }); + await fs.writeFile(path.join(changesDir, 'change-2', 'proposal.md'), '# Change 2'); + + // Wait for cache to expire + await new Promise(resolve => setTimeout(resolve, 60)); + + // Should now see both changes + const secondResult = await shortTTLProvider.getChangeIds(); + expect(secondResult).toEqual(['change-1', 'change-2']); + }); + }); + + describe('getSpecIds', () => { + it('should return empty array when no specs exist', async () => { + const specIds = await provider.getSpecIds(); + expect(specIds).toEqual([]); + }); + + it('should return spec IDs', async () => { + const specsDir = path.join(testDir, 'openspec', 'specs'); + await fs.mkdir(specsDir, { recursive: true }); + + // Create some specs + await fs.mkdir(path.join(specsDir, 'spec-1'), { recursive: true }); + await fs.writeFile(path.join(specsDir, 'spec-1', 'spec.md'), '# Spec 1'); + + await fs.mkdir(path.join(specsDir, 'spec-2'), { recursive: true }); + await fs.writeFile(path.join(specsDir, 'spec-2', 'spec.md'), '# Spec 2'); + + const specIds = await provider.getSpecIds(); + expect(specIds).toEqual(['spec-1', 'spec-2']); + }); + + it('should cache results for the TTL duration', async () => { + const specsDir = path.join(testDir, 'openspec', 'specs'); + await fs.mkdir(specsDir, { recursive: true }); + + await fs.mkdir(path.join(specsDir, 'spec-1'), { recursive: true }); + await fs.writeFile(path.join(specsDir, 'spec-1', 'spec.md'), '# Spec 1'); + + // First call + const firstResult = await provider.getSpecIds(); + expect(firstResult).toEqual(['spec-1']); + + // Add another spec + await fs.mkdir(path.join(specsDir, 'spec-2'), { recursive: true }); + await fs.writeFile(path.join(specsDir, 'spec-2', 'spec.md'), '# Spec 2'); + + // Second call should return cached result + const secondResult = await provider.getSpecIds(); + expect(secondResult).toEqual(['spec-1']); + }); + + it('should refresh cache after TTL expires', async () => { + const shortTTLProvider = new CompletionProvider(50, testDir); + + const specsDir = path.join(testDir, 'openspec', 'specs'); + await fs.mkdir(specsDir, { recursive: true }); + + await fs.mkdir(path.join(specsDir, 'spec-1'), { recursive: true }); + await fs.writeFile(path.join(specsDir, 'spec-1', 'spec.md'), '# Spec 1'); + + const firstResult = await shortTTLProvider.getSpecIds(); + expect(firstResult).toEqual(['spec-1']); + + // Add another spec + await fs.mkdir(path.join(specsDir, 'spec-2'), { recursive: true }); + await fs.writeFile(path.join(specsDir, 'spec-2', 'spec.md'), '# Spec 2'); + + // Wait for cache to expire + await new Promise(resolve => setTimeout(resolve, 60)); + + const secondResult = await shortTTLProvider.getSpecIds(); + expect(secondResult).toEqual(['spec-1', 'spec-2']); + }); + }); + + describe('getAllIds', () => { + it('should return both change and spec IDs', async () => { + const changesDir = path.join(testDir, 'openspec', 'changes'); + const specsDir = path.join(testDir, 'openspec', 'specs'); + await fs.mkdir(changesDir, { recursive: true }); + await fs.mkdir(specsDir, { recursive: true }); + + // Create a change + await fs.mkdir(path.join(changesDir, 'my-change'), { recursive: true }); + await fs.writeFile(path.join(changesDir, 'my-change', 'proposal.md'), '# Change'); + + // Create a spec + await fs.mkdir(path.join(specsDir, 'my-spec'), { recursive: true }); + await fs.writeFile(path.join(specsDir, 'my-spec', 'spec.md'), '# Spec'); + + const result = await provider.getAllIds(); + expect(result).toEqual({ + changeIds: ['my-change'], + specIds: ['my-spec'], + }); + }); + + it('should return empty arrays when no items exist', async () => { + const result = await provider.getAllIds(); + expect(result).toEqual({ + changeIds: [], + specIds: [], + }); + }); + }); + + describe('clearCache', () => { + it('should clear all cached data', async () => { + const changesDir = path.join(testDir, 'openspec', 'changes'); + await fs.mkdir(changesDir, { recursive: true }); + + await fs.mkdir(path.join(changesDir, 'change-1'), { recursive: true }); + await fs.writeFile(path.join(changesDir, 'change-1', 'proposal.md'), '# Change 1'); + + // Populate cache + await provider.getChangeIds(); + + // Clear cache + provider.clearCache(); + + // Add new change + await fs.mkdir(path.join(changesDir, 'change-2'), { recursive: true }); + await fs.writeFile(path.join(changesDir, 'change-2', 'proposal.md'), '# Change 2'); + + // Should see new data immediately + const result = await provider.getChangeIds(); + expect(result).toEqual(['change-1', 'change-2']); + }); + }); + + describe('getCacheStats', () => { + it('should report invalid cache when empty', () => { + const stats = provider.getCacheStats(); + expect(stats.changeCache.valid).toBe(false); + expect(stats.specCache.valid).toBe(false); + expect(stats.changeCache.age).toBeUndefined(); + expect(stats.specCache.age).toBeUndefined(); + }); + + it('should report valid cache after data is fetched', async () => { + const changesDir = path.join(testDir, 'openspec', 'changes'); + await fs.mkdir(changesDir, { recursive: true }); + + await fs.mkdir(path.join(changesDir, 'change-1'), { recursive: true }); + await fs.writeFile(path.join(changesDir, 'change-1', 'proposal.md'), '# Change 1'); + + await provider.getChangeIds(); + + const stats = provider.getCacheStats(); + expect(stats.changeCache.valid).toBe(true); + expect(stats.changeCache.age).toBeDefined(); + expect(stats.changeCache.age).toBeLessThan(100); + }); + + it('should report invalid cache after TTL expires', async () => { + const shortTTLProvider = new CompletionProvider(50, testDir); + + const changesDir = path.join(testDir, 'openspec', 'changes'); + await fs.mkdir(changesDir, { recursive: true }); + + await fs.mkdir(path.join(changesDir, 'change-1'), { recursive: true }); + await fs.writeFile(path.join(changesDir, 'change-1', 'proposal.md'), '# Change 1'); + + await shortTTLProvider.getChangeIds(); + + // Wait for cache to expire + await new Promise(resolve => setTimeout(resolve, 60)); + + const stats = shortTTLProvider.getCacheStats(); + expect(stats.changeCache.valid).toBe(false); + expect(stats.changeCache.age).toBeGreaterThan(50); + }); + }); + + describe('constructor', () => { + it('should use default TTL of 2000ms', async () => { + const defaultProvider = new CompletionProvider(); + expect(defaultProvider).toBeDefined(); + // We can verify this behavior by checking cache stats after waiting + }); + + it('should accept custom TTL', async () => { + const customProvider = new CompletionProvider(5000, testDir); + expect(customProvider).toBeDefined(); + }); + + it('should use process.cwd() as default project root', () => { + const defaultProvider = new CompletionProvider(); + expect(defaultProvider).toBeDefined(); + }); + }); +}); diff --git a/test/core/completions/generators/zsh-generator.test.ts b/test/core/completions/generators/zsh-generator.test.ts new file mode 100644 index 00000000..3febdab8 --- /dev/null +++ b/test/core/completions/generators/zsh-generator.test.ts @@ -0,0 +1,381 @@ +import { describe, it, expect } from 'vitest'; +import { ZshGenerator } from '../../../../src/core/completions/generators/zsh-generator.js'; +import { CommandDefinition } from '../../../../src/core/completions/types.js'; + +describe('ZshGenerator', () => { + let generator: ZshGenerator; + + beforeEach(() => { + generator = new ZshGenerator(); + }); + + describe('interface compliance', () => { + it('should have shell property set to "zsh"', () => { + expect(generator.shell).toBe('zsh'); + }); + + it('should implement generate method', () => { + expect(typeof generator.generate).toBe('function'); + }); + }); + + describe('generate', () => { + it('should generate valid zsh completion script with header', () => { + const commands: CommandDefinition[] = [ + { + name: 'init', + description: 'Initialize OpenSpec', + flags: [], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain('#compdef openspec'); + expect(script).toContain('# Zsh completion script for OpenSpec CLI'); + expect(script).toContain('_openspec() {'); + }); + + it('should include all commands in the command list', () => { + const commands: CommandDefinition[] = [ + { + name: 'init', + description: 'Initialize OpenSpec', + flags: [], + }, + { + name: 'validate', + description: 'Validate specs', + flags: [], + }, + { + name: 'show', + description: 'Show a spec', + flags: [], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain("'init:Initialize OpenSpec'"); + expect(script).toContain("'validate:Validate specs'"); + expect(script).toContain("'show:Show a spec'"); + }); + + it('should generate command completion functions', () => { + const commands: CommandDefinition[] = [ + { + name: 'init', + description: 'Initialize OpenSpec', + flags: [], + }, + { + name: 'validate', + description: 'Validate specs', + flags: [], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain('_openspec_init() {'); + expect(script).toContain('_openspec_validate() {'); + }); + + it('should handle commands with flags', () => { + const commands: CommandDefinition[] = [ + { + name: 'validate', + description: 'Validate specs', + flags: [ + { + name: 'strict', + description: 'Enable strict mode', + }, + { + name: 'json', + description: 'Output as JSON', + }, + ], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain('--strict'); + expect(script).toContain('[Enable strict mode]'); + expect(script).toContain('--json'); + expect(script).toContain('[Output as JSON]'); + }); + + it('should handle flags with short options', () => { + const commands: CommandDefinition[] = [ + { + name: 'show', + description: 'Show a spec', + flags: [ + { + name: 'requirement', + short: 'r', + description: 'Show specific requirement', + takesValue: true, + }, + ], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain("'(-r --requirement)'{-r,--requirement}'"); + expect(script).toContain('[Show specific requirement]'); + }); + + it('should handle flags that take values', () => { + const commands: CommandDefinition[] = [ + { + name: 'validate', + description: 'Validate specs', + flags: [ + { + name: 'type', + description: 'Specify item type', + takesValue: true, + values: ['change', 'spec'], + }, + ], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain('--type'); + expect(script).toContain('[Specify item type]'); + expect(script).toContain(':value:(change spec)'); + }); + + it('should handle flags with takesValue but no specific values', () => { + const commands: CommandDefinition[] = [ + { + name: 'validate', + description: 'Validate specs', + flags: [ + { + name: 'concurrency', + description: 'Max concurrent validations', + takesValue: true, + }, + ], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain('--concurrency'); + expect(script).toContain('[Max concurrent validations]'); + expect(script).toContain(':value:'); + }); + + it('should handle commands with subcommands', () => { + const commands: CommandDefinition[] = [ + { + name: 'change', + description: 'Manage changes', + flags: [], + subcommands: [ + { + name: 'show', + description: 'Show a change', + flags: [], + }, + { + name: 'list', + description: 'List changes', + flags: [], + }, + ], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain("'show:Show a change'"); + expect(script).toContain("'list:List changes'"); + expect(script).toContain('_openspec_change_show() {'); + expect(script).toContain('_openspec_change_list() {'); + }); + + it('should handle positional arguments for change-id', () => { + const commands: CommandDefinition[] = [ + { + name: 'archive', + description: 'Archive a change', + acceptsPositional: true, + positionalType: 'change-id', + flags: [], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain("'*: :_openspec_complete_changes'"); + }); + + it('should handle positional arguments for spec-id', () => { + const commands: CommandDefinition[] = [ + { + name: 'show-spec', + description: 'Show a spec', + acceptsPositional: true, + positionalType: 'spec-id', + flags: [], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain("'*: :_openspec_complete_specs'"); + }); + + it('should handle positional arguments for change-or-spec-id', () => { + const commands: CommandDefinition[] = [ + { + name: 'show', + description: 'Show an item', + acceptsPositional: true, + positionalType: 'change-or-spec-id', + flags: [], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain("'*: :_openspec_complete_items'"); + }); + + it('should handle positional arguments for paths', () => { + const commands: CommandDefinition[] = [ + { + name: 'init', + description: 'Initialize OpenSpec', + acceptsPositional: true, + positionalType: 'path', + flags: [], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain("'*:path:_files'"); + }); + + it('should escape special characters in descriptions', () => { + const commands: CommandDefinition[] = [ + { + name: 'test', + description: "Test with 'quotes' and [brackets] and back\\slash and colon:", + flags: [ + { + name: 'flag', + description: "Special chars: 'quotes' [brackets] back\\slash colon:", + }, + ], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain("\\'quotes\\'"); + expect(script).toContain('\\[brackets\\]'); + expect(script).toContain('\\\\slash'); + expect(script).toContain('\\:'); + }); + + it('should sanitize command names with hyphens for function names', () => { + const commands: CommandDefinition[] = [ + { + name: 'my-command', + description: 'A hyphenated command', + flags: [], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain('_openspec_my_command() {'); + }); + + it('should handle complex nested subcommands with flags', () => { + const commands: CommandDefinition[] = [ + { + name: 'spec', + description: 'Manage specs', + flags: [], + subcommands: [ + { + name: 'validate', + description: 'Validate a spec', + acceptsPositional: true, + positionalType: 'spec-id', + flags: [ + { + name: 'strict', + description: 'Enable strict mode', + }, + { + name: 'json', + description: 'Output as JSON', + }, + ], + }, + ], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain('_openspec_spec() {'); + expect(script).toContain('_openspec_spec_validate() {'); + expect(script).toContain('--strict'); + expect(script).toContain('--json'); + expect(script).toContain("'*: :_openspec_complete_specs'"); + }); + + it('should generate script that ends with main function call', () => { + const commands: CommandDefinition[] = [ + { + name: 'init', + description: 'Initialize', + flags: [], + }, + ]; + + const script = generator.generate(commands); + + expect(script.trim().endsWith('_openspec "$@"')).toBe(true); + }); + + it('should handle empty command list', () => { + const commands: CommandDefinition[] = []; + + const script = generator.generate(commands); + + expect(script).toContain('#compdef openspec'); + expect(script).toContain('_openspec() {'); + }); + + it('should handle commands with no flags', () => { + const commands: CommandDefinition[] = [ + { + name: 'view', + description: 'Display dashboard', + flags: [], + }, + ]; + + const script = generator.generate(commands); + + expect(script).toContain('_openspec_view() {'); + expect(script).toContain('_arguments'); + }); + }); +}); diff --git a/test/core/completions/installers/zsh-installer.test.ts b/test/core/completions/installers/zsh-installer.test.ts new file mode 100644 index 00000000..9dfe1aa8 --- /dev/null +++ b/test/core/completions/installers/zsh-installer.test.ts @@ -0,0 +1,311 @@ +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; +import { promises as fs } from 'fs'; +import path from 'path'; +import os from 'os'; +import { randomUUID } from 'crypto'; +import { ZshInstaller } from '../../../../src/core/completions/installers/zsh-installer.js'; + +describe('ZshInstaller', () => { + let testHomeDir: string; + let installer: ZshInstaller; + + beforeEach(async () => { + // Create a temporary home directory for testing + testHomeDir = path.join(os.tmpdir(), `openspec-zsh-test-${randomUUID()}`); + await fs.mkdir(testHomeDir, { recursive: true }); + installer = new ZshInstaller(testHomeDir); + }); + + afterEach(async () => { + // Clean up test directory + await fs.rm(testHomeDir, { recursive: true, force: true }); + }); + + describe('isOhMyZshInstalled', () => { + it('should return false when Oh My Zsh is not installed', async () => { + const isInstalled = await installer.isOhMyZshInstalled(); + expect(isInstalled).toBe(false); + }); + + it('should return true when Oh My Zsh directory exists', async () => { + // Create .oh-my-zsh directory + const ohMyZshPath = path.join(testHomeDir, '.oh-my-zsh'); + await fs.mkdir(ohMyZshPath, { recursive: true }); + + const isInstalled = await installer.isOhMyZshInstalled(); + expect(isInstalled).toBe(true); + }); + + it('should return false when .oh-my-zsh exists but is a file', async () => { + // Create .oh-my-zsh as a file instead of directory + const ohMyZshPath = path.join(testHomeDir, '.oh-my-zsh'); + await fs.writeFile(ohMyZshPath, 'not a directory'); + + const isInstalled = await installer.isOhMyZshInstalled(); + expect(isInstalled).toBe(false); + }); + }); + + describe('getInstallationPath', () => { + it('should return Oh My Zsh path when Oh My Zsh is installed', async () => { + // Create .oh-my-zsh directory + const ohMyZshPath = path.join(testHomeDir, '.oh-my-zsh'); + await fs.mkdir(ohMyZshPath, { recursive: true }); + + const result = await installer.getInstallationPath(); + + expect(result.isOhMyZsh).toBe(true); + expect(result.path).toBe(path.join(testHomeDir, '.oh-my-zsh', 'completions', '_openspec')); + }); + + it('should return standard Zsh path when Oh My Zsh is not installed', async () => { + const result = await installer.getInstallationPath(); + + expect(result.isOhMyZsh).toBe(false); + expect(result.path).toBe(path.join(testHomeDir, '.zsh', 'completions', '_openspec')); + }); + }); + + describe('backupExistingFile', () => { + it('should return undefined when file does not exist', async () => { + const nonExistentPath = path.join(testHomeDir, 'nonexistent.txt'); + const backupPath = await installer.backupExistingFile(nonExistentPath); + + expect(backupPath).toBeUndefined(); + }); + + it('should create backup when file exists', async () => { + const filePath = path.join(testHomeDir, 'test.txt'); + await fs.writeFile(filePath, 'original content'); + + const backupPath = await installer.backupExistingFile(filePath); + + expect(backupPath).toBeDefined(); + expect(backupPath).toContain('.backup-'); + + // Verify backup file exists and has correct content + const backupContent = await fs.readFile(backupPath!, 'utf-8'); + expect(backupContent).toBe('original content'); + }); + + it('should create backup with timestamp in filename', async () => { + const filePath = path.join(testHomeDir, 'test.txt'); + await fs.writeFile(filePath, 'content'); + + const backupPath = await installer.backupExistingFile(filePath); + + expect(backupPath).toMatch(/\.backup-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}/); + }); + }); + + describe('install', () => { + const testScript = '#compdef openspec\n_openspec() {\n echo "test"\n}\n'; + + it('should install to Oh My Zsh path when Oh My Zsh is present', async () => { + // Create .oh-my-zsh directory + const ohMyZshPath = path.join(testHomeDir, '.oh-my-zsh'); + await fs.mkdir(ohMyZshPath, { recursive: true }); + + const result = await installer.install(testScript); + + expect(result.success).toBe(true); + expect(result.isOhMyZsh).toBe(true); + expect(result.installedPath).toBe(path.join(ohMyZshPath, 'completions', '_openspec')); + expect(result.message).toContain('Oh My Zsh'); + + // Verify file was created with correct content + const content = await fs.readFile(result.installedPath!, 'utf-8'); + expect(content).toBe(testScript); + }); + + it('should install to standard Zsh path when Oh My Zsh is not present', async () => { + const result = await installer.install(testScript); + + expect(result.success).toBe(true); + expect(result.isOhMyZsh).toBe(false); + expect(result.installedPath).toBe(path.join(testHomeDir, '.zsh', 'completions', '_openspec')); + + // Verify file was created + const content = await fs.readFile(result.installedPath!, 'utf-8'); + expect(content).toBe(testScript); + }); + + it('should create necessary directories if they do not exist', async () => { + const result = await installer.install(testScript); + + expect(result.success).toBe(true); + + // Verify directory structure was created + const completionsDir = path.dirname(result.installedPath!); + const stat = await fs.stat(completionsDir); + expect(stat.isDirectory()).toBe(true); + }); + + it('should backup existing file before overwriting', async () => { + const targetPath = path.join(testHomeDir, '.zsh', 'completions', '_openspec'); + await fs.mkdir(path.dirname(targetPath), { recursive: true }); + await fs.writeFile(targetPath, 'old script'); + + const result = await installer.install(testScript); + + expect(result.success).toBe(true); + expect(result.backupPath).toBeDefined(); + expect(result.backupPath).toContain('.backup-'); + + // Verify backup has old content + const backupContent = await fs.readFile(result.backupPath!, 'utf-8'); + expect(backupContent).toBe('old script'); + + // Verify new file has new content + const newContent = await fs.readFile(targetPath, 'utf-8'); + expect(newContent).toBe(testScript); + }); + + it('should include instructions in result for Oh My Zsh', async () => { + const ohMyZshPath = path.join(testHomeDir, '.oh-my-zsh'); + await fs.mkdir(ohMyZshPath, { recursive: true }); + + const result = await installer.install(testScript); + + expect(result.instructions).toBeDefined(); + expect(result.instructions!.length).toBeGreaterThan(0); + expect(result.instructions!.join(' ')).toContain('exec zsh'); + expect(result.instructions!.join(' ')).toContain('automatically'); + }); + + it('should include fpath instructions for standard Zsh', async () => { + const result = await installer.install(testScript); + + expect(result.instructions).toBeDefined(); + expect(result.instructions!.join('\n')).toContain('fpath'); + expect(result.instructions!.join('\n')).toContain('.zshrc'); + expect(result.instructions!.join('\n')).toContain('compinit'); + }); + + it('should handle installation errors gracefully', async () => { + // Create installer with non-existent/invalid home directory + const invalidInstaller = new ZshInstaller('/root/invalid/nonexistent/path'); + + const result = await invalidInstaller.install(testScript); + + expect(result.success).toBe(false); + expect(result.message).toContain('Failed to install'); + }); + }); + + describe('uninstall', () => { + const testScript = '#compdef openspec\n_openspec() {}\n'; + + it('should remove installed completion script', async () => { + // Install first + await installer.install(testScript); + + // Verify it's installed + const beforeUninstall = await installer.isInstalled(); + expect(beforeUninstall).toBe(true); + + // Uninstall + const result = await installer.uninstall(); + + expect(result.success).toBe(true); + expect(result.message).toContain('removed'); + + // Verify it's gone + const afterUninstall = await installer.isInstalled(); + expect(afterUninstall).toBe(false); + }); + + it('should return failure when script is not installed', async () => { + const result = await installer.uninstall(); + + expect(result.success).toBe(false); + expect(result.message).toContain('not installed'); + }); + + it('should remove from correct location for Oh My Zsh', async () => { + const ohMyZshPath = path.join(testHomeDir, '.oh-my-zsh'); + await fs.mkdir(ohMyZshPath, { recursive: true }); + + await installer.install(testScript); + + const result = await installer.uninstall(); + + expect(result.success).toBe(true); + expect(result.message).toContain(path.join('.oh-my-zsh', 'completions', '_openspec')); + }); + }); + + describe('isInstalled', () => { + const testScript = '#compdef openspec\n_openspec() {}\n'; + + it('should return false when not installed', async () => { + const isInstalled = await installer.isInstalled(); + expect(isInstalled).toBe(false); + }); + + it('should return true when installed', async () => { + await installer.install(testScript); + + const isInstalled = await installer.isInstalled(); + expect(isInstalled).toBe(true); + }); + + it('should check correct location for Oh My Zsh', async () => { + const ohMyZshPath = path.join(testHomeDir, '.oh-my-zsh'); + await fs.mkdir(ohMyZshPath, { recursive: true }); + + await installer.install(testScript); + + const isInstalled = await installer.isInstalled(); + expect(isInstalled).toBe(true); + }); + }); + + describe('getInstallationInfo', () => { + const testScript = '#compdef openspec\n_openspec() {}\n'; + + it('should return not installed when script does not exist', async () => { + const info = await installer.getInstallationInfo(); + + expect(info.installed).toBe(false); + expect(info.path).toBeUndefined(); + expect(info.isOhMyZsh).toBeUndefined(); + }); + + it('should return installation info when installed', async () => { + await installer.install(testScript); + + const info = await installer.getInstallationInfo(); + + expect(info.installed).toBe(true); + expect(info.path).toBeDefined(); + expect(info.path).toContain('_openspec'); + expect(info.isOhMyZsh).toBe(false); + }); + + it('should indicate Oh My Zsh when installed there', async () => { + const ohMyZshPath = path.join(testHomeDir, '.oh-my-zsh'); + await fs.mkdir(ohMyZshPath, { recursive: true }); + + await installer.install(testScript); + + const info = await installer.getInstallationInfo(); + + expect(info.installed).toBe(true); + expect(info.isOhMyZsh).toBe(true); + expect(info.path).toContain('.oh-my-zsh'); + }); + }); + + describe('constructor', () => { + it('should use provided home directory', () => { + const customInstaller = new ZshInstaller('/custom/home'); + expect(customInstaller).toBeDefined(); + }); + + it('should use os.homedir() by default', () => { + const defaultInstaller = new ZshInstaller(); + expect(defaultInstaller).toBeDefined(); + }); + }); +}); diff --git a/test/utils/shell-detection.test.ts b/test/utils/shell-detection.test.ts new file mode 100644 index 00000000..34373c83 --- /dev/null +++ b/test/utils/shell-detection.test.ts @@ -0,0 +1,158 @@ +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; +import { detectShell, SupportedShell } from '../../src/utils/shell-detection.js'; + +describe('shell-detection', () => { + let originalShell: string | undefined; + let originalPSModulePath: string | undefined; + let originalComspec: string | undefined; + let originalPlatform: NodeJS.Platform; + + beforeEach(() => { + // Save original environment + originalShell = process.env.SHELL; + originalPSModulePath = process.env.PSModulePath; + originalComspec = process.env.COMSPEC; + originalPlatform = process.platform; + + // Clear environment for clean testing + delete process.env.SHELL; + delete process.env.PSModulePath; + delete process.env.COMSPEC; + }); + + afterEach(() => { + // Restore original environment + if (originalShell !== undefined) { + process.env.SHELL = originalShell; + } else { + delete process.env.SHELL; + } + if (originalPSModulePath !== undefined) { + process.env.PSModulePath = originalPSModulePath; + } else { + delete process.env.PSModulePath; + } + if (originalComspec !== undefined) { + process.env.COMSPEC = originalComspec; + } else { + delete process.env.COMSPEC; + } + Object.defineProperty(process, 'platform', { + value: originalPlatform, + }); + }); + + describe('detectShell', () => { + it('should detect zsh from SHELL environment variable', () => { + process.env.SHELL = '/bin/zsh'; + expect(detectShell()).toBe('zsh'); + }); + + it('should detect zsh from various zsh paths', () => { + const zshPaths = [ + '/usr/bin/zsh', + '/usr/local/bin/zsh', + '/opt/homebrew/bin/zsh', + '/home/user/.local/bin/zsh', + ]; + + for (const path of zshPaths) { + process.env.SHELL = path; + expect(detectShell()).toBe('zsh'); + } + }); + + it('should detect bash from SHELL environment variable', () => { + process.env.SHELL = '/bin/bash'; + expect(detectShell()).toBe('bash'); + }); + + it('should detect bash from various bash paths', () => { + const bashPaths = [ + '/usr/bin/bash', + '/usr/local/bin/bash', + '/opt/homebrew/bin/bash', + '/home/user/.local/bin/bash', + ]; + + for (const path of bashPaths) { + process.env.SHELL = path; + expect(detectShell()).toBe('bash'); + } + }); + + it('should detect fish from SHELL environment variable', () => { + process.env.SHELL = '/usr/bin/fish'; + expect(detectShell()).toBe('fish'); + }); + + it('should detect fish from various fish paths', () => { + const fishPaths = [ + '/bin/fish', + '/usr/local/bin/fish', + '/opt/homebrew/bin/fish', + '/home/user/.local/bin/fish', + ]; + + for (const path of fishPaths) { + process.env.SHELL = path; + expect(detectShell()).toBe('fish'); + } + }); + + it('should be case-insensitive when detecting shell', () => { + process.env.SHELL = '/BIN/ZSH'; + expect(detectShell()).toBe('zsh'); + + process.env.SHELL = '/USR/BIN/BASH'; + expect(detectShell()).toBe('bash'); + + process.env.SHELL = '/USR/BIN/FISH'; + expect(detectShell()).toBe('fish'); + }); + + it('should detect PowerShell from PSModulePath environment variable', () => { + process.env.PSModulePath = 'C:\\Program Files\\PowerShell\\Modules'; + expect(detectShell()).toBe('powershell'); + }); + + it('should detect PowerShell on Windows platform with PSModulePath', () => { + Object.defineProperty(process, 'platform', { + value: 'win32', + }); + process.env.PSModulePath = 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\Modules'; + expect(detectShell()).toBe('powershell'); + }); + + it('should return undefined for unsupported shell', () => { + process.env.SHELL = '/bin/tcsh'; + expect(detectShell()).toBeUndefined(); + }); + + it('should return undefined when SHELL is not set and not on Windows', () => { + Object.defineProperty(process, 'platform', { + value: 'linux', + }); + expect(detectShell()).toBeUndefined(); + }); + + it('should return undefined for cmd.exe on Windows', () => { + Object.defineProperty(process, 'platform', { + value: 'win32', + }); + process.env.COMSPEC = 'C:\\Windows\\System32\\cmd.exe'; + expect(detectShell()).toBeUndefined(); + }); + + it('should return undefined when no shell information is available', () => { + expect(detectShell()).toBeUndefined(); + }); + }); + + describe('SupportedShell type', () => { + it('should accept valid shell types', () => { + const shells: SupportedShell[] = ['zsh', 'bash', 'fish', 'powershell']; + expect(shells).toHaveLength(4); + }); + }); +}); From 1784ffaa16a985f3cf613ceb0a89fc72a8b40769 Mon Sep 17 00:00:00 2001 From: noam-home Date: Thu, 6 Nov 2025 20:05:17 +0200 Subject: [PATCH 2/6] after code review changes --- README.md | 66 ---- scripts/postinstall.js | 3 + src/commands/completion.ts | 96 ++--- src/core/completions/command-registry.ts | 129 +++---- src/core/completions/factory.ts | 6 +- .../completions/generators/zsh-generator.ts | 106 +++-- .../completions/installers/zsh-installer.ts | 195 +++++++++- .../generators/zsh-generator.test.ts | 2 +- .../installers/zsh-installer.test.ts | 365 +++++++++++++++++- 9 files changed, 715 insertions(+), 253 deletions(-) diff --git a/README.md b/README.md index 1dedceb9..48a8ea42 100644 --- a/README.md +++ b/README.md @@ -244,72 +244,6 @@ openspec validate # Check spec formatting and structure openspec archive [--yes|-y] # Move a completed change into archive/ (non-interactive with --yes) ``` -## Shell Completions - -OpenSpec provides shell tab completions for faster command entry. Completions are automatically installed during `npm install` for supported shells. - -### Supported Shells - -- **Zsh** (including Oh My Zsh) - fully supported with dynamic change/spec ID completion -- **Bash** - coming soon -- **Fish** - coming soon - -### Auto-Install Behavior - -When you install OpenSpec via `npm install -g @fission-ai/openspec`, the completions are automatically installed for your shell if: -- Your shell is detected and supported (currently Zsh) -- You're not in a CI environment (`CI=true`) -- You haven't opted out with `OPENSPEC_NO_COMPLETIONS=1` - -**Oh My Zsh users**: Completions are installed to `~/.oh-my-zsh/completions/_openspec` and activate automatically after restarting your shell. - -**Standard Zsh users**: Completions are installed to `~/.zsh/completions/_openspec`. You'll need to add the completions directory to your `fpath` in `~/.zshrc`: - -```bash -# Add completions directory to fpath -fpath=(~/.zsh/completions $fpath) - -# Initialize completion system -autoload -Uz compinit -compinit -``` - -Then restart your shell with `exec zsh`. - -### Manual Installation - -If auto-install was skipped or you want to reinstall: - -```bash -# Install for your current shell (auto-detected) -openspec completion install - -# Install for a specific shell -openspec completion install zsh - -# View the completion script -openspec completion generate zsh -``` - -### Disabling Auto-Install - -To prevent automatic installation during `npm install`: - -```bash -# Set environment variable before installing -OPENSPEC_NO_COMPLETIONS=1 npm install -g @fission-ai/openspec -``` - -### Uninstalling Completions - -```bash -# Uninstall completions for your current shell -openspec completion uninstall - -# Uninstall for a specific shell -openspec completion uninstall zsh -``` - ## Example: How AI Creates OpenSpec Files When you ask your AI assistant to "add two-factor authentication", it creates: diff --git a/scripts/postinstall.js b/scripts/postinstall.js index 43b92d76..81843591 100644 --- a/scripts/postinstall.js +++ b/scripts/postinstall.js @@ -88,6 +88,9 @@ async function installCompletions(shell) { if (result.isOhMyZsh) { console.log(`✓ Shell completions installed`); console.log(` Restart shell: exec zsh`); + } else if (result.zshrcConfigured) { + console.log(`✓ Shell completions installed and configured`); + console.log(` Restart shell: exec zsh`); } else { console.log(`✓ Shell completions installed to ~/.zsh/completions/`); console.log(` Add to ~/.zshrc: fpath=(~/.zsh/completions $fpath)`); diff --git a/src/commands/completion.ts b/src/commands/completion.ts index 8ed90b1c..359601ae 100644 --- a/src/commands/completion.ts +++ b/src/commands/completion.ts @@ -21,35 +21,47 @@ interface UninstallOptions { */ export class CompletionCommand { /** - * Generate completion script and output to stdout + * Resolve shell parameter or exit with error * - * @param options - Options for generation (shell type) + * @param shell - The shell parameter (may be undefined) + * @param operationName - Name of the operation (for error messages) + * @returns Resolved shell or null if should exit */ - async generate(options: GenerateOptions = {}): Promise { - const shell = this.normalizeShell(options.shell); + private resolveShellOrExit(shell: string | undefined, operationName: string): SupportedShell | null { + const normalizedShell = this.normalizeShell(shell); - if (!shell) { + if (!normalizedShell) { const detected = detectShell(); if (detected && CompletionFactory.isSupported(detected)) { - // Use detected shell - await this.generateForShell(detected); - return; + return detected; } // No shell specified and cannot auto-detect console.error('Error: Could not auto-detect shell. Please specify shell explicitly.'); - console.error('Usage: openspec completion generate '); + console.error(`Usage: openspec completion ${operationName} [shell]`); console.error(`Currently supported: ${CompletionFactory.getSupportedShells().join(', ')}`); process.exitCode = 1; - return; + return null; } - if (!CompletionFactory.isSupported(shell)) { - console.error(`Error: Shell '${shell}' is not supported yet. Currently supported: ${CompletionFactory.getSupportedShells().join(', ')}`); + if (!CompletionFactory.isSupported(normalizedShell)) { + console.error(`Error: Shell '${normalizedShell}' is not supported yet. Currently supported: ${CompletionFactory.getSupportedShells().join(', ')}`); process.exitCode = 1; - return; + return null; } + return normalizedShell; + } + + /** + * Generate completion script and output to stdout + * + * @param options - Options for generation (shell type) + */ + async generate(options: GenerateOptions = {}): Promise { + const shell = this.resolveShellOrExit(options.shell, 'generate'); + if (!shell) return; + await this.generateForShell(shell); } @@ -59,29 +71,8 @@ export class CompletionCommand { * @param options - Options for installation (shell type, verbose output) */ async install(options: InstallOptions = {}): Promise { - const shell = this.normalizeShell(options.shell); - - if (!shell) { - const detected = detectShell(); - if (detected && CompletionFactory.isSupported(detected)) { - // Use detected shell - await this.installForShell(detected, options.verbose || false); - return; - } - - // No shell specified and cannot auto-detect - console.error('Error: Could not auto-detect shell. Please specify shell explicitly.'); - console.error('Usage: openspec completion install [shell]'); - console.error(`Currently supported: ${CompletionFactory.getSupportedShells().join(', ')}`); - process.exitCode = 1; - return; - } - - if (!CompletionFactory.isSupported(shell)) { - console.error(`Error: Shell '${shell}' is not supported yet. Currently supported: ${CompletionFactory.getSupportedShells().join(', ')}`); - process.exitCode = 1; - return; - } + const shell = this.resolveShellOrExit(options.shell, 'install'); + if (!shell) return; await this.installForShell(shell, options.verbose || false); } @@ -92,29 +83,8 @@ export class CompletionCommand { * @param options - Options for uninstallation (shell type) */ async uninstall(options: UninstallOptions = {}): Promise { - const shell = this.normalizeShell(options.shell); - - if (!shell) { - const detected = detectShell(); - if (detected && CompletionFactory.isSupported(detected)) { - // Use detected shell - await this.uninstallForShell(detected); - return; - } - - // No shell specified and cannot auto-detect - console.error('Error: Could not auto-detect shell. Please specify shell explicitly.'); - console.error('Usage: openspec completion uninstall [shell]'); - console.error(`Currently supported: ${CompletionFactory.getSupportedShells().join(', ')}`); - process.exitCode = 1; - return; - } - - if (!CompletionFactory.isSupported(shell)) { - console.error(`Error: Shell '${shell}' is not supported yet. Currently supported: ${CompletionFactory.getSupportedShells().join(', ')}`); - process.exitCode = 1; - return; - } + const shell = this.resolveShellOrExit(options.shell, 'uninstall'); + if (!shell) return; await this.uninstallForShell(shell); } @@ -154,14 +124,20 @@ export class CompletionCommand { if (result.backupPath) { console.log(` Backup created: ${result.backupPath}`); } + if (result.zshrcConfigured) { + console.log(` ~/.zshrc configured automatically`); + } } - // Print instructions + // Print instructions (only shown if .zshrc wasn't auto-configured) if (result.instructions && result.instructions.length > 0) { console.log(''); for (const instruction of result.instructions) { console.log(instruction); } + } else if (result.zshrcConfigured) { + console.log(''); + console.log('Restart your shell or run: exec zsh'); } } else { console.error(`✗ ${result.message}`); diff --git a/src/core/completions/command-registry.ts b/src/core/completions/command-registry.ts index 9466b005..fc4b67ea 100644 --- a/src/core/completions/command-registry.ts +++ b/src/core/completions/command-registry.ts @@ -1,4 +1,32 @@ -import { CommandDefinition } from './types.js'; +import { CommandDefinition, FlagDefinition } from './types.js'; + +/** + * Common flags used across multiple commands + */ +const COMMON_FLAGS = { + json: { + name: 'json', + description: 'Output as JSON', + } as FlagDefinition, + jsonValidation: { + name: 'json', + description: 'Output validation results as JSON', + } as FlagDefinition, + strict: { + name: 'strict', + description: 'Enable strict validation mode', + } as FlagDefinition, + noInteractive: { + name: 'no-interactive', + description: 'Disable interactive prompts', + } as FlagDefinition, + type: { + name: 'type', + description: 'Specify item type when ambiguous', + takesValue: true, + values: ['change', 'spec'], + } as FlagDefinition, +} as const; /** * Registry of all OpenSpec CLI commands with their flags and metadata. @@ -62,29 +90,15 @@ export const COMMAND_REGISTRY: CommandDefinition[] = [ name: 'specs', description: 'Validate all specs', }, - { - name: 'type', - description: 'Specify item type when ambiguous', - takesValue: true, - values: ['change', 'spec'], - }, - { - name: 'strict', - description: 'Enable strict validation mode', - }, - { - name: 'json', - description: 'Output validation results as JSON', - }, + COMMON_FLAGS.type, + COMMON_FLAGS.strict, + COMMON_FLAGS.jsonValidation, { name: 'concurrency', description: 'Max concurrent validations (defaults to env OPENSPEC_CONCURRENCY or 6)', takesValue: true, }, - { - name: 'no-interactive', - description: 'Disable interactive prompts', - }, + COMMON_FLAGS.noInteractive, ], }, { @@ -93,20 +107,9 @@ export const COMMAND_REGISTRY: CommandDefinition[] = [ acceptsPositional: true, positionalType: 'change-or-spec-id', flags: [ - { - name: 'json', - description: 'Output as JSON', - }, - { - name: 'type', - description: 'Specify item type when ambiguous', - takesValue: true, - values: ['change', 'spec'], - }, - { - name: 'no-interactive', - description: 'Disable interactive prompts', - }, + COMMON_FLAGS.json, + COMMON_FLAGS.type, + COMMON_FLAGS.noInteractive, { name: 'deltas-only', description: 'Show only deltas (JSON only, change-specific)', @@ -163,10 +166,7 @@ export const COMMAND_REGISTRY: CommandDefinition[] = [ acceptsPositional: true, positionalType: 'change-id', flags: [ - { - name: 'json', - description: 'Output as JSON', - }, + COMMON_FLAGS.json, { name: 'deltas-only', description: 'Show only deltas (JSON only)', @@ -175,20 +175,14 @@ export const COMMAND_REGISTRY: CommandDefinition[] = [ name: 'requirements-only', description: 'Alias for --deltas-only (deprecated)', }, - { - name: 'no-interactive', - description: 'Disable interactive prompts', - }, + COMMON_FLAGS.noInteractive, ], }, { name: 'list', description: 'List all active changes (deprecated)', flags: [ - { - name: 'json', - description: 'Output as JSON', - }, + COMMON_FLAGS.json, { name: 'long', description: 'Show id and title with counts', @@ -201,18 +195,9 @@ export const COMMAND_REGISTRY: CommandDefinition[] = [ acceptsPositional: true, positionalType: 'change-id', flags: [ - { - name: 'strict', - description: 'Enable strict validation mode', - }, - { - name: 'json', - description: 'Output validation report as JSON', - }, - { - name: 'no-interactive', - description: 'Disable interactive prompts', - }, + COMMON_FLAGS.strict, + COMMON_FLAGS.jsonValidation, + COMMON_FLAGS.noInteractive, ], }, ], @@ -228,10 +213,7 @@ export const COMMAND_REGISTRY: CommandDefinition[] = [ acceptsPositional: true, positionalType: 'spec-id', flags: [ - { - name: 'json', - description: 'Output as JSON', - }, + COMMON_FLAGS.json, { name: 'requirements', description: 'Show only requirements, exclude scenarios (JSON only)', @@ -246,20 +228,14 @@ export const COMMAND_REGISTRY: CommandDefinition[] = [ description: 'Show specific requirement by ID (JSON only)', takesValue: true, }, - { - name: 'no-interactive', - description: 'Disable interactive prompts', - }, + COMMON_FLAGS.noInteractive, ], }, { name: 'list', description: 'List all specifications', flags: [ - { - name: 'json', - description: 'Output as JSON', - }, + COMMON_FLAGS.json, { name: 'long', description: 'Show id and title with counts', @@ -272,18 +248,9 @@ export const COMMAND_REGISTRY: CommandDefinition[] = [ acceptsPositional: true, positionalType: 'spec-id', flags: [ - { - name: 'strict', - description: 'Enable strict validation mode', - }, - { - name: 'json', - description: 'Output validation report as JSON', - }, - { - name: 'no-interactive', - description: 'Disable interactive prompts', - }, + COMMON_FLAGS.strict, + COMMON_FLAGS.jsonValidation, + COMMON_FLAGS.noInteractive, ], }, ], diff --git a/src/core/completions/factory.ts b/src/core/completions/factory.ts index add3d497..50e9a69b 100644 --- a/src/core/completions/factory.ts +++ b/src/core/completions/factory.ts @@ -19,6 +19,8 @@ export type { InstallationResult }; * This design makes it easy to add support for additional shells */ export class CompletionFactory { + private static readonly SUPPORTED_SHELLS: SupportedShell[] = ['zsh']; + /** * Create a completion generator for the specified shell * @@ -58,7 +60,7 @@ export class CompletionFactory { * @returns true if the shell is supported */ static isSupported(shell: string): shell is SupportedShell { - return shell === 'zsh'; + return this.SUPPORTED_SHELLS.includes(shell as SupportedShell); } /** @@ -67,6 +69,6 @@ export class CompletionFactory { * @returns Array of supported shell names */ static getSupportedShells(): SupportedShell[] { - return ['zsh']; + return [...this.SUPPORTED_SHELLS]; } } diff --git a/src/core/completions/generators/zsh-generator.ts b/src/core/completions/generators/zsh-generator.ts index c413266a..b886f866 100644 --- a/src/core/completions/generators/zsh-generator.ts +++ b/src/core/completions/generators/zsh-generator.ts @@ -82,42 +82,92 @@ export class ZshGenerator implements CompletionGenerator { return script.join('\n'); } + /** + * Generate a single completion function + * + * @param functionName - Name of the completion function + * @param varName - Name of the local array variable + * @param varLabel - Label for the completion items + * @param commandLines - Command line(s) to populate the array + * @param comment - Optional comment describing the function + */ + private generateCompletionFunction( + functionName: string, + varName: string, + varLabel: string, + commandLines: string[], + comment?: string + ): string[] { + const lines: string[] = []; + + if (comment) { + lines.push(comment); + } + + lines.push(`${functionName}() {`); + lines.push(` local -a ${varName}`); + + if (commandLines.length === 1) { + lines.push(` ${commandLines[0]}`); + } else { + lines.push(` ${varName}=(`); + for (let i = 0; i < commandLines.length; i++) { + const suffix = i < commandLines.length - 1 ? ' \\' : ''; + lines.push(` ${commandLines[i]}${suffix}`); + } + lines.push(' )'); + } + + lines.push(` _describe "${varLabel}" ${varName}`); + lines.push('}'); + lines.push(''); + + return lines; + } + /** * Generate dynamic completion helper functions for change and spec IDs */ private generateDynamicCompletionHelpers(): string[] { const lines: string[] = []; - // Helper function for completing change IDs lines.push('# Dynamic completion helpers'); - lines.push('_openspec_complete_changes() {'); - lines.push(' local -a changes'); - lines.push(' # Use openspec list to get available changes'); - lines.push(' changes=(${(f)"$(openspec list --changes 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"})'); - lines.push(' _describe "change" changes'); - lines.push('}'); - lines.push(''); + + // Helper function for completing change IDs + lines.push( + ...this.generateCompletionFunction( + '_openspec_complete_changes', + 'changes', + 'change', + ['changes=(${(f)"$(openspec list --changes 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"})'], + '# Use openspec list to get available changes' + ) + ); // Helper function for completing spec IDs - lines.push('_openspec_complete_specs() {'); - lines.push(' local -a specs'); - lines.push(' # Use openspec list to get available specs'); - lines.push(' specs=(${(f)"$(openspec list --specs 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"})'); - lines.push(' _describe "spec" specs'); - lines.push('}'); - lines.push(''); + lines.push( + ...this.generateCompletionFunction( + '_openspec_complete_specs', + 'specs', + 'spec', + ['specs=(${(f)"$(openspec list --specs 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"})'], + '# Use openspec list to get available specs' + ) + ); // Helper function for completing both changes and specs - lines.push('_openspec_complete_items() {'); - lines.push(' local -a items'); - lines.push(' # Get both changes and specs'); - lines.push(' items=('); - lines.push(' ${(f)"$(openspec list --changes 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"} \\'); - lines.push(' ${(f)"$(openspec list --specs 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"}'); - lines.push(' )'); - lines.push(' _describe "item" items'); - lines.push('}'); - lines.push(''); + lines.push( + ...this.generateCompletionFunction( + '_openspec_complete_items', + 'items', + 'item', + [ + '${(f)"$(openspec list --changes 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"}', + '${(f)"$(openspec list --specs 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"}', + ], + '# Get both changes and specs' + ) + ); return lines; } @@ -266,10 +316,8 @@ export class ZshGenerator implements CompletionGenerator { } } - // Close the quote - if (!flag.short) { - parts.push("'"); - } + // Close the quote (needed for both short and long forms) + parts.push("'"); return parts.join(''); } diff --git a/src/core/completions/installers/zsh-installer.ts b/src/core/completions/installers/zsh-installer.ts index ef0ad38c..847107b6 100644 --- a/src/core/completions/installers/zsh-installer.ts +++ b/src/core/completions/installers/zsh-installer.ts @@ -1,6 +1,7 @@ import { promises as fs } from 'fs'; import path from 'path'; import os from 'os'; +import { FileSystemUtils } from '../../../utils/file-system.js'; /** * Installation result information @@ -10,6 +11,7 @@ export interface InstallationResult { installedPath?: string; backupPath?: string; isOhMyZsh: boolean; + zshrcConfigured?: boolean; message: string; instructions?: string[]; } @@ -21,6 +23,14 @@ export interface InstallationResult { export class ZshInstaller { private readonly homeDir: string; + /** + * Markers for .zshrc configuration management + */ + private readonly ZSHRC_MARKERS = { + start: '# OPENSPEC:START', + end: '# OPENSPEC:END', + }; + constructor(homeDir: string = os.homedir()) { this.homeDir = homeDir; } @@ -52,7 +62,7 @@ export class ZshInstaller { if (isOhMyZsh) { // Oh My Zsh custom completions directory return { - path: path.join(this.homeDir, '.oh-my-zsh', 'completions', '_openspec'), + path: path.join(this.homeDir, '.oh-my-zsh', 'custom', 'completions', '_openspec'), isOhMyZsh: true, }; } else { @@ -84,6 +94,138 @@ export class ZshInstaller { } } + /** + * Get the path to .zshrc file + * + * @returns Path to .zshrc + */ + private getZshrcPath(): string { + return path.join(this.homeDir, '.zshrc'); + } + + /** + * Generate .zshrc configuration content + * + * @param completionsDir - Directory containing completion scripts + * @returns Configuration content + */ + private generateZshrcConfig(completionsDir: string): string { + return [ + '# OpenSpec shell completions configuration', + `fpath=(${completionsDir} $fpath)`, + 'autoload -Uz compinit', + 'compinit', + ].join('\n'); + } + + /** + * Configure .zshrc to enable completions + * Only applies to standard Zsh (not Oh My Zsh) + * + * @param completionsDir - Directory containing completion scripts + * @returns true if configured successfully, false otherwise + */ + async configureZshrc(completionsDir: string): Promise { + // Check if auto-configuration is disabled + if (process.env.OPENSPEC_NO_AUTO_CONFIG === '1') { + return false; + } + + try { + const zshrcPath = this.getZshrcPath(); + const config = this.generateZshrcConfig(completionsDir); + + // Check write permissions + const canWrite = await FileSystemUtils.canWriteFile(zshrcPath); + if (!canWrite) { + return false; + } + + // Use marker-based update + await FileSystemUtils.updateFileWithMarkers( + zshrcPath, + config, + this.ZSHRC_MARKERS.start, + this.ZSHRC_MARKERS.end + ); + + return true; + } catch { + // Fail gracefully - don't break installation + return false; + } + } + + /** + * Check if .zshrc has OpenSpec configuration markers + * + * @returns true if .zshrc exists and has markers + */ + private async hasZshrcConfig(): Promise { + try { + const zshrcPath = this.getZshrcPath(); + const content = await fs.readFile(zshrcPath, 'utf-8'); + return content.includes(this.ZSHRC_MARKERS.start) && content.includes(this.ZSHRC_MARKERS.end); + } catch { + return false; + } + } + + /** + * Remove .zshrc configuration + * Used during uninstallation + * + * @returns true if removed successfully, false otherwise + */ + async removeZshrcConfig(): Promise { + try { + const zshrcPath = this.getZshrcPath(); + + // Check if file exists + try { + await fs.access(zshrcPath); + } catch { + // File doesn't exist, nothing to remove + return true; + } + + // Read file content + const content = await fs.readFile(zshrcPath, 'utf-8'); + + // Check if markers exist + if (!content.includes(this.ZSHRC_MARKERS.start) || !content.includes(this.ZSHRC_MARKERS.end)) { + // Markers don't exist, nothing to remove + return true; + } + + // Remove content between markers (including markers) + const lines = content.split('\n'); + const startIndex = lines.findIndex((line) => line.trim() === this.ZSHRC_MARKERS.start); + const endIndex = lines.findIndex((line) => line.trim() === this.ZSHRC_MARKERS.end); + + if (startIndex === -1 || endIndex === -1 || endIndex < startIndex) { + // Invalid marker placement + return false; + } + + // Remove lines between markers (inclusive) + lines.splice(startIndex, endIndex - startIndex + 1); + + // Remove trailing empty lines at the start if the markers were at the top + while (lines.length > 0 && lines[0].trim() === '') { + lines.shift(); + } + + // Write back + await fs.writeFile(zshrcPath, lines.join('\n'), 'utf-8'); + + return true; + } catch { + // Fail gracefully + return false; + } + } + /** * Install the completion script * @@ -104,17 +246,26 @@ export class ZshInstaller { // Write the completion script await fs.writeFile(targetPath, completionScript, 'utf-8'); - // Generate instructions - const instructions = this.generateInstructions(isOhMyZsh, targetPath); + // Auto-configure .zshrc for standard Zsh users + let zshrcConfigured = false; + if (!isOhMyZsh) { + zshrcConfigured = await this.configureZshrc(targetDir); + } + + // Generate instructions (only if .zshrc wasn't auto-configured) + const instructions = zshrcConfigured ? undefined : this.generateInstructions(isOhMyZsh, targetPath); return { success: true, installedPath: targetPath, backupPath, isOhMyZsh, + zshrcConfigured, message: isOhMyZsh ? 'Completion script installed successfully for Oh My Zsh' - : 'Completion script installed successfully for Zsh', + : zshrcConfigured + ? 'Completion script installed and .zshrc configured successfully' + : 'Completion script installed successfully for Zsh', instructions, }; } catch (error) { @@ -170,21 +321,47 @@ export class ZshInstaller { */ async uninstall(): Promise<{ success: boolean; message: string }> { try { - const { path: targetPath } = await this.getInstallationPath(); + const { path: targetPath, isOhMyZsh } = await this.getInstallationPath(); + // Try to remove completion script + let scriptRemoved = false; try { await fs.access(targetPath); await fs.unlink(targetPath); - return { - success: true, - message: `Completion script removed from ${targetPath}`, - }; + scriptRemoved = true; } catch { + // Script not installed + } + + // Try to remove .zshrc configuration (only for standard Zsh) + let zshrcWasPresent = false; + let zshrcCleaned = false; + if (!isOhMyZsh) { + zshrcWasPresent = await this.hasZshrcConfig(); + if (zshrcWasPresent) { + zshrcCleaned = await this.removeZshrcConfig(); + } + } + + if (!scriptRemoved && !zshrcWasPresent) { return { success: false, message: 'Completion script is not installed', }; } + + const messages: string[] = []; + if (scriptRemoved) { + messages.push(`Completion script removed from ${targetPath}`); + } + if (zshrcCleaned && !isOhMyZsh) { + messages.push('Removed OpenSpec configuration from ~/.zshrc'); + } + + return { + success: true, + message: messages.join('. '), + }; } catch (error) { return { success: false, diff --git a/test/core/completions/generators/zsh-generator.test.ts b/test/core/completions/generators/zsh-generator.test.ts index 3febdab8..415021f1 100644 --- a/test/core/completions/generators/zsh-generator.test.ts +++ b/test/core/completions/generators/zsh-generator.test.ts @@ -126,7 +126,7 @@ describe('ZshGenerator', () => { const script = generator.generate(commands); - expect(script).toContain("'(-r --requirement)'{-r,--requirement}'"); + expect(script).toContain("'(-r --requirement)'{-r,--requirement}'[Show specific requirement]:value:'"); expect(script).toContain('[Show specific requirement]'); }); diff --git a/test/core/completions/installers/zsh-installer.test.ts b/test/core/completions/installers/zsh-installer.test.ts index 9dfe1aa8..1841c22a 100644 --- a/test/core/completions/installers/zsh-installer.test.ts +++ b/test/core/completions/installers/zsh-installer.test.ts @@ -55,7 +55,7 @@ describe('ZshInstaller', () => { const result = await installer.getInstallationPath(); expect(result.isOhMyZsh).toBe(true); - expect(result.path).toBe(path.join(testHomeDir, '.oh-my-zsh', 'completions', '_openspec')); + expect(result.path).toBe(path.join(testHomeDir, '.oh-my-zsh', 'custom', 'completions', '_openspec')); }); it('should return standard Zsh path when Oh My Zsh is not installed', async () => { @@ -110,7 +110,7 @@ describe('ZshInstaller', () => { expect(result.success).toBe(true); expect(result.isOhMyZsh).toBe(true); - expect(result.installedPath).toBe(path.join(ohMyZshPath, 'completions', '_openspec')); + expect(result.installedPath).toBe(path.join(ohMyZshPath, 'custom', 'completions', '_openspec')); expect(result.message).toContain('Oh My Zsh'); // Verify file was created with correct content @@ -173,13 +173,23 @@ describe('ZshInstaller', () => { expect(result.instructions!.join(' ')).toContain('automatically'); }); - it('should include fpath instructions for standard Zsh', async () => { + it('should include fpath instructions for standard Zsh when auto-config is disabled', async () => { + const originalEnv = process.env.OPENSPEC_NO_AUTO_CONFIG; + process.env.OPENSPEC_NO_AUTO_CONFIG = '1'; + const result = await installer.install(testScript); expect(result.instructions).toBeDefined(); expect(result.instructions!.join('\n')).toContain('fpath'); expect(result.instructions!.join('\n')).toContain('.zshrc'); expect(result.instructions!.join('\n')).toContain('compinit'); + + // Restore env + if (originalEnv === undefined) { + delete process.env.OPENSPEC_NO_AUTO_CONFIG; + } else { + process.env.OPENSPEC_NO_AUTO_CONFIG = originalEnv; + } }); it('should handle installation errors gracefully', async () => { @@ -215,7 +225,8 @@ describe('ZshInstaller', () => { expect(afterUninstall).toBe(false); }); - it('should return failure when script is not installed', async () => { + it('should return failure when script and .zshrc config are not installed', async () => { + // Don't create .zshrc or completion script - nothing to remove const result = await installer.uninstall(); expect(result.success).toBe(false); @@ -231,7 +242,7 @@ describe('ZshInstaller', () => { const result = await installer.uninstall(); expect(result.success).toBe(true); - expect(result.message).toContain(path.join('.oh-my-zsh', 'completions', '_openspec')); + expect(result.message).toContain(path.join('.oh-my-zsh', 'custom', 'completions', '_openspec')); }); }); @@ -308,4 +319,348 @@ describe('ZshInstaller', () => { expect(defaultInstaller).toBeDefined(); }); }); + + describe('configureZshrc', () => { + const completionsDir = '/test/.zsh/completions'; + + it('should create .zshrc with markers and config when file does not exist', async () => { + const result = await installer.configureZshrc(completionsDir); + + expect(result).toBe(true); + + const zshrcPath = path.join(testHomeDir, '.zshrc'); + const content = await fs.readFile(zshrcPath, 'utf-8'); + + expect(content).toContain('# OPENSPEC:START'); + expect(content).toContain('# OPENSPEC:END'); + expect(content).toContain('# OpenSpec shell completions configuration'); + expect(content).toContain(`fpath=(${completionsDir} $fpath)`); + expect(content).toContain('autoload -Uz compinit'); + expect(content).toContain('compinit'); + }); + + it('should prepend markers and config when .zshrc exists without markers', async () => { + const zshrcPath = path.join(testHomeDir, '.zshrc'); + await fs.writeFile(zshrcPath, '# My custom zsh config\nalias ll="ls -la"\n'); + + const result = await installer.configureZshrc(completionsDir); + + expect(result).toBe(true); + + const content = await fs.readFile(zshrcPath, 'utf-8'); + + expect(content).toContain('# OPENSPEC:START'); + expect(content).toContain('# OPENSPEC:END'); + expect(content).toContain('# My custom zsh config'); + expect(content).toContain('alias ll="ls -la"'); + + // Config should be before existing content + const configIndex = content.indexOf('# OPENSPEC:START'); + const aliasIndex = content.indexOf('alias ll'); + expect(configIndex).toBeLessThan(aliasIndex); + }); + + it('should update config between markers when .zshrc has existing markers', async () => { + const zshrcPath = path.join(testHomeDir, '.zshrc'); + const initialContent = [ + '# OPENSPEC:START', + '# Old config', + 'fpath=(/old/path $fpath)', + '# OPENSPEC:END', + '', + '# My custom config', + ].join('\n'); + + await fs.writeFile(zshrcPath, initialContent); + + const result = await installer.configureZshrc(completionsDir); + + expect(result).toBe(true); + + const content = await fs.readFile(zshrcPath, 'utf-8'); + + expect(content).toContain('# OPENSPEC:START'); + expect(content).toContain('# OPENSPEC:END'); + expect(content).toContain(`fpath=(${completionsDir} $fpath)`); + expect(content).not.toContain('# Old config'); + expect(content).not.toContain('/old/path'); + expect(content).toContain('# My custom config'); + }); + + it('should preserve user content outside markers', async () => { + const zshrcPath = path.join(testHomeDir, '.zshrc'); + const userContent = [ + '# My zsh config', + 'export PATH="/custom/path:$PATH"', + '', + '# OPENSPEC:START', + '# Old OpenSpec config', + '# OPENSPEC:END', + '', + 'alias ls="ls -G"', + ].join('\n'); + + await fs.writeFile(zshrcPath, userContent); + + const result = await installer.configureZshrc(completionsDir); + + expect(result).toBe(true); + + const content = await fs.readFile(zshrcPath, 'utf-8'); + + expect(content).toContain('# My zsh config'); + expect(content).toContain('export PATH="/custom/path:$PATH"'); + expect(content).toContain('alias ls="ls -G"'); + expect(content).toContain(`fpath=(${completionsDir} $fpath)`); + expect(content).not.toContain('# Old OpenSpec config'); + }); + + it('should return false when OPENSPEC_NO_AUTO_CONFIG is set', async () => { + const originalEnv = process.env.OPENSPEC_NO_AUTO_CONFIG; + process.env.OPENSPEC_NO_AUTO_CONFIG = '1'; + + const result = await installer.configureZshrc(completionsDir); + + expect(result).toBe(false); + + const zshrcPath = path.join(testHomeDir, '.zshrc'); + const exists = await fs.access(zshrcPath).then(() => true).catch(() => false); + expect(exists).toBe(false); + + // Restore env + if (originalEnv === undefined) { + delete process.env.OPENSPEC_NO_AUTO_CONFIG; + } else { + process.env.OPENSPEC_NO_AUTO_CONFIG = originalEnv; + } + }); + + it('should handle write permission errors gracefully', async () => { + // Create installer with path that can't be written + const invalidInstaller = new ZshInstaller('/root/invalid/path'); + + const result = await invalidInstaller.configureZshrc(completionsDir); + + expect(result).toBe(false); + }); + }); + + describe('removeZshrcConfig', () => { + it('should return true when .zshrc does not exist', async () => { + const result = await installer.removeZshrcConfig(); + expect(result).toBe(true); + }); + + it('should return true when .zshrc exists but has no markers', async () => { + const zshrcPath = path.join(testHomeDir, '.zshrc'); + await fs.writeFile(zshrcPath, '# My custom config\nalias ll="ls -la"\n'); + + const result = await installer.removeZshrcConfig(); + + expect(result).toBe(true); + + // Content should be unchanged + const content = await fs.readFile(zshrcPath, 'utf-8'); + expect(content).toBe('# My custom config\nalias ll="ls -la"\n'); + }); + + it('should remove markers and config when present', async () => { + const zshrcPath = path.join(testHomeDir, '.zshrc'); + const content = [ + '# My config', + '', + '# OPENSPEC:START', + '# OpenSpec shell completions configuration', + 'fpath=(~/.zsh/completions $fpath)', + 'autoload -Uz compinit', + 'compinit', + '# OPENSPEC:END', + '', + 'alias ll="ls -la"', + ].join('\n'); + + await fs.writeFile(zshrcPath, content); + + const result = await installer.removeZshrcConfig(); + + expect(result).toBe(true); + + const newContent = await fs.readFile(zshrcPath, 'utf-8'); + + expect(newContent).not.toContain('# OPENSPEC:START'); + expect(newContent).not.toContain('# OPENSPEC:END'); + expect(newContent).not.toContain('OpenSpec shell completions'); + expect(newContent).toContain('# My config'); + expect(newContent).toContain('alias ll="ls -la"'); + }); + + it('should remove leading empty lines when markers were at top', async () => { + const zshrcPath = path.join(testHomeDir, '.zshrc'); + const content = [ + '# OPENSPEC:START', + '# OpenSpec config', + '# OPENSPEC:END', + '', + '# User config below', + ].join('\n'); + + await fs.writeFile(zshrcPath, content); + + const result = await installer.removeZshrcConfig(); + + expect(result).toBe(true); + + const newContent = await fs.readFile(zshrcPath, 'utf-8'); + + // Should not start with empty lines + expect(newContent).toBe('# User config below'); + }); + + it('should handle invalid marker placement gracefully', async () => { + const zshrcPath = path.join(testHomeDir, '.zshrc'); + + // End marker before start marker + await fs.writeFile(zshrcPath, '# OPENSPEC:END\n# OPENSPEC:START\n'); + + const result = await installer.removeZshrcConfig(); + + expect(result).toBe(false); + }); + + it('should return true when only one marker is present', async () => { + const zshrcPath = path.join(testHomeDir, '.zshrc'); + await fs.writeFile(zshrcPath, '# OPENSPEC:START\nsome config\n'); + + const result = await installer.removeZshrcConfig(); + + // Should return true (markers don't exist as a pair) + expect(result).toBe(true); + }); + }); + + describe('install with .zshrc auto-configuration', () => { + const testScript = '#compdef openspec\n_openspec() {}\n'; + + it('should auto-configure .zshrc for standard Zsh', async () => { + const result = await installer.install(testScript); + + expect(result.success).toBe(true); + expect(result.zshrcConfigured).toBe(true); + + // Verify .zshrc was created + const zshrcPath = path.join(testHomeDir, '.zshrc'); + const content = await fs.readFile(zshrcPath, 'utf-8'); + + expect(content).toContain('# OPENSPEC:START'); + expect(content).toContain('fpath='); + expect(content).toContain('compinit'); + }); + + it('should not configure .zshrc for Oh My Zsh users', async () => { + const ohMyZshPath = path.join(testHomeDir, '.oh-my-zsh'); + await fs.mkdir(ohMyZshPath, { recursive: true }); + + const result = await installer.install(testScript); + + expect(result.success).toBe(true); + expect(result.isOhMyZsh).toBe(true); + expect(result.zshrcConfigured).toBe(false); + + // Verify .zshrc was not created + const zshrcPath = path.join(testHomeDir, '.zshrc'); + const exists = await fs.access(zshrcPath).then(() => true).catch(() => false); + expect(exists).toBe(false); + }); + + it('should not include manual instructions when .zshrc was auto-configured', async () => { + const result = await installer.install(testScript); + + expect(result.success).toBe(true); + expect(result.zshrcConfigured).toBe(true); + expect(result.instructions).toBeUndefined(); + }); + + it('should include instructions when .zshrc auto-config fails', async () => { + const originalEnv = process.env.OPENSPEC_NO_AUTO_CONFIG; + process.env.OPENSPEC_NO_AUTO_CONFIG = '1'; + + const result = await installer.install(testScript); + + expect(result.success).toBe(true); + expect(result.zshrcConfigured).toBe(false); + expect(result.instructions).toBeDefined(); + expect(result.instructions!.join('\n')).toContain('fpath'); + + // Restore env + if (originalEnv === undefined) { + delete process.env.OPENSPEC_NO_AUTO_CONFIG; + } else { + process.env.OPENSPEC_NO_AUTO_CONFIG = originalEnv; + } + }); + + it('should update success message when .zshrc is configured', async () => { + const result = await installer.install(testScript); + + expect(result.success).toBe(true); + expect(result.message).toContain('.zshrc configured'); + }); + }); + + describe('uninstall with .zshrc cleanup', () => { + const testScript = '#compdef openspec\n_openspec() {}\n'; + + it('should remove .zshrc config when uninstalling', async () => { + // Install first (which creates .zshrc config) + await installer.install(testScript); + + // Verify .zshrc was configured + const zshrcPath = path.join(testHomeDir, '.zshrc'); + let content = await fs.readFile(zshrcPath, 'utf-8'); + expect(content).toContain('# OPENSPEC:START'); + + // Uninstall + const result = await installer.uninstall(); + + expect(result.success).toBe(true); + expect(result.message).toContain('Removed OpenSpec configuration from ~/.zshrc'); + + // Verify .zshrc config was removed + content = await fs.readFile(zshrcPath, 'utf-8'); + expect(content).not.toContain('# OPENSPEC:START'); + }); + + it('should not remove .zshrc config for Oh My Zsh users', async () => { + const ohMyZshPath = path.join(testHomeDir, '.oh-my-zsh'); + await fs.mkdir(ohMyZshPath, { recursive: true }); + + await installer.install(testScript); + + const result = await installer.uninstall(); + + expect(result.success).toBe(true); + expect(result.message).not.toContain('.zshrc'); + }); + + it('should succeed even if only .zshrc config is removed', async () => { + // Manually create .zshrc config without installing completion script + const zshrcPath = path.join(testHomeDir, '.zshrc'); + await fs.writeFile(zshrcPath, '# OPENSPEC:START\nconfig\n# OPENSPEC:END\n'); + + const result = await installer.uninstall(); + + expect(result.success).toBe(true); + expect(result.message).toContain('Removed OpenSpec configuration from ~/.zshrc'); + }); + + it('should include both messages when removing script and .zshrc', async () => { + await installer.install(testScript); + + const result = await installer.uninstall(); + + expect(result.success).toBe(true); + expect(result.message).toContain('Completion script removed'); + expect(result.message).toContain('Removed OpenSpec configuration from ~/.zshrc'); + }); + }); }); From b0bb9ad1c00d1af51cbe0a3e0ab43108dbc85c91 Mon Sep 17 00:00:00 2001 From: noam-home Date: Thu, 6 Nov 2025 20:25:32 +0200 Subject: [PATCH 3/6] expose only postinstall.js script --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index af290350..dd9253f4 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "files": [ "dist", "bin", - "scripts", + "scripts/postinstall.js", "!dist/**/*.test.js", "!dist/**/__tests__", "!dist/**/*.map" From b53f12d6fa1136c86593734d56bc454f495436c4 Mon Sep 17 00:00:00 2001 From: noam-home Date: Thu, 6 Nov 2025 20:33:37 +0200 Subject: [PATCH 4/6] Replace _openspec "$@" with compdef in zsh-generator.ts to prevent execution during load --- src/core/completions/generators/zsh-generator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/completions/generators/zsh-generator.ts b/src/core/completions/generators/zsh-generator.ts index b886f866..87f4c930 100644 --- a/src/core/completions/generators/zsh-generator.ts +++ b/src/core/completions/generators/zsh-generator.ts @@ -76,7 +76,7 @@ export class ZshGenerator implements CompletionGenerator { script.push(...this.generateDynamicCompletionHelpers()); // Register the completion function - script.push('_openspec "$@"'); + script.push('compdef _openspec openspec'); script.push(''); return script.join('\n'); From ce19aacbc78530a951cd0f2c5dfcf0ac3b6489e8 Mon Sep 17 00:00:00 2001 From: Noam Meron <69599972+noameron@users.noreply.github.com> Date: Thu, 6 Nov 2025 20:39:08 +0200 Subject: [PATCH 5/6] Update test/commands/completion.test.ts Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- test/commands/completion.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/commands/completion.test.ts b/test/commands/completion.test.ts index d474daf1..0e81ecc1 100644 --- a/test/commands/completion.test.ts +++ b/test/commands/completion.test.ts @@ -189,7 +189,7 @@ describe('CompletionCommand', () => { expect(consoleErrorSpy).toHaveBeenCalledWith( "Error: Shell 'powershell' is not supported yet. Currently supported: zsh" ); - process.exitCode = 1; + expect(process.exitCode).toBe(1); }); }); From e9789460015980da1b56a4eeae5ea9b1719dc9c2 Mon Sep 17 00:00:00 2001 From: noam-home Date: Thu, 6 Nov 2025 21:27:10 +0200 Subject: [PATCH 6/6] - Fix dispatcher to use $words instead of $line for subcommand routing - Add __complete endpoint with tab-separated output for safe parsing - Replace brittle awk parsing with __complete in completion helpers - Add uninstall confirmation prompt with --yes flag to skip - Prefer $ZSH env var for Oh My Zsh detection before dir check - Add fpath verification guidance for OMZ installations - Update cli-completion spec to document generate subcommand --- src/cli/index.ts | 19 ++++- src/commands/completion.ts | 75 ++++++++++++++++++- .../completions/generators/zsh-generator.ts | 65 ++++++++-------- .../completions/installers/zsh-installer.ts | 34 ++++++++- src/utils/item-discovery.ts | 21 ++++++ test/commands/completion.test.ts | 10 +-- .../generators/zsh-generator.test.ts | 4 +- 7 files changed, 182 insertions(+), 46 deletions(-) diff --git a/src/cli/index.ts b/src/cli/index.ts index cc450682..0cd5885f 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -288,10 +288,11 @@ completionCmd completionCmd .command('uninstall [shell]') .description('Uninstall completion script for a shell') - .action(async (shell?: string) => { + .option('-y, --yes', 'Skip confirmation prompts') + .action(async (shell?: string, options?: { yes?: boolean }) => { try { const completionCommand = new CompletionCommand(); - await completionCommand.uninstall({ shell }); + await completionCommand.uninstall({ shell, yes: options?.yes }); } catch (error) { console.log(); ora().fail(`Error: ${(error as Error).message}`); @@ -299,4 +300,18 @@ completionCmd } }); +// Hidden command for machine-readable completion data +program + .command('__complete ', { hidden: true }) + .description('Output completion data in machine-readable format (internal use)') + .action(async (type: string) => { + try { + const completionCommand = new CompletionCommand(); + await completionCommand.complete({ type }); + } catch (error) { + // Silently fail for graceful shell completion experience + process.exitCode = 1; + } + }); + program.parse(); diff --git a/src/commands/completion.ts b/src/commands/completion.ts index 359601ae..bf092d8e 100644 --- a/src/commands/completion.ts +++ b/src/commands/completion.ts @@ -1,7 +1,10 @@ import ora from 'ora'; +import { confirm } from '@inquirer/prompts'; import { CompletionFactory } from '../core/completions/factory.js'; import { COMMAND_REGISTRY } from '../core/completions/command-registry.js'; import { detectShell, SupportedShell } from '../utils/shell-detection.js'; +import { CompletionProvider } from '../core/completions/completion-provider.js'; +import { getArchivedChangeIds } from '../utils/item-discovery.js'; interface GenerateOptions { shell?: string; @@ -14,12 +17,22 @@ interface InstallOptions { interface UninstallOptions { shell?: string; + yes?: boolean; +} + +interface CompleteOptions { + type: string; } /** * Command for managing shell completions for OpenSpec CLI */ export class CompletionCommand { + private completionProvider: CompletionProvider; + + constructor() { + this.completionProvider = new CompletionProvider(); + } /** * Resolve shell parameter or exit with error * @@ -80,13 +93,13 @@ export class CompletionCommand { /** * Uninstall completion script from the installation location * - * @param options - Options for uninstallation (shell type) + * @param options - Options for uninstallation (shell type, yes flag) */ async uninstall(options: UninstallOptions = {}): Promise { const shell = this.resolveShellOrExit(options.shell, 'uninstall'); if (!shell) return; - await this.uninstallForShell(shell); + await this.uninstallForShell(shell, options.yes || false); } /** @@ -153,9 +166,22 @@ export class CompletionCommand { /** * Uninstall completion script for a specific shell */ - private async uninstallForShell(shell: SupportedShell): Promise { + private async uninstallForShell(shell: SupportedShell, skipConfirmation: boolean): Promise { const installer = CompletionFactory.createInstaller(shell); + // Prompt for confirmation unless --yes flag is provided + if (!skipConfirmation) { + const confirmed = await confirm({ + message: 'Remove OpenSpec configuration from ~/.zshrc?', + default: false, + }); + + if (!confirmed) { + console.log('Uninstall cancelled.'); + return; + } + } + const spinner = ora(`Uninstalling ${shell} completion script...`).start(); try { @@ -176,6 +202,49 @@ export class CompletionCommand { } } + /** + * Output machine-readable completion data for shell consumption + * Format: tab-separated "id\tdescription" per line + * + * @param options - Options specifying completion type + */ + async complete(options: CompleteOptions): Promise { + const type = options.type.toLowerCase(); + + try { + switch (type) { + case 'changes': { + const changeIds = await this.completionProvider.getChangeIds(); + for (const id of changeIds) { + console.log(`${id}\tactive change`); + } + break; + } + case 'specs': { + const specIds = await this.completionProvider.getSpecIds(); + for (const id of specIds) { + console.log(`${id}\tspecification`); + } + break; + } + case 'archived-changes': { + const archivedIds = await getArchivedChangeIds(); + for (const id of archivedIds) { + console.log(`${id}\tarchived change`); + } + break; + } + default: + // Invalid type - silently exit with no output for graceful shell completion failure + process.exitCode = 1; + break; + } + } catch { + // Silently fail for graceful shell completion experience + process.exitCode = 1; + } + } + /** * Normalize shell parameter to lowercase */ diff --git a/src/core/completions/generators/zsh-generator.ts b/src/core/completions/generators/zsh-generator.ts index 87f4c930..765fd5b4 100644 --- a/src/core/completions/generators/zsh-generator.ts +++ b/src/core/completions/generators/zsh-generator.ts @@ -51,7 +51,7 @@ export class ZshGenerator implements CompletionGenerator { script.push(' _describe "openspec command" commands'); script.push(' ;;'); script.push(' args)'); - script.push(' case $line[1] in'); + script.push(' case $words[1] in'); // Generate completion for each command for (const cmd of commands) { @@ -132,42 +132,43 @@ export class ZshGenerator implements CompletionGenerator { const lines: string[] = []; lines.push('# Dynamic completion helpers'); + lines.push(''); // Helper function for completing change IDs - lines.push( - ...this.generateCompletionFunction( - '_openspec_complete_changes', - 'changes', - 'change', - ['changes=(${(f)"$(openspec list --changes 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"})'], - '# Use openspec list to get available changes' - ) - ); + lines.push('# Use openspec __complete to get available changes'); + lines.push('_openspec_complete_changes() {'); + lines.push(' local -a changes'); + lines.push(' while IFS=$\'\\t\' read -r id desc; do'); + lines.push(' changes+=("$id:$desc")'); + lines.push(' done < <(openspec __complete changes 2>/dev/null)'); + lines.push(' _describe "change" changes'); + lines.push('}'); + lines.push(''); // Helper function for completing spec IDs - lines.push( - ...this.generateCompletionFunction( - '_openspec_complete_specs', - 'specs', - 'spec', - ['specs=(${(f)"$(openspec list --specs 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"})'], - '# Use openspec list to get available specs' - ) - ); + lines.push('# Use openspec __complete to get available specs'); + lines.push('_openspec_complete_specs() {'); + lines.push(' local -a specs'); + lines.push(' while IFS=$\'\\t\' read -r id desc; do'); + lines.push(' specs+=("$id:$desc")'); + lines.push(' done < <(openspec __complete specs 2>/dev/null)'); + lines.push(' _describe "spec" specs'); + lines.push('}'); + lines.push(''); // Helper function for completing both changes and specs - lines.push( - ...this.generateCompletionFunction( - '_openspec_complete_items', - 'items', - 'item', - [ - '${(f)"$(openspec list --changes 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"}', - '${(f)"$(openspec list --specs 2>/dev/null | tail -n +2 | awk \'{print $1":"$2}\')"}', - ], - '# Get both changes and specs' - ) - ); + lines.push('# Get both changes and specs'); + lines.push('_openspec_complete_items() {'); + lines.push(' local -a items'); + lines.push(' while IFS=$\'\\t\' read -r id desc; do'); + lines.push(' items+=("$id:$desc")'); + lines.push(' done < <(openspec __complete changes 2>/dev/null)'); + lines.push(' while IFS=$\'\\t\' read -r id desc; do'); + lines.push(' items+=("$id:$desc")'); + lines.push(' done < <(openspec __complete specs 2>/dev/null)'); + lines.push(' _describe "item" items'); + lines.push('}'); + lines.push(''); return lines; } @@ -211,7 +212,7 @@ export class ZshGenerator implements CompletionGenerator { lines.push(' _describe "subcommand" subcommands'); lines.push(' ;;'); lines.push(' args)'); - lines.push(' case $line[1] in'); + lines.push(' case $words[1] in'); for (const subcmd of cmd.subcommands) { lines.push(` ${subcmd.name})`); diff --git a/src/core/completions/installers/zsh-installer.ts b/src/core/completions/installers/zsh-installer.ts index 847107b6..30a704da 100644 --- a/src/core/completions/installers/zsh-installer.ts +++ b/src/core/completions/installers/zsh-installer.ts @@ -38,9 +38,15 @@ export class ZshInstaller { /** * Check if Oh My Zsh is installed * - * @returns true if Oh My Zsh directory exists + * @returns true if Oh My Zsh is detected via $ZSH env var or directory exists */ async isOhMyZshInstalled(): Promise { + // First check for $ZSH environment variable (standard OMZ setup) + if (process.env.ZSH) { + return true; + } + + // Fall back to checking for ~/.oh-my-zsh directory const ohMyZshPath = path.join(this.homeDir, '.oh-my-zsh'); try { @@ -253,7 +259,15 @@ export class ZshInstaller { } // Generate instructions (only if .zshrc wasn't auto-configured) - const instructions = zshrcConfigured ? undefined : this.generateInstructions(isOhMyZsh, targetPath); + let instructions = zshrcConfigured ? undefined : this.generateInstructions(isOhMyZsh, targetPath); + + // Add fpath guidance for Oh My Zsh installations + if (isOhMyZsh) { + const fpathGuidance = this.generateOhMyZshFpathGuidance(targetDir); + if (fpathGuidance) { + instructions = instructions ? [...instructions, '', ...fpathGuidance] : fpathGuidance; + } + } return { success: true, @@ -277,6 +291,22 @@ export class ZshInstaller { } } + /** + * Generate Oh My Zsh fpath verification guidance + * + * @param completionsDir - Custom completions directory path + * @returns Array of guidance strings, or undefined if not needed + */ + private generateOhMyZshFpathGuidance(completionsDir: string): string[] | undefined { + return [ + 'Note: Oh My Zsh typically auto-loads completions from custom/completions.', + `Verify that ${completionsDir} is in your fpath by running:`, + ' echo $fpath | grep "custom/completions"', + '', + 'If not found, completions may not work. Restart your shell to ensure changes take effect.', + ]; + } + /** * Generate user instructions for enabling completions * diff --git a/src/utils/item-discovery.ts b/src/utils/item-discovery.ts index b6008378..1a86c3ae 100644 --- a/src/utils/item-discovery.ts +++ b/src/utils/item-discovery.ts @@ -43,3 +43,24 @@ export async function getSpecIds(root: string = process.cwd()): Promise { + const archivePath = path.join(root, 'openspec', 'changes', 'archive'); + try { + const entries = await fs.readdir(archivePath, { withFileTypes: true }); + const result: string[] = []; + for (const entry of entries) { + if (!entry.isDirectory() || entry.name.startsWith('.')) continue; + const proposalPath = path.join(archivePath, entry.name, 'proposal.md'); + try { + await fs.access(proposalPath); + result.push(entry.name); + } catch { + // skip directories without proposal.md + } + } + return result.sort(); + } catch { + return []; + } +} + diff --git a/test/commands/completion.test.ts b/test/commands/completion.test.ts index 0e81ecc1..093d0d36 100644 --- a/test/commands/completion.test.ts +++ b/test/commands/completion.test.ts @@ -154,7 +154,7 @@ describe('CompletionCommand', () => { describe('uninstall subcommand', () => { it('should uninstall Zsh completion script', async () => { - await command.uninstall({ shell: 'zsh' }); + await command.uninstall({ shell: 'zsh', yes: true }); expect(consoleLogSpy).toHaveBeenCalledWith( expect.stringContaining('Completion script removed') @@ -165,7 +165,7 @@ describe('CompletionCommand', () => { it('should auto-detect Zsh shell when no shell specified', async () => { vi.mocked(shellDetection.detectShell).mockReturnValue('zsh'); - await command.uninstall({}); + await command.uninstall({ yes: true }); expect(consoleLogSpy).toHaveBeenCalledWith( expect.stringContaining('Completion script removed') @@ -175,7 +175,7 @@ describe('CompletionCommand', () => { it('should show error when shell cannot be auto-detected', async () => { vi.mocked(shellDetection.detectShell).mockReturnValue(undefined); - await command.uninstall({}); + await command.uninstall({ yes: true }); expect(consoleErrorSpy).toHaveBeenCalledWith( 'Error: Could not auto-detect shell. Please specify shell explicitly.' @@ -184,7 +184,7 @@ describe('CompletionCommand', () => { }); it('should show error for unsupported shell', async () => { - await command.uninstall({ shell: 'powershell' }); + await command.uninstall({ shell: 'powershell', yes: true }); expect(consoleErrorSpy).toHaveBeenCalledWith( "Error: Shell 'powershell' is not supported yet. Currently supported: zsh" @@ -235,7 +235,7 @@ describe('CompletionCommand', () => { } as any)); const cmd = new CompletionCommand(); - await cmd.uninstall({ shell: 'zsh' }); + await cmd.uninstall({ shell: 'zsh', yes: true }); expect(consoleErrorSpy).toHaveBeenCalledWith( expect.stringContaining('Completion script is not installed') diff --git a/test/core/completions/generators/zsh-generator.test.ts b/test/core/completions/generators/zsh-generator.test.ts index 415021f1..74bef2ac 100644 --- a/test/core/completions/generators/zsh-generator.test.ts +++ b/test/core/completions/generators/zsh-generator.test.ts @@ -340,7 +340,7 @@ describe('ZshGenerator', () => { expect(script).toContain("'*: :_openspec_complete_specs'"); }); - it('should generate script that ends with main function call', () => { + it('should generate script that ends with compdef registration', () => { const commands: CommandDefinition[] = [ { name: 'init', @@ -351,7 +351,7 @@ describe('ZshGenerator', () => { const script = generator.generate(commands); - expect(script.trim().endsWith('_openspec "$@"')).toBe(true); + expect(script.trim().endsWith('compdef _openspec openspec')).toBe(true); }); it('should handle empty command list', () => {