{"success":true,"name":"pin-legacy-behavior-with-characterization-tests-before-refac","description":"About to refactor old code that has no tests","category":"ai-brimind","path":"ai-brimind/pin-legacy-behavior-with-characterization-tests-before-refac/SKILL.md","file":"SKILL.md","content":"---\nname: pin-legacy-behavior-with-characterization-tests-before-refac\ndescription: \"About to refactor old code that has no tests\"\n---\n\n# Pin Legacy Behavior With Characterization Tests Before Refactoring It\n\n## When to use\nYou're about to restructure, clean, or rewrite existing code that works in production but has little or no test coverage.\n\n## Example request\nMessy function refactored 'carefully' by hand → a subtle rounding difference ships unnoticed; tests written BEFORE the change would have caught the drift. — Old code contains a weird branch nobody understands → deleting it as 'obviously dead' breaks a real customer flow; the weirdness was load-bearing behavior. — Characterization test asserts what the code DOES, not what it should do → even the ugly outputs get pinned; the point is detecting change, not judging correctness. — Refactor done under green pinned tests → any behavior change fails immediately; that's the safety-net pattern for working with legacy code.\n\n## Instructions\nAct as a museum restorer who photographs every angle of the artifact before touching it, so any accidental change is instantly visible. Before refactoring untested working code: 1) Identify the boundary you'll preserve — the function's inputs and outputs, the endpoint's responses, the module's public surface — the refactor may change everything inside it and nothing observable at it. 2) Write characterization tests that capture what the code ACTUALLY does today with real, representative inputs — including the ugly, surprising outputs; you are photographing reality, not describing the ideal. Do not fix behavior while pinning it. 3) Feed the edges too: empty, weird, and boundary inputs often hide the load-bearing quirks that make old code scary. 4) Run the pinned tests, confirm green, and only then refactor in small steps, keeping them green after each step — a red test means you changed behavior, which is a separate decision to make consciously, not a side effect. 5) If a behavior looks like a bug, record it and decide its fate as its own change AFTER the refactor, so one commit never mixes restructuring with behavior change. If you can't reach the code to test it, extract the smallest testable seam first. Only start restructuring once the net is confirmed green, and explain why the tests prove the refactor preserved the real observable behavior, not just your understanding of it.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #967\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}