Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

35.2 KB
Raw Blame
import { randomUUID } from "node:crypto";
import { signedHeaders, withSourceAuthNonce } from "../plugins/chassis/src/core-client.ts";
import { mintSignedPayload, verifySignedPayload } from "../src/auth/signed-token.ts";
import "./support/auto-fake-sprites.ts";

import { test } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { AddressInfo } from "node:net";
import { createInsecureTestServer, createServer } from "../src/api/server.ts";
import { buildApp } from "./support/build-app.ts";
import { INVITE_EMAIL_NOT_CONFIGURED, renderInviteEmail } from "../src/admin/invite-email.ts";
import type { Mailer } from "../plugins/chassis/src/resend.ts";
import { adminStatusFromGrants } from "../src/admin/admin-service.ts";
import { coreEmailAdmission, coreEmailAllowed } from "../plugins/chassis/src/external-members.ts";
import { mintCapabilityToken, CAPABILITY_TTL_MS, CONTROL_PLANE_AUD } from "../src/auth/capability-token.ts";
import { scopeId, type TurnRequest } from "../src/types.ts";
import { testConfig } from "./support/test-config.ts";

const ALICE = "admin-alice@default-org";
const NOBODY = "user-uma@default-org";
const SECRET = "external-users-test-secret".repeat(2);
const PORTAL = "https://portal.example.com";
const ORG = scopeId("org", "default-org");
const DAY_MS = 24 * 60 * 60 * 1000;

type Sent = { to: string; subject: string; text: string; html: string };

function stubMailer(fail?: string): { sent: Sent[]; mailer: Mailer } {
  const sent: Sent[] = [];
  return {
    sent,
    mailer: {
      async send(message) {
        if (fail) throw new Error(fail);
        sent.push(message);
        return "msg-1";
      },
    },
  };
}

function start(
  opts: { mailer?: Mailer; signed?: boolean; emailAuthDomain?: string; emailAuthPrincipals?: string[] } = {},
) {
  const built = buildApp(
    testConfig({
      dataDir: mkdtempSync(join(tmpdir(), "admin-external-users-")),
      ...(opts.signed ? { signingSecret: SECRET } : {}),
    }),
  );
  const deps = {
    admin: built.admin,
    sessions: built.sessions,
    auditLog: built.auditLog,
    identity: built.identity,
    directory: built.directory,
    config: built.config,
    portalUrl: PORTAL,
    portalIdentitySecret: SECRET,
    replayDedupe: { durable: true, claim: built.replayDedupe!.claim.bind(built.replayDedupe!) },
    brandingDefault: { selfLabel: "Acme Bot" },
    ...(opts.mailer ? { inviteMailer: opts.mailer } : {}),
    ...(opts.emailAuthDomain ? { emailAuthDomain: opts.emailAuthDomain } : {}),
    ...(opts.emailAuthPrincipals ? { emailAuthPrincipals: opts.emailAuthPrincipals } : {}),
  };
  const server = opts.signed
    ? createServer(built.app, { ...deps, signingSecret: SECRET })
    : createInsecureTestServer(built.app, deps);
  server.listen(0);
  const base = `http://localhost:${(server.address() as AddressInfo).port}`;
  return { base, built, close: () => new Promise<void>((r) => server.close(() => r())) };
}

const invite = (base: string, body: unknown, headers: Record<string, string> = { "x-admin-actor": ALICE }) =>
  fetch(`${base}/v1/admin/external-users`, {
    method: "POST",
    headers: { ...headers, "content-type": "application/json" },
    body: JSON.stringify(body),
  });
const revoke = (base: string, email: string, headers: Record<string, string> = { "x-admin-actor": ALICE }) =>
  fetch(`${base}/v1/admin/external-users/${encodeURIComponent(email)}`, { method: "DELETE", headers });
const roster = async (base: string, headers: Record<string, string> = { "x-admin-actor": ALICE }): Promise<any> =>
  (await fetch(`${base}/v1/admin/users`, { headers })).json();

const capFor = (actorId: string) =>
  mintCapabilityToken(
    {
      actorId,
      scopeId: scopeId("personal", actorId),
      aud: CONTROL_PLANE_AUD,
      liveActor: true,
      exp: Date.now() + CAPABILITY_TTL_MS,
    },
    SECRET,
  );

test("broker admission preserves configured email members without external invitations", async (t) => {
  const s = start({ signed: true, emailAuthDomain: "corp.example", emailAuthPrincipals: ["[email protected]"] });
  t.after(s.close);
  for (const email of ["[email protected]", "[email protected]"]) {
    assert.equal(s.built.identity.externalMember(email), undefined);
    assert.deepEqual(await coreEmailAdmission(s.base, SECRET, email), { allowed: true });
  }
  for (const email of ["[email protected]", "[email protected]", "[email protected]"]) {
    assert.deepEqual(await coreEmailAdmission(s.base, SECRET, email), { allowed: false });
  }
  await s.built.identity.deactivate("[email protected]");
  assert.deepEqual(await coreEmailAdmission(s.base, SECRET, "[email protected]"), { allowed: false });
  await s.built.identity.putExternalMember({
    email: "[email protected]",
    role: "member",
    expiresAt: Date.now() - 1,
    invitedBy: ALICE,
    createdAt: Date.now() - DAY_MS,
    updatedAt: Date.now(),
  });
  assert.deepEqual(await coreEmailAdmission(s.base, SECRET, "[email protected]"), { allowed: false });
});

test("inviting an external user stores the record, lists it as active, audits, and reports the missing mailer", async () => {
  const s = start();
  try {
    const expiresAt = new Date(Date.now() + 30 * DAY_MS).toISOString();
    const r = await invite(s.base, { email: "  [email protected]  ", expiresAt });
    assert.equal(r.status, 200);
    const d: any = await r.json();
    assert.equal(d.ok, true);
    assert.equal(d.created, true);
    assert.equal(d.member.email, "[email protected]");
    assert.equal(d.member.role, "member");
    assert.equal(d.member.invitedBy, "admin-alice");
    assert.equal(d.member.expiresAt, Date.parse(expiresAt));
    assert.equal(d.emailSent, false);
    assert.equal(d.emailProblem, INVITE_EMAIL_NOT_CONFIGURED);
    assert.equal(d.signInUrl, `${PORTAL}/auth/login`);

    const list = await roster(s.base);
    assert.deepEqual(list.inviteEmail, {
      configured: false,
      problem: INVITE_EMAIL_NOT_CONFIGURED,
      signInUrl: `${PORTAL}/auth/login`,
    });
    assert.equal(list.externalUsers.length, 1);
    assert.equal(list.externalUsers[0].email, "[email protected]");
    assert.equal(list.externalUsers[0].status, "active");
    assert.ok(!list.users.some((u: any) => u.principalId === "[email protected]"), "not folded into users");

    const again = await invite(s.base, { email: "[email protected]", expiresAt: Date.now() + 60 * DAY_MS });
    const updated: any = await again.json();
    assert.equal(updated.created, false);
    assert.equal(updated.member.createdAt, d.member.createdAt);
    assert.equal(updated.emailSent, false);
    assert.equal(updated.emailProblem, undefined, "an update does not retry the invitation email");

    const events = await s.built.auditLog.events();
    assert.ok(events.some((e) => e.action === "external_user.invite" && e.resource === "[email protected]"));
    assert.ok(events.some((e) => e.action === "external_user.update" && e.resource === "[email protected]"));

    assert.equal((await invite(s.base, { email: "[email protected]", expiresAt }, { "x-admin-actor": NOBODY })).status, 403);
  } finally {
    await s.close();
  }
});

test("role org_admin grants admin; member revokes it; DELETE drops the grant and expires the record", async () => {
  const s = start();
  try {
    const expiresAt = Date.now() + 7 * DAY_MS;
    const isAdmin = async (email: string) => adminStatusFromGrants(await s.built.admin.listGrants(), email).isAdmin;

    assert.equal((await invite(s.base, { email: "[email protected]", role: "org_admin", expiresAt })).status, 200);
    assert.equal(await isAdmin("[email protected]"), true);

    assert.equal((await invite(s.base, { email: "[email protected]", role: "member", expiresAt })).status, 200);
    assert.equal(await isAdmin("[email protected]"), false);

    assert.equal((await invite(s.base, { email: "[email protected]", role: "org_admin", expiresAt })).status, 200);
    assert.equal(await isAdmin("[email protected]"), true);
    assert.equal(s.built.identity.classify("[email protected]").type, "internal");

    const gone = await revoke(s.base, "[email protected]");
    assert.equal(gone.status, 200);
    assert.equal(await isAdmin("[email protected]"), false);
    assert.equal(
      s.built.identity.classify("[email protected]").type,
      "guest",
      "a revoked external loses access now",
    );
    const tomb = s.built.identity.externalMember("[email protected]");
    assert.equal(tomb?.role, "member");
    assert.ok(typeof tomb?.expiresAt === "number" && tomb.expiresAt <= Date.now());
    assert.deepEqual(
      (await roster(s.base)).externalUsers.map((m: any) => [m.email, m.status]),
      [["[email protected]", "expired"]],
    );
    const events = await s.built.auditLog.events();
    assert.ok(events.some((e) => e.action === "external_user.revoke"));
    assert.deepEqual(
      events.filter((e) => e.action === "grant.create" || e.action === "grant.revoke").map((e) => e.action),
      ["grant.create", "grant.revoke", "grant.create", "grant.revoke"],
    );

    const again: any = await (await revoke(s.base, "[email protected]")).json();
    assert.deepEqual(again, { ok: true, removed: false }, "revoking again is a no-op until a day has passed");
    assert.equal((await revoke(s.base, "[email protected]")).status, 404);

    const back = await invite(s.base, { email: "[email protected]", expiresAt });
    assert.equal(((await back.json()) as any).created, false);
    assert.equal(s.built.identity.classify("[email protected]").type, "internal", "a new expiry readmits them");

    const now = Date.now();
    await s.built.identity.putExternalMember({
      email: "[email protected]",
      role: "member",
      expiresAt: now - 2 * DAY_MS,
      invitedBy: "admin-alice",
      createdAt: now - 30 * DAY_MS,
      updatedAt: now - 2 * DAY_MS,
    });
    const forgotten: any = await (await revoke(s.base, "[email protected]")).json();
    assert.deepEqual(forgotten, { ok: true, removed: true });
    assert.equal(s.built.identity.externalMember("[email protected]"), undefined);
    assert.ok((await s.built.auditLog.events()).some((e) => e.action === "external_user.forget"));
  } finally {
    await s.close();
  }
});

test("an address that already belongs to an org member cannot be invited", async () => {
  const s = start({ emailAuthDomain: "corp.example", emailAuthPrincipals: ["[email protected]"] });
  try {
    const expiresAt = Date.now() + DAY_MS;
    const alice = s.built.admin.resolveActor(ALICE)!;
    await s.built.admin.createGrant(alice, { principalId: "[email protected]", role: "org_admin", scopeId: ORG });
    await s.built.directory.replace([{ principalId: "[email protected]", displayName: "Dana", type: "internal" }]);
    const dm: TurnRequest = {
      surface: "test",
      actor: { externalId: "[email protected]" },
      conversation: { kind: "dm", threadRef: "dm:seen:t1" },
      text: "hello",
    };
    assert.equal((await s.built.app.turn(dm)).status, "ok");

    for (const email of [
      "[email protected]",
      "[email protected]",
      "[email protected]",
      "[email protected]",
      "[email protected]",
    ]) {
      const r = await invite(s.base, { email, expiresAt });
      assert.equal(r.status, 409, email);
      assert.match(((await r.json()) as any).message, /already belongs to a member.*Make admin.*Users page/s);
      assert.equal(s.built.identity.externalMember(email), undefined, email);
    }
    assert.equal(adminStatusFromGrants(await s.built.admin.listGrants(), "[email protected]").isAdmin, true);
    assert.deepEqual((await roster(s.base)).externalUsers, []);

    assert.equal((await invite(s.base, { email: "[email protected]", expiresAt })).status, 200);
    await s.built.admin.createGrant(alice, { principalId: "[email protected]", role: "org_admin", scopeId: ORG });
    const extend = await invite(s.base, { email: "[email protected]", expiresAt: expiresAt + DAY_MS });
    assert.equal(extend.status, 409, "a member record cannot silently carry an independently granted admin role");
    assert.match(((await extend.json()) as any).message, /holds an org admin grant/);
    const del = await revoke(s.base, "[email protected]");
    assert.equal(del.status, 409);
    assert.equal(adminStatusFromGrants(await s.built.admin.listGrants(), "[email protected]").isAdmin, true);
    assert.equal(s.built.identity.classify("[email protected]").type, "internal");

    const adopt: any = await (
      await invite(s.base, { email: "[email protected]", role: "org_admin", expiresAt: expiresAt + DAY_MS })
    ).json();
    assert.equal(adopt.member.role, "org_admin", "re-inviting with role org_admin adopts the grant");
    const grantAudits = (await s.built.auditLog.events()).filter((e) => e.resource === "[email protected]/org_admin");
    assert.deepEqual(grantAudits, [], "adopting an existing grant creates nothing");
    assert.equal((await revoke(s.base, "[email protected]")).status, 200);
    assert.equal(adminStatusFromGrants(await s.built.admin.listGrants(), "[email protected]").isAdmin, false);
  } finally {
    await s.close();
  }
});

test("an expired external org admin no longer passes the admin authorizer", async () => {
  const s = start();
  try {
    assert.equal(
      (await invite(s.base, { email: "[email protected]", role: "org_admin", expiresAt: Date.now() + DAY_MS }))
        .status,
      200,
    );
    const boss = { "x-admin-actor": "[email protected]@default-org" };
    assert.equal((await fetch(`${s.base}/v1/admin/users`, { headers: boss })).status, 200);
    const record = s.built.identity.externalMember("[email protected]")!;
    await s.built.identity.putExternalMember({ ...record, expiresAt: Date.now() - 1 });
    const denied = await fetch(`${s.base}/v1/admin/users`, { headers: boss });
    assert.equal(denied.status, 403);
    assert.match(((await denied.json()) as any).message, /no longer active/);
    const who: any = await (await fetch(`${s.base}/v1/admin/whoami`, { headers: boss })).json();
    assert.equal(who.isAdmin, false);
  } finally {
    await s.close();
  }
});

test("revoking the last org admin is refused and leaves the external record untouched", async () => {
  const s = start();
  try {
    const expiresAt = Date.now() + DAY_MS;
    assert.equal((await invite(s.base, { email: "[email protected]", role: "org_admin", expiresAt })).status, 200);
    const alice = s.built.admin.resolveActor(ALICE)!;
    await s.built.admin.revokeGrant(alice, "admin-bob", ORG, "org_admin");
    await s.built.admin.revokeGrant(alice, "admin-alice", ORG, "org_admin");

    const boss = { "x-admin-actor": "[email protected]@default-org" };
    const r = await revoke(s.base, "[email protected]", boss);
    assert.equal(r.status, 400);
    assert.match(((await r.json()) as any).message, /last org admin/);
    const kept = s.built.identity.externalMember("[email protected]");
    assert.equal(kept?.role, "org_admin");
    assert.equal(kept?.expiresAt, expiresAt);
    assert.ok(!(await s.built.auditLog.events()).some((e) => e.action === "external_user.revoke"));

    const demote = await invite(s.base, { email: "[email protected]", role: "member", expiresAt }, boss);
    assert.equal(demote.status, 400);
    assert.equal(s.built.identity.externalMember("[email protected]")?.role, "org_admin");
  } finally {
    await s.close();
  }
});

test("invite validation: past expiry, missing expiry, bad email, and bad role are 400", async () => {
  const s = start();
  try {
    const cases: unknown[] = [
      { email: "[email protected]", expiresAt: Date.now() - 1000 },
      { email: "[email protected]" },
      { email: "[email protected]", expiresAt: "not a date" },
      { email: "not-an-email", expiresAt: Date.now() + DAY_MS },
      { email: "[email protected]", role: "owner", expiresAt: Date.now() + DAY_MS },
    ];
    for (const body of cases) {
      const r = await invite(s.base, body);
      assert.equal(r.status, 400, JSON.stringify(body));
      assert.equal(((await r.json()) as any).error, "bad_request");
    }
    assert.deepEqual((await roster(s.base)).externalUsers, []);

    const dated: any = await (await invite(s.base, { email: "[email protected]", expiresAt: "2031-03-04" })).json();
    assert.equal(dated.member.expiresAt, Date.UTC(2031, 2, 4, 23, 59, 59, 999), "a bare date ends that day, UTC");
  } finally {
    await s.close();
  }
});

test("with a mailer the invitation goes out carrying the sign-in URL and brand; a send failure still saves the record", async () => {
  const ok = stubMailer();
  const s = start({ mailer: ok.mailer });
  try {
    const expiresAt = Date.now() + 14 * DAY_MS;
    const r: any = await (await invite(s.base, { email: "[email protected]", expiresAt })).json();
    assert.equal(r.emailSent, true);
    assert.equal(r.emailProblem, undefined);
    assert.equal(ok.sent.length, 1);
    assert.equal(ok.sent[0]!.to, "[email protected]");
    assert.match(ok.sent[0]!.subject, /invited to Acme Bot/);
    assert.ok(ok.sent[0]!.text.includes(`${PORTAL}/auth/login`));
    assert.match(ok.sent[0]!.text, /admin-alice/);
    assert.match(ok.sent[0]!.html, /Acme Bot/);
    assert.equal((await roster(s.base)).inviteEmail.configured, true);

    const resent: any = await (
      await invite(s.base, { email: "[email protected]", expiresAt, resendInvite: true })
    ).json();
    assert.equal(resent.emailSent, true);
    assert.equal(ok.sent.length, 2);

    assert.equal((await revoke(s.base, "[email protected]")).status, 200);
    const readmit: any = await (await invite(s.base, { email: "[email protected]", expiresAt })).json();
    assert.equal(readmit.created, false);
    assert.equal(readmit.emailSent, true, "readmitting a revoked address sends the invitation again");
    assert.equal(ok.sent.length, 3);
    const extend: any = await (
      await invite(s.base, { email: "[email protected]", expiresAt: expiresAt + DAY_MS })
    ).json();
    assert.equal(extend.emailSent, false, "extending an active member sends nothing");
    assert.equal(ok.sent.length, 3);
  } finally {
    await s.close();
  }

  const broken = stubMailer("Resend rejected the message: HTTP 403 domain not verified");
  const f = start({ mailer: broken.mailer });
  try {
    const r: any = await (
      await invite(f.base, { email: "[email protected]", expiresAt: Date.now() + DAY_MS })
    ).json();
    assert.equal(r.ok, true);
    assert.equal(r.emailSent, false);
    assert.match(r.emailProblem, /HTTP 403 domain not verified/);
    assert.equal(r.signInUrl, `${PORTAL}/auth/login`);
    assert.equal(f.built.identity.externalMember("[email protected]")?.role, "member");
  } finally {
    await f.close();
  }
});

test("renderInviteEmail escapes HTML and names the sign-in URL and expiry", () => {
  const mail = renderInviteEmail({
    to: "[email protected]",
    brandName: "<Acme>",
    invitedBy: "admin-alice",
    signInUrl: "https://portal.example.com/auth/login",
    expiresAt: Date.UTC(2030, 0, 2),
  });
  assert.equal(mail.subject, "You've been invited to <Acme>");
  assert.match(mail.html, /&lt;Acme&gt;/);
  assert.doesNotMatch(mail.html, /<Acme>/);
  assert.match(mail.text, /https:\/\/portal\.example\.com\/auth\/login/);
  assert.match(mail.text, /02 Jan 2030/);
});

test("an agent token may invite a member but never grant, demote, or revoke an org_admin external", async () => {
  const s = start({ signed: true });
  try {
    const cap = { "x-agent-capability": await capFor("admin-alice") };
    const expiresAt = Date.now() + DAY_MS;

    const member = await invite(s.base, { email: "[email protected]", expiresAt }, cap);
    assert.equal(member.status, 200);
    assert.equal(((await member.json()) as any).member.invitedBy, "admin-alice");

    const promote = await invite(s.base, { email: "[email protected]", role: "org_admin", expiresAt }, cap);
    assert.equal(promote.status, 403);
    assert.match(((await promote.json()) as any).message, /portal-only/);
    assert.equal(s.built.identity.externalMember("[email protected]"), undefined);

    const now = Date.now();
    await s.built.identity.putExternalMember({
      email: "[email protected]",
      role: "org_admin",
      expiresAt,
      invitedBy: "admin-bob",
      createdAt: now,
      updatedAt: now,
    });
    const demote = await invite(s.base, { email: "[email protected]", role: "member", expiresAt }, cap);
    assert.equal(demote.status, 403);
    const del = await revoke(s.base, "[email protected]", cap);
    assert.equal(del.status, 403);
    assert.match(((await del.json()) as any).message, /portal-only/);
    assert.equal(s.built.identity.externalMember("[email protected]")?.role, "org_admin");

    assert.equal((await revoke(s.base, "[email protected]", cap)).status, 200);
    assert.equal(s.built.identity.classify("[email protected]").type, "guest");

    const alice = s.built.admin.resolveActor(ALICE)!;
    await s.built.admin.createGrant(alice, { principalId: "[email protected]", role: "org_admin", scopeId: ORG });
    const demoteViaInvite = await invite(s.base, { email: "[email protected]", role: "member", expiresAt }, cap);
    assert.equal(demoteViaInvite.status, 409);
    assert.equal(adminStatusFromGrants(await s.built.admin.listGrants(), "[email protected]").isAdmin, true);
    assert.equal(s.built.identity.externalMember("[email protected]"), undefined);

    await s.built.identity.putExternalMember({
      email: "[email protected]",
      role: "member",
      expiresAt,
      invitedBy: "admin-bob",
      createdAt: now,
      updatedAt: now,
    });
    await s.built.admin.createGrant(alice, {
      principalId: "[email protected]",
      role: "org_admin",
      scopeId: ORG,
    });
    for (const attempt of [
      revoke(s.base, "[email protected]", cap),
      invite(s.base, { email: "[email protected]", role: "member", expiresAt: expiresAt + DAY_MS }, cap),
    ]) {
      const r = await attempt;
      assert.equal(r.status, 403);
      assert.match(((await r.json()) as any).message, /portal-only/);
    }
    assert.equal(s.built.identity.classify("[email protected]").type, "internal");
    assert.equal(adminStatusFromGrants(await s.built.admin.listGrants(), "[email protected]").isAdmin, true);

    assert.equal((await invite(s.base, { email: "[email protected]", expiresAt }, { "x-admin-actor": ALICE })).status, 401);
  } finally {
    await s.close();
  }
});

test("expiry classifies an external as guest, and the signed broker check answers accordingly", async () => {
  const s = start({ signed: true });
  try {
    const now = Date.now();
    const record = (email: string, expiresAt: number) => ({
      email,
      role: "member" as const,
      expiresAt,
      invitedBy: "admin-alice",
      createdAt: now,
      updatedAt: now,
    });
    await s.built.identity.putExternalMember(record("[email protected]", now + DAY_MS));
    await s.built.identity.putExternalMember(record("[email protected]", now - 1));
    assert.equal(s.built.identity.classify("[email protected]").type, "internal");
    assert.equal(s.built.identity.classify("[email protected]").type, "guest");

    assert.equal(await coreEmailAllowed(s.base, SECRET, "[email protected]", "test"), true);
    assert.equal(await coreEmailAllowed(s.base, SECRET, "[email protected]", "test"), false);
    assert.equal(await coreEmailAllowed(s.base, SECRET, "[email protected]", "test"), false);
    assert.equal(await coreEmailAllowed(s.base, "wrong-secret".repeat(4), "[email protected]", "test"), false);
    assert.equal(await coreEmailAllowed("http://127.0.0.1:1", SECRET, "[email protected]", "test"), false);
    const unsigned = await fetch(`${s.base}/v1/auth/broker/email-allowed?email=live%40partner.example`);
    assert.equal(unsigned.status, 401);

    await s.built.identity.deactivate("[email protected]");
    assert.equal(await coreEmailAllowed(s.base, SECRET, "[email protected]", "test"), false);
    await s.built.identity.reactivate("[email protected]");

    const list = await roster(s.base, { "x-agent-capability": await capFor("admin-alice") });
    assert.deepEqual(
      list.externalUsers.map((m: any) => [m.email, m.status]),
      [
        ["[email protected]", "active"],
        ["[email protected]", "expired"],
      ],
    );
  } finally {
    await s.close();
  }
});

test("the directory resolves participants without loading their session windows", async (t) => {
  const s = start();
  t.after(s.close);
  for (const thread of ["first", "second"]) {
    const session = await s.built.sessions.getOrCreateByThread(thread, "dm", "personal:alice");
    await s.built.sessions.addParticipant(session.id, "alice");
    await s.built.sessions.addParticipant(session.id, "inactive");
  }
  await s.built.identity.deactivate("inactive");
  t.mock.method(s.built.sessions, "listParticipants", () => {
    throw new Error("Directory lookup must not load participant windows");
  });
  const alice = { principalId: "alice", displayName: "alice", type: "internal" };
  assert.deepEqual(await s.built.app.directoryMembers(), [alice]);
  assert.deepEqual(await s.built.app.directoryMember("alice"), alice);
  assert.deepEqual(await s.built.app.resolveRecipient("alice"), { kind: "one", member: alice });
  assert.equal(await s.built.app.directoryMember("inactive"), null);
});

test("the directory resolves an active external member and not an expired one", async () => {
  const s = start();
  try {
    const now = Date.now();
    const record = (email: string, expiresAt: number) => ({
      email,
      role: "member" as const,
      expiresAt,
      invitedBy: "admin-alice",
      createdAt: now,
      updatedAt: now,
    });
    await s.built.identity.putExternalMember(record("[email protected]", now + DAY_MS));
    await s.built.identity.putExternalMember(record("[email protected]", now - 1));

    assert.deepEqual(await s.built.app.directoryMember("[email protected]"), {
      principalId: "[email protected]",
      displayName: "[email protected]",
      type: "internal",
    });
    assert.equal(await s.built.app.directoryMember("[email protected]"), null);
    assert.equal((await s.built.app.resolveRecipient("[email protected]")).kind, "one");
    assert.equal((await s.built.app.resolveRecipient("[email protected]")).kind, "none");
    assert.ok((await s.built.app.directoryMembers()).some((m) => m.principalId === "[email protected]"));
    assert.ok(!(await s.built.app.directoryMembers()).some((m) => m.principalId === "[email protected]"));

    const hit = await fetch(`${s.base}/v1/admin/directory?q=${encodeURIComponent("[email protected]")}`, {
      headers: { "x-admin-actor": ALICE },
    });
    assert.deepEqual(((await hit.json()) as any).members, [
      { principalId: "[email protected]", displayName: "[email protected]" },
    ]);
  } finally {
    await s.close();
  }
});

test("directory sync never deactivates an external member; manual deactivation still wins", async () => {
  const s = start();
  try {
    const now = Date.now();
    await s.built.identity.putExternalMember({
      email: "[email protected]",
      role: "member",
      expiresAt: now + DAY_MS,
      invitedBy: "admin-alice",
      createdAt: now,
      updatedAt: now,
    });
    const outcome = await s.built.identity.recordDirectorySync(["[email protected]"], []);
    assert.deepEqual(outcome.deactivated, []);
    assert.equal(s.built.identity.classify("[email protected]").type, "internal");
    await s.built.identity.deactivate("[email protected]");
    assert.equal(s.built.identity.classify("[email protected]").type, "guest");
  } finally {
    await s.close();
  }
});

const inviteTeammate = (base: string, body: unknown, headers: Record<string, string> = { "x-admin-actor": ALICE }) =>
  fetch(`${base}/v1/admin/users/invite`, {
    method: "POST",
    headers: { ...headers, "content-type": "application/json" },
    body: JSON.stringify(body),
  });

test("teammates receive permanent access and appear before their first session", async () => {
  const { sent, mailer } = stubMailer();
  const s = start({ mailer, emailAuthDomain: "corp.example" });
  try {
    const r = await inviteTeammate(s.base, { email: " [email protected] " });
    assert.equal(r.status, 200);
    const d: any = await r.json();
    assert.equal(d.member.expiresAt, null);
    assert.equal(d.member.kind, "teammate");
    assert.equal(d.emailSent, true);
    assert.ok(sent[0]);
    assert.match(sent[0].text, /no expiration/);
    assert.doesNotMatch(sent[0].html, /Invalid Date/);
    const list = await roster(s.base);
    assert.ok(list.users.some((u: any) => u.principalId === "[email protected]" && u.sessionCount === 0));
    assert.equal(list.externalUsers[0].status, "active");
    assert.equal(await coreEmailAllowed(s.base, undefined, "[email protected]", "test"), true);
    assert.equal((await inviteTeammate(s.base, { email: "[email protected]" })).status, 200);
    assert.equal(sent.length, 2);
    assert.equal((await roster(s.base)).externalUsers.length, 1);
    assert.equal((await revoke(s.base, "[email protected]")).status, 200);
    assert.equal(await coreEmailAllowed(s.base, undefined, "[email protected]", "test"), false);
    assert.equal(s.built.identity.classify("[email protected]").type, "guest");
  } finally {
    await s.close();
  }
});

test("teammate invitations validate expiry, require admin, and do not demote existing admins", async () => {
  const s = start();
  try {
    assert.equal((await inviteTeammate(s.base, { email: "[email protected]" }, { "x-admin-actor": NOBODY })).status, 403);
    assert.equal((await inviteTeammate(s.base, { email: "invalid" })).status, 400);
    assert.equal(
      (await inviteTeammate(s.base, { email: "[email protected]", expiresAt: Date.now() - DAY_MS })).status,
      400,
    );
    const first = await inviteTeammate(s.base, { email: "[email protected]", role: "org_admin" });
    assert.equal(first.status, 200);
    const again = await inviteTeammate(s.base, { email: "[email protected]", role: "member" });
    assert.equal(again.status, 200);
    assert.equal(((await again.json()) as any).member.role, "org_admin");
    assert.equal(adminStatusFromGrants(await s.built.admin.listGrants(), "[email protected]").isAdmin, true);
    assert.equal((await invite(s.base, { email: "[email protected]", expiresAt: Date.now() + DAY_MS })).status, 400);
  } finally {
    await s.close();
  }
});

test("agent capabilities cannot create permanent teammate access", async () => {
  const s = start({ signed: true });
  try {
    const r = await inviteTeammate(
      s.base,
      { email: "[email protected]" },
      { "x-agent-capability": await capFor("admin-alice") },
    );
    assert.equal(r.status, 403);
    assert.equal(s.built.identity.externalMember("[email protected]"), undefined);
  } finally {
    await s.close();
  }
});

test("invitation links work without email delivery and are single use, scoped, and revocable", async () => {
  const s = start();
  const redeem = (token: string) =>
    fetch(`${s.base}/v1/auth/invitations/redeem`, {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({ token }),
    });
  const issue = async () => {
    const r = await inviteTeammate(s.base, { email: "[email protected]" });
    assert.equal(r.status, 200);
    const data: any = await r.json();
    assert.equal(data.emailSent, false);
    const url = new URL(data.signInUrl);
    assert.equal(url.pathname, "/auth/invite");
    return new URLSearchParams(url.hash.slice(1)).get("token")!;
  };
  try {
    const token = await issue();
    const claims: any = await verifySignedPayload(token, SECRET);
    assert.equal((await redeem(token + "tampered")).status, 400);
    assert.equal((await redeem(await mintSignedPayload({ ...claims, exp: Date.now() - 1 }, SECRET))).status, 400);
    assert.equal((await redeem(await mintSignedPayload({ ...claims, org: "org:other" }, SECRET))).status, 400);
    assert.equal(
      (await redeem(await mintSignedPayload({ ...claims, aud: "https://elsewhere.example" }, SECRET))).status,
      400,
    );
    const results = await Promise.all([redeem(token), redeem(token)]);
    assert.deepEqual(results.map((r) => r.status).sort(), [200, 400]);
    const accepted: any = await results.find((r) => r.status === 200)!.json();
    assert.equal(accepted.email, "[email protected]");
    const revoked = await issue();
    assert.equal((await revoke(s.base, "[email protected]")).status, 200);
    assert.equal((await redeem(revoked)).status, 403);
    await issue();
    assert.equal((await redeem(revoked)).status, 403);
  } finally {
    await s.close();
  }
});

test("invitation redemption requires source authentication before consuming a valid invitation", async () => {
  const s = start({ signed: true });
  try {
    const now = Date.now();
    const inviteId = randomUUID();
    const email = "[email protected]";
    await s.built.identity.putExternalMember({
      email,
      role: "member",
      expiresAt: null,
      kind: "teammate",
      inviteId,
      invitedBy: "admin-alice",
      createdAt: now,
      updatedAt: now,
    });
    const token = await mintSignedPayload(
      {
        purpose: "teammate-invite",
        org: ORG,
        aud: PORTAL,
        email,
        inviteId,
        iat: now,
        exp: now + DAY_MS,
        jti: randomUUID(),
      },
      SECRET,
    );
    const body = JSON.stringify({ token });
    const path = withSourceAuthNonce("/v1/auth/invitations/redeem", SECRET);
    const unsigned = await fetch(`${s.base}${path}`, {
      method: "POST",
      headers: { "content-type": "application/json" },
      body,
    });
    assert.equal(unsigned.status, 401);
    const signed = await fetch(`${s.base}${path}`, {
      method: "POST",
      headers: signedHeaders(SECRET, "POST", path, body),
      body,
    });
    assert.equal(signed.status, 200);
    assert.deepEqual(await signed.json(), { email });
    const reusedPath = withSourceAuthNonce("/v1/auth/invitations/redeem", SECRET);
    const reused = await fetch(`${s.base}${reusedPath}`, {
      method: "POST",
      headers: signedHeaders(SECRET, "POST", reusedPath, body),
      body,
    });
    assert.equal(reused.status, 400);
    assert.deepEqual(await reused.json(), { error: "invitation_used" });
  } finally {
    await s.close();
  }
});

test("teammate invites reject manual deactivation before changing access or sending mail", async () => {
  const { sent, mailer } = stubMailer();
  const s = start({ mailer });
  const email = "[email protected]";
  try {
    await s.built.identity.deactivate(email);
    const denied = await inviteTeammate(s.base, { email, role: "org_admin" });
    assert.equal(denied.status, 409);
    assert.match(((await denied.json()) as any).message, /Reactivate/);
    assert.equal(s.built.identity.externalMember(email), undefined);
    assert.equal(adminStatusFromGrants(await s.built.admin.listGrants(), email).isAdmin, false);
    assert.equal(sent.length, 0);
    assert.equal(s.built.identity.classify(email).type, "guest");

    await s.built.identity.reactivate(email);
    assert.equal((await inviteTeammate(s.base, { email })).status, 200);
    assert.equal((await revoke(s.base, email)).status, 200);
    assert.equal((await inviteTeammate(s.base, { email })).status, 200);
    assert.equal(s.built.identity.classify(email).type, "internal");
    assert.equal(sent.length, 2);
  } finally {
    await s.close();
  }
});