fix(delegation): handle undefined totalCost in timeout error formatting

Add defensive checks in result-formatter, headless-executor, and session-manager
to prevent TypeError when delegated sessions timeout without emitting a result.
Includes 4 comprehensive unit tests for undefined/null totalCost scenarios.

Fixes formatting error that prevented timeout messages from displaying.
This commit is contained in:
kaitranntt committed 2025-11-16 06:40:31 -05:00
1 parent 4df5a7d357
commit fb3deeff38
4 files changed
+87 -3

No files matched your search

@@ -284,5 +284,85 @@ runner.test('Should show file counts in info box', () => {
assertIncludes(formatted, 'Files Modified: 1', 'Should show modified count');
});
// Test 15: Handle undefined totalCost in timeout error
runner.test('Should handle undefined totalCost in timeout error', () => {
const result = {
profile: 'glm',
cwd: '/test',
duration: 120000,
sessionId: 'test-session-123',
totalCost: undefined,
numTurns: 5,
timedOut: true
};
// Should not throw TypeError
const formatted = ResultFormatter.format(result);
assertIncludes(formatted, 'Execution timed out', 'Should show timeout message');
assertIncludes(formatted, 'test-ses', 'Should show abbreviated session ID');
// Cost line should be omitted when undefined
assert(!formatted.includes('Cost: $'), 'Should not show cost when undefined');
});
// Test 16: Handle null totalCost in timeout error
runner.test('Should handle null totalCost in timeout error', () => {
const result = {
profile: 'kimi',
cwd: '/test',
duration: 60000,
sessionId: 'test-session-456',
totalCost: null,
numTurns: 3,
timedOut: true
};
// Should not throw TypeError
const formatted = ResultFormatter.format(result);
assertIncludes(formatted, 'Execution timed out', 'Should show timeout message');
assert(!formatted.includes('Cost: $'), 'Should not show cost when null');
});
// Test 17: Show totalCost when defined in timeout error
runner.test('Should show totalCost when defined in timeout error', () => {
const result = {
profile: 'glm',
cwd: '/test',
duration: 90000,
sessionId: 'test-session-789',
totalCost: 0.1234,
numTurns: 4,
timedOut: true
};
const formatted = ResultFormatter.format(result);
assertIncludes(formatted, 'Cost: $0.1234', 'Should show formatted cost');
});
// Test 18: Handle undefined totalCost in normal result
runner.test('Should handle undefined totalCost in normal result', () => {
const result = {
profile: 'kimi',
cwd: '/test',
exitCode: 0,
stdout: 'Task completed',
stderr: '',
duration: 5000,
success: true,
sessionId: 'session-abc',
totalCost: undefined,
numTurns: 2
};
// Should not throw TypeError
const formatted = ResultFormatter.format(result);
assertIncludes(formatted, '[OK]', 'Should show success');
// Cost line should be omitted in info box when undefined
assert(!formatted.includes('Cost: $'), 'Should not show cost when undefined');
});
// Run all tests
runner.run();