Six new tests, all green, after a rewrite
You ask the agent to clean up `parse_tags`, a messy legacy function. It returns a tidy version plus six new tests, all green, and says behaviour is unchanged. The agent wrote those tests *after* the rewrite, by reading the rewrite. If the cleanup had changed what callers get back, would any of those tests go red?
After a refactor, the oracle is gone
A test's **oracle** is where its expected value comes from. For a refactor, the specification is "do exactly what the old code did", so the oracle must be the old code's actual outputs. The agent's new tests take their expected values from the **new** code. They confirm whatever it does now, including a changed order or different null handling, and they never run the old code. Green is only a proxy signal. So record the old outputs before the agent replaces the code. The later cost if you don't: a caller breaks on a quirk nobody pinned.
How to pin behaviour
Tests that record what existing code currently does are called **characterization tests**. To write them:
1. Pick edge inputs: empty, null/`None`, duplicates, invalid input (which error?), input whose order matters, calls with side effects.
2. Run the **old** code. Paste its actual result as the expected value, even if it looks wrong, and add a note saying so.
3. Check that all of them are green on the old code. Commit them on their own.
4. Tell the agent: refactor, and do not edit these tests.
5. Any red afterwards means behaviour changed. Tell: the diff edits a pinned test. Later cost: the behaviour change ships hidden behind a green suite.
Pinning parse_tags before the refactor
The current code splits on commas, strips and lowercases each tag, and skips blanks and repeats, keeping first-seen order. Each expected value below was copied from running it, not taken from a spec.
- Pin 1 covers stripping, lowercasing, a blank and a repeat in one input.
- Pin 2 uses `"b,A,a"`, where first-seen order differs from sorted order. With `"a,b"` the two orders match, so a switch to sorting could not turn it red.
- Pin 3 records an accident: `None.split` raises `AttributeError`. Nobody designed that, but a caller may catch it.
// Python
import pytest
from tags import parse_tags
def test_cleans_and_dedupes():
assert parse_tags(" a, ,A ") == ["a"]
def test_keeps_first_seen_order():
assert parse_tags("b,A,a") == ["b", "a"]
def test_none_raises_attribute_error():
with pytest.raises(AttributeError):
parse_tags(None)
The pins catch what the agent's tests missed
The agent's rewrite (below) returns `[]` for any falsy input, then sorts a set comprehension of the cleaned tags. Running old and new code:
| input | old | new | test |
|---|---|---|---|
| `"a,b"` | `[a,b]` | `[a,b]` | agent's: green |
| `"b,A,a"` | `[b,a]` | `[a,b]` | pin: **RED** |
| `None` | AttributeError | `[]` | pin: **RED** |
Ask the agent to restore first-seen order and remove the `None` guard. Change the behaviour later, on purpose.
// Python
def parse_tags(raw):
if not raw: return []
return sorted({t.strip().lower()
for t in raw.split(",") if t.strip()})
Record first, then refactor
Before a refactor, write characterization tests: record what the old code actually does (edge inputs, error types, order) and confirm the tests are green on the old code. Forbid the agent to edit them, and treat any edit to a pinned test in the diff as a tell. Tests written after the refactor can only agree with it.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.