fix: grepFile global regex lastIndex bug, add 12 tests

Fix grepFile() silently skipping matches when called with /g flag regex.
The global flag makes .test() stateful, causing alternating match/miss
on consecutive matching lines. Strip g flag since per-line testing
doesn't need global state.

Add first-ever tests for evaluate-session.js (5 tests: short session,
long session, missing transcript, malformed stdin, env var fallback)
and suggest-compact.js (5 tests: counter increment, threshold trigger,
periodic suggestions, below-threshold silence, invalid threshold).
This commit is contained in:
Affaan Mustafa
2026-02-13 01:18:07 -08:00
parent 02120fbf5f
commit 6f95dbe7ba
3 changed files with 192 additions and 1 deletions

View File

@@ -684,6 +684,37 @@ function runTests() {
assert.deepStrictEqual(parsed, { a: { b: 1 }, c: [1, 2] });
})) passed++; else failed++;
// grepFile with global regex (regression: g flag causes alternating matches)
console.log('\ngrepFile (global regex fix):');
if (test('grepFile with /g flag finds ALL matching lines (not alternating)', () => {
const testFile = path.join(utils.getTempDir(), `utils-test-grep-g-${Date.now()}.txt`);
try {
// 4 consecutive lines matching the same pattern
utils.writeFile(testFile, 'match-line\nmatch-line\nmatch-line\nmatch-line');
// Bug: without fix, /match/g would only find lines 1 and 3 (alternating)
const matches = utils.grepFile(testFile, /match/g);
assert.strictEqual(matches.length, 4, `Should find all 4 lines, found ${matches.length}`);
assert.strictEqual(matches[0].lineNumber, 1);
assert.strictEqual(matches[1].lineNumber, 2);
assert.strictEqual(matches[2].lineNumber, 3);
assert.strictEqual(matches[3].lineNumber, 4);
} finally {
fs.unlinkSync(testFile);
}
})) passed++; else failed++;
if (test('grepFile preserves regex flags other than g (e.g. case-insensitive)', () => {
const testFile = path.join(utils.getTempDir(), `utils-test-grep-flags-${Date.now()}.txt`);
try {
utils.writeFile(testFile, 'FOO\nfoo\nFoO\nbar');
const matches = utils.grepFile(testFile, /foo/gi);
assert.strictEqual(matches.length, 3, `Should find 3 case-insensitive matches, found ${matches.length}`);
} finally {
fs.unlinkSync(testFile);
}
})) passed++; else failed++;
// Summary
console.log('\n=== Test Results ===');
console.log(`Passed: ${passed}`);