tripwire-e2e 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.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +168 -2
  3. package/bin/tripwire.js +8 -0
  4. package/operators/_util.js +114 -0
  5. package/operators/agent-proposed.js +41 -0
  6. package/operators/auth-drop.js +30 -0
  7. package/operators/dead-control.js +23 -0
  8. package/operators/double-apply.js +20 -0
  9. package/operators/fake-success.js +21 -0
  10. package/operators/field-swap.js +42 -0
  11. package/operators/hide-control.js +30 -0
  12. package/operators/index.js +77 -0
  13. package/operators/item-drop.js +36 -0
  14. package/operators/item-dup.js +20 -0
  15. package/operators/label-swap.js +30 -0
  16. package/operators/missing-row-action.js +24 -0
  17. package/operators/number-drift.js +40 -0
  18. package/operators/silent-fail.js +19 -0
  19. package/operators/stale-read.js +34 -0
  20. package/operators/text-change.js +43 -0
  21. package/operators/validation-off.js +21 -0
  22. package/operators/wrong-link.js +28 -0
  23. package/operators/wrong-owner.js +41 -0
  24. package/package.json +41 -4
  25. package/policies/tripwire.clean.policy.yaml +23 -0
  26. package/policies/tripwire.mutant.policy.yaml +13 -0
  27. package/src/actions.js +59 -0
  28. package/src/agent.js +30 -0
  29. package/src/browser/mutate.js +113 -0
  30. package/src/browser/recorder.js +80 -0
  31. package/src/cli.js +37 -0
  32. package/src/commands/check.js +28 -0
  33. package/src/commands/fix.js +199 -0
  34. package/src/commands/list.js +24 -0
  35. package/src/commands/proxy.js +28 -0
  36. package/src/commands/report.js +11 -0
  37. package/src/commands/run.js +310 -0
  38. package/src/commands/sabotage.js +74 -0
  39. package/src/config.js +52 -0
  40. package/src/jsonpath.js +58 -0
  41. package/src/kane.js +150 -0
  42. package/src/ledger.js +53 -0
  43. package/src/mutant.js +26 -0
  44. package/src/profile.js +34 -0
  45. package/src/proxy.js +289 -0
  46. package/src/report.js +182 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 da-b0s
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,169 @@
1
- # Temporary Holding Version
1
+ # Tripwire
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ **Break the app on purpose to find out whether a green Kane suite means anything.**
4
+
5
+ Coding agents write the app and its tests, and they are very good at writing tests that pass.
6
+ A passing end-to-end test only matters if it would have failed when the app was broken, and
7
+ nobody checks that. Tripwire does.
8
+
9
+ A proxy sits between the browser and your app and breaks it in one small, realistic way at a time:
10
+ a wrong total, a failed save reported as success, a missing button, another user's order in your
11
+ list. It never touches your code. [Kane CLI](https://github.com/LambdaTest/kane-cli) replays your
12
+ suite against each broken version with healing off. Every bug the suite walks past goes to your
13
+ coding agent, which adds one plain-English assertion, and Kane proves it: the strengthened test
14
+ **passes on the real app and fails on the broken one**.
15
+
16
+ ![tripwire score](https://raw.githubusercontent.com/da-b0s/tripwire/main/demo/shop/.tripwire/report/badge.svg)
17
+
18
+ ## Results
19
+
20
+ On the demo shop, with a 6-test suite written by a fresh Claude Code session that was never told
21
+ about Tripwire (run of 18 Nov 2026, Kane CLI 0.10.5):
22
+
23
+ | | Score | Killed | Survived | Masked by heal | Not reached |
24
+ |---|---|---|---|---|---|
25
+ | Agent's first-draft suite | **83.3%** | 25 | 4 | 1 | 2 |
26
+ | After `tripwire fix` (2 tests strengthened) | **90%** | 27 | 2 | 1 | 2 |
27
+
28
+ Bugs the first-draft suite walked past, all green in Kane:
29
+
30
+ - **Another customer's order (#91002) listed in "My orders".** Fixed: `cancel_order_test.md` now
31
+ checks the list holds exactly the user's own orders.
32
+ - **A product price shown 10% too high** ($19.80 instead of $18.00). Fixed: `browse_test.md` now
33
+ checks a price in the product grid.
34
+ - The "Keep order" button missing from the cancel dialog.
35
+ - The "+" and "−" quantity buttons swapping labels (the test presses them by accessibility name,
36
+ so it never looks at what the user sees).
37
+
38
+ Masked by healing: a one-off "session expired" (401) during cancel. Strict replay failed; with
39
+ Kane's default adaptive healing the test re-authored itself past the sign-in page and passed.
40
+
41
+ Each fix was proven on the first attempt by two Kane runs: pass on the real app, fail with the bug
42
+ armed. One honest caveat: the order-list fix names the injected order ("no other rows such as
43
+ #91002"); its "exactly 2 orders" check is what catches the bug, but naming the planted value is
44
+ overfitting, so the fix prompt now forbids it. Score = killed ÷ (killed + survived + masked) over 32 of 40 selected mutants; the remaining
45
+ 8 are queued (`tripwire run --resume`). The full report
46
+ ([live page](https://da-b0s.github.io/tripwire/), [source](https://github.com/da-b0s/tripwire/blob/main/docs/index.html)) has a screenshot of
47
+ every broken page the suite passed on, and Kane's own triage of every failure.
48
+
49
+ **Cost:** baseline replay of 6 tests ≈ 4 credits; mutant runs ≈ 1 to 5 credits each (a failing
50
+ replay costs more because Kane investigates it); a heal check ≈ 15 to 35; each proven fix ≈ 25.
51
+
52
+ ## Quickstart
53
+
54
+ You need Node 18+, Chrome, and a [TestMu AI](https://www.testmuai.com) account (free credits).
55
+
56
+ ```bash
57
+ npm install -g @testmuai/kane-cli tripwire-e2e
58
+ kane-cli login
59
+ ```
60
+
61
+ Point your `*_test.md` files at the proxy (`url: http://localhost:8080` in the frontmatter), start
62
+ your app, then:
63
+
64
+ ```bash
65
+ tripwire run # baseline, generate mutants, replay, score, report
66
+ tripwire fix # hand each survivor to your agent; Kane proves every new assertion
67
+ tripwire check # exit 1 below the threshold (for hooks and CI)
68
+ ```
69
+
70
+ Try it on the bundled demo shop (its Kane suite and recordings are committed):
71
+
72
+ ```bash
73
+ git clone https://github.com/da-b0s/tripwire && cd tripwire && npm install
74
+ npm run shop # terminal 1: the app, on :3000
75
+ ```
76
+
77
+ ```bash
78
+ cd demo/shop && npx tripwire run # terminal 2: starts the proxy on :8080 itself
79
+ ```
80
+
81
+ Without spending any credits you can inspect the committed results: `npx tripwire check`,
82
+ `npx tripwire list`, `npx tripwire run --dry-run` and `npx tripwire report` all work offline.
83
+
84
+ Long runs are safe to interrupt: `tripwire run --resume` continues from the last finished mutant,
85
+ and `tripwire run --retry-errors` re-runs only mutants that hit a Kane service error.
86
+
87
+ ## What a score means
88
+
89
+ | Status | Meaning | Counted |
90
+ |---|---|---|
91
+ | **killed** | at least one test failed while the bug was live | yes |
92
+ | **survived** | the bug fired and every test still passed | yes, against you |
93
+ | **masked** | strict replay caught it, but with Kane's default healing the test re-authored itself and passed on the broken app | yes, against you |
94
+ | **not reached** | the bug never fired in any test (e.g. no test submits invalid input) | no, listed separately |
95
+
96
+ Score = killed ÷ (killed + survived + masked). Kane exit codes 2 and 3 (errors, timeouts) are
97
+ retried once and never counted as kills. Mutants are only excluded as "equivalent" with a
98
+ human-written reason in `.tripwire/equivalents.json`, and every exclusion is listed in the report.
99
+
100
+ ## The operators
101
+
102
+ Generated from the traffic and the pages your suite actually produced, so every mutant is
103
+ relevant to at least one test. Data mutants only change values that were on screen, and interface
104
+ mutants target controls by their visible text.
105
+
106
+ | Family | Operator | What breaks |
107
+ |---|---|---|
108
+ | data | number-drift | a price, total or count is off by one unit or 10% |
109
+ | data | item-drop / item-dup | a list loses or repeats an element |
110
+ | data | field-swap | two displayed fields of a record are exchanged |
111
+ | data | stale-read | a read after a write returns the old data |
112
+ | data | wrong-owner | another user's record appears in a list |
113
+ | behaviour | silent-fail | a write returns 500 and is dropped |
114
+ | behaviour | fake-success | a write answers with its usual success body but is never saved |
115
+ | behaviour | double-apply | a POST is applied twice |
116
+ | behaviour | validation-off | a 400/422 becomes 200 |
117
+ | behaviour | auth-drop | one request mid-flow returns 401 |
118
+ | interface | hide-control | a control the tests see but never use disappears |
119
+ | interface | dead-control | a control the tests click does nothing |
120
+ | interface | label-swap | two button labels are exchanged |
121
+ | interface | text-change | a heading or status shows the wrong words |
122
+ | interface | wrong-link | a link points to a different page |
123
+ | interface | missing-row-action | the first row loses its edit/delete/cancel action |
124
+ | saboteur | agent-proposed | domain rules broken on purpose, proposed by your agent (`tripwire sabotage`) |
125
+
126
+ ## Use it in your agent
127
+
128
+ - **Claude Code Stop hook**: [hooks/stop-hook.mjs](https://github.com/da-b0s/tripwire/blob/main/hooks/stop-hook.mjs) blocks "done" while tests
129
+ changed since the last run or the score is below the threshold, and names the bugs that got past.
130
+ Merge [hooks/claude-settings.json](https://github.com/da-b0s/tripwire/blob/main/hooks/claude-settings.json) into `.claude/settings.json`.
131
+ - **Any agent**: copy [hooks/AGENTS.md](https://github.com/da-b0s/tripwire/blob/main/hooks/AGENTS.md).
132
+ - **CI**: [hooks/tripwire.yml](https://github.com/da-b0s/tripwire/blob/main/hooks/tripwire.yml) runs incrementally (`--since`) on pull requests.
133
+ - **Memory**: `.tripwire/lessons.md` collects assertion patterns that caught real bugs; the agent
134
+ reads it before writing new tests, so first drafts get stronger.
135
+
136
+ ## What Kane does here
137
+
138
+ | Kane feature | Role in Tripwire |
139
+ |---|---|
140
+ | `_test.md` replay | the suite runs once per mutant without re-authoring |
141
+ | `--no-adaptive-heal` and heal events | strict verdicts; then a healing-on rerun detects masked bugs |
142
+ | Policies and `evidence verdict` | an inverted policy (`failed + broken >= 1`) judges every mutant run; a clean policy gates the baseline |
143
+ | `metrics.tests_without_checks` | assertion-free tests are reported first |
144
+ | Evidence packs | screenshots of the broken page for the report and for the agent |
145
+ | NDJSON events, exit codes, failure triage | machine-readable verdicts; Kane's own triage is shown next to each planted bug |
146
+ | Official skill | the test-writing agent uses TestMu's skill; Tripwire checks the result |
147
+
148
+ ## Limits
149
+
150
+ - Tripwire mutates traffic and the rendered page, not source code. It measures whether the suite
151
+ notices breakage a user would see. It does not replace unit-level mutation testing.
152
+ - Server-rendered pages carry data in HTML, not JSON; data operators work on JSON APIs, interface
153
+ operators work on any page. Websockets and static assets pass through untouched.
154
+ - A Kane replay re-evaluates its AI checks, so it is cheap but not free (measured: ~2 credits per
155
+ 3-step replay). See [NOTES.md](https://github.com/da-b0s/tripwire/blob/main/NOTES.md) for every measurement.
156
+ - Only run it against apps you own or run locally.
157
+
158
+ ## For judges
159
+
160
+ - Everything runs locally against the demo shop; you only need a Kane login.
161
+ - The demo suite in `demo/shop/tests/` was written by a separate Claude Code session that was never
162
+ told about Tripwire; its full transcript is in `demo/shop/agent-session.jsonl` and the prompt in
163
+ [WRITE_TESTS_PROMPT.md](https://github.com/da-b0s/tripwire/blob/main/demo/shop/WRITE_TESTS_PROMPT.md).
164
+ - If you cannot run it: the committed report, ledger and screenshots in `demo/shop/.tripwire/`
165
+ are the record of the runs shown in the video.
166
+
167
+ ## License
168
+
169
+ MIT
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+ // tripwire: mutation testing for Kane CLI suites.
3
+ import { main } from '../src/cli.js';
4
+
5
+ main(process.argv.slice(2)).then(
6
+ (code) => { process.exitCode = code ?? 0; },
7
+ (err) => { console.error(`tripwire: ${err.message}`); if (process.env.TRIPWIRE_DEBUG) console.error(err.stack); process.exitCode = 2; }
8
+ );
@@ -0,0 +1,114 @@
1
+ // Shared helpers for operators. An operator receives the baseline profile:
2
+ // profile.tests = [{ test, traffic: [entry], pages: [{url, controls, text}], clicks: [control] }]
3
+ // and returns concrete mutant definitions (see README "Mutant definition format").
4
+ import { walk, get } from '../src/jsonpath.js';
5
+
6
+ export const WRITE = new Set(['POST', 'PUT', 'PATCH', 'DELETE']);
7
+ export const routeKey = (e) => `${e.method} ${e.path}`;
8
+ export const esc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
9
+
10
+ const MONEYISH = /total|price|subtotal|amount|cost|discount|shipping|tax|fee|balance/i;
11
+ const COUNTISH = /count|qty|quantity|items?$|stock|number|seats|nights|guests/i;
12
+
13
+ export function testText(t) {
14
+ return t.pages.map((p) => p.text || '').join('\n');
15
+ }
16
+
17
+ // Is this value something a user could actually see on one of the test's pages?
18
+ export function rendered(value, text, key = '') {
19
+ if (typeof value === 'number') {
20
+ if (!Number.isFinite(value)) return false;
21
+ const forms = Number.isInteger(value) ? [String(value), value.toFixed(2)] : [value.toFixed(2), String(value)];
22
+ if (Number.isInteger(value) && Math.abs(value) < 10 && !COUNTISH.test(key) && !MONEYISH.test(key)) return false;
23
+ return forms.some((f) => new RegExp(`(^|[^\\d.])${esc(f)}(?![\\d])`).test(text));
24
+ }
25
+ if (typeof value === 'string') {
26
+ const v = value.trim();
27
+ if (v.length < 3 || v.length > 200) return false;
28
+ if (/^\d{4}-\d\d-\d\dT/.test(v)) return false; // timestamps are usually reformatted
29
+ return text.includes(v);
30
+ }
31
+ return false;
32
+ }
33
+
34
+ export const isMoneyKey = (k) => MONEYISH.test(k);
35
+ export const isCountKey = (k) => COUNTISH.test(k);
36
+ export const lastKey = (p) => { const m = p.match(/(?:\.([\w$-]+)|\[(\d+)\])$/); return m ? (m[1] ?? m[2]) : ''; };
37
+
38
+ // Unique (method, path) JSON GET responses per test, keeping the last body seen.
39
+ export function jsonReads(t) {
40
+ const by = new Map();
41
+ for (const e of t.traffic) if (e.method === 'GET' && e.type === 'json' && e.status < 400 && e.json !== undefined && !e.mutated) by.set(routeKey(e), e);
42
+ return [...by.values()];
43
+ }
44
+
45
+ export function writes(t) {
46
+ const by = new Map();
47
+ for (const e of t.traffic) if (WRITE.has(e.method) && !e.mutated) { if (!by.has(routeKey(e)) || e.status < 400) by.set(routeKey(e), e); }
48
+ return [...by.values()];
49
+ }
50
+
51
+ // All tests whose baseline traffic touched this route.
52
+ export function touchingRoute(profile, method, path) {
53
+ return profile.tests.filter((t) => t.traffic.some((e) => e.method === method && e.path === path)).map((t) => t.test);
54
+ }
55
+
56
+ // All tests that saw a control with this role and text.
57
+ export function touchingControl(profile, role, text) {
58
+ return profile.tests.filter((t) => t.pages.some((p) => p.controls.some((c) => c.role === role && c.text === text))).map((t) => t.test);
59
+ }
60
+
61
+ export function clickedAnywhere(profile, role, text) {
62
+ return profile.tests.some((t) => t.clicks.some((c) => c.role === role && c.text === text));
63
+ }
64
+
65
+ // A label with a live number in it ("Cart 2", "3 items") is one control, not several.
66
+ // A private-use character marks "a number was here", so a literal '#' in a label ("#1002") is kept.
67
+ const NUM = '';
68
+ const masked = (text) => text.replace(/\d+(?:[.,]\d+)?/g, NUM);
69
+ const patternFor = (text) => '^' + esc(masked(text)).split(NUM).join('\\d+(?:[.,]\\d+)?') + '$';
70
+
71
+ // Distinct visible controls across the suite:
72
+ // [{ role, text, pattern?, href, inRow, tag, cls, tests:[...], pages:[...], clicked }]
73
+ export function controls(profile) {
74
+ const variants = new Map();
75
+ for (const t of profile.tests) for (const p of t.pages) for (const c of p.controls) {
76
+ const k = `${c.role}|${masked(c.text)}`;
77
+ if (!variants.has(k)) variants.set(k, new Set());
78
+ variants.get(k).add(c.text);
79
+ }
80
+ const keyOf = (c) => {
81
+ const k = `${c.role}|${masked(c.text)}`;
82
+ return variants.get(k) && variants.get(k).size > 1 ? k : `${c.role}|${c.text}`;
83
+ };
84
+ const by = new Map();
85
+ for (const t of profile.tests) {
86
+ for (const p of t.pages) for (const c of p.controls) {
87
+ const k = keyOf(c);
88
+ if (!by.has(k)) {
89
+ const dynamic = k !== `${c.role}|${c.text}`;
90
+ by.set(k, { ...c, ...(dynamic ? { pattern: patternFor(c.text), text: masked(c.text).split(NUM).join('N') } : {}), tests: new Set(), pages: new Set(), clicked: false });
91
+ }
92
+ by.get(k).tests.add(t.test);
93
+ by.get(k).pages.add(p.url);
94
+ }
95
+ for (const c of t.clicks) { const k = keyOf(c); if (by.has(k)) by.get(k).clicked = true; }
96
+ }
97
+ return [...by.values()].map((c) => ({ ...c, tests: [...c.tests], pages: [...c.pages] }));
98
+ }
99
+
100
+ // The target a DOM mutant uses to find a control again in the page.
101
+ export const targetOf = (c) => (c.pattern ? { role: c.role, text: c.text, pattern: c.pattern } : { role: c.role, text: c.text });
102
+
103
+ export { walk, get };
104
+
105
+ export function mutant(fields) {
106
+ const { id, family, operator, match, action, touches_tests, describe, times, expect } = fields;
107
+ const m = { id, family, operator, match, action, touches_tests: [...new Set(touches_tests)].sort(), describe };
108
+ if (times) m.times = times;
109
+ if (expect) m.expect = expect; // what a good test should notice
110
+ return m;
111
+ }
112
+
113
+ export const fmtVal = (v) => (typeof v === 'number' ? (Number.isInteger(v) ? String(v) : v.toFixed(2)) : JSON.stringify(v));
114
+ export const nice = (key) => key.replace(/([a-z])([A-Z])/g, '$1 $2').replace(/[_-]/g, ' ').toLowerCase();
@@ -0,0 +1,41 @@
1
+ // Saboteur: domain-specific mutants proposed by the coding agent from the acceptance criteria and
2
+ // test files ("coupon stacks", "free shipping off by one", "past dates accepted"), expressed in the
3
+ // same definition format as every other mutant. `tripwire sabotage` writes them to .tripwire/saboteur.json.
4
+ import fs from 'node:fs';
5
+ import path from 'node:path';
6
+ import { touchingRoute, touchingControl, mutant } from './_util.js';
7
+ import { validateMutant } from '../src/mutant.js';
8
+
9
+ export function loadSaboteur(dir) {
10
+ const file = path.join(dir, 'saboteur.json');
11
+ if (!fs.existsSync(file)) return { mutants: [], rejected: [] };
12
+ const raw = JSON.parse(fs.readFileSync(file, 'utf8'));
13
+ const list = Array.isArray(raw) ? raw : raw.mutants || [];
14
+ const mutants = [];
15
+ const rejected = [];
16
+ for (const m of list) {
17
+ const errors = validateMutant(m);
18
+ if (errors.length) rejected.push({ id: m?.id, errors });
19
+ else mutants.push(m);
20
+ }
21
+ return { mutants, rejected };
22
+ }
23
+
24
+ export default {
25
+ name: 'agent-proposed',
26
+ family: 'saboteur',
27
+ summary: 'Domain rules from the acceptance criteria, broken on purpose and expressed as response rewrites.',
28
+ generate(profile, ctx = {}) {
29
+ const { mutants } = loadSaboteur(ctx.dir || '.tripwire');
30
+ return mutants.map((m) => {
31
+ let touches = m.touches_tests;
32
+ if (!touches || !touches.length) {
33
+ touches = m.match.document
34
+ ? touchingControl(profile, m.action.dom?.target?.role || 'button', m.action.dom?.target?.text || '')
35
+ : touchingRoute(profile, m.match.method || 'GET', m.match.path);
36
+ if (!touches.length && m.match.document) touches = profile.tests.map((t) => t.test);
37
+ }
38
+ return mutant({ ...m, id: m.id.startsWith('agent-proposed:') ? m.id : `agent-proposed:${m.id}`, family: 'saboteur', operator: 'agent-proposed', touches_tests: touches });
39
+ });
40
+ }
41
+ };
@@ -0,0 +1,30 @@
1
+ // Behaviour: one request in the middle of a flow returns 401 (an expired session).
2
+ // The user should see a sign-in prompt, not a blank or half-rendered page.
3
+ import { touchingRoute, mutant } from './_util.js';
4
+
5
+ export default {
6
+ name: 'auth-drop',
7
+ family: 'behaviour',
8
+ summary: 'One request mid-flow returns 401. The user should see a sign-in prompt, not a blank page.',
9
+ generate(profile) {
10
+ const out = new Map();
11
+ for (const t of profile.tests) {
12
+ // skip the first JSON read of the test (that is the page load, not "mid-flow")
13
+ const reads = t.traffic.filter((e) => e.type === 'json' && e.status < 400 && !e.mutated);
14
+ for (const e of reads.slice(1)) {
15
+ const id = `auth-drop:${e.method} ${e.path}`;
16
+ if (out.has(id)) continue;
17
+ out.set(id, mutant({
18
+ id, family: 'behaviour', operator: 'auth-drop', times: 1,
19
+ match: { method: e.method, path: e.path },
20
+ action: { type: 'respond', status: 401, body: { error: 'Unauthorized' } },
21
+ touches_tests: touchingRoute(profile, e.method, e.path),
22
+ describe: `${e.method} ${e.path} returns 401 once, mid-flow`,
23
+ expect: 'The test checks the page it lands on after each step'
24
+ }));
25
+ }
26
+ }
27
+ // keep this family small: up to 4, preferring writes (the riskiest place to lose a session)
28
+ return [...out.values()].sort((a, b) => (a.match.method === 'GET') - (b.match.method === 'GET')).slice(0, 4);
29
+ }
30
+ };
@@ -0,0 +1,23 @@
1
+ // Interface: a control the test clicks no longer does anything. If the test passes anyway,
2
+ // it never checked the effect of the click.
3
+ import { controls, mutant } from './_util.js';
4
+ import { domMutant } from './hide-control.js';
5
+
6
+ export default {
7
+ name: 'dead-control',
8
+ family: 'interface',
9
+ summary: 'The click handler of a control is disabled by an injected script.',
10
+ generate(profile) {
11
+ return controls(profile)
12
+ .filter((c) => c.role === 'button' && c.clicked)
13
+ .map((c) => mutant({
14
+ id: `dead-control:button:"${c.text}"`,
15
+ family: 'interface', operator: 'dead-control',
16
+ match: { document: true },
17
+ action: domMutant('dead', c),
18
+ touches_tests: c.tests,
19
+ describe: `Clicking "${c.text}" does nothing`,
20
+ expect: `The test checks what "${c.text}" is supposed to change`
21
+ }));
22
+ }
23
+ };
@@ -0,0 +1,20 @@
1
+ // Behaviour: a non-idempotent write is applied twice (double submit, retry without idempotency key).
2
+ import { writes, touchingRoute, mutant } from './_util.js';
3
+
4
+ export default {
5
+ name: 'double-apply',
6
+ family: 'behaviour',
7
+ summary: 'A POST or PATCH is forwarded twice: two rows, or a doubled quantity.',
8
+ generate(profile) {
9
+ // PUT and DELETE are idempotent by contract, so applying them twice is an equivalent mutant.
10
+ return profile.tests.flatMap((t) => writes(t).filter((e) => e.status < 400 && (e.method === 'POST' || e.method === 'PATCH')).map((e) => mutant({
11
+ id: `double-apply:${e.method} ${e.path}`,
12
+ family: 'behaviour', operator: 'double-apply',
13
+ match: { method: e.method, path: e.path },
14
+ action: { type: 'forward-twice' },
15
+ touches_tests: touchingRoute(profile, e.method, e.path),
16
+ describe: `${e.method} ${e.path} is applied twice`,
17
+ expect: 'The test checks the exact quantity or number of records afterwards'
18
+ })));
19
+ }
20
+ };
@@ -0,0 +1,21 @@
1
+ // Behaviour: a write answers with its usual success body but never reaches the app.
2
+ // The page looks right until anything re-reads the data.
3
+ import { writes, touchingRoute, mutant } from './_util.js';
4
+
5
+ export default {
6
+ name: 'fake-success',
7
+ family: 'behaviour',
8
+ summary: 'A write returns its normal success response but is not forwarded, so after a reload the change is gone.',
9
+ generate(profile) {
10
+ return profile.tests.flatMap((t) => writes(t).filter((e) => e.status < 400 && e.json !== undefined).map((e) => mutant({
11
+ id: `fake-success:${e.method} ${e.path}`,
12
+ family: 'behaviour', operator: 'fake-success',
13
+ match: { method: e.method, path: e.path },
14
+ // replay the success body recorded at baseline: realistic, and the UI keeps working
15
+ action: { type: 'respond', status: e.status, body: e.json },
16
+ touches_tests: touchingRoute(profile, e.method, e.path),
17
+ describe: `${e.method} ${e.path} reports success but the change is never saved`,
18
+ expect: 'The test re-reads the data (reload or another page) after the change'
19
+ })));
20
+ }
21
+ };
@@ -0,0 +1,42 @@
1
+ // Data: two displayed string fields of one record are exchanged (first/last name, from/to, name/email).
2
+ import { jsonReads, walk, rendered, testText, touchingRoute, mutant, nice } from './_util.js';
3
+
4
+ const ID_LIKE = /^(id|_id|uuid|.*Id|.*_id|sku|slug|key|ref)$/;
5
+
6
+ export default {
7
+ name: 'field-swap',
8
+ family: 'data',
9
+ summary: 'Two displayed string fields of one record are exchanged.',
10
+ generate(profile) {
11
+ const out = [];
12
+ for (const t of profile.tests) {
13
+ const text = testText(t);
14
+ for (const e of jsonReads(t)) {
15
+ let perRoute = 0;
16
+ walk(e.json, (p, v) => {
17
+ if (perRoute >= 2 || !v || typeof v !== 'object' || Array.isArray(v)) return;
18
+ // human-readable fields only: swapping an id or code into a label is not a mix-up users meet
19
+ const shown = Object.entries(v).filter(([k, x]) => typeof x === 'string' && !ID_LIKE.test(k) && /[A-Za-z]{2}/.test(x) && rendered(x, text, k));
20
+ for (let i = 0; i < shown.length && perRoute < 2; i++) {
21
+ for (let j = i + 1; j < shown.length && perRoute < 2; j++) {
22
+ if (shown[i][1] === shown[j][1]) continue;
23
+ const [a, av] = shown[i];
24
+ const [b, bv] = shown[j];
25
+ perRoute++;
26
+ out.push(mutant({
27
+ id: `field-swap:${e.method} ${e.path}:${p}.${a}<->${b}`,
28
+ family: 'data', operator: 'field-swap',
29
+ match: { method: e.method, path: e.path },
30
+ action: { type: 'json', ops: [{ op: 'swap', path: p, a, b }] },
31
+ touches_tests: touchingRoute(profile, e.method, e.path),
32
+ describe: `${nice(a)} and ${nice(b)} are swapped ("${av}" shows as ${nice(b)})`,
33
+ expect: `The ${nice(a)} shown is checked, not just that something is shown`
34
+ }));
35
+ }
36
+ }
37
+ });
38
+ }
39
+ }
40
+ return out;
41
+ }
42
+ };
@@ -0,0 +1,30 @@
1
+ // Interface: a button or link the tests saw but never clicked disappears.
2
+ // Catches suites that never look at controls they do not use.
3
+ import { controls, mutant, targetOf } from './_util.js';
4
+
5
+ const domMutant = (op, c, extra = {}) => ({ type: 'dom', dom: { op, target: targetOf(c), ...extra } });
6
+
7
+ export default {
8
+ name: 'hide-control',
9
+ family: 'interface',
10
+ summary: 'A button or link that the flow does not click is hidden.',
11
+ generate(profile) {
12
+ // Buttons first, then links outside lists (navigation, calls to action), then the rest.
13
+ const rank = (c) => (c.role === 'button' ? 0 : c.inRow ? 2 : 1);
14
+ return controls(profile)
15
+ .filter((c) => (c.role === 'button' || c.role === 'link') && !c.clicked && c.text.length <= 40)
16
+ .sort((a, b) => rank(a) - rank(b))
17
+ .slice(0, 12)
18
+ .map((c) => mutant({
19
+ id: `hide-control:${c.role}:"${c.text}"`,
20
+ family: 'interface', operator: 'hide-control',
21
+ match: { document: true },
22
+ action: domMutant('hide', c),
23
+ touches_tests: c.tests,
24
+ describe: `The "${c.text}" ${c.role} is missing`,
25
+ expect: `The test checks that "${c.text}" is offered`
26
+ }));
27
+ }
28
+ };
29
+
30
+ export { domMutant };
@@ -0,0 +1,77 @@
1
+ // The operator registry and mutant selection.
2
+ import numberDrift from './number-drift.js';
3
+ import itemDrop from './item-drop.js';
4
+ import itemDup from './item-dup.js';
5
+ import fieldSwap from './field-swap.js';
6
+ import staleRead from './stale-read.js';
7
+ import wrongOwner from './wrong-owner.js';
8
+ import silentFail from './silent-fail.js';
9
+ import fakeSuccess from './fake-success.js';
10
+ import doubleApply from './double-apply.js';
11
+ import validationOff from './validation-off.js';
12
+ import authDrop from './auth-drop.js';
13
+ import hideControl from './hide-control.js';
14
+ import deadControl from './dead-control.js';
15
+ import labelSwap from './label-swap.js';
16
+ import textChange from './text-change.js';
17
+ import wrongLink from './wrong-link.js';
18
+ import missingRowAction from './missing-row-action.js';
19
+ import agentProposed from './agent-proposed.js';
20
+
21
+ export const OPERATORS = [
22
+ numberDrift, itemDrop, itemDup, fieldSwap, staleRead, wrongOwner,
23
+ silentFail, fakeSuccess, doubleApply, validationOff, authDrop,
24
+ hideControl, deadControl, labelSwap, textChange, wrongLink, missingRowAction,
25
+ agentProposed
26
+ ];
27
+
28
+ // Generate every candidate, merge duplicates (same id from several tests), drop excluded equivalents.
29
+ export function generateAll(profile, { dir = '.tripwire', only, excluded = new Set() } = {}) {
30
+ const byId = new Map();
31
+ const counts = {};
32
+ for (const op of OPERATORS) {
33
+ if (only && !only.includes(op.name) && !only.includes(op.family)) continue;
34
+ let made = [];
35
+ try { made = op.generate(profile, { dir }); } catch (err) { made = []; counts[op.name] = { error: err.message }; continue; }
36
+ counts[op.name] = { generated: made.length };
37
+ for (const m of made) {
38
+ if (!m.touches_tests.length) continue;
39
+ if (byId.has(m.id)) {
40
+ const prev = byId.get(m.id);
41
+ prev.touches_tests = [...new Set([...prev.touches_tests, ...m.touches_tests])].sort();
42
+ } else byId.set(m.id, m);
43
+ }
44
+ }
45
+ const all = [...byId.values()];
46
+ return { candidates: all.filter((m) => !excluded.has(m.id)), excluded: all.filter((m) => excluded.has(m.id)), counts };
47
+ }
48
+
49
+ // Pick up to max mutants, spread across families, operators and tests (round robin), saboteur first.
50
+ export function select(candidates, max) {
51
+ if (!max || candidates.length <= max) return candidates;
52
+ const groups = new Map();
53
+ for (const m of candidates) {
54
+ const k = m.operator;
55
+ if (!groups.has(k)) groups.set(k, []);
56
+ groups.get(k).push(m);
57
+ }
58
+ // within an operator, alternate between the tests that are touched so no single test dominates
59
+ for (const [k, list] of groups) {
60
+ const byTest = new Map();
61
+ for (const m of list) { const t = m.touches_tests[0]; if (!byTest.has(t)) byTest.set(t, []); byTest.get(t).push(m); }
62
+ const queues = [...byTest.values()];
63
+ const spread = [];
64
+ while (queues.some((q) => q.length)) for (const q of queues) if (q.length) spread.push(q.shift());
65
+ groups.set(k, spread);
66
+ }
67
+ const order = [...groups.keys()].sort((a, b) => (a === 'agent-proposed' ? -1 : b === 'agent-proposed' ? 1 : 0));
68
+ const picked = [];
69
+ while (picked.length < max && order.some((k) => groups.get(k).length)) {
70
+ for (const k of order) {
71
+ if (picked.length >= max) break;
72
+ const g = groups.get(k);
73
+ if (g.length) picked.push(g.shift());
74
+ }
75
+ }
76
+ return picked;
77
+ }
@@ -0,0 +1,36 @@
1
+ // Data: one element is removed from a list response.
2
+ import { jsonReads, walk, rendered, testText, touchingRoute, mutant, nice, lastKey } from './_util.js';
3
+
4
+ export function lists(profile) {
5
+ const out = [];
6
+ for (const t of profile.tests) {
7
+ const text = testText(t);
8
+ for (const e of jsonReads(t)) {
9
+ walk(e.json, (p, v) => {
10
+ if (!Array.isArray(v) || v.length === 0 || typeof v[0] !== 'object' || v[0] === null) return;
11
+ const label = Object.entries(v[0]).find(([k, x]) => typeof x === 'string' && rendered(x, text, k));
12
+ if (label) out.push({ e, p, v, label: label[1] });
13
+ });
14
+ }
15
+ }
16
+ return out;
17
+ }
18
+
19
+ const dropOp = {
20
+ name: 'item-drop',
21
+ family: 'data',
22
+ summary: 'One element is removed from a list response.',
23
+ generate(profile) {
24
+ return lists(profile).map(({ e, p, v, label }) => mutant({
25
+ id: `item-drop:${e.method} ${e.path}:${p}[0]`,
26
+ family: 'data', operator: 'item-drop',
27
+ match: { method: e.method, path: e.path },
28
+ action: { type: 'json', ops: [{ op: 'remove-index', path: p, index: 0 }] },
29
+ touches_tests: touchingRoute(profile, e.method, e.path),
30
+ describe: `"${label}" is missing from the ${nice(lastKey(p) || 'list')} (${v.length} → ${v.length - 1} items)`,
31
+ expect: 'The list length or a named item is checked'
32
+ }));
33
+ }
34
+ };
35
+
36
+ export default dropOp;