lie-detector 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,169 @@
1
+ ---
2
+ name: test-quality-audit
3
+ description: Audit whether tests actually verify behavior instead of merely executing code. Use when the user asks "do my tests really test anything?", "why is coverage high but bugs still ship?", "mutation testing", "test quality", "weak tests", "AI-written tests", "tests pass but don't test anything", or wants a mutation score for a diff. Runs local quick checks and optional mutation testing for TypeScript/JavaScript (vitest or jest) and Python (pytest). No network calls, no commits, no pushes.
4
+ ---
5
+
6
+ # Test Quality Audit
7
+
8
+ Coverage answers "was this line executed?" It does not answer "would a test fail
9
+ if this line were wrong?" This skill finds the gap between the two.
10
+
11
+ ## When to use
12
+
13
+ Use this skill when the user:
14
+
15
+ - asks whether their tests are real or just coverage theater;
16
+ - has AI-written tests that pass but assert little;
17
+ - wants a mutation score for a diff, branch, or whole repository;
18
+ - asks why a refactor keeps breaking despite "100% coverage";
19
+ - wants to strengthen tests before a release or PR.
20
+
21
+ Do not use it to generate a full test suite from scratch. This skill audits and
22
+ suggests; the user stays in control of writing tests.
23
+
24
+ ## Core rule
25
+
26
+ **Never write to the user's source or test files without explicit approval.**
27
+ The CLI writes only `.lie-detector/report.md` and `.lie-detector/report.json`.
28
+ Mutation runs write a mutated file temporarily and restore it in a `finally`
29
+ block and on `SIGINT`/`SIGTERM`.
30
+
31
+ ## Workflow
32
+
33
+ ### 1. Establish scope
34
+
35
+ Ask only if the answer is not obvious from the conversation:
36
+
37
+ - Project root (default: current directory).
38
+ - Diff scope (default: merge-base against `origin/main`, then `HEAD~1`), or
39
+ `--all` for the whole project.
40
+ - Language (auto-detected from the files).
41
+
42
+ Prefer the smallest scope that answers the question. A diff audit is faster and
43
+ less noisy than a whole-repo audit.
44
+
45
+ ### 2. Run quick checks first
46
+
47
+ ```bash
48
+ npx lie-detector --quick --all
49
+ ```
50
+
51
+ This performs static checks only: no mutants, no test execution, no file writes
52
+ outside `.lie-detector/`. It catches tests with no assertions, tautologies,
53
+ self-comparisons, snapshot-only tests, mock-only tests, weak assertions, and
54
+ empty Python tests.
55
+
56
+ If the user wants a fast first pass, stop here and report the findings. If they
57
+ want proof, continue to mutation testing.
58
+
59
+ ### 3. Run mutation testing
60
+
61
+ ```bash
62
+ npx lie-detector --all --lang ts --max-mutants 50
63
+ npx lie-detector --all --lang py --max-mutants 50
64
+ ```
65
+
66
+ For a diff:
67
+
68
+ ```bash
69
+ npx lie-detector --base origin/main
70
+ ```
71
+
72
+ Useful controls:
73
+
74
+ - `--test-cmd "<command>"` when auto-detection is wrong.
75
+ - `--timeout <ms>` per mutant (default 30000).
76
+ - `--max-mutants <n>` to bound runtime.
77
+ - `--fail-under <n>` to make the command exit non-zero below a score.
78
+
79
+ Run a small sample first when the repository is large. Tell the user the
80
+ estimated cost before launching a long run.
81
+
82
+ ### 4. Read the report
83
+
84
+ Open `.lie-detector/report.md` for the human summary and
85
+ `.lie-detector/report.json` for exact numbers. The report contains:
86
+
87
+ - coverage and mutation score side by side;
88
+ - survived mutants with the changed expression;
89
+ - quick findings ranked by test;
90
+ - the weakest tests;
91
+ - a suggested test skeleton.
92
+
93
+ Use `references/report-format.md` when you need the JSON fields.
94
+
95
+ ### 5. Interpret, do not just repeat numbers
96
+
97
+ Use this order:
98
+
99
+ 1. **Coverage high, mutation low** → tests execute code but do not verify it.
100
+ This is the core finding.
101
+ 2. **Survived mutants in one area** → missing edge case or missing assertion.
102
+ Group them by behavior, not by line number.
103
+ 3. **Quick findings** → structural test smells. A test with no assertion cannot
104
+ kill a mutant even if it runs every line.
105
+ 4. **Equivalent mutants** → some survivors are semantically identical to the
106
+ original. Mark them as likely equivalent; do not demand a test for them.
107
+
108
+ A mutation score is evidence, not a grade. A 40% score in a glue-code module may
109
+ be acceptable; a 40% score in a pricing or auth module is not.
110
+
111
+ ### 6. Suggest strengthened tests
112
+
113
+ For the top 3 findings, show:
114
+
115
+ - the behavior that is untested;
116
+ - a minimal test that would kill the mutant(s);
117
+ - the assertion that makes it meaningful;
118
+ - any fixture or edge case the user must decide on.
119
+
120
+ Prefer one strong test over ten shallow ones. Do not suggest
121
+ `expect(result).toBeDefined()` as a fix.
122
+
123
+ After showing the suggestion, ask for approval before editing files. When
124
+ approved, make the smallest edit that adds the assertion and re-run the audit.
125
+
126
+ ### 7. Close the loop
127
+
128
+ Re-run the same scope after approved changes. Report the before/after mutation
129
+ score and which mutants are now killed. If the score did not move, say so and
130
+ explain why.
131
+
132
+ ## Output format
133
+
134
+ Keep the final answer short:
135
+
136
+ ```text
137
+ Coverage: 100% · Mutation: 12% (3/25 killed)
138
+ Quick findings: 6
139
+
140
+ Top gaps
141
+ 1. src/pricing.ts:42 — discount branch is never asserted.
142
+ 2. test/pricing.test.ts:18 — test only checks that the function was called.
143
+ 3. src/auth.ts:77 — boundary `>` is untested at the threshold.
144
+
145
+ Suggested next test
146
+ [one minimal test sketch]
147
+
148
+ I have not modified any files. Want me to add the first test and re-run?
149
+ ```
150
+
151
+ ## Safety and limits
152
+
153
+ - Local-first: no API keys, no telemetry, no network calls.
154
+ - No commits, pushes, or branch operations.
155
+ - Mutation runs are sequential in this MVP; no parallel workers.
156
+ - TypeScript/JavaScript via vitest or jest; Python via pytest. Other runners
157
+ need `--test-cmd`.
158
+ - Stryker and mutmut are not bundled; this skill uses its own small mutator.
159
+ - A timeout counts as a killed mutant. An error is reported separately.
160
+ - Files can be excluded with a `lie-detector-ignore` comment on the line or the
161
+ line above.
162
+
163
+ ## References
164
+
165
+ - `references/quick-patterns.md` — what each static pattern means and its
166
+ false-positive caveats.
167
+ - `references/mutation-operators.md` — operator list, diff scoping, timeouts.
168
+ - `references/report-format.md` — Markdown and JSON report fields.
169
+ - `references/troubleshooting.md` — detection failures, slow runs, restore.
@@ -0,0 +1,76 @@
1
+ # Mutation operators
2
+
3
+ The MVP mutator is intentionally small. It favors speed and predictable output
4
+ over exhaustive coverage.
5
+
6
+ ## TypeScript/JavaScript
7
+
8
+ | Original | Mutant |
9
+ | --- | --- |
10
+ | `<` | `<=` |
11
+ | `>` | `>=` |
12
+ | `<=` | `<` |
13
+ | `>=` | `>` |
14
+ | `===` | `!==` |
15
+ | `!==` | `===` |
16
+ | `==` | `!=` |
17
+ | `!=` | `==` |
18
+ | `+` | `-` |
19
+ | `-` | `+` |
20
+ | `*` | `/` |
21
+ | `/` | `*` |
22
+ | `%` | `*` |
23
+ | `&&` | `||` |
24
+ | `||` | `&&` |
25
+ | `return expr` | `return undefined` |
26
+ | `if (cond)` | `if (!(cond))` |
27
+ | string literal | `""` (or a non-empty marker when already empty) |
28
+ | boolean literal | flipped |
29
+ | numeric literal | `0` → `1`, non-zero → `0` |
30
+
31
+ Only nodes whose start line is in the selected diff are mutated. Use `--all` to
32
+ scan every source line.
33
+
34
+ ## Python
35
+
36
+ The same operator families, implemented with `ast`:
37
+
38
+ - comparisons (`<`, `<=`, `>`, `>=`, `==`, `!=`, `is`, `in`);
39
+ - boolean operators (`and`, `or`);
40
+ - arithmetic (`+`, `-`, `*`, `/`, `//`, `%`);
41
+ - `return expr` → `return None`;
42
+ - `if`/ternary condition negation;
43
+ - string, boolean, and numeric constants.
44
+
45
+ Python mutants are generated with `ast.unparse`, so comments and some formatting
46
+ are not preserved in the temporary mutated file. The original file is always
47
+ restored byte-for-byte from the backup.
48
+
49
+ ## Diff scoping
50
+
51
+ - Default base: `merge-base origin/main`, then `origin/master`, then `HEAD~1`,
52
+ then `HEAD` if the repository has a single commit.
53
+ - `--base <ref>` chooses an explicit base.
54
+ - `--all` ignores the diff and scans the whole project.
55
+ - Untracked files are included with all lines marked as changed.
56
+
57
+ ## Ignore marker
58
+
59
+ Put `lie-detector-ignore` on the line or the line above to skip a node:
60
+
61
+ ```ts
62
+ // lie-detector-ignore
63
+ return Date.now() > expiresAt;
64
+ ```
65
+
66
+ ## Result semantics
67
+
68
+ | Status | Meaning |
69
+ | --- | --- |
70
+ | `killed` | The test suite failed while the mutant was active. |
71
+ | `survived` | The suite passed; the behavior is not covered by an assertion. |
72
+ | `timeout` | The suite exceeded `--timeout`; counted as killed. |
73
+ | `error` | The runner failed before a meaningful test result; reported separately. |
74
+
75
+ Timeouts are counted as killed because a hanging test is a detectable reaction,
76
+ but the report keeps the timeout count visible.
@@ -0,0 +1,33 @@
1
+ # Quick patterns
2
+
3
+ Quick mode is static: it never executes the test suite and never writes to source
4
+ files. It catches structural smells that make a test incapable of detecting a
5
+ regression.
6
+
7
+ | Pattern | What it sees | Example | Caveat |
8
+ | --- | --- | --- | --- |
9
+ | `no-assertion` | Test body has no `expect(...)`/`assert(...)` call | `it("adds", () => { add(1, 2); })` | A test may assert through a helper; check the helper before editing. |
10
+ | `tautology` | A literal is compared to itself | `expect(true).toBe(true)` / `assert True` | `expect(true).toBe(false)` is a real (if odd) assertion. |
11
+ | `self-comparison` | A value is compared to itself | `expect(x).toBe(x)` / `assert x == x` | Intentional identity checks are rare; confirm with the user. |
12
+ | `snapshot-only` | Every assertion is a snapshot | `expect(tree).toMatchSnapshot()` | Snapshot tests are useful for UI shape; they are weak for logic. |
13
+ | `mock-only` | Every assertion checks a mock call | `expect(fn).toHaveBeenCalled()` | Interaction tests are legitimate; pair with a value assertion when possible. |
14
+ | `weak-assertion` | Only existence/truthiness is asserted | `expect(x).toBeDefined()` / `assert x is not None` | Sometimes the contract really is "not None". |
15
+ | `empty-test` | Python test body is only `pass` | `def test_x(): pass` | A placeholder may be intentional; flag it, do not delete it. |
16
+
17
+ ## Python-specific notes
18
+
19
+ - `pytest.raises(...)` counts as an assertion.
20
+ - `mock.assert_called_once()` is treated as a mock assertion, not a value
21
+ assertion.
22
+ - `unittest` helpers such as `self.assertEqual(...)` count as value assertions.
23
+ - A test with only `mock.assert_called_once()` is `mock-only`, not
24
+ `no-assertion`.
25
+
26
+ ## TypeScript/JavaScript notes
27
+
28
+ - `test(...)` and `it(...)` are recognized, including `.only`, `.skip`, and
29
+ `.concurrent`.
30
+ - `expect(...).not.toBe(...)` is handled.
31
+ - Nested `expect` calls inside a callback may not be parsed by the lightweight
32
+ matcher extraction; absence of a finding is not proof of a good test.
33
+ - `assert(...)` from Node's `node:assert` counts as an assertion.
@@ -0,0 +1,93 @@
1
+ # Report format
2
+
3
+ The CLI always writes two files under `.lie-detector/`:
4
+
5
+ - `report.md` — human-readable summary;
6
+ - `report.json` — stable machine-readable data.
7
+
8
+ ## JSON shape
9
+
10
+ ```json
11
+ {
12
+ "version": "0.1.0",
13
+ "generatedAt": "2026-12-06T00:00:00.000Z",
14
+ "root": "/absolute/project/path",
15
+ "language": "ts",
16
+ "mode": "mutation",
17
+ "base": "origin/main",
18
+ "usedAll": false,
19
+ "durationMs": 1234,
20
+ "testCommand": "npm test",
21
+ "coverage": { "percent": 100, "source": "coverage/coverage-summary.json" },
22
+ "files": ["src/pricing.ts", "test/pricing.test.ts"],
23
+ "mutants": [
24
+ {
25
+ "mutant": {
26
+ "id": "src/pricing.ts:42:10:1",
27
+ "file": "src/pricing.ts",
28
+ "line": 42,
29
+ "column": 10,
30
+ "operator": "> -> >=",
31
+ "original": ">",
32
+ "replacement": ">=",
33
+ "mutatedSource": "..."
34
+ },
35
+ "status": "survived",
36
+ "durationMs": 120,
37
+ "exitCode": 0,
38
+ "signal": null,
39
+ "stdout": "",
40
+ "stderr": ""
41
+ }
42
+ ],
43
+ "quickFindings": [
44
+ {
45
+ "file": "test/pricing.test.ts",
46
+ "line": 18,
47
+ "testName": "applies discount",
48
+ "pattern": "tautology",
49
+ "message": "Assertion compares a literal to itself.",
50
+ "severity": "high"
51
+ }
52
+ ],
53
+ "totals": {
54
+ "total": 1,
55
+ "killed": 0,
56
+ "survived": 1,
57
+ "timeout": 0,
58
+ "error": 0,
59
+ "mutationScore": 0,
60
+ "coveragePercent": 100,
61
+ "quickFindings": 1
62
+ },
63
+ "weakestTests": [
64
+ {
65
+ "file": "test/pricing.test.ts",
66
+ "line": 18,
67
+ "testName": "applies discount",
68
+ "findings": 1,
69
+ "patterns": ["tautology"]
70
+ }
71
+ ]
72
+ }
73
+ ```
74
+
75
+ ## Score rules
76
+
77
+ - `mutationScore = killed / total * 100`, rounded to one decimal.
78
+ - `killed` includes both `killed` and `timeout` results.
79
+ - `total` includes every attempted mutant, including errors.
80
+ - `mutationScore` is `null` when no mutants ran (for example, `--quick`).
81
+ - `coveragePercent` is `null` when no supported coverage file exists.
82
+
83
+ ## Coverage sources
84
+
85
+ The reader checks, in order:
86
+
87
+ 1. `coverage/coverage-summary.json` (Istanbul/v8, reads `total.lines.pct`)
88
+ 2. `coverage-summary.json`
89
+ 3. `coverage.json` (coverage.py, reads `totals.percent_covered`)
90
+ 4. `coverage/coverage.json`
91
+
92
+ The report never runs coverage itself. Generate coverage with the project's own
93
+ test command first.
@@ -0,0 +1,68 @@
1
+ # Troubleshooting
2
+
3
+ ## "No source files found"
4
+
5
+ - Confirm the project root: `--root <dir>`.
6
+ - Use `--all` if the repository has no commits or the diff is empty.
7
+ - Use `--base <ref>` if the default base is not the right comparison.
8
+ - Check `--lang`; a TypeScript-only run will not see Python files.
9
+
10
+ ## "Could not detect a test command"
11
+
12
+ Add a `test` script to `package.json`, or pass one explicitly:
13
+
14
+ ```bash
15
+ npx lie-detector --all --test-cmd "npm test -- --runInBand"
16
+ ```
17
+
18
+ For Python, the default is `python3 -m pytest -q`. If pytest is not installed,
19
+ use:
20
+
21
+ ```bash
22
+ npx lie-detector --all --lang py --test-cmd "python3 -m unittest discover -s . -p 'test_*.py'"
23
+ ```
24
+
25
+ ## Coverage shows `n/a`
26
+
27
+ The report only reads an existing coverage file. Run the project's coverage
28
+ command first, for example:
29
+
30
+ ```bash
31
+ vitest run --coverage --coverage.reporter=json-summary
32
+ python3 -m coverage run -m pytest && python3 -m coverage json
33
+ ```
34
+
35
+ ## A run is slow
36
+
37
+ - Lower `--max-mutants`.
38
+ - Prefer `--quick`.
39
+ - Narrow the diff with `--base`.
40
+ - Lower `--timeout` only if the suite is known to be fast.
41
+ - Avoid whole-repo runs in monorepos; audit one package at a time.
42
+
43
+ ## A file was not restored
44
+
45
+ The runner restores in `finally` and installs `SIGINT`/`SIGTERM` handlers. If a
46
+ process is killed with `SIGKILL`, the handler cannot run. Backups are kept in
47
+ `.lie-detector/backup/`; restore manually by copying the `.bak` file over the
48
+ source. The backup path is printed in the report directory.
49
+
50
+ ## The mutation score looks too low
51
+
52
+ - Check whether the tests actually assert return values.
53
+ - Look for survived mutants in generated or glue code; consider
54
+ `lie-detector-ignore`.
55
+ - Some mutants are equivalent and cannot be killed by any realistic test.
56
+ - A timeout counts as killed; check the timeout count in the report.
57
+
58
+ ## The mutation score looks too high
59
+
60
+ - Confirm the test command is running the tests you think it is.
61
+ - A test suite that fails for an unrelated reason will mark every mutant killed.
62
+ - Run the suite once on the original code before trusting a mutation run.
63
+
64
+ ## Quick findings are noisy
65
+
66
+ Quick mode is heuristic. Treat findings as prompts for review, not as failures.
67
+ A snapshot test or a mock interaction test can be legitimate; combine it with a
68
+ value assertion when the behavior matters.