fix: use nullish coalescing for confidence default + add 3 tests (round 85)

Fix confidence=0 showing 80% instead of 0% in patterns() (|| → ??).
Test evaluate-session.js config parse error catch, getSessionIdShort
fallback at root CWD, and precise confidence=0 assertion.
This commit is contained in:
Affaan Mustafa
2026-02-13 12:11:26 -08:00
parent cedcf9a701
commit 8cacf0f6a6
4 changed files with 95 additions and 1 deletions

View File

@@ -125,7 +125,7 @@ ${chalk.bold('Files Tracked:')} ${chalk.green(data.files)}
console.log(chalk.gray('─'.repeat(50)));
patterns.forEach((pattern, i) => {
const confidence = pattern.confidence || 0.8;
const confidence = pattern.confidence ?? 0.8;
const confidenceBar = progressBar(Math.round(confidence * 100), 15);
console.log(`
${chalk.bold(chalk.yellow(`${i + 1}.`))} ${chalk.bold(pattern.name)}

View File

@@ -311,6 +311,55 @@ function runTests() {
cleanupTestDir(testDir);
})) passed++; else failed++;
// ── Round 85: config file parse error (corrupt JSON) ──
console.log('\nRound 85: config parse error catch block:');
if (test('falls back to defaults when config file contains invalid JSON', () => {
// The evaluate-session.js script reads config from:
// path.join(__dirname, '..', '..', 'skills', 'continuous-learning', 'config.json')
// where __dirname = scripts/hooks/ → config = repo_root/skills/continuous-learning/config.json
const configPath = path.join(__dirname, '..', '..', 'skills', 'continuous-learning', 'config.json');
let originalContent = null;
try {
originalContent = fs.readFileSync(configPath, 'utf8');
} catch {
// Config file may not exist — that's fine
}
try {
// Write corrupt JSON to the config file
fs.writeFileSync(configPath, 'NOT VALID JSON {{{ corrupt data !!!', 'utf8');
// Create a transcript with 12 user messages (above default threshold of 10)
const testDir = createTestDir();
const transcript = createTranscript(testDir, 12);
const result = runEvaluate({ transcript_path: transcript });
assert.strictEqual(result.code, 0, 'Should exit 0 despite corrupt config');
// With corrupt config, defaults apply: min_session_length = 10
// 12 >= 10 → should evaluate (not "too short")
assert.ok(!result.stderr.includes('too short'),
`Should NOT say too short — corrupt config falls back to default min=10. Got: ${result.stderr}`);
assert.ok(
result.stderr.includes('12 messages') || result.stderr.includes('evaluate'),
`Should evaluate with 12 messages using default threshold. Got: ${result.stderr}`
);
// The catch block logs "Failed to parse config" — verify that log message
assert.ok(result.stderr.includes('Failed to parse config'),
`Should log config parse error. Got: ${result.stderr}`);
cleanupTestDir(testDir);
} finally {
// Restore original config file
if (originalContent !== null) {
fs.writeFileSync(configPath, originalContent, 'utf8');
} else {
// Config didn't exist before — remove the corrupt one we created
try { fs.unlinkSync(configPath); } catch { /* best-effort */ }
}
}
})) passed++; else failed++;
// Summary
console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`);
process.exit(failed > 0 ? 1 : 0);

View File

@@ -1165,6 +1165,35 @@ function runTests() {
}
})) passed++; else failed++;
// ── Round 85: getSessionIdShort fallback parameter ──
console.log('\ngetSessionIdShort fallback (Round 85):');
if (test('getSessionIdShort uses fallback when getProjectName returns null (CWD at root)', () => {
if (process.platform === 'win32') {
console.log(' (skipped — root CWD differs on Windows)');
return;
}
// Spawn a subprocess at CWD=/ with CLAUDE_SESSION_ID empty.
// At /, git rev-parse --show-toplevel fails → getGitRepoName() = null.
// path.basename('/') = '' → '' || null = null → getProjectName() = null.
// So getSessionIdShort('my-custom-fallback') = null || 'my-custom-fallback'.
const utilsPath = path.join(__dirname, '..', '..', 'scripts', 'lib', 'utils.js');
const script = `
const utils = require('${utilsPath.replace(/'/g, "\\'")}');
process.stdout.write(utils.getSessionIdShort('my-custom-fallback'));
`;
const { spawnSync } = require('child_process');
const result = spawnSync('node', ['-e', script], {
encoding: 'utf8',
cwd: '/',
env: { ...process.env, CLAUDE_SESSION_ID: '' },
timeout: 10000
});
assert.strictEqual(result.status, 0, `Should exit 0, got status ${result.status}. stderr: ${result.stderr}`);
assert.strictEqual(result.stdout, 'my-custom-fallback',
`At CWD=/ with no session ID, should use the fallback parameter. Got: "${result.stdout}"`);
})) passed++; else failed++;
// Summary
console.log('\n=== Test Results ===');
console.log(`Passed: ${passed}`);

View File

@@ -481,6 +481,22 @@ function runTests() {
assert.strictEqual(typeof mod.SkillCreateOutput, 'function', 'SkillCreateOutput should be a constructor');
})) passed++; else failed++;
// ── Round 85: patterns() confidence=0 uses ?? (not ||) ──
console.log('\nRound 85: patterns() confidence=0 nullish coalescing:');
if (test('patterns() with confidence=0 shows 0%, not 80% (nullish coalescing fix)', () => {
const output = new SkillCreateOutput('repo');
const logs = captureLog(() => output.patterns([
{ name: 'Zero Confidence', trigger: 'never', confidence: 0, evidence: 'none' },
]));
const combined = stripAnsi(logs.join('\n'));
// With ?? operator: 0 ?? 0.8 = 0 → Math.round(0 * 100) = 0 → shows "0%"
// With || operator (bug): 0 || 0.8 = 0.8 → shows "80%"
assert.ok(combined.includes('0%'), 'Should show 0% for zero confidence');
assert.ok(!combined.includes('80%'),
'Should NOT show 80% — confidence=0 is explicitly provided, not missing');
})) passed++; else failed++;
// Summary
console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`);
process.exit(failed > 0 ? 1 : 0);