Multiplayer agent harness for work.
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import {
personKey,
personKeys,
samePerson,
samePersonInDirectory,
samePersonMatcher,
} from "../src/directory/person.ts";
import { createDirectoryStore } from "../src/directory/directory-store.ts";
import { canAdministerCron, canAdministerWebhook } from "../src/api/control-service.ts";
import type { Cron, Webhook } from "../src/types.ts";
import { scopeId } from "../src/types.ts";
describe("personKey / samePerson: the canonical same-person primitive", () => {
it("trims, and lowercases emails only (the exact formula the connector/secret-drop checks used)", () => {
assert.equal(personKey(" [email protected] "), "[email protected]");
assert.equal(personKey("U-Jordan"), "U-Jordan", "a non-email id is byte-exact — never folded");
assert.equal(personKey(undefined), "");
assert.equal(personKey(null), "");
});
it("equates email-case drift, never distinct ids, and never the empty id", () => {
assert.equal(samePerson("[email protected]", "[email protected]"), true);
assert.equal(samePerson("[email protected]", "[email protected]"), false);
assert.equal(samePerson("U-jordan", "u-jordan"), false, "non-email ids stay case-sensitive");
assert.equal(samePerson("", ""), false, "an empty id names nobody — fail closed");
});
it("bridges slackId ↔ email principal ONLY via an authoritative roster row", async () => {
const dir = createDirectoryStore();
await dir.replace([
{ principalId: "[email protected]", displayName: "Jordan", type: "internal", slackId: "U-jordan" },
{ principalId: "[email protected]", displayName: "Casey", type: "internal", slackId: "U-casey" },
]);
assert.equal(await samePersonInDirectory(dir, "[email protected]", "U-jordan"), true);
assert.equal(
await samePersonInDirectory(dir, "U-jordan", "[email protected]"),
true,
"cased email still finds its roster row",
);
assert.equal(await samePersonInDirectory(dir, "[email protected]", "U-casey"), false, "distinct people never merge");
assert.equal(
await samePersonInDirectory(dir, "[email protected]", "U-ghost"),
false,
"no roster row, no bridge — fail closed",
);
assert.equal(
await samePersonInDirectory(createDirectoryStore(), "[email protected]", "U-jordan"),
false,
"empty directory bridges nothing",
);
});
it("personKeys collects every id the roster asserts for one person", () => {
const keys = personKeys({ principalId: "[email protected]", slackId: "U-jordan" }, "[email protected]");
assert.deepEqual([...keys].sort(), ["U-jordan", "[email protected]"]);
});
});
describe("canAdminister: owner checks are same-person, not raw id equality", () => {
const cron = (over: Partial<Cron> = {}): Cron =>
({
id: "c1",
owner: "[email protected]",
ownerScopeId: scopeId("personal", "[email protected]"),
schedule: { everyMs: 1 },
createdAt: 0,
...over,
}) as Cron;
function appOver(dir = createDirectoryStore()) {
return {
isCurrentSharedScopeMember: async () => false,
artifactGrantPermission: async () => null,
membershipControlsScope: async () => false,
managesScope: async () => false,
samePerson: (a: string, b: string) => samePersonInDirectory(dir, a, b),
};
}
it("an email-cased owner passes for the lowercase capability actor", async () => {
assert.equal(await canAdministerCron(appOver(), cron(), "[email protected]"), true);
assert.equal(await canAdministerCron(appOver(), cron(), "[email protected]"), false);
});
it("a Slack-id owner resolves through the directory for an email-identity actor", async () => {
const dir = createDirectoryStore();
await dir.replace([
{ principalId: "[email protected]", displayName: "Jordan", type: "internal", slackId: "U-jordan" },
]);
const slackOwned = cron({ owner: "U-jordan", ownerScopeId: scopeId("personal", "U-jordan") });
assert.equal(await canAdministerCron(appOver(dir), slackOwned, "[email protected]"), true);
assert.equal(
await canAdministerCron(appOver(), slackOwned, "[email protected]"),
false,
"without the roster the bridge fails closed",
);
});
it("webhooks share the same owner rule", async () => {
const webhook = {
id: "w1",
owner: "[email protected]",
ownerScopeId: scopeId("personal", "[email protected]"),
} as Webhook;
assert.equal(await canAdministerWebhook(appOver(), webhook, "[email protected]"), true);
assert.equal(await canAdministerWebhook(appOver(), webhook, "[email protected]"), false);
});
it("the list-path matcher agrees with the per-item check, including the roster bridge", async () => {
const dir = createDirectoryStore();
await dir.replace([
{ principalId: "[email protected]", displayName: "Jordan", type: "internal", slackId: "U-jordan" },
]);
const slackOwned = cron({ owner: "U-jordan", ownerScopeId: scopeId("personal", "U-jordan") });
const isJordan = await samePersonMatcher(dir, "[email protected]");
assert.equal(await canAdministerCron(appOver(dir), slackOwned, "[email protected]", isJordan), true);
assert.equal(
await canAdministerCron(appOver(dir), cron({ owner: "[email protected]" }), "[email protected]", isJordan),
false,
);
});
});
describe("samePersonMatcher: one roster read answers many owner checks", () => {
it("matches the actor's own row's ids without further lookups, and never a stranger", async () => {
const dir = createDirectoryStore();
await dir.replace([
{ principalId: "[email protected]", displayName: "Jordan", type: "internal", slackId: "U-jordan" },
{ principalId: "[email protected]", displayName: "Casey", type: "internal", slackId: "U-casey" },
]);
const isJordan = await samePersonMatcher(dir, "[email protected]");
assert.equal(await isJordan("[email protected]"), true);
assert.equal(await isJordan("U-jordan"), true);
assert.equal(await isJordan("U-casey"), false);
assert.equal(await isJordan("[email protected]"), false);
assert.equal(await isJordan(""), false, "an empty id names nobody — fail closed");
});
it("an actor id with NO row of its own still bridges through the owner's row", async () => {
const dir = createDirectoryStore();
await dir.replace([
{ principalId: "[email protected]", displayName: "Jordan", type: "internal", slackId: "U-jordan" },
]);
const isJordan = await samePersonMatcher(dir, "U-jordan");
assert.equal(await isJordan("[email protected]"), true, "falls back to the per-pair directory bridge");
assert.equal(await isJordan("[email protected]"), false);
const noRoster = await samePersonMatcher(createDirectoryStore(), "U-jordan");
assert.equal(await noRoster("[email protected]"), false, "no roster row anywhere — fail closed");
});
});