@app-vitals/agent-admin 1.107.0 → 1.107.2
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/admin-access.real.e2e.ts +88 -0
- package/e2e/real/agents-real.real.e2e.ts +116 -0
- package/e2e/real/constants.ts +73 -0
- package/e2e/real/fixture.ts +120 -0
- package/e2e/real/harness.ts +191 -0
- package/e2e/real/member-access-self-serve.real.e2e.ts +114 -0
- package/e2e/real/member-agents.real.e2e.ts +267 -0
- package/e2e/real/member-chat.real.e2e.ts +153 -0
- package/e2e/real/owner-access.real.e2e.ts +237 -0
- package/e2e/real/seed.ts +105 -0
- package/e2e/real/session.ts +29 -0
- package/package.json +2 -2
- package/playwright.config.ts +11 -0
- package/src/accounts-api.smoke.test.ts +0 -11
- package/src/admin-ui-accounts.smoke.test.ts +6 -14
- package/src/admin-ui.ts +2 -1
- package/src/agent-members.integration.test.ts +1 -1
- package/src/chat-credential-config.unit.test.ts +30 -0
- package/src/member-id-stamp.unit.test.ts +81 -0
- package/src/push-service.integration.test.ts +1 -1
- package/src/reconcile-open-work.integration.test.ts +237 -0
- package/src/secrets-equal.ts +8 -0
- package/src/secrets-equal.unit.test.ts +15 -0
- package/src/{member-id-test-support.ts → test-support/member-id-test-support.ts} +2 -2
- package/src/user-notification-prefs.integration.test.ts +1 -1
package/e2e/real/seed.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/e2e/real/seed.ts
|
|
3
|
+
*
|
|
4
|
+
* Seeds the admin database for the real-service e2e layer: two accounts, a
|
|
5
|
+
* platform admin (allowlist only, no row), an owner and a member in account
|
|
6
|
+
* one, an owner in account two, and three agents with different repos. The
|
|
7
|
+
* member is on `alpha` only; `beta` (same account) and `gamma` (other account)
|
|
8
|
+
* are agents the member is not on.
|
|
9
|
+
*
|
|
10
|
+
* Standalone: `ADMIN_E2E_SEED_URL=postgres://... bun admin/e2e/real/seed.ts`.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { createAdminPrismaClient } from "../../src/prisma-client.ts";
|
|
14
|
+
import {
|
|
15
|
+
ACCOUNTS,
|
|
16
|
+
AGENTS,
|
|
17
|
+
CHAT_ADMIN_TOKEN,
|
|
18
|
+
EMAILS,
|
|
19
|
+
THREADS,
|
|
20
|
+
} from "./constants.ts";
|
|
21
|
+
|
|
22
|
+
const MEMBER1_ID = "member-e2e-member1";
|
|
23
|
+
|
|
24
|
+
export async function seedAdminDb(databaseUrl: string): Promise<void> {
|
|
25
|
+
const prisma = createAdminPrismaClient(databaseUrl);
|
|
26
|
+
try {
|
|
27
|
+
for (const account of Object.values(ACCOUNTS)) {
|
|
28
|
+
await prisma.account.create({
|
|
29
|
+
data: { id: account.id, name: account.name, maxAgents: 5 },
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
await prisma.accountMember.createMany({
|
|
33
|
+
data: [
|
|
34
|
+
{ accountId: ACCOUNTS.one.id, email: EMAILS.owner1, role: "owner" },
|
|
35
|
+
{
|
|
36
|
+
id: MEMBER1_ID,
|
|
37
|
+
accountId: ACCOUNTS.one.id,
|
|
38
|
+
email: EMAILS.member1,
|
|
39
|
+
role: "member",
|
|
40
|
+
},
|
|
41
|
+
{ accountId: ACCOUNTS.two.id, email: EMAILS.owner2, role: "owner" },
|
|
42
|
+
],
|
|
43
|
+
});
|
|
44
|
+
for (const agent of Object.values(AGENTS)) {
|
|
45
|
+
await prisma.agent.create({
|
|
46
|
+
data: {
|
|
47
|
+
id: agent.id,
|
|
48
|
+
name: agent.name,
|
|
49
|
+
repos: [...agent.repos],
|
|
50
|
+
accountId: agent.accountId,
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
await prisma.agentMember.create({
|
|
55
|
+
// AgentMember reads key on memberId (AccountMember.id), not email.
|
|
56
|
+
data: {
|
|
57
|
+
agentId: AGENTS.alpha.id,
|
|
58
|
+
email: EMAILS.member1,
|
|
59
|
+
memberId: MEMBER1_ID,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
} finally {
|
|
63
|
+
await prisma.$disconnect();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Seeds one chat thread (with one message) per agent through the real chat
|
|
69
|
+
* service HTTP API, using the seeded admin token. Additive to seedAdminDb.
|
|
70
|
+
*/
|
|
71
|
+
export async function seedChatService(chatUrl: string): Promise<void> {
|
|
72
|
+
const headers = {
|
|
73
|
+
Authorization: `Bearer ${CHAT_ADMIN_TOKEN}`,
|
|
74
|
+
"Content-Type": "application/json",
|
|
75
|
+
};
|
|
76
|
+
for (const t of Object.values(THREADS)) {
|
|
77
|
+
const created = await fetch(`${chatUrl}/threads`, {
|
|
78
|
+
method: "POST",
|
|
79
|
+
headers,
|
|
80
|
+
body: JSON.stringify({ agentId: AGENTS[t.agent].id, title: t.title }),
|
|
81
|
+
});
|
|
82
|
+
if (!created.ok) {
|
|
83
|
+
throw new Error(`seed thread ${t.title} failed: ${created.status}`);
|
|
84
|
+
}
|
|
85
|
+
const thread = (await created.json()) as { id: string };
|
|
86
|
+
const msg = await fetch(`${chatUrl}/threads/${thread.id}/messages`, {
|
|
87
|
+
method: "POST",
|
|
88
|
+
headers,
|
|
89
|
+
body: JSON.stringify({ role: "user", body: t.message }),
|
|
90
|
+
});
|
|
91
|
+
if (!msg.ok) {
|
|
92
|
+
throw new Error(`seed message for ${t.title} failed: ${msg.status}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (import.meta.main) {
|
|
98
|
+
const url = process.env.ADMIN_E2E_SEED_URL;
|
|
99
|
+
if (!url) {
|
|
100
|
+
console.error("ADMIN_E2E_SEED_URL is required");
|
|
101
|
+
process.exit(1);
|
|
102
|
+
}
|
|
103
|
+
await seedAdminDb(url);
|
|
104
|
+
console.log("[admin-e2e] seeded");
|
|
105
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/e2e/real/session.ts
|
|
3
|
+
*
|
|
4
|
+
* Mints an `admin_session` cookie value with the test session secret, in the
|
|
5
|
+
* same shape the admin app issues after a real sign-in (HS256 JWT). No sign-in
|
|
6
|
+
* route is involved; `isAdmin` must agree with the harness allowlist because
|
|
7
|
+
* the admin app re-derives it from the allowlist when it renews a session.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { sign } from "hono/jwt";
|
|
11
|
+
import { EMAILS, SESSION_SECRET } from "./constants.ts";
|
|
12
|
+
|
|
13
|
+
export async function mintSessionCookie(
|
|
14
|
+
email: string,
|
|
15
|
+
secret: string = SESSION_SECRET,
|
|
16
|
+
): Promise<string> {
|
|
17
|
+
const now = Math.floor(Date.now() / 1000);
|
|
18
|
+
return sign(
|
|
19
|
+
{
|
|
20
|
+
userId: `e2e-${email}`,
|
|
21
|
+
email,
|
|
22
|
+
isAdmin: email === EMAILS.admin,
|
|
23
|
+
iat: now,
|
|
24
|
+
exp: now + 3600,
|
|
25
|
+
},
|
|
26
|
+
secret,
|
|
27
|
+
"HS256",
|
|
28
|
+
);
|
|
29
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-vitals/agent-admin",
|
|
3
|
-
"version": "1.107.
|
|
3
|
+
"version": "1.107.2",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/app-vitals/agent.git",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@anthropic-ai/sdk": "0.137.0",
|
|
47
|
-
"@app-vitals/agent-lib": "1.107.
|
|
47
|
+
"@app-vitals/agent-lib": "1.107.2",
|
|
48
48
|
"@hono/zod-openapi": "^1.0.0",
|
|
49
49
|
"@prisma/adapter-pg": "^7.10.0",
|
|
50
50
|
"@prisma/client": "^7.10.0",
|
package/playwright.config.ts
CHANGED
|
@@ -18,7 +18,18 @@ export default defineConfig({
|
|
|
18
18
|
},
|
|
19
19
|
projects: [
|
|
20
20
|
{
|
|
21
|
+
// Fake-data specs: each boots admin/e2e/test-server.ts (in-memory deps).
|
|
21
22
|
name: "chromium",
|
|
23
|
+
testIgnore: "**/*.real.e2e.ts",
|
|
24
|
+
use: { browserName: "chromium" },
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
// Real-service specs: real admin + chat on temporary Postgres databases
|
|
28
|
+
// (ADMIN_E2E_PG_URL), seeded roles. See e2e/real/harness.ts.
|
|
29
|
+
name: "real-services",
|
|
30
|
+
testMatch: "**/*.real.e2e.ts",
|
|
31
|
+
timeout: 60_000,
|
|
32
|
+
retries: 0,
|
|
22
33
|
use: { browserName: "chromium" },
|
|
23
34
|
},
|
|
24
35
|
],
|
|
@@ -228,16 +228,6 @@ describe("behaviour", () => {
|
|
|
228
228
|
});
|
|
229
229
|
});
|
|
230
230
|
|
|
231
|
-
it("POST /accounts with an already-used owner email -> 409", async () => {
|
|
232
|
-
const { app } = build();
|
|
233
|
-
const res = await req(app, "POST", "/accounts", adminHeaders, {
|
|
234
|
-
name: "N",
|
|
235
|
-
ownerEmail: "taken@x.com",
|
|
236
|
-
maxAgents: 1,
|
|
237
|
-
});
|
|
238
|
-
expect(res.status).toBe(409);
|
|
239
|
-
});
|
|
240
|
-
|
|
241
231
|
it("POST /accounts validation failure -> 400", async () => {
|
|
242
232
|
const { app } = build();
|
|
243
233
|
const res = await req(app, "POST", "/accounts", adminHeaders, {
|
|
@@ -255,7 +245,6 @@ describe("behaviour", () => {
|
|
|
255
245
|
trialExpiresAt: null,
|
|
256
246
|
});
|
|
257
247
|
expect(res.status).toBe(200);
|
|
258
|
-
expect(((await res.json()) as { maxAgents: number }).maxAgents).toBe(3);
|
|
259
248
|
expect(calls[0]?.args[1]).toEqual({ maxAgents: 3, trialExpiresAt: null });
|
|
260
249
|
});
|
|
261
250
|
|
|
@@ -355,8 +355,12 @@ describe("/admin/accounts", () => {
|
|
|
355
355
|
|
|
356
356
|
it("suspend and reactivate go through the account lifecycle (SSP-8.2)", async () => {
|
|
357
357
|
const { app, lifecycleCalls } = setup();
|
|
358
|
-
await post(app, "/admin/accounts/a1/suspend", {})
|
|
359
|
-
|
|
358
|
+
expect((await post(app, "/admin/accounts/a1/suspend", {})).status).toBe(
|
|
359
|
+
302,
|
|
360
|
+
);
|
|
361
|
+
expect((await post(app, "/admin/accounts/a1/reactivate", {})).status).toBe(
|
|
362
|
+
302,
|
|
363
|
+
);
|
|
360
364
|
expect(lifecycleCalls).toEqual([
|
|
361
365
|
{ id: "a1", data: { status: "suspended" } },
|
|
362
366
|
{ id: "a1", data: { status: "active" } },
|
|
@@ -372,18 +376,6 @@ describe("/admin/accounts", () => {
|
|
|
372
376
|
expect(account.status).toBe("trial_expired");
|
|
373
377
|
});
|
|
374
378
|
|
|
375
|
-
it("suspend and reactivate set status", async () => {
|
|
376
|
-
const { app, account } = setup();
|
|
377
|
-
expect((await post(app, "/admin/accounts/a1/suspend", {})).status).toBe(
|
|
378
|
-
302,
|
|
379
|
-
);
|
|
380
|
-
expect(account.status).toBe("suspended");
|
|
381
|
-
expect((await post(app, "/admin/accounts/a1/reactivate", {})).status).toBe(
|
|
382
|
-
302,
|
|
383
|
-
);
|
|
384
|
-
expect(account.status).toBe("active");
|
|
385
|
-
});
|
|
386
|
-
|
|
387
379
|
it("adds member and owner; rejects invalid and duplicate emails", async () => {
|
|
388
380
|
const { app, members } = setup();
|
|
389
381
|
expect(
|
package/src/admin-ui.ts
CHANGED
|
@@ -171,6 +171,7 @@ import {
|
|
|
171
171
|
renderPwaHeadTags,
|
|
172
172
|
sanitizeStartUrl,
|
|
173
173
|
} from "./pwa.ts";
|
|
174
|
+
import { secretsEqual } from "./secrets-equal.ts";
|
|
174
175
|
import {
|
|
175
176
|
DEFAULT_SELF_SERVE_CONTACT_EMAIL,
|
|
176
177
|
type SelfServeConfig,
|
|
@@ -5420,7 +5421,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
5420
5421
|
}
|
|
5421
5422
|
const auth = c.req.header("authorization") ?? "";
|
|
5422
5423
|
const presented = auth.startsWith("Bearer ") ? auth.slice(7) : "";
|
|
5423
|
-
if (!presented || presented
|
|
5424
|
+
if (!presented || !secretsEqual(presented, pushWebhookToken)) {
|
|
5424
5425
|
return c.json({ error: "unauthorized" }, 401);
|
|
5425
5426
|
}
|
|
5426
5427
|
let body: {
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
10
10
|
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
11
11
|
import { AgentMemberService } from "./agent-members.ts";
|
|
12
|
-
import { resetMemberIdFixture } from "./member-id-test-support.ts";
|
|
12
|
+
import { resetMemberIdFixture } from "./test-support/member-id-test-support.ts";
|
|
13
13
|
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
14
14
|
|
|
15
15
|
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import { parseChatCredentialConfig } from "./chat-credential-config.ts";
|
|
3
|
+
|
|
4
|
+
describe("parseChatCredentialConfig", () => {
|
|
5
|
+
it("is enabled only for the exact value 'enabled'", () => {
|
|
6
|
+
expect(
|
|
7
|
+
parseChatCredentialConfig({ SHIPWRIGHT_CHAT_TOKEN_MINT_ENABLED: "enabled" }),
|
|
8
|
+
).toEqual({ enabled: true });
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("is disabled when the flag is unset", () => {
|
|
12
|
+
expect(parseChatCredentialConfig({})).toEqual({ enabled: false });
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// The parser never throws: any invalid/unrecognized value fails closed.
|
|
16
|
+
it.each(["true", "1", "ENABLED", "Enabled", " enabled", "disabled", ""])(
|
|
17
|
+
"fails closed (disabled) for invalid value %p",
|
|
18
|
+
(value) => {
|
|
19
|
+
expect(
|
|
20
|
+
parseChatCredentialConfig({ SHIPWRIGHT_CHAT_TOKEN_MINT_ENABLED: value }),
|
|
21
|
+
).toEqual({ enabled: false });
|
|
22
|
+
},
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
it("ignores unrelated env vars", () => {
|
|
26
|
+
expect(parseChatCredentialConfig({ OTHER: "enabled" })).toEqual({
|
|
27
|
+
enabled: false,
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import { deleteMemberRows, stampMemberId } from "./member-id-stamp.ts";
|
|
3
|
+
import type { PrismaTransactionClient } from "./prisma-tx.ts";
|
|
4
|
+
|
|
5
|
+
const TABLES = [
|
|
6
|
+
"agentMember",
|
|
7
|
+
"chatThreadWatch",
|
|
8
|
+
"pushSubscription",
|
|
9
|
+
"userNotificationPrefs",
|
|
10
|
+
] as const;
|
|
11
|
+
|
|
12
|
+
type Call = { table: string; op: string; args: any };
|
|
13
|
+
|
|
14
|
+
function fakeClient(counts: Partial<Record<(typeof TABLES)[number], number>> = {}) {
|
|
15
|
+
const calls: Call[] = [];
|
|
16
|
+
const client: any = {};
|
|
17
|
+
for (const t of TABLES) {
|
|
18
|
+
client[t] = {
|
|
19
|
+
updateMany: async (args: any) => {
|
|
20
|
+
calls.push({ table: t, op: "updateMany", args });
|
|
21
|
+
return { count: counts[t] ?? 0 };
|
|
22
|
+
},
|
|
23
|
+
deleteMany: async (args: any) => {
|
|
24
|
+
calls.push({ table: t, op: "deleteMany", args });
|
|
25
|
+
return { count: counts[t] ?? 0 };
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return { client: client as PrismaTransactionClient, calls };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe("stampMemberId", () => {
|
|
33
|
+
it("stamps null-memberId rows in all four tables with a normalized, case-insensitive email", async () => {
|
|
34
|
+
const { client, calls } = fakeClient();
|
|
35
|
+
await stampMemberId(client, " Bob@X.com ", "m1");
|
|
36
|
+
expect(calls.map((c) => c.table).sort()).toEqual([...TABLES].sort());
|
|
37
|
+
const email = { equals: "bob@x.com", mode: "insensitive" };
|
|
38
|
+
for (const c of calls) {
|
|
39
|
+
expect(c.op).toBe("updateMany");
|
|
40
|
+
expect(c.args.data).toEqual({ memberId: "m1" });
|
|
41
|
+
expect(c.args.where.memberId).toBeNull();
|
|
42
|
+
const field = c.table === "agentMember" ? "email" : "userEmail";
|
|
43
|
+
expect(c.args.where[field]).toEqual(email);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("returns the summed count of changed rows", async () => {
|
|
48
|
+
const { client } = fakeClient({ agentMember: 2, pushSubscription: 3 });
|
|
49
|
+
expect(await stampMemberId(client, "a@b.c", "m1")).toBe(5);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("returns 0 when nothing matches", async () => {
|
|
53
|
+
const { client } = fakeClient();
|
|
54
|
+
expect(await stampMemberId(client, "a@b.c", "m1")).toBe(0);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("deleteMemberRows", () => {
|
|
59
|
+
it("matches rows by memberId or by email with null memberId, leaving other members' rows", async () => {
|
|
60
|
+
const { client, calls } = fakeClient();
|
|
61
|
+
await deleteMemberRows(client, "Bob@X.com", "m1");
|
|
62
|
+
expect(calls.map((c) => c.table).sort()).toEqual([...TABLES].sort());
|
|
63
|
+
const email = { equals: "bob@x.com", mode: "insensitive" };
|
|
64
|
+
for (const c of calls) {
|
|
65
|
+
expect(c.op).toBe("deleteMany");
|
|
66
|
+
const field = c.table === "agentMember" ? "email" : "userEmail";
|
|
67
|
+
expect(c.args.where).toEqual({
|
|
68
|
+
OR: [{ memberId: "m1" }, { [field]: email, memberId: null }],
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("returns the summed count of deleted rows", async () => {
|
|
74
|
+
const { client } = fakeClient({
|
|
75
|
+
agentMember: 1,
|
|
76
|
+
chatThreadWatch: 1,
|
|
77
|
+
userNotificationPrefs: 4,
|
|
78
|
+
});
|
|
79
|
+
expect(await deleteMemberRows(client, "a@b.c", "m1")).toBe(6);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
10
10
|
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
11
|
-
import { resetMemberIdFixture } from "./member-id-test-support.ts";
|
|
11
|
+
import { resetMemberIdFixture } from "./test-support/member-id-test-support.ts";
|
|
12
12
|
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
13
13
|
import { PushService } from "./push-service.ts";
|
|
14
14
|
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/reconcile-open-work.integration.test.ts
|
|
3
|
+
* T-019: the OpenWorkPort contract driven through the real GithubSyncService
|
|
4
|
+
* repo-removal reconcile, with the real cache and a real GitHubReconcileAudit
|
|
5
|
+
* row in Postgres. Covers the repo-removal path (preview lists, apply blocks
|
|
6
|
+
* per repo with the lost-access reason) and partial-failure behaviour (a port
|
|
7
|
+
* failure becomes a warning, never an error; a failed apply blocks nothing).
|
|
8
|
+
*
|
|
9
|
+
* Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
13
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
14
|
+
import { DEPLOYMENT_WIDE_ACCOUNT_KEY } from "./github-agent-installation-crawler.ts";
|
|
15
|
+
import { GitHubRepoCacheService } from "./github-repo-cache.ts";
|
|
16
|
+
import { GithubSyncService } from "./github-sync.ts";
|
|
17
|
+
import { PrismaInstallationAudit } from "./installation-ports.ts";
|
|
18
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
19
|
+
import type { OpenWorkItem, OpenWorkPort } from "./reconcile-open-work.ts";
|
|
20
|
+
import { repoLostAccessBlockReason } from "./repo-removal-open-work.ts";
|
|
21
|
+
|
|
22
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
23
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
24
|
+
|
|
25
|
+
const AGENT_ID = "oow-it-agent";
|
|
26
|
+
const ORG = "oow-eng";
|
|
27
|
+
const APP_ID = 888;
|
|
28
|
+
const KEEP = `${ORG}/keep`;
|
|
29
|
+
const GONE_A = `${ORG}/gone-a`;
|
|
30
|
+
const GONE_B = `${ORG}/gone-b`;
|
|
31
|
+
const ALL = [KEEP, GONE_A, GONE_B];
|
|
32
|
+
|
|
33
|
+
/** In-memory open-work port that records every call; `failOn` repos reject. */
|
|
34
|
+
function recordingPort(failOn: string[] = []) {
|
|
35
|
+
const work: OpenWorkItem[] = ALL.map((repo) => ({
|
|
36
|
+
id: `task-${repo.split("/")[1]}`,
|
|
37
|
+
repo,
|
|
38
|
+
}));
|
|
39
|
+
const blocked: { repos: readonly string[]; reason: string }[] = [];
|
|
40
|
+
const port: OpenWorkPort = {
|
|
41
|
+
listOpenWork: async (_agent, repos) =>
|
|
42
|
+
work.filter((w) => repos.includes(w.repo)),
|
|
43
|
+
blockOpenWork: async (_agent, repos, reason) => {
|
|
44
|
+
if (repos.some((r) => failOn.includes(r))) {
|
|
45
|
+
throw new Error(`store down for ${repos.join(",")}`);
|
|
46
|
+
}
|
|
47
|
+
blocked.push({ repos, reason });
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
return { port, blocked };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
describeOrSkip("reconcile-open-work (integration)", () => {
|
|
54
|
+
let prisma: PrismaClient;
|
|
55
|
+
let cache: GitHubRepoCacheService;
|
|
56
|
+
const checkedAt = new Date(Date.now() - 60_000);
|
|
57
|
+
|
|
58
|
+
const clean = async () => {
|
|
59
|
+
await prisma.gitHubReconcileAudit.deleteMany({
|
|
60
|
+
where: { agentId: AGENT_ID },
|
|
61
|
+
});
|
|
62
|
+
await prisma.gitHubRepoAccess.deleteMany({
|
|
63
|
+
where: { accountId: DEPLOYMENT_WIDE_ACCOUNT_KEY, org: ORG },
|
|
64
|
+
});
|
|
65
|
+
await prisma.gitHubCachedRepo.deleteMany();
|
|
66
|
+
await prisma.gitHubRepoSnapshot.deleteMany();
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
async function seed() {
|
|
70
|
+
const snap = await cache.upsertSnapshot(
|
|
71
|
+
{
|
|
72
|
+
accountId: DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
73
|
+
appKind: "agent" as const,
|
|
74
|
+
appRef: String(APP_ID),
|
|
75
|
+
org: ORG,
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
completedAt: checkedAt,
|
|
79
|
+
totalCount: ALL.length,
|
|
80
|
+
repositorySelection: "selected",
|
|
81
|
+
lastError: null,
|
|
82
|
+
},
|
|
83
|
+
);
|
|
84
|
+
await cache.upsertRepos(
|
|
85
|
+
snap.id,
|
|
86
|
+
ALL.map((fullName, i) => ({ githubId: i + 1, fullName })),
|
|
87
|
+
checkedAt,
|
|
88
|
+
);
|
|
89
|
+
for (const full of ALL) {
|
|
90
|
+
const canPush = full === KEEP;
|
|
91
|
+
await prisma.gitHubRepoAccess.create({
|
|
92
|
+
data: {
|
|
93
|
+
accountId: DEPLOYMENT_WIDE_ACCOUNT_KEY,
|
|
94
|
+
org: ORG,
|
|
95
|
+
repo: full.split("/")[1] as string,
|
|
96
|
+
username: "dev",
|
|
97
|
+
canPush,
|
|
98
|
+
level: canPush ? "write" : "read",
|
|
99
|
+
checkedAt,
|
|
100
|
+
status: "ok",
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function service(openWork: OpenWorkPort | undefined, removeFails = false) {
|
|
107
|
+
const calls: string[] = [];
|
|
108
|
+
let repos = [...ALL];
|
|
109
|
+
const svc = new GithubSyncService({
|
|
110
|
+
agents: {
|
|
111
|
+
get: async () => ({
|
|
112
|
+
id: AGENT_ID,
|
|
113
|
+
githubUsername: "dev",
|
|
114
|
+
appId: APP_ID,
|
|
115
|
+
repos,
|
|
116
|
+
}),
|
|
117
|
+
setRepos: async (_id, next) => {
|
|
118
|
+
calls.push("setRepos");
|
|
119
|
+
repos = next;
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
access: async () => {
|
|
123
|
+
throw new Error("live resolver must not be used");
|
|
124
|
+
},
|
|
125
|
+
cache,
|
|
126
|
+
lookupRepos: async () => [
|
|
127
|
+
{ org: ORG, completedAt: checkedAt, repos: ALL },
|
|
128
|
+
],
|
|
129
|
+
verifyPush: async (_u, repo) => repo === KEEP,
|
|
130
|
+
installation: {
|
|
131
|
+
listInstalled: async () => ({
|
|
132
|
+
status: "found",
|
|
133
|
+
orgs: [{ org: ORG, repos: ALL }],
|
|
134
|
+
}),
|
|
135
|
+
addRepos: async () => {},
|
|
136
|
+
removeRepos: async () => {
|
|
137
|
+
calls.push("removeRepos");
|
|
138
|
+
if (removeFails) throw new Error("installation PATCH failed");
|
|
139
|
+
},
|
|
140
|
+
setRepositorySelection: async () => {},
|
|
141
|
+
},
|
|
142
|
+
audit: new PrismaInstallationAudit(prisma),
|
|
143
|
+
...(openWork ? { openWork } : {}),
|
|
144
|
+
});
|
|
145
|
+
return { svc, calls };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const removeBoth = {
|
|
149
|
+
agentId: AGENT_ID,
|
|
150
|
+
caller: "root",
|
|
151
|
+
appAdds: [],
|
|
152
|
+
appRemoves: [GONE_A, GONE_B],
|
|
153
|
+
agentRemoves: [GONE_A, GONE_B],
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
beforeEach(async () => {
|
|
157
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
158
|
+
cache = new GitHubRepoCacheService(prisma);
|
|
159
|
+
await clean();
|
|
160
|
+
await seed();
|
|
161
|
+
});
|
|
162
|
+
afterEach(async () => {
|
|
163
|
+
await clean();
|
|
164
|
+
await prisma.$disconnect();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("preview lists the open work on exactly the repos the reconcile would remove", async () => {
|
|
168
|
+
const { port, blocked } = recordingPort();
|
|
169
|
+
const { svc } = service(port);
|
|
170
|
+
const r = await svc.preview(AGENT_ID);
|
|
171
|
+
if (r.status !== "previewed") throw new Error(r.status);
|
|
172
|
+
expect(r.plan.removedRepos.sort()).toEqual([GONE_A, GONE_B]);
|
|
173
|
+
expect(r.openWork.map((w) => w.repo).sort()).toEqual([GONE_A, GONE_B]);
|
|
174
|
+
expect(r.warnings).toEqual([]);
|
|
175
|
+
expect(blocked).toEqual([]); // preview is read-only
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("apply blocks each removed repo's work separately with the lost-access reason", async () => {
|
|
179
|
+
const { port, blocked } = recordingPort();
|
|
180
|
+
const { svc, calls } = service(port);
|
|
181
|
+
const r = await svc.apply(removeBoth);
|
|
182
|
+
if (r.status !== "applied") throw new Error(r.status);
|
|
183
|
+
expect(r.warnings).toEqual([]);
|
|
184
|
+
expect(calls).toEqual(["removeRepos", "setRepos"]);
|
|
185
|
+
expect(blocked).toEqual([
|
|
186
|
+
{ repos: [GONE_A], reason: repoLostAccessBlockReason(GONE_A) },
|
|
187
|
+
{ repos: [GONE_B], reason: repoLostAccessBlockReason(GONE_B) },
|
|
188
|
+
]);
|
|
189
|
+
const rows = await prisma.gitHubReconcileAudit.findMany({
|
|
190
|
+
where: { agentId: AGENT_ID },
|
|
191
|
+
});
|
|
192
|
+
expect(rows).toHaveLength(1);
|
|
193
|
+
expect(rows[0]?.result).toEqual({ status: "applied" });
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("a port failure on one repo is a warning; the other repo is still blocked and the apply still succeeds", async () => {
|
|
197
|
+
const { port, blocked } = recordingPort([GONE_A]);
|
|
198
|
+
const { svc } = service(port);
|
|
199
|
+
const r = await svc.apply(removeBoth);
|
|
200
|
+
if (r.status !== "applied") throw new Error(r.status);
|
|
201
|
+
expect(r.warnings).toEqual([
|
|
202
|
+
`open work for ${GONE_A} was not blocked: store down for ${GONE_A}`,
|
|
203
|
+
]);
|
|
204
|
+
expect(blocked.map((b) => b.repos)).toEqual([[GONE_B]]);
|
|
205
|
+
const rows = await prisma.gitHubReconcileAudit.findMany({
|
|
206
|
+
where: { agentId: AGENT_ID },
|
|
207
|
+
});
|
|
208
|
+
expect(rows[0]?.result).toEqual({ status: "applied" });
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("a failed apply blocks no work and records the failure in the audit row", async () => {
|
|
212
|
+
const { port, blocked } = recordingPort();
|
|
213
|
+
const { svc, calls } = service(port, true);
|
|
214
|
+
const r = await svc.apply(removeBoth);
|
|
215
|
+
if (r.status !== "failed") throw new Error(r.status);
|
|
216
|
+
expect(r.error).toBe("installation PATCH failed");
|
|
217
|
+
expect(calls).toEqual(["removeRepos"]);
|
|
218
|
+
expect(blocked).toEqual([]);
|
|
219
|
+
const rows = await prisma.gitHubReconcileAudit.findMany({
|
|
220
|
+
where: { agentId: AGENT_ID },
|
|
221
|
+
});
|
|
222
|
+
expect(rows[0]?.result).toEqual({
|
|
223
|
+
status: "failed",
|
|
224
|
+
error: "installation PATCH failed",
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("without a port the reconcile still applies and reports no open work or warnings", async () => {
|
|
229
|
+
const { svc } = service(undefined);
|
|
230
|
+
const p = await svc.preview(AGENT_ID);
|
|
231
|
+
if (p.status !== "previewed") throw new Error(p.status);
|
|
232
|
+
expect(p.openWork).toEqual([]);
|
|
233
|
+
const r = await svc.apply(removeBoth);
|
|
234
|
+
if (r.status !== "applied") throw new Error(r.status);
|
|
235
|
+
expect(r.warnings).toEqual([]);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { createHash, timingSafeEqual } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
/** Constant-time string compare; hashes first so length doesn't leak. */
|
|
4
|
+
export function secretsEqual(a: string, b: string): boolean {
|
|
5
|
+
const ha = createHash("sha256").update(a).digest();
|
|
6
|
+
const hb = createHash("sha256").update(b).digest();
|
|
7
|
+
return timingSafeEqual(ha, hb);
|
|
8
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { secretsEqual } from "./secrets-equal.ts";
|
|
3
|
+
|
|
4
|
+
describe("secretsEqual", () => {
|
|
5
|
+
test("equal strings match", () => {
|
|
6
|
+
expect(secretsEqual("abc", "abc")).toBe(true);
|
|
7
|
+
});
|
|
8
|
+
test("different strings of same length differ", () => {
|
|
9
|
+
expect(secretsEqual("abc", "abd")).toBe(false);
|
|
10
|
+
});
|
|
11
|
+
test("different lengths differ without throwing", () => {
|
|
12
|
+
expect(secretsEqual("abc", "abcd")).toBe(false);
|
|
13
|
+
expect(secretsEqual("", "abc")).toBe(false);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* admin/src/member-id-test-support.ts
|
|
2
|
+
* admin/src/test-support/member-id-test-support.ts
|
|
3
3
|
* Shared seed helpers for the ACM-4.1 per-table memberId integration tests.
|
|
4
4
|
* Wipes the four per-user tables plus accounts, then seeds one account with
|
|
5
5
|
* two members and one agent.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type { PrismaClient } from "
|
|
8
|
+
import type { PrismaClient } from "../../prisma/client/client.ts";
|
|
9
9
|
|
|
10
10
|
export async function resetMemberIdFixture(prisma: PrismaClient) {
|
|
11
11
|
await prisma.userNotificationPrefs.deleteMany();
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
10
10
|
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
11
|
-
import { resetMemberIdFixture } from "./member-id-test-support.ts";
|
|
11
|
+
import { resetMemberIdFixture } from "./test-support/member-id-test-support.ts";
|
|
12
12
|
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
13
13
|
import { UserNotificationPrefsService } from "./user-notification-prefs.ts";
|
|
14
14
|
|