Voltar ao blog

Write Tests That Catch Mistakes in AI-Assisted Code

13 de setembro de 20266 min
Ferramentas de desenvolvimento

Este artigo ainda não foi traduzido — exibindo o original em inglês.

When AI helps write code, it can also help write tests that repeat the same misunderstanding. A useful test starts from the expected behavior, not from the implementation it is supposed to check.

This exercise is an original teaching example. The flawed function below is deliberately constructed; it is not attributed to a provider or presented as a defect found in OpenTechnologyApp. The downloadable Node exercise includes a complete implementation and tests using only built-in modules.

Write a contract with a boundary

The function classifies a single synthetic item relative to a supplied calendar date. The rule is:

  • A completed item returns completed.
  • An unfinished item with a missing or invalid due date returns unknown.
  • A valid due date before the reference date returns overdue.
  • A due date equal to the reference date returns due-today.
  • A later due date returns upcoming.

Dates are validated YYYY-MM-DD calendar values. The reference date is required and invalid reference dates are rejected. The function does not read the system clock, query a database or decide who is allowed to see an item.

Those last boundaries make the example reproducible. A real application must also define its time-zone policy and perform authorization before supplying records.

Find the plausible mistake

A draft might contain:

▶ show code
if (item.dueDate <= today) return 'overdue';

That treats due-today items as overdue. A test using only yesterday's date will pass and miss the error. A test generated by copying the same comparison into its expected-value calculation will miss it too.

Write the expectation independently:

▶ show code
assert.equal(
  classify({ status: 'todo', dueDate: '2026-09-13' }, '2026-09-13'),
  'due-today'
);

The complete download defines classify; this short excerpt is not a standalone program. The fixed date is part of the input, so the test means the same thing tomorrow.

Exercise the other boundaries

The download includes six named tests: dates around the reference date, completed items, missing/invalid due dates, leap-day validation, invalid reference dates and unchanged input objects. The invalid-date cases include a date that has the right string shape but is not a real calendar date.

Keep the status/date contract small enough to inspect. The function is a teaching classifier, not a production API, an authentication policy or a universal task-schema implementation.

Run the exercise

Save the linked file as ai-control-date-exercise.mjs in an empty working directory with Node.js installed. It was checked locally using Node 22. Run:

▶ show code
node --test ai-control-date-exercise.mjs

Node's built-in test runner supports the --test command used here. No package install, credential or network connection is needed by this exercise. Node test-runner documentation.

Expected result: six passing tests. Read their names and assertions; do not treat a green count as proof of broader coverage.

Prove the test can fail

In a disposable copy, change the comparison in classify from < today to <= today, then run the same command. The boundary test should fail. Restore the original file and run it again.

That deliberate mutation was checked while preparing this guide: the original passed six tests and the modified copy failed the date-boundary test. This proves the test catches that specific mistake. It says nothing about every other possible bug.

If your own test still passes after introducing the error it is supposed to detect, inspect the fixture and expected value before adding more tests. A larger suite with the same mistaken assumption will not improve the evidence.

Connect the result to the change

In the change review, state the trigger, old result, corrected result and the test that distinguishes them. Record any inputs the exercise does not cover. Follow the build documentation workflow so the README, proposal and release claim use the same contract.

Delete the disposable mutated copy when finished. Keep the working exercise and the evidence you need; no live records or external artifacts require cleanup.

Entre em contato

Interessado em um tema? Deixe uma mensagem e escolha uma categoria. Também estou disponível para uma reunião de consultoria gratuita — entre em contato e combinamos.