Agent Skillsgnomeria/usbtree › write-tests

write-tests

GitHub

指导编写与仓库风格一致的测试,根据缺陷层级选择单元测试、冒烟或TUI测试,遵循行为驱动原则,确保低成本捕获回归并诚实报告结果。

.agents/skills/write-tests/SKILL.md gnomeria/usbtree

Trigger Scenarios

用户要求为函数/组件添加测试 新功能实现后缺乏测试覆盖

Install

npx skills add gnomeria/usbtree --skill write-tests -g -y
More Options

Non-standard path

npx skills add https://github.com/gnomeria/usbtree/tree/main/.agents/skills/write-tests -g -y

Use without installing

npx skills use gnomeria/usbtree@write-tests

指定 Agent (Claude Code)

npx skills add gnomeria/usbtree --skill write-tests -a claude-code -g -y

安装 repo 全部 skill

npx skills add gnomeria/usbtree --all -g -y

预览 repo 内 skill

npx skills add gnomeria/usbtree --list

SKILL.md

Frontmatter
{
    "name": "write-tests",
    "description": "Author tests that match the repo's stack and existing test style, at the cheapest level that catches the regression. Use when the user says \"write tests for\", \"add test coverage\", \"test this function\/handler\/component\", \"this needs tests\", or after implementing a feature that lacks tests."
}

Write Tests

Test fails when behavior breaks, passes when it doesn't — nothing more. Run at end, report honestly.

Step 1 — Match repo style

  • Tests live in-file: #[cfg(test)] mod tests at bottom, use super::*.
  • Builder helpers for fat structs (see dev() in src/usb.rs tests) — reuse existing helper, extend it, don't build parallel one.
  • Plain assert_eq!/assert!. No assertion crates, no mock frameworks.
  • Raw descriptor bytes: inline arrays with #[rustfmt::skip] + comment per chunk (existing pattern).

Step 2 — Choose level

Cheapest level that catches the regression:

  • Unit #[test] — pure logic: tree flatten/fold, parsing (usb.ids, sysfs values, descriptors), diff. Default. No hardware, no I/O.
  • Smoke./target/release/usbtree --demo --dump: exercises scan→tree→render path, no hardware. For wiring bugs, not logic bugs.
  • TUI in pty--demo in pty for interaction (keys, fold, filter). Manual/scripted, last resort — most TUI logic is testable as pure functions on App state; prefer extracting that.

Pure parse bug tested through pty = wrong. Keybinding wiring tested as unit = also wrong. Match level to where bug lives.

Step 3 — Write

  • Behavior, not implementation. Assert outputs and state, not internals.
  • Name by behavior: max_power_parses_sysfs, filter_finds_devices_in_collapsed_hubs — not test1.
  • Independent tests: own data, no ordering deps. No shared mutable state — tests run parallel by default.
  • Filesystem cases: tempfile::TempDir if already a dep, else skip fs test and test the parse fn on a &str instead — push I/O to edge, test the pure part.
  • Cover happy path, interesting failures (malformed input, missing sysfs file, truncated descriptor), boundary values. Skip permutations hitting same branch.
  • Don't test nusb/ratatui — test your use of them.

Step 4 — Run and report

  • cargo test name_fragment first, then full cargo test.
  • Report pass/fail, verbatim output for red.
  • Test caught real bug in code under test → fix code (or flag), never the test.
  • Never weaken assertion or skip to get green. Never report green without running.

Version History

  • 171c884 Current 2026-07-19 10:05

    重写技能以适配 Rust 语言栈,移除对 Go/JS 生态的支持,新增 CI 和知识图谱相关说明。

  • 9a316d8 2026-07-11 17:01

Same Skill Collection

.agents/skills/ci-github-actions/SKILL.md
.agents/skills/debug/SKILL.md
.agents/skills/knowledge-graph/SKILL.md
.agents/skills/refactor/SKILL.md
.agents/skills/rust-patterns/SKILL.md

Metadata

Files
0
Version
7573ea3
Hash
7a15a695
Indexed
2026-07-11 17:01

Главная - Вики-сайт
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-04 03:16
浙ICP备14020137号-1 $Гость$