feat(09-01): define granular event types, IPC channels, entity getters, and Zustand patch methods

- Add 5 granular event interfaces (TaskUpdated, RunUpdated, ApprovalUpdated, AgentUpdated, BudgetChanged) to DesktopEvent union
- Add 4 entity getter IPC channels (getTask, getAgent, getRun, getApproval) with Zod validation and company ownership checks
- Add getRun method to AppDatabase following existing getTask pattern
- Add 4 preload bridge methods for entity getters
- Add 8 patch/remove methods and refreshBadges to CompanySlice
- Add selective-ipc.test.ts with exhaustive union type tests
- Extend store.test.ts with 14 patch method tests

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 4.6 committed 2026-03-27 19:33:33 -04:00
1 parent c1de05a7f1
commit 920a764856
8 files changed
+631 -5

No files matched your search

+117
View File
@@ -0,0 +1,117 @@
import { describe, it, expect } from "vitest";
import type {
DesktopEvent,
TaskUpdatedEvent,
RunUpdatedEvent,
ApprovalUpdatedEvent,
AgentUpdatedEvent,
BudgetChangedEvent,
} from "@shared/types";
describe("selective IPC event types", () => {
it("TaskUpdatedEvent discriminated by type field", () => {
const event: TaskUpdatedEvent = {
type: "task-updated",
taskId: "t-1",
action: "created",
};
expect(event.type).toBe("task-updated");
expect(event.taskId).toBe("t-1");
expect(event.action).toBe("created");
});
it("RunUpdatedEvent discriminated by type field", () => {
const event: RunUpdatedEvent = {
type: "run-updated",
runId: "r-1",
action: "completed",
};
expect(event.type).toBe("run-updated");
expect(event.runId).toBe("r-1");
expect(event.action).toBe("completed");
});
it("ApprovalUpdatedEvent discriminated by type field", () => {
const event: ApprovalUpdatedEvent = {
type: "approval-updated",
approvalId: "a-1",
action: "decided",
};
expect(event.type).toBe("approval-updated");
expect(event.approvalId).toBe("a-1");
expect(event.action).toBe("decided");
});
it("AgentUpdatedEvent discriminated by type field", () => {
const event: AgentUpdatedEvent = {
type: "agent-updated",
agentId: "ag-1",
action: "updated",
};
expect(event.type).toBe("agent-updated");
expect(event.agentId).toBe("ag-1");
expect(event.action).toBe("updated");
});
it("BudgetChangedEvent discriminated by type field", () => {
const event: BudgetChangedEvent = {
type: "budget-changed",
companyId: "c-1",
};
expect(event.type).toBe("budget-changed");
expect(event.companyId).toBe("c-1");
});
it("all five new event types are assignable to DesktopEvent union", () => {
const events: DesktopEvent[] = [
{ type: "task-updated", taskId: "t-1", action: "created" },
{ type: "run-updated", runId: "r-1", action: "status_changed" },
{ type: "approval-updated", approvalId: "a-1", action: "created" },
{ type: "agent-updated", agentId: "ag-1", action: "paused" },
{ type: "budget-changed", companyId: "c-1" },
];
expect(events).toHaveLength(5);
});
it("DesktopEvent union has all 14 members (9 original + 5 new)", () => {
// This is a compile-time check -- if any type is missing, TypeScript will error
const exhaustiveCheck = (event: DesktopEvent): string => {
switch (event.type) {
case "run-log":
return "run-log";
case "domain-changed":
return "domain-changed";
case "shortcut":
return "shortcut";
case "run-status":
return "run-status";
case "heartbeat-status":
return "heartbeat-status";
case "agent-message":
return "agent-message";
case "automation-fired":
return "automation-fired";
case "new-message":
return "new-message";
case "notification-click":
return "notification-click";
case "task-updated":
return "task-updated";
case "run-updated":
return "run-updated";
case "approval-updated":
return "approval-updated";
case "agent-updated":
return "agent-updated";
case "budget-changed":
return "budget-changed";
default: {
const _exhaustive: never = event;
return _exhaustive;
}
}
};
// Dummy call to avoid unused-var warning
expect(typeof exhaustiveCheck).toBe("function");
});
});
+300 -2
View File
@@ -1,12 +1,14 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { useAppStore } from "@renderer/state/store";
import type { ProfileSnapshot } from "@shared/types";
import type { ProfileSnapshot, TaskRecord, RunRecord, AgentRecord, ApprovalRecord } from "@shared/types";
// Mock window.agentCompany for refreshSnapshot test
// Mock window.agentCompany for refreshSnapshot and refreshBadges tests
const mockLoadSnapshot = vi.fn();
const mockGetSidebarBadges = vi.fn();
vi.stubGlobal("window", {
agentCompany: {
loadSnapshot: mockLoadSnapshot,
getSidebarBadges: mockGetSidebarBadges,
},
});
@@ -218,3 +220,299 @@ describe("useAppStore", () => {
expect(useAppStore.getState().socialAccounts).toEqual([account]);
});
});
// -- Minimal snapshot fixture for patch tests --
function makeSnapshot(overrides: Partial<ProfileSnapshot> = {}): ProfileSnapshot {
return {
currentCompanyId: "company-1",
profilePath: "/test/path",
updatesEnabled: true,
theme: "system",
locale: "en",
backend: { secretBackend: "safe_storage" },
companies: [],
connectors: [],
workspaces: [],
agents: [],
goals: [],
projects: [],
tasks: [],
approvals: [],
runs: [],
costs: [],
activity: [],
secrets: [],
comments: [],
socialAccounts: [],
meetings: [],
documents: [],
knowledgeBase: [],
sprints: [],
automationRules: [],
agentMessages: [],
workflows: [],
...overrides,
};
}
const fakeTask = (id: string, title = "Task"): TaskRecord => ({
id,
companyId: "company-1",
projectId: null,
goalId: null,
parentId: null,
title,
description: "",
assigneeAgentId: null,
workspaceId: null,
status: "open",
priority: "medium",
activeRunId: null,
sessionJson: null,
sessionDisplayId: null,
costUsd: 0,
requiresUserReview: false,
createdAt: "2026-01-01",
updatedAt: "2026-01-01",
} as TaskRecord);
const fakeRun = (id: string): RunRecord => ({
id,
companyId: "company-1",
taskId: "t-1",
agentId: "ag-1",
workspaceId: null,
connectorId: "claude_local",
status: "queued",
summary: "",
errorMessage: null,
exitCode: null,
signal: null,
model: null,
sessionDisplayId: null,
costUsd: 0,
unattributedCost: false,
startedAt: null,
finishedAt: null,
logPath: null,
createdAt: "2026-01-01",
updatedAt: "2026-01-01",
} as RunRecord);
const fakeAgent = (id: string, name = "Agent"): AgentRecord => ({
id,
companyId: "company-1",
name,
role: "engineer",
title: "",
department: "engineering",
status: "idle",
reportsTo: null,
connectorId: "claude_local",
workspaceId: null,
model: null,
capabilities: "",
budgetMonthlyUsd: 100,
spentMonthlyUsd: 0,
metadataJson: "{}",
heartbeatEnabled: false,
heartbeatIntervalSec: 0,
lastHeartbeatAt: null,
createdAt: "2026-01-01",
updatedAt: "2026-01-01",
} as AgentRecord);
const fakeApproval = (id: string): ApprovalRecord => ({
id,
companyId: "company-1",
relatedTaskId: "t-1",
requestedByAgentId: "ag-1",
type: "task_completion",
payloadSummary: "",
impactSummary: "",
decisionNote: null,
state: "pending",
createdAt: "2026-01-01",
updatedAt: "2026-01-01",
} as ApprovalRecord);
describe("CompanySlice patch methods", () => {
beforeEach(() => {
useAppStore.setState(useAppStore.getInitialState());
vi.clearAllMocks();
});
// -- patchTask --
it("patchTask inserts new task at beginning when task ID is not found", () => {
const snapshot = makeSnapshot({ tasks: [fakeTask("t-existing")] });
useAppStore.getState().setSnapshot(snapshot);
const newTask = fakeTask("t-new", "New Task");
useAppStore.getState().patchTask(newTask);
const tasks = useAppStore.getState().snapshot!.tasks;
expect(tasks).toHaveLength(2);
expect(tasks[0]!.id).toBe("t-new");
expect(tasks[1]!.id).toBe("t-existing");
});
it("patchTask updates existing task in-place when task ID matches", () => {
const snapshot = makeSnapshot({ tasks: [fakeTask("t-1", "Original")] });
useAppStore.getState().setSnapshot(snapshot);
const updated = fakeTask("t-1", "Updated");
useAppStore.getState().patchTask(updated);
const tasks = useAppStore.getState().snapshot!.tasks;
expect(tasks).toHaveLength(1);
expect(tasks[0]!.title).toBe("Updated");
});
it("removeTask filters out task by ID", () => {
const snapshot = makeSnapshot({ tasks: [fakeTask("t-1"), fakeTask("t-2")] });
useAppStore.getState().setSnapshot(snapshot);
useAppStore.getState().removeTask("t-1");
const tasks = useAppStore.getState().snapshot!.tasks;
expect(tasks).toHaveLength(1);
expect(tasks[0]!.id).toBe("t-2");
});
// -- patchRun --
it("patchRun inserts new run when run ID is not found", () => {
const snapshot = makeSnapshot({ runs: [fakeRun("r-existing")] });
useAppStore.getState().setSnapshot(snapshot);
useAppStore.getState().patchRun(fakeRun("r-new"));
const runs = useAppStore.getState().snapshot!.runs;
expect(runs).toHaveLength(2);
expect(runs[0]!.id).toBe("r-new");
});
it("patchRun updates existing run in-place", () => {
const run = fakeRun("r-1");
const snapshot = makeSnapshot({ runs: [run] });
useAppStore.getState().setSnapshot(snapshot);
const updated = { ...fakeRun("r-1"), status: "running" as const };
useAppStore.getState().patchRun(updated);
const runs = useAppStore.getState().snapshot!.runs;
expect(runs).toHaveLength(1);
expect(runs[0]!.status).toBe("running");
});
it("removeRun filters out run by ID", () => {
const snapshot = makeSnapshot({ runs: [fakeRun("r-1"), fakeRun("r-2")] });
useAppStore.getState().setSnapshot(snapshot);
useAppStore.getState().removeRun("r-1");
const runs = useAppStore.getState().snapshot!.runs;
expect(runs).toHaveLength(1);
expect(runs[0]!.id).toBe("r-2");
});
// -- patchAgent --
it("patchAgent inserts new agent when agent ID is not found", () => {
const snapshot = makeSnapshot({ agents: [fakeAgent("ag-existing")] });
useAppStore.getState().setSnapshot(snapshot);
useAppStore.getState().patchAgent(fakeAgent("ag-new", "New Agent"));
const agents = useAppStore.getState().snapshot!.agents;
expect(agents).toHaveLength(2);
expect(agents[0]!.id).toBe("ag-new");
});
it("patchAgent updates existing agent in-place", () => {
const snapshot = makeSnapshot({ agents: [fakeAgent("ag-1", "Old")] });
useAppStore.getState().setSnapshot(snapshot);
useAppStore.getState().patchAgent(fakeAgent("ag-1", "New"));
const agents = useAppStore.getState().snapshot!.agents;
expect(agents).toHaveLength(1);
expect(agents[0]!.name).toBe("New");
});
it("removeAgent filters out agent by ID", () => {
const snapshot = makeSnapshot({ agents: [fakeAgent("ag-1"), fakeAgent("ag-2")] });
useAppStore.getState().setSnapshot(snapshot);
useAppStore.getState().removeAgent("ag-1");
const agents = useAppStore.getState().snapshot!.agents;
expect(agents).toHaveLength(1);
expect(agents[0]!.id).toBe("ag-2");
});
// -- patchApproval --
it("patchApproval inserts new approval when approval ID is not found", () => {
const snapshot = makeSnapshot({ approvals: [fakeApproval("ap-existing")] });
useAppStore.getState().setSnapshot(snapshot);
useAppStore.getState().patchApproval(fakeApproval("ap-new"));
const approvals = useAppStore.getState().snapshot!.approvals;
expect(approvals).toHaveLength(2);
expect(approvals[0]!.id).toBe("ap-new");
});
it("patchApproval updates existing approval in-place", () => {
const approval = fakeApproval("ap-1");
const snapshot = makeSnapshot({ approvals: [approval] });
useAppStore.getState().setSnapshot(snapshot);
const updated = { ...fakeApproval("ap-1"), state: "approved" as const };
useAppStore.getState().patchApproval(updated);
const approvals = useAppStore.getState().snapshot!.approvals;
expect(approvals).toHaveLength(1);
expect(approvals[0]!.state).toBe("approved");
});
it("removeApproval filters out approval by ID", () => {
const snapshot = makeSnapshot({ approvals: [fakeApproval("ap-1"), fakeApproval("ap-2")] });
useAppStore.getState().setSnapshot(snapshot);
useAppStore.getState().removeApproval("ap-1");
const approvals = useAppStore.getState().snapshot!.approvals;
expect(approvals).toHaveLength(1);
expect(approvals[0]!.id).toBe("ap-2");
});
// -- refreshBadges --
it("refreshBadges calls window.agentCompany.getSidebarBadges and updates badges", async () => {
const mockBadges = { tasks: 5, approvals: 2, runs: 0, messages: 3 };
mockGetSidebarBadges.mockResolvedValue({ ok: true, data: mockBadges });
// Set current company ID first
useAppStore.getState().setCurrentCompanyId("company-1");
await useAppStore.getState().refreshBadges();
expect(mockGetSidebarBadges).toHaveBeenCalledWith("company-1");
expect(useAppStore.getState().badges).toEqual(mockBadges);
});
// -- null-snapshot safety --
it("patch methods are no-ops when snapshot is null", () => {
expect(useAppStore.getState().snapshot).toBeNull();
// These should not crash
useAppStore.getState().patchTask(fakeTask("t-1"));
useAppStore.getState().removeTask("t-1");
useAppStore.getState().patchRun(fakeRun("r-1"));
useAppStore.getState().removeRun("r-1");
useAppStore.getState().patchAgent(fakeAgent("ag-1"));
useAppStore.getState().removeAgent("ag-1");
useAppStore.getState().patchApproval(fakeApproval("ap-1"));
useAppStore.getState().removeApproval("ap-1");
expect(useAppStore.getState().snapshot).toBeNull();
});
});