Files
AgentCompany/tests/unit/message-dispatch.test.ts
T
ZacharyZhang-NY e94939a562 test(connector-id): migrate fixtures and assertions to pi-mono ids
Replaces legacy adapter ids (codex_local / claude_local / gemini_local /
claude_compatible) across ~42 test files with pi-mono equivalents:
- codex_local → openai-codex
- claude_local → claude-pro
- gemini_local → gemini-cli
- claude_compatible → anthropic

Hand-rewrites where fixture semantics changed:
- connector-matching: TASK_TYPE_PREFERENCE ordering rebased (claude-pro
  first for code/content/management; google first for research)
- connector-model-policy: getModels(provider) from pi-ai instead of the
  deleted adapter.module.models array
- session-management + edge-case-session: adapter exemption removed;
  rotation now uniform under pi-mono
- ipc-core-admin-handlers: CSV compat model replaced with pi-ai anthropic
- connectors.spec: lookup by openai-codex id (catalog now starts with claude-pro)
- onboarding-model-pool: listProviderModels(provider) helper; label text
  changed to "ChatGPT Plus/Pro (Codex)"
- database integration: added vault insecure-fallback env + electron mock
- command-paths: uses new @main/connector-helpers normalizeCommandValue
- pi-mono-migrations: added `as string` casts for 3 negative-existence
  assertions verifying legacy ids were removed

Drops:
- pnpm-workspace.yaml packages: [] (was "packages/*"; directory empty)
- obsolete CONNECTOR_STRENGTHS import; .module.models probing

Verification:
- pnpm typecheck → 0 errors
- pnpm lint → 0 errors / 0 warnings
- pnpm build:verify → 0 errors / 0 warnings
- pnpm test:unit → 508/508 pass
- pnpm test:integration → 96/96 pass
- All 6 adapter hard gates pass (NOT FOUND / 0 matches / empty)
- pi-ai / pi-agent-core / pi-coding-agent all resolved @0.67.6
2026-04-17 02:36:53 -04:00

252 lines
7.0 KiB
TypeScript

import { mkdtemp, rm } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { afterEach, describe, expect, it, vi } from "vitest";
import { AppDatabase } from "../../src/main/database";
import { createThreadIntegration } from "../../src/main/thread-integration";
import { dispatchAgentMessage, HUMAN_SENDER_ID } from "../../src/main/message-dispatch";
const tempDirs: string[] = [];
async function createTempDir(prefix: string) {
const dir = await mkdtemp(join(tmpdir(), prefix));
tempDirs.push(dir);
return dir;
}
afterEach(async () => {
await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
});
function createScaffold(db: AppDatabase) {
db.updateConnectorHealth({
id: "openai-codex",
status: "ready",
authState: "authenticated",
version: "test",
lastError: null,
});
const companyId = db.saveCompany({
name: "Message Dispatch Co",
description: "Tests board wake and wait-state resolution",
status: "active",
executionMode: "ai_cofounder",
});
const workspaceId = db.saveWorkspace({
companyId,
name: "Primary",
localPath: "/tmp/message-dispatch",
repoUrl: "",
repoRef: "",
isPrimary: true,
});
const ceoId = db.saveAgent({
companyId,
name: "CEO",
role: "CEO",
title: "Chief Executive Officer",
status: "idle",
reportsTo: null,
connectorId: "openai-codex",
workspaceId,
model: null,
capabilities: "Lead the company",
budgetMonthlyUsd: 250,
});
const ctoId = db.saveAgent({
companyId,
name: "CTO",
role: "CTO",
title: "Chief Technology Officer",
status: "idle",
reportsTo: ceoId,
connectorId: "openai-codex",
workspaceId,
model: null,
capabilities: "Lead engineering",
budgetMonthlyUsd: 200,
department: "engineering",
});
const cmoId = db.saveAgent({
companyId,
name: "CMO",
role: "CMO",
title: "Chief Marketing Officer",
status: "idle",
reportsTo: ceoId,
connectorId: "openai-codex",
workspaceId,
model: null,
capabilities: "Lead marketing",
budgetMonthlyUsd: 200,
department: "marketing",
});
const pendingId = db.saveAgent({
companyId,
name: "Pending Recruit",
role: "IC",
title: "Pending Hire",
status: "pending_approval",
reportsTo: ctoId,
connectorId: "openai-codex",
workspaceId,
model: null,
capabilities: "Pending",
budgetMonthlyUsd: 100,
department: "engineering",
});
const terminatedId = db.saveAgent({
companyId,
name: "Former COO",
role: "COO",
title: "Former COO",
status: "terminated",
reportsTo: ceoId,
connectorId: "openai-codex",
workspaceId,
model: null,
capabilities: "No longer active",
budgetMonthlyUsd: 100,
department: "operations",
});
const taskId = db.saveTask({
companyId,
title: "Review architecture plan",
description: "Need board feedback in the thread.",
assigneeAgentId: ctoId,
workspaceId,
priority: "high",
status: "todo",
});
return { companyId, workspaceId, ceoId, ctoId, cmoId, pendingId, terminatedId, taskId };
}
describe("message dispatch", () => {
it("wakes top-level active agents for company messages and active agents for incident messages", async () => {
const profileDir = await createTempDir("agentcompany-message-dispatch-");
const db = new AppDatabase(profileDir);
db.init();
const { companyId, ceoId, ctoId, cmoId } = createScaffold(db);
const wakeAgentIfPossible = vi.fn().mockReturnValue(null);
const companyResult = dispatchAgentMessage({
db,
wakeAgentIfPossible,
}, {
companyId,
fromAgentId: HUMAN_SENDER_ID,
fromHumanUser: true,
channel: "company",
body: "Board broadcast",
});
expect(companyResult.ok).toBe(true);
if (!companyResult.ok) throw new Error("Expected company message dispatch to succeed.");
expect(companyResult.data.wakeTargets).toEqual([ceoId]);
const incidentResult = dispatchAgentMessage({
db,
wakeAgentIfPossible,
}, {
companyId,
fromAgentId: HUMAN_SENDER_ID,
fromHumanUser: true,
channel: "incident",
body: "Urgent issue",
});
expect(incidentResult.ok).toBe(true);
if (!incidentResult.ok) throw new Error("Expected incident message dispatch to succeed.");
expect(new Set(incidentResult.data.wakeTargets)).toEqual(new Set([ceoId, ctoId, cmoId]));
db.close();
});
it("resolves only user_reply wait states for company-wide human messages, but scoped replies resolve thread wait states", async () => {
const profileDir = await createTempDir("agentcompany-message-wait-");
const db = new AppDatabase(profileDir);
db.init();
const { companyId, ctoId, taskId } = createScaffold(db);
const threadIntegration = createThreadIntegration({
db,
logger: { info: () => {}, error: () => {} },
});
threadIntegration.onTaskCreated(taskId, companyId);
const thread = db.listThreads({
companyId,
type: "task",
relatedEntityType: "task",
relatedEntityId: taskId,
limit: 1,
})[0];
if (!thread) {
throw new Error("Expected task thread to exist.");
}
const replyWait = db.setThreadWaitState({
threadId: thread.id,
companyId,
waitingFor: "user_reply",
relatedApprovalId: null,
reason: "Reply needed",
});
const reviewWait = db.setThreadWaitState({
threadId: thread.id,
companyId,
waitingFor: "user_review",
relatedApprovalId: null,
reason: "Review needed",
});
const approvalWait = db.setThreadWaitState({
threadId: thread.id,
companyId,
waitingFor: "user_approval",
relatedApprovalId: "approval-1",
reason: "Approval needed",
});
dispatchAgentMessage({
db,
onHumanMessage: (currentCompanyId, toAgentId, threadId, waitStateId) =>
threadIntegration.onHumanMessage(currentCompanyId, toAgentId, threadId, waitStateId),
}, {
companyId,
fromAgentId: HUMAN_SENDER_ID,
fromHumanUser: true,
channel: "company",
body: "Board-wide update",
});
let unresolved = db.listThreadWaitStates(companyId);
expect(unresolved.some((entry) => entry.id === replyWait.id)).toBe(false);
expect(unresolved.some((entry) => entry.id === reviewWait.id)).toBe(true);
dispatchAgentMessage({
db,
onHumanMessage: (currentCompanyId, toAgentId, threadId, waitStateId) =>
threadIntegration.onHumanMessage(currentCompanyId, toAgentId, threadId, waitStateId),
}, {
companyId,
fromAgentId: HUMAN_SENDER_ID,
fromHumanUser: true,
channel: "direct",
toAgentId: ctoId,
threadId: thread.id,
body: "Specific reply for the thread",
});
unresolved = db.listThreadWaitStates(companyId);
expect(unresolved.some((entry) => entry.id === reviewWait.id)).toBe(false);
expect(unresolved.some((entry) => entry.id === approvalWait.id)).toBe(true);
db.close();
});
});