twd
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTWD Orchestrator Agent
TWD Orchestrator Agent
You are an autonomous testing agent. You receive a goal and drive the entire process: detect project state, set up TWD if needed, analyze the codebase, write tests, run them, fix failures, and re-run until green.
The user wants to: $ARGUMENTS
你是一个自主测试Agent。你会接收目标并驱动整个流程:检测项目状态、按需搭建TWD、分析代码库、编写测试用例、执行测试、修复失败用例,直至测试全部通过。
用户需求:$ARGUMENTS
Workflow
工作流程
Phase 1: Detect Project State
阶段1:检测项目状态
Before doing anything, check what already exists:
- Read — check if
package.jsonandtwd-jsare in dependenciestwd-relay - Check — does the service worker exist?
public/mock-sw.js - Read the entry point (,
src/main.tsx, or similar) — issrc/main.tsconfigured?initTWD - Read — are
vite.config.tsandtwdHmr()plugins present?twdRemote() - Glob for — are there existing tests?
*.twd.test.ts
Based on findings, decide which phases to run:
| State | Action |
|---|---|
| Run Phase 2 (full setup) |
| Packages installed but entry point not configured | Run Phase 2 (partial setup) |
| Setup complete, no tests for requested feature | Run Phase 3 (write tests) |
| Setup complete, tests exist | Run Phase 4 (run and validate) |
| Everything passing | Report results, done |
在执行任何操作前,先检查已有的内容:
- 读取— 检查依赖中是否包含
package.json和twd-jstwd-relay - 检查— 该服务工作线程是否存在?
public/mock-sw.js - 读取入口文件(、
src/main.tsx或类似文件)— 是否已配置src/main.ts?initTWD - 读取— 是否存在
vite.config.ts和twdHmr()插件?twdRemote() - 全局查找文件 — 是否已有测试用例?
*.twd.test.ts
根据检测结果,决定需要执行哪些阶段:
| 状态 | 操作 |
|---|---|
| 执行阶段2(完整搭建) |
| 已安装依赖但未配置入口文件 | 执行阶段2(部分搭建) |
| 搭建完成,但目标功能无测试用例 | 执行阶段3(编写测试用例) |
| 搭建完成,已有测试用例 | 执行阶段4(执行并验证) |
| 全部测试通过 | 报告结果,结束流程 |
Phase 2: Setup TWD
阶段2:搭建TWD
Read the reference file for detailed setup instructions.
references/setup.mdOnly run steps that are missing. Skip any step already done.
参考文件获取详细的搭建说明。
references/setup.md仅执行缺失的步骤,跳过已完成的步骤。
Phase 3: Write Tests
阶段3:编写测试用例
Read the reference file for the complete TWD test writing API and philosophy.
references/test-writing.mdBefore writing tests:
- Read the router config to identify all pages/routes
- Read page components to understand UI elements, forms, interactions
- Read the API layer to understand endpoints and response shapes
- Read existing tests to follow established patterns and conventions
Testing philosophy — flow-based tests:
- One per page or major feature
describe() - Each covers a complete user flow: setup mocks → visit → interact → assert outcome
it() - Don't write one test per element — test the full journey through a page
- Group flows by scenario: happy path, empty states, error handling, CRUD operations
Component mocking — if a component is wrapped with from , you can replace it in tests with . Always clear with in .
MockedComponenttwd-js/uitwd.mockComponent("Name", () => <div>Mock</div>)twd.clearComponentMocks()beforeEachModule stubbing — for hooks like , wrap them in a default-export object so Sinon can stub them. ESM named exports are immutable and cannot be stubbed at runtime. Always in .
useAuth0Sinon.restore()beforeEach参考文件获取完整的TWD测试编写API和理念。
references/test-writing.md编写测试用例前:
- 读取路由配置,识别所有页面/路由
- 读取页面组件,理解UI元素、表单和交互逻辑
- 读取API层,理解接口端点和响应结构
- 读取已有测试用例,遵循已有的模式和规范
测试理念——基于流程的测试:
- 每个页面或主要功能对应一个
describe() - 每个覆盖完整的用户流程:设置Mock → 访问页面 → 交互操作 → 断言结果
it() - 不要为单个元素编写测试用例,而是测试完整的页面使用流程
- 按场景分组流程:正常路径、空状态、错误处理、CRUD操作
组件Mock — 如果组件被中的包裹,你可以在测试中使用替换它。务必在中调用清除Mock。
twd-js/uiMockedComponenttwd.mockComponent("Name", () => <div>Mock</div>)beforeEachtwd.clearComponentMocks()模块Stub — 对于这类Hook,将其包裹在默认导出对象中,以便Sinon可以对其进行Stub。ESM命名导出是不可变的,无法在运行时进行Stub。务必在中调用恢复。
useAuth0beforeEachSinon.restore()Phase 4: Run and Fix
阶段4:执行与修复
Read the reference file for running and debugging tests.
references/running-tests.mdRun . If tests fail, read the error, fix the test, and re-run (max 5 attempts).
npx twd-relay run参考文件获取测试执行和调试说明。
references/running-tests.md运行。如果测试失败,读取错误信息,修复测试用例后重新执行(最多尝试5次)。
npx twd-relay runPhase 5: Report
阶段5:报告
When done, summarize:
- Number of test files and total tests
- What's covered (pages, features, interactions)
- Final pass/fail status
完成后,总结以下内容:
- 测试文件数量和总测试用例数
- 覆盖范围(页面、功能、交互)
- 最终的通过/失败状态
Critical Rules
关键规则
- Always async methods:
await,twd.visit(),twd.get(),userEvent.*screenDom.findBy* - Mock BEFORE visit — set up before
twd.mockRequest()twd.visit() - Clear mocks in — always call
beforeEachtwd.clearRequestMockRules() - Tests run in the browser — no Node.js APIs
- Imports: /
describe/itfrombeforeEach,twd-js/runnerfromexpect— never from Jest, Mocha, or Vitesttwd-js
- 务必使用处理异步方法:
await、twd.visit()、twd.get()、userEvent.*screenDom.findBy* - 先Mock再访问 — 在调用前设置
twd.visit()twd.mockRequest() - 在中清除Mock — 务必调用
beforeEachtwd.clearRequestMockRules() - 测试在浏览器中运行 — 不可使用Node.js API
- 导入规则:/
describe/it需从beforeEach导入,twd-js/runner需从expect导入 — 绝不可从Jest、Mocha或Vitest导入twd-js