test(KB-213): fix EventSource mock cleanup in useMultiAgentLogs tests

- Fix EventSource mock cleanup to prevent test interference
- Ensure proper cleanup after each test in useMultiAgentLogs test suite
- Add changeset for the test fix
This commit is contained in:
gsxdsm
2026-03-30 16:26:34 -07:00
parent 5f60afc02d
commit 2a73e602f4
2 changed files with 32 additions and 1 deletions

View File

@@ -1,3 +1,18 @@
/**
* EventSource Mock Cleanup Requirements:
*
* This test file uses a MockEventSource class that tracks all instances in a static
* `instances` array. To prevent test isolation issues, we must ensure:
*
* 1. `MockEventSource.instances` is reset to empty before each test
* 2. Any lingering EventSource instances are closed and removed after each test
* 3. Fake timers are restored to real timers after each test (in case a test failed
* before it could restore them)
*
* Without proper cleanup, fake timers from one test can leak to subsequent tests,
* causing `waitFor()` calls to hang indefinitely.
*/
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { renderHook, act, waitFor } from "@testing-library/react";
import { MAX_LOG_ENTRIES, useMultiAgentLogs } from "../useMultiAgentLogs";
@@ -25,11 +40,22 @@ function getConnections(taskId: string): MockEventSource[] {
}
beforeEach(() => {
MockEventSource.instances = [];
mockFetchAgentLogs.mockReset().mockResolvedValue([]);
// Ensure we start with real timers for every test
vi.useRealTimers();
});
afterEach(() => {
// Clean up is handled by global afterEach in vitest.setup.ts
// Close all lingering EventSource instances to clear reconnect timers
for (const instance of MockEventSource.instances) {
instance.close();
}
MockEventSource.instances = [];
// Safety: ensure real timers are restored even if a test failed
vi.useRealTimers();
});
describe("useMultiAgentLogs", () => {