CAPSTONE: THE GREEN SUITE
Three bugs, a green build and a 25-minute wait
A checkout suite is green. It tests the discount at 5000 and 20000, mocks the payment provider by hand, and uses sleep(2000) before reading stock, with CI retrying failures three times. Which statement is the safest?
This lesson combines four earlier ones. An off-by-one at a rule’s edge hides from tests that only use middle values, as boundary values showed. A mock that answers with the old response shape passes while production breaks, the trap in the mock that lied. A sleep is a guess about a race, as in the flaky test from a race (the cousin of the data race itself). And a retry that discards the first failure erases the evidence, per retries hide the bug. The honest suite fixes each at its source, and then spends the time it saved with test selection.
// the suite the team trusts
test('discount', () => expect(discount(5000)).toBe(0)); // middle values only
test('pays', () => expect(parse({ status: 'succeeded' }).paid).toBe(true)); // hand-written mock
test('stock', async () => { place(order); await sleep(2000); expect(stock()).toBe(9); });
// CI: retry failed tests x3, keep the last result// the honest suite
for (const [sub, want] of [[9999, 0], [10000, 1000], [10001, 1000]]) expect(discount(sub)).toBe(want);
expect(shape(mock)).toEqual(shape(await provider.sandbox())); // contract, not a guess
for (const order of [['write','read'], ['read','write']]) { reset(1); expect(place(order).ok).toBe(true); }
// CI: record every attempt; a pass on retry is a failure with a label Open ONE CHANGE AT A TIME and watch the strip. The first run is green with three bugs in the code, and the verdict line goes red. Each change then names one more bug, and the race is the last to be named. After the fix, the last run is green and means something.
This suite is honest: it names all three bugs. Inject one change, before the CI run, that quietly stops it naming one of them. Then try the change that looks risky but is not.
GOAL: a bug in the code is not named by any failing test
Write checkoutSuite(app, seed). Return an array of failure messages; an empty array means green. Spec: app.discount(subtotal) gives 10% off from 10000 inclusive (so 0 below, 1000 at 10000). app.parsePayment(resp) returns { paid } and must read what the provider really sends: app.providerReal() is the provider sandbox response, app.providerMock() is the team’s hand-written mock. app.placeOrder(steps) runs the order write and the inventory read in the order you give, ['write','read'] or ['read','write'], and returns { ok }; app.placeOrderLive() uses real timing, so it flakes on the buggy service. Orders use stock, and tests leave stock behind: call app.reset(n) to set it. The build must be red for each bug on every seed, green on the correct service on every seed, and no retry may turn a real failure green.
Any checkout, billing or booking service with a long CI: a 20-minute suite with auto-retry, mocks of payment providers (Stripe, Paystack, Flutterwave), discount, fee and tax rules with thresholds, and stock or balance reads after a write. The review comment is: this test is green, show me the change that makes it red.
“Your suite is green, slow and flaky. What do you do?” Do not start with parallelism. Say you would check what each test can fail on: add edge-value tests, verify mocks against the provider with a contract, replace sleeps with controlled schedules, record every retry attempt, and only then make it fast with test selection and a full run on merge. Close with how you would measure it: seed bugs on purpose and see which ones go red.