aboutsummaryrefslogtreecommitdiff
path: root/.agents/skills/write-tests/SKILL.md
blob: 5dcd2b9957576c12d19fd45328466216f0cdeae6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
---
name: write-tests
description: Write or extend Catch2 host tests (BHT) for TeachOS when the user explicitly asks for tests, including regression tests that reproduce a bug and stress tests. If the user only asks what to test, answer in prose and use this skill only once they ask for the code. Never changes production code.
---

# Write tests

## Hard limits

- Write only to `**/*.tests.cpp`, `**/*.stress.cpp` and `**/test_support/**`.
- Never edit production code.
  That includes temporary edits to check that a test can fail, and includes making a type printable (see below).
- No git commands that change state.
- If a test needs a seam in production code (an injection point, an extracted function), stop and report what is needed and why.
  Don't add it.

## 1. Confirm the contract

The interface under test must already exist (a header the human wrote, or a signature given in the request).
If it does not, stop and propose cases in prose instead.
State the source of the expected behavior: the header's docs, `docs/`, or the user's description.
Derive cases from the contract, not from reading the implementation.
If the contract is ambiguous, ask.

## 2. Learn local conventions

Read the neighbouring `*.tests.cpp` files and the `test_support/` of the library.
Reuse what exists: simulated memory, the bump frame allocator, the log buffer, the VFS and ext2 fixtures, and the `kapi` fakes that stand in for the architecture layer.
Add a new fake only if nothing fits.
Keep it minimal, put it under `test_support/`, and say so in the report.

## 3. Plan the cases

List the cases in a few lines before writing: happy path, boundaries, error paths (assert the specific `kstd::errc` when the contract names one), and state transitions.
If the request was broad, wait for a go-ahead.

## 4. Write

Match the neighbours:
- `SCENARIO("<behavior in plain words>", "[layer][subsystem]")` with `GIVEN` / `WHEN` / `THEN`.
  One assertion idea per `THEN`.
- Tags are layer plus subsystem, as in `[kapi][devices]` or `[filesystem][path]`.
  They become ctest labels, so don't invent new ones casually.
- Include order follows the neighbours: unit header, kstd, Catch2, standard library.
  Tests may use the full standard library.
- `REQUIRE` when later steps depend on it, `CHECK` for independent assertions.
- Deterministic only: no sleeps, no timing assumptions.
  Concurrency goes in `*.stress.cpp` (ThreadSanitizer), bounded in run time.
- To print a type in failures, add a `Catch::StringMaker` to the library's `test_support/string_makers.hpp`.
  Never touch kernel code for that.
- Binary assets (`*.img`) are out of scope.
  Ask the human.
- ASan, LSan and UBSan are on.
  Tests must be clean.
  Don't suppress reports.
- Test files need no license header.
  `REUSE.toml` covers them.

## 5. Run

New files are picked up by the next build.
- `cmake --preset bht`, then `cmake --build --preset bht-dbg`
- `ctest --preset bht-dbg --output-on-failure -R <name>` (or `-L <tag>`)
- Concurrency: the `bht-stress` presets.

If a test fails, decide which side is wrong.
Test wrong: fix the test.
Production wrong: stop, report the diagnosis with evidence, and leave the fix to the human.

## 6. Check the tests can fail

For each test, say which kind of bug it would catch.
Reason about it; don't break production code to find out.

## 7. Report

Files touched.
For each test, what it checks, in one line.
The contract source.
Cases deliberately not covered, and why.
Seams you would need in production code.
Anything you couldn't run.