fix: harden session hook guards and session ID handling

This commit is contained in:
Affaan Mustafa
2026-03-25 03:36:36 -04:00
parent 00bc7f30be
commit 7b510c886e
8 changed files with 96 additions and 36 deletions

View File

@@ -477,6 +477,12 @@ src/main.ts
assert.strictEqual(result, null, 'Empty string should not match any session');
})) passed++; else failed++;
if (test('getSessionById returns null for non-string IDs', () => {
assert.strictEqual(sessionManager.getSessionById(null), null);
assert.strictEqual(sessionManager.getSessionById(undefined), null);
assert.strictEqual(sessionManager.getSessionById(42), null);
})) passed++; else failed++;
if (test('getSessionById metadata and stats populated when includeContent=true', () => {
const result = sessionManager.getSessionById('abcd1234', true);
assert.ok(result, 'Should find session');
@@ -1601,18 +1607,13 @@ src/main.ts
'Null search should return sessions (confirming they exist but space filtered them)');
})) passed++; else failed++;
// ── Round 98: getSessionById with null sessionId throws TypeError ──
console.log('\nRound 98: getSessionById (null sessionId — crashes at line 297):');
// ── Round 98: getSessionById with null sessionId returns null ──
console.log('\nRound 98: getSessionById (null sessionId — guarded null return):');
if (test('getSessionById(null) throws TypeError when session files exist', () => {
// session-manager.js line 297: `sessionId.length > 0` — calling .length on null
// throws TypeError because there's no early guard for null/undefined input.
// This only surfaces when valid .tmp files exist in the sessions directory.
assert.throws(
() => sessionManager.getSessionById(null),
{ name: 'TypeError' },
'null.length should throw TypeError (no input guard at function entry)'
);
if (test('getSessionById(null) returns null when session files exist', () => {
// Keep a populated sessions directory so the early input guard is exercised even when
// candidate files are present.
assert.strictEqual(sessionManager.getSessionById(null), null);
})) passed++; else failed++;
// Cleanup test environment for Rounds 95-98 that needed sessions

View File

@@ -74,8 +74,8 @@ function runTests() {
if (test('getSessionSearchDirs includes canonical and legacy paths', () => {
const searchDirs = utils.getSessionSearchDirs();
assert.ok(searchDirs.includes(utils.getSessionsDir()), 'Should include canonical session dir');
assert.ok(searchDirs.includes(utils.getLegacySessionsDir()), 'Should include legacy session dir');
assert.strictEqual(searchDirs[0], utils.getSessionsDir(), 'Canonical session dir should be searched first');
assert.strictEqual(searchDirs[1], utils.getLegacySessionsDir(), 'Legacy session dir should be searched second');
})) passed++; else failed++;
if (test('getTempDir returns valid temp directory', () => {
@@ -184,6 +184,14 @@ function runTests() {
}
})) passed++; else failed++;
if (test('sanitizeSessionId avoids Windows reserved device names', () => {
const con = utils.sanitizeSessionId('CON');
const aux = utils.sanitizeSessionId('aux');
assert.ok(con.startsWith('CON-'), `Expected CON to get a suffix, got: ${con}`);
assert.ok(aux.startsWith('aux-'), `Expected aux to get a suffix, got: ${aux}`);
assert.notStrictEqual(utils.sanitizeSessionId('COM1'), 'COM1');
})) passed++; else failed++;
// Session ID tests
console.log('\nSession ID Functions:');