Refactor console log formatting in tests

This commit is contained in:
Pangerkumzuk Longkumer
2026-02-18 07:21:58 +05:30
committed by GitHub
parent 8966282e48
commit 2ad888ca82

View File

@@ -19,11 +19,11 @@ const compactScript = path.join(__dirname, '..', '..', 'scripts', 'hooks', 'sugg
function test(name, fn) {
try {
fn();
console.log(` \u2713 ${name}`);
console.log(` \u2713 ${name}`);
return true;
} catch (err) {
console.log(` \u2717 ${name}`);
console.log(` Error: ${err.message}`);
console.log(` \u2717 ${name}`);
console.log(` Error: ${err.message}`);
return false;
}
}
@@ -55,7 +55,9 @@ function getCounterFilePath(sessionId) {
}
function runTests() {
console.log('\n=== Testing suggest-compact.js ===\n');
console.log('
=== Testing suggest-compact.js ===
');
let passed = 0;
let failed = 0;
@@ -66,7 +68,11 @@ function runTests() {
// Cleanup helper
function cleanupCounter() {
try { fs.unlinkSync(counterFile); } catch {}
try {
fs.unlinkSync(counterFile);
} catch (err) {
// Ignore error
}
}
// Basic functionality
@@ -80,7 +86,8 @@ function runTests() {
const count = parseInt(fs.readFileSync(counterFile, 'utf8').trim(), 10);
assert.strictEqual(count, 1, 'Counter should be 1 after first run');
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
if (test('increments counter on subsequent runs', () => {
cleanupCounter();
@@ -90,10 +97,12 @@ function runTests() {
const count = parseInt(fs.readFileSync(counterFile, 'utf8').trim(), 10);
assert.strictEqual(count, 3, 'Counter should be 3 after three runs');
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
// Threshold suggestion
console.log('\nThreshold suggestion:');
console.log('
Threshold suggestion:');
if (test('suggests compact at threshold (COMPACT_THRESHOLD=3)', () => {
cleanupCounter();
@@ -106,7 +115,8 @@ function runTests() {
`Should suggest compact at threshold. Got stderr: ${result.stderr}`
);
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
if (test('does NOT suggest compact before threshold', () => {
cleanupCounter();
@@ -117,10 +127,12 @@ function runTests() {
'Should NOT suggest compact before threshold'
);
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
// Interval suggestion (every 25 calls after threshold)
console.log('\nInterval suggestion:');
console.log('
Interval suggestion:');
if (test('suggests at threshold + 25 interval', () => {
cleanupCounter();
@@ -135,10 +147,12 @@ function runTests() {
`Should suggest at threshold+25 interval. Got stderr: ${result.stderr}`
);
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
// Environment variable handling
console.log('\nEnvironment variable handling:');
console.log('
Environment variable handling:');
if (test('uses default threshold (50) when COMPACT_THRESHOLD is not set', () => {
cleanupCounter();
@@ -151,7 +165,8 @@ function runTests() {
`Should use default threshold of 50. Got stderr: ${result.stderr}`
);
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
if (test('ignores invalid COMPACT_THRESHOLD (negative)', () => {
cleanupCounter();
@@ -163,7 +178,8 @@ function runTests() {
`Should fallback to 50 for negative threshold. Got stderr: ${result.stderr}`
);
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
if (test('ignores non-numeric COMPACT_THRESHOLD', () => {
cleanupCounter();
@@ -175,10 +191,12 @@ function runTests() {
`Should fallback to 50 for non-numeric threshold. Got stderr: ${result.stderr}`
);
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
// Corrupted counter file
console.log('\nCorrupted counter file:');
console.log('
Corrupted counter file:');
if (test('resets counter on corrupted file content', () => {
cleanupCounter();
@@ -189,7 +207,8 @@ function runTests() {
const count = parseInt(fs.readFileSync(counterFile, 'utf8').trim(), 10);
assert.strictEqual(count, 1, 'Should reset to 1 on corrupted file');
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
if (test('resets counter on extremely large value', () => {
cleanupCounter();
@@ -200,7 +219,8 @@ function runTests() {
const count = parseInt(fs.readFileSync(counterFile, 'utf8').trim(), 10);
assert.strictEqual(count, 1, 'Should reset to 1 for value > 1000000');
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
if (test('handles empty counter file', () => {
cleanupCounter();
@@ -211,10 +231,12 @@ function runTests() {
const count = parseInt(fs.readFileSync(counterFile, 'utf8').trim(), 10);
assert.strictEqual(count, 1, 'Should start at 1 for empty file');
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
// Session isolation
console.log('\nSession isolation:');
console.log('
Session isolation:');
if (test('uses separate counter files per session ID', () => {
const sessionA = `compact-a-${Date.now()}`;
@@ -230,23 +252,27 @@ function runTests() {
assert.strictEqual(countA, 2, 'Session A should have count 2');
assert.strictEqual(countB, 1, 'Session B should have count 1');
} finally {
try { fs.unlinkSync(fileA); } catch {}
try { fs.unlinkSync(fileB); } catch {}
try { fs.unlinkSync(fileA); } catch (err) { /* ignore */ }
try { fs.unlinkSync(fileB); } catch (err) { /* ignore */ }
}
})) passed++; else failed++;
})) passed++;
else failed++;
// Always exits 0
console.log('\nExit code:');
console.log('
Exit code:');
if (test('always exits 0 (never blocks Claude)', () => {
cleanupCounter();
const result = runCompact({ CLAUDE_SESSION_ID: testSession });
assert.strictEqual(result.code, 0, 'Should always exit 0');
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
// ── Round 29: threshold boundary values ──
console.log('\nThreshold boundary values:');
console.log('
Threshold boundary values:');
if (test('rejects COMPACT_THRESHOLD=0 (falls back to 50)', () => {
cleanupCounter();
@@ -258,7 +284,8 @@ function runTests() {
`Should fallback to 50 for threshold=0. Got stderr: ${result.stderr}`
);
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
if (test('accepts COMPACT_THRESHOLD=10000 (boundary max)', () => {
cleanupCounter();
@@ -270,7 +297,8 @@ function runTests() {
`Should accept threshold=10000. Got stderr: ${result.stderr}`
);
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
if (test('rejects COMPACT_THRESHOLD=10001 (falls back to 50)', () => {
cleanupCounter();
@@ -282,7 +310,8 @@ function runTests() {
`Should fallback to 50 for threshold=10001. Got stderr: ${result.stderr}`
);
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
if (test('rejects float COMPACT_THRESHOLD (e.g. 3.5)', () => {
cleanupCounter();
@@ -297,7 +326,8 @@ function runTests() {
'Float threshold should be parseInt-ed to 3, no suggestion at count=50'
);
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
if (test('counter value at exact boundary 1000000 is valid', () => {
cleanupCounter();
@@ -307,7 +337,8 @@ function runTests() {
const count = parseInt(fs.readFileSync(counterFile, 'utf8').trim(), 10);
assert.strictEqual(count, 1000000, 'Counter at 1000000 boundary should be valid');
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
if (test('counter value at 1000001 is clamped (reset to 1)', () => {
cleanupCounter();
@@ -316,14 +347,16 @@ function runTests() {
const count = parseInt(fs.readFileSync(counterFile, 'utf8').trim(), 10);
assert.strictEqual(count, 1, 'Counter > 1000000 should be reset to 1');
cleanupCounter();
})) passed++; else failed++;
})) passed++;
else failed++;
// ── Round 64: default session ID fallback ──
console.log('\nDefault session ID fallback (Round 64):');
console.log('
Default session ID fallback (Round 64):');
if (test('uses "default" session ID when CLAUDE_SESSION_ID is empty', () => {
const defaultCounterFile = getCounterFilePath('default');
try { fs.unlinkSync(defaultCounterFile); } catch {}
try { fs.unlinkSync(defaultCounterFile); } catch (err) { /* ignore */ }
try {
// Pass empty CLAUDE_SESSION_ID — falsy, so script uses 'default'
const env = { ...process.env, CLAUDE_SESSION_ID: '' };
@@ -338,12 +371,14 @@ function runTests() {
const count = parseInt(fs.readFileSync(defaultCounterFile, 'utf8').trim(), 10);
assert.strictEqual(count, 1, 'Counter should be 1 for first run with default session');
} finally {
try { fs.unlinkSync(defaultCounterFile); } catch {}
try { fs.unlinkSync(defaultCounterFile); } catch (err) { /* ignore */ }
}
})) passed++; else failed++;
})) passed++;
else failed++;
// Summary
console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`);
console.log(`
Results: Passed: ${passed}, Failed: ${failed}`);
process.exit(failed > 0 ? 1 : 0);
}