LLMs are probabilistic engines that guess the next word. Unit tests are deterministic cages. To write flawless software, you must lock the probability inside the cage.
1The Specification Document
AI-TDD begins with a .spec.md file. You do not write code; you write the rules. You define the exact shape of the incoming data, the exact shape of the expected return data, and a bulleted list of every edge case (e.g., 'If token is expired, throw AuthError'). This markdown file acts as the ultimate Source of Truth for the LLM.
- Input: amount > 0
- Edge case: throw InvalidAmount
2The RED Phase (Generating Tests)
Do not ask the AI to write the code yet. You attach your Spec file and prompt: 'Based strictly on this Spec, generate a comprehensive Jest test suite.' The AI will generate the tests. You run the tests in your terminal. They will all fail (RED) because the logic doesn't exist. This is exactly what you want. You have now established the safety perimeter.
// Failing tests generated
3The GREEN Phase (Generating Logic)
Now you attach the Spec file AND the Test file to your prompt. Command: 'Write the implementation code to make these tests pass.' The AI generates the function. You run the tests. If they turn GREEN, the code is mathematically sound. If they remain RED, you feed the stack trace back to the AI. You iterate until everything is GREEN.
// Source code logic compiled
