Community寫作與編輯github.com

MoonshotAI/test

用於編寫或審查測試,或當被問及如何撰寫一個好的單一測試時。它將「測試合約/責任,而非實作」的原則編碼為每個測試的規則——為每個 `it` 命名並結構化一個行為,透過公開表面驅動,僅樁接真正的外部邊界,透過文件化開關控制時間與設定,並保持測試清晰、隔離且易於重構。同一套規則同時驅動編寫(寫入模式)與稽核既有測試(審查模式)。

test 是什麼?

MoonshotAI/test 是一個 AI 代理技能,旨在指導開發者與 AI 代理撰寫和審查單元測試。其核心原則是「測試合約/責任,而非實作」,並為每個 `it` 區塊提供具體的操作規則。該技能支援兩種模式:**寫入模式**用於建立新測試,**審查模式**則用於稽核既有測試或差異,兩者共享相同的規則集,僅在行動姿態上有所不同。 此技能涵蓋多項關鍵實踐:每個 `it` 僅驗證一個行為;透過公開控制平面驅動系統,並斷言可觀察的效果(如回傳值、持久化狀態、事件);僅樁接真實的外部邊界(如遠端服務、網路或時間控制);保持測試 DAMP(描述性且有意義的短語)風格,使用字面值做斷言;確保測試隔離且無隨機失敗(hermetic),並在 `afterEach` 中還原所有模擬與環境變數。它還定義了品質標竿 **CCCR**:清晰性(Clarity)、完整性(Completeness)、簡潔性(Conciseness)和韌性(Resilience)。 審查模式提供一個嚴謹的檢查清單,依序檢查:合約而非實作、每個 `it` 單一行為、邊界紀律、DAMP 期望值、隔離性,以及 CCCR 閱讀。違規時會報告檔案與行號、違反的規則及修復建議。此技能支援任何具有測試框架的專案,但特別適用於 TypeScript/JavaScript 生態系統(如 Vitest 或 Jest),並可結合 Claude Code、Cursor 或 Codex 等 AI 代理工具使用。它透過將最佳實踐編碼為可重複的規則,幫助團隊提升測試品質與可維護性。

相容平台~Claude Code~Codex CLI✓Cursor
npx skills add https://github.com/MoonshotAI/kimi-code/tree/main/.agents/skills/agent-core-review/test

Installed? Explore more 寫作與編輯 skills: steipete/notion, langchain-ai/langchain, bytedance/podcast-generation · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

測試 — 撰寫與審查

每個測試的規則,將一個原則具體化:測試合約/責任,而非實作。這是撰寫單一 it 的操作指南,也是審查單一 it 的透鏡。

兩種模式,一套規則

  • 寫入模式 — 編寫測試。套用以下規則來產出測試。
  • 審查模式 — 稽核既有測試或測試差異。將相同的規則作為檢查清單;針對每個違規,回報 file:line、違反的規則以及修復方式。請參閱末尾的「審查模式」。

兩種模式的規則完全相同——只有行動姿態不同(產出 vs. 稽核)。

測試合約,而非實作

  • 透過系統的公開控制平面驅動,並對可觀察的效果(回傳值、持久化狀態、觸發的事件、注入的訊息)進行斷言,絕不觸及原始碼細節。
  • 透過協作者的合約——也就是介面加上其識別符——來解析協作者,而非綁定具體實作的模組。
  • 不要為了測試而觸及私有欄位或新增後門。如果你覺得有必要,表示縫合點錯了——修正設計,而不是修正測試。

每個 it 一個行為

每個 it 涵蓋恰好一個責任/情境。如果名稱需要出現「和」字,就該拆分了。

it('當呼叫者未經授權時回傳 401', ...);
it('當同一個 tick 重複發生時,不會重複觸發', ...);

命名與結構

  • describe('<區塊> (<責任>)' — 命名責任,而非類別。
  • it(...) 讀起來像一個句子,但它仍然必須編碼三件事——行為/方法、狀態或條件,以及預期結果:it('<行為> 當 <條件>,<結果>')。一個像 當 Y 時做 X 卻沒有結果的名稱,模糊到無法有效地失敗。
    • 使用空格,而不是 Java 風格的 method_state_outcome 底線——那種慣例存在,只是因為 Java 測試方法名稱不能包含空格。以字串命名的測試讀起來就像一個正常的句子。
    • 好的:it('當呼叫者未經授權時回傳 401') · it('推進游標且不會在重複的 tick 上重複觸發')
    • 不好的:it('正常運作') · it('正確處理驗證') — 沒有條件,沒有結果
  • 安排 / 行動 / 斷言(Arrange / Act / Assert)。在需要輔助閱讀時,使用簡短的 // Given // When // Then 是可以的;但不要機械地貼在簡單的測試上。

建立小型測試框架

當多個測試共享設定時,撰寫一個工廠函式(rig()、createHost(),或符合程式碼庫的任何名稱),回傳測試所需的最小表面。測試透過這個框架來取得所需;它們不需要每次都重建整個世界。保持框架單純:僅負責接線,不做任何斷言。

僅樁接真正的外部邊界

預設使用真實的協作者,並以生產環境的方式接線。樁接那個最小的、真正外部的縫合點:

  • 遠端 / 模型 / 服務邊界 — 監控合約方法(介面),並擷取系統透過它傳送的內容。不要啟動真實的外部元件。
  • 網路 / 其他處理序邊界 — 在邊界處樁接,而非內部。
  • 時間、計時器、抖動 — 使用系統暴露的文件化控制開關(環境變數、注入的時鐘、手動 tick)。不要使用 fake timers 或真實的 setTimeout 來驅動時間。
  • 環境 / 設定開關通常在建置時快照 — 在建構受測系統之前設定它們,並在 afterEach 中還原。

保持測試 DAMP,並讓原因緊鄰結果

  • DAMP 勝於 DRY:在斷言中使用字面預期值;不要使用與受測程式碼相同的邏輯來計算預期值。
  • 將關鍵的前置條件放在 it(或它的框架)內部,讓讀者能看到原因緊鄰結果。將 beforeEach 保留給橫切面的管線工作(環境變數快照、清理),而不是用來隱藏情境的設定。
// 好 — 預期值是讀者可以直接檢查的字面值。
expect(discount).toBe(15);
// 不好 — 重新推導預期值;鏡像了實作。
expect(discount).toBe(price * rate);

只斷言相關的內容

斷言能夠證明合約的效果。使用匹配器 / 部分物件比對來忽略無關的欄位。不要斷言內部計數器、呼叫順序,或使用者無法依賴的形狀。

隔離並清理(無隨機失敗)

每個測試都必須是封閉的且順序獨立的。在 afterEach 中:

  • 還原每個 mock / spy
  • 還原你碰過的每個環境變數(在 beforeEach 中快照)
  • 釋放 host / container 並重設其參考

不依賴於掛鐘時間、執行順序或殘留的磁碟狀態——當狀態持久化時,為每個情境提供各自獨立的身份 / 工作空間。

品質標竿:CCCR

在完成之前,根據以下條件檢查每個測試:

  • 清晰性(Clarity) — 陌生人可以僅從失敗訊息就判斷出哪裡出了問題。
  • 完整性(Completeness) — 涵蓋了責任的成功、錯誤和邊界路徑。
  • 簡潔性(Conciseness) — 沒有重複或推測的情境;每個 it 一個情境。
  • 韌性(Resilience) — 在內部重構後,無需修改測試(因為它斷言的是合約,而非實作)。

每個檔案的情境標頭

每個測試檔案以簡短的標頭註解開始:情境、所斷言的責任、接線方式(哪些協作者是真實的,與單一樁接的邊界為何),以及如何執行。

審查模式 — 稽核既有測試

將上述規則作為檢查清單,套用至範圍內的每個測試(一個檔案、一個差異或一個具名的 it)。對於每個命中,回報 file:line + 違反的規則 + 修復方式;除非被要求,否則不要重寫。以一個合約問題作為引導:這個測試證明了什麼可觀察的行為?它能在重構後存活嗎?

依序檢查:

  1. 合約,而非實作 — 斷言可觀察的效果,而非私有欄位、呼叫順序或使用者無法依賴的內部形狀。
  2. 每個 it 一個行為 — 名稱承載行為 + 條件 + 結果;名稱中有「和」字表示需要拆分。
  3. 邊界紀律 — 僅樁接真正的外部縫合點;時間透過文件化的開關驅動,而非 fake timers / 真實的 setTimeout。
  4. DAMP 期望值 — 預期值是字面值,而非由受測程式碼的邏輯重新推導。
  5. 隔離性 — mocks / spies / 環境變數 / host 在 afterEach 中還原;不依賴掛鐘、執行順序或殘留的磁碟狀態。
  6. CCCR 閱讀 — 清晰性、完整性(成功 / 錯誤 / 邊界)、簡潔性、韌性。

以證據 + 修復的方式回報發現,例如「foo.test.ts:42 對 service.internalMap 進行斷言(合約)— 改為斷言回傳值。」如果測試通過了透鏡檢驗,簡短說明即可;對某個規則保持沉默表示它符合規定。

快速檢查清單(撰寫與審查)

  • 透過合約解析;沒有具體實作的導入
  • 每個 it 一個行為;名稱承載行為 + 條件 + 結果;AAA
  • 僅樁接真正的外部縫合點;時間透過開關驅動,而非 fake timers
  • 字面期望值;僅相關的斷言
  • mocks / 環境變數 / host 在 afterEach 中還原;封閉,無隨機失敗
  • 已完成 CCCR 閱讀

Individual skills in this repo

This repo contains 13 individual skills — each has its own dedicated page.

MoonshotAI/agent-core-dev

Use when developing in packages/agent-core-v2 (the DI × Scope agent engine) — adding or modifying a domain Service, choosing a LifecycleScope, wiring DI dependencies, splitting a domain across scopes, owning or migrating a config section, gating behavior behind an experimental flag, raising coded errors, working on the permission system, writing DI/Scope tests, porting business logic from agent-core (v1) to v2, triaging a main-branch commit against v2, or exposing a v2 domain over server-v2 while keeping the /api/v1 wire contract compatible with released clients. Self-contained guide organized by development stage (orient → design → implement → test → verify) plus align workflows for v1→v2 migration, main-branch commit triage, and server-v2 wire exposure; each file carries the rules, examples, and red lines for its step.

MoonshotAI/agent-core-review

Use ONLY for code review and test write/review guidance in `packages/agent-core-v2` (the DI × Scope agent engine). Does NOT apply to the legacy `packages/agent-core` or to any other package — for those, do not load this skill. Groups the review and testing lenses used for agent-core-v2 — `slop` (single-level-of-abstraction / layered error-handling review, invoked only on explicit request) and `test` (contract-driven per-test rules for both authoring and reviewing tests). Apply the sub-skill that matches the task; do not apply `slop` unprompted.

MoonshotAI/consolidate

Apply an approved sub-skill grouping by moving user-specified skills into a parent bundle, with timestamped backups of every modified directory.

MoonshotAI/gen-changesets

Use when generating changesets in the kimi-code repository, including package bump selection, internal package and CLI bundle handling, bump levels, major confirmation, and English changelog wording.

MoonshotAI/gen-docs

Update Kimi Code CLI user documentation after meaningful code changes that affect product behavior or user experience.

MoonshotAI/kimi-datasource

Universal data-source assistant. Use this skill when the user wants external structured data such as stocks, financial reports, technical indicators, A-share/HK/US markets, global macroeconomics, Chinese enterprise registry information, arXiv papers, Google Scholar results, or Chinese laws/regulations and judicial cases. This plugin exposes tools via MCP server `plugin-kimi-datasource_data`; call them in the flow `mcp__plugin-kimi-datasource_data__get_data_source_desc` → `mcp__plugin-kimi-datasource_data__call_data_source_tool`.

MoonshotAI/pre-changelog

Use before merging a kimi-code release PR to preview the user-facing CLI changelog in Chinese. Reads the changelog that changesets pre-generated in the release PR, then reuses sync-changelog's strip / classify / translate logic to render a Chinese preview. Writes no files.

MoonshotAI/review

Analyze the available skill set and recommend candidate groups that could be consolidated into sub-skill bundles. Read-only — proposes a plan, does not move files.

MoonshotAI/slop

Invoke only when the user explicitly asks to review code through the "single level of abstraction / layered error handling" lens — a function does only its own layer's business logic while errors are handled above or below. The agent reports detections, raw-count measurements, and move directions. Apply only when the user explicitly requests this lens.

MoonshotAI/sub-skill

Discover and reorganize the skill inventory into hierarchical sub-skill bundles. Use when the user asks to review, group, or consolidate skills into a parent bundle.

MoonshotAI/sync-changelog

Use after a release succeeds, when maintainers need to sync apps/kimi-code/CHANGELOG.md into docs/en/release-notes/changelog.md and docs/zh/release-notes/changelog.md, then open a PR on a dedicated branch.

MoonshotAI/translate-docs

Translate and sync bilingual user documentation between docs/zh/ and docs/en/ following the source-of-truth rules in docs/AGENTS.md.

MoonshotAI/write-tui

Use when writing or modifying the kimi-code terminal UI in apps/kimi-code/src/tui — components, dialogs/selectors, slash commands, themes, streaming render, or the KimiTUI controllers. Covers the architecture, where new features go, test placement, the theme system mechanics, and the dialog interaction/visual spec (DESIGN.md).

相關技能