dhpk-legacy-characterization-tests

dhpk-legacy-characterization-tests is a skill for Claude Code, Codex from hmj1026/dhpk. It costs 108 tokens per session (2,802 once invoked), scanned A, original, MIT.

A guide for writing characterization tests around old code that has few or no tests. These tests record what the code currently does before you change its structure.

In plain words
What is it for?
Use them before refactoring legacy controllers, models, or services, or before extracting code into new components. The guide covers observing behavior, locking it with tests, refactoring, and later replacing snapshots with focused unit tests.
Why use it?
They provide a safety net for refactoring by detecting unintended behavior changes. They are useful when the existing behavior matters even if it is not clearly documented.

Skill for Claude CodeCodex

Written for Claude Code and Codex: shipped in a Claude Code plugin, but also agents/openai.yaml present.

Part of the dhpk plugin — 39 skills, 1 command, 1 agent, 3 hooks shipped together

Good fit Use them before refactoring legacy controllers, models, or services, or before extracting code into new components. The guide covers observing behavior, locking it with tests, refactoring, and later replacing snapshots with focused unit tests.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/hmj1026/dhpk/dhpk-legacy-characterization-tests
View source ↗ hmj1026/dhpk
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Any agent
npx skills add hmj1026/dhpk --skill dhpk-legacy-characterization-tests
Clone the repo
git clone --depth 1 https://github.com/hmj1026/dhpk

Made for: Claude Code, Codex.

Or install dhpk, the plugin that ships this one along with the rest of its 39 skills, 1 command, 1 agent, 3 hooks.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for dhpk-legacy-characterization-tests

README.md
[![agentmods](https://agentmods.dev/badge/skills/hmj1026/dhpk/dhpk-legacy-characterization-tests/github.svg)](https://agentmods.dev/skills/hmj1026/dhpk/dhpk-legacy-characterization-tests)
Your own site
<a href="https://agentmods.dev/skills/hmj1026/dhpk/dhpk-legacy-characterization-tests"><img src="https://agentmods.dev/badge/skills/hmj1026/dhpk/dhpk-legacy-characterization-tests/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for dhpk-legacy-characterization-tests

Your own site · 80×15
<a href="https://agentmods.dev/skills/hmj1026/dhpk/dhpk-legacy-characterization-tests"><img src="https://agentmods.dev/badge/skills/hmj1026/dhpk/dhpk-legacy-characterization-tests.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 108 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,802 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00108 $0.02802
Opus 5.5 $0.00043 $0.01121
Sonnet 5.5 $0.00022 $0.00560
Haiku 4.5 $0.00011 $0.00280

Measured yesterday against content hash c408b77413f2, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

dhpk-legacy-characterization-tests scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

generated/claude-marketplace/package/modules/phpunit-5.7/skills/dhpk-legacy-characterization-tests/SKILL.md · 238 lines

How it starts

The opening of the file, as written. The whole thing — 238 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Legacy Code Characterization Skill

概述 — source: observed behavior and existing tests are SSOT; this skill owns workflow, not production semantics

針對無測試或低測試覆蓋率的 legacy code,撰寫 characterization tests(特徵測試)以鎖定現有行為,為後續重構提供安全網。

此技術來自 Michael Feathers 的《Working Effectively with Legacy Code》。核心思想: 先觀察程式實際在做什麼,把觀察到的輸出寫成 assertion,讓它通過(GREEN),再開始重構。

When to Use

  • 準備重構超過 300 行的 legacy class / controller
  • 需要為關鍵業務邏輯建立回歸測試,但無法修改現有結構
  • 測試覆蓋率提升計畫中的 legacy 模組
  • 提取 Service / Repository 前的行為快照

When NOT to Use

  • 新功能開發(用 TDD 流程)
  • 已有充分測試的模組
  • 純 bug fix 且根因明確

Characterization Test 生命週期

Characterization tests 不是永久存在的,有明確三段生命週期:

  1. 鎖定(Lock):寫 characterization test,執行確認 GREEN → 行為已鎖定
  2. 重構(Refactor):安全重構目標程式碼,測試持續 GREEN 代表行為不變
  3. 提取(Extract):重構完成後,抽出 pure unit test 承接輸出契約;characterization test 可退役或保留為回歸

典型路徑:LegacyClassCharacterizationTest(鎖定原始行為)→ 抽出 XxxNormalizer → XxxNormalizerTest(pure unit 承接)→ characterization test 退役


工作流程

Phase 1: 行為盤點

  1. 閱讀目標檔案,列出所有公開方法
  2. 識別外部依賴(DB query、外部 API、全域 state、singleton)
  3. 標記高風險方法(涉及金額、付款、庫存、資料寫入)
  4. 確認既有測試覆蓋狀況
  5. Render surface 盤點:當某功能使先前恆為空的欄位開始存放真實資料時,列出消費該欄位的所有 render surface(畫面/編輯、列印、匯出),不僅是編輯/API 路徑 — 未被任何測試演練的 surface 可能藏有列印排版錯位等潛在格式 bug
  6. 產出方法清單 + 依賴清單
格式:
| 方法 | 行數 | 依賴 | 風險等級 | 現有測試 |
|------|------|------|---------|---------|
| actionCheckout | 45-120 | DB, PaymentAPI | HIGH | 無 |
| calcDiscount   | 30-55  | 無              | LOW  | 無 |

大型 class(trait-heavy / 數千行)注意事項:

  • 不嘗試整體覆蓋;以高風險方法優先
  • 多 trait 混入時,明確標記測試覆蓋的是哪個 trait 的行為
  • 優先選擇邊界清楚的 public methods,避免深度依賴 HTTP 或 global state

Phase 2: 測試策略選擇

根據依賴類型選擇測試層級:

依賴類型 建議層級 特性
純計算(無外部依賴) Unit test 最快、最穩定
需要 DB,但可 mock Unit test + stub 隔離 DB,測試邏輯
需要真實 DB 讀寫 Integration test 使用 transaction rollback 隔離
需要外部 API Integration/Unit + mock mock 外部 API,觀察本地邏輯
需要完整 HTTP 流程 Functional / E2E 成本最高,謹慎使用

選擇原則:盡量往上移(unit 優先),只有在真正需要真實環境時才選 integration/functional。

Read the full file on GitHub · 238 lines

Files

What ships with it

3 files beside SKILL.md in the same directory: the scripts, references and assets a skill reads on demand. Not counted in the per-session cost; read them before you install if any of them is executable.

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday Changed · +1 lines c408b77413f2
  2. 1mo ago First seen · 237 lines · 108 tokens per session scan A 5038b17c2eb2

Subscribe to this mod's changes

dhpk-legacy-characterization-tests is a skill published in the GitHub repository hmj1026/dhpk (2 stars, last pushed today), licensed MIT. It adds 108 tokens to every session and 2,802 once invoked, about $0.0004 per session on Opus 5.5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-05.