summaryrefslogtreecommitdiff
path: root/scripts/floor-guard-reference-test.js
blob: 9868294dfe07637c7ee5109e40bf5716479163b5 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env node

'use strict';

// Contract test for skills/constraint-driven-development/references/floor-guard.md.
// The fenced `js` block is extracted unchanged and run, with real git, against small
// fixtures: each is a fresh repository with one source file, one test file and a
// CONSTRAINTS.md carrying a floor bullet, a coverage minimum, a bundle maximum and an
// Exceptions table. Loosening moves must exit 1, tightening and no-op moves must exit 0.

const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { spawnSync } = require('node:child_process');
const { after, before, test } = require('node:test');

const REFERENCE = path.join(
  __dirname, '..', 'skills', 'constraint-driven-development', 'references', 'floor-guard.md',
);
const CONSTRAINTS = [
  '# Constraints',
  '',
  'Last reviewed: 2026-08-08 by @owner',
  '',
  '## Floor',
  '- No skipped tests.',
  '- Latency: p95 under 200 ms and p99 under 900 ms.',
  '',
  '| Dimension | Rule |',
  '|---|---|',
  '| Coverage | >= 80% |',
  '| Bundle | <= 200 KB |',
  '',
  '## Exceptions',
  '',
  '| ID | Rule | Owner | Expires |',
  '|---|---|---|---|',
  '| W1 | no-explicit-any | @owner | 2026-11-01 |',
  '',
].join('\n');

let scratch;
let guard;
const sandboxes = [];

before(() => {
  scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-skills-floor-guard-test-'));
  const source = fs.readFileSync(REFERENCE, 'utf8');
  const match = source.match(/```js\n([\s\S]*?)\n```/);
  assert.ok(match, 'floor-guard.md must contain one fenced js block');
  guard = path.join(scratch, 'floor-guard.mjs');
  fs.writeFileSync(guard, match[1] + '\n');
});

after(() => {
  for (const dir of sandboxes) fs.rmSync(dir, { recursive: true, force: true });
  if (scratch) fs.rmSync(scratch, { recursive: true, force: true });
});

function git(cwd, ...args) {
  const result = spawnSync('git', args, {
    cwd,
    encoding: 'utf8',
    env: {
      ...process.env,
      GIT_AUTHOR_NAME: 'Floor Guard Test',
      GIT_AUTHOR_EMAIL: 'floor-guard@example.invalid',
      GIT_COMMITTER_NAME: 'Floor Guard Test',
      GIT_COMMITTER_EMAIL: 'floor-guard@example.invalid',
      GIT_CONFIG_GLOBAL: '/dev/null',
    },
  });
  assert.equal(result.status, 0, `git ${args.join(' ')} failed: ${result.stderr}`);
  return result.stdout;
}

function makeRepo() {
  const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-skills-floor-guard-fixture-'));
  sandboxes.push(root);
  git(root, 'init', '-q');
  fs.writeFileSync(path.join(root, 'app.js'), 'module.exports = 1;\n');
  fs.writeFileSync(
    path.join(root, 'app.test.js'),
    "const assert = require('assert');\nassert.equal(require('./app'), 1);\n",
  );
  fs.writeFileSync(path.join(root, 'CONSTRAINTS.md'), CONSTRAINTS);
  git(root, 'add', '.');
  git(root, 'commit', '-qm', 'baseline');
  return root;
}

function editConstraints(root, from, to) {
  const file = path.join(root, 'CONSTRAINTS.md');
  const text = fs.readFileSync(file, 'utf8');
  assert.ok(text.includes(from), `fixture CONSTRAINTS.md must contain ${JSON.stringify(from)}`);
  fs.writeFileSync(file, text.replace(from, to));
}

function commitFile(root, name, text) {
  fs.writeFileSync(path.join(root, name), text);
  git(root, 'add', name);
  git(root, 'commit', '-qm', `add ${name}`);
}

function runGuard(root, cwd = root) {
  return spawnSync(process.execPath, [guard, '--base', 'HEAD'], { cwd, encoding: 'utf8' });
}

const cases = [
  ['unchanged repository is clean', 0, () => {}],
  ['a suppression added to a tracked file is a violation', 1, (root) => {
    fs.writeFileSync(path.join(root, 'app.js'), '// @ts-ignore\nmodule.exports = 1;\n');
  }],
  ['a suppression in a new untracked file is a violation', 1, (root) => {
    fs.writeFileSync(path.join(root, 'new.js'), '// @ts-ignore\nmodule.exports = 1;\n');
  }],
  ['a skipped test in a new untracked file is a violation', 1, (root) => {
    fs.writeFileSync(path.join(root, 'new.test.js'), "test.skip('regression', () => {});\n");
  }],
  ['a suppression in a staged new file is a violation', 1, (root) => {
    fs.writeFileSync(path.join(root, 'new.js'), '// @ts-ignore\nmodule.exports = 1;\n');
    git(root, 'add', '-A');
  }],
  ['a deleted test file is a violation', 1, (root) => {
    fs.unlinkSync(path.join(root, 'app.test.js'));
  }],
  ['a staged deleted test file is a violation', 1, (root) => {
    fs.unlinkSync(path.join(root, 'app.test.js'));
    git(root, 'add', '-A');
  }],
  ['an assertion removed from a surviving test file is a violation', 1, (root) => {
    fs.writeFileSync(path.join(root, 'app.test.js'), "const assert = require('assert');\n");
  }],
  ['lowering a minimum is a violation', 1, (root) => {
    editConstraints(root, '| Coverage | >= 80% |', '| Coverage | >= 60% |');
  }],
  ['raising a minimum is silent', 0, (root) => {
    editConstraints(root, '| Coverage | >= 80% |', '| Coverage | >= 90% |');
  }],
  ['raising a maximum is a violation', 1, (root) => {
    editConstraints(root, '| Bundle | <= 200 KB |', '| Bundle | <= 300 KB |');
  }],
  ['lowering a maximum is silent', 0, (root) => {
    editConstraints(root, '| Bundle | <= 200 KB |', '| Bundle | <= 150 KB |');
  }],
  ['deleting one threshold from a rule is a violation', 1, (root) => {
    editConstraints(root, 'p95 under 200 ms and p99 under 900 ms.', 'p95 under 200 ms.');
  }],
  ['a number inserted before a threshold is silent', 0, (root) => {
    editConstraints(root, '| Coverage | >= 80% |', '| Coverage | per ADR 7, >= 80% |');
  }],
  ['adding a threshold to a rule is silent', 0, (root) => {
    editConstraints(root, '| Bundle | <= 200 KB |', '| Bundle | <= 200 KB, <= 60 KB gzipped |');
  }],
  ['a deleted floor rule is a violation', 1, (root) => {
    editConstraints(root, '- No skipped tests.\n', '');
  }],
  ['a new exception row is a violation', 1, (root) => {
    const file = path.join(root, 'CONSTRAINTS.md');
    fs.appendFileSync(file, '| W2 | temporary waiver | @owner | 2026-10-01 |\n');
  }],
];

for (const [name, expectedExit, mutate] of cases) {
  test(`floor guard: ${name}`, () => {
    const root = makeRepo();
    mutate(root);
    const result = runGuard(root);
    assert.equal(
      result.status, expectedExit,
      `expected exit ${expectedExit}, got ${result.status}\nstdout: ${result.stdout}\nstderr: ${result.stderr}`,
    );
  });
}

test('floor guard: a removed exception row is silent', () => {
  const root = makeRepo();
  editConstraints(root, '| W1 | no-explicit-any | @owner | 2026-11-01 |\n', '');
  const result = runGuard(root);
  assert.equal(result.status, 0, result.stderr);
});

test('floor guard: a missing merge base exits 2, never 0', () => {
  const root = makeRepo();
  const result = spawnSync(process.execPath, [guard, '--base', 'no-such-ref'], {
    cwd: root, encoding: 'utf8',
  });
  assert.equal(result.status, 2, result.stderr);
});

test('floor guard: a threshold with no readable direction is reported when it changes', () => {
  const root = makeRepo();
  editConstraints(root, '| W1 | no-explicit-any | @owner | 2026-11-01 |', '| W1 | no-explicit-any | @owner | 2027-11-01 |');
  const result = runGuard(root);
  assert.equal(result.status, 1, result.stderr);
  assert.match(result.stderr, /threshold-changed/);
});

test('floor guard: a renamed rule is reported as removed, with a note that its label may have changed', () => {
  const root = makeRepo();
  editConstraints(root, '| Coverage | >= 80% |', '| Code coverage | >= 80% |');
  const result = runGuard(root);
  assert.equal(result.status, 1, result.stderr);
  assert.match(result.stderr, /\[rule-removed\]/);
  assert.match(result.stderr, /label changed/);
});

test('floor guard: a threshold that loses its direction words is reported as removed, with a note saying so', () => {
  const root = makeRepo();
  editConstraints(root, '| Coverage | >= 80% |', '| Coverage | 80% |');
  const result = runGuard(root);
  assert.equal(result.status, 1, result.stderr);
  assert.match(result.stderr, /\[threshold-removed\]/);
  assert.match(result.stderr, /direction words/);
});

// Inside a hunk every line is content. An added line that starts with `++` shows in the diff
// as `+++...`, and a removed line that starts with `--` as `---...`: neither is a file header.
test('floor guard: an added line that starts with ++ is still checked', () => {
  const root = makeRepo();
  fs.writeFileSync(
    path.join(root, 'app.js'),
    'let n = 0;\n++n; // eslint-disable-line no-plusplus\nmodule.exports = n;\n',
  );
  const result = runGuard(root);
  assert.equal(result.status, 1, result.stderr);
  assert.match(result.stderr, /\[silenced-checker\] app\.js:/);
});

test('floor guard: an added line that starts with "++ " does not rename the file for later hunks', () => {
  const root = makeRepo();
  commitFile(root, 'counter.test.js', [
    "const assert = require('assert');",
    'let n = 0;',
    'n += 1;',
    'const m = 2;',
    'assert.equal(n, 1);',
    'assert.equal(m, 2);',
    '',
  ].join('\n'));
  const file = path.join(root, 'counter.test.js');
  const text = fs.readFileSync(file, 'utf8');
  fs.writeFileSync(file, text.replace('n += 1;', '++ n;').replace('assert.equal(m, 2);\n', ''));
  const result = runGuard(root);
  assert.equal(result.status, 1, result.stderr);
  assert.match(result.stderr, /\[assertion-removed\] counter\.test\.js:/);
});

test('floor guard: a removed line that starts with -- is still checked', () => {
  const root = makeRepo();
  commitFile(root, 'countdown.test.js', [
    "const assert = require('assert');",
    'let n = 1;',
    '--n; assert.equal(n, 0);',
    '',
  ].join('\n'));
  const file = path.join(root, 'countdown.test.js');
  fs.writeFileSync(file, fs.readFileSync(file, 'utf8').replace('--n; assert.equal(n, 0);\n', ''));
  const result = runGuard(root);
  assert.equal(result.status, 1, result.stderr);
  assert.match(result.stderr, /\[assertion-removed\] countdown\.test\.js:/);
});

// `git ls-files` lists only the current directory's subtree, relative to it, while `git diff`
// covers the whole tree with paths from the top. Run from a subfolder, the guard has to see the
// same files, under the same names, as it does from the top.
test('floor guard: run from a subfolder, an untracked file outside it is still checked', () => {
  const root = makeRepo();
  fs.mkdirSync(path.join(root, 'sub'));
  fs.writeFileSync(path.join(root, 'new.js'), '// @ts-ignore\nmodule.exports = 1;\n');
  const result = runGuard(root, path.join(root, 'sub'));
  assert.equal(result.status, 1, result.stderr);
  assert.match(result.stderr, /\[silenced-checker\] new\.js:/);
});

test('floor guard: run from a subfolder, an untracked file inside it is named from the top', () => {
  const root = makeRepo();
  fs.mkdirSync(path.join(root, 'sub'));
  fs.writeFileSync(path.join(root, 'sub', 'new.js'), '// @ts-ignore\nmodule.exports = 1;\n');
  const result = runGuard(root, path.join(root, 'sub'));
  assert.equal(result.status, 1, result.stderr);
  assert.match(result.stderr, /\[silenced-checker\] sub\/new\.js:/);
});

test('floor guard: outside a git work tree exits 2, never 0', () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-skills-floor-guard-no-repo-'));
  sandboxes.push(dir);
  const result = runGuard(dir);
  assert.equal(result.status, 2, result.stderr);
});

// The skill's CONSTRAINTS.md template keeps a ratchet's direction in its own column:
// `| Project coverage | 62.4% | must not fall |`. The direction words sit after a `|`.
const MEASURED = [
  '',
  '## Measured, not yet enforced',
  '',
  '| Metric | Today | Direction |',
  '|--------|-------|-----------|',
  '| Project coverage | 62.4% | must not fall |',
  '| Bundle size (main) | 184 kB | must not grow |',
  '',
].join('\n');

for (const [name, expectedExit, from, to] of [
  ['raising a must-not-fall ratchet in its own column is silent', 0, '| 62.4% |', '| 65.0% |'],
  ['lowering a must-not-fall ratchet in its own column is a violation', 1, '| 62.4% |', '| 55.0% |'],
  ['lowering a must-not-grow ratchet in its own column is silent', 0, '| 184 kB |', '| 170 kB |'],
  ['raising a must-not-grow ratchet in its own column is a violation', 1, '| 184 kB |', '| 250 kB |'],
]) {
  test(`floor guard: ${name}`, () => {
    const root = makeRepo();
    commitFile(root, 'CONSTRAINTS.md', CONSTRAINTS + MEASURED);
    editConstraints(root, from, to);
    const result = runGuard(root);
    assert.equal(
      result.status, expectedExit,
      `expected exit ${expectedExit}, got ${result.status}\nstdout: ${result.stdout}\nstderr: ${result.stderr}`,
    );
    if (expectedExit === 1) assert.match(result.stderr, /\[threshold-loosened\]/);
  });
}