@app-vitals/agent-admin 1.108.0 → 1.109.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/Dockerfile +4 -0
  2. package/account-move.example.json +22 -0
  3. package/e2e/real/admin-access.real.e2e.ts +77 -68
  4. package/e2e/real/agent-management.real.e2e.ts +165 -143
  5. package/e2e/real/owner-access.real.e2e.ts +228 -212
  6. package/openapi.platform.json +11 -6
  7. package/package.json +2 -2
  8. package/src/account-agent-create.ts +18 -9
  9. package/src/account-agent-create.unit.test.ts +12 -14
  10. package/src/account-created-notifier.ts +1 -1
  11. package/src/account-move.integration.test.ts +594 -0
  12. package/src/account-move.ts +867 -0
  13. package/src/account-move.unit.test.ts +206 -0
  14. package/src/account-onboarding.integration.test.ts +69 -0
  15. package/src/account-onboarding.ts +29 -3
  16. package/src/account-trial-expiry-sweeper.ts +1 -1
  17. package/src/accounts-api.integration.test.ts +5 -2
  18. package/src/accounts-api.smoke.test.ts +21 -5
  19. package/src/accounts-api.ts +12 -15
  20. package/src/admin-boot-config.ts +2 -0
  21. package/src/admin-boot-config.unit.test.ts +27 -0
  22. package/src/admin-boot.ts +11 -10
  23. package/src/admin-local-agent.smoke.test.ts +65 -0
  24. package/src/admin-ui-account-pages.ts +2 -3
  25. package/src/admin-ui-account-pages.unit.test.ts +5 -3
  26. package/src/admin-ui-account.smoke.test.ts +7 -3
  27. package/src/admin-ui-account.ts +10 -15
  28. package/src/admin-ui-accounts.smoke.test.ts +4 -4
  29. package/src/admin-ui-accounts.ts +7 -11
  30. package/src/admin-ui-pages.ts +16 -5
  31. package/src/admin-ui-pages.unit.test.ts +25 -20
  32. package/src/admin-ui-settings-nav-context.smoke.test.ts +24 -0
  33. package/src/admin-ui-settings.smoke.test.ts +17 -1
  34. package/src/admin-ui.smoke.test.ts +167 -2
  35. package/src/admin-ui.ts +75 -50
  36. package/src/agent-management-access.integration.test.ts +18 -6
  37. package/src/agent-manifest.ts +2 -1
  38. package/src/agent-provisioner.integration.test.ts +10 -10
  39. package/src/agent-provisioner.ts +10 -1
  40. package/src/agent-provisioner.unit.test.ts +110 -36
  41. package/src/agent-templates-api.integration.test.ts +23 -5
  42. package/src/agent-templates-api.ts +11 -8
  43. package/src/agents-api.smoke.test.ts +106 -8
  44. package/src/agents-api.ts +5 -3
  45. package/src/agents.integration.test.ts +112 -2
  46. package/src/agents.ts +37 -6
  47. package/src/caller-scope.integration.test.ts +8 -6
  48. package/src/caller-scope.ts +32 -26
  49. package/src/caller-scope.unit.test.ts +15 -11
  50. package/src/github-enterprise-isolation.unit.test.ts +17 -0
  51. package/src/move-accounts.ts +201 -0
  52. package/src/openapi-schemas.ts +6 -0
  53. package/src/platform-routes.ts +2 -2
  54. package/src/self-serve-config.ts +8 -8
  55. package/src/self-serve-config.unit.test.ts +3 -10
@@ -88,9 +88,11 @@ describe("renderAccountPage", () => {
88
88
  });
89
89
 
90
90
  describe("renderZeroQuotaNotice", () => {
91
- test("shows request-a-trial message with escaped contact address", () => {
92
- const html = renderZeroQuotaNotice("dan@app-vitals.com");
93
- expect(html).toContain("Email dan@app-vitals.com to request a trial");
91
+ test("shows a neutral limit message with no contact address", () => {
92
+ const html = renderZeroQuotaNotice();
93
+ expect(html).toContain("This account has reached its agent limit.");
94
+ expect(html).not.toContain("trial");
95
+ expect(html).not.toContain("mailto:");
94
96
  });
95
97
  });
96
98
 
@@ -153,12 +153,16 @@ function post(
153
153
  }
154
154
 
155
155
  describe("/admin/account", () => {
156
- it("returns 404 for every route when the flag is off", async () => {
156
+ it("serves the page and invites with the self-serve flag off", async () => {
157
157
  const { app } = setup({ enabled: false });
158
- expect((await app.request("/admin/account")).status).toBe(404);
158
+ const res = await app.request("/admin/account", {
159
+ headers: { "x-test-user-email": "owner@x.com" },
160
+ });
161
+ expect(res.status).toBe(200);
162
+ expect(await res.text()).toContain("Acme");
159
163
  expect(
160
164
  (await post(app, "/admin/account/invite", { email: "a@b.co" })).status,
161
- ).toBe(404);
165
+ ).not.toBe(404);
162
166
  });
163
167
 
164
168
  it("returns 404 for a signed-in user without an account", async () => {
@@ -1,11 +1,11 @@
1
1
  /**
2
2
  * admin/src/admin-ui-account.ts
3
- * GET/POST /admin/account — self-serve account management (SSP-3.2): name,
4
- * quota, members, pending invites. Owners manage; members are read-only.
3
+ * GET/POST /admin/account — account management (SSP-3.2): name, quota,
4
+ * members, pending invites. Owners manage; members are read-only.
5
5
  *
6
- * Registered into admin-ui.ts's app via registerAccountRoutes(). Flag-gated:
7
- * every route 404s when self-serve is off, and for signed-in users with no
8
- * account.
6
+ * Registered into admin-ui.ts's app via registerAccountRoutes(). Every route
7
+ * 404s for signed-in users with no account. Independent of the self-serve
8
+ * setting.
9
9
  */
10
10
 
11
11
  import type { Context, Hono, MiddlewareHandler } from "hono";
@@ -22,8 +22,11 @@ import type { AdminUIEnv } from "./admin-ui.ts";
22
22
  import type { SettingsNav } from "./admin-ui-settings-layout.ts";
23
23
 
24
24
  export interface AccountRouteDeps {
25
- /** SHIPWRIGHT_SELF_SERVE_ENABLED === "enabled". */
26
- enabled: boolean;
25
+ /**
26
+ * @deprecated Ignored: these routes no longer depend on the self-serve
27
+ * setting. Kept optional so existing callers keep compiling.
28
+ */
29
+ enabled?: boolean;
27
30
  requireAuth: MiddlewareHandler<AdminUIEnv>;
28
31
  accounts: Pick<AccountService, "getByMemberEmail" | "update" | "countAgents">;
29
32
  members: Pick<
@@ -45,14 +48,6 @@ export function registerAccountRoutes(
45
48
  ): void {
46
49
  const notFound = () => new Response("Not Found", { status: 404 });
47
50
 
48
- // Flag-off 404s before auth so the route is indistinguishable from absent.
49
- app.use(`${ACCOUNT_PATH}`, async (_c, next) =>
50
- deps.enabled ? next() : notFound(),
51
- );
52
- app.use(`${ACCOUNT_PATH}/*`, async (_c, next) =>
53
- deps.enabled ? next() : notFound(),
54
- );
55
-
56
51
  async function render(
57
52
  c: Context<AdminUIEnv>,
58
53
  account: Account,
@@ -279,15 +279,15 @@ describe("/admin/accounts", () => {
279
279
  }
280
280
  });
281
281
 
282
- it("404 everywhere when flag is off, even for admin", async () => {
282
+ it("serves every route to admins with the self-serve flag off", async () => {
283
283
  const { app } = setup({ enabled: false });
284
284
  expect(
285
285
  (await app.request("/admin/accounts", { headers: ADMIN })).status,
286
- ).toBe(404);
286
+ ).toBe(200);
287
287
  expect(
288
288
  (await app.request("/admin/accounts/a1", { headers: ADMIN })).status,
289
- ).toBe(404);
290
- expect((await post(app, "/admin/accounts/a1/suspend", {})).status).toBe(
289
+ ).toBe(200);
290
+ expect((await post(app, "/admin/accounts/a1/suspend", {})).status).not.toBe(
291
291
  404,
292
292
  );
293
293
  });
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * admin/src/admin-ui-accounts.ts
3
3
  * GET/POST /admin/accounts[/:id] — platform-admin account management
4
- * (SSP-5.2). Admin only (403 otherwise) and flag-gated (404 when self-serve
5
- * is off). Status and trial edits go through AccountLifecycle (SSP-8.2), so
4
+ * (SSP-5.2). Admin only (403 otherwise), independent of the self-serve
5
+ * setting. Status and trial edits go through AccountLifecycle (SSP-8.2), so
6
6
  * suspend locks down the account's crons and reactivate restores them.
7
7
  */
8
8
 
@@ -30,8 +30,11 @@ import type { AgentService } from "./agents.ts";
30
30
  import { UnprocessableEntityError } from "./errors.ts";
31
31
 
32
32
  export interface AdminAccountsRouteDeps {
33
- /** SHIPWRIGHT_SELF_SERVE_ENABLED === "enabled". */
34
- enabled: boolean;
33
+ /**
34
+ * @deprecated Ignored: these routes no longer depend on the self-serve
35
+ * setting. Kept optional so existing callers keep compiling.
36
+ */
37
+ enabled?: boolean;
35
38
  requireAuth: MiddlewareHandler<AdminUIEnv>;
36
39
  accounts: Pick<
37
40
  AccountService,
@@ -62,13 +65,6 @@ export function registerAdminAccountsRoutes(
62
65
  const notFound = () => new Response("Not Found", { status: 404 });
63
66
  const forbidden = () => new Response("Forbidden", { status: 403 });
64
67
 
65
- app.use(ADMIN_ACCOUNTS_PATH, async (_c, next) =>
66
- deps.enabled ? next() : notFound(),
67
- );
68
- app.use(`${ADMIN_ACCOUNTS_PATH}/*`, async (_c, next) =>
69
- deps.enabled ? next() : notFound(),
70
- );
71
-
72
68
  const requireAdmin: MiddlewareHandler<AdminUIEnv> = async (c, next) =>
73
69
  c.var.isAdmin === true ? next() : forbidden();
74
70
 
@@ -11,7 +11,7 @@
11
11
  */
12
12
 
13
13
  import { PROGRESS_LABELS } from "@app-vitals/agent-lib/progress-phases";
14
- import { getSelfServeSpecialty } from "./account-agent-create.ts";
14
+ import { getOwnerAgentSpecialty } from "./account-agent-create.ts";
15
15
  import {
16
16
  renderGithubAccessCard,
17
17
  renderGithubInstallCard,
@@ -579,7 +579,9 @@ export function renderNewLocalAgentPage(
579
579
  * remaining agent quota and the prerequisites, and pins the type to
580
580
  * the self-serve specialty (the server forces it regardless).
581
581
  */
582
- selfServe?: { remaining: number; max: number; contactEmail: string };
582
+ selfServe?: { remaining: number; max: number };
583
+ /** Platform admins: accounts to pick from when more than one exists. */
584
+ accountChoices?: { id: string; name: string }[];
583
585
  /**
584
586
  * ATP-2.4: present when agent templates are available. Lists the
585
587
  * template-provided defaults and adds the opt-out checkbox; `allowEnv`
@@ -648,8 +650,7 @@ export function renderNewLocalAgentPage(
648
650
  ${
649
651
  selfServe.remaining <= 0
650
652
  ? `<p style="font-size:13px;color:var(--vos-danger-text);margin-bottom:8px">
651
- This account has reached its agent limit. Delete an agent to free a slot,
652
- or contact <a href="mailto:${escapeHtml(selfServe.contactEmail)}">${escapeHtml(selfServe.contactEmail)}</a> for more.
653
+ This account has reached its agent limit. Delete an agent to free a slot.
653
654
  </p>`
654
655
  : ""
655
656
  }
@@ -701,8 +702,17 @@ export function renderNewLocalAgentPage(
701
702
  </div>
702
703
  </fieldset>`
703
704
  : "";
705
+ const accountFieldHtml = opts?.accountChoices
706
+ ? `<div class="form-group">
707
+ <label class="form-label" for="accountId">Account <span style="color:#ef4444">*</span></label>
708
+ <select id="accountId" name="accountId" class="form-input" required>
709
+ <option value="">Choose an account</option>
710
+ ${opts.accountChoices.map((a) => `<option value="${escapeHtml(a.id)}">${escapeHtml(a.name)}</option>`).join("\n")}
711
+ </select>
712
+ </div>`
713
+ : "";
704
714
  const typeFieldHtml = selfServe
705
- ? `<input type="hidden" name="type" value="${escapeHtml(getSelfServeSpecialty())}" />`
715
+ ? `<input type="hidden" name="type" value="${escapeHtml(getOwnerAgentSpecialty())}" />`
706
716
  : `<div class="form-group">
707
717
  <label class="form-label" for="type">Specialty <span style="color:#ef4444">*</span></label>
708
718
  <select id="type" name="type" class="form-input" required>
@@ -742,6 +752,7 @@ export function renderNewLocalAgentPage(
742
752
  />
743
753
  </div>
744
754
  ${typeFieldHtml}
755
+ ${accountFieldHtml}
745
756
  <fieldset style="border:1px solid var(--vos-border);border-radius:8px;padding:16px">
746
757
  <legend style="font-size:13px;font-weight:600;padding:0 8px">Runtime</legend>
747
758
  <div class="form-group" style="margin-bottom:0">
@@ -4333,7 +4333,7 @@ describe("renderAgentDetailPage — canManage gating (SSP-4.3)", () => {
4333
4333
 
4334
4334
  describe("renderNewLocalAgentPage — self-serve account user (SSP-4.2)", () => {
4335
4335
  const CODING_TYPE = { name: "coding", displayName: "Coding Agent" };
4336
- const selfServe = { remaining: 2, max: 3, contactEmail: "help@example.com" };
4336
+ const selfServe = { remaining: 2, max: 3 };
4337
4337
 
4338
4338
  test("shows remaining quota and the credential prerequisites", () => {
4339
4339
  const html = renderNewLocalAgentPage(USER_NAME, [CODING_TYPE], {
@@ -4355,31 +4355,22 @@ describe("renderNewLocalAgentPage — self-serve account user (SSP-4.2)", () =>
4355
4355
  });
4356
4356
 
4357
4357
  describe("hidden type input", () => {
4358
- const saved = {
4359
- selfServe: process.env.SHIPWRIGHT_SELF_SERVE_AGENT_TYPE,
4360
- defaultType: process.env.SHIPWRIGHT_DEFAULT_AGENT_TYPE,
4361
- };
4362
- const restore = (key: string, value: string | undefined) => {
4363
- if (value === undefined) delete process.env[key];
4364
- else process.env[key] = value;
4365
- };
4358
+ const saved = process.env.SHIPWRIGHT_DEFAULT_AGENT_TYPE;
4366
4359
  afterEach(() => {
4367
- restore("SHIPWRIGHT_SELF_SERVE_AGENT_TYPE", saved.selfServe);
4368
- restore("SHIPWRIGHT_DEFAULT_AGENT_TYPE", saved.defaultType);
4360
+ if (saved === undefined) delete process.env.SHIPWRIGHT_DEFAULT_AGENT_TYPE;
4361
+ else process.env.SHIPWRIGHT_DEFAULT_AGENT_TYPE = saved;
4369
4362
  });
4370
4363
 
4371
- test("carries the SHIPWRIGHT_SELF_SERVE_AGENT_TYPE value instead of a type select", () => {
4372
- process.env.SHIPWRIGHT_SELF_SERVE_AGENT_TYPE = "base";
4364
+ test("carries the deployment default type instead of a type select", () => {
4365
+ process.env.SHIPWRIGHT_DEFAULT_AGENT_TYPE = "base";
4373
4366
  const html = renderNewLocalAgentPage(USER_NAME, [CODING_TYPE], {
4374
4367
  selfServe,
4375
4368
  });
4376
4369
  expect(html).toContain('<input type="hidden" name="type" value="base"');
4377
- expect(html).not.toContain('name="type" value="coding"');
4378
4370
  expect(html).not.toMatch(/<select[^>]*name="type"/);
4379
4371
  });
4380
4372
 
4381
- test("falls back to the resolved default specialty when the env is unset", () => {
4382
- delete process.env.SHIPWRIGHT_SELF_SERVE_AGENT_TYPE;
4373
+ test("falls back to specialist when the default is unset", () => {
4383
4374
  delete process.env.SHIPWRIGHT_DEFAULT_AGENT_TYPE;
4384
4375
  const html = renderNewLocalAgentPage(USER_NAME, [CODING_TYPE], {
4385
4376
  selfServe,
@@ -4390,12 +4381,26 @@ describe("renderNewLocalAgentPage — self-serve account user (SSP-4.2)", () =>
4390
4381
  });
4391
4382
  });
4392
4383
 
4393
- test("at zero remaining, explains the limit and points at the contact email", () => {
4384
+ test("at zero remaining, explains the limit without a contact address", () => {
4394
4385
  const html = renderNewLocalAgentPage(USER_NAME, [CODING_TYPE], {
4395
4386
  selfServe: { ...selfServe, remaining: 0 },
4396
4387
  });
4397
4388
  expect(html).toContain("agent limit");
4398
- expect(html).toContain("help@example.com");
4389
+ expect(html).not.toContain("mailto:");
4390
+ });
4391
+
4392
+ test("admin form offers an account select only when choices are given", () => {
4393
+ const withChoices = renderNewLocalAgentPage(USER_NAME, [CODING_TYPE], {
4394
+ accountChoices: [
4395
+ { id: "a1", name: "Alpha" },
4396
+ { id: "a2", name: "Beta" },
4397
+ ],
4398
+ });
4399
+ expect(withChoices).toMatch(/<select[^>]*name="accountId"/);
4400
+ expect(withChoices).toContain('value="a2">Beta');
4401
+ expect(renderNewLocalAgentPage(USER_NAME, [CODING_TYPE])).not.toContain(
4402
+ 'name="accountId"',
4403
+ );
4399
4404
  });
4400
4405
 
4401
4406
  test("admin form (no selfServe opt) is unchanged", () => {
@@ -4408,9 +4413,9 @@ describe("renderNewLocalAgentPage — self-serve account user (SSP-4.2)", () =>
4408
4413
  describe("renderAgentsPage zero-quota notice (SSP-3.2)", () => {
4409
4414
  test("replaces the create CTA with the notice and a disabled button", () => {
4410
4415
  const html = renderAgentsPage([], "u@x.com", true, "UTC", {
4411
- zeroQuotaNotice: renderZeroQuotaNotice("dan@app-vitals.com"),
4416
+ zeroQuotaNotice: renderZeroQuotaNotice(),
4412
4417
  });
4413
- expect(html).toContain("Email dan@app-vitals.com to request a trial");
4418
+ expect(html).toContain("This account has reached its agent limit.");
4414
4419
  expect(html).toContain("disabled");
4415
4420
  expect(html).not.toContain(
4416
4421
  '<a href="/admin/agents/new" class="btn btn-primary">',
@@ -146,6 +146,30 @@ describe("contract: nav item present iff its route is registered", () => {
146
146
  path: SETTINGS_ACCOUNTS_PATH,
147
147
  present: true,
148
148
  },
149
+ {
150
+ name: "Accounts on, self-serve off",
151
+ deps: {
152
+ selfServe: { ...selfServe, enabled: false },
153
+ accountServices: accountServices("admin@example.com"),
154
+ },
155
+ path: SETTINGS_ACCOUNTS_PATH,
156
+ present: true,
157
+ },
158
+ {
159
+ name: "Accounts on, no self-serve config",
160
+ deps: { accountServices: accountServices("admin@example.com") },
161
+ path: SETTINGS_ACCOUNTS_PATH,
162
+ present: true,
163
+ },
164
+ {
165
+ name: "Account page, self-serve off",
166
+ deps: {
167
+ selfServe: { ...selfServe, enabled: false },
168
+ accountServices: accountServices("admin@example.com"),
169
+ },
170
+ path: SETTINGS_ACCOUNT_PATH,
171
+ present: true,
172
+ },
149
173
  {
150
174
  name: "Accounts off",
151
175
  deps: {},
@@ -81,7 +81,8 @@ describe("GET /admin/settings", () => {
81
81
  headers: { cookie: await userCookie() },
82
82
  });
83
83
  expect(res.status).toBe(302);
84
- expect(res.headers.get("location")).toBe("/admin/settings/templates");
84
+ // Account comes first for an owner, with or without self-serve signup.
85
+ expect(res.headers.get("location")).toBe("/admin/account");
85
86
  });
86
87
 
87
88
  it("lands an account member on Notifications (ACM-7.2)", async () => {
@@ -239,6 +240,21 @@ describe("GET /admin/settings with accounts (SGL-2.3)", () => {
239
240
  expect(res.headers.get("location")).toBe("/admin/account");
240
241
  });
241
242
 
243
+ it("lands an account owner on Account with self-serve off", async () => {
244
+ const app = createAdminUIApp(
245
+ makeMockDeps({
246
+ templateService,
247
+ selfServe: { ...selfServe, enabled: false },
248
+ accountServices: memberAccounts("m@acme.com"),
249
+ } as never),
250
+ );
251
+ const res = await app.request("/admin/settings", {
252
+ headers: { cookie: await userCookie("m@acme.com") },
253
+ });
254
+ expect(res.status).toBe(302);
255
+ expect(res.headers.get("location")).toBe("/admin/account");
256
+ });
257
+
242
258
  it("lands an account member on Notifications, not Account (ACM-7.2)", async () => {
243
259
  const app = createAdminUIApp(
244
260
  makeMockDeps({
@@ -613,7 +613,77 @@ describe("admin UI — Google callback self-serve signup (SSP-3.1)", () => {
613
613
  expect(notified).toEqual([]);
614
614
  });
615
615
 
616
- it("flag off: unknown email still gets 403 and nothing is provisioned", async () => {
616
+ it("flag off: unknown email still gets 403 and no account is created", async () => {
617
+ const calls: string[] = [];
618
+ const app = createAdminUIApp(
619
+ makeMockDeps({
620
+ googleClient: identity(),
621
+ selfServe: { ...enabled, enabled: false },
622
+ accountOnboarding: {
623
+ provisionForEmail: async () => {
624
+ calls.push("provision");
625
+ return { kind: "created", accountId: "acc-1" };
626
+ },
627
+ joinForEmail: async (email) => {
628
+ calls.push(`join:${email}`);
629
+ return null;
630
+ },
631
+ },
632
+ }),
633
+ );
634
+ const res = await app.request(callbackRequest(nonce));
635
+ expect(res.status).toBe(403);
636
+ expect(res.headers.get("Set-Cookie") ?? "").not.toContain("admin_session=");
637
+ expect(calls).toEqual(["join:new.user@example.com"]);
638
+ });
639
+
640
+ it("flag off: an invited email joins its account and signs in, without a notification", async () => {
641
+ const notified: unknown[] = [];
642
+ let provisioned = false;
643
+ const app = createAdminUIApp(
644
+ makeMockDeps({
645
+ googleClient: identity(),
646
+ selfServe: { ...enabled, enabled: false },
647
+ accountOnboarding: {
648
+ provisionForEmail: async () => {
649
+ provisioned = true;
650
+ return { kind: "created", accountId: "acc-1" };
651
+ },
652
+ joinForEmail: async () => ({ kind: "joined", accountId: "acc-1" }),
653
+ },
654
+ accountCreatedNotifier: async (info) => {
655
+ notified.push(info);
656
+ },
657
+ }),
658
+ );
659
+ const res = await app.request(callbackRequest(nonce));
660
+ expect(res.status).toBe(302);
661
+ expect(res.headers.get("Location")).toBe("/admin/agents");
662
+ expect(res.headers.get("Set-Cookie")).toContain("admin_session=");
663
+ expect(provisioned).toBe(false);
664
+ expect(notified).toEqual([]);
665
+ });
666
+
667
+ it("flag off: an existing account owner with no agent memberships signs in", async () => {
668
+ const app = createAdminUIApp(
669
+ makeMockDeps({
670
+ googleClient: identity(),
671
+ selfServe: { ...enabled, enabled: false },
672
+ accountOnboarding: {
673
+ provisionForEmail: async () => {
674
+ throw new Error("must not create");
675
+ },
676
+ joinForEmail: async () => ({ kind: "existing", accountId: "acc-1" }),
677
+ },
678
+ }),
679
+ );
680
+ const res = await app.request(callbackRequest(nonce));
681
+ expect(res.status).toBe(302);
682
+ expect(res.headers.get("Location")).toBe("/admin/agents");
683
+ expect(res.headers.get("Set-Cookie")).toContain("admin_session=");
684
+ });
685
+
686
+ it("flag off: onboarding without joinForEmail keeps the 403", async () => {
617
687
  let called = false;
618
688
  const app = createAdminUIApp(
619
689
  makeMockDeps({
@@ -632,6 +702,25 @@ describe("admin UI — Google callback self-serve signup (SSP-3.1)", () => {
632
702
  expect(called).toBe(false);
633
703
  });
634
704
 
705
+ it("flag off: a join failure redirects to server_error without a session", async () => {
706
+ const app = createAdminUIApp(
707
+ makeMockDeps({
708
+ googleClient: identity(),
709
+ selfServe: { ...enabled, enabled: false },
710
+ accountOnboarding: {
711
+ provisionForEmail: async () => ({ kind: "created", accountId: "a" }),
712
+ joinForEmail: async () => {
713
+ throw new Error("db down");
714
+ },
715
+ },
716
+ }),
717
+ );
718
+ const res = await app.request(callbackRequest(nonce));
719
+ expect(res.status).toBe(302);
720
+ expect(res.headers.get("Location")).toContain("error=server_error");
721
+ expect(res.headers.get("Set-Cookie") ?? "").not.toContain("admin_session=");
722
+ });
723
+
635
724
  it("flag on: unverified email creates nothing", async () => {
636
725
  let called = false;
637
726
  const app = createAdminUIApp(
@@ -7884,7 +7973,7 @@ describe("admin UI — agent management is limited to admins and account owners"
7884
7973
  expect(res.status).toBe(403);
7885
7974
  });
7886
7975
 
7887
- it(`flag off (no account on scope): owner gets 403 on POST /admin/agents/:id/${path}`, async () => {
7976
+ it(`owner with no account on the scope gets 403 on POST /admin/agents/:id/${path}`, async () => {
7888
7977
  const res = await makeApp(scopeOf(null, "owner")).request(
7889
7978
  `/admin/agents/${AGENT_ID}/${path}`,
7890
7979
  form(fields, await callerCookie()),
@@ -8217,3 +8306,79 @@ describe("admin UI — admin sign-in ensures an AccountMember", () => {
8217
8306
  expect(emails).toEqual(["dev@localhost"]);
8218
8307
  });
8219
8308
  });
8309
+
8310
+ // ─── Agents list account features do not depend on the self-serve flag ─────
8311
+
8312
+ describe("admin UI — agents list account features, self-serve off and on", () => {
8313
+ const OWNER = "owner@example.com";
8314
+ const agent = {
8315
+ id: AGENT_ID,
8316
+ name: "Test Agent",
8317
+ slackId: "U123456",
8318
+ selfHosted: false,
8319
+ specialty: "coding",
8320
+ accountId: "acct-a",
8321
+ createdAt: new Date("2024-01-01"),
8322
+ updatedAt: new Date("2024-01-01"),
8323
+ };
8324
+ const makeApp = (enabled: boolean) => {
8325
+ const base = makeMockDeps();
8326
+ return createAdminUIApp(
8327
+ makeMockDeps({
8328
+ selfServe: { enabled, defaultMaxAgents: 0 },
8329
+ callerScopeResolver: async () => ({
8330
+ kind: "scoped" as const,
8331
+ accountId: "acct-a",
8332
+ agentIds: [AGENT_ID],
8333
+ role: "owner" as const,
8334
+ }),
8335
+ agentService: {
8336
+ ...base.agentService,
8337
+ listAll: async () => [agent],
8338
+ listByIds: async () => [agent],
8339
+ } as never,
8340
+ accountServices: {
8341
+ accounts: {
8342
+ getByMemberEmail: async (email: string) =>
8343
+ email === OWNER
8344
+ ? { id: "acct-a", name: "Acme Co", maxAgents: 0 }
8345
+ : null,
8346
+ listWithCounts: async () => [{ id: "acct-a", name: "Acme Co" }],
8347
+ },
8348
+ members: { getByEmail: async () => null },
8349
+ } as never,
8350
+ }),
8351
+ );
8352
+ };
8353
+
8354
+ for (const enabled of [false, true]) {
8355
+ const label = enabled ? "on" : "off";
8356
+
8357
+ it(`self-serve ${label}: admins see the Account column`, async () => {
8358
+ const res = await makeApp(enabled).request("/admin/agents", {
8359
+ headers: { Cookie: `admin_session=${await makeSessionCookie()}` },
8360
+ });
8361
+ expect(res.status).toBe(200);
8362
+ const body = await res.text();
8363
+ expect(body).toContain("<th>Account</th>");
8364
+ expect(body).toContain('href="/admin/accounts/acct-a"');
8365
+ });
8366
+
8367
+ it(`self-serve ${label}: an owner of a zero-limit account sees the limit notice`, async () => {
8368
+ const cookie = await makeSessionCookie(
8369
+ SESSION_SECRET,
8370
+ "google-sub-owner",
8371
+ OWNER,
8372
+ false,
8373
+ );
8374
+ const res = await makeApp(enabled).request("/admin/agents", {
8375
+ headers: { Cookie: `admin_session=${cookie}` },
8376
+ });
8377
+ expect(res.status).toBe(200);
8378
+ const body = await res.text();
8379
+ expect(body).toContain("This account has reached its agent limit.");
8380
+ expect(body).not.toContain("request a trial");
8381
+ expect(body).not.toContain("quota@example.com");
8382
+ });
8383
+ }
8384
+ });