write-tests

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Cover Local Changes with Tests

为本地变更添加测试覆盖

User Arguments

用户参数

User can provide a what tests or modules to focus on:
$ARGUMENTS
If nothing is provided, focus on all changes in current git diff that not commited. If everything is commited, then will cover latest commit.
用户可以指定需要关注的测试或模块:
$ARGUMENTS
若未提供任何参数,则聚焦于当前git diff中所有未提交的变更。若所有变更已提交,则为最新提交添加测试覆盖。

Context

背景

After implementing new features or refactoring existing code, it's critical to ensure all business logic changes are covered by tests. This command orchestrates automated test creation for local changes using coverage analysis and specialized agents.
在实现新功能或重构现有代码后,确保所有业务逻辑变更都有测试覆盖至关重要。本命令通过覆盖分析和专门的Agent,为本地变更自动创建测试。

Goal

目标

Achieve comprehensive test coverage for all critical business logic in local code changes.
为本地代码变更中的所有关键业务逻辑实现全面的测试覆盖。

Important Constraints

重要约束

  • Focus on critical business logic - not every line needs 100% coverage
  • Preserve existing tests - only add new tests, don't modify existing ones
  • "Analyse complexity of changes" -
    • if there 2 or more changed files, or one file with complex logic, then Do not write tests yourself - only orchestrate agents!
    • if there is only one changed file, and it's a simple change, then you can write tests yourself.
  • 聚焦关键业务逻辑 - 无需实现100%代码行覆盖
  • 保留现有测试 - 仅添加新测试,不得修改现有测试
  • "分析变更复杂度" -
    • 若有2个及以上变更文件,或单个文件包含复杂逻辑,则请勿自行编写测试 - 仅需编排Agent执行!
    • 若仅单个变更文件且变更简单,则可自行编写测试。

Workflow Steps

工作流程步骤

Preparation

准备阶段

  1. Read sadd skill if available
    • If available, read the sadd skill to understand best practices for managing agents
  2. Discover test infrastructure
    • Read @README.md and package.json (or equivalent project config)
    • Identify commands to run tests and coverage reports
    • Understand project structure and testing conventions
  3. Run all tests
    • Execute full test suite to establish baseline
  1. 读取sadd skill(若可用)
    • 若存在,读取sadd skill以了解管理Agent的最佳实践
  2. 探索测试基础设施
    • 读取@README.md和package.json(或等效项目配置文件)
    • 确定运行测试和生成覆盖报告的命令
    • 了解项目结构和测试规范
  3. 运行所有测试
    • 执行完整测试套件以建立基准

Analysis

分析阶段

Do steps 4-5 in parallel using haiku agents:
  1. Verify single test execution
    • Choose any passing test file
    • Launch haiku agent with instructions to find proper command to run this only test file
      • Ask him to iterate until you can reliably run individual tests
    • After he complete try running a specific test file if it exists
    • This ensures agents can run tests in isolation
  2. Analyze local changes
    • Run
      git status -u
      to identify all changed files (including untracked files)
      • If there no uncommited changes, then run
        git show --name-status
        to get the list of files that were changed in the latest commit.
    • Filter out non-code files (docs, configs, etc.)
    • Launch separate haikue agent per changed file to analyze file itself, and the complexity of the changes, and prepare short summary of it.
    • Extract list of files with actual logic changes
使用haiku agents并行执行步骤4-5:
  1. 验证单个测试执行
    • 选择任意通过的测试文件
    • 启动haiku agent,指示其找到运行该单个测试文件的正确命令
      • 要求其反复尝试,直到能可靠运行单个测试
    • 完成后尝试运行特定测试文件(若存在)
    • 确保Agent可独立运行测试
  2. 分析本地变更
    • 运行
      git status -u
      以识别所有变更文件(包括未跟踪文件)
      • 若无未提交变更,则运行
        git show --name-status
        获取最新提交中变更的文件列表
    • 过滤非代码文件(文档、配置文件等)
    • 为每个变更文件启动单独的haikue agent,分析文件本身及变更复杂度,并准备简短摘要
    • 提取包含实际逻辑变更的文件列表

Test Writing

测试编写阶段

Simple Single File Flow

单个简单文件流程

If there is only one changed file, and it's a simple change, then you can write tests yourself. Following this guidline:
  1. Read TDD skill for best practices on writing tests
  2. Read the target file {FILE_PATH} and understand the logic
  3. Review existing test files for patterns and style, if not exists then create it.
  4. Analyse which tests cases should be added to cover the changes.
  5. Create comprehensive tests for all identified cases
  6. Run the test command identified before.
  7. Iterate and fix any issues until all tests pass
Ensure tests are:
  • Clear and maintainable
  • Follow project conventions
  • Test behavior, not implementation
  • Cover edge cases and error paths
若仅单个变更文件且变更简单,则可自行编写测试。遵循以下指南:
  1. 读取TDD skill以了解编写测试的最佳实践
  2. 读取目标文件{FILE_PATH}并理解其逻辑
  3. 查看现有测试文件以了解模式和风格,若不存在则创建新测试文件
  4. 分析应添加哪些测试用例以覆盖变更
  5. 为所有识别出的用例创建全面的测试
  6. 运行之前确定的测试命令
  7. 迭代并修复所有问题,直到所有测试通过
确保测试:
  • 清晰且易于维护
  • 遵循项目规范
  • 测试行为而非实现细节
  • 覆盖边缘情况和错误路径

Multiple Files or Complex File Flow

多文件或复杂文件流程

If there are multiple changed files, or one file with complex logic, then you need to use specialized agents to cover the changes. Following this guidline:
  1. Launch
    review:test-coverage-reviewer
    agents (parallel)
    (Sonnet or Opus models)
    • Launch one coverage-reviewer agent per changed file
    • Provide each agent with:
      • Context: What changed in this file (git diff)
      • Target: Which specific file to analyze
      • Resources: Read README and relevant documentation
      • Goal: Identify what test suites need to be added
      • Output: List of test cases needed for critical business logic
    • Collect all coverage review reports
  2. Launch
    developer
    agents for test file (parallel)
    (Sonnet or Opus models)
    • Launch one developer agent per changed file that needs tests
    • Provide each agent with:
      • Context: Coverage review report for this file
      • Target: Which specific file to create tests for
      • Test cases: List from coverage-reviewer agent
      • Guidance: Read TDD skill (if available) for best practices on writing tests.
      • Resources: Read README and test examples
      • Command: How to run tests for this file
      • Goal: Create comprehensive tests for all identified cases
      • Constraint: Add new tests, don't modify existing logic (unless clearly broken)
  3. Verify coverage (iteration) (Sonnet or Opus models)
    • Launch
      review:test-coverage-reviewer
      agents again per file
    • Provide:
      • Context: Original changes + new tests added
      • Goal: Verify all critical business logic is covered
      • Output: Confirmation or list of missing coverage
  4. Iterate if needed
  • If any files still lack coverage: Return to step 5
  • Launch new developer agents only for files with gaps
  • Provide specific instructions on what's still missing
  • Continue until all critical business logic is covered
  1. Final verification
  • Run full test suite to ensure all tests pass
  • Generate coverage report if available
  • Verify no regressions in existing tests
若存在多个变更文件,或单个文件包含复杂逻辑,则需使用专门的Agent来覆盖变更。遵循以下指南:
  1. 并行启动
    review:test-coverage-reviewer
    Agent
    (Sonnet或Opus模型)
    • 为每个变更文件启动一个coverage-reviewer Agent
    • 为每个Agent提供:
      • 上下文:该文件的变更内容(git diff)
      • 目标:需要分析的具体文件
      • 资源:读取README及相关文档
      • 目标:识别需要添加的测试套件
      • 输出:关键业务逻辑所需的测试用例列表
    • 收集所有覆盖评审报告
  2. 为测试文件并行启动
    developer
    Agent
    (Sonnet或Opus模型)
    • 为每个需要测试的变更文件启动一个developer Agent
    • 为每个Agent提供:
      • 上下文:该文件的覆盖评审报告
      • 目标:需要为其创建测试的具体文件
      • 测试用例:coverage-reviewer Agent提供的列表
      • 指导:读取TDD skill(若可用)以了解编写测试的最佳实践
      • 资源:读取README及测试示例
      • 命令:运行该文件测试的方法
      • 目标:为所有识别出的用例创建全面的测试
      • 约束:仅添加新测试,不得修改现有逻辑(除非明显存在问题)
  3. 验证覆盖情况(迭代)(Sonnet或Opus模型)
    • 为每个文件再次启动
      review:test-coverage-reviewer
      Agent
    • 提供:
      • 上下文:原始变更内容 + 添加的新测试
      • 目标:验证所有关键业务逻辑已被覆盖
      • 输出:确认信息或缺失覆盖的列表
  4. 必要时迭代
    • 若任何文件仍存在覆盖缺口:返回步骤5
    • 仅为存在缺口的文件启动新的developer Agent
    • 提供关于缺失内容的具体指示
    • 持续迭代直到所有关键业务逻辑被覆盖
  5. 最终验证
  • 运行完整测试套件以确保所有测试通过
  • 若可用则生成覆盖报告
  • 验证现有测试无回归问题

Success Criteria

成功标准

  • All critical business logic in changed files has test coverage ✅
  • All tests pass (new and existing) ✅
  • Test quality verified by coverage-reviewer agents ✅
  • 变更文件中的所有关键业务逻辑均有测试覆盖 ✅
  • 所有测试(新测试和现有测试)均通过 ✅
  • 测试质量已通过coverage-reviewer Agent验证 ✅

Agent Instructions Templates

Agent指令模板

Coverage Review Agent (Initial Analysis)

覆盖评审Agent(初始分析)

Analyze the file {FILE_PATH} for test coverage needs.

Context: This file was modified in local changes:
{GIT_DIFF_OUTPUT}

Your task:
1. Read the changed file and understand the business logic
2. Identify all critical code paths that need testing:
   - New functions/methods added
   - Modified business logic
   - Edge cases and error handling
   - Integration points
3. Review existing tests (if any) to avoid duplication
4. Create a list of test cases needed, prioritized by importance:
   - CRITICAL: Core business logic, data mutations
   - IMPORTANT: Error handling, validations
   - NICE_TO_HAVE: Edge cases, performance

Output format:
- List of test cases with descriptions
- Priority level for each
- Suggested test file location
分析文件{FILE_PATH}的测试覆盖需求。

上下文:该文件在本地变更中被修改:
{GIT_DIFF_OUTPUT}

你的任务:
1. 读取变更后的文件并理解其业务逻辑
2. 识别所有需要测试的关键代码路径:
   - 新增的函数/方法
   - 修改的业务逻辑
   - 边缘情况和错误处理
   - 集成点
3. 查看现有测试(若有)以避免重复
4. 创建按优先级排序的测试用例列表:
   - 关键:核心业务逻辑、数据变更
   - 重要:错误处理、验证逻辑
   - 可选:边缘情况、性能测试

输出格式:
- 带描述的测试用例列表
- 每个用例的优先级
- 建议的测试文件位置

Developer Agent (Test Creation)

开发Agent(测试创建)

Create tests for file {FILE_PATH} based on coverage analysis.

Coverage review identified these test cases:
{TEST_CASES_LIST}

Your task:
1. Read TDD skill (if available) for best practices on writing tests
2. Read @README.md for project context and testing conventions
3. Read the target file {FILE_PATH} and understand the logic
4. Review existing test files for patterns and style
5. Create comprehensive tests for all identified cases
6. Run the tests: {TEST_COMMAND}
7. Iterate until all tests pass
8. Ensure tests are:
   - Clear and maintainable
   - Follow project conventions
   - Test behavior, not implementation
   - Cover edge cases and error paths

Test command: {TEST_COMMAND}
基于覆盖分析为文件{FILE_PATH}创建测试。

覆盖评审识别出以下测试用例:
{TEST_CASES_LIST}

你的任务:
1. 读取TDD skill(若可用)以了解编写测试的最佳实践
2. 读取@README.md以获取项目上下文和测试规范
3. 读取目标文件{FILE_PATH}并理解其逻辑
4. 查看现有测试文件以了解模式和风格
5. 为所有识别出的用例创建全面的测试
6. 运行测试:{TEST_COMMAND}
7. 迭代直到所有测试通过
8. 确保测试:
   - 清晰且易于维护
   - 遵循项目规范
   - 测试行为而非实现细节
   - 覆盖边缘情况和错误路径

测试命令:{TEST_COMMAND}

Coverage Review Agent (Verification)

覆盖评审Agent(验证阶段)

Verify test coverage for file {FILE_PATH}.

Context: Tests were added to cover local changes in this file.

Your task:
1. Read the changed file {FILE_PATH}
2. Read the new test file(s) created
3. Verify all critical business logic is covered:
   - All new functions have tests
   - All modified logic has tests
   - Edge cases are tested
   - Error handling is tested
4. Identify any gaps in coverage
5. Confirm test quality (clear, maintainable, follows TDD principles)

Output:
- PASS: All critical business logic is covered ✅
- GAPS: List specific missing test cases that need to be added
验证文件{FILE_PATH}的测试覆盖情况。

上下文:已添加测试以覆盖该文件的本地变更。

你的任务:
1. 读取变更后的文件{FILE_PATH}
2. 读取创建的新测试文件
3. 验证所有关键业务逻辑已被覆盖:
   - 所有新增函数均有测试
   - 所有修改的逻辑均有测试
   - 边缘情况已被测试
   - 错误处理已被测试
4. 识别任何覆盖缺口
5. 确认测试质量(清晰、可维护、遵循TDD原则)

输出:
- 通过:所有关键业务逻辑已被覆盖 ✅
- 缺口:列出需要添加的具体缺失测试用例