codehere 2.1.0-rc.3 → 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/noskip.js CHANGED
@@ -25,6 +25,8 @@
25
25
  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
26
  // SOFTWARE.
27
27
 
28
+ import { unquotePath } from './gitpath.js';
29
+
28
30
  // ---------- helpers (noskip src/util.js) ----------
29
31
 
30
32
  const normalizeLine = (text) => text.trim().replace(/\s+/g, ' ');
@@ -81,30 +83,30 @@ const EXT_LANG = {
81
83
  py: 'py', pyi: 'py', go: 'go', rs: 'rs', java: 'java', kt: 'java', kts: 'java', groovy: 'java', scala: 'java', cs: 'cs',
82
84
  rb: 'rb', php: 'php', swift: 'swift', dart: 'dart', c: 'c', cc: 'c', cpp: 'c', cxx: 'c', h: 'c', hh: 'c', hpp: 'c', hxx: 'c', ex: 'ex', exs: 'ex',
83
85
  };
84
- const languageOf = (p) => EXT_LANG[(/\.([^./]+)$/.exec(p) || [])[1]?.toLowerCase()] ?? null;
86
+ export const languageOf = (p) => EXT_LANG[(/\.([^./]+)$/.exec(p) || [])[1]?.toLowerCase()] ?? null;
85
87
 
86
88
  const STR = String.raw`(['"\x60])((?:\\.|(?!\1)[^\\])*)\1`;
87
89
  const NAME_PATTERNS = {
88
90
  js: [
89
91
  {
90
92
  re: new RegExp(
91
- String.raw`\b(?:it|test|specify)(?:\s*\.\s*(?:only|skip|concurrent|sequential|failing|fails|todo|fixme|skipIf\([^)]*\)|runIf\([^)]*\)|each\s*(?:\([^)]*\)|\x60[^\x60]*\x60)))*\s*\(\s*` + STR,
93
+ String.raw`\b(?:it|test|specify)(?:\s*\.\s*(?:only|skip|concurrent|sequential|failing|fails|todo|fixme|skipIf\([^)]{0,1000}\)|runIf\([^)]{0,1000}\)|each\s*(?:\([^)]{0,1000}\)|\x60[^\x60]*\x60)))*\s*\(\s*` + STR,
92
94
  'g',
93
95
  ),
94
96
  pick: (m) => m[2],
95
97
  },
96
98
  ],
97
99
  py: [{ re: /^[ \t]*(?:async[ \t]+)?def[ \t]+(test\w*)[ \t]*\(/gm }],
98
- go: [{ re: /^func\s+(?:\([^)]*\)\s*)?((?:Test|Benchmark|Fuzz|Example)\w*)\s*\(/gm }, { re: /\bt\.Run\(\s*"((?:\\.|[^"\\])*)"/g }, { re: /\bname:\s*"((?:\\.|[^"\\])*)"/g }],
99
- rs: [{ re: /#\[(?:[\w:]+::)?test\b[^\]]*\]\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?(?:async\s+)?fn\s+(\w+)/g }],
100
+ go: [{ re: /^func\s+(?:\([^)]{0,1000}\)\s*)?((?:Test|Benchmark|Fuzz|Example)\w*)\s*\(/gm }, { re: /\bt\.Run\(\s*"((?:\\.|[^"\\])*)"/g }, { re: /\bname:\s*"((?:\\.|[^"\\])*)"/g }],
101
+ rs: [{ re: /#\[(?:[\w:]+::)?test\b[^\]]{0,1000}\]\s*(?:#\[[^\]]{0,1000}\]\s*){0,20}(?:pub(?:\([^)\n]{0,200}\))?\s+)?(?:async\s+)?fn\s+(\w+)/g }],
100
102
  java: [
101
- { re: /@(?:Test|ParameterizedTest|RepeatedTest|TestFactory|TestTemplate)\b[^\n]*\n(?:\s*@[^\n]*\n)*\s*(?:(?:public|private|protected|internal|open|override|suspend|static|final)\s+)*(?:void\s+|fun\s+)?`?([^`(\n]+?)`?\s*\(/g },
103
+ { re: /@(?:Test|ParameterizedTest|RepeatedTest|TestFactory|TestTemplate)\b[^\n]{0,1000}\n(?:[ \t]*(?:@[^\n]{0,1000})?\n){0,20}[ \t]*(?:(?:public|private|protected|internal|open|override|suspend|static|final)\s+)*(?:void\s+|fun\s+)?`?([^`(\s][^`(\n]{0,199}?)`?[ \t]{0,40}\(/g },
102
104
  { re: /\bvoid\s+(test\w+)\s*\(/g },
103
105
  ],
104
- cs: [{ re: /\[\s*(?:Fact|Theory|Test|TestMethod|TestCase|TestCaseSource|SkippableFact)\b[^\]]*\]\s*(?:\[[^\]]*\]\s*)*(?:(?:public|private|protected|internal|async|static|virtual|override)\s+)*[\w<>[\],.?]+\s+(\w+)\s*\(/g }],
105
- rb: [{ re: new RegExp(String.raw`^\s*(?:it|specify|scenario|example|test)\s*\(?\s*` + STR, 'gm'), pick: (m) => m[2] }, { re: /^\s*def\s+(test_\w+)/gm }],
106
+ cs: [{ re: /\[\s*(?:Fact|Theory|Test|TestMethod|TestCase|TestCaseSource|SkippableFact)\b[^\]]{0,1000}\]\s*(?:\[[^\]]{0,1000}\]\s*){0,20}(?:(?:public|private|protected|internal|async|static|virtual|override)\s+)*[\w<>[\],.?]+\s+(\w+)\s*\(/g }],
107
+ rb: [{ re: new RegExp(String.raw`^[ \t]*(?:it|specify|scenario|example|test)[ \t]*(?:\(\s*)?` + STR, 'gm'), pick: (m) => m[2] }, { re: /^[ \t]*def\s+(test_\w+)/gm }],
106
108
  php: [{ re: /function\s+(test\w+)\s*\(/g }, { re: /(?:#\[Test\]|@test\b)[^]{0,200}?function\s+(\w+)\s*\(/g }, { re: new RegExp(String.raw`\b(?:it|test)\s*\(\s*` + STR, 'g'), pick: (m) => m[2] }],
107
- swift: [{ re: /func\s+(test\w+)\s*\(/g }, { re: /@Test\b(?:\([^)]*\))?\s*(?:@\w+(?:\([^)]*\))?\s*)*func\s+(\w+)\s*\(/g }],
109
+ swift: [{ re: /func\s+(test\w+)\s*\(/g }, { re: /@Test\b(?:\([^)]{0,1000}\))?\s*(?:@\w+(?:\([^)]{0,1000}\))?\s*){0,20}func\s+(\w+)\s*\(/g }],
108
110
  dart: [{ re: new RegExp(String.raw`\b(?:test|testWidgets)\s*\(\s*` + STR, 'g'), pick: (m) => m[2] }],
109
111
  c: [{ re: /\b(?:TEST|TEST_F|TEST_P|TYPED_TEST)\s*\(\s*(\w+)\s*,\s*(\w+)\s*\)/g, pick: (m) => `${m[1]}.${m[2]}` }, { re: /\bTEST_CASE\s*\(\s*"((?:\\.|[^"\\])*)"/g }],
110
112
  ex: [{ re: /\btest\s+"((?:\\.|[^"\\])*)"/g }],
@@ -427,11 +429,11 @@ function parseDiff(diff) {
427
429
  continue;
428
430
  }
429
431
  if (line.startsWith('--- ') && !h) {
430
- from = line.slice(4).replace(/^a\//, '');
432
+ from = unquotePath(line.slice(4)).replace(/^a\//, '');
431
433
  continue;
432
434
  }
433
435
  if (line.startsWith('+++ ') && !h) {
434
- const to = line.slice(4).replace(/^b\//, '');
436
+ const to = unquotePath(line.slice(4)).replace(/^b\//, '');
435
437
  f = { path: to === '/dev/null' ? from : to, status: from === '/dev/null' ? 'A' : to === '/dev/null' ? 'D' : 'M', hunks: [], removed: [], added: [] };
436
438
  files.push(f);
437
439
  continue;
@@ -531,6 +533,9 @@ export function testChanges(diff, { read, isTestFile }) {
531
533
  if (removed.length) removedIn.add(file.path);
532
534
  continue;
533
535
  }
536
+ // only a file that lost tests needs the added ones; building them for every file is quadratic in new files
537
+ // (cold review, L13 round 7)
538
+ if (!removed.length) continue;
534
539
  const own = addedIn((x) => x === file);
535
540
  const elsewhere = addedIn((x) => x !== file);
536
541
  for (const r of removed) {
package/src/signals.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // Deterministic signals. No AI in the decision path.
2
- import { testChanges } from './noskip.js';
2
+ import { testChanges, languageOf } from './noskip.js';
3
+ import { unquotePath, hunkReader } from './gitpath.js';
3
4
 
4
5
  const ERROR_LINE =
5
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;
@@ -132,7 +133,14 @@ export function similarity(a, b) {
132
133
 
133
134
  // ---------- test tampering ----------
134
135
 
135
- const TEST_FILE = /(^|\/)(test|tests|__tests__|spec)\/|\.(test|spec)\.[a-z]+$|_test\.(go|py)$|(^|\/)test_[^/]+\.py$/i;
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.
136
144
  // Data the tests read, not tests: deleting or editing it is not counted as weakening a test.
137
145
  const FIXTURE = /(^|\/)(__fixtures__|fixtures?|__snapshots__|__mocks__|testdata)\//i;
138
146
  export const isTestFile = (file) => TEST_FILE.test(file) && !FIXTURE.test(file);
@@ -173,9 +181,9 @@ const TAMPER_ADDED_IN_TESTS = [
173
181
  { re: new RegExp(String.raw`\bassert\.\w+\(\s*${LITERAL}\s*,\s*\1\s*\)`), kind: WEAKENED, why: 'an assertion that cannot fail was added' },
174
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' },
175
183
  ];
176
- // assert(...), assert.equal(...), expect(...), self.assertEqual(...), and pytest's bare `assert x`;
177
- // not expect.objectContaining(...) and the other matchers inside one assertion
178
- 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/;
179
187
  // package.json's test script made to pass whatever the tests do: `|| true`, `|| :`, `|| echo`, `; exit 0`
180
188
  const ALWAYS_PASSES = /"test"\s*:\s*"[^"]*(\|\|\s*(true|:|exit\s+0|echo\b[^"&|;]*)|;\s*(true|exit\s+0))\s*"/;
181
189
  const COMMENT = /^[+-]\s*(\/\/|#|\/\*|\*)\s*/;
@@ -183,7 +191,7 @@ const COMMENT = /^[+-]\s*(\/\/|#|\/\*|\*)\s*/;
183
191
  // Options that make the runner leave tests out (node --test, jest, vitest, mocha, playwright, pytest,
184
192
  // go, cargo), on a test script or a pytest addopts line. Reporter, coverage and other options are not.
185
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;
186
- const TEST_SCRIPT = /"test(?::[^"]*)?"\s*:\s*"((?:[^"\\]|\\.)*)"/;
194
+ const TEST_SCRIPT = /"(test(?::[^"]*)?)"\s*:\s*"((?:[^"\\]|\\.)*)"/;
187
195
  const ADDOPTS = /^[+-]\s*"?addopts"?\s*=(.*)$/;
188
196
  const PYTEST_CONFIG = /(^|\/)(pytest\.ini|pyproject\.toml|setup\.cfg|tox\.ini)$/;
189
197
  // The same, as keys in a runner's own config file
@@ -195,31 +203,70 @@ export const isTestConfig = (file) => /(^|\/)package\.json$/.test(file) || PYTES
195
203
  function filtersOn(file, line) {
196
204
  let text = null;
197
205
  let where = '';
198
- if (/(^|\/)package\.json$/.test(file)) (text = (TEST_SCRIPT.exec(line) || [])[1] ?? null), (where = 'by the test script');
199
- else if (PYTEST_CONFIG.test(file)) (text = (ADDOPTS.exec(line) || [])[1] ?? null), (where = "by the test runner's options");
200
- else if (RUNNER_CONFIG.test(file)) (text = line.slice(1)), (where = "by the test runner's config");
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);
201
219
  if (text == null) return [];
202
- const re = RUNNER_CONFIG.test(file) ? CONFIG_KEY : FILTER;
203
- return [...text.matchAll(re)].map((m) => ({ option: m[1], where }));
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;
204
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');
205
229
 
206
230
  // Which characters of a line are inside a string literal: test code often writes another test as
207
231
  // text, and a skip in that text is not a skip. One line at a time, so a string that spans lines (a
208
- // template literal, a docstring) is not seen; after // or a # comment, no quote opens a string.
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.
209
235
  function stringMask(line) {
210
236
  const mask = new Uint8Array(line.length);
211
237
  let quote = '';
238
+ let last = ''; // the last character outside strings and comments that is not a space
212
239
  for (let i = 0; i < line.length; i++) {
213
240
  const c = line[i];
214
241
  if (quote) {
215
242
  mask[i] = 1;
216
243
  if (c === '\\') mask[++i] = 1;
217
- else if (c === quote) quote = '';
218
- } else if (c === '"' || c === "'" || c === '`') (quote = c), (mask[i] = 1);
219
- else if ((c === '/' && line[i + 1] === '/') || (c === '#' && (i <= 1 || /\s/.test(line[i - 1])))) break;
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];
220
265
  }
266
+ mask.open = quote;
221
267
  return mask;
222
268
  }
269
+ const REGEX_AFTER = /(?:^|[^\w$.])(?:return|typeof|void|delete|in|of|instanceof|new|throw|case|do|else|yield|await)\s*$/;
223
270
 
224
271
  // An assertion line with its values taken out. A removed and an added line with the same shape and other
225
272
  // values are one assertion whose expected value changed; assertTrue and assertFalse share a shape.
@@ -352,6 +399,24 @@ const LITERAL_RE = /(?<![\w.$])-?\d[\d_]*(?:\.\d+)?(?![\w$])|'((?:[^'\\]|\\.)*)'
352
399
  function literals(text) {
353
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, ''))}`));
354
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
+ }
355
420
  // Values too common to say anything when code returns them
356
421
  const DULL = new Set(['n:0', 'n:1', 'n:-1', 'n:2', 's:']);
357
422
 
@@ -375,9 +440,35 @@ const RUNNER_MARKER = /\b(NODE_TEST_CONTEXT|JEST_WORKER_ID|VITEST_WORKER_ID|VITE
375
440
  const TEST_SETUP = /(^|\/)(conftest\.py|setupTests\.[cm]?[jt]sx?|(jest|vitest)\.setup\.[cm]?[jt]sx?)$/;
376
441
 
377
442
  // CI files, and the commands in them that run the tests
378
- const CI_FILE = /(^|\/)\.github\/workflows\/[^/]+\.ya?ml$|(^|\/)\.gitlab-ci\.ya?ml$|(^|\/)\.circleci\/config\.ya?ml$|(^|\/)(azure|bitbucket)-pipelines\.ya?ml$/;
379
- const TEST_CMD = /\b(?:(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?test\b|node\s+--test\b|(?:npx\s+)?(?:jest|vitest|mocha)\b|(?:python3?\s+-m\s+)?pytest\b|python3?\s+-m\s+unittest\b|go\s+test\b|cargo\s+test\b|make\s+test\b)/;
380
- 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*$)`);
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*$)`);
381
472
  const CI_IGNORE = /^\s*(continue-on-error|allow_failure)\s*:\s*true\b/;
382
473
 
383
474
  // Whether the YAML block a key at line `at` belongs to (its step or job) runs the tests.
@@ -401,10 +492,16 @@ function blockRunsTests(lines, at, text) {
401
492
  return false;
402
493
  }
403
494
 
404
- // re found in the code of the line, not only inside its strings
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*$/;
405
500
  function inCode(re, line, mask) {
406
501
  const g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g');
407
- for (const m of line.matchAll(g)) if (!mask()[m.index + (m[0].length - m[0].trimStart().length)]) return true;
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;
408
505
  return false;
409
506
  }
410
507
 
@@ -418,8 +515,12 @@ export function tamperFindings(nameStatus, diff, opts) {
418
515
  // read(file), when given, returns the file as it is now (or null): the finish check passes one, the audit
419
516
  // does not and relies on the diff's context lines. tests, when given, is the repo's test files as
420
517
  // "path\0content" (or a function returning them, called only when it is needed), for the expected values a
421
- // hardcoded answer would copy.
422
- export function tamperEvents(nameStatus, diff, { read, tests } = {}) {
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);
423
524
  const events = [];
424
525
  const seen = new Set();
425
526
  const add = (kind, file, why) => {
@@ -427,10 +528,131 @@ export function tamperEvents(nameStatus, diff, { read, tests } = {}) {
427
528
  seen.add(why);
428
529
  events.push({ kind, file, why });
429
530
  };
430
- for (const line of nameStatus.split('\n')) {
431
- const [status, file] = line.split(/\s+/);
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) {
432
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}`);
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
+ }
433
654
  }
655
+ let oldPath = ''; // the file a "--- a/" header names: a deleted CI file's lines are read under its own name (round 10)
434
656
  let current = '';
435
657
  let removedAsserts = 0;
436
658
  let addedAsserts = 0;
@@ -448,10 +670,13 @@ export function tamperEvents(nameStatus, diff, { read, tests } = {}) {
448
670
  let ciAdded = 0; // lines running the tests added to any CI file
449
671
  const weakenedIn = new Set(); // one weakened-matcher finding per file
450
672
  let hadFallback = false; // the test script already ended in `|| true` before this session
451
- let hadFilters = new Set(); // options that left tests out before this session
673
+ let hadFilters = new Map(); // options that left tests out before this session, with their values
674
+ const inHunk = hunkReader();
452
675
  for (const line of diff.split('\n')) {
453
- if (line.startsWith('+++ ')) {
454
- 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;
455
680
  removed = new Set();
456
681
  removedShapes = new Map();
457
682
  removedValueChecks = new Map();
@@ -459,10 +684,13 @@ export function tamperEvents(nameStatus, diff, { read, tests } = {}) {
459
684
  pendingCatch = 0;
460
685
  prevText = '';
461
686
  hadFallback = false;
462
- hadFilters = new Set();
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\//, '');
463
692
  continue;
464
693
  }
465
- if (line.startsWith('--- ')) continue;
466
694
  if (line.startsWith('@@')) {
467
695
  newLine = Number((/\+(\d+)/.exec(line) || [])[1] || 0);
468
696
  continue;
@@ -471,7 +699,7 @@ export function tamperEvents(nameStatus, diff, { read, tests } = {}) {
471
699
  if (!line.startsWith('-')) newLine++;
472
700
  if (at && at <= 20000) fileLines[at - 1] = line.slice(1);
473
701
  const inTest = isTestFile(current);
474
- if (CI_FILE.test(current) && !COMMENT.test(line)) {
702
+ if (isCi(current) && !COMMENT.test(line)) {
475
703
  const text = line.slice(1, 10001);
476
704
  if (line.startsWith('-') && TEST_CMD.test(text)) ciRemoved.push(current);
477
705
  if (line.startsWith('+') && TEST_CMD.test(text)) ciAdded++;
@@ -493,7 +721,7 @@ export function tamperEvents(nameStatus, diff, { read, tests } = {}) {
493
721
  if (r) {
494
722
  const same = SAME_LINE_IF.exec(line.slice(1, 10001));
495
723
  const cond = same ? same[1] : /^\s*if\b/.test(before) ? before : null;
496
- if (cond != null) returns.push({ file: current, text: r[1], value: literals(r[1])[0], given: literals(cond) });
724
+ if (cond != null) returns.push({ file: current, text: r[1], value: literals(r[1])[0], given: comparedLiterals(cond) });
497
725
  }
498
726
  }
499
727
  const probe = line.slice(0, 10000); // the patterns can take quadratic time on a long line
@@ -503,14 +731,33 @@ export function tamperEvents(nameStatus, diff, { read, tests } = {}) {
503
731
  if (line.startsWith('-')) hadFallback = true;
504
732
  else if (!hadFallback) add(CHECKS_OFF, current, `the test script was changed to always exit 0 in ${current}`);
505
733
  }
506
- if (isTestConfig(current)) {
507
- for (const f of filtersOn(current, probe)) {
508
- if (line.startsWith('-')) hadFilters.add(f.option);
509
- else if (line.startsWith('+') && !hadFilters.has(f.option)) add(SKIPPED, current, `tests were left out ${f.where} (${f.option}) in ${current}`);
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
+ }
510
748
  }
511
749
  }
512
750
  if (line.startsWith('+')) {
513
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
+ }
514
761
  if (!inTest) continue;
515
762
  const text = probe.slice(1);
516
763
  let swallowAt = 0;
@@ -636,8 +883,14 @@ const counts = (passed, failed) => (passed == null && failed == null ? null : {
636
883
  const SUMMARIES = [
637
884
  // node --test, spec and TAP reporters: "ℹ tests 3", "ℹ pass 2", "ℹ fail 1"
638
885
  { re: /^(?:ℹ|#) tests \d+$/gm, read: (t) => counts(num(t, /^(?:ℹ|#) pass (\d+)$/m), num(t, /^(?:ℹ|#) fail (\d+)$/m)) },
639
- // jest "Tests: 2 failed, 46 passed, 48 total", vitest " Tests 2 failed | 46 passed (48)"
640
- { 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) },
641
894
  // pytest "===== 1 failed, 1 passed in 0.02s ====="
642
895
  {
643
896
  re: /^[=\s]*(?:\d+ \w+(?:, )?)+ in [\d.]+s\b.*$/gm,