AGENT SKILL

Rust TDD 开发工作流

tdd-rust

面向 RTK 过滤器开发的 TDD 工作流。采用符合 Rust 惯用法的红-绿-重构(Red-Green-Refactor)流程。使用真实测试夹具、token 节省断言,以及基于 insta 的快照测试。在实现新过滤器时自动触发。

使用场景Rust 开发
License 许可
AGPL-3.0
最近更新
2026-11-13
Stars
★ 13
Forks
2

01 前言 Front Matter

name
tdd-rust
tags
[ "tdd", "testing", "rust", "filters", "snapshots", "token-savings", "rtk" ]
effort
medium
triggers
[ "new filter", "implement filter", "add command", "write tests for", "test coverage", "fix failing test" ]
description
TDD workflow for RTK filter development. Red-Green-Refactor with Rust idioms. Real fixtures, token savings assertions, snapshot tests with insta. Auto-triggers on new filter implementation.
allowed-tools
[ "Read", "Write", "Edit", "Bash" ]

02 资源文件 1

03 SKILL.md 正文

RTK TDD Workflow

Enforce Red-Green-Refactor for all RTK filter development.

The Loop

1. RED   — Write failing test with real fixture
2. GREEN — Implement minimum code to pass
3. REFACTOR — Clean up, verify still passing
4. SAVINGS — Verify ≥60% token reduction
5. SNAPSHOT — Lock output format with insta

Step 1: Real Fixture First

Never write synthetic test data. Capture real command output:

# Capture real output from the actual command
git log -20 > tests/fixtures/git_log_raw.txt
cargo test 2>&1 > tests/fixtures/cargo_test_raw.txt
cargo clippy 2>&1 > tests/fixtures/cargo_clippy_raw.txt
gh pr view 42 > tests/fixtures/gh_pr_view_raw.txt

# For commands with ANSI codes — capture as-is
script -q /dev/null cargo test 2>&1 > tests/fixtures/cargo_test_ansi_raw.txt

Fixture naming: tests/fixtures/<command>_raw.txt

Step 2: Write the Test (Red)

#[cfg(test)]
mod tests {
    use super::*;
    use insta::assert_snapshot;

    fn count_tokens(s: &str) -> usize {
        s.split_whitespace().count()
    }

    // Test 1: Output format (snapshot)
    #[test]
    fn test_filter_output_format() {
        let input = include_str!("../tests/fixtures/mycmd_raw.txt");
        let output = filter_mycmd(input).expect("filter should not fail");
        assert_snapshot!(output);
    }

    // Test 2: Token savings ≥60%
    #[test]
    fn test_token_savings() {
        let input = include_str!("../tests/fixtures/mycmd_raw.txt");
        let output = filter_mycmd(input).expect("filter should not fail");

        let input_tokens = count_tokens(input);
        let output_tokens = count_tokens(&output);
        let savings = 100.0 * (1.0 - output_tokens as f64 / input_tokens as f64);

        assert!(
            savings >= 60.0,
            "Expected ≥60% token savings, got {:.1}% ({} → {} tokens)",
            savings, input_tokens, output_tokens
        );
    }

    // Test 3: Edge cases
    #[test]
    fn test_empty_input() {
        let result = filter_mycmd("");
        assert!(result.is_ok());
        // Empty input = empty output OR passthrough, never panic
    }

    #[test]
    fn test_malformed_input() {
        let result = filter_mycmd("not valid command output\nrandom text\n");
        // Must not panic — either filter best-effort or return input unchanged
        assert!(result.is_ok());
    }
}

Run: cargo test → should fail (function doesn't exist yet).

Step 3: Minimum Implementation (Green)

// src/mycmd_cmd.rs

use anyhow::{Context, Result};
use lazy_static::lazy_static;
use regex::Regex;

lazy_static! {
    static ref ERROR_RE: Regex = Regex::new(r"^error").unwrap();
}

pub fn filter_mycmd(input: &str) -> Result<String> {
    if input.is_empty() {
        return Ok(String::new());
    }

    let filtered: Vec<&str> = input.lines()
        .filter(|line| ERROR_RE.is_match(line))
        .collect();

    Ok(filtered.join("\n"))
}

Run: cargo test → green.

Step 4: Accept Snapshot

# First run creates the snapshot
cargo test test_filter_output_format

# Review what was captured
cargo insta review
# Press 'a' to accept

# Snapshot saved to src/snapshots/mycmd_cmd__tests__test_filter_output_format.snap

Step 5: Wire to main.rs (Integration)

// src/main.rs
mod mycmd_cmd;

#[derive(Subcommand)]
pub enum Commands {
    // ... existing commands ...
    Mycmd(MycmdArgs),
}

// In match:
Commands::Mycmd(args) => mycmd_cmd::run(args),
// src/mycmd_cmd.rs — add run() function
pub fn run(args: MycmdArgs) -> Result<()> {
    let output = execute_command("mycmd", &args.to_vec())
        .context("Failed to execute mycmd")?;

    let filtered = filter_mycmd(&output.stdout)
        .unwrap_or_else(|e| {
            eprintln!("rtk: filter warning: {}", e);
            output.stdout.clone()
        });

    tracking::record("mycmd", &output.stdout, &filtered)?;
    print!("{}", filtered);

    if !output.status.success() {
        std::process::exit(output.status.code().unwrap_or(1));
    }
    Ok(())
}

Step 6: Quality Gate

cargo fmt --all && cargo clippy --all-targets && cargo test

All 3 must pass. Zero clippy warnings.

Arrange-Act-Assert Pattern

#[test]
fn test_filters_only_errors() {
    // Arrange
    let input = "info: starting build\nerror[E0001]: undefined\nwarning: unused\n";

    // Act
    let output = filter_mycmd(input).expect("should succeed");

    // Assert
    assert!(output.contains("error[E0001]"), "Should keep error lines");
    assert!(!output.contains("info:"), "Should drop info lines");
    assert!(!output.contains("warning:"), "Should drop warning lines");
}

RTK-Specific Test Patterns

Test ANSI stripping

#[test]
fn test_strips_ansi_codes() {
    let input = "\x1b[32mSuccess\x1b[0m\n\x1b[31merror: failed\x1b[0m\n";
    let output = filter_mycmd(input).expect("should succeed");
    assert!(!output.contains("\x1b["), "ANSI codes should be stripped");
    assert!(output.contains("error: failed"), "Content should be preserved");
}

Test fallback behavior

#[test]
fn test_filter_handles_unexpected_format() {
    // Give it something completely unexpected
    let input = "completely unexpected\x00binary\xff data";
    // Should not panic — returns Ok() with either empty or passthrough
    let result = filter_mycmd(input);
    assert!(result.is_ok(), "Filter must not panic on unexpected input");
}

Test savings at multiple sizes

#[test]
fn test_savings_large_output() {
    // 1000-line fixture → must still hit ≥60%
    let large_input: String = (0..1000)
        .map(|i| format!("info: processing item {}\n", i))
        .collect();
    let output = filter_mycmd(&large_input).expect("should succeed");

    let savings = 100.0 * (1.0 - count_tokens(&output) as f64 / count_tokens(&large_input) as f64);
    assert!(savings >= 60.0, "Large output savings: {:.1}%", savings);
}

What "Done" Looks Like

Checklist before moving on:

  • [ ] tests/fixtures/<cmd>_raw.txt — real command output
  • [ ] filter_<cmd>() function returns Result<String>
  • [ ] Snapshot test passes and accepted via cargo insta review
  • [ ] Token savings test: ≥60% verified
  • [ ] Empty input test: no panic
  • [ ] Malformed input test: no panic
  • [ ] run() function with fallback pattern
  • [ ] Registered in main.rs Commands enum
  • [ ] cargo fmt --all && cargo clippy --all-targets && cargo test — all green

Never Do This

// ❌ Synthetic fixture data
let input = "fake error: something went wrong";  // Not real cargo output

// ❌ Missing savings test
#[test]
fn test_filter() {
    let output = filter_mycmd(input);
    assert!(!output.is_empty());  // No savings verification
}

// ❌ unwrap() in production code
let filtered = filter_mycmd(input).unwrap();  // Panic in prod

// ❌ Regex inside the filter function
fn filter_mycmd(input: &str) -> Result<String> {
    let re = Regex::new(r"^error").unwrap();  // Recompiles every call
    ...
}

04 同仓库更多 Skills

MDMAtk
MDMAtk/TormentNexus

构建提交与发布自动化

ship

构建、提交、推送与版本升级工作流——自动化完整的发布周期。

发布经理部署与发布管理
★ 13
MDMAtk
MDMAtk/TormentNexus

RTK CLI 安全守卫

security-guardian

面向 RTK 的 CLI 安全专家——涵盖命令注入、shell 转义与 hook 安全。

安全工程师安全检测工程
★ 13
MDMAtk
MDMAtk/TormentNexus

RTK 完整分诊分析

rtk-triage

RTK 完整分诊:并行执行 issue-triage 与 pr-triage,随后交叉比对数据,检测重复覆盖、安全漏洞、无 PR 的 P0 问题及内部冲突。结果保存至 claudedocs/RTK-YYYY-MM-DD.md。参数:"en"/"fr" 指定语言(默认 fr),"save" 强制保存。

软件开发工程师工单与问题跟踪管理
★ 13
MDMAtk
MDMAtk/TormentNexus

Rust TDD 测试驱动开发

rtk-tdd

为 Rust 开发强制推行 TDD(红-绿-重构)。在实现、测试、重构和缺陷修复任务中自动触发。提供符合 Rust 习惯的测试模式,涵盖 anyhow/thiserror、cfg(test) 以及 Arrange-Act-Assert 工作流。

软件开发工程师Rust 开发
★ 13
MDMAtk
MDMAtk/TormentNexus

代码仓库回顾报告

repo-recap

生成全面的代码仓库回顾报告(PR、issue、release),用于与团队分享。传入 "en" 或 "fr" 作为参数指定语言(默认为 fr)。

软件开发工程师代码仓库协作管理
★ 13
MDMAtk
MDMAtk/TormentNexus

PR 分诊与审查

pr-triage

PR 分诊:审查未合并的 PR、对选定的 PR 进行深度审查、起草并发布审查评论。参数:"all" 表示审查全部;也可指定 PR 编号进行聚焦(例如 "42 57");"en"/"fr" 用于指定语言;无参数 = 仅以法语进行审核。

开源社区运营专员代码仓库协作管理
★ 13