large update
This commit is contained in:
1 parent
2ed952f19c
commit
65e85135c1
111 files changed
+3936
-2933
No files matched your search
@@ -1,78 +1,7 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { createEventBatcher, createDomainEventBatcher, type GranularEventEntry } from "@renderer/lib/ipc-batcher";
|
||||
import { createDomainEventBatcher, type GranularEventEntry } from "@renderer/lib/ipc-batcher";
|
||||
import type { DesktopEvent } from "@shared/types";
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createDomainEventBatcher", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
|
||||
@@ -122,182 +122,7 @@ describe("wakeup lifecycle", () => {
|
||||
expect(triggers[1].trigger).toBe("task.status_changed");
|
||||
});
|
||||
|
||||
it("enqueueWakeupRequest coalesces into existing deferred wakeup", () => {
|
||||
const companyId = createTestCompany(db);
|
||||
createTestConnector(db);
|
||||
const workspaceId = createTestWorkspace(db, companyId);
|
||||
const agentId = createTestAgent(db, companyId, workspaceId);
|
||||
|
||||
db.enqueueWakeupRequest(companyId, agentId, "task.created", "First task");
|
||||
db.deferWakeupRequest(agentId, companyId);
|
||||
|
||||
// Verify it's deferred
|
||||
const deferredWakes = db.listWakeupRequests(companyId, { status: "deferred" });
|
||||
expect(deferredWakes.length).toBe(1);
|
||||
|
||||
// Enqueue another -- should coalesce into the deferred wake
|
||||
db.enqueueWakeupRequest(companyId, agentId, "task.status_changed", "Status update");
|
||||
|
||||
const allWakes = db.listWakeupRequests(companyId, { agentId });
|
||||
// Only one row (the deferred one, now with coalesced trigger)
|
||||
expect(allWakes.length).toBe(1);
|
||||
expect(allWakes[0].coalescedCount).toBe(1);
|
||||
|
||||
const triggers = JSON.parse(allWakes[0].triggerDetail!);
|
||||
expect(triggers).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("claimWakeupRequest transitions queued to claimed", () => {
|
||||
const companyId = createTestCompany(db);
|
||||
createTestConnector(db);
|
||||
const workspaceId = createTestWorkspace(db, companyId);
|
||||
const agentId = createTestAgent(db, companyId, workspaceId);
|
||||
const taskId = db.saveTask({
|
||||
companyId,
|
||||
title: "Claim task",
|
||||
description: "For run creation",
|
||||
assigneeAgentId: agentId,
|
||||
workspaceId,
|
||||
priority: "medium",
|
||||
status: "todo",
|
||||
});
|
||||
|
||||
db.enqueueWakeupRequest(companyId, agentId, "task.created", "New task");
|
||||
|
||||
const { id: runId } = db.createRun({
|
||||
companyId,
|
||||
taskId,
|
||||
agentId,
|
||||
workspaceId,
|
||||
connectorId: "codex_local",
|
||||
});
|
||||
|
||||
const claimed = db.claimWakeupRequest(agentId, companyId, runId);
|
||||
expect(claimed).not.toBeNull();
|
||||
expect(claimed!.triggerDetail).toBeTruthy();
|
||||
|
||||
const wakes = db.listWakeupRequests(companyId, { status: "claimed" });
|
||||
expect(wakes.length).toBe(1);
|
||||
expect(wakes[0].runId).toBe(runId);
|
||||
expect(wakes[0].claimedAt).toBeTruthy();
|
||||
});
|
||||
|
||||
it("claimWakeupRequest returns null when no queued wake exists", () => {
|
||||
const companyId = createTestCompany(db);
|
||||
createTestConnector(db);
|
||||
const workspaceId = createTestWorkspace(db, companyId);
|
||||
const agentId = createTestAgent(db, companyId, workspaceId);
|
||||
|
||||
const result = db.claimWakeupRequest(agentId, companyId, "fake-run-id");
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("completeWakeupRequest transitions claimed to completed", () => {
|
||||
const companyId = createTestCompany(db);
|
||||
createTestConnector(db);
|
||||
const workspaceId = createTestWorkspace(db, companyId);
|
||||
const agentId = createTestAgent(db, companyId, workspaceId);
|
||||
const taskId = db.saveTask({
|
||||
companyId,
|
||||
title: "Complete task",
|
||||
description: "For run creation",
|
||||
assigneeAgentId: agentId,
|
||||
workspaceId,
|
||||
priority: "medium",
|
||||
status: "todo",
|
||||
});
|
||||
|
||||
db.enqueueWakeupRequest(companyId, agentId, "task.created", "New task");
|
||||
|
||||
const { id: runId } = db.createRun({
|
||||
companyId,
|
||||
taskId,
|
||||
agentId,
|
||||
workspaceId,
|
||||
connectorId: "codex_local",
|
||||
});
|
||||
|
||||
const claimed = db.claimWakeupRequest(agentId, companyId, runId);
|
||||
expect(claimed).not.toBeNull();
|
||||
|
||||
db.completeWakeupRequest(claimed!.id);
|
||||
|
||||
const completed = db.listWakeupRequests(companyId, { status: "completed" });
|
||||
expect(completed.length).toBe(1);
|
||||
expect(completed[0].finishedAt).toBeTruthy();
|
||||
});
|
||||
|
||||
it("deferWakeupRequest transitions queued to deferred", () => {
|
||||
const companyId = createTestCompany(db);
|
||||
createTestConnector(db);
|
||||
const workspaceId = createTestWorkspace(db, companyId);
|
||||
const agentId = createTestAgent(db, companyId, workspaceId);
|
||||
|
||||
db.enqueueWakeupRequest(companyId, agentId, "task.created", "New task");
|
||||
db.deferWakeupRequest(agentId, companyId);
|
||||
|
||||
const deferred = db.listWakeupRequests(companyId, { status: "deferred" });
|
||||
expect(deferred.length).toBe(1);
|
||||
|
||||
const queued = db.listWakeupRequests(companyId, { status: "queued" });
|
||||
expect(queued.length).toBe(0);
|
||||
});
|
||||
|
||||
it("promoteWakeupRequests promotes deferred to queued", () => {
|
||||
const companyId = createTestCompany(db);
|
||||
createTestConnector(db);
|
||||
const workspaceId = createTestWorkspace(db, companyId);
|
||||
const agentId = createTestAgent(db, companyId, workspaceId);
|
||||
|
||||
db.enqueueWakeupRequest(companyId, agentId, "task.created", "First task");
|
||||
db.deferWakeupRequest(agentId, companyId);
|
||||
|
||||
// Verify deferred
|
||||
expect(db.listWakeupRequests(companyId, { status: "deferred" }).length).toBe(1);
|
||||
|
||||
const promoted = db.promoteWakeupRequests(agentId, companyId);
|
||||
expect(promoted).toBe(1);
|
||||
|
||||
const queued = db.listWakeupRequests(companyId, { status: "queued" });
|
||||
expect(queued.length).toBe(1);
|
||||
|
||||
const deferred = db.listWakeupRequests(companyId, { status: "deferred" });
|
||||
expect(deferred.length).toBe(0);
|
||||
});
|
||||
|
||||
it("promoteWakeupRequests merges deferred into existing queued", () => {
|
||||
const companyId = createTestCompany(db);
|
||||
createTestConnector(db);
|
||||
const workspaceId = createTestWorkspace(db, companyId);
|
||||
const agentId = createTestAgent(db, companyId, workspaceId);
|
||||
|
||||
// Create a queued wake, defer it, then create a new queued wake
|
||||
db.enqueueWakeupRequest(companyId, agentId, "first.trigger", "First");
|
||||
db.deferWakeupRequest(agentId, companyId);
|
||||
|
||||
// Now create a new queued wake for the same agent
|
||||
db.enqueueWakeupRequest(companyId, agentId, "second.trigger", "Second");
|
||||
|
||||
// At this point: 1 deferred (first) coalesced with second trigger, or 1 deferred + 1 queued
|
||||
// Actually: enqueueWakeupRequest coalesces into deferred, so we have 1 deferred row with 2 triggers
|
||||
// Let's create a scenario with distinct deferred + queued:
|
||||
|
||||
// Start fresh for clarity
|
||||
// Clear by completing
|
||||
const wakesBeforePromote = db.listWakeupRequests(companyId, { agentId });
|
||||
// The coalescing means both triggers are in one deferred row
|
||||
// So let's verify promote on that single deferred row
|
||||
const promoted = db.promoteWakeupRequests(agentId, companyId);
|
||||
|
||||
// After promote: should have 1 queued row with merged triggers
|
||||
const queued = db.listWakeupRequests(companyId, { status: "queued" });
|
||||
expect(queued.length).toBe(1);
|
||||
|
||||
const triggers = JSON.parse(queued[0].triggerDetail!);
|
||||
expect(triggers.length).toBeGreaterThanOrEqual(2);
|
||||
});
|
||||
|
||||
it("listWakeupRequests returns rows filtered by companyId and optional status/agentId", () => {
|
||||
it("listWakeupRequests returns rows filtered by companyId and optional agentId", () => {
|
||||
const companyId = createTestCompany(db);
|
||||
createTestConnector(db);
|
||||
const workspaceId = createTestWorkspace(db, companyId);
|
||||
@@ -315,15 +140,5 @@ describe("wakeup lifecycle", () => {
|
||||
const agent1Wakes = db.listWakeupRequests(companyId, { agentId: agent1Id });
|
||||
expect(agent1Wakes.length).toBe(1);
|
||||
expect(agent1Wakes[0].agentId).toBe(agent1Id);
|
||||
|
||||
// Filter by status
|
||||
db.deferWakeupRequest(agent1Id, companyId);
|
||||
const queuedOnly = db.listWakeupRequests(companyId, { status: "queued" });
|
||||
expect(queuedOnly.length).toBe(1);
|
||||
expect(queuedOnly[0].agentId).toBe(agent2Id);
|
||||
|
||||
const deferredOnly = db.listWakeupRequests(companyId, { status: "deferred" });
|
||||
expect(deferredOnly.length).toBe(1);
|
||||
expect(deferredOnly[0].agentId).toBe(agent1Id);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user