fix: harden activity event handling

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
David Kaya
2026-03-21 18:18:06 +01:00
co-authored by Copilot
parent dd40ebbfca
commit 867f2932df
5 changed files with 136 additions and 15 deletions
+48
View File
@@ -0,0 +1,48 @@
import { describe, expect, test } from 'bun:test';
import {
markRunTurnPendingErrored,
shouldHandleRunTurnEvent,
type RunTurnPendingCommand,
} from '@main/sidecar/runTurnPending';
describe('run turn pending helpers', () => {
test('marks a run-turn pending command as errored and rejects it once', () => {
const rejected: Error[] = [];
const pending: RunTurnPendingCommand = {
kind: 'run-turn',
resolve: () => undefined,
reject: (error) => rejected.push(error),
onDelta: () => undefined,
onActivity: () => undefined,
errored: false,
};
const first = markRunTurnPendingErrored(pending, 'boom');
const second = markRunTurnPendingErrored(pending, new Error('later'));
expect(first).toBeInstanceOf(Error);
expect(first.message).toBe('boom');
expect(second.message).toBe('later');
expect(pending.errored).toBe(true);
expect(rejected).toHaveLength(1);
expect(rejected[0].message).toBe('boom');
});
test('stops handling turn events after the pending command has errored', () => {
const pending: RunTurnPendingCommand = {
kind: 'run-turn',
resolve: () => undefined,
reject: () => undefined,
onDelta: () => undefined,
onActivity: () => undefined,
errored: false,
};
expect(shouldHandleRunTurnEvent(pending)).toBe(true);
markRunTurnPendingErrored(pending, new Error('boom'));
expect(shouldHandleRunTurnEvent(pending)).toBe(false);
});
});
+36
View File
@@ -83,6 +83,42 @@ describe('session activity helpers', () => {
});
});
test('warns when an agent-activity event is missing identifiers', () => {
const originalWarn = console.warn;
const warnings: unknown[][] = [];
console.warn = (...args: unknown[]) => {
warnings.push(args);
};
try {
const current: SessionActivityMap = {
'session-1': {
architect: {
agentId: 'architect',
agentName: 'Architect',
activityType: 'thinking',
},
},
};
expect(
applySessionEventActivity(current, {
sessionId: 'session-1',
kind: 'agent-activity',
occurredAt: '2026-03-23T00:00:00.000Z',
activityType: 'thinking',
agentId: ' ',
agentName: ' ',
}),
).toBe(current);
expect(warnings).toHaveLength(1);
expect(String(warnings[0][0])).toContain('Dropping agent-activity event');
} finally {
console.warn = originalWarn;
}
});
test('clears stale activity when a session restarts', () => {
const current: SessionActivityMap = {
'session-1': {