Files
AgentCompany/tests/unit/mention-resolution.test.ts
T
ZacharyZhang-NY 07d0b4587d Promote AgentCompany-New to the repository root and retire the old app
The legacy application - its src, tests, landing site, promo assets, docs,
release tooling, CI workflows, and root configs - leaves the tree; it stays
reachable in history. Everything under AgentCompany-New moves up unchanged:
same package name, so the production userData/database path is untouched,
and e2e/build scripts already resolved paths relative to the project. The
developer cred.txt stays gitignored at its new location. Full verify:ci
passes from the new root.
2026-07-07 00:04:13 -04:00

129 lines
6.2 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, test } from "vitest";
import { DatabaseConnection } from "../../src/main/database/connection";
import { OrganizationService } from "../../src/main/services/organization-service";
import { ChatService } from "../../src/main/services/chat-service";
import { canonicalMentionHandle } from "../../src/shared/mention-handles";
import { dispatchAgentTool, type AgentToolContext } from "../../src/main/services/agent-tools";
function call(name: string, args: Record<string, unknown>) {
return { id: `call_${randomUUID()}`, name, arguments: args };
}
describe("mention resolution", () => {
test("canonicalMentionHandle strips spaces and lowercases", () => {
expect(canonicalMentionHandle("Sarah Chen")).toBe("sarahchen");
expect(canonicalMentionHandle("J.P. Morgan")).toBe("jpmorgan");
expect(canonicalMentionHandle("alice")).toBe("alice");
});
test("@mention with canonical handle resolves to multi-word agent name", () => {
const connection = new DatabaseConnection(join(tmpdir(), `ac-mention-${randomUUID()}.db`));
const organization = new OrganizationService(connection.db);
let orgId = "";
let ceoId = "";
let providerId = "";
let engineerId = "";
connection.transaction(() => {
orgId = organization.createOrganization({ organizationName: "TestCorp", employeeName: "Yang", mission: "m", goal: "g" });
providerId = (connection.db.prepare("select id from providers where org_id = ? and provider_key = 'openai'").get(orgId) as { id: string }).id;
ceoId = organization.createAgent({
orgId, name: "Alex Kim", role: "CEO", department: "Executive",
modelProviderId: providerId, modelId: "gpt-5.4", budgetCents: 25000, managerAgentId: null,
});
engineerId = organization.createAgent({
orgId, name: "Sarah Chen", role: "Engineer", department: "Engineering",
modelProviderId: providerId, modelId: "gpt-5.4", budgetCents: 10000, managerAgentId: ceoId,
});
});
const handle = canonicalMentionHandle("Sarah Chen");
expect(handle).toBe("sarahchen");
const deps = { db: connection.db, organization, chat: new ChatService(connection.db) };
const ctx: AgentToolContext = {
orgId, agent: { id: ceoId, modelProviderId: providerId, modelId: "gpt-5.4" },
taskProjectId: null, taskGoalId: null, workspace: null,
};
const result = dispatchAgentTool(deps, ctx, call("send_message", {
channel: "engineering",
body: `@${handle} can you review this?`,
}));
expect(result).toContain("Sarah Chen");
const mentions = connection.db.prepare(
"select target_type, target_id, wake_state from chat_mentions where target_type = 'agent' and target_id = ?",
).all(engineerId) as Array<{ target_type: string; target_id: string; wake_state: string }>;
expect(mentions.length).toBeGreaterThan(0);
expect(mentions[0].target_id).toBe(engineerId);
});
test("read_messages returns recent channel messages", () => {
const connection = new DatabaseConnection(join(tmpdir(), `ac-read-${randomUUID()}.db`));
const organization = new OrganizationService(connection.db);
let orgId = "";
let ceoId = "";
let providerId = "";
connection.transaction(() => {
orgId = organization.createOrganization({ organizationName: "ReadCorp", employeeName: "Yang", mission: "m", goal: "g" });
providerId = (connection.db.prepare("select id from providers where org_id = ? and provider_key = 'openai'").get(orgId) as { id: string }).id;
ceoId = organization.createAgent({
orgId, name: "CEO", role: "CEO", department: "Executive",
modelProviderId: providerId, modelId: "gpt-5.4", budgetCents: 25000, managerAgentId: null,
});
});
const deps = { db: connection.db, organization, chat: new ChatService(connection.db) };
const ctx: AgentToolContext = {
orgId, agent: { id: ceoId, modelProviderId: null, modelId: null },
taskProjectId: null, taskGoalId: null, workspace: null,
};
dispatchAgentTool(deps, ctx, call("send_message", { channel: "general", body: "Hello team!" }));
dispatchAgentTool(deps, ctx, call("send_message", { channel: "general", body: "Kickoff meeting at 3pm." }));
const readResult = dispatchAgentTool(deps, ctx, call("read_messages", { channel: "general" }));
expect(readResult).toContain("Hello team!");
expect(readResult).toContain("Kickoff meeting at 3pm.");
});
test("read_messages with no channel returns agent mentions", () => {
const connection = new DatabaseConnection(join(tmpdir(), `ac-inbox-${randomUUID()}.db`));
const organization = new OrganizationService(connection.db);
let orgId = "";
let ceoId = "";
let engineerId = "";
let providerId = "";
connection.transaction(() => {
orgId = organization.createOrganization({ organizationName: "InboxCorp", employeeName: "Yang", mission: "m", goal: "g" });
providerId = (connection.db.prepare("select id from providers where org_id = ? and provider_key = 'openai'").get(orgId) as { id: string }).id;
ceoId = organization.createAgent({
orgId, name: "CEO", role: "CEO", department: "Executive",
modelProviderId: providerId, modelId: "gpt-5.4", budgetCents: 25000, managerAgentId: null,
});
engineerId = organization.createAgent({
orgId, name: "Dana", role: "Engineer", department: "Engineering",
modelProviderId: null, modelId: null, budgetCents: 1000, managerAgentId: ceoId,
});
});
const deps = { db: connection.db, organization, chat: new ChatService(connection.db) };
const ceoCtx: AgentToolContext = {
orgId, agent: { id: ceoId, modelProviderId: null, modelId: null },
taskProjectId: null, taskGoalId: null, workspace: null,
};
dispatchAgentTool(deps, ceoCtx, call("send_message", { channel: "engineering", body: "@dana check the deployment" }));
const danaCtx: AgentToolContext = {
orgId, agent: { id: engineerId, modelProviderId: null, modelId: null },
taskProjectId: null, taskGoalId: null, workspace: null,
};
const inbox = dispatchAgentTool(deps, danaCtx, call("read_messages", {}));
expect(inbox).toContain("check the deployment");
});
});