In this lab you will add task filtering to the practice project. You will define accepted statuses and error behavior before asking Autohand Code to implement the function. The checkpoint is a passing test suite that proves filtering, input preservation, and compatibility with the earlier repair.

Before you start

Complete fix a bug, with all existing tests passing. Estimated time: 20–25 minutes. This lab introduces a new exported function without changing the completion or counting functions.

1. Choose the feature contract

Add filterTasks(tasks, status). Require an explicit status rather than an implicit default.

Status Result
all A new array containing every task in its original order
open Tasks whose completed value is false
done Tasks whose completed value is true
Any other value, including omitted status Throw TypeError

For valid statuses, an empty input produces an empty array. Do not mutate the input. Deep-cloning task objects is not required; callers should not rely on the returned objects being new copies.

2. Plan the change

Enter /plan on in your interactive session:

Plan filterTasks(tasks, status) for src/tasks.js.
Statuses: all, open, done. Any other value throws TypeError.
Return a new array, preserve order, and do not mutate input objects.
Describe tests using a mixed list of complete and open tasks.
Preserve completeTask and countOpenTasks. Do not implement yet.

Check that the plan covers all three valid statuses and the invalid-status path. If it introduces a dependency or changes existing APIs, ask for a focused plan.

3. Implement with tests

Enter /plan off when ready, then submit:

Implement the approved filterTasks contract and its tests.
Use a mixed fixture so open and done produce different results.
Cover all, open, done, invalid status, omitted status, and empty input.
Assert order, a new result array, and unchanged input values.
Run npm test and npm run check; report the actual results.

4. Inspect the result

npm test
npm run check
git diff -- src/tasks.js test/tasks.test.js

Expected: both the earlier completion tests and the new filtering tests pass. The exported function is present, invalid status throws TypeError, and no dependency was added. Exact test counts depend on whether cases are separate tests or grouped assertions; evaluate the covered behavior.

Troubleshooting

If a test for open passes with an implementation that returns everything, the fixture probably contains only open tasks. Add a completed task. If all returns the original array, assert that result and input are different array references. If an invalid status silently produces an empty array, check that the test expects an exception rather than any false-like result.

Checkpoint and independent exercise

Explain the difference between a new array and deeply cloned objects. Add a test proving order is preserved for two matching tasks separated by a nonmatching task. Then continue to review and handoff.