Self-paced learning
Lab: fix a bug with regression tests
In this lab you will repair the task-completion bug and add tests that protect its behavior. Autohand Code will implement a focused change after you define the contract. You finish when the repaired function completes only a matching task, preserves its input, and handles unknown IDs and empty lists correctly.
Before you start
Complete set up and explore in the same directory. Your baseline should have three tests with one intentional failure. Estimated time: 20–25 minutes. Keep the baseline commit so you can review exactly what the repair changes.
1. Define the contract
| Input | Expected result |
|---|---|
| Two open tasks, ID a | Task a completes; task b stays open |
| Unknown ID | Task values remain unchanged |
| Empty task list | Empty result |
| Any supported input | Original array and its task objects are not mutated |
This exercise assumes task IDs are unique. Handling duplicate IDs is a separate product decision and is not part of this repair.
2. Ask for the smallest repair
In the existing session, enter /plan off once you have reviewed the exploration result. Then submit:
Fix completeTask in src/tasks.js according to this contract:
only the matching ID is completed; unknown IDs preserve task values;
an empty array produces an empty result; input objects are not mutated.
Add unknown-ID and empty-array regression tests in test/tasks.test.js.
Keep the existing tests and exported API. Do not add dependencies.
Run npm test and explain why these tests detect the original bug.
Review any requested operations before approving them. The task requires source and test edits in this local practice project, not network or deployment work.
3. Verify independently
npm test
npm run check
git diff --check
git diff -- src/tasks.js test/tasks.test.js
Expected: all tests pass, syntax checking succeeds, and the diff contains the repair plus the new regression cases. There should be at least five tests if the agent added one separate test for each requested case. Test names can vary; inspect the assertions to confirm coverage.
4. Check test strength
Read the original failing assertion: it must still verify that the nonmatching task stays open. The unknown-ID test should compare task values before and after. The empty-array test should compare against an empty array. The immutability check should verify the input after calling the function.
A weaker test that checks only the requested task becomes complete would pass with the original broken implementation. Explain that difference in your own words before proceeding.
Troubleshooting
If tests pass but the function mutates input objects, add an assertion over the original data and rerun. If the agent deletes a failing assertion, ask it to restore the behavior check and fix the implementation. If unrelated files change, review why they were needed and remove only the changes you confirm belong to this task.
Checkpoint and independent exercise
You can show the passing test output and explain the exact logic change. Add a test for a task that is already complete and verify that it remains complete. Then continue to add a feature from a contract. Keep your source and tests in this directory for the later review lab.