codehere 2.1.0-rc.2 → 2.1.0-rc.4

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.
package/src/signals.js CHANGED
@@ -1,4 +1,6 @@
1
1
  // Deterministic signals. No AI in the decision path.
2
+ import { testChanges, languageOf } from './noskip.js';
3
+ import { unquotePath, hunkReader } from './gitpath.js';
2
4
 
3
5
  const ERROR_LINE =
4
6
  /(error|exception|traceback|failed|failure|fatal|cannot|can't|not found|undefined is not|is not defined|is not a function|segmentation fault|ERR!|panic:|denied|refused|unexpected token|syntaxerror|typeerror|referenceerror|modulenotfound|no such file)/i;
@@ -131,20 +133,37 @@ export function similarity(a, b) {
131
133
 
132
134
  // ---------- test tampering ----------
133
135
 
134
- const TEST_FILE = /(^|\/)(test|tests|__tests__|spec)\/|\.(test|spec)\.[a-z]+$|_test\.(go|py)$|(^|\/)test_[^/]+\.py$/i;
135
- export const isTestFile = (file) => TEST_FILE.test(file);
136
+ // The names the common runners collect (cold review, L13 round 7): a test folder, a .NET test project (Cart.Tests/),
137
+ // .test. and .spec., _test (Go, Python, Ruby, Dart, Elixir, Deno and node --test), and pytest's test_*.py. Not
138
+ // node's test-*.js, *-test.js and test.js: those also name helpers and features (test-utils.js, ab-test.js), and
139
+ // deleting one is not deleting a test (round 8, m1).
140
+ const CODE_EXT = String.raw`(?:go|py|rb|dart|exs?|[cm]?[jt]sx?)`;
141
+ const TEST_NAME = new RegExp(String.raw`\.(test|spec)\.[a-z]+$|_test\.${CODE_EXT}$|(^|\/)test_[^/]+\.py$`, 'i');
142
+ const TEST_FILE = new RegExp(String.raw`(^|\/)(test|tests|__tests__|spec)\/|(^|\/)[^/]+\.tests?\/|${TEST_NAME.source}`, 'i');
143
+ // TEST_NAME is the name a runner collects a test file by, wherever it is: pytest, jest and vitest do not collect test/cart.js.
144
+ // Data the tests read, not tests: deleting or editing it is not counted as weakening a test.
145
+ const FIXTURE = /(^|\/)(__fixtures__|fixtures?|__snapshots__|__mocks__|testdata)\//i;
146
+ export const isTestFile = (file) => TEST_FILE.test(file) && !FIXTURE.test(file);
136
147
 
137
148
  // Kinds: the first three weaken the tests themselves, which is what `codehere audit` counts;
138
- // checks-off turns off a check around them (hooks, types, the test script's exit code).
149
+ // expected-changed is an assertion whose expected value changed, which the finish check reports and the
150
+ // audit does not (in history it is mostly ordinary upkeep); checks-off turns off a check around the tests
151
+ // (hooks, types, the test script's exit code).
139
152
  const DELETED = 'test-deleted';
140
153
  const SKIPPED = 'test-skipped';
141
154
  const WEAKENED = 'assertion-weakened';
155
+ const EXPECTED = 'expected-changed';
156
+ const HARDCODED = 'hardcoded-answer';
157
+ const UNDER_TEST = 'test-env';
142
158
  const CHECKS_OFF = 'checks-off';
159
+ const REPLACED = 'test-replaced';
143
160
 
144
161
  const TAMPER_ADDED = [
145
162
  { re: /\b(it|test|describe)\.skip\s*\(/, kind: SKIPPED, why: 'a test was skipped (.skip)' },
146
163
  { re: /\b(xit|xdescribe|xtest)\s*\(/, kind: SKIPPED, why: 'a test was disabled (xit/xdescribe)' },
147
- { re: /@pytest\.mark\.skip|unittest\.skip/, kind: SKIPPED, why: 'a Python test was skipped' },
164
+ // skip, and skipif or skipIf only when the condition is a literal: skipUnless(shutil.which("uv")) is a
165
+ // test that cannot run on this machine, as { skip: process.platform === 'win32' } is in JS
166
+ { re: /@pytest\.mark\.skip\b|\bunittest\.skip\s*\(|\b(skipif|skipIf)\s*\(\s*True\b|\bskipUnless\s*\(\s*False\b/, kind: SKIPPED, why: 'a Python test was skipped' },
148
167
  { re: /@ts-nocheck/, kind: CHECKS_OFF, why: 'type checking was turned off (@ts-nocheck)' },
149
168
  { re: /--no-verify/, kind: CHECKS_OFF, why: 'git hooks were bypassed (--no-verify)' },
150
169
  { re: /expect\([^)]*\)\.(toBe|toEqual)\(\s*(true|1)\s*\)\s*;?\s*\/\/\s*(todo|temp|hack)/i, kind: WEAKENED, why: 'an assertion was replaced with a placeholder' },
@@ -162,21 +181,346 @@ const TAMPER_ADDED_IN_TESTS = [
162
181
  { re: new RegExp(String.raw`\bassert\.\w+\(\s*${LITERAL}\s*,\s*\1\s*\)`), kind: WEAKENED, why: 'an assertion that cannot fail was added' },
163
182
  { re: new RegExp(String.raw`\bexpect\(\s*${LITERAL}\s*\)\.(toBe|toEqual|toStrictEqual)\(\s*\1\s*\)`), kind: WEAKENED, why: 'an assertion that cannot fail was added' },
164
183
  ];
165
- // assert(...), assert.equal(...), expect(...), self.assertEqual(...), and pytest's bare `assert x`;
166
- // not expect.objectContaining(...) and the other matchers inside one assertion
167
- const ASSERT = /\b(expect\s*\(|assert[\w.]*\s*\()|^[+-]\s*assert\s/;
184
+ // assert(...), assert.equal(...), expect(...), self.assertEqual(...), .NET's Assert.Equal(...), and pytest's bare
185
+ // `assert x`; not expect.objectContaining(...) and the other matchers inside one assertion
186
+ const ASSERT = /\b(expect\s*\(|assert[\w.]*\s*\(|Assert\.\w+\s*\()|^[+-]\s*assert\s/;
168
187
  // package.json's test script made to pass whatever the tests do: `|| true`, `|| :`, `|| echo`, `; exit 0`
169
188
  const ALWAYS_PASSES = /"test"\s*:\s*"[^"]*(\|\|\s*(true|:|exit\s+0|echo\b[^"&|;]*)|;\s*(true|exit\s+0))\s*"/;
170
189
  const COMMENT = /^[+-]\s*(\/\/|#|\/\*|\*)\s*/;
171
190
 
191
+ // Options that make the runner leave tests out (node --test, jest, vitest, mocha, playwright, pytest,
192
+ // go, cargo), on a test script or a pytest addopts line. Reporter, coverage and other options are not.
193
+ const FILTER = /(?:^|\s)(--test-skip-pattern|--test-name-pattern|--testPathIgnorePatterns|--testNamePattern|--testPathPatterns?|--grep-invert|--grep|--fgrep|--invert|--ignore(?:-glob)?|--deselect|--skip|-skip|-run|-k)(?=[\s=]|$)/g;
194
+ const TEST_SCRIPT = /"(test(?::[^"]*)?)"\s*:\s*"((?:[^"\\]|\\.)*)"/;
195
+ const ADDOPTS = /^[+-]\s*"?addopts"?\s*=(.*)$/;
196
+ const PYTEST_CONFIG = /(^|\/)(pytest\.ini|pyproject\.toml|setup\.cfg|tox\.ini)$/;
197
+ // The same, as keys in a runner's own config file
198
+ const RUNNER_CONFIG = /(^|\/)(jest|vitest|playwright)\.config\.[\w.]+$|(^|\/)\.mocharc(\.[\w]+)?$/;
199
+ const CONFIG_KEY = /(?:^|[\s{,'"])(testPathIgnorePatterns|testNamePattern|testIgnore|grepInvert|grep|invert|ignore)['"]?\s*:/g;
200
+ export const isTestConfig = (file) => /(^|\/)package\.json$/.test(file) || PYTEST_CONFIG.test(file) || RUNNER_CONFIG.test(file);
201
+
202
+ // The options a line of this file sets that leave tests out, with where they were set.
203
+ function filtersOn(file, line) {
204
+ let text = null;
205
+ let where = '';
206
+ let script = null;
207
+ let re = FILTER;
208
+ if (/(^|\/)package\.json$/.test(file)) {
209
+ [, script = null, text = null] = TEST_SCRIPT.exec(line) || [];
210
+ where = 'by the test script';
211
+ // the script as npm runs it: \" is a quote (round 12)
212
+ try {
213
+ if (text != null) text = JSON.parse(`"${text}"`);
214
+ } catch {}
215
+ // jest's own config under package.json's "jest" key (round 12)
216
+ if (text == null && PKG_JEST_KEY.test(line)) (text = line.slice(1)), (where = "by the test runner's config"), (re = PKG_JEST_KEY_G);
217
+ } else if (PYTEST_CONFIG.test(file)) (text = (ADDOPTS.exec(line) || [])[1] ?? null), (where = "by the test runner's options");
218
+ else if (RUNNER_CONFIG.test(file)) (text = line.slice(1)), (where = "by the test runner's config"), (re = CONFIG_KEY);
219
+ if (text == null) return [];
220
+ const found = [...text.matchAll(re)].map((m) => ({ option: m[1], where, script, text }));
221
+ // jest's and vitest's -t (--testNamePattern); mocha's -t is a timeout, so only after jest or vitest as the command
222
+ // word, not inside jest-extended or vitest.config.ts (rounds 9 and 10)
223
+ if (where === 'by the test script' && JEST_T.test(text)) found.push({ option: '-t', where, script, text });
224
+ return found;
225
+ }
226
+ const JEST_T = /(?:^|[\s;&|(/])(?:jest|vitest)(?:\.[cm]?js)?\s(?:[^&|;]*?\s)?-t(?=[\s=]|$)/; // also by its path (round 11)
227
+ const PKG_JEST_KEY = /"(testPathIgnorePatterns|testNamePattern)"\s*:/;
228
+ const PKG_JEST_KEY_G = new RegExp(PKG_JEST_KEY.source, 'g');
229
+
230
+ // Which characters of a line are inside a string literal: test code often writes another test as
231
+ // text, and a skip in that text is not a skip. One line at a time, so a string that spans lines (a
232
+ // template literal, a docstring) is not seen; after // or a # comment, no quote opens a string. A quote in a
233
+ // /* comment */ or a regex literal opens none either, so "/* don't */ it.skip(" is a skip (cold review, L13
234
+ // round 5). A / starts a regex where an expression can start: after an operator, a bracket or nothing.
235
+ function stringMask(line) {
236
+ const mask = new Uint8Array(line.length);
237
+ let quote = '';
238
+ let last = ''; // the last character outside strings and comments that is not a space
239
+ for (let i = 0; i < line.length; i++) {
240
+ const c = line[i];
241
+ if (quote) {
242
+ mask[i] = 1;
243
+ if (c === '\\') mask[++i] = 1;
244
+ else if (c === quote) (quote = ''), (last = c);
245
+ continue;
246
+ }
247
+ if (c === '"' || c === "'" || c === '`') (quote = c), (mask[i] = 1);
248
+ else if (c === '/' && line[i + 1] === '*') {
249
+ const end = line.indexOf('*/', i + 2);
250
+ if (end < 0) break;
251
+ i = end + 1;
252
+ continue;
253
+ } else if ((c === '/' && line[i + 1] === '/') || (c === '#' && (i <= 1 || /\s/.test(line[i - 1])))) break;
254
+ else if (c === '/' && (!last || '(,=:[!&|?{};+-*%<>~^'.includes(last) || REGEX_AFTER.test(line.slice(Math.max(0, i - 12), i)))) {
255
+ let j = i + 1;
256
+ for (let inClass = false; j < line.length; j++) {
257
+ if (line[j] === '\\') j++;
258
+ else if (line[j] === '[') inClass = true;
259
+ else if (line[j] === ']') inClass = false;
260
+ else if (line[j] === '/' && !inClass) break;
261
+ }
262
+ if (j < line.length) i = j;
263
+ }
264
+ if (!/\s/.test(c)) last = line[i];
265
+ }
266
+ mask.open = quote;
267
+ return mask;
268
+ }
269
+ const REGEX_AFTER = /(?:^|[^\w$.])(?:return|typeof|void|delete|in|of|instanceof|new|throw|case|do|else|yield|await)\s*$/;
270
+
271
+ // An assertion line with its values taken out. A removed and an added line with the same shape and other
272
+ // values are one assertion whose expected value changed; assertTrue and assertFalse share a shape.
273
+ // numbers may use _ as a separator (10_000), as JS and Python allow
274
+ const VALUES = /(?<![\w.$])(?:\d[\d_]*(?:\.\d+)?|true|false|True|False|null|None|undefined)(?![\w$])|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^`\\]|\\.)*`/g;
275
+ function shape(line) {
276
+ const values = [];
277
+ const text = line
278
+ .slice(1)
279
+ .trim()
280
+ .replace(/\bassert(True|False)\b/g, (m, b) => (values.push(b), 'assert<b>'))
281
+ .replace(VALUES, (v) => (values.push(v.slice(0, 40)), '<v>'));
282
+ return { text, values };
283
+ }
284
+
285
+ // The arguments of the call whose '(' is at open, split at top-level commas; null if it does not close.
286
+ function argsAt(t, open) {
287
+ const args = [];
288
+ let depth = 0;
289
+ let quote = '';
290
+ let start = open + 1;
291
+ for (let i = open; i < t.length; i++) {
292
+ const c = t[i];
293
+ if (quote) {
294
+ if (c === '\\') i++;
295
+ else if (c === quote) quote = '';
296
+ } else if (c === '"' || c === "'" || c === '`') quote = c;
297
+ else if ('([{'.includes(c)) depth++;
298
+ else if (')]}'.includes(c)) {
299
+ if (--depth === 0) return (args.push(t.slice(start, i).trim()), { args, end: i });
300
+ } else if (c === ',' && depth === 1) args.push(t.slice(start, i).trim()), (start = i + 1);
301
+ }
302
+ return null;
303
+ }
304
+
305
+ // Where a top-level operator (==) starts in a Python assert, outside brackets and strings; -1 if none.
306
+ function topLevel(t, op) {
307
+ let depth = 0;
308
+ let quote = '';
309
+ for (let i = 0; i < t.length; i++) {
310
+ const c = t[i];
311
+ if (quote) {
312
+ if (c === '\\') i++;
313
+ else if (c === quote) quote = '';
314
+ } else if (c === '"' || c === "'") quote = c;
315
+ else if ('([{'.includes(c)) depth++;
316
+ else if (')]}'.includes(c)) depth--;
317
+ else if (depth === 0 && t.startsWith(op, i)) return i;
318
+ }
319
+ return -1;
320
+ }
321
+
322
+ // An assertion's subject and whether it checks a value (equal, toBe, assertEqual, ==) or only that the
323
+ // subject is set or truthy (ok, assert(), toBeDefined, toBeTruthy, assertTrue, a bare Python assert).
324
+ // The same subject going from the first kind to the second is a weakened assertion.
325
+ const VALUE_CALL = /\b(equal|strictEqual|deepEqual|deepStrictEqual|assertEqual|assertEquals)\s*\(/;
326
+ const TRUTHY_CALL = /(?:^|[^\w.])(assert\.ok|assert)\s*\(|\b(assertTrue|assertIsNotNone)\s*\(/;
327
+ const EXPECT_VALUE = /^\.(toBe|toEqual|toStrictEqual)\s*\(/;
328
+ const EXPECT_TRUTHY = /^\.(toBeDefined|toBeTruthy|not\.toBeNull|not\.toBeUndefined)\s*\(\s*\)/;
329
+ function strength(line) {
330
+ const t = line.slice(1).trim().replace(/;$/, '');
331
+ let m = VALUE_CALL.exec(t);
332
+ if (m) {
333
+ const c = argsAt(t, m.index + m[0].length - 1);
334
+ return c && c.args.length >= 2 ? { subject: c.args[0], value: true, form: `${m[1]}(…)`, expected: c.args[1] } : null;
335
+ }
336
+ m = /\bexpect\s*\(/.exec(t);
337
+ if (m) {
338
+ const c = argsAt(t, m.index + m[0].length - 1);
339
+ if (!c) return null;
340
+ const rest = t.slice(c.end + 1);
341
+ const v = EXPECT_VALUE.exec(rest);
342
+ if (v) {
343
+ const e = argsAt(rest, rest.indexOf('('));
344
+ return { subject: c.args[0], value: true, form: `${v[1]}(…)`, expected: e ? e.args[0] : '' };
345
+ }
346
+ const w = EXPECT_TRUTHY.exec(rest);
347
+ return w ? { subject: c.args[0], value: false, form: `${w[1]}()` } : null;
348
+ }
349
+ m = TRUTHY_CALL.exec(t);
350
+ if (m) {
351
+ const c = argsAt(t, m.index + m[0].length - 1);
352
+ return c ? { subject: c.args[0], value: false, form: `${(m[1] || m[2]).replace('assert.', '')}(…)` } : null;
353
+ }
354
+ m = /^assert\s+(.+)$/.exec(t);
355
+ if (m) {
356
+ const comma = topLevel(m[1], ',');
357
+ const body = (comma >= 0 ? m[1].slice(0, comma) : m[1]).trim(); // drop a message
358
+ const eq = topLevel(body, '==');
359
+ if (eq >= 0) return { subject: body.slice(0, eq).trim(), value: true, form: '==', expected: body.slice(eq + 2).trim() };
360
+ const none = /^(.+?)\s+is\s+not\s+None$/.exec(body);
361
+ if (none) return { subject: none[1].trim(), value: false, form: 'is not None' };
362
+ if (/[<>!]=?|\bin\b|\bis\b/.test(body)) return null;
363
+ return { subject: body, value: false, form: 'a bare assert' };
364
+ }
365
+ return null;
366
+ }
367
+
368
+ // A catch that swallows what was thrown: catch {}, catch (e) {} or catch (e) { /* ignore */ } on one line;
369
+ // a catch line that opens a body which the next line closes; Python's except ...: pass, on one or two lines.
370
+ const CATCH_EMPTY = /\bcatch\s*(?:\(\s*[\w$]*\s*\))?\s*\{\s*(?:\/\*[^*]*\*\/|\/\/[^\n]*)?\s*\}/;
371
+ const CATCH_OPEN = /\bcatch\s*(?:\(\s*[\w$]*\s*\))?\s*\{\s*$/;
372
+ const EXCEPT_PASS = /^\s*except\b[^:]*:\s*pass\s*(#.*)?$/;
373
+ const EXCEPT_OPEN = /^\s*except\b[^:]*:\s*(#.*)?$/;
374
+ const TRY_OPEN = /\btry\s*\{|^\s*try\s*:/;
375
+
376
+ // Whether the try that a swallowing catch at line `at` (1-based) closes holds an assertion. The lines are the
377
+ // file as it is now when it can be read, else the lines of the diff around the catch (git's context).
378
+ function swallowsAssertion(lines, at, catchText) {
379
+ let i = at - 1;
380
+ if ((lines[i] || '').trim() !== catchText.trim()) {
381
+ const near = [-3, -2, -1, 1, 2, 3].map((d) => at - 1 + d).find((k) => (lines[k] || '').trim() === catchText.trim());
382
+ if (near === undefined) return false;
383
+ i = near;
384
+ }
385
+ for (let k = i, n = 0; k >= 0 && n < 60; k--, n++) {
386
+ let text = lines[k] || '';
387
+ const cut = k === i ? text.search(/\bcatch\b|\bexcept\b/) : -1;
388
+ if (cut >= 0) text = text.slice(0, cut); // try { assert(...) } catch {} on one line: the part before the catch
389
+ const open = text.search(TRY_OPEN);
390
+ if (ASSERT.test('+' + (open >= 0 ? text.slice(open).replace(TRY_OPEN, '') : text))) return true;
391
+ if (open >= 0) return false;
392
+ }
393
+ return false;
394
+ }
395
+
396
+ // Literals in a piece of code: numbers (10_000 is 10000) and the contents of quoted strings, tagged so the
397
+ // number 500 and the string "500" stay apart.
398
+ const LITERAL_RE = /(?<![\w.$])-?\d[\d_]*(?:\.\d+)?(?![\w$])|'((?:[^'\\]|\\.)*)'|"((?:[^"\\]|\\.)*)"/g;
399
+ function literals(text) {
400
+ return [...String(text).matchAll(LITERAL_RE)].map((m) => (m[1] !== undefined || m[2] !== undefined ? `s:${m[1] ?? m[2]}` : `n:${Number(m[0].replace(/_/g, ''))}`));
401
+ }
402
+ // The literals a condition singles an input out by: price === 250, 250 == price, [250].includes(price),
403
+ // price in (250,), Object.is(price, 250). A literal inside arithmetic (n % 15 === 0) is not one (cold review,
404
+ // L13 round 5); a comment after it does not make it arithmetic (round 6).
405
+ const LIT = String.raw`-?\d[\d_]*(?:\.\d+)?(?![\w$])|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"`;
406
+ const LITS = String.raw`(?:${LIT})(?:\s*,\s*(?:${LIT}))*\s*,?`;
407
+ const COMPARED = new RegExp(
408
+ [
409
+ String.raw`(?<![=!<>])===?\s*(${LIT})(?!\s*(?:[-+*%]|\/(?![/*])))`,
410
+ String.raw`(?<![-+*/%]\s*|[\w$.])(${LIT})\s*===?(?!=)`,
411
+ String.raw`\[\s*(${LITS})\s*\]\s*\.includes\s*\(`,
412
+ String.raw`\bin\s*[[({]\s*(${LITS})\s*[\])}]`,
413
+ String.raw`\bObject\.is\s*\(([^()]*)\)`,
414
+ ].join('|'),
415
+ 'g'
416
+ );
417
+ function comparedLiterals(cond) {
418
+ return [...String(cond).matchAll(COMPARED)].flatMap((m) => literals(m.slice(1).find((x) => x !== undefined)));
419
+ }
420
+ // Values too common to say anything when code returns them
421
+ const DULL = new Set(['n:0', 'n:1', 'n:-1', 'n:2', 's:']);
422
+
423
+ // What a test line expects, when it compares a call on literal inputs with one literal value.
424
+ function expectationOf(line) {
425
+ const st = strength('+' + line);
426
+ if (!st || !st.value || !st.expected) return null;
427
+ const want = literals(st.expected);
428
+ if (want.length !== 1 || st.expected.replace(/^[-'"]|['"]$/g, '').length === 0) return null;
429
+ return { inputs: new Set(literals(st.subject)), expected: want[0] };
430
+ }
431
+
432
+ // A return of one literal under a condition, on the same line or the line before: if (x === 250) return 500;
433
+ const RETURN_LITERAL = /\breturn\s+(-?\d[\d_]*(?:\.\d+)?|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")\s*;?\s*\}?\s*$/;
434
+ const SAME_LINE_IF = /\bif\s*\(?(.*?)\)?\s*(?:\{|:)?\s*return\b/;
435
+
436
+ // A test runner's own markers, read by code outside the tests: the code can then behave one way under the
437
+ // runner and another way for users. NODE_ENV === 'test' is not one: apps use it to pick a test database.
438
+ const RUNNER_MARKER = /\b(NODE_TEST_CONTEXT|JEST_WORKER_ID|VITEST_WORKER_ID|VITEST_POOL_ID|PYTEST_CURRENT_TEST)\b|(['"]pytest['"]\s+in\s+sys\.modules)|\b(testing\.Testing\(\))/;
439
+ // Test setup that may read them: pytest's conftest.py and the setup files jest and vitest load
440
+ const TEST_SETUP = /(^|\/)(conftest\.py|setupTests\.[cm]?[jt]sx?|(jest|vitest)\.setup\.[cm]?[jt]sx?)$/;
441
+
442
+ // CI files, and the commands in them that run the tests
443
+ // at the repo's top, where CI reads them: one moved elsewhere no longer runs (round 10)
444
+ const CI_FILE = /^\.github\/workflows\/[^/]+\.ya?ml$|^\.gitlab-ci\.yml$|^\.circleci\/config\.yml$|^(azure|bitbucket)-pipelines\.yml$/; // the names each reads by default (round 12)
445
+ // Every file that says what the tests are or how they run: test code in a language the scan reads, test configs,
446
+ // test setup and CI files. Codehere keeps its own record of these at session start, so a change to one is read
447
+ // even when git does not show it (docs/finish-check-promise.md). Data in a test folder is not one.
448
+ // A CI file at the repo's top; ../ in front when Codehere is set up in a subfolder of the repo (round 11)
449
+ export const isCiFile = (f) => CI_FILE.test(String(f).replace(/^(\.\.\/)+/, ''));
450
+ // The same for a known setup: up is the path from Codehere's folder to the repo's top ('' at the top, '../..' in a
451
+ // package), so the package's own .github/workflows is not the repo's CI (round 12)
452
+ export const ciFileIn = (up) => (f) => (up ? String(f).startsWith(`${up}/`) && CI_FILE.test(String(f).slice(up.length + 1)) : CI_FILE.test(String(f)));
453
+ export const isCheckFile = (f) => (isTestFile(f) && languageOf(f) != null) || isTestConfig(f) || isCiFile(f) || TEST_SETUP.test(f);
454
+ // Test code only: a check file that is not a config, setup or CI file. New test code in a folder git ignores is a
455
+ // build or an install, not the agent's tests; a new config or setup file changes how the tests run wherever it is.
456
+ export const isTestCode = (f) => isCheckFile(f) && !isTestConfig(f) && !isCiFile(f) && !TEST_SETUP.test(f);
457
+ // the common runners, workspace tools and build tools that run the tests (round 12)
458
+ const TEST_CMD = new RegExp(
459
+ [
460
+ String.raw`\b(?:npm|pnpm|yarn|bun)\b[^\n|&;]*?\btest\b`,
461
+ String.raw`\b(?:turbo|nx|lerna)\b[^\n|&;]*?\btest\b`,
462
+ String.raw`\bnode\s+--test\b`,
463
+ String.raw`\b(?:npx\s+)?(?:jest|vitest|mocha)\b`,
464
+ String.raw`\b(?:python3?\s+-m\s+)?pytest\b|\bpython3?\s+-m\s+unittest\b|\btox\b|\bnox\b`,
465
+ String.raw`\bgo\s+test\b|\bcargo\s+(?:test|nextest)\b|\bmake\s+(?:test|check)\b|\bctest\b`,
466
+ String.raw`\bgradlew?\b[^\n|&;]*?\b(?:test|check|build)\b|\bmvnw?\b[^\n|&;]*?\b(?:test|verify|install|package)\b`,
467
+ String.raw`\bdotnet\s+test\b|\b(?:bundle\s+exec\s+)?(?:rspec|rake\s+test)\b|\bmix\s+test\b|\bswift\s+test\b`,
468
+ String.raw`\bphpunit\b|\bpest\b|\b(?:dart|flutter)\s+test\b`,
469
+ ].join('|')
470
+ );
471
+ const CI_ALWAYS_PASSES = new RegExp(`(?:${TEST_CMD.source})` + String.raw`[^\n]*?(\|\|\s*(true|:|exit\s+0|echo\b)|;\s*(true|exit\s+0)\s*$)`);
472
+ const CI_IGNORE = /^\s*(continue-on-error|allow_failure)\s*:\s*true\b/;
473
+
474
+ // Whether the YAML block a key at line `at` belongs to (its step or job) runs the tests.
475
+ function blockRunsTests(lines, at, text) {
476
+ let i = at - 1;
477
+ if ((lines[i] || '').trim() !== text.trim()) {
478
+ const near = [-3, -2, -1, 1, 2, 3].map((d) => at - 1 + d).find((k) => (lines[k] || '').trim() === text.trim());
479
+ if (near === undefined) return false;
480
+ i = near;
481
+ }
482
+ const indent = (l) => l.length - l.trimStart().length;
483
+ const own = indent(lines[i]);
484
+ let start = i;
485
+ while (start > 0 && !((lines[start - 1] || '').trim() && indent(lines[start - 1]) < own)) start--;
486
+ start = Math.max(0, start - 1);
487
+ const top = indent(lines[start] || '');
488
+ for (let k = start; k < lines.length && k < start + 200; k++) {
489
+ if (k > start && (lines[k] || '').trim() && indent(lines[k]) <= top) break;
490
+ if (TEST_CMD.test(lines[k] || '')) return true;
491
+ }
492
+ return false;
493
+ }
494
+
495
+ // re found in the code of the line, not only inside its strings. A ' or " string still open where the line ends
496
+ // means the mask lost its place, as at a regex literal it read as a division: the whole line is then read as
497
+ // code, so a stray quote cannot hide a skip (cold review, L13 round 6). Python's triple quotes and a line
498
+ // continued with \ do open strings that end on a later line.
499
+ const OPEN_ON_PURPOSE = /'''|"""|\\\s*$/;
500
+ function inCode(re, line, mask) {
501
+ const g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g');
502
+ let m = mask();
503
+ if ((m.open === "'" || m.open === '"') && !OPEN_ON_PURPOSE.test(line)) m = [];
504
+ for (const x of line.matchAll(g)) if (!m[x.index + (x[0].length - x[0].trimStart().length)]) return true;
505
+ return false;
506
+ }
507
+
172
508
  // What the finish check prints: the events' text, in order, each once.
173
- export function tamperFindings(nameStatus, diff) {
174
- return tamperEvents(nameStatus, diff).map((e) => e.why);
509
+ export function tamperFindings(nameStatus, diff, opts) {
510
+ return tamperEvents(nameStatus, diff, opts).map((e) => e.why);
175
511
  }
176
512
 
177
513
  // Each finding as { kind, file, why }; file is undefined for the count of removed assertions,
178
514
  // which spans the whole diff. A why already seen is not repeated.
179
- export function tamperEvents(nameStatus, diff) {
515
+ // read(file), when given, returns the file as it is now (or null): the finish check passes one, the audit
516
+ // does not and relies on the diff's context lines. tests, when given, is the repo's test files as
517
+ // "path\0content" (or a function returning them, called only when it is needed), for the expected values a
518
+ // hardcoded answer would copy. testFiles, when given, is a function returning the paths of the repo's test
519
+ // files before the change (at session start, or at the commit's parent), called only for a test renamed to
520
+ // another code extension.
521
+ export function tamperEvents(nameStatus, diff, { read, tests, testFiles, command, up = '' } = {}) {
522
+ // CI files at the repo's top: with Codehere set up in a subfolder, a path from root that starts with up/ (round 12)
523
+ const isCi = ciFileIn(up);
180
524
  const events = [];
181
525
  const seen = new Set();
182
526
  const add = (kind, file, why) => {
@@ -184,39 +528,317 @@ export function tamperEvents(nameStatus, diff) {
184
528
  seen.add(why);
185
529
  events.push({ kind, file, why });
186
530
  };
187
- for (const line of nameStatus.split('\n')) {
188
- const [status, file] = line.split(/\s+/);
189
- if (status === 'D' && file && TEST_FILE.test(file)) add(DELETED, file, `test file deleted: ${file}`);
531
+ const ext = (f) => (f.match(/\.[^./]+$/) || [''])[0];
532
+ // A rename to another code extension (.mjs to .ts) is a migration when tests the repo already had use that
533
+ // extension, or other tests made the same move in this change. A lone test moved to one may stop running:
534
+ // Node 22's test runner skips .ts files, Node 24's runs them (cold review, L13 round 5). A new file does not
535
+ // count, so an empty zz.test.ts added beside the move does not make it one (round 6, m6).
536
+ const entries = nameStatus.split('\n').map((line) => line.split('\t').map(unquotePath));
537
+ const renames = entries.filter(([status, file, to]) => /^R\d*$/.test(status) && file && to && isTestFile(file));
538
+ let others = null;
539
+ const migrated = (file, to) =>
540
+ languageOf(to) != null &&
541
+ (renames.some(([, a, b]) => b !== to && ext(a) === ext(file) && ext(b) === ext(to)) ||
542
+ (typeof testFiles === 'function' && (others ||= testFiles()).some((f) => f !== to && f !== file && ext(f) === ext(to))));
543
+ for (const [status, file, to] of entries) {
544
+ if (status === 'D' && file && isTestFile(file)) add(DELETED, file, `test file deleted: ${file}`);
545
+ // a rename to a name that is not a test file, that drops the test name inside a test folder (test/cart.test.mjs
546
+ // to test/cart.mjs), or to another extension (.off, .bak), hides the test
547
+ const dropsName = TEST_NAME.test(file) && !TEST_NAME.test(to);
548
+ if (/^R\d*$/.test(status) && to && isTestFile(file) && (!isTestFile(to) || dropsName || (ext(to) !== ext(file) && !migrated(file, to)))) add(DELETED, file, `test file renamed: ${file} to ${to}`);
190
549
  }
550
+ // A test:* script counts unless nothing can run it: no other script names it or holds a glob matching it
551
+ // (run-s test:*; a lint glob such as "src/**/*.js" is not one: round 12), and the approved command does not name it.
552
+ // A side script such as test:smoke, called by nothing, is not what the tests run (round 10); a chain or a glob is
553
+ // (round 11). Without the file as it is now (the audit), every one counts.
554
+ const runsTests = (script) => {
555
+ if (!script || script === 'test' || !read) return true;
556
+ try {
557
+ const scripts = JSON.parse(read(current) || '{}').scripts || {};
558
+ const globbed = (v) => (v.match(/[\w-]+:[\w:*-]*\*[\w:*-]*/g) || []).some((g) => new RegExp(`^${g.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*')}$`).test(script));
559
+ const named = Object.entries(scripts).some(([k, v]) => k !== script && typeof v === 'string' && (v.includes(script) || globbed(v)));
560
+ return named || String(command || '').includes(script);
561
+ } catch {
562
+ return true;
563
+ }
564
+ };
565
+ // A filter option's values: a filter that already held some is new again when a value is added, such as a second
566
+ // path in testPathIgnorePatterns or a second --ignore (round 11). In a config, only the key's own value counts, not
567
+ // other keys on the same line (round 12).
568
+ const filterKey = (f) => `${f.script || ''}\0${f.option}`;
569
+ const ownValue = (text, key) => {
570
+ const at = text.indexOf(key);
571
+ if (at < 0) return text;
572
+ const rest = text.slice(at + key.length).replace(/^['"]?\s*:\s*/, '');
573
+ const end = rest.startsWith('[') ? rest.indexOf(']') + 1 : rest.search(/[,}]/);
574
+ return end > 0 ? rest.slice(0, end) : rest;
575
+ };
576
+ const quoted = (text) => [...text.matchAll(/'([^']*)'|"([^"]*)"|`([^`]*)`/g)].map((m) => m[1] ?? m[2] ?? m[3]);
577
+ const filterValues = (f) => {
578
+ if (f.where === "by the test runner's config") return quoted(ownValue(f.text, f.option));
579
+ const opt = f.option.replace(/[.*+?^${}()|[\]\\-]/g, '\\$&');
580
+ return [...f.text.matchAll(new RegExp(`(?:^|\\s)${opt}(?:=|\\s+)("[^"]*"|'[^']*'|[^\\s"']+)`, 'g'))].map((m) => m[1]);
581
+ };
582
+ // a key line that opens a list whose items come on the next lines: the items are judged one by one
583
+ const opensList = (f) => f.where === "by the test runner's config" && /\[[^\]]*$/.test(ownValue(f.text, f.option));
584
+ const filterSeen = (had, f) => {
585
+ const set = had.get(filterKey(f)) || new Set();
586
+ const vals = filterValues(f);
587
+ vals.forEach((v) => set.add(v));
588
+ if (!vals.length) set.add('');
589
+ had.set(filterKey(f), set);
590
+ };
591
+ const filterIsNew = (f, extra = new Set()) => {
592
+ const had = hadFilters.get(filterKey(f));
593
+ const vals = filterValues(f);
594
+ if (!runsTests(f.script)) return false;
595
+ if (opensList(f)) return !had;
596
+ return !had || (vals.length ? vals.some((v) => !had.has(v) && !extra.has(v)) : !had.has(''));
597
+ };
598
+ // A line inside a list or an addopts block that starts on an earlier line: the file as it is now says which key
599
+ // it belongs to (testPathIgnorePatterns: [ ... ], addopts = then indented lines: round 12). One pass over the file
600
+ // works out every line's key, so a long config costs one read (a line-by-line walk timed out on 200,000 lines).
601
+ // Null without the file.
602
+ const keyed = new Map();
603
+ const enclosing = (file, at) => {
604
+ if (!read || !at) return null;
605
+ if (!keyed.has(file)) {
606
+ const lines = (read(file) || '').split('\n');
607
+ const out = new Array(lines.length).fill(null);
608
+ const pytest = PYTEST_CONFIG.test(file);
609
+ const keyRe = RUNNER_CONFIG.test(file) ? new RegExp(CONFIG_KEY.source) : /(^|\/)package\.json$/.test(file) ? PKG_JEST_KEY : null;
610
+ const stack = [];
611
+ let inAddopts = false;
612
+ for (let k = 0; k < lines.length; k++) {
613
+ const line = lines[k].slice(0, 10000);
614
+ if (pytest) {
615
+ // ini continuation lines are indented under their key; pyproject's addopts = [ items are too
616
+ if (/^\s+\S/.test(line)) out[k] = inAddopts ? { kind: 'options', where: "by the test runner's options" } : null;
617
+ else if (line.trim()) inAddopts = /^\s*"?addopts"?\s*=/.test(line);
618
+ continue;
619
+ }
620
+ if (!keyRe) continue;
621
+ out[k] = stack.length ? stack[stack.length - 1] : null;
622
+ const m = keyRe.exec(line);
623
+ for (const c of line.match(/[[\]]/g) || []) {
624
+ if (c === '[') stack.push(m ? { kind: 'items', option: m[1], where: "by the test runner's config" } : stack[stack.length - 1] || null);
625
+ else stack.pop();
626
+ }
627
+ }
628
+ keyed.set(file, out);
629
+ }
630
+ return keyed.get(file)[at - 1] || null;
631
+ };
632
+ // Every filter a removed line of a test config held, per file, before any added line is judged: with -U0 a line
633
+ // that moved up comes before its removal (round 12). Quoted values on removed lines with no key are kept too, for
634
+ // list items that moved.
635
+ const removedFilters = new Map();
636
+ const removedItems = new Map();
637
+ {
638
+ const inPre = hunkReader();
639
+ let file = '';
640
+ let old = '';
641
+ for (const line of diff.split('\n')) {
642
+ const body = inPre(line);
643
+ if (!body && line.startsWith('--- ')) old = unquotePath(line.slice(4).replace(/\t$/, '')).replace(/^a\//, '');
644
+ else if (!body && line.startsWith('+++ ')) {
645
+ file = unquotePath(line.slice(4).replace(/\t$/, '')).replace(/^b\//, '');
646
+ if (file === '/dev/null' && isCi(old)) file = old;
647
+ } else if (line.startsWith('-') && !(!body && line.startsWith('--- ')) && isTestConfig(file)) {
648
+ const found = filtersOn(file, line.slice(0, 10000));
649
+ if (!removedFilters.has(file)) removedFilters.set(file, new Map());
650
+ found.forEach((f) => filterSeen(removedFilters.get(file), f));
651
+ if (!found.length) quoted(line).forEach((v) => (removedItems.get(file) || removedItems.set(file, new Set()).get(file)).add(v));
652
+ }
653
+ }
654
+ }
655
+ let oldPath = ''; // the file a "--- a/" header names: a deleted CI file's lines are read under its own name (round 10)
191
656
  let current = '';
192
657
  let removedAsserts = 0;
193
658
  let addedAsserts = 0;
194
659
  let removed = new Set(); // this file's removed assertions, to spot one coming back as a comment
660
+ let removedShapes = new Map(); // this file's removed assertions by shape, to spot a changed expected value
661
+ let removedValueChecks = new Map(); // this file's removed value checks by subject, to spot a weakened one
662
+ const changedIn = new Set(); // one changed-value finding per file
663
+ let newLine = 0; // the line number in the file as it is now, from the hunk headers
664
+ let fileLines = []; // this file's lines from the diff (added and context), by line number, for the audit
665
+ let pendingCatch = 0; // a catch or except line whose body may be closed or passed on the next line
666
+ let prevText = ''; // the previous added or context line of this file
667
+ const expectations = []; // from test lines in the diff, then from the repo's test files
668
+ const returns = []; // literal returns under a condition in code files, checked after the diff is read
669
+ const ciRemoved = []; // CI files that lost a line running the tests
670
+ let ciAdded = 0; // lines running the tests added to any CI file
671
+ const weakenedIn = new Set(); // one weakened-matcher finding per file
195
672
  let hadFallback = false; // the test script already ended in `|| true` before this session
673
+ let hadFilters = new Map(); // options that left tests out before this session, with their values
674
+ const inHunk = hunkReader();
196
675
  for (const line of diff.split('\n')) {
197
- if (line.startsWith('+++ ')) {
198
- current = line.replace(/^\+\+\+ (b\/)?/, '');
676
+ const body = inHunk(line);
677
+ if (!body && line.startsWith('+++ ')) {
678
+ current = unquotePath(line.slice(4).replace(/\t$/, '')).replace(/^b\//, ''); // git ends a path holding a space with a tab
679
+ if (current === '/dev/null' && isCi(oldPath)) current = oldPath;
199
680
  removed = new Set();
681
+ removedShapes = new Map();
682
+ removedValueChecks = new Map();
683
+ fileLines = [];
684
+ pendingCatch = 0;
685
+ prevText = '';
200
686
  hadFallback = false;
687
+ hadFilters = new Map([...(removedFilters.get(current) || new Map())].map(([k, v]) => [k, new Set(v)]));
688
+ continue;
689
+ }
690
+ if (!body && line.startsWith('--- ')) {
691
+ oldPath = unquotePath(line.slice(4).replace(/\t$/, '')).replace(/^a\//, '');
201
692
  continue;
202
693
  }
203
- if (line.startsWith('--- ')) continue;
204
- const inTest = TEST_FILE.test(current);
694
+ if (line.startsWith('@@')) {
695
+ newLine = Number((/\+(\d+)/.exec(line) || [])[1] || 0);
696
+ continue;
697
+ }
698
+ const at = line.startsWith('-') ? 0 : newLine;
699
+ if (!line.startsWith('-')) newLine++;
700
+ if (at && at <= 20000) fileLines[at - 1] = line.slice(1);
701
+ const inTest = isTestFile(current);
702
+ if (isCi(current) && !COMMENT.test(line)) {
703
+ const text = line.slice(1, 10001);
704
+ if (line.startsWith('-') && TEST_CMD.test(text)) ciRemoved.push(current);
705
+ if (line.startsWith('+') && TEST_CMD.test(text)) ciAdded++;
706
+ if (line.startsWith('+') && CI_ALWAYS_PASSES.test(text)) add(CHECKS_OFF, current, `the CI test step was changed to always pass in ${current}`);
707
+ if (line.startsWith('+') && CI_IGNORE.test(text)) {
708
+ const now = read ? read(current) : null;
709
+ if (blockRunsTests(now == null ? fileLines : now.split('\n'), at, text)) add(CHECKS_OFF, current, `the CI test step was changed to always pass in ${current}`);
710
+ }
711
+ }
712
+ const before = prevText;
713
+ if (!line.startsWith('-')) prevText = line.slice(1, 10001);
714
+ if (inTest && !line.startsWith('-')) {
715
+ const e = expectationOf(line.slice(1, 10001));
716
+ if (e) expectations.push(e);
717
+ } else if (!inTest && line.startsWith('+') && !isTestConfig(current)) {
718
+ const marker = !COMMENT.test(line) && !TEST_SETUP.test(current) && !FIXTURE.test(current) && RUNNER_MARKER.exec(line.slice(1, 10001));
719
+ if (marker) add(UNDER_TEST, current, `${current} checks whether it is running under a test (${(marker[1] || marker[2] || marker[3]).replace(/['"]/g, '')})`);
720
+ const r = RETURN_LITERAL.exec(line.slice(1, 10001));
721
+ if (r) {
722
+ const same = SAME_LINE_IF.exec(line.slice(1, 10001));
723
+ const cond = same ? same[1] : /^\s*if\b/.test(before) ? before : null;
724
+ if (cond != null) returns.push({ file: current, text: r[1], value: literals(r[1])[0], given: comparedLiterals(cond) });
725
+ }
726
+ }
205
727
  const probe = line.slice(0, 10000); // the patterns can take quadratic time on a long line
728
+ let lineMask = null;
729
+ const mask = () => (lineMask ||= stringMask(probe));
206
730
  if (/(^|\/)package\.json$/.test(current) && ALWAYS_PASSES.test(probe)) {
207
731
  if (line.startsWith('-')) hadFallback = true;
208
732
  else if (!hadFallback) add(CHECKS_OFF, current, `the test script was changed to always exit 0 in ${current}`);
209
733
  }
734
+ if (isTestConfig(current) && line.startsWith('+')) {
735
+ const found = filtersOn(current, probe);
736
+ for (const f of found) if (filterIsNew(f)) add(SKIPPED, current, `tests were left out ${f.where} (${f.option}) in ${current}`);
737
+ const inside = found.length ? null : enclosing(current, at);
738
+ const extra = removedItems.get(current) || new Set();
739
+ if (inside?.kind === 'items') {
740
+ const f = { option: inside.option, where: inside.where, script: null, text: probe.slice(1) };
741
+ if (quoted(f.text).length && filterIsNew(f, extra)) add(SKIPPED, current, `tests were left out ${f.where} (${f.option}) in ${current}`);
742
+ } else if (inside?.kind === 'options') {
743
+ const text = probe.slice(1).replace(/["',]/g, ' ');
744
+ for (const m of text.matchAll(FILTER)) {
745
+ const f = { option: m[1], where: inside.where, script: null, text };
746
+ if (filterIsNew(f, extra)) add(SKIPPED, current, `tests were left out ${f.where} (${f.option}) in ${current}`);
747
+ }
748
+ }
749
+ }
210
750
  if (line.startsWith('+')) {
211
- for (const t of TAMPER_ADDED) if (t.re.test(probe)) add(t.kind, current, `${t.why} in ${current}`);
751
+ for (const t of TAMPER_ADDED) if (inCode(t.re, probe, mask)) add(t.kind, current, `${t.why} in ${current}`);
752
+ // the rest of a line longer than the probe, in overlapping pieces, so a skip past its first 10,000 characters
753
+ // is read (cold review, L13 round 7); a single match longer than the 1,000-character overlap can be missed
754
+ for (let from = 9000; from < line.length; from += 9000) {
755
+ const piece = `+${line.slice(from, from + 10000)}`;
756
+ let pieceMask = null;
757
+ const pmask = () => (pieceMask ||= stringMask(piece));
758
+ for (const t of [...TAMPER_ADDED, ...(inTest ? TAMPER_ADDED_IN_TESTS : [])]) if (inCode(t.re, piece, pmask)) add(t.kind, current, `${t.why} in ${current}`);
759
+ if (isTestConfig(current)) for (const f of filtersOn(current, piece)) if (filterIsNew(f)) add(SKIPPED, current, `tests were left out ${f.where} (${f.option}) in ${current}`);
760
+ }
212
761
  if (!inTest) continue;
213
- for (const t of TAMPER_ADDED_IN_TESTS) if (t.re.test(probe)) add(t.kind, current, `${t.why} in ${current}`);
762
+ const text = probe.slice(1);
763
+ let swallowAt = 0;
764
+ let swallowText = '';
765
+ if (pendingCatch && at === pendingCatch.at + 1 && (text.trim() === '}' || text.trim() === 'pass')) [swallowAt, swallowText] = [pendingCatch.at, pendingCatch.text];
766
+ pendingCatch = 0;
767
+ if (inCode(CATCH_EMPTY, probe, mask) || EXCEPT_PASS.test(text)) [swallowAt, swallowText] = [at, text];
768
+ else if (inCode(CATCH_OPEN, probe, mask) || EXCEPT_OPEN.test(text)) pendingCatch = { at, text };
769
+ if (swallowAt) {
770
+ const now = read ? read(current) : null;
771
+ const lines = now == null ? fileLines : now.split('\n');
772
+ if (swallowsAssertion(lines, swallowAt, swallowText)) add(WEAKENED, current, `an assertion was wrapped in a try that swallows its failure in ${current}`);
773
+ }
774
+ for (const t of TAMPER_ADDED_IN_TESTS) if (inCode(t.re, probe, mask)) add(t.kind, current, `${t.why} in ${current}`);
214
775
  if (COMMENT.test(line)) {
215
776
  if (removed.has(line.replace(COMMENT, '').trim())) add(WEAKENED, current, `an assertion was commented out in ${current}`);
216
- } else if (ASSERT.test(line)) addedAsserts++;
217
- } else if (line.startsWith('-') && inTest && !COMMENT.test(line) && ASSERT.test(line)) {
777
+ } else if (inCode(ASSERT, probe, mask)) {
778
+ addedAsserts++;
779
+ const now = shape(probe);
780
+ const before = removedShapes.get(now.text);
781
+ const i = before ? before.findIndex((v, k) => v !== now.values[k]) : -1;
782
+ if (i >= 0 && !changedIn.has(current)) {
783
+ changedIn.add(current);
784
+ add(EXPECTED, current, `an expected value was changed in ${current} (${before[i]} → ${now.values[i]})`);
785
+ }
786
+ const st = strength(probe);
787
+ if (st && !st.value && removedValueChecks.has(st.subject) && !weakenedIn.has(current)) {
788
+ weakenedIn.add(current);
789
+ add(WEAKENED, current, `an assertion was weakened in ${current} (${removedValueChecks.get(st.subject)} became ${st.form})`);
790
+ }
791
+ }
792
+ } else if (line.startsWith('-') && inTest && !COMMENT.test(line) && inCode(ASSERT, probe, mask)) {
218
793
  removedAsserts++;
219
794
  removed.add(line.slice(1).trim());
795
+ const was = shape(probe);
796
+ removedShapes.set(was.text, was.values);
797
+ const st = strength(probe);
798
+ if (st && st.value) removedValueChecks.set(st.subject, st.form);
799
+ }
800
+ }
801
+ if (ciRemoved.length > ciAdded) add(CHECKS_OFF, ciRemoved[0], `the test step was removed from CI in ${ciRemoved[0]}`);
802
+ const candidates = returns.filter((r) => !DULL.has(r.value));
803
+ if (candidates.length) {
804
+ for (const t of (typeof tests === 'function' ? tests() : tests) || []) {
805
+ for (const l of String(t).slice(String(t).indexOf('\0') + 1).split('\n')) {
806
+ const e = expectationOf(l.slice(0, 10000));
807
+ if (e) expectations.push(e);
808
+ }
809
+ }
810
+ for (const r of candidates) {
811
+ // numbers first: a string in the condition is often a key ("price"), the number is the input
812
+ const order = [...r.given.filter((g) => g.startsWith('n:')), ...r.given.filter((g) => g.startsWith('s:'))];
813
+ const given = order.find((g) => !DULL.has(g) && expectations.some((e) => e.expected === r.value && e.inputs.has(g)));
814
+ const show = (g) => (g.startsWith('n:') ? g.slice(2) : JSON.stringify(g.slice(2)));
815
+ if (given) add(HARDCODED, r.file, `${r.file} returns ${r.text} when given ${show(given)}, the exact value a test expects`);
816
+ }
817
+ }
818
+ // Expected values, replaced tests and test data, by the rules ported from noskip (src/noskip.js). They need
819
+ // the files as they are now, so only the finish check runs them.
820
+ if (read) {
821
+ const replacedIn = new Set();
822
+ const WORDS = {
823
+ expected: (c) => `an expected value was changed in ${c.file} (${c.from} → ${c.to})`,
824
+ call: (c) => `the call a test checks was changed and its expected value kept in ${c.file} (${c.from} → ${c.to})`,
825
+ flipped: (c) => `an assertion was flipped to its opposite in ${c.file} (${c.from} → ${c.to})`,
826
+ assertion: (c) => `an assertion was changed in ${c.file} (${c.from} → ${c.to})`,
827
+ dropped: (c) => `a case was removed from a test list in ${c.file} (${c.from})`,
828
+ reordered: (c) => `test data was reordered in ${c.file} (${c.from} moved)`,
829
+ };
830
+ for (const c of testChanges(diff, { read, isTestFile })) {
831
+ if (c.type === 'skipped') add(SKIPPED, c.file, `a test that already existed was given a skip option in ${c.file} ("${c.from}")`);
832
+ else if (c.type === 'replaced') {
833
+ if (!replacedIn.has(c.file)) add(REPLACED, c.file, `a test was replaced by a different one in ${c.file} ("${c.from}" → "${c.to}"${c.where ? ` in ${c.where}` : ''})`);
834
+ replacedIn.add(c.file);
835
+ } else if (c.type === 'weakened') {
836
+ if (!weakenedIn.has(c.file)) add(WEAKENED, c.file, `an assertion was weakened in ${c.file} (${c.from} became ${c.to})`);
837
+ weakenedIn.add(c.file);
838
+ } else {
839
+ if (!changedIn.has(c.file)) add(EXPECTED, c.file, WORDS[c.what](c));
840
+ changedIn.add(c.file);
841
+ }
220
842
  }
221
843
  }
222
844
  if (removedAsserts - addedAsserts >= 2) {
@@ -261,8 +883,14 @@ const counts = (passed, failed) => (passed == null && failed == null ? null : {
261
883
  const SUMMARIES = [
262
884
  // node --test, spec and TAP reporters: "ℹ tests 3", "ℹ pass 2", "ℹ fail 1"
263
885
  { re: /^(?:ℹ|#) tests \d+$/gm, read: (t) => counts(num(t, /^(?:ℹ|#) pass (\d+)$/m), num(t, /^(?:ℹ|#) fail (\d+)$/m)) },
264
- // jest "Tests: 2 failed, 46 passed, 48 total", vitest " Tests 2 failed | 46 passed (48)"
265
- { re: /^\s*Tests:?\s+.*\d+ (?:passed|failed)\b.*$/gm, read: (t, line) => counts(num(line, /(\d+) passed/), num(line, /(\d+) failed/)) },
886
+ // jest "Tests: 2 failed, 46 passed, 48 total", vitest " Tests 2 failed | 46 passed (48)"; a line with only
887
+ // skipped or todo tests is 0 passed (round 9)
888
+ {
889
+ re: /^\s*Tests:?\s+.*\d+ (?:passed|failed|skipped|todo)\b.*$/gm,
890
+ read: (t, line) => counts(num(line, /(\d+) passed/) ?? 0, num(line, /(\d+) failed/)),
891
+ },
892
+ // jest and vitest with --passWithNoTests: "No tests found, exiting with code 0", "No test files found, exiting with code 0"
893
+ { re: /^No test(?: file)?s found, exiting with code 0\b.*$/gm, read: () => counts(0, 0) },
266
894
  // pytest "===== 1 failed, 1 passed in 0.02s ====="
267
895
  {
268
896
  re: /^[=\s]*(?:\d+ \w+(?:, )?)+ in [\d.]+s\b.*$/gm,