@app-vitals/agent-admin 1.35.1 → 1.36.1

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.
@@ -1,18 +1,19 @@
1
1
  import { describe, expect, it, test } from "bun:test";
2
2
  import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
3
- import type { InstallationReposPort } from "./installation-repos-service.ts";
4
3
  import {
5
- LOST_ACCESS_AUDIT_PLAN_HASH,
6
- type LostAccessPlanInput,
7
- LostAccessRemovalService,
4
+ GITHUB_SYNC_AUDIT_PLAN_HASH,
5
+ GithubSyncService,
6
+ NOT_ADDABLE_REASON,
8
7
  NOT_PRESENT_REASON,
9
- planLostAccessRemoval,
8
+ planGithubSync,
10
9
  STILL_PUSHABLE_REASON,
11
- } from "./lost-access-removal.ts";
10
+ type GithubSyncPlanInput,
11
+ } from "./github-sync.ts";
12
12
  import type {
13
13
  InstallationAgentRecord,
14
14
  InstallationAuditEntry,
15
15
  } from "./installation-ports.ts";
16
+ import type { InstallationReposPort } from "./installation-repos-service.ts";
16
17
  import type { OpenWorkPort } from "./reconcile-open-work.ts";
17
18
  import { repoLostAccessBlockReason } from "./repo-removal-open-work.ts";
18
19
  import {
@@ -53,15 +54,18 @@ const access = (
53
54
  ...extra,
54
55
  });
55
56
 
57
+ // The user can push to a, d, e; not to b, c.
56
58
  const ACME = org("acme", [
57
59
  repo("acme/a", true),
58
60
  repo("acme/b", false),
59
61
  repo("acme/c", false),
62
+ repo("acme/d", true),
63
+ repo("acme/e", true),
60
64
  ]);
61
65
 
62
66
  const base = (
63
- over: Partial<LostAccessPlanInput> = {},
64
- ): LostAccessPlanInput => ({
67
+ over: Partial<GithubSyncPlanInput> = {},
68
+ ): GithubSyncPlanInput => ({
65
69
  username: "octo",
66
70
  access: access([ACME]),
67
71
  agentRepos: ["acme/a", "acme/b"],
@@ -69,71 +73,104 @@ const base = (
69
73
  ...over,
70
74
  });
71
75
 
72
- describe("planLostAccessRemoval", () => {
73
- test("removes repos the user cannot push to from installation and agent repos", () => {
74
- const p = planLostAccessRemoval(base());
76
+ describe("planGithubSync (preview)", () => {
77
+ test("lists app adds, app removes and agent removes", () => {
78
+ const p = planGithubSync(base());
75
79
  expect(p.skipped).toBe(false);
76
- expect(p.repos).toEqual(["acme/b", "acme/c"]);
77
- expect(p.installationRemoves).toEqual(["acme/b", "acme/c"]);
78
- expect(p.configRemoves).toEqual(["acme/b"]);
80
+ expect(p.appAdds).toEqual(["acme/d", "acme/e"]);
81
+ expect(p.appRemoves).toEqual(["acme/b", "acme/c"]);
82
+ expect(p.agentRemoves).toEqual(["acme/b"]);
83
+ expect(p.removedRepos).toEqual(["acme/b", "acme/c"]);
84
+ });
85
+
86
+ test("agent repos are never added", () => {
87
+ const p = planGithubSync(base({ agentRepos: [] }));
88
+ expect(p.appAdds).toEqual(["acme/d", "acme/e"]);
89
+ expect(p.agentRemoves).toEqual([]);
90
+ expect("agentAdds" in p).toBe(false);
79
91
  });
80
92
 
81
- test("never adds: a pushable repo missing from the agent stays missing", () => {
82
- const p = planLostAccessRemoval(
83
- base({ agentRepos: [], installationRepos: { acme: ["acme/a"] } }),
93
+ test("adds have no cap", () => {
94
+ const many = Array.from({ length: 40 }, (_, i) => repo(`acme/n${i}`, true));
95
+ const p = planGithubSync(
96
+ base({
97
+ access: access([org("acme", many)]),
98
+ agentRepos: [],
99
+ installationRepos: { acme: [] },
100
+ }),
84
101
  );
85
- expect(p.repos).toEqual([]);
86
- expect(p.configRemoves).toEqual([]);
102
+ expect(p.appAdds).toHaveLength(40);
87
103
  });
88
104
 
89
- test("a repo not listed in a complete org listing counts as no access", () => {
90
- const p = planLostAccessRemoval(
91
- base({ agentRepos: ["acme/gone"], installationRepos: {} }),
105
+ test("nothing to do once the lists are applied", () => {
106
+ const p = planGithubSync(
107
+ base({
108
+ agentRepos: ["acme/a"],
109
+ installationRepos: { acme: ["acme/a", "acme/d", "acme/e"] },
110
+ }),
92
111
  );
93
- expect(p.configRemoves).toEqual(["acme/gone"]);
112
+ expect([p.appAdds, p.appRemoves, p.agentRemoves]).toEqual([[], [], []]);
94
113
  });
95
114
 
96
- test("push checks ignore case", () => {
97
- const p = planLostAccessRemoval(
115
+ test("push checks and installed lookups ignore case", () => {
116
+ const p = planGithubSync(
98
117
  base({ agentRepos: ["ACME/A"], installationRepos: { acme: ["Acme/a"] } }),
99
118
  );
100
- expect(p.repos).toEqual([]);
119
+ expect(p.appRemoves).toEqual([]);
120
+ expect(p.agentRemoves).toEqual([]);
121
+ expect(p.appAdds).not.toContain("acme/a");
122
+ });
123
+
124
+ test("a repo missing from a complete org listing counts as no access", () => {
125
+ const p = planGithubSync(
126
+ base({ agentRepos: ["acme/gone"], installationRepos: { acme: [] } }),
127
+ );
128
+ expect(p.agentRemoves).toEqual(["acme/gone"]);
129
+ });
130
+
131
+ test("orgs without the agent App installed get no adds", () => {
132
+ const p = planGithubSync(
133
+ base({
134
+ access: access([ACME, org("other", [repo("other/x", true)])]),
135
+ installationRepos: { acme: [] },
136
+ }),
137
+ );
138
+ expect(p.appAdds.every((r) => r.startsWith("acme/"))).toBe(true);
139
+ expect(p.appAdds).not.toContain("other/x");
101
140
  });
102
141
 
103
142
  test.each([
104
143
  ["partial", { status: "partial" as const, unresolved: 1 }],
105
144
  ["failed", { status: "rate_limited" as const }],
145
+ ["not installed", { status: "lookup_app_not_installed" as const }],
106
146
  [
107
147
  "selected-only",
108
148
  { status: "error" as const, reason: SELECTED_REPOS_REASON },
109
149
  ],
110
- ])("skips a %s org and reports it", (_label, extra) => {
111
- const p = planLostAccessRemoval(
112
- base({ access: access([org("acme", [repo("acme/a", true)], extra)]) }),
150
+ ])("skips a %s org for adds and removes and reports it", (_l, extra) => {
151
+ const p = planGithubSync(
152
+ base({
153
+ access: access([
154
+ org("acme", [repo("acme/d", true), repo("acme/b", false)], extra),
155
+ ]),
156
+ }),
113
157
  );
114
- expect(p.repos).toEqual([]);
158
+ expect([p.appAdds, p.appRemoves, p.agentRemoves]).toEqual([[], [], []]);
115
159
  expect(p.skippedOrgs).toHaveLength(1);
116
160
  expect(p.skippedOrgs[0]?.org).toBe("acme");
117
161
  expect(p.warnings[0]).toContain("acme");
118
162
  });
119
163
 
120
- test("selected-only reason matches the shared access-rules wording", () => {
121
- const p = planLostAccessRemoval(
122
- base({
123
- access: access([org("acme", [], { reason: SELECTED_REPOS_REASON })]),
124
- }),
125
- );
126
- expect(p.skippedOrgs[0]?.reason).toContain("selected repositories");
127
- });
128
-
129
164
  test("an org missing from the access result is skipped, not emptied", () => {
130
- const p = planLostAccessRemoval(
165
+ const p = planGithubSync(
131
166
  base({
132
167
  agentRepos: ["other/x"],
133
168
  installationRepos: { other: ["other/x"] },
169
+ access: access([]),
134
170
  }),
135
171
  );
136
- expect(p.repos).toEqual([]);
172
+ expect(p.appRemoves).toEqual([]);
173
+ expect(p.agentRemoves).toEqual([]);
137
174
  expect(p.skippedOrgs).toEqual([
138
175
  { org: "other", reason: "org was not checked" },
139
176
  ]);
@@ -153,30 +190,55 @@ describe("planLostAccessRemoval", () => {
153
190
  ],
154
191
  ["unresolved", { access: null }, "not resolved"],
155
192
  ])("a %s user skips the whole plan", (_l, over, reason) => {
156
- const p = planLostAccessRemoval(base(over as Partial<LostAccessPlanInput>));
193
+ const p = planGithubSync(base(over as Partial<GithubSyncPlanInput>));
157
194
  expect(p.skipped).toBe(true);
158
195
  expect(p.skipReason).toContain(reason);
159
- expect(p.repos).toEqual([]);
196
+ expect([p.appAdds, p.appRemoves, p.agentRemoves]).toEqual([[], [], []]);
160
197
  });
198
+ });
161
199
 
162
- test("requested limits candidates and reports the ones left alone", () => {
163
- const p = planLostAccessRemoval(
200
+ describe("planGithubSync (confirmed lists)", () => {
201
+ it("re-checks every confirmed repo and reports the ones left alone", () => {
202
+ const p = planGithubSync(
164
203
  base({
165
204
  agentRepos: ["acme/a", "acme/b", "other/x"],
166
- requested: ["acme/a", "acme/b", "acme/zzz", "other/x"],
167
- installationRepos: { acme: ["acme/a", "acme/b", "acme/c"] },
205
+ requested: {
206
+ appAdds: ["acme/d", "acme/b", "acme/c", "other/y"],
207
+ appRemoves: ["acme/b", "acme/a", "acme/zzz"],
208
+ agentRemoves: ["acme/b", "acme/a", "other/x"],
209
+ },
168
210
  }),
169
211
  );
170
- expect(p.repos).toEqual(["acme/b"]);
171
- expect(p.installationRemoves).toEqual(["acme/b"]);
172
- expect(p.notRemoved).toEqual([
173
- { repo: "acme/a", reason: STILL_PUSHABLE_REASON },
174
- { repo: "acme/zzz", reason: NOT_PRESENT_REASON },
175
- {
176
- repo: "other/x",
177
- reason: "removals skipped for org: org was not checked",
178
- },
179
- ]);
212
+ expect(p.appAdds).toEqual(["acme/d"]);
213
+ expect(p.appRemoves).toEqual(["acme/b"]);
214
+ expect(p.agentRemoves).toEqual(["acme/b"]);
215
+ expect(p.notApplied).toContainEqual({
216
+ repo: "acme/b",
217
+ reason: NOT_ADDABLE_REASON,
218
+ });
219
+ expect(p.notApplied).toContainEqual({
220
+ repo: "acme/a",
221
+ reason: STILL_PUSHABLE_REASON,
222
+ });
223
+ expect(p.notApplied).toContainEqual({
224
+ repo: "acme/zzz",
225
+ reason: NOT_PRESENT_REASON,
226
+ });
227
+ expect(p.notApplied).toContainEqual({
228
+ repo: "other/x",
229
+ reason: "skipped for org: org was not checked",
230
+ });
231
+ });
232
+
233
+ it("a confirmed add is never applied to the agent's repos", () => {
234
+ const p = planGithubSync(
235
+ base({
236
+ agentRepos: [],
237
+ requested: { appAdds: ["acme/d"], appRemoves: [], agentRemoves: [] },
238
+ }),
239
+ );
240
+ expect(p.appAdds).toEqual(["acme/d"]);
241
+ expect(p.agentRemoves).toEqual([]);
180
242
  });
181
243
  });
182
244
 
@@ -194,31 +256,35 @@ function harness(
194
256
  const calls: unknown[][] = [];
195
257
  const audits: InstallationAuditEntry[] = [];
196
258
  let repos = opts.agent?.repos ?? ["acme/a", "acme/b"];
259
+ let installed = opts.installed ?? [
260
+ { org: "acme", repos: ["acme/a", "acme/b", "acme/c"] },
261
+ ];
197
262
  const agent: InstallationAgentRecord | null =
198
263
  opts.agent === null
199
264
  ? null
200
- : {
201
- id: "ag",
202
- githubUsername: "octo",
203
- appId: 7,
204
- ...opts.agent,
205
- repos,
206
- };
265
+ : { id: "ag", githubUsername: "octo", appId: 7, ...opts.agent, repos };
207
266
  const installation: Pick<
208
267
  InstallationReposPort,
209
- "listInstalled" | "removeRepos"
268
+ "listInstalled" | "addRepos" | "removeRepos"
210
269
  > = {
211
270
  listInstalled: async (appId) => {
212
271
  calls.push(["listInstalled", appId]);
213
- return (
214
- opts.installed ?? [
215
- { org: "acme", repos: ["acme/a", "acme/b", "acme/c"] },
216
- ]
272
+ return installed;
273
+ },
274
+ addRepos: async (appId, o, r) => {
275
+ calls.push(["addRepos", appId, o, [...r]]);
276
+ installed = installed.map((i) =>
277
+ i.org === o ? { ...i, repos: [...i.repos, ...r] } : i,
217
278
  );
218
279
  },
219
280
  removeRepos: async (appId, o, r) => {
220
281
  calls.push(["removeRepos", appId, o, [...r]]);
221
282
  if (opts.removeFails) throw new Error("github says no");
283
+ installed = installed.map((i) =>
284
+ i.org === o
285
+ ? { ...i, repos: i.repos.filter((x) => !r.includes(x)) }
286
+ : i,
287
+ );
222
288
  },
223
289
  };
224
290
  const openWork: OpenWorkPort | undefined =
@@ -234,7 +300,7 @@ function harness(
234
300
  },
235
301
  ...opts.openWork,
236
302
  };
237
- const svc = new LostAccessRemovalService({
303
+ const svc = new GithubSyncService({
238
304
  agents: {
239
305
  get: async () => (agent ? { ...agent, repos } : null),
240
306
  setRepos: async (_id, next) => {
@@ -256,22 +322,36 @@ function harness(
256
322
  return { svc, calls, audits, repos: () => repos };
257
323
  }
258
324
 
259
- const removeInput = (repos: string[], extra = {}) => ({
325
+ const applyInput = (
326
+ lists: {
327
+ appAdds?: string[];
328
+ appRemoves?: string[];
329
+ agentRemoves?: string[];
330
+ },
331
+ extra = {},
332
+ ) => ({
260
333
  agentId: "ag",
261
334
  caller: "root",
262
- repos,
335
+ appAdds: [],
336
+ appRemoves: [],
337
+ agentRemoves: [],
338
+ ...lists,
263
339
  ...extra,
264
340
  });
265
341
 
266
- describe("LostAccessRemovalService.preview", () => {
267
- it("returns the repos that would be removed plus their open work, changing nothing", async () => {
342
+ describe("GithubSyncService.preview", () => {
343
+ it("returns all three lists plus the open work removals block, changing nothing", async () => {
268
344
  const { svc, calls, audits } = harness();
269
345
  const r = await svc.preview("ag");
270
- expect(r.status).toBe("previewed");
271
- if (r.status !== "previewed") return;
272
- expect(r.plan.repos).toEqual(["acme/b", "acme/c"]);
346
+ if (r.status !== "previewed") throw new Error(r.status);
347
+ expect(r.plan.appAdds).toEqual(["acme/d", "acme/e"]);
348
+ expect(r.plan.appRemoves).toEqual(["acme/b", "acme/c"]);
349
+ expect(r.plan.agentRemoves).toEqual(["acme/b"]);
273
350
  expect(r.openWork).toEqual([{ id: "T-1", repo: "acme/b", title: "fix" }]);
274
- expect(calls.map((c) => c[0])).toEqual(["listInstalled", "list"]);
351
+ expect(calls).toEqual([
352
+ ["listInstalled", 7],
353
+ ["list", "ag", ["acme/b", "acme/c"]],
354
+ ]);
275
355
  expect(audits).toEqual([]);
276
356
  });
277
357
 
@@ -302,7 +382,6 @@ describe("LostAccessRemovalService.preview", () => {
302
382
  const noApp = await harness({ agent: { appId: undefined } }).svc.preview(
303
383
  "ag",
304
384
  );
305
- expect(noApp).toMatchObject({ status: "skipped" });
306
385
  expect(JSON.stringify(noApp)).toContain("GH_APP_ID");
307
386
  });
308
387
 
@@ -313,10 +392,16 @@ describe("LostAccessRemovalService.preview", () => {
313
392
  });
314
393
  });
315
394
 
316
- describe("LostAccessRemovalService.remove", () => {
317
- it("removes installation first, then agent repos, then audits, then blocks", async () => {
395
+ describe("GithubSyncService.apply", () => {
396
+ it("adds, removes, removes agent repos, audits once, then blocks open work", async () => {
318
397
  const { svc, calls, audits, repos } = harness();
319
- const r = await svc.remove(removeInput(["acme/b", "acme/c"]));
398
+ const r = await svc.apply(
399
+ applyInput({
400
+ appAdds: ["acme/d"],
401
+ appRemoves: ["acme/b", "acme/c"],
402
+ agentRemoves: ["acme/b"],
403
+ }),
404
+ );
320
405
  expect(r).toMatchObject({
321
406
  status: "applied",
322
407
  auditId: "audit-1",
@@ -324,6 +409,7 @@ describe("LostAccessRemovalService.remove", () => {
324
409
  });
325
410
  expect(calls).toEqual([
326
411
  ["listInstalled", 7],
412
+ ["addRepos", 7, "acme", ["acme/d"]],
327
413
  ["removeRepos", 7, "acme", ["acme/b", "acme/c"]],
328
414
  ["setRepos", ["acme/a"]],
329
415
  ["audit"],
@@ -335,9 +421,9 @@ describe("LostAccessRemovalService.remove", () => {
335
421
  {
336
422
  agentId: "ag",
337
423
  caller: "root",
338
- planHash: LOST_ACCESS_AUDIT_PLAN_HASH,
424
+ planHash: GITHUB_SYNC_AUDIT_PLAN_HASH,
339
425
  diff: {
340
- installationAdds: [],
426
+ installationAdds: ["acme/d"],
341
427
  installationRemoves: ["acme/b", "acme/c"],
342
428
  configAdds: [],
343
429
  configRemoves: ["acme/b"],
@@ -345,22 +431,30 @@ describe("LostAccessRemovalService.remove", () => {
345
431
  result: { status: "applied" },
346
432
  },
347
433
  ]);
434
+ // A second preview has nothing left to do.
435
+ const again = await svc.preview("ag");
436
+ if (again.status !== "previewed") throw new Error(again.status);
437
+ expect(again.plan.appRemoves).toEqual([]);
438
+ expect(again.plan.agentRemoves).toEqual([]);
439
+ expect(again.plan.appAdds).toEqual(["acme/e"]);
348
440
  });
349
441
 
350
- it("leaves a named repo the user can still push to alone and reports it", async () => {
351
- const { svc, repos } = harness();
352
- const r = await svc.remove(removeInput(["acme/a", "acme/b"]));
442
+ it("leaves confirmed repos that no longer qualify alone and reports them", async () => {
443
+ const { svc } = harness();
444
+ const r = await svc.apply(
445
+ applyInput({ appRemoves: ["acme/a", "acme/b"], appAdds: ["acme/b"] }),
446
+ );
353
447
  if (r.status !== "applied") throw new Error(r.status);
354
- expect(r.plan.repos).toEqual(["acme/b"]);
355
- expect(r.plan.notRemoved).toEqual([
356
- { repo: "acme/a", reason: STILL_PUSHABLE_REASON },
448
+ expect(r.plan.appRemoves).toEqual(["acme/b"]);
449
+ expect(r.plan.notApplied.map((n) => n.repo).sort()).toEqual([
450
+ "acme/a",
451
+ "acme/b",
357
452
  ]);
358
- expect(repos()).toEqual(["acme/a"]);
359
453
  });
360
454
 
361
- it("noop with no audit when nothing named still lacks access", async () => {
455
+ it("noop with no audit when nothing confirmed still applies", async () => {
362
456
  const { svc, calls, audits } = harness();
363
- const r = await svc.remove(removeInput(["acme/a"]));
457
+ const r = await svc.apply(applyInput({ appRemoves: ["acme/a"] }));
364
458
  expect(r.status).toBe("noop");
365
459
  expect(audits).toEqual([]);
366
460
  expect(calls.map((c) => c[0])).toEqual(["listInstalled"]);
@@ -382,40 +476,45 @@ describe("LostAccessRemovalService.remove", () => {
382
476
  },
383
477
  }),
384
478
  ]) {
385
- const r = await h.svc.remove(removeInput(["acme/b"]));
479
+ const r = await h.svc.apply(applyInput({ appRemoves: ["acme/b"] }));
386
480
  expect(r.status).toBe("skipped");
387
481
  expect(h.audits).toEqual([]);
388
482
  expect(h.calls.filter((c) => c[0] !== "listInstalled")).toEqual([]);
389
483
  }
390
484
  });
391
485
 
392
- it("skipped orgs are reported and nothing there is removed", async () => {
486
+ it("skipped orgs get neither adds nor removes", async () => {
393
487
  const { svc, audits } = harness({
394
488
  access: {
395
489
  status: "ok",
396
490
  access: access([
397
- org("acme", [], { reason: SELECTED_REPOS_REASON, status: "error" }),
491
+ org("acme", [repo("acme/d", true)], {
492
+ reason: SELECTED_REPOS_REASON,
493
+ status: "error",
494
+ }),
398
495
  ]),
399
496
  },
400
497
  });
401
- const r = await svc.remove(removeInput(["acme/b"]));
402
- expect(r.status).toBe("noop");
403
- if (r.status !== "noop") return;
498
+ const r = await svc.apply(
499
+ applyInput({ appAdds: ["acme/d"], appRemoves: ["acme/b"] }),
500
+ );
501
+ if (r.status !== "noop") throw new Error(r.status);
404
502
  expect(r.plan.skippedOrgs[0]?.org).toBe("acme");
405
503
  expect(audits).toEqual([]);
406
504
  });
407
505
 
408
- it("more than 10 removals need confirmRemovals", async () => {
506
+ it("more than 10 removals need confirmRemovals; adds do not count", async () => {
409
507
  const many = Array.from({ length: 11 }, (_, i) => `acme/r${i}`);
410
508
  const h = harness({
411
509
  agent: { repos: many },
412
510
  installed: [{ org: "acme", repos: many }],
413
511
  });
414
- const r = await h.svc.remove(removeInput(many));
512
+ const lists = { appRemoves: many, agentRemoves: many, appAdds: ["acme/d"] };
513
+ const r = await h.svc.apply(applyInput(lists));
415
514
  expect(r).toMatchObject({ status: "removal_cap", removals: 11 });
416
515
  expect(h.audits).toEqual([]);
417
516
  expect(h.repos()).toEqual(many);
418
- const ok = await h.svc.remove(removeInput(many, { confirmRemovals: true }));
517
+ const ok = await h.svc.apply(applyInput(lists, { confirmRemovals: true }));
419
518
  expect(ok.status).toBe("applied");
420
519
  expect(h.repos()).toEqual([]);
421
520
  });
@@ -428,30 +527,36 @@ describe("LostAccessRemovalService.remove", () => {
428
527
  },
429
528
  },
430
529
  });
431
- const r = await svc.remove(removeInput(["acme/b"]));
432
- expect(r.status).toBe("applied");
433
- if (r.status !== "applied") return;
530
+ const r = await svc.apply(applyInput({ agentRemoves: ["acme/b"] }));
531
+ if (r.status !== "applied") throw new Error(r.status);
434
532
  expect(r.warnings[0]).toContain("acme/b");
435
533
  expect(r.warnings[0]).toContain("store down");
436
534
  expect(audits).toHaveLength(1);
437
535
  });
438
536
 
439
537
  it("works without an open-work port", async () => {
440
- const r = await harness({ openWork: null }).svc.remove(
441
- removeInput(["acme/b"]),
538
+ const r = await harness({ openWork: null }).svc.apply(
539
+ applyInput({ agentRemoves: ["acme/b"] }),
442
540
  );
443
541
  expect(r).toMatchObject({ status: "applied", warnings: [] });
444
542
  });
445
543
 
446
544
  it("a GitHub failure writes a failed audit with only what landed and blocks nothing", async () => {
447
545
  const { svc, calls, audits, repos } = harness({ removeFails: true });
448
- const r = await svc.remove(removeInput(["acme/b"]));
546
+ const r = await svc.apply(
547
+ applyInput({
548
+ appAdds: ["acme/d"],
549
+ appRemoves: ["acme/b"],
550
+ agentRemoves: ["acme/b"],
551
+ }),
552
+ );
449
553
  expect(r).toMatchObject({ status: "failed", auditId: "audit-1" });
450
554
  expect(repos()).toEqual(["acme/a", "acme/b"]);
451
555
  expect(audits[0]?.result).toEqual({
452
556
  status: "failed",
453
557
  error: "github says no",
454
558
  });
559
+ expect(audits[0]?.diff.installationAdds).toEqual(["acme/d"]);
455
560
  expect(audits[0]?.diff.installationRemoves).toEqual([]);
456
561
  expect(audits[0]?.diff.configRemoves).toEqual([]);
457
562
  expect(calls.some((c) => c[0] === "block")).toBe(false);
@@ -459,7 +564,7 @@ describe("LostAccessRemovalService.remove", () => {
459
564
 
460
565
  it("removes an agent repo even when the App is not installed on its org", async () => {
461
566
  const { svc, calls, repos } = harness({ installed: [] });
462
- const r = await svc.remove(removeInput(["acme/b"]));
567
+ const r = await svc.apply(applyInput({ agentRemoves: ["acme/b"] }));
463
568
  expect(r.status).toBe("applied");
464
569
  expect(calls.some((c) => c[0] === "removeRepos")).toBe(false);
465
570
  expect(repos()).toEqual(["acme/a"]);
@@ -0,0 +1,78 @@
1
+ /**
2
+ * admin/src/installation-ports-fixture.integration.test.ts
3
+ * Installation ports driven by the synthetic enterprise-organization-installation
4
+ * fixture (no app_id), mapped through the real client mapper. No network, no DB.
5
+ */
6
+
7
+ import { describe, expect, it } from "bun:test";
8
+ import { toInstallation } from "./github-enterprise-client.ts";
9
+ import enterpriseOrgInstallations from "./fixtures/enterprise-org-installations.json";
10
+ import { createInstallationReposPort } from "./installation-repos-service.ts";
11
+ import {
12
+ AppNotInstalledError,
13
+ createInstallationPort,
14
+ } from "./installation-ports.ts";
15
+
16
+ const ORG = "example-org";
17
+ const CLIENT_ID = "Iv1.0000000000000000";
18
+ const SLUG = "example-agent-app";
19
+
20
+ function harness() {
21
+ const calls: string[] = [];
22
+ const port = createInstallationPort({
23
+ listOrgInstallations: async () =>
24
+ enterpriseOrgInstallations.map((r) =>
25
+ toInstallation(r as Parameters<typeof toInstallation>[0]),
26
+ ),
27
+ listInstallationRepositories: async (_o, id) => {
28
+ calls.push(`list:${id}`);
29
+ return [{ fullName: `${ORG}/repo-a` }];
30
+ },
31
+ addRepositories: async (_o, id, names) => {
32
+ calls.push(`add:${id}:${names.join(",")}`);
33
+ },
34
+ removeRepositories: async (_o, id, names) => {
35
+ calls.push(`rm:${id}:${names.join(",")}`);
36
+ },
37
+ });
38
+ const reposPort = createInstallationReposPort(port, {
39
+ listInstallableOrganizations: async () => [{ login: ORG }],
40
+ });
41
+ return { calls, port, reposPort };
42
+ }
43
+
44
+ describe("installation ports with the no-app_id fixture", () => {
45
+ for (const [label, identity] of [
46
+ ["client id", { clientId: CLIENT_ID }],
47
+ ["slug", { slug: SLUG.toUpperCase() }],
48
+ ] as const) {
49
+ it(`finds the installation by ${label}`, async () => {
50
+ const { calls, port, reposPort } = harness();
51
+ expect(await port.listRepos(7, [ORG], identity)).toEqual({
52
+ [ORG]: [`${ORG}/repo-a`],
53
+ });
54
+ await port.addRepos(7, ORG, [`${ORG}/repo-b`], identity);
55
+ await port.removeRepos(7, ORG, [`${ORG}/repo-c`], identity);
56
+ expect(await reposPort.listInstalled(7, identity)).toEqual([
57
+ { org: ORG, repos: [`${ORG}/repo-a`] },
58
+ ]);
59
+ expect(calls).toEqual([
60
+ "list:1000001",
61
+ "add:1000001:repo-b",
62
+ "rm:1000001:repo-c",
63
+ "list:1000001",
64
+ ]);
65
+ });
66
+ }
67
+
68
+ it("lists no orgs and throws for an agent with no identifier", async () => {
69
+ const { port, reposPort } = harness();
70
+ await expect(port.listRepos(7, [ORG])).rejects.toBeInstanceOf(
71
+ AppNotInstalledError,
72
+ );
73
+ await expect(
74
+ port.addRepos(7, ORG, [`${ORG}/x`], {}),
75
+ ).rejects.toBeInstanceOf(AppNotInstalledError);
76
+ expect(await reposPort.listInstalled(7)).toEqual([]);
77
+ });
78
+ });