@app-vitals/agent-admin 1.72.0 → 1.74.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.
@@ -1302,6 +1302,7 @@ describe("admin UI — authenticated pages", () => {
1302
1302
  id: "m1",
1303
1303
  agentId: AGENT_ID,
1304
1304
  email,
1305
+ memberId: null,
1305
1306
  createdAt: new Date(),
1306
1307
  },
1307
1308
  ]
@@ -1312,6 +1313,7 @@ describe("admin UI — authenticated pages", () => {
1312
1313
  id: "m1",
1313
1314
  agentId: AGENT_ID,
1314
1315
  email: MEMBER_EMAIL,
1316
+ memberId: null,
1315
1317
  createdAt: new Date(),
1316
1318
  }),
1317
1319
  remove: async () => {},
@@ -2881,7 +2883,15 @@ describe("admin UI — member access control", () => {
2881
2883
  agentMemberService: {
2882
2884
  listByEmail: async (email: string) =>
2883
2885
  email === MEMBER_EMAIL
2884
- ? [{ id: "m1", agentId: AGENT_ID, email, createdAt: new Date() }]
2886
+ ? [
2887
+ {
2888
+ id: "m1",
2889
+ agentId: AGENT_ID,
2890
+ email,
2891
+ memberId: null,
2892
+ createdAt: new Date(),
2893
+ },
2894
+ ]
2885
2895
  : [],
2886
2896
  exists: async (_agentId: string, email: string) =>
2887
2897
  email === MEMBER_EMAIL,
@@ -2889,6 +2899,7 @@ describe("admin UI — member access control", () => {
2889
2899
  id: "m1",
2890
2900
  agentId: AGENT_ID,
2891
2901
  email: MEMBER_EMAIL,
2902
+ memberId: null,
2892
2903
  createdAt: new Date(),
2893
2904
  }),
2894
2905
  remove: async () => {},
@@ -3031,6 +3042,7 @@ describe("admin UI — member access control", () => {
3031
3042
  id: "m1",
3032
3043
  agentId: AGENT_ID,
3033
3044
  email: MEMBER_EMAIL,
3045
+ memberId: null,
3034
3046
  createdAt: new Date(),
3035
3047
  },
3036
3048
  ]
@@ -3040,6 +3052,7 @@ describe("admin UI — member access control", () => {
3040
3052
  id: "m1",
3041
3053
  agentId: AGENT_ID,
3042
3054
  email: MEMBER_EMAIL,
3055
+ memberId: null,
3043
3056
  createdAt: new Date(),
3044
3057
  }),
3045
3058
  remove: async () => {},
@@ -3186,6 +3199,7 @@ describe("admin UI — member access control", () => {
3186
3199
  id: "m1",
3187
3200
  agentId: AGENT_ID,
3188
3201
  email: MEMBER_EMAIL,
3202
+ memberId: null,
3189
3203
  createdAt: new Date(),
3190
3204
  },
3191
3205
  ]
@@ -3195,6 +3209,7 @@ describe("admin UI — member access control", () => {
3195
3209
  id: "m1",
3196
3210
  agentId: AGENT_ID,
3197
3211
  email: MEMBER_EMAIL,
3212
+ memberId: null,
3198
3213
  createdAt: new Date(),
3199
3214
  }),
3200
3215
  remove: async () => {},
@@ -3668,7 +3683,13 @@ describe("admin UI — member management routes", () => {
3668
3683
  exists: async () => false,
3669
3684
  add: async (agentId: string, email: string) => {
3670
3685
  created = { agentId, email };
3671
- return { id: "m-new", agentId, email, createdAt: new Date() };
3686
+ return {
3687
+ id: "m-new",
3688
+ agentId,
3689
+ email,
3690
+ memberId: null,
3691
+ createdAt: new Date(),
3692
+ };
3672
3693
  },
3673
3694
  remove: async () => {},
3674
3695
  listByAgentId: async () => [],
@@ -3761,6 +3782,7 @@ describe("admin UI — member management routes", () => {
3761
3782
  id: "m1",
3762
3783
  agentId: AGENT_ID,
3763
3784
  email: "member@example.com",
3785
+ memberId: null,
3764
3786
  createdAt: new Date(),
3765
3787
  }),
3766
3788
  remove: async (_agentId: string, memberId: string) => {
@@ -3821,13 +3843,22 @@ describe("admin UI — marketplaces (PMK-1.5)", () => {
3821
3843
  agentMemberService: {
3822
3844
  listByEmail: async (email: string) =>
3823
3845
  email === MEMBER
3824
- ? [{ id: "m1", agentId: AGENT_ID, email, createdAt: new Date() }]
3846
+ ? [
3847
+ {
3848
+ id: "m1",
3849
+ agentId: AGENT_ID,
3850
+ email,
3851
+ memberId: null,
3852
+ createdAt: new Date(),
3853
+ },
3854
+ ]
3825
3855
  : [],
3826
3856
  exists: async (_agentId: string, email: string) => email === MEMBER,
3827
3857
  add: async () => ({
3828
3858
  id: "m1",
3829
3859
  agentId: AGENT_ID,
3830
3860
  email: MEMBER,
3861
+ memberId: null,
3831
3862
  createdAt: new Date(),
3832
3863
  }),
3833
3864
  remove: async () => {},
@@ -4051,7 +4082,15 @@ describe("admin UI — plugin add/remove (PUI-1.2)", () => {
4051
4082
  agentMemberService: {
4052
4083
  listByEmail: async (email: string) =>
4053
4084
  email === MEMBER
4054
- ? [{ id: "m1", agentId: AGENT_ID, email, createdAt: new Date() }]
4085
+ ? [
4086
+ {
4087
+ id: "m1",
4088
+ agentId: AGENT_ID,
4089
+ email,
4090
+ memberId: null,
4091
+ createdAt: new Date(),
4092
+ },
4093
+ ]
4055
4094
  : [],
4056
4095
  exists: async (_a: string, email: string) => email === MEMBER,
4057
4096
  add: async () => ({}) as never,
@@ -6985,6 +7024,7 @@ describe("admin UI — Slack access settings route (restrictSlackToMembers)", ()
6985
7024
  id: "m1",
6986
7025
  agentId: AGENT_ID,
6987
7026
  email: "member@example.com",
7027
+ memberId: null,
6988
7028
  createdAt: new Date("2024-01-01"),
6989
7029
  },
6990
7030
  ],
@@ -7247,6 +7287,7 @@ describe("admin UI — Okta-authenticated access control", () => {
7247
7287
  id: "m1",
7248
7288
  agentId: AGENT_ID,
7249
7289
  email: OKTA_MEMBER_EMAIL,
7290
+ memberId: null,
7250
7291
  createdAt: new Date(),
7251
7292
  },
7252
7293
  ]
@@ -7257,6 +7298,7 @@ describe("admin UI — Okta-authenticated access control", () => {
7257
7298
  id: "m1",
7258
7299
  agentId: AGENT_ID,
7259
7300
  email: OKTA_MEMBER_EMAIL,
7301
+ memberId: null,
7260
7302
  createdAt: new Date(),
7261
7303
  }),
7262
7304
  remove: async () => {},
@@ -7361,7 +7403,15 @@ describe("admin UI — Okta-authenticated access control", () => {
7361
7403
  agentMemberService: {
7362
7404
  listByEmail: async (email: string) =>
7363
7405
  email === OKTA_MEMBER_EMAIL
7364
- ? [{ id: "m1", agentId: AGENT_ID, email, createdAt: new Date() }]
7406
+ ? [
7407
+ {
7408
+ id: "m1",
7409
+ agentId: AGENT_ID,
7410
+ email,
7411
+ memberId: null,
7412
+ createdAt: new Date(),
7413
+ },
7414
+ ]
7365
7415
  : [],
7366
7416
  exists: async (_agentId: string, email: string) =>
7367
7417
  email === OKTA_MEMBER_EMAIL,
@@ -7369,6 +7419,7 @@ describe("admin UI — Okta-authenticated access control", () => {
7369
7419
  id: "m1",
7370
7420
  agentId: AGENT_ID,
7371
7421
  email: OKTA_MEMBER_EMAIL,
7422
+ memberId: null,
7372
7423
  createdAt: new Date(),
7373
7424
  }),
7374
7425
  remove: async () => {},
@@ -7819,9 +7870,7 @@ describe("admin UI — self-serve agent management (SSP-4.3)", () => {
7819
7870
  expect(accessRes.status).toBe(200);
7820
7871
  const html = `${await res.text()}${await accessRes.text()}`;
7821
7872
  expect(html.includes("Danger Zone")).toBe(shown);
7822
- expect(html.includes('<div class="card-title">Slack</div>')).toBe(
7823
- shown,
7824
- );
7873
+ expect(html.includes('<div class="card-title">Slack</div>')).toBe(shown);
7825
7874
  expect(html.includes(`/admin/agents/${AGENT_ID}/members`)).toBe(shown);
7826
7875
  });
7827
7876
  }
@@ -437,6 +437,7 @@ function makeMockDeps(): AdminDeps {
437
437
  id: "member-1",
438
438
  agentId,
439
439
  email,
440
+ memberId: null,
440
441
  createdAt: new Date(),
441
442
  }),
442
443
  listByAgentId: async () => [],
@@ -14,6 +14,7 @@ interface FakeAgentMemberRow {
14
14
  id: string;
15
15
  agentId: string;
16
16
  email: string;
17
+ memberId: string | null;
17
18
  createdAt: Date;
18
19
  }
19
20
 
@@ -64,6 +65,7 @@ function makeFakePrisma(seed: FakeAgentMemberRow[] = []) {
64
65
  id: `member-${nextId++}`,
65
66
  agentId: data.agentId,
66
67
  email: data.email,
68
+ memberId: null,
67
69
  createdAt: new Date("2024-01-01"),
68
70
  };
69
71
  rows.set(row.id, row);
@@ -88,11 +90,14 @@ function makeFakePrisma(seed: FakeAgentMemberRow[] = []) {
88
90
 
89
91
  type FakePrisma = ReturnType<typeof makeFakePrisma>;
90
92
 
91
- function seedRow(overrides: Partial<FakeAgentMemberRow> = {}): FakeAgentMemberRow {
93
+ function seedRow(
94
+ overrides: Partial<FakeAgentMemberRow> = {},
95
+ ): FakeAgentMemberRow {
92
96
  return {
93
97
  id: "member-existing",
94
98
  agentId: "agent-1",
95
99
  email: "person@example.com",
100
+ memberId: null,
96
101
  createdAt: new Date("2024-01-01"),
97
102
  ...overrides,
98
103
  };
@@ -577,6 +577,7 @@ function makeMockDeps(): AdminDeps {
577
577
  id: "member-test-id",
578
578
  agentId,
579
579
  email,
580
+ memberId: null,
580
581
  createdAt: new Date("2024-01-01"),
581
582
  }),
582
583
  // Default: at least one member, so restrictSlackToMembers tests that
@@ -587,6 +588,7 @@ function makeMockDeps(): AdminDeps {
587
588
  id: "member-test-id",
588
589
  agentId,
589
590
  email: "member@example.com",
591
+ memberId: null,
590
592
  createdAt: new Date("2024-01-01"),
591
593
  },
592
594
  ],
@@ -3069,6 +3071,7 @@ describe("admin API — restrictSlackToMembers field", () => {
3069
3071
  id: "member-1",
3070
3072
  agentId: AGENT_ID,
3071
3073
  email: "member@example.com",
3074
+ memberId: null,
3072
3075
  createdAt: new Date("2024-01-01"),
3073
3076
  },
3074
3077
  ],
@@ -5228,6 +5231,7 @@ describe("non-admin session cookie scoping", () => {
5228
5231
  id: "m1",
5229
5232
  agentId: AGENT_ID,
5230
5233
  email,
5234
+ memberId: null,
5231
5235
  createdAt: new Date("2024-01-01"),
5232
5236
  },
5233
5237
  ]
@@ -6221,6 +6225,7 @@ describe("admin API — repo removal blocks open work", () => {
6221
6225
  id: "m1",
6222
6226
  agentId: AGENT_ID,
6223
6227
  email,
6228
+ memberId: null,
6224
6229
  createdAt: new Date("2024-01-01"),
6225
6230
  },
6226
6231
  ]
@@ -1142,6 +1142,7 @@ function buildCombinedApp() {
1142
1142
  id: "member-1",
1143
1143
  agentId,
1144
1144
  email,
1145
+ memberId: null,
1145
1146
  createdAt: new Date(),
1146
1147
  }),
1147
1148
  listByAgentId: async () => [],
@@ -346,6 +346,7 @@ function makeBaseDeps(overrides?: Partial<AdminUIDeps>): AdminUIDeps {
346
346
  id: "m1",
347
347
  agentId,
348
348
  email,
349
+ memberId: null,
349
350
  createdAt: new Date(),
350
351
  }),
351
352
  remove: async () => {},
@@ -1610,6 +1611,7 @@ function makeMemberDeps(
1610
1611
  id: `m-${agentId}`,
1611
1612
  agentId,
1612
1613
  email,
1614
+ memberId: null,
1613
1615
  createdAt: new Date(),
1614
1616
  }))
1615
1617
  : [],
@@ -1654,3 +1654,34 @@ export const TemplateItemKeyQuerySchema = z.object({
1654
1654
  kind: TemplateItemKindSchema,
1655
1655
  key: z.string().min(1),
1656
1656
  });
1657
+
1658
+ // ─── Principals (ACM-1.1) ─────────────────────────────────────────────────────
1659
+
1660
+ export const PrincipalSchema = z
1661
+ .object({
1662
+ id: z.string().openapi({ example: "clx1abc" }),
1663
+ type: z.enum(["user", "agent"]),
1664
+ email: z
1665
+ .string()
1666
+ .nullable()
1667
+ .openapi({ description: "null for agents", example: "o@acme.com" }),
1668
+ })
1669
+ .openapi("Principal");
1670
+
1671
+ export const PrincipalLookupBodySchema = z
1672
+ .object({ ids: z.array(z.string().min(1)).max(500) })
1673
+ .openapi("PrincipalLookupBody");
1674
+
1675
+ export const PrincipalLookupResponseSchema = z
1676
+ .object({
1677
+ principals: z.array(PrincipalSchema),
1678
+ notFound: z.array(z.string()),
1679
+ })
1680
+ .openapi("PrincipalLookupResponse");
1681
+
1682
+ export const PrincipalByEmailQuerySchema = z.object({
1683
+ email: z
1684
+ .string()
1685
+ .min(1)
1686
+ .openapi({ param: { name: "email", in: "query" }, example: "o@acme.com" }),
1687
+ });
@@ -0,0 +1,67 @@
1
+ /**
2
+ * admin/src/principal-lookup.ts
3
+ * PrincipalLookupService (ACM-1.1): a principal is an AccountMember.id (type
4
+ * "user") or an Agent.id (type "agent"). Resolves principal ids to
5
+ * {type,email} in batch and a (case-insensitive) email to its principal.
6
+ *
7
+ * Agents have no email of their own, so they resolve with email: null, and an
8
+ * email only ever resolves to a user principal.
9
+ */
10
+
11
+ import type { PrismaClient } from "../prisma/client/client.ts";
12
+ import { normalizeEmail } from "./account-email.ts";
13
+
14
+ export interface Principal {
15
+ id: string;
16
+ type: "user" | "agent";
17
+ email: string | null;
18
+ }
19
+
20
+ export interface PrincipalLookupResult {
21
+ principals: Principal[];
22
+ notFound: string[];
23
+ }
24
+
25
+ export class PrincipalLookupService {
26
+ constructor(private prisma: PrismaClient) {}
27
+
28
+ /** Unknown ids land in `notFound`; results follow first-seen request order. */
29
+ async resolveMany(ids: string[]): Promise<PrincipalLookupResult> {
30
+ const unique = [...new Set(ids)];
31
+ if (unique.length === 0) return { principals: [], notFound: [] };
32
+
33
+ const [members, agents] = await Promise.all([
34
+ this.prisma.accountMember.findMany({
35
+ where: { id: { in: unique } },
36
+ select: { id: true, email: true },
37
+ }),
38
+ this.prisma.agent.findMany({
39
+ where: { id: { in: unique } },
40
+ select: { id: true },
41
+ }),
42
+ ]);
43
+ const byId = new Map<string, Principal>();
44
+ for (const a of agents)
45
+ byId.set(a.id, { id: a.id, type: "agent", email: null });
46
+ // Ids are cuids so collisions are not expected; a member wins if one occurs.
47
+ for (const m of members)
48
+ byId.set(m.id, { id: m.id, type: "user", email: m.email });
49
+
50
+ const principals: Principal[] = [];
51
+ const notFound: string[] = [];
52
+ for (const id of unique) {
53
+ const p = byId.get(id);
54
+ if (p) principals.push(p);
55
+ else notFound.push(id);
56
+ }
57
+ return { principals, notFound };
58
+ }
59
+
60
+ async findIdByEmail(email: string): Promise<Principal | null> {
61
+ const member = await this.prisma.accountMember.findUnique({
62
+ where: { email: normalizeEmail(email) },
63
+ select: { id: true, email: true },
64
+ });
65
+ return member ? { id: member.id, type: "user", email: member.email } : null;
66
+ }
67
+ }
@@ -0,0 +1,104 @@
1
+ /**
2
+ * admin/src/principal-lookup.unit.test.ts
3
+ * PrincipalLookupService (ACM-1.1): id -> {type,email} (batch) and email -> id.
4
+ */
5
+
6
+ import { describe, expect, it } from "bun:test";
7
+ import { PrincipalLookupService } from "./principal-lookup.ts";
8
+
9
+ function fakePrisma(
10
+ members: { id: string; email: string }[],
11
+ agents: { id: string }[],
12
+ ) {
13
+ const calls = { member: 0, agent: 0 };
14
+ const prisma = {
15
+ accountMember: {
16
+ findMany: async ({ where }: { where: { id: { in: string[] } } }) => {
17
+ calls.member++;
18
+ return members.filter((m) => where.id.in.includes(m.id));
19
+ },
20
+ findUnique: async ({ where }: { where: { email: string } }) =>
21
+ members.find((m) => m.email === where.email) ?? null,
22
+ },
23
+ agent: {
24
+ findMany: async ({ where }: { where: { id: { in: string[] } } }) => {
25
+ calls.agent++;
26
+ return agents.filter((a) => where.id.in.includes(a.id));
27
+ },
28
+ },
29
+ };
30
+ return { prisma: prisma as never, calls };
31
+ }
32
+
33
+ describe("PrincipalLookupService.resolveMany", () => {
34
+ it("resolves a mixed list of member and agent ids", async () => {
35
+ const { prisma } = fakePrisma(
36
+ [{ id: "m1", email: "a@x.com" }],
37
+ [{ id: "a1" }],
38
+ );
39
+ const res = await new PrincipalLookupService(prisma).resolveMany([
40
+ "m1",
41
+ "a1",
42
+ ]);
43
+ expect(res.principals).toEqual([
44
+ { id: "m1", type: "user", email: "a@x.com" },
45
+ { id: "a1", type: "agent", email: null },
46
+ ]);
47
+ expect(res.notFound).toEqual([]);
48
+ });
49
+
50
+ it("returns unknown ids as notFound, not an error", async () => {
51
+ const { prisma } = fakePrisma([{ id: "m1", email: "a@x.com" }], []);
52
+ const res = await new PrincipalLookupService(prisma).resolveMany([
53
+ "m1",
54
+ "nope",
55
+ ]);
56
+ expect(res.principals.map((p) => p.id)).toEqual(["m1"]);
57
+ expect(res.notFound).toEqual(["nope"]);
58
+ });
59
+
60
+ it("dedupes ids and preserves request order", async () => {
61
+ const { prisma } = fakePrisma(
62
+ [
63
+ { id: "m1", email: "a@x.com" },
64
+ { id: "m2", email: "b@x.com" },
65
+ ],
66
+ [],
67
+ );
68
+ const res = await new PrincipalLookupService(prisma).resolveMany([
69
+ "m2",
70
+ "m1",
71
+ "m2",
72
+ "z",
73
+ "z",
74
+ ]);
75
+ expect(res.principals.map((p) => p.id)).toEqual(["m2", "m1"]);
76
+ expect(res.notFound).toEqual(["z"]);
77
+ });
78
+
79
+ it("makes no queries for an empty list", async () => {
80
+ const { prisma, calls } = fakePrisma([], []);
81
+ const res = await new PrincipalLookupService(prisma).resolveMany([]);
82
+ expect(res).toEqual({ principals: [], notFound: [] });
83
+ expect(calls).toEqual({ member: 0, agent: 0 });
84
+ });
85
+ });
86
+
87
+ describe("PrincipalLookupService.findIdByEmail", () => {
88
+ it("normalizes case and whitespace before matching", async () => {
89
+ const { prisma } = fakePrisma([{ id: "m1", email: "a@x.com" }], []);
90
+ const svc = new PrincipalLookupService(prisma);
91
+ expect(await svc.findIdByEmail(" A@X.com ")).toEqual({
92
+ id: "m1",
93
+ type: "user",
94
+ email: "a@x.com",
95
+ });
96
+ });
97
+
98
+ it("returns null for an unknown email", async () => {
99
+ const { prisma } = fakePrisma([], []);
100
+ expect(
101
+ await new PrincipalLookupService(prisma).findIdByEmail("q@x.com"),
102
+ ).toBeNull();
103
+ });
104
+ });
@@ -0,0 +1,115 @@
1
+ /**
2
+ * admin/src/principals-api.integration.test.ts
3
+ * /principals lookups against real Postgres (ACM-1.1), plus the additive
4
+ * nullable memberId columns from 20261118000000_add_member_id_columns.
5
+ *
6
+ * Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
7
+ */
8
+
9
+ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
10
+ import type { PrismaClient } from "../prisma/client/client.ts";
11
+ import { PrincipalLookupService } from "./principal-lookup.ts";
12
+ import { createPrincipalsApp } from "./principals-api.ts";
13
+ import { createAdminPrismaClient } from "./prisma-client.ts";
14
+
15
+ const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
16
+ const describeOrSkip = TEST_DB ? describe : describe.skip;
17
+
18
+ describeOrSkip("/principals API (integration)", () => {
19
+ let prisma: PrismaClient;
20
+ let app: ReturnType<typeof createPrincipalsApp>;
21
+ const headers = {
22
+ Authorization: "Bearer k",
23
+ "Content-Type": "application/json",
24
+ };
25
+
26
+ beforeEach(async () => {
27
+ prisma = createAdminPrismaClient(TEST_DB as string);
28
+ await prisma.agentMember.deleteMany();
29
+ await prisma.accountMember.deleteMany();
30
+ await prisma.agent.deleteMany();
31
+ await prisma.account.deleteMany();
32
+ app = createPrincipalsApp({
33
+ lookup: new PrincipalLookupService(prisma),
34
+ sessionSecret: "s".repeat(32),
35
+ adminApiKeys: new Map([["k", { name: "t", scope: "*" }]]),
36
+ agentTokenService: { validate: async () => null } as never,
37
+ });
38
+ });
39
+
40
+ afterEach(async () => {
41
+ await prisma.$disconnect();
42
+ });
43
+
44
+ it("batch resolves mixed member/agent ids and reports unknown ids as notFound", async () => {
45
+ const account = await prisma.account.create({ data: { name: "Acme" } });
46
+ const member = await prisma.accountMember.create({
47
+ data: { accountId: account.id, email: "owner@acme.com", role: "owner" },
48
+ });
49
+ const agent = await prisma.agent.create({ data: { name: "bot" } });
50
+
51
+ const res = await app.request("/principals/lookup", {
52
+ method: "POST",
53
+ headers,
54
+ body: JSON.stringify({ ids: [member.id, agent.id, "missing"] }),
55
+ });
56
+ expect(res.status).toBe(200);
57
+ expect(await res.json()).toEqual({
58
+ principals: [
59
+ { id: member.id, type: "user", email: "owner@acme.com" },
60
+ { id: agent.id, type: "agent", email: null },
61
+ ],
62
+ notFound: ["missing"],
63
+ });
64
+ });
65
+
66
+ it("email -> id is case-insensitive", async () => {
67
+ const account = await prisma.account.create({ data: { name: "Acme" } });
68
+ const member = await prisma.accountMember.create({
69
+ data: { accountId: account.id, email: "owner@acme.com", role: "owner" },
70
+ });
71
+ const res = await app.request("/principals/by-email?email=Owner@ACME.com", {
72
+ headers,
73
+ });
74
+ expect(res.status).toBe(200);
75
+ expect(await res.json()).toEqual({
76
+ id: member.id,
77
+ type: "user",
78
+ email: "owner@acme.com",
79
+ });
80
+ const miss = await app.request(
81
+ "/principals/by-email?email=nobody@acme.com",
82
+ { headers },
83
+ );
84
+ expect(miss.status).toBe(404);
85
+ });
86
+
87
+ it("memberId columns exist, are nullable and default to NULL", async () => {
88
+ const cols = await prisma.$queryRaw<
89
+ {
90
+ table_name: string;
91
+ is_nullable: string;
92
+ column_default: string | null;
93
+ }[]
94
+ >`SELECT table_name, is_nullable, column_default FROM information_schema.columns
95
+ WHERE column_name = 'memberId'
96
+ AND table_name IN ('UserNotificationPrefs','ChatThreadWatch','PushSubscription','AgentMember')
97
+ ORDER BY table_name`;
98
+ expect(cols.map((c) => c.table_name)).toEqual([
99
+ "AgentMember",
100
+ "ChatThreadWatch",
101
+ "PushSubscription",
102
+ "UserNotificationPrefs",
103
+ ]);
104
+ for (const c of cols) {
105
+ expect(c.is_nullable).toBe("YES");
106
+ expect(c.column_default).toBeNull();
107
+ }
108
+ const fks = await prisma.$queryRaw<{ n: bigint }[]>`SELECT count(*) AS n
109
+ FROM information_schema.table_constraints tc
110
+ JOIN information_schema.constraint_column_usage ccu USING (constraint_name)
111
+ WHERE ccu.column_name = 'memberId'
112
+ AND tc.table_name IN ('UserNotificationPrefs','ChatThreadWatch','PushSubscription','AgentMember')`;
113
+ expect(Number(fks[0]?.n)).toBe(0);
114
+ });
115
+ });