test: cover session-end message.role path, getExecCommand non-string args, and legacy hooks format

Round 80: Three previously untested conditional branches:
- session-end.js: entry.message?.role === 'user' third OR condition
  (fires when type is not 'user' but message.role is)
- package-manager.js: getExecCommand with truthy non-string args
  (typeof check short-circuits, value still appended via ternary)
- validate-hooks.js: legacy array format parsing path (lines 115-135)
  with 'Hook N' error labels instead of 'EventType[N]'
This commit is contained in:
Affaan Mustafa
2026-02-13 10:39:35 -08:00
parent 9dccdb9068
commit 2573cbb7b0
3 changed files with 83 additions and 0 deletions

View File

@@ -2053,6 +2053,31 @@ function runTests() {
cleanupTestDir(testDir); cleanupTestDir(agentsDir); cleanupTestDir(skillsDir);
})) passed++; else failed++;
// ── Round 80: validate-hooks.js legacy array format (lines 115-135) ──
console.log('\nRound 80: validate-hooks.js (legacy array format):');
if (test('validates hooks in legacy array format (hooks is an array, not object)', () => {
const testDir = createTestDir();
// The legacy array format wraps hooks as { hooks: [...] } where the array
// contains matcher objects directly. This exercises lines 115-135 of
// validate-hooks.js which use "Hook ${i}" error labels instead of "${eventType}[${i}]".
const hooksJson = JSON.stringify({
hooks: [
{
matcher: 'Edit',
hooks: [{ type: 'command', command: 'echo legacy test' }]
}
]
});
fs.writeFileSync(path.join(testDir, 'hooks.json'), hooksJson);
const result = runValidatorWithDir('validate-hooks', 'HOOKS_FILE', path.join(testDir, 'hooks.json'));
assert.strictEqual(result.code, 0, 'Should pass on valid legacy array format');
assert.ok(result.stdout.includes('Validated 1 hook'),
`Should report 1 validated matcher, got: ${result.stdout}`);
cleanupTestDir(testDir);
})) passed++; else failed++;
// Summary
console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`);
process.exit(failed > 0 ? 1 : 0);

View File

@@ -3252,6 +3252,45 @@ async function runTests() {
`stderr should contain [StrategicCompact] Error:, got: ${result.stderr}`);
})) passed++; else failed++;
// ── Round 80: session-end.js entry.message?.role === 'user' third OR condition ──
console.log('\nRound 80: session-end.js (entry.message.role user — third OR condition):');
if (await asyncTest('extracts user messages from entries where only message.role is user (not type or role)', async () => {
const isoHome = path.join(os.tmpdir(), `ecc-msgrole-${Date.now()}`);
const sessionsDir = path.join(isoHome, '.claude', 'sessions');
fs.mkdirSync(sessionsDir, { recursive: true });
const testDir = createTestDir();
const transcriptPath = path.join(testDir, 'transcript.jsonl');
// Entries where type is NOT 'user' and there is no direct role field,
// but message.role IS 'user'. This exercises the third OR condition at
// session-end.js line 48: entry.message?.role === 'user'
const lines = [
'{"type":"human","message":{"role":"user","content":"Refactor the auth module"}}',
'{"type":"human","message":{"role":"assistant","content":"I will refactor it"}}',
'{"type":"human","message":{"role":"user","content":"Add integration tests too"}}',
];
fs.writeFileSync(transcriptPath, lines.join('\n'));
const stdinJson = JSON.stringify({ transcript_path: transcriptPath });
try {
const result = await runScript(path.join(scriptsDir, 'session-end.js'), stdinJson, {
HOME: isoHome, USERPROFILE: isoHome
});
assert.strictEqual(result.code, 0);
const files = fs.readdirSync(sessionsDir).filter(f => f.endsWith('-session.tmp'));
assert.ok(files.length > 0, 'Should create session file');
const content = fs.readFileSync(path.join(sessionsDir, files[0]), 'utf8');
// The third OR condition should fire for type:"human" + message.role:"user"
assert.ok(content.includes('Refactor the auth module') || content.includes('auth'),
`Session should include message extracted via message.role path. Got: ${content.substring(0, 300)}`);
} finally {
fs.rmSync(isoHome, { recursive: true, force: true });
cleanupTestDir(testDir);
}
})) passed++; else failed++;
// Summary
console.log('\n=== Test Results ===');
console.log(`Passed: ${passed}`);

View File

@@ -1331,6 +1331,25 @@ function runTests() {
}
})) passed++; else failed++;
// ── Round 80: getExecCommand with truthy non-string args ──
console.log('\nRound 80: getExecCommand (truthy non-string args):');
if (test('getExecCommand with args=42 (truthy number) appends stringified value', () => {
const originalEnv = process.env.CLAUDE_PACKAGE_MANAGER;
try {
process.env.CLAUDE_PACKAGE_MANAGER = 'npm';
// args=42: truthy, so typeof check at line 334 short-circuits
// (typeof 42 !== 'string'), skipping validation. Line 339:
// 42 ? ' ' + 42 → ' 42' → appended.
const cmd = pm.getExecCommand('prettier', 42);
assert.ok(cmd.includes('prettier'), 'Should include binary name');
assert.ok(cmd.includes('42'), 'Truthy number should be stringified and appended');
} finally {
if (originalEnv !== undefined) process.env.CLAUDE_PACKAGE_MANAGER = originalEnv;
else delete process.env.CLAUDE_PACKAGE_MANAGER;
}
})) passed++; else failed++;
// Summary
console.log('\n=== Test Results ===');
console.log(`Passed: ${passed}`);