@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.
- package/Dockerfile +4 -0
- package/account-move.example.json +22 -0
- package/e2e/real/admin-access.real.e2e.ts +77 -68
- package/e2e/real/agent-management.real.e2e.ts +165 -143
- package/e2e/real/owner-access.real.e2e.ts +228 -212
- package/openapi.platform.json +11 -6
- package/package.json +2 -2
- package/src/account-agent-create.ts +18 -9
- package/src/account-agent-create.unit.test.ts +12 -14
- package/src/account-created-notifier.ts +1 -1
- package/src/account-move.integration.test.ts +594 -0
- package/src/account-move.ts +867 -0
- package/src/account-move.unit.test.ts +206 -0
- package/src/account-onboarding.integration.test.ts +69 -0
- package/src/account-onboarding.ts +29 -3
- package/src/account-trial-expiry-sweeper.ts +1 -1
- package/src/accounts-api.integration.test.ts +5 -2
- package/src/accounts-api.smoke.test.ts +21 -5
- package/src/accounts-api.ts +12 -15
- package/src/admin-boot-config.ts +2 -0
- package/src/admin-boot-config.unit.test.ts +27 -0
- package/src/admin-boot.ts +11 -10
- package/src/admin-local-agent.smoke.test.ts +65 -0
- package/src/admin-ui-account-pages.ts +2 -3
- package/src/admin-ui-account-pages.unit.test.ts +5 -3
- package/src/admin-ui-account.smoke.test.ts +7 -3
- package/src/admin-ui-account.ts +10 -15
- package/src/admin-ui-accounts.smoke.test.ts +4 -4
- package/src/admin-ui-accounts.ts +7 -11
- package/src/admin-ui-pages.ts +16 -5
- package/src/admin-ui-pages.unit.test.ts +25 -20
- package/src/admin-ui-settings-nav-context.smoke.test.ts +24 -0
- package/src/admin-ui-settings.smoke.test.ts +17 -1
- package/src/admin-ui.smoke.test.ts +167 -2
- package/src/admin-ui.ts +75 -50
- package/src/agent-management-access.integration.test.ts +18 -6
- package/src/agent-manifest.ts +2 -1
- package/src/agent-provisioner.integration.test.ts +10 -10
- package/src/agent-provisioner.ts +10 -1
- package/src/agent-provisioner.unit.test.ts +110 -36
- package/src/agent-templates-api.integration.test.ts +23 -5
- package/src/agent-templates-api.ts +11 -8
- package/src/agents-api.smoke.test.ts +106 -8
- package/src/agents-api.ts +5 -3
- package/src/agents.integration.test.ts +112 -2
- package/src/agents.ts +37 -6
- package/src/caller-scope.integration.test.ts +8 -6
- package/src/caller-scope.ts +32 -26
- package/src/caller-scope.unit.test.ts +15 -11
- package/src/github-enterprise-isolation.unit.test.ts +17 -0
- package/src/move-accounts.ts +201 -0
- package/src/openapi-schemas.ts +6 -0
- package/src/platform-routes.ts +2 -2
- package/src/self-serve-config.ts +8 -8
- package/src/self-serve-config.unit.test.ts +3 -10
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/account-move.integration.test.ts
|
|
3
|
+
*
|
|
4
|
+
* multi-account-2.6: the admin account move (accounts, members, agents and
|
|
5
|
+
* deployment-wide settings) against real Postgres: preview writes nothing,
|
|
6
|
+
* apply moves everything in one transaction, a rerun is a no-op, a reverse
|
|
7
|
+
* puts back exactly what moved, and every refusal happens before any write.
|
|
8
|
+
* A moved member keeps its id and every row keyed by it.
|
|
9
|
+
*
|
|
10
|
+
* Requires DATABASE_URL_ADMIN_TEST; skips otherwise.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
14
|
+
import { existsSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
|
|
15
|
+
import { tmpdir } from "node:os";
|
|
16
|
+
import { join } from "node:path";
|
|
17
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
18
|
+
import {
|
|
19
|
+
type AccountMoveMapping,
|
|
20
|
+
applyMove,
|
|
21
|
+
applyReverse,
|
|
22
|
+
MoveRefusedError,
|
|
23
|
+
parseMapping,
|
|
24
|
+
parseReceipt,
|
|
25
|
+
planMove,
|
|
26
|
+
planReverse,
|
|
27
|
+
} from "./account-move.ts";
|
|
28
|
+
import { runMoveAccountsCli } from "./move-accounts.ts";
|
|
29
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
30
|
+
|
|
31
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
32
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
33
|
+
|
|
34
|
+
/** Everything the move touches, in a comparable shape. */
|
|
35
|
+
async function snapshot(prisma: PrismaClient) {
|
|
36
|
+
const [accounts, members, agents, apps, slack, templates] = await Promise.all(
|
|
37
|
+
[
|
|
38
|
+
prisma.account.findMany({
|
|
39
|
+
select: { id: true, name: true, maxAgents: true },
|
|
40
|
+
orderBy: { id: "asc" },
|
|
41
|
+
}),
|
|
42
|
+
prisma.accountMember.findMany({
|
|
43
|
+
select: { id: true, accountId: true, email: true, role: true },
|
|
44
|
+
orderBy: { email: "asc" },
|
|
45
|
+
}),
|
|
46
|
+
prisma.agent.findMany({
|
|
47
|
+
select: { id: true, accountId: true },
|
|
48
|
+
orderBy: { id: "asc" },
|
|
49
|
+
}),
|
|
50
|
+
prisma.gitHubEnterpriseApp.findMany({
|
|
51
|
+
select: { id: true, accountId: true, role: true },
|
|
52
|
+
orderBy: { id: "asc" },
|
|
53
|
+
}),
|
|
54
|
+
prisma.slackConfigToken.findMany({
|
|
55
|
+
select: { id: true, accountId: true },
|
|
56
|
+
orderBy: { id: "asc" },
|
|
57
|
+
}),
|
|
58
|
+
prisma.agentTemplate.findMany({
|
|
59
|
+
select: { id: true, accountId: true, specialty: true },
|
|
60
|
+
orderBy: { id: "asc" },
|
|
61
|
+
}),
|
|
62
|
+
],
|
|
63
|
+
);
|
|
64
|
+
return { accounts, members, agents, apps, slack, templates };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
describeOrSkip("admin account move (integration)", () => {
|
|
68
|
+
let prisma: PrismaClient;
|
|
69
|
+
let ownerId: string;
|
|
70
|
+
let memberId: string;
|
|
71
|
+
|
|
72
|
+
const mapping = (): AccountMoveMapping =>
|
|
73
|
+
parseMapping({
|
|
74
|
+
accounts: [
|
|
75
|
+
{
|
|
76
|
+
id: "example-account-a",
|
|
77
|
+
name: "Example Account A",
|
|
78
|
+
maxAgents: 2,
|
|
79
|
+
agents: ["example-agent-one", "agent-two-id"],
|
|
80
|
+
members: [
|
|
81
|
+
{ email: "Owner@Example.com", role: "owner" },
|
|
82
|
+
{ email: "member@example.com" },
|
|
83
|
+
],
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
id: "example-home",
|
|
87
|
+
maxAgents: 5,
|
|
88
|
+
agents: ["example-agent-three"],
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
settings: [
|
|
92
|
+
{ setting: "githubLookupApp", toAccountId: "example-home" },
|
|
93
|
+
{ setting: "slackConfigToken", toAccountId: "example-home" },
|
|
94
|
+
{
|
|
95
|
+
setting: "agentTemplate",
|
|
96
|
+
specialty: null,
|
|
97
|
+
toAccountId: "example-home",
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
beforeEach(async () => {
|
|
103
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
104
|
+
await prisma.userNotificationPrefs.deleteMany();
|
|
105
|
+
await prisma.chatThreadWatch.deleteMany();
|
|
106
|
+
await prisma.pushSubscription.deleteMany();
|
|
107
|
+
await prisma.agentMember.deleteMany();
|
|
108
|
+
await prisma.accountInvite.deleteMany();
|
|
109
|
+
await prisma.accountMember.deleteMany();
|
|
110
|
+
await prisma.agentTemplate.deleteMany();
|
|
111
|
+
await prisma.slackConfigToken.deleteMany();
|
|
112
|
+
await prisma.gitHubEnterpriseApp.deleteMany();
|
|
113
|
+
await prisma.agent.deleteMany();
|
|
114
|
+
await prisma.account.deleteMany();
|
|
115
|
+
|
|
116
|
+
await prisma.account.create({
|
|
117
|
+
data: { id: "example-home", name: "Example Home", maxAgents: 0 },
|
|
118
|
+
});
|
|
119
|
+
ownerId = (
|
|
120
|
+
await prisma.accountMember.create({
|
|
121
|
+
data: {
|
|
122
|
+
accountId: "example-home",
|
|
123
|
+
email: "owner@example.com",
|
|
124
|
+
role: "member",
|
|
125
|
+
},
|
|
126
|
+
})
|
|
127
|
+
).id;
|
|
128
|
+
memberId = (
|
|
129
|
+
await prisma.accountMember.create({
|
|
130
|
+
data: {
|
|
131
|
+
accountId: "example-home",
|
|
132
|
+
email: "member@example.com",
|
|
133
|
+
role: "member",
|
|
134
|
+
},
|
|
135
|
+
})
|
|
136
|
+
).id;
|
|
137
|
+
await prisma.accountMember.create({
|
|
138
|
+
data: {
|
|
139
|
+
accountId: "example-home",
|
|
140
|
+
email: "admin@example.com",
|
|
141
|
+
role: "owner",
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
await prisma.agent.create({
|
|
145
|
+
data: {
|
|
146
|
+
id: "agent-one-id",
|
|
147
|
+
name: "example-agent-one",
|
|
148
|
+
accountId: "example-home",
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
await prisma.agent.create({ data: { id: "agent-two-id", name: "two" } });
|
|
152
|
+
await prisma.agent.create({
|
|
153
|
+
data: { id: "agent-three-id", name: "example-agent-three" },
|
|
154
|
+
});
|
|
155
|
+
await prisma.gitHubEnterpriseApp.create({
|
|
156
|
+
data: {
|
|
157
|
+
id: "lookup-app",
|
|
158
|
+
role: "lookup",
|
|
159
|
+
appId: "1",
|
|
160
|
+
slug: "example-lookup",
|
|
161
|
+
privateKeyEncrypted: "x",
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
await prisma.slackConfigToken.create({
|
|
165
|
+
data: { id: "slack-token", refreshTokenEncrypted: "x" },
|
|
166
|
+
});
|
|
167
|
+
await prisma.agentTemplate.create({ data: { id: "global-template" } });
|
|
168
|
+
// Rows tied to the member id that must survive the move.
|
|
169
|
+
await prisma.agentMember.create({
|
|
170
|
+
data: { agentId: "agent-one-id", memberId: ownerId },
|
|
171
|
+
});
|
|
172
|
+
await prisma.chatThreadWatch.create({
|
|
173
|
+
data: { threadId: "t-1", agentId: "agent-one-id", memberId: ownerId },
|
|
174
|
+
});
|
|
175
|
+
await prisma.pushSubscription.create({
|
|
176
|
+
data: { endpoint: "ep-1", p256dh: "p", auth: "a", memberId: ownerId },
|
|
177
|
+
});
|
|
178
|
+
await prisma.userNotificationPrefs.create({ data: { memberId: ownerId } });
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
afterEach(async () => {
|
|
182
|
+
await prisma.$disconnect();
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("previews by default and writes nothing", async () => {
|
|
186
|
+
const before = await snapshot(prisma);
|
|
187
|
+
const plan = await planMove(prisma, mapping());
|
|
188
|
+
expect(plan.changes.map((c) => c.type)).toEqual([
|
|
189
|
+
"createAccount",
|
|
190
|
+
"updateAccount",
|
|
191
|
+
"moveMember",
|
|
192
|
+
"moveMember",
|
|
193
|
+
"moveAgent",
|
|
194
|
+
"moveAgent",
|
|
195
|
+
"moveAgent",
|
|
196
|
+
"moveSetting",
|
|
197
|
+
"moveSetting",
|
|
198
|
+
"moveSetting",
|
|
199
|
+
]);
|
|
200
|
+
expect(await snapshot(prisma)).toEqual(before);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("applies every change, keeps member ids and their rows, leaves no deployment-wide settings", async () => {
|
|
204
|
+
const receipt = await applyMove(prisma, mapping());
|
|
205
|
+
expect(receipt.changes).toHaveLength(10);
|
|
206
|
+
|
|
207
|
+
const a = await prisma.account.findUniqueOrThrow({
|
|
208
|
+
where: { id: "example-account-a" },
|
|
209
|
+
});
|
|
210
|
+
expect(a).toMatchObject({ name: "Example Account A", maxAgents: 2 });
|
|
211
|
+
const home = await prisma.account.findUniqueOrThrow({
|
|
212
|
+
where: { id: "example-home" },
|
|
213
|
+
});
|
|
214
|
+
expect(home).toMatchObject({ name: "Example Home", maxAgents: 5 });
|
|
215
|
+
|
|
216
|
+
const owner = await prisma.accountMember.findUniqueOrThrow({
|
|
217
|
+
where: { email: "owner@example.com" },
|
|
218
|
+
});
|
|
219
|
+
expect(owner).toMatchObject({
|
|
220
|
+
id: ownerId,
|
|
221
|
+
accountId: "example-account-a",
|
|
222
|
+
role: "owner",
|
|
223
|
+
});
|
|
224
|
+
const member = await prisma.accountMember.findUniqueOrThrow({
|
|
225
|
+
where: { email: "member@example.com" },
|
|
226
|
+
});
|
|
227
|
+
expect(member).toMatchObject({
|
|
228
|
+
id: memberId,
|
|
229
|
+
accountId: "example-account-a",
|
|
230
|
+
role: "member",
|
|
231
|
+
});
|
|
232
|
+
expect(
|
|
233
|
+
await prisma.agentMember.count({ where: { memberId: ownerId } }),
|
|
234
|
+
).toBe(1);
|
|
235
|
+
expect(
|
|
236
|
+
await prisma.chatThreadWatch.count({ where: { memberId: ownerId } }),
|
|
237
|
+
).toBe(1);
|
|
238
|
+
expect(
|
|
239
|
+
await prisma.pushSubscription.count({ where: { memberId: ownerId } }),
|
|
240
|
+
).toBe(1);
|
|
241
|
+
expect(
|
|
242
|
+
await prisma.userNotificationPrefs.count({
|
|
243
|
+
where: { memberId: ownerId },
|
|
244
|
+
}),
|
|
245
|
+
).toBe(1);
|
|
246
|
+
|
|
247
|
+
const agents = await prisma.agent.findMany({ orderBy: { id: "asc" } });
|
|
248
|
+
expect(agents.map((x) => [x.id, x.accountId])).toEqual([
|
|
249
|
+
["agent-one-id", "example-account-a"],
|
|
250
|
+
["agent-three-id", "example-home"],
|
|
251
|
+
["agent-two-id", "example-account-a"],
|
|
252
|
+
]);
|
|
253
|
+
|
|
254
|
+
expect(
|
|
255
|
+
await prisma.gitHubEnterpriseApp.count({ where: { accountId: null } }),
|
|
256
|
+
).toBe(0);
|
|
257
|
+
expect(
|
|
258
|
+
await prisma.slackConfigToken.count({ where: { accountId: null } }),
|
|
259
|
+
).toBe(0);
|
|
260
|
+
expect(
|
|
261
|
+
await prisma.agentTemplate.count({ where: { accountId: null } }),
|
|
262
|
+
).toBe(0);
|
|
263
|
+
expect(
|
|
264
|
+
(
|
|
265
|
+
await prisma.slackConfigToken.findUniqueOrThrow({
|
|
266
|
+
where: { id: "slack-token" },
|
|
267
|
+
})
|
|
268
|
+
).accountId,
|
|
269
|
+
).toBe("example-home");
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("warns, without refusing, about an account left with no owner, access outside the new account and an agent limit", async () => {
|
|
273
|
+
const plan = await planMove(
|
|
274
|
+
prisma,
|
|
275
|
+
parseMapping({
|
|
276
|
+
accounts: [
|
|
277
|
+
{
|
|
278
|
+
id: "example-account-a",
|
|
279
|
+
name: "A",
|
|
280
|
+
maxAgents: 0,
|
|
281
|
+
agents: ["agent-two-id"],
|
|
282
|
+
members: [
|
|
283
|
+
{ email: "admin@example.com", role: "owner" },
|
|
284
|
+
{ email: "owner@example.com" },
|
|
285
|
+
],
|
|
286
|
+
},
|
|
287
|
+
],
|
|
288
|
+
}),
|
|
289
|
+
);
|
|
290
|
+
const text = plan.warnings.join("\n");
|
|
291
|
+
expect(text).toContain('account "example-home" is left with no owner');
|
|
292
|
+
expect(text).toContain(
|
|
293
|
+
'member "owner@example.com" keeps access to 1 agent(s) outside account "example-account-a"',
|
|
294
|
+
);
|
|
295
|
+
expect(text).toContain(
|
|
296
|
+
'account "example-account-a" will have 1 agents, over its limit of 0',
|
|
297
|
+
);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("is safe to rerun: a second preview and apply change nothing", async () => {
|
|
301
|
+
await applyMove(prisma, mapping());
|
|
302
|
+
const after = await snapshot(prisma);
|
|
303
|
+
expect((await planMove(prisma, mapping())).changes).toEqual([]);
|
|
304
|
+
const second = await applyMove(prisma, mapping());
|
|
305
|
+
expect(second.changes).toEqual([]);
|
|
306
|
+
expect(await snapshot(prisma)).toEqual(after);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("reverses exactly what it moved", async () => {
|
|
310
|
+
const before = await snapshot(prisma);
|
|
311
|
+
const receipt = parseReceipt(
|
|
312
|
+
JSON.parse(JSON.stringify(await applyMove(prisma, mapping()))),
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
const preview = await planReverse(prisma, receipt);
|
|
316
|
+
expect(preview.changes.at(-1)).toEqual({
|
|
317
|
+
type: "deleteAccount",
|
|
318
|
+
accountId: "example-account-a",
|
|
319
|
+
});
|
|
320
|
+
const afterMove = await snapshot(prisma);
|
|
321
|
+
expect(await snapshot(prisma)).toEqual(afterMove);
|
|
322
|
+
|
|
323
|
+
await applyReverse(prisma, receipt);
|
|
324
|
+
expect(await snapshot(prisma)).toEqual(before);
|
|
325
|
+
// The member rows still point at the same member.
|
|
326
|
+
expect(
|
|
327
|
+
await prisma.userNotificationPrefs.count({
|
|
328
|
+
where: { memberId: ownerId },
|
|
329
|
+
}),
|
|
330
|
+
).toBe(1);
|
|
331
|
+
|
|
332
|
+
// Reversing again finds nothing left to put back.
|
|
333
|
+
const again = await planReverse(prisma, receipt);
|
|
334
|
+
expect(again.changes).toEqual([]);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it("reverse skips rows changed since the move and keeps a created account that is no longer empty", async () => {
|
|
338
|
+
const receipt = await applyMove(prisma, mapping());
|
|
339
|
+
await prisma.agent.update({
|
|
340
|
+
where: { id: "agent-two-id" },
|
|
341
|
+
data: { accountId: "example-home" },
|
|
342
|
+
});
|
|
343
|
+
await prisma.agent.create({
|
|
344
|
+
data: {
|
|
345
|
+
id: "agent-new-id",
|
|
346
|
+
name: "created-after",
|
|
347
|
+
accountId: "example-account-a",
|
|
348
|
+
},
|
|
349
|
+
});
|
|
350
|
+
const plan = await applyReverse(prisma, receipt);
|
|
351
|
+
expect(plan.skipped.join("\n")).toContain("agent-two-id");
|
|
352
|
+
expect(plan.skipped.join("\n")).toContain("example-account-a");
|
|
353
|
+
expect(
|
|
354
|
+
await prisma.account.count({ where: { id: "example-account-a" } }),
|
|
355
|
+
).toBe(1);
|
|
356
|
+
expect(
|
|
357
|
+
(await prisma.agent.findUniqueOrThrow({ where: { id: "agent-one-id" } }))
|
|
358
|
+
.accountId,
|
|
359
|
+
).toBe("example-home");
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
describe("refusals happen before any write", () => {
|
|
363
|
+
async function expectRefused(raw: unknown, ...reasons: RegExp[]) {
|
|
364
|
+
const before = await snapshot(prisma);
|
|
365
|
+
const m = parseMapping(raw);
|
|
366
|
+
let refused: unknown;
|
|
367
|
+
try {
|
|
368
|
+
await applyMove(prisma, m);
|
|
369
|
+
} catch (e) {
|
|
370
|
+
refused = e;
|
|
371
|
+
}
|
|
372
|
+
expect(refused).toBeInstanceOf(MoveRefusedError);
|
|
373
|
+
const text = (refused as MoveRefusedError).reasons.join("\n");
|
|
374
|
+
for (const r of reasons) expect(text).toMatch(r);
|
|
375
|
+
// The preview refuses the same way.
|
|
376
|
+
await expect(planMove(prisma, m)).rejects.toBeInstanceOf(
|
|
377
|
+
MoveRefusedError,
|
|
378
|
+
);
|
|
379
|
+
expect(await snapshot(prisma)).toEqual(before);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
it("refuses an unknown agent", async () => {
|
|
383
|
+
await expectRefused(
|
|
384
|
+
{
|
|
385
|
+
accounts: [
|
|
386
|
+
{ id: "example-account-a", name: "A", agents: ["no-such-agent"] },
|
|
387
|
+
],
|
|
388
|
+
},
|
|
389
|
+
/unknown agent "no-such-agent"/,
|
|
390
|
+
);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("refuses an agent name that matches more than one agent", async () => {
|
|
394
|
+
await prisma.agent.create({ data: { id: "dup-1", name: "same-name" } });
|
|
395
|
+
await prisma.agent.create({ data: { id: "dup-2", name: "same-name" } });
|
|
396
|
+
await expectRefused(
|
|
397
|
+
{
|
|
398
|
+
accounts: [
|
|
399
|
+
{ id: "example-account-a", name: "A", agents: ["same-name"] },
|
|
400
|
+
],
|
|
401
|
+
},
|
|
402
|
+
/agent "same-name" matches 2 agents/,
|
|
403
|
+
);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("refuses an agent listed in two accounts", async () => {
|
|
407
|
+
await expectRefused(
|
|
408
|
+
{
|
|
409
|
+
accounts: [
|
|
410
|
+
{ id: "example-account-a", name: "A", agents: ["agent-two-id"] },
|
|
411
|
+
{ id: "example-home", agents: ["two"] },
|
|
412
|
+
],
|
|
413
|
+
},
|
|
414
|
+
/agent "two" \(agent-two-id\) is listed in 2 accounts/,
|
|
415
|
+
);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it("refuses an unknown member", async () => {
|
|
419
|
+
await expectRefused(
|
|
420
|
+
{
|
|
421
|
+
accounts: [
|
|
422
|
+
{
|
|
423
|
+
id: "example-account-a",
|
|
424
|
+
name: "A",
|
|
425
|
+
members: [{ email: "nobody@example.com" }],
|
|
426
|
+
},
|
|
427
|
+
],
|
|
428
|
+
},
|
|
429
|
+
/unknown member "nobody@example.com"/,
|
|
430
|
+
);
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
it("refuses a member needed in two accounts", async () => {
|
|
434
|
+
await expectRefused(
|
|
435
|
+
{
|
|
436
|
+
accounts: [
|
|
437
|
+
{
|
|
438
|
+
id: "example-account-a",
|
|
439
|
+
name: "A",
|
|
440
|
+
members: [{ email: "member@example.com" }],
|
|
441
|
+
},
|
|
442
|
+
{
|
|
443
|
+
id: "example-home",
|
|
444
|
+
members: [{ email: "MEMBER@example.com", role: "owner" }],
|
|
445
|
+
},
|
|
446
|
+
],
|
|
447
|
+
},
|
|
448
|
+
/member "member@example.com" is listed in 2 accounts/,
|
|
449
|
+
);
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
it("refuses a conflicting existing setting", async () => {
|
|
453
|
+
await prisma.slackConfigToken.create({
|
|
454
|
+
data: { accountId: "example-home", refreshTokenEncrypted: "y" },
|
|
455
|
+
});
|
|
456
|
+
await expectRefused(
|
|
457
|
+
{
|
|
458
|
+
settings: [
|
|
459
|
+
{ setting: "slackConfigToken", toAccountId: "example-home" },
|
|
460
|
+
],
|
|
461
|
+
},
|
|
462
|
+
/account "example-home" already has its own Slack config token/,
|
|
463
|
+
);
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
it("refuses a setting with no deployment-wide row to move", async () => {
|
|
467
|
+
await expectRefused(
|
|
468
|
+
{
|
|
469
|
+
settings: [
|
|
470
|
+
{ setting: "githubInstallerApp", toAccountId: "example-home" },
|
|
471
|
+
],
|
|
472
|
+
},
|
|
473
|
+
/no deployment-wide GitHub installer app/,
|
|
474
|
+
);
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
it("refuses a setting for an account that does not exist", async () => {
|
|
478
|
+
await expectRefused(
|
|
479
|
+
{
|
|
480
|
+
settings: [
|
|
481
|
+
{ setting: "slackConfigToken", toAccountId: "no-such-account" },
|
|
482
|
+
],
|
|
483
|
+
},
|
|
484
|
+
/account "no-such-account" does not exist/,
|
|
485
|
+
);
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
it("refuses a new account with no name", async () => {
|
|
489
|
+
await expectRefused(
|
|
490
|
+
{ accounts: [{ id: "example-account-a" }] },
|
|
491
|
+
/account "example-account-a" does not exist and has no name/,
|
|
492
|
+
);
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
it("lists every reason at once", async () => {
|
|
496
|
+
await expectRefused(
|
|
497
|
+
{
|
|
498
|
+
accounts: [
|
|
499
|
+
{
|
|
500
|
+
id: "example-account-a",
|
|
501
|
+
name: "A",
|
|
502
|
+
agents: ["no-such-agent"],
|
|
503
|
+
members: [{ email: "nobody@example.com" }],
|
|
504
|
+
},
|
|
505
|
+
],
|
|
506
|
+
},
|
|
507
|
+
/unknown agent/,
|
|
508
|
+
/unknown member/,
|
|
509
|
+
);
|
|
510
|
+
});
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
describe("CLI", () => {
|
|
514
|
+
let dir: string;
|
|
515
|
+
beforeEach(() => {
|
|
516
|
+
dir = mkdtempSync(join(tmpdir(), "account-move-"));
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
function cli(args: string[]) {
|
|
520
|
+
const out: string[] = [];
|
|
521
|
+
const err: string[] = [];
|
|
522
|
+
return runMoveAccountsCli(args, {
|
|
523
|
+
db: prisma,
|
|
524
|
+
stdout: (s) => out.push(s),
|
|
525
|
+
stderr: (s) => err.push(s),
|
|
526
|
+
}).then((code) => ({ code, out: out.join("\n"), err: err.join("\n") }));
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
it("previews, applies with a receipt, and reverses", async () => {
|
|
530
|
+
const before = await snapshot(prisma);
|
|
531
|
+
const mappingPath = join(dir, "mapping.json");
|
|
532
|
+
const receiptPath = join(dir, "receipt.json");
|
|
533
|
+
writeFileSync(
|
|
534
|
+
mappingPath,
|
|
535
|
+
JSON.stringify({
|
|
536
|
+
accounts: [
|
|
537
|
+
{
|
|
538
|
+
id: "example-account-a",
|
|
539
|
+
name: "A",
|
|
540
|
+
agents: ["example-agent-one"],
|
|
541
|
+
},
|
|
542
|
+
],
|
|
543
|
+
}),
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
const preview = await cli(["--mapping", mappingPath]);
|
|
547
|
+
expect(preview.code).toBe(0);
|
|
548
|
+
expect(preview.out).toContain("PREVIEW");
|
|
549
|
+
expect(preview.out).toContain("example-agent-one");
|
|
550
|
+
expect(await snapshot(prisma)).toEqual(before);
|
|
551
|
+
|
|
552
|
+
const applied = await cli([
|
|
553
|
+
"--mapping",
|
|
554
|
+
mappingPath,
|
|
555
|
+
"--apply",
|
|
556
|
+
"--receipt",
|
|
557
|
+
receiptPath,
|
|
558
|
+
]);
|
|
559
|
+
expect(applied.code).toBe(0);
|
|
560
|
+
expect(existsSync(receiptPath)).toBe(true);
|
|
561
|
+
expect(
|
|
562
|
+
JSON.parse(readFileSync(receiptPath, "utf8")).changes,
|
|
563
|
+
).toHaveLength(2);
|
|
564
|
+
|
|
565
|
+
const rerun = await cli(["--mapping", mappingPath]);
|
|
566
|
+
expect(rerun.out).toContain("nothing to change");
|
|
567
|
+
|
|
568
|
+
const reverse = await cli(["--reverse", receiptPath, "--apply"]);
|
|
569
|
+
expect(reverse.code).toBe(0);
|
|
570
|
+
expect(await snapshot(prisma)).toEqual(before);
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
it("exits 1 on a refusal and writes no receipt", async () => {
|
|
574
|
+
const mappingPath = join(dir, "mapping.json");
|
|
575
|
+
const receiptPath = join(dir, "receipt.json");
|
|
576
|
+
writeFileSync(
|
|
577
|
+
mappingPath,
|
|
578
|
+
JSON.stringify({
|
|
579
|
+
accounts: [{ id: "example-account-a", name: "A", agents: ["nope"] }],
|
|
580
|
+
}),
|
|
581
|
+
);
|
|
582
|
+
const r = await cli([
|
|
583
|
+
"--mapping",
|
|
584
|
+
mappingPath,
|
|
585
|
+
"--apply",
|
|
586
|
+
"--receipt",
|
|
587
|
+
receiptPath,
|
|
588
|
+
]);
|
|
589
|
+
expect(r.code).toBe(1);
|
|
590
|
+
expect(r.err).toContain("REFUSED");
|
|
591
|
+
expect(existsSync(receiptPath)).toBe(false);
|
|
592
|
+
});
|
|
593
|
+
});
|
|
594
|
+
});
|