Files
AgentCompany/tests/unit/profile-identity.test.ts
T
ZacharyZhang-NY 04093c4869 Harden the seams a project sweep surfaced
A systematic review of the whole codebase (main process, renderer,
IPC/migration/packaging seams) surfaced four real defects; each is
fixed at its root:

- Legacy catalogue drift: the operator's profile still carried 89
  active skills from the removed ~/.agents import era next to the 228
  built-ins. Catalogue sync now mirrors the bundle - actives whose slug
  the bundle does not ship retire (bindings keep resolving via their
  pinned revisions; retired skills leave list_skills and cannot be
  granted anew)
- Broken-bundle guard: staging now refuses a bundle without the
  humanizer skill, so an empty or quarantined resources folder can
  never trigger the mirror pass into retiring a healthy catalogue
- Duplicate agent names: @mentions, DM routing, and task assignment all
  resolve by name, so createAgent and updateAgent renames now reject a
  name already held by an active agent (case-insensitive); the mention
  resolver still refuses to guess between legacy duplicates
- Stale DM titles: agent-to-agent conversation rows titled themselves
  from the frozen creation-time space name, so renames never reached
  the sidebar; direct titles now derive from members' current names

Verified clean in the same sweep: IPC surface (82 channels, handler +
preload + api aligned), per-migration transactions with rollback,
quota-parking month rollover, retry-budget counting, meeting spot
reuse across floor-plan rebuilds.
2026-07-08 23:41:51 -04:00

126 lines
5.6 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";
function harness(): { connection: DatabaseConnection; organization: OrganizationService; orgId: string; agentId: string; employeeId: string } {
const connection = new DatabaseConnection(join(tmpdir(), `ac-profile-${randomUUID()}.db`));
const organization = new OrganizationService(connection.db);
let orgId = "";
let agentId = "";
connection.transaction(() => {
orgId = organization.createOrganization({ organizationName: "OrbitOps", employeeName: "Yang", mission: "m", goal: "g" });
agentId = organization.createAgent({
orgId, name: "CEO", role: "CEO", department: "Executive",
modelProviderId: null, modelId: null, budgetCents: 25000, managerAgentId: null, roleLevel: "c_suite",
});
});
const employeeId = (connection.db.prepare("select id from employees where org_id = ?").get(orgId) as { id: string }).id;
return { connection, organization, orgId, agentId, employeeId };
}
describe("identity editing", () => {
test("renames an agent (the CEO included) and stores its avatar", () => {
const h = harness();
h.connection.transaction(() => h.organization.updateAgent({
orgId: h.orgId,
agentId: h.agentId,
name: "Nova Chen",
role: "CEO",
title: "Chief Executive Officer",
roleLevel: "c_suite",
department: "Executive",
budgetCents: 25000,
modelProviderId: null,
modelId: null,
avatarEmoji: "🚀",
avatarColor: "#26B69A",
}));
const row = h.connection.db.prepare("select name, avatar_emoji, avatar_color from agents where id = ?").get(h.agentId) as
{ name: string; avatar_emoji: string; avatar_color: string };
expect(row).toEqual({ name: "Nova Chen", avatar_emoji: "🚀", avatar_color: "#26b69a" });
// Clearing the avatar returns the tile to the hashed palette.
h.connection.transaction(() => h.organization.updateAgent({
orgId: h.orgId,
agentId: h.agentId,
role: "CEO",
title: "Chief Executive Officer",
roleLevel: "c_suite",
department: "Executive",
budgetCents: 25000,
modelProviderId: null,
modelId: null,
avatarEmoji: "",
avatarColor: "",
}));
const cleared = h.connection.db.prepare("select name, avatar_emoji, avatar_color from agents where id = ?").get(h.agentId) as
{ name: string; avatar_emoji: string | null; avatar_color: string | null };
expect(cleared).toEqual({ name: "Nova Chen", avatar_emoji: null, avatar_color: null });
h.connection.close();
});
test("rejects renaming or hiring into an existing active agent name", () => {
const h = harness();
h.connection.transaction(() => {
h.organization.createAgent({
orgId: h.orgId, name: "Dana Lee", role: "Engineer", department: "Engineering",
modelProviderId: null, modelId: null, budgetCents: 1000, managerAgentId: h.agentId,
});
});
// Hiring a second "Dana Lee" (any case) is ambiguous for mentions and DMs.
expect(() => h.organization.createAgent({
orgId: h.orgId, name: "dana lee", role: "Designer", department: "Design",
modelProviderId: null, modelId: null, budgetCents: 1000, managerAgentId: h.agentId,
})).toThrow("already exists");
// Renaming the CEO onto that name is equally ambiguous.
expect(() => h.organization.updateAgent({
orgId: h.orgId, agentId: h.agentId, name: "Dana Lee", role: "CEO", title: "CEO",
roleLevel: "c_suite", department: "Executive", budgetCents: 25000, modelProviderId: null, modelId: null,
})).toThrow("already exists");
// Saving without changing the name stays legal.
h.connection.transaction(() => h.organization.updateAgent({
orgId: h.orgId, agentId: h.agentId, name: "CEO", role: "CEO", title: "CEO",
roleLevel: "c_suite", department: "Executive", budgetCents: 25000, modelProviderId: null, modelId: null,
}));
h.connection.close();
});
test("rejects blank names and malformed avatar colors", () => {
const h = harness();
const base = {
orgId: h.orgId,
agentId: h.agentId,
role: "CEO",
title: "Chief Executive Officer",
roleLevel: "c_suite",
department: "Executive",
budgetCents: 25000,
modelProviderId: null,
modelId: null,
};
expect(() => h.organization.updateAgent({ ...base, name: " " })).toThrow("Agent name is required.");
expect(() => h.organization.updateAgent({ ...base, avatarColor: "red" })).toThrow("Avatar color must be a #rrggbb hex value.");
h.connection.close();
});
test("updates the operator's display name and avatar", () => {
const h = harness();
h.connection.transaction(() => h.organization.updateEmployeeProfile({
orgId: h.orgId,
employeeId: h.employeeId,
displayName: "Yang Zhang",
avatarEmoji: "🎯",
avatarColor: "#5e6ad2",
}));
const row = h.connection.db.prepare("select display_name, avatar_emoji, avatar_color from employees where id = ?").get(h.employeeId) as
{ display_name: string; avatar_emoji: string; avatar_color: string };
expect(row).toEqual({ display_name: "Yang Zhang", avatar_emoji: "🎯", avatar_color: "#5e6ad2" });
expect(() => h.organization.updateEmployeeProfile({ orgId: h.orgId, employeeId: h.employeeId, displayName: " " }))
.toThrow("Display name is required.");
h.connection.close();
});
});