Give me 5 minutes per test and I'll show you if it can fail

2026-11-28来源 dev.toEnglish

My AI once wrote a safety check for me. The check was green. It had been green since the day it was written. It would have stayed green if I had deleted the feature it was guarding.

I only saw it when I asked one question: what would turn this red? The answer was "nothing". A check that has never said no is a polite decoration.

Agents produce a lot of these. Another time, 13 tests came back green, and the line they were meant to test was commented out. The tests passed because they never ran my code.

The fix takes five minutes per test. It is called a negative control.

What a negative control is

A normal check asks: does it pass on the right thing?

A negative control asks: does it fail on the wrong thing?

You run the same check against something that is supposed to be broken. If the check still passes, it cannot tell good from bad, and a green from it means nothing.

You can do this by hand. Comment out the feature and run the test. If it stays green, you found a dead test. The trouble is that nobody does this twice, and agents never do it unless you force them.

Five minutes, step by step

Say the agent added a rate limiter and a test for it. Test file: test/rate-limit.test.mjs.

1. Find a way to switch the feature off. The cheapest ones:

  • an environment variable that disables it (RATE_LIMIT_DISABLED=1)
  • an old fixture or an old copy of the file
  • a file that you know is broken

If there is no switch, add one. Five lines. That is the real cost of this method, and it is worth paying: a feature you cannot switch off is hard to test anyway.

2. Run the test with the feature off. It must fail.

RATE_LIMIT_DISABLED=1 node --test test/rate-limit.test.mjs echo $? 

Exit code 0 means your test passes without the feature. It is dead. Rewrite it before you trust anything it says. Exit code 1 means the test can say no.

3. Put both runs into one gate. I use a small free tool for this, proof-gate. One file, MIT, no dependencies, Node 18+.

git clone https://github.com/cachly-dev/proof-gate cp proof-gate/proof-gate.mjs ./proof-gate.mjs 

proof-gate.json:

{ "tasks": { "rate-limit": { "checks": [ { "name": "acceptance", "run": "node --test test/rate-limit.test.mjs" } ], "controls": [ { "name": "says no to the version without the limit", "run": "node --test test/rate-limit.test.mjs", "env": { "RATE_LIMIT_DISABLED": "1" } } ] } } } 

4. Run it.

node proof-gate.mjs rate-limit 

When the test is alive:

TASK: rate-limit GATE: 0 (2/2) RECEIPT: .proof-gate/receipts/rate-limit.json NOTE: - 

When the test is dead, meaning the control passed:

TASK: rate-limit GATE: 1 (1/2) RECEIPT: .proof-gate/receipts/rate-limit.json NOTE: control passed, so the check cannot say no: says no to the version without the limit 

The gate counts a control as green only when it fails. That is the whole trick, and it is why the numbers read 1/2 there: the check passed, the control did not do its job.

Three quick ways to break a feature on purpose

If you have no switch yet, these work without changing the product code:

  • Comment out the one line the test is meant to cover, run the test, restore the line.
  • Run the test against an older commit: git stash your work, git checkout <old-sha> -- src/feature.ts, run, then check the file out again.
  • Point the test at a fixture file with wrong data in it.

All three are the same idea. Make the thing wrong, and watch for red. For the gate you want a repeatable version of it, which is why the environment variable is the better route.

The rule that keeps agents honest

The gate refuses to run a task that has no control at all:

TASK: rate-limit GATE: 2 (refused) RECEIPT: - NOTE: no controls: a check nobody has seen fail is not a check yet (add one that must fail) 

Exit code 2 is "refused", and refused is never green. So the agent cannot skip the step by leaving it out. Add this to CLAUDE.md or AGENTS.md so it knows what to do when the control is red:

## Controls - Every task needs at least one control: the same check pointed at something broken. It must fail. - If a control passes, fix the check, not the control. 

The second line closes a loophole. Without it, an agent that sees a passing control may simply delete the control.

Where a control does not help

Be honest about the limits.

  • A control proves the check can fail. It does not prove the check tests the right thing. A test of the wrong behaviour can have a perfect control.
  • Some features have no cheap off switch (a payment flow, an email send). There you need a fake that fails on purpose, and that takes longer than five minutes.
  • It does not replace reading the diff. It tells you which tests deserve less trust.

The one question to keep

For every test an agent hands you, ask: has this ever been red? If nobody can say yes, you do not know what the test does. That question costs nothing and takes ten seconds to ask.

Try it tonight

Pick the newest test your agent wrote. Switch its feature off. Run the test. If it stays green, you have found a dead one, and you did it in under five minutes.

If you want a bigger frame around this, the full version of the method (task packs, a model ladder, a gate and a ledger) is free. Leave your address at cachly.dev/forge and the download link arrives after you confirm. That is the only mention.

Do you remember the last test of yours that you caught being green for no reason?

I build cachly. It keeps what you explain to your AI: your rules, your decisions and the bugs you already solved. Claude Code, Cursor, Copilot and the others read it in every session, for you alone or for your whole team, and every lesson carries the name of whoever learned it.

Try it, in increasing order of commitment:

  • 30 seconds, no account: npx @cachly-dev/mcp-server@latest demo in any git repo. It reads your log locally and prints what an assistant would already know about the project. After npx fetches the package, the command makes no network calls.
  • Claude Code plugin: /plugin marketplace add cachly-dev/cachly-mcp, then /plugin install cachly-brain@cachly.
  • 5 minutes, free tier: npx @cachly-dev/mcp-server@latest autopilot writes the MCP configuration for whichever assistant you use.
  • Or from the web: cachly.dev · free tier, servers in Germany.

On Product Hunt from 1 December: cachly on Product Hunt. Follow it there if you want a note on launch day.

原文:https://www.a8q9.cn/en/article/give-me-5-minutes-per-test-and-i-ll-show-you-if-it-can-fail-ded6f5.html

相关文章