@app-vitals/agent-admin 1.107.2 → 1.107.4
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.
- package/e2e/real/agent-management.real.e2e.ts +195 -0
- package/e2e/real/seed.ts +0 -1
- package/package.json +2 -2
- package/prisma/client/internal/class.ts +4 -4
- package/prisma/client/internal/prismaNamespace.ts +1 -5
- package/prisma/client/internal/prismaNamespaceBrowser.ts +1 -5
- package/prisma/client/models/AgentMember.ts +26 -61
- package/prisma/client/models/ChatThreadWatch.ts +19 -47
- package/prisma/client/models/PushSubscription.ts +15 -43
- package/prisma/client/models/UserNotificationPrefs.ts +38 -66
- package/prisma/migrations/20261130000000_drop_email_key_columns/migration.sql +49 -0
- package/prisma/schema/schema.prisma +11 -15
- package/src/account-member-removal.integration.test.ts +31 -44
- package/src/account-members.ts +4 -6
- package/src/account-onboarding.ts +2 -5
- package/src/account-onboarding.unit.test.ts +71 -0
- package/src/accounts.ts +1 -3
- package/src/admin-dev-login.smoke.test.ts +1 -1
- package/src/admin-local-agent.smoke.test.ts +28 -6
- package/src/admin-ui-agent-members.smoke.test.ts +31 -7
- package/src/admin-ui-pages.ts +23 -1
- package/src/admin-ui-pages.unit.test.ts +17 -0
- package/src/admin-ui.fixtures.ts +1 -1
- package/src/admin-ui.smoke.test.ts +163 -100
- package/src/admin-ui.ts +132 -55
- package/src/agent-cron-run-stats.smoke.test.ts +1 -1
- package/src/agent-deletion.integration.test.ts +1 -1
- package/src/agent-management-access.integration.test.ts +269 -0
- package/src/agent-members.integration.test.ts +14 -10
- package/src/agent-members.ts +28 -18
- package/src/agents-api.integration.test.ts +2 -2
- package/src/agents-api.smoke.test.ts +68 -13
- package/src/agents-api.ts +2 -0
- package/src/agents.integration.test.ts +1 -2
- package/src/api-auth.ts +31 -1
- package/src/api-auth.unit.test.ts +106 -6
- package/src/api.smoke.test.ts +1 -1
- package/src/caller-scope.ts +21 -0
- package/src/caller-scope.unit.test.ts +39 -0
- package/src/chat.smoke.test.ts +68 -14
- package/src/drop-email-key-columns-migration.integration.test.ts +141 -0
- package/src/ensure-account-member.integration.test.ts +17 -232
- package/src/ensure-account-member.ts +0 -5
- package/src/github-enterprise-client.unit.test.ts +62 -2
- package/src/member-rows.ts +24 -0
- package/src/member-rows.unit.test.ts +48 -0
- package/src/principals-api.integration.test.ts +3 -2
- package/src/push-service.integration.test.ts +7 -12
- package/src/push-service.ts +28 -39
- package/src/push-service.unit.test.ts +8 -8
- package/src/slack-provisioning.integration.test.ts +1 -1
- package/src/user-notification-prefs.integration.test.ts +5 -16
- package/src/user-notification-prefs.ts +12 -18
- package/scripts/backfill-member-id.ts +0 -50
- package/src/member-id-backfill.integration.test.ts +0 -167
- package/src/member-id-backfill.ts +0 -253
- package/src/member-id-backfill.unit.test.ts +0 -12
- package/src/member-id-stamp.ts +0 -68
- package/src/member-id-stamp.unit.test.ts +0 -81
|
@@ -462,7 +462,15 @@ async function makeMemberJwt(
|
|
|
462
462
|
);
|
|
463
463
|
}
|
|
464
464
|
|
|
465
|
-
function buildScopedApp(
|
|
465
|
+
function buildScopedApp(
|
|
466
|
+
memberOf: Array<[string, string]>,
|
|
467
|
+
manage?: {
|
|
468
|
+
scope?: Parameters<
|
|
469
|
+
typeof createAdminAuthMiddleware
|
|
470
|
+
>[0]["callerScopeResolver"];
|
|
471
|
+
agentAccountId?: string | null;
|
|
472
|
+
},
|
|
473
|
+
) {
|
|
466
474
|
const app = new Hono<AdminAuthEnv>();
|
|
467
475
|
app.use(
|
|
468
476
|
"*",
|
|
@@ -475,6 +483,10 @@ function buildScopedApp(memberOf: Array<[string, string]>) {
|
|
|
475
483
|
.filter(([, e]) => e === email)
|
|
476
484
|
.map(([agentId]) => ({ agentId }) as never),
|
|
477
485
|
},
|
|
486
|
+
...(manage?.scope ? { callerScopeResolver: manage.scope } : {}),
|
|
487
|
+
...(manage && "agentAccountId" in manage
|
|
488
|
+
? { getAgentAccountId: async () => manage.agentAccountId }
|
|
489
|
+
: {}),
|
|
478
490
|
}),
|
|
479
491
|
);
|
|
480
492
|
app.all("*", (c) =>
|
|
@@ -511,14 +523,102 @@ describe("createAdminAuthMiddleware — non-admin session cookie scoping", () =>
|
|
|
511
523
|
expect(res.status).toBe(403);
|
|
512
524
|
});
|
|
513
525
|
|
|
514
|
-
it("
|
|
526
|
+
it("lets a member agent read /agents/:id/** and ask, but not manage it", async () => {
|
|
515
527
|
const app = buildScopedApp(member);
|
|
516
528
|
const jwt = await makeMemberJwt();
|
|
517
|
-
const
|
|
518
|
-
|
|
519
|
-
|
|
529
|
+
const allowed: Array<[string, string]> = [
|
|
530
|
+
["GET", `/agents/${AGENT_ID}`],
|
|
531
|
+
["GET", `/agents/${AGENT_ID}/crons`],
|
|
532
|
+
["POST", `/agents/${AGENT_ID}/ask`],
|
|
533
|
+
];
|
|
534
|
+
for (const [method, path] of allowed) {
|
|
535
|
+
const res = await app.request(path, {
|
|
536
|
+
method,
|
|
537
|
+
headers: { Cookie: `admin_session=${jwt}` },
|
|
538
|
+
});
|
|
539
|
+
expect([method, path, res.status]).toEqual([method, path, 200]);
|
|
540
|
+
}
|
|
541
|
+
const denied: Array<[string, string]> = [
|
|
542
|
+
["PATCH", `/agents/${AGENT_ID}`],
|
|
543
|
+
["DELETE", `/agents/${AGENT_ID}`],
|
|
544
|
+
["PATCH", `/agents/${AGENT_ID}/crons`],
|
|
545
|
+
["POST", `/agents/${AGENT_ID}/envs`],
|
|
546
|
+
["POST", `/agents/${AGENT_ID}/tokens`],
|
|
547
|
+
["DELETE", `/agents/${AGENT_ID}/tokens/t1`],
|
|
548
|
+
["POST", `/agents/${AGENT_ID}/tools`],
|
|
549
|
+
["POST", `/agents/${AGENT_ID}/chat-credentials`],
|
|
550
|
+
["PUT", `/agents/${AGENT_ID}/github-installations`],
|
|
551
|
+
];
|
|
552
|
+
for (const [method, path] of denied) {
|
|
553
|
+
const res = await app.request(path, {
|
|
554
|
+
method,
|
|
555
|
+
headers: { Cookie: `admin_session=${jwt}` },
|
|
556
|
+
});
|
|
557
|
+
expect([method, path, res.status]).toEqual([method, path, 403]);
|
|
558
|
+
}
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
describe("management (platform admin or owner of the agent's account)", () => {
|
|
562
|
+
const scopeOf =
|
|
563
|
+
(accountId: string | null, role: "owner" | "member" | null) =>
|
|
564
|
+
async () => ({
|
|
565
|
+
kind: "scoped" as const,
|
|
566
|
+
accountId,
|
|
567
|
+
agentIds: [AGENT_ID],
|
|
568
|
+
...(role ? { role } : {}),
|
|
569
|
+
});
|
|
570
|
+
const manageStatus = async (
|
|
571
|
+
scope: ReturnType<typeof scopeOf>,
|
|
572
|
+
agentAccountId: string | null,
|
|
573
|
+
withLookup = true,
|
|
574
|
+
) => {
|
|
575
|
+
const app = buildScopedApp(
|
|
576
|
+
member,
|
|
577
|
+
withLookup ? { scope, agentAccountId } : { scope },
|
|
578
|
+
);
|
|
579
|
+
const jwt = await makeMemberJwt();
|
|
580
|
+
const res = await app.request(`/agents/${AGENT_ID}`, {
|
|
581
|
+
method: "PATCH",
|
|
582
|
+
headers: { Cookie: `admin_session=${jwt}` },
|
|
583
|
+
});
|
|
584
|
+
return res.status;
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
it("allows the owner of the agent's account", async () => {
|
|
588
|
+
expect(await manageStatus(scopeOf("acct-1", "owner"), "acct-1")).toBe(
|
|
589
|
+
200,
|
|
590
|
+
);
|
|
591
|
+
});
|
|
592
|
+
it("denies an owner of another account", async () => {
|
|
593
|
+
expect(await manageStatus(scopeOf("acct-2", "owner"), "acct-1")).toBe(
|
|
594
|
+
403,
|
|
595
|
+
);
|
|
596
|
+
});
|
|
597
|
+
it("denies a member of the agent's own account", async () => {
|
|
598
|
+
expect(await manageStatus(scopeOf("acct-1", "member"), "acct-1")).toBe(
|
|
599
|
+
403,
|
|
600
|
+
);
|
|
601
|
+
});
|
|
602
|
+
it("denies an owner when the agent has no account", async () => {
|
|
603
|
+
expect(await manageStatus(scopeOf("acct-1", "owner"), null)).toBe(403);
|
|
604
|
+
});
|
|
605
|
+
it("denies an owner when the account lookup is not wired (fail closed)", async () => {
|
|
606
|
+
expect(
|
|
607
|
+
await manageStatus(scopeOf("acct-1", "owner"), "acct-1", false),
|
|
608
|
+
).toBe(403);
|
|
609
|
+
});
|
|
610
|
+
it("denies when self-serve is off (resolver yields no accountId)", async () => {
|
|
611
|
+
expect(await manageStatus(scopeOf(null, "owner"), "acct-1")).toBe(403);
|
|
612
|
+
});
|
|
613
|
+
it("allows a platform admin cookie", async () => {
|
|
614
|
+
const app = buildScopedApp([]);
|
|
615
|
+
const jwt = await makeMemberJwt("admin@example.com", true);
|
|
616
|
+
const res = await app.request(`/agents/${AGENT_ID}`, {
|
|
617
|
+
method: "DELETE",
|
|
618
|
+
headers: { Cookie: `admin_session=${jwt}` },
|
|
619
|
+
});
|
|
620
|
+
expect(res.status).toBe(200);
|
|
520
621
|
});
|
|
521
|
-
expect(res.status).toBe(200);
|
|
522
622
|
});
|
|
523
623
|
|
|
524
624
|
it("returns 403 for an agent outside the membership", async () => {
|
package/src/api.smoke.test.ts
CHANGED
package/src/caller-scope.ts
CHANGED
|
@@ -110,6 +110,27 @@ export function scopeIncludesAgent(
|
|
|
110
110
|
return scope.kind === "all" || scope.agentIds.includes(agentId);
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
+
/**
|
|
114
|
+
* The single "may manage this agent" predicate (settings, members,
|
|
115
|
+
* credentials, tools, crons, tokens, delete): platform admins, or the owner
|
|
116
|
+
* of the agent's own account. Everything else (members, owners of another
|
|
117
|
+
* account, callers with no account) only uses the agent. The scope carries
|
|
118
|
+
* an accountId only when the self-serve flag is on, so flag-off owners never
|
|
119
|
+
* manage. An unknown role or an agent with no account fails closed.
|
|
120
|
+
*/
|
|
121
|
+
export function scopeCanManageAgent(
|
|
122
|
+
scope: CallerScope,
|
|
123
|
+
agentAccountId: string | null | undefined,
|
|
124
|
+
): boolean {
|
|
125
|
+
if (scope.kind === "all") return true;
|
|
126
|
+
return (
|
|
127
|
+
scope.role === "owner" &&
|
|
128
|
+
scope.accountId !== null &&
|
|
129
|
+
!!agentAccountId &&
|
|
130
|
+
scope.accountId === agentAccountId
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
113
134
|
/**
|
|
114
135
|
* How a caller may read the admin UI's task-store views — Tasks, PRs and
|
|
115
136
|
* Sessions (SSP-6.8). Platform admins see every account; an account user
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
resolveCallerScope,
|
|
6
6
|
resolveMemberRepoScope,
|
|
7
7
|
resolveViewerRole,
|
|
8
|
+
scopeCanManageAgent,
|
|
8
9
|
scopeIncludesAgent,
|
|
9
10
|
taskStoreViewScope,
|
|
10
11
|
} from "./caller-scope.ts";
|
|
@@ -288,3 +289,41 @@ describe("resolveMemberRepoScope", () => {
|
|
|
288
289
|
expect(await resolveMemberRepoScope(scoped(["a1"]), deps)).toEqual([]);
|
|
289
290
|
});
|
|
290
291
|
});
|
|
292
|
+
|
|
293
|
+
describe("scopeCanManageAgent", () => {
|
|
294
|
+
const scoped = (
|
|
295
|
+
accountId: string | null,
|
|
296
|
+
role?: "owner" | "member",
|
|
297
|
+
): CallerScope => ({
|
|
298
|
+
kind: "scoped",
|
|
299
|
+
accountId,
|
|
300
|
+
agentIds: ["a1"],
|
|
301
|
+
...(role ? { role } : {}),
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("platform admins (kind all) manage any agent, with or without an account", () => {
|
|
305
|
+
expect(scopeCanManageAgent({ kind: "all" }, "acct-1")).toBe(true);
|
|
306
|
+
expect(scopeCanManageAgent({ kind: "all" }, null)).toBe(true);
|
|
307
|
+
});
|
|
308
|
+
it("the owner of the agent's account manages it", () => {
|
|
309
|
+
expect(scopeCanManageAgent(scoped("acct-1", "owner"), "acct-1")).toBe(true);
|
|
310
|
+
});
|
|
311
|
+
it("an owner of another account does not", () => {
|
|
312
|
+
expect(scopeCanManageAgent(scoped("acct-2", "owner"), "acct-1")).toBe(
|
|
313
|
+
false,
|
|
314
|
+
);
|
|
315
|
+
});
|
|
316
|
+
it("a member of the agent's account does not", () => {
|
|
317
|
+
expect(scopeCanManageAgent(scoped("acct-1", "member"), "acct-1")).toBe(
|
|
318
|
+
false,
|
|
319
|
+
);
|
|
320
|
+
});
|
|
321
|
+
it("unknown role, no account on scope (flag off) or no agent account fail closed", () => {
|
|
322
|
+
expect(scopeCanManageAgent(scoped("acct-1"), "acct-1")).toBe(false);
|
|
323
|
+
expect(scopeCanManageAgent(scoped(null, "owner"), "acct-1")).toBe(false);
|
|
324
|
+
expect(scopeCanManageAgent(scoped("acct-1", "owner"), null)).toBe(false);
|
|
325
|
+
expect(scopeCanManageAgent(scoped("acct-1", "owner"), undefined)).toBe(
|
|
326
|
+
false,
|
|
327
|
+
);
|
|
328
|
+
});
|
|
329
|
+
});
|
package/src/chat.smoke.test.ts
CHANGED
|
@@ -346,7 +346,7 @@ function makeBaseDeps(overrides?: Partial<AdminUIDeps>): AdminUIDeps {
|
|
|
346
346
|
id: "m1",
|
|
347
347
|
agentId,
|
|
348
348
|
email,
|
|
349
|
-
memberId:
|
|
349
|
+
memberId: "member-1",
|
|
350
350
|
createdAt: new Date(),
|
|
351
351
|
}),
|
|
352
352
|
remove: async () => {},
|
|
@@ -1610,8 +1610,7 @@ function makeMemberDeps(
|
|
|
1610
1610
|
? memberOf.map((agentId) => ({
|
|
1611
1611
|
id: `m-${agentId}`,
|
|
1612
1612
|
agentId,
|
|
1613
|
-
email
|
|
1614
|
-
memberId: null,
|
|
1613
|
+
memberId: `member-${email}`,
|
|
1615
1614
|
createdAt: new Date(),
|
|
1616
1615
|
}))
|
|
1617
1616
|
: [],
|
|
@@ -1871,17 +1870,72 @@ describe("chat routes — caller scope matrix (ACM-7.4)", () => {
|
|
|
1871
1870
|
path: (agent: string, thread: string) => string;
|
|
1872
1871
|
init?: () => { headers: Record<string, string>; body: BodyInit };
|
|
1873
1872
|
}[] = [
|
|
1874
|
-
{
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
{
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1873
|
+
{
|
|
1874
|
+
name: "thread view",
|
|
1875
|
+
method: "GET",
|
|
1876
|
+
path: (a, t) => `/admin/chat/${a}/threads/${t}`,
|
|
1877
|
+
},
|
|
1878
|
+
{
|
|
1879
|
+
name: "create thread",
|
|
1880
|
+
method: "POST",
|
|
1881
|
+
path: (a) => `/admin/chat/${a}/threads`,
|
|
1882
|
+
init: () => ({ headers: {}, body: new FormData() }),
|
|
1883
|
+
},
|
|
1884
|
+
{
|
|
1885
|
+
name: "post message (form)",
|
|
1886
|
+
method: "POST",
|
|
1887
|
+
path: (a, t) => `/admin/chat/${a}/threads/${t}/messages`,
|
|
1888
|
+
init: () => ({
|
|
1889
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
1890
|
+
body: "body=hi",
|
|
1891
|
+
}),
|
|
1892
|
+
},
|
|
1893
|
+
{
|
|
1894
|
+
name: "upload",
|
|
1895
|
+
method: "POST",
|
|
1896
|
+
path: (a, t) => `/admin/chat/${a}/threads/${t}/messages/upload`,
|
|
1897
|
+
init: form,
|
|
1898
|
+
},
|
|
1899
|
+
{
|
|
1900
|
+
name: "attachment",
|
|
1901
|
+
method: "GET",
|
|
1902
|
+
path: (a, t) => `/admin/chat/${a}/threads/${t}/messages/m1/attachment`,
|
|
1903
|
+
},
|
|
1904
|
+
{
|
|
1905
|
+
name: "rename",
|
|
1906
|
+
method: "POST",
|
|
1907
|
+
path: (a, t) => `/admin/chat/${a}/threads/${t}/rename`,
|
|
1908
|
+
init: () => json({ title: "x" }),
|
|
1909
|
+
},
|
|
1910
|
+
{
|
|
1911
|
+
name: "delete",
|
|
1912
|
+
method: "POST",
|
|
1913
|
+
path: (a, t) => `/admin/chat/${a}/threads/${t}/delete`,
|
|
1914
|
+
init: () => json({}),
|
|
1915
|
+
},
|
|
1916
|
+
{
|
|
1917
|
+
name: "messages.json GET",
|
|
1918
|
+
method: "GET",
|
|
1919
|
+
path: (a, t) => `/admin/chat/${a}/threads/${t}/messages.json`,
|
|
1920
|
+
},
|
|
1921
|
+
{
|
|
1922
|
+
name: "messages.json POST",
|
|
1923
|
+
method: "POST",
|
|
1924
|
+
path: (a, t) => `/admin/chat/${a}/threads/${t}/messages.json`,
|
|
1925
|
+
init: () => json({ body: "hi" }),
|
|
1926
|
+
},
|
|
1927
|
+
{
|
|
1928
|
+
name: "push subscribe",
|
|
1929
|
+
method: "POST",
|
|
1930
|
+
path: (a) => `/admin/chat/${a}/push/subscribe`,
|
|
1931
|
+
init: () => json({}),
|
|
1932
|
+
},
|
|
1933
|
+
{
|
|
1934
|
+
name: "push unsubscribe",
|
|
1935
|
+
method: "POST",
|
|
1936
|
+
path: (a) => `/admin/chat/${a}/push/unsubscribe`,
|
|
1937
|
+
init: () => json({}),
|
|
1938
|
+
},
|
|
1885
1939
|
];
|
|
1886
1940
|
|
|
1887
1941
|
async function call(
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/drop-email-key-columns-migration.integration.test.ts
|
|
3
|
+
* Migration test for 20261130000000_drop_email_key_columns (ACM-5.1): the
|
|
4
|
+
* email key columns on AgentMember, ChatThreadWatch, PushSubscription and
|
|
5
|
+
* UserNotificationPrefs are dropped and uniques re-keyed on memberId.
|
|
6
|
+
*
|
|
7
|
+
* Replays the real migration files inside a throwaway Postgres schema:
|
|
8
|
+
* - guard: any NULL memberId row aborts the migration, changing nothing.
|
|
9
|
+
* - seeded: fully-stamped rows survive; email columns are gone; the new
|
|
10
|
+
* uniques / primary key are enforced.
|
|
11
|
+
*
|
|
12
|
+
* Requires DATABASE_URL_ADMIN_TEST; skips otherwise.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
16
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
17
|
+
import { Client } from "pg";
|
|
18
|
+
|
|
19
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
20
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
21
|
+
|
|
22
|
+
const MIGRATIONS_DIR = new URL("../prisma/migrations/", import.meta.url);
|
|
23
|
+
const DROP = "20261130000000_drop_email_key_columns";
|
|
24
|
+
|
|
25
|
+
const ALL_MIGRATIONS = readdirSync(MIGRATIONS_DIR, { withFileTypes: true })
|
|
26
|
+
.filter((d) => d.isDirectory())
|
|
27
|
+
.map((d) => d.name)
|
|
28
|
+
.sort();
|
|
29
|
+
|
|
30
|
+
function migrationSql(dir: string): string {
|
|
31
|
+
return readFileSync(new URL(`${dir}/migration.sql`, MIGRATIONS_DIR), "utf8");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
describeOrSkip("drop_email_key_columns migration (ACM-5.1)", () => {
|
|
35
|
+
const schema = `drop_email_keys_${process.pid}_${Date.now()}`;
|
|
36
|
+
let client: Client;
|
|
37
|
+
|
|
38
|
+
const columns = async (table: string): Promise<string[]> =>
|
|
39
|
+
(
|
|
40
|
+
await client.query<{ column_name: string }>(
|
|
41
|
+
`SELECT column_name FROM information_schema.columns
|
|
42
|
+
WHERE table_schema = $1 AND table_name = $2`,
|
|
43
|
+
[schema, table],
|
|
44
|
+
)
|
|
45
|
+
).rows.map((r) => r.column_name);
|
|
46
|
+
|
|
47
|
+
const seed = async (memberId: string | null): Promise<void> => {
|
|
48
|
+
await client.query(
|
|
49
|
+
`INSERT INTO "Agent" ("id","name","updatedAt") VALUES ('a1','a1',now())`,
|
|
50
|
+
);
|
|
51
|
+
await client.query(
|
|
52
|
+
`INSERT INTO "AgentMember" ("id","agentId","email","memberId")
|
|
53
|
+
VALUES ('am1','a1','x@acme.com',$1)`,
|
|
54
|
+
[memberId],
|
|
55
|
+
);
|
|
56
|
+
await client.query(
|
|
57
|
+
`INSERT INTO "ChatThreadWatch" ("id","userEmail","threadId","agentId","memberId","updatedAt")
|
|
58
|
+
VALUES ('w1','x@acme.com','t1','a1',$1,now())`,
|
|
59
|
+
[memberId],
|
|
60
|
+
);
|
|
61
|
+
await client.query(
|
|
62
|
+
`INSERT INTO "PushSubscription" ("id","userEmail","endpoint","p256dh","auth","memberId","updatedAt")
|
|
63
|
+
VALUES ('p1','x@acme.com','https://push/1','k','a',$1,now())`,
|
|
64
|
+
[memberId],
|
|
65
|
+
);
|
|
66
|
+
await client.query(
|
|
67
|
+
`INSERT INTO "UserNotificationPrefs" ("userEmail","memberId","updatedAt")
|
|
68
|
+
VALUES ('x@acme.com',$1,now())`,
|
|
69
|
+
[memberId],
|
|
70
|
+
);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
beforeEach(async () => {
|
|
74
|
+
client = new Client({ connectionString: TEST_DB as string });
|
|
75
|
+
await client.connect();
|
|
76
|
+
await client.query(`CREATE SCHEMA "${schema}"`);
|
|
77
|
+
await client.query(`SET search_path TO "${schema}"`);
|
|
78
|
+
for (const dir of ALL_MIGRATIONS.slice(0, ALL_MIGRATIONS.indexOf(DROP))) {
|
|
79
|
+
await client.query(migrationSql(dir));
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
afterEach(async () => {
|
|
84
|
+
await client.query(`DROP SCHEMA IF EXISTS "${schema}" CASCADE`);
|
|
85
|
+
await client.end();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("is the last migration in apply order", () => {
|
|
89
|
+
expect(ALL_MIGRATIONS.at(-1)).toBe(DROP);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("aborts, changing nothing, while any row has a NULL memberId", async () => {
|
|
93
|
+
await seed(null);
|
|
94
|
+
await expect(client.query(migrationSql(DROP))).rejects.toThrow(
|
|
95
|
+
/ACM-5\.1 aborted.*Stamp each listed row with its AccountMember id, or delete it/,
|
|
96
|
+
);
|
|
97
|
+
expect(await columns("AgentMember")).toContain("email");
|
|
98
|
+
expect(await columns("UserNotificationPrefs")).toContain("userEmail");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("drops the email columns, keeps rows, and re-keys uniques on memberId", async () => {
|
|
102
|
+
await seed("m1");
|
|
103
|
+
await client.query(migrationSql(DROP));
|
|
104
|
+
|
|
105
|
+
for (const [table, col] of [
|
|
106
|
+
["AgentMember", "email"],
|
|
107
|
+
["ChatThreadWatch", "userEmail"],
|
|
108
|
+
["PushSubscription", "userEmail"],
|
|
109
|
+
["UserNotificationPrefs", "userEmail"],
|
|
110
|
+
] as const) {
|
|
111
|
+
expect(await columns(table)).not.toContain(col);
|
|
112
|
+
expect(await columns(table)).toContain("memberId");
|
|
113
|
+
const { rows } = await client.query(
|
|
114
|
+
`SELECT count(*)::int AS n FROM "${table}"`,
|
|
115
|
+
);
|
|
116
|
+
expect(rows[0].n).toBe(1);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
await expect(
|
|
120
|
+
client.query(
|
|
121
|
+
`INSERT INTO "AgentMember" ("id","agentId","memberId") VALUES ('am2','a1','m1')`,
|
|
122
|
+
),
|
|
123
|
+
).rejects.toThrow(/AgentMember_agentId_memberId_key/);
|
|
124
|
+
await expect(
|
|
125
|
+
client.query(
|
|
126
|
+
`INSERT INTO "ChatThreadWatch" ("id","threadId","agentId","memberId","updatedAt")
|
|
127
|
+
VALUES ('w2','t1','a1','m1',now())`,
|
|
128
|
+
),
|
|
129
|
+
).rejects.toThrow(/ChatThreadWatch_memberId_threadId_key/);
|
|
130
|
+
await expect(
|
|
131
|
+
client.query(
|
|
132
|
+
`INSERT INTO "UserNotificationPrefs" ("memberId","updatedAt") VALUES ('m1',now())`,
|
|
133
|
+
),
|
|
134
|
+
).rejects.toThrow(/UserNotificationPrefs_pkey/);
|
|
135
|
+
await expect(
|
|
136
|
+
client.query(
|
|
137
|
+
`INSERT INTO "AgentMember" ("id","agentId") VALUES ('am3','a1')`,
|
|
138
|
+
),
|
|
139
|
+
).rejects.toThrow(/null value/);
|
|
140
|
+
});
|
|
141
|
+
});
|