Files
AgentCompany/tests/unit/ipc-batcher.test.ts
T
ZacharyZhang-NYandClaude Opus 4.6 5e9d36c058 feat(01-01): create Zustand store with 6 slices, IPC batcher, and unit tests
- Install zustand 5.x and immer 11.x dependencies
- Create 6 Zustand slices: navigation, company, selection, ui, communication, social
- Create combined store with devtools + immer middleware
- Create IPC event batcher with 50ms debounce window (schedule/flush/dispose)
- Create typed selector hooks for narrow state subscriptions
- Add 14 unit tests covering store initialization, slice behaviors, and batcher timing

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 01:30:37 -04:00

74 lines
1.9 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { createEventBatcher } from "@renderer/lib/ipc-batcher";
describe("createEventBatcher", () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it("calls callback after windowMs timeout (50ms)", () => {
const callback = vi.fn();
const batcher = createEventBatcher(callback, 50);
batcher.schedule();
expect(callback).not.toHaveBeenCalled();
vi.advanceTimersByTime(50);
expect(callback).toHaveBeenCalledTimes(1);
});
it("coalesces multiple schedule() calls within window into single callback", () => {
const callback = vi.fn();
const batcher = createEventBatcher(callback, 50);
batcher.schedule();
batcher.schedule();
batcher.schedule();
vi.advanceTimersByTime(50);
expect(callback).toHaveBeenCalledTimes(1);
});
it("flush() immediately invokes callback and clears pending timer", () => {
const callback = vi.fn();
const batcher = createEventBatcher(callback, 50);
batcher.schedule();
batcher.flush();
expect(callback).toHaveBeenCalledTimes(1);
// Advancing time should not trigger another callback
vi.advanceTimersByTime(100);
expect(callback).toHaveBeenCalledTimes(1);
});
it("dispose() cancels pending timer without invoking callback", () => {
const callback = vi.fn();
const batcher = createEventBatcher(callback, 50);
batcher.schedule();
batcher.dispose();
vi.advanceTimersByTime(100);
expect(callback).not.toHaveBeenCalled();
});
it("schedule() after flush() starts new timer", () => {
const callback = vi.fn();
const batcher = createEventBatcher(callback, 50);
batcher.schedule();
batcher.flush();
expect(callback).toHaveBeenCalledTimes(1);
batcher.schedule();
vi.advanceTimersByTime(50);
expect(callback).toHaveBeenCalledTimes(2);
});
});