tests — commands
tests — commands
This document provides an overview of the tests/commands module, which is dedicated to ensuring the correctness and reliability of the application's command-line interface (CLI) and in-chat slash commands.
Module Overview
The tests/commands module contains unit and integration tests for the various command handlers defined in src/commands/handlers and src/commands/cli. Its primary purpose is to validate:
- Command Parsing: Correct interpretation of arguments and options.
- Handler Logic: The internal behavior of each command handler.
- Output Formatting: That commands produce the expected user-facing messages (success, error, usage).
- Side Effects: That commands correctly interact with underlying services and managers (e.g., updating settings, calling external APIs, modifying context).
- Integration: How command handlers interact with mocked dependencies.
By thoroughly testing these commands, developers can ensure a consistent and predictable user experience, as well as the stability of the core command processing logic.
Testing Strategy
The tests in this module employ a common strategy to achieve isolation and reliability:
- Mocking Dependencies: External modules and services (e.g.,
fs, LLM clients, various managers likeLessonsTracker,SkillsHub,AutonomyManager) are extensively mocked usingjest.mockorvi.mock. This allows tests to focus solely on the command handler's logic without relying on actual filesystem operations, network calls, or complex state management. CommandHandlerResultValidation: Many command handlers return aCommandHandlerResultobject. Tests frequently assert on the properties of this object, such as:
handled:trueif the command was processed,falseif it should be passed to the AI.output/entry?.content: The human-readable message to display.error: Any error message generated by the command.passToAI:trueif the command's intent should be further processed by the AI.prompt/systemPrompt: Additional context or instructions to pass to the AI.
- Spying on Interactions:
jest.spyOnorvi.spyOnare used to verify that mocked functions are called with the correct arguments and the expected number of times, ensuring that the command handlers interact with their dependencies as intended. - CLI Program Simulation: For
commander.js-based CLI commands, a mockCommandprogram is created, andparseAsyncis used to simulate command-line execution, capturingconsole.logandconsole.erroroutput for validation.
Example: Mocking for Isolation
The following Mermaid diagram illustrates the general testing approach, using agent-handlers.test.ts as an example:
graph TD
A[agent-handlers.test.ts] --> B{handleAgent()};
A --> C{checkAgentTriggers()};
B -- Calls --> D(getCustomAgentLoader());
C -- Calls --> D;
D -- Mocked by --> E[jest.mock('../../src/agent/custom/custom-agent-loader.js')];
E -- Provides Mocked Methods --> F(listAgents, getAgent, findByTrigger, etc.);
B -- Returns --> G{CommandHandlerResult};
C -- Returns --> G;
A -- Asserts on --> G;
A -- Asserts on --> F;
Key Test Files and Their Focus
The tests/commands module is organized to mirror the structure of the command handlers, with distinct test files for different command groups.
Slash Command Handlers
These tests validate commands typically invoked within an interactive chat environment (e.g., /command).
agent-handlers.test.ts:- Commands Tested:
/agent - Focus: Verifies the
/agentcommand's functionality, including listing available agents, displaying detailed agent information (/agent info), creating new agent configuration files (/agent create), reloading agents (/agent reload), and activating agents by ID (/agent). It also tests thecheckAgentTriggersfunction for natural language agent activation. - Key Mocks:
../../src/agent/custom/custom-agent-loader.js btw-handler.test.ts:- Commands Tested:
/btw - Focus: Ensures the "by the way" command correctly makes a one-shot LLM call for quick questions, handles client availability, and processes LLM responses or errors.
- Key Mocks: A mock LLM client with a
chatmethod. context-handlers.test.ts:- Commands Tested:
/add,/context,/workspace - Focus: Validates adding files to the context (
/add), managing the context (summary, clear, list), and detecting workspace information (/workspace). Tests cover glob pattern matching and reporting no files found. core-handlers.test.ts:- Commands Tested:
/help,/yolo,/autonomy,/pipeline,/parallel,/model-router,/skill,/save - Focus: Covers a broad range of core functionalities:
handleHelp: Displays command categories and usage tips.handleYoloMode: Manages YOLO mode (on/off/safe) and command allow/deny lists.handleAutonomy: Sets and displays autonomy levels (suggest, confirm, auto, full, yolo).handlePipeline: Lists and initiates predefined development pipelines (e.g.,code-review,bug-fix).handleParallel: Passes tasks to the AI for parallel execution.handleModelRouter: Manages and displays model routing settings (auto/manual).handleSkill: Lists, activates, and deactivates skills.handleSaveConversation: Saves chat history to a file.- Key Mocks:
getAutonomyManager,resetSkillManager. extra-handlers.test.ts:- Commands Tested:
/undo,/diff,/context stats,/search,/test,/fix,/review - Focus: Tests UX-oriented commands:
handleUndo: Initiates an undo operation (checks for checkpoint availability).handleDiff: Displaysgit diffoutput or "no changes" messages.handleContextStats: Shows detailed context window statistics (token usage, model, status).handleSearch: Performs file content searches.handleTest: Simulates running tests (e.g.,npm test).handleFix: Simulates running code fixers (e.g., ESLint, TypeScript).handleReview: Passes code changes to the AI for review.- Also includes tests for the
EnhancedCommandHandlerdispatch logic for these commands. permissions-handlers.test.ts:- Commands Tested:
/permissions - Focus: Manages tool permissions, allowing users to add/remove tools or tool categories from allow/block lists. Tests cover listing permissions, resetting, and displaying available categories.
- Key Mocks:
getToolFilter,resetToolFilter.
CLI Command Handlers
These tests validate commands typically invoked directly from the terminal (e.g., buddy command).
backup-handlers.test.ts:- Commands Tested:
buddy backup(subcommands:create,verify,list,restore) - Focus: Validates the backup and restore functionality for the
.codebuddy/directory. Tests cover creating backups (with options like--only-config), verifying backup integrity, listing existing backups, and initiating a restore operation. - Key Mocks:
fsmodule (forexistsSync,readdirSync,readFileSync,writeFileSync,statSync). lessons-command.test.ts:- Commands Tested:
buddy lessons(subcommands:list,add,search,clear,context) - Focus: Tests the
commander.jsintegration for thelessonscommand, ensuring correct argument parsing, option handling, and interaction with theLessonsTracker. - Key Mocks:
../../src/agent/lessons-tracker.js(specificallygetLessonsTracker). Native Engine-commands.test.ts:- Commands Tested:
buddy heartbeat,buddy hub,buddy identity,buddy groups,buddy auth-profile(and their numerous subcommands) - Focus: This is a comprehensive test file covering a wide array of CLI commands Advanced enterprise architecture for:
- Heartbeat:
start,stop,status,tick(daemon management). - Hub:
search,install,uninstall,update,list,info,publish,sync(skill hub interaction). - Identity:
show,get,set,prompt(managing agent identity files like SOUL.md). - Groups:
status,list,block,unblock(group security management). - Auth Profile:
list,add,remove,reset(managing authentication profiles for LLM providers). - Key Mocks:
getHeartbeatEngine,getSkillsHub,getIdentityManager,getGroupSecurity,getAuthProfileManager,resetAuthProfileManager. pipeline-command.test.ts:- Commands Tested:
buddy pipeline - Focus: Validates the loading and validation of pipeline definition files (JSON and YAML). Ensures that pipeline definitions adhere to the expected schema and that errors are reported for invalid structures.
- Key Mocks:
fs,path,js-yaml.
Development Pipelines
dev/issue-pipeline.test.ts:- Functions Tested:
runIssuePipeline - Focus: This test validates a more complex, multi-step development workflow. It simulates fetching a GitHub issue, creating a new Git branch, running a relevant workflow (e.g.,
fix-tests,security-audit,add-featurebased on issue labels), and creating a pull request. - Key Mocks:
child_process(execSync),../../../src/commands/dev/workflows.js(runWorkflow), and a mock agent.
Key Interfaces and Utilities Tested
Beyond the command handlers themselves, these tests implicitly or explicitly validate the behavior of several core interfaces and utility functions:
CommandHandlerResult: Its structure and how different fields (handled,output,error,passToAI,prompt,systemPrompt) are used by handlers.getAutonomyManager: The state and behavior of the autonomy system (YOLO mode, approval levels).resetSkillManager: The ability to reset the skill management system.getToolFilter,resetToolFilter: The mechanism for controlling tool access permissions.- Various Manager Mocks: The tests for
Native Engine-commands.test.tsandlessons-command.test.tsheavily rely on mocking and verifying interactions with their respective manager classes (e.g.,HeartbeatEngine,SkillsHub,IdentityManager,GroupSecurity,AuthProfileManager,LessonsTracker), ensuring that these underlying services are correctly invoked and their responses handled.