twd

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

TWD 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:
  1. Read
    package.json
    — check if
    twd-js
    and
    twd-relay
    are in dependencies
  2. Check
    public/mock-sw.js
    — does the service worker exist?
  3. Read the entry point (
    src/main.tsx
    ,
    src/main.ts
    , or similar) — is
    initTWD
    configured?
  4. Read
    vite.config.ts
    — are
    twdHmr()
    and
    twdRemote()
    plugins present?
  5. Glob for
    *.twd.test.ts
    — are there existing tests?
Based on findings, decide which phases to run:
StateAction
twd-js
not in package.json
Run Phase 2 (full setup)
Packages installed but entry point not configuredRun Phase 2 (partial setup)
Setup complete, no tests for requested featureRun Phase 3 (write tests)
Setup complete, tests existRun Phase 4 (run and validate)
Everything passingReport results, done
在执行任何操作前,先检查已有的内容:
  1. 读取
    package.json
    — 检查依赖中是否包含
    twd-js
    twd-relay
  2. 检查
    public/mock-sw.js
    — 该服务工作线程是否存在?
  3. 读取入口文件
    src/main.tsx
    src/main.ts
    或类似文件)— 是否已配置
    initTWD
  4. 读取
    vite.config.ts
    — 是否存在
    twdHmr()
    twdRemote()
    插件?
  5. 全局查找
    *.twd.test.ts
    文件
    — 是否已有测试用例?
根据检测结果,决定需要执行哪些阶段:
状态操作
package.json
中无
twd-js
执行阶段2(完整搭建)
已安装依赖但未配置入口文件执行阶段2(部分搭建)
搭建完成,但目标功能无测试用例执行阶段3(编写测试用例)
搭建完成,已有测试用例执行阶段4(执行并验证)
全部测试通过报告结果,结束流程

Phase 2: Setup TWD

阶段2:搭建TWD

Read the reference file
references/setup.md
for detailed setup instructions.
Only run steps that are missing. Skip any step already done.
参考文件
references/setup.md
获取详细的搭建说明。
仅执行缺失的步骤,跳过已完成的步骤。

Phase 3: Write Tests

阶段3:编写测试用例

Read the reference file
references/test-writing.md
for the complete TWD test writing API and philosophy.
Before writing tests:
  1. Read the router config to identify all pages/routes
  2. Read page components to understand UI elements, forms, interactions
  3. Read the API layer to understand endpoints and response shapes
  4. Read existing tests to follow established patterns and conventions
Testing philosophy — flow-based tests:
  • One
    describe()
    per page or major feature
  • Each
    it()
    covers a complete user flow: setup mocks → visit → interact → assert outcome
  • 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
MockedComponent
from
twd-js/ui
, you can replace it in tests with
twd.mockComponent("Name", () => <div>Mock</div>)
. Always clear with
twd.clearComponentMocks()
in
beforeEach
.
Module stubbing — for hooks like
useAuth0
, wrap them in a default-export object so Sinon can stub them. ESM named exports are immutable and cannot be stubbed at runtime. Always
Sinon.restore()
in
beforeEach
.
参考文件
references/test-writing.md
获取完整的TWD测试编写API和理念。
编写测试用例前:
  1. 读取路由配置,识别所有页面/路由
  2. 读取页面组件,理解UI元素、表单和交互逻辑
  3. 读取API层,理解接口端点和响应结构
  4. 读取已有测试用例,遵循已有的模式和规范
测试理念——基于流程的测试:
  • 每个页面或主要功能对应一个
    describe()
  • 每个
    it()
    覆盖完整的用户流程:设置Mock → 访问页面 → 交互操作 → 断言结果
  • 不要为单个元素编写测试用例,而是测试完整的页面使用流程
  • 按场景分组流程:正常路径、空状态、错误处理、CRUD操作
组件Mock — 如果组件被
twd-js/ui
中的
MockedComponent
包裹,你可以在测试中使用
twd.mockComponent("Name", () => <div>Mock</div>)
替换它。务必在
beforeEach
中调用
twd.clearComponentMocks()
清除Mock。
模块Stub — 对于
useAuth0
这类Hook,将其包裹在默认导出对象中,以便Sinon可以对其进行Stub。ESM命名导出是不可变的,无法在运行时进行Stub。务必在
beforeEach
中调用
Sinon.restore()
恢复。

Phase 4: Run and Fix

阶段4:执行与修复

Read the reference file
references/running-tests.md
for running and debugging tests.
Run
npx twd-relay run
. If tests fail, read the error, fix the test, and re-run (max 5 attempts).
参考文件
references/running-tests.md
获取测试执行和调试说明。
运行
npx twd-relay run
。如果测试失败,读取错误信息,修复测试用例后重新执行(最多尝试5次)。

Phase 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

关键规则

  1. Always
    await
    async methods:
    twd.visit()
    ,
    twd.get()
    ,
    userEvent.*
    ,
    screenDom.findBy*
  2. Mock BEFORE visit — set up
    twd.mockRequest()
    before
    twd.visit()
  3. Clear mocks in
    beforeEach
    — always call
    twd.clearRequestMockRules()
  4. Tests run in the browser — no Node.js APIs
  5. Imports:
    describe
    /
    it
    /
    beforeEach
    from
    twd-js/runner
    ,
    expect
    from
    twd-js
    — never from Jest, Mocha, or Vitest
  1. 务必使用
    await
    处理异步方法:
    twd.visit()
    twd.get()
    userEvent.*
    screenDom.findBy*
  2. 先Mock再访问 — 在调用
    twd.visit()
    前设置
    twd.mockRequest()
  3. beforeEach
    中清除Mock
    — 务必调用
    twd.clearRequestMockRules()
  4. 测试在浏览器中运行 — 不可使用Node.js API
  5. 导入规则
    describe
    /
    it
    /
    beforeEach
    需从
    twd-js/runner
    导入,
    expect
    需从
    twd-js
    导入 — 绝不可从Jest、Mocha或Vitest导入