@app-vitals/agent-admin 1.81.0 → 1.83.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.
@@ -6,9 +6,10 @@
6
6
 
7
7
  import { describe, expect, it } from "bun:test";
8
8
  import {
9
+ type CachedAccessRow,
10
+ type InstallationCachePort,
9
11
  InstallationReposService,
10
- NO_INSTALLATION_REASON,
11
- type InstallationReposPort,
12
+ type InstallationSnapshotRow,
12
13
  userPushHints,
13
14
  } from "./installation-repos-service.ts";
14
15
  import type {
@@ -101,93 +102,197 @@ function noHint(r: { userCanPush?: boolean }): boolean {
101
102
  return r.userCanPush === undefined;
102
103
  }
103
104
 
105
+ const row = (
106
+ repo: string,
107
+ status: string,
108
+ canPush = false,
109
+ ): CachedAccessRow => ({ repo, status, canPush });
110
+
111
+ describe("userPushHints from cached access rows", () => {
112
+ const repos = ["acme/push", "acme/readonly"];
113
+
114
+ it("reports true/false per repo when every repo has an ok row", () => {
115
+ const out = userPushHints(
116
+ { cachedRows: [row("push", "ok", true), row("ReadOnly", "ok", false)] },
117
+ "acme",
118
+ repos,
119
+ );
120
+ expect(out).toEqual([
121
+ { fullName: "acme/push", userCanPush: true },
122
+ { fullName: "acme/readonly", userCanPush: false },
123
+ ]);
124
+ });
125
+
126
+ it("omits every hint when any pair is missing, errored or terminal (never false)", () => {
127
+ for (const second of [
128
+ undefined,
129
+ row("readonly", "error", false),
130
+ row("readonly", "terminal", false),
131
+ ]) {
132
+ const rows = [row("push", "ok", true), ...(second ? [second] : [])];
133
+ expect(
134
+ userPushHints({ cachedRows: rows }, "acme", repos).every(noHint),
135
+ ).toBe(true);
136
+ }
137
+ });
138
+
139
+ it("omits hints with no rows at all", () => {
140
+ expect(userPushHints({ cachedRows: [] }, "acme", repos).every(noHint)).toBe(
141
+ true,
142
+ );
143
+ });
144
+ });
145
+
146
+ const T0 = new Date("2026-11-20T10:00:00Z");
147
+ const snap = (
148
+ over: Partial<InstallationSnapshotRow> = {},
149
+ ): InstallationSnapshotRow => ({
150
+ org: "acme",
151
+ completedAt: T0,
152
+ totalCount: 2,
153
+ lastError: null,
154
+ rateLimitResetAt: null,
155
+ repos: [{ fullName: "acme/push" }, { fullName: "acme/readonly" }],
156
+ ...over,
157
+ });
158
+
104
159
  function build(
105
160
  opts: {
106
161
  agent?: { githubUsername: string | null; appId?: number } | null;
107
- noInstallation?: boolean;
108
- access?: () => Promise<
109
- | { status: "unconfigured" }
110
- | { status: "ok"; access: UserRepoAccessResult }
111
- >;
162
+ snapshots?: InstallationSnapshotRow[];
163
+ rows?: CachedAccessRow[];
112
164
  } = {},
113
165
  ) {
114
- const installation: InstallationReposPort = {
115
- listInstalled: async () =>
116
- opts.noInstallation
117
- ? { status: "none" }
118
- : {
119
- status: "found",
120
- orgs: [{ org: "acme", repos: ["acme/push", "acme/unlisted"] }],
121
- },
122
- listRepos: async () => ({}),
123
- addRepos: async () => {},
124
- removeRepos: async () => {},
166
+ const calls: string[] = [];
167
+ const cache: InstallationCachePort = {
168
+ listAgentSnapshots: async (accountId, appRef) => {
169
+ calls.push(`snapshots:${accountId}:${appRef}`);
170
+ return opts.snapshots ?? [snap()];
171
+ },
172
+ getAccessRows: async (q) => {
173
+ calls.push(`rows:${q.accountId}:${q.org}:${q.username}`);
174
+ return opts.rows ?? [row("push", "ok", true), row("readonly", "ok")];
175
+ },
125
176
  };
126
177
  const agent =
127
178
  opts.agent === undefined
128
179
  ? { id: "a1", repos: [], githubUsername: "dev", appId: 42 }
129
180
  : opts.agent && { id: "a1", repos: [], ...opts.agent };
181
+ const boom = () => {
182
+ throw new Error("GitHub must not be called");
183
+ };
130
184
  const svc = new InstallationReposService({
131
185
  agents: { get: async () => agent },
132
- access:
133
- opts.access ??
134
- (async () => ({ status: "ok", access: accessOf([orgResult()]) })),
135
- installation,
186
+ access: boom,
187
+ installation: {
188
+ listInstalled: boom,
189
+ listRepos: boom,
190
+ addRepos: boom,
191
+ removeRepos: boom,
192
+ },
193
+ cache,
136
194
  });
137
- return { svc };
195
+ return { svc, calls };
138
196
  }
139
197
 
140
- describe("InstallationReposService.list", () => {
141
- it("returns repos per org with hints for a bound user", async () => {
142
- const { svc } = build();
143
- expect(await svc.list("a1")).toEqual({
198
+ describe("InstallationReposService.list (cache read)", () => {
199
+ it("returns cached repos with hints and sync detail for a complete snapshot", async () => {
200
+ const { svc, calls } = build();
201
+ const r = await svc.list("a1");
202
+ expect(r).toMatchObject({
144
203
  status: "ok",
145
204
  appId: 42,
205
+ complete: true,
206
+ syncedAt: T0.toISOString(),
207
+ reposSeen: 2,
208
+ totalCount: 2,
146
209
  orgs: [
147
210
  {
148
211
  org: "acme",
212
+ complete: true,
213
+ syncedAt: T0.toISOString(),
214
+ reposSeen: 2,
215
+ totalCount: 2,
149
216
  repos: [
150
217
  { fullName: "acme/push", userCanPush: true },
151
- { fullName: "acme/unlisted" },
218
+ { fullName: "acme/readonly", userCanPush: false },
152
219
  ],
153
220
  },
154
221
  ],
155
222
  });
223
+ expect(calls[0]).toBe("snapshots:deployment:42");
224
+ });
225
+
226
+ it("omits hints when an access pair is errored or missing", async () => {
227
+ const r = await build({
228
+ rows: [row("push", "ok", true), row("readonly", "error")],
229
+ }).svc.list("a1");
230
+ expect(r.status === "ok" && r.orgs[0]?.repos.every(noHint)).toBe(true);
156
231
  });
157
232
 
158
- it("omits hints for an unbound agent without calling access", async () => {
159
- let called = false;
160
- const { svc } = build({
233
+ it("omits hints and skips the access read for an unbound agent", async () => {
234
+ const { svc, calls } = build({
161
235
  agent: { githubUsername: null, appId: 42 },
162
- access: async () => {
163
- called = true;
164
- return { status: "unconfigured" };
165
- },
166
236
  });
167
237
  const r = await svc.list("a1");
168
- expect(called).toBe(false);
238
+ expect(calls.some((c) => c.startsWith("rows:"))).toBe(false);
169
239
  expect(r.status === "ok" && r.orgs[0]?.repos.every(noHint)).toBe(true);
170
240
  });
171
241
 
172
- it("omits hints when access is unconfigured or the lookup throws", async () => {
173
- for (const access of [
174
- async () => ({ status: "unconfigured" as const }),
175
- async () => {
176
- throw new Error("502");
177
- },
178
- ]) {
179
- const r = await build({ access }).svc.list("a1");
180
- expect(r.status === "ok" && r.orgs[0]?.repos.every(noHint)).toBe(true);
181
- }
242
+ it("an incomplete snapshot is not complete and carries error and rate-limit detail", async () => {
243
+ const reset = new Date("2026-11-20T11:00:00Z");
244
+ const r = await build({
245
+ snapshots: [
246
+ snap({
247
+ completedAt: null,
248
+ totalCount: 5,
249
+ lastError: "boom",
250
+ rateLimitResetAt: reset,
251
+ repos: [{ fullName: "acme/push" }],
252
+ }),
253
+ ],
254
+ }).svc.list("a1");
255
+ expect(r).toMatchObject({
256
+ status: "ok",
257
+ complete: false,
258
+ reposSeen: 1,
259
+ totalCount: 5,
260
+ lastError: "boom",
261
+ rateLimitResetAt: reset.toISOString(),
262
+ });
263
+ expect(r.status === "ok" && r.syncedAt).toBeUndefined();
264
+ expect(r.status === "ok" && r.orgs[0]?.repos.every(noHint)).toBe(true);
182
265
  });
183
266
 
184
- it("is skipped with a reason when the App matches no installation", async () => {
185
- const r = await build({ noInstallation: true }).svc.list("a1");
186
- expect(r).toEqual({
187
- status: "skipped",
188
- appId: 42,
189
- reason: NO_INSTALLATION_REASON,
267
+ it("no snapshots yet: ok, empty, not complete", async () => {
268
+ const r = await build({ snapshots: [] }).svc.list("a1");
269
+ expect(r).toMatchObject({ status: "ok", orgs: [], complete: false });
270
+ });
271
+
272
+ it("uses the agent's account for the cache scope", async () => {
273
+ const calls: string[] = [];
274
+ const svc = new InstallationReposService({
275
+ agents: {
276
+ get: async () => ({
277
+ id: "a1",
278
+ repos: [],
279
+ githubUsername: null,
280
+ appId: 7,
281
+ accountId: "acct-1",
282
+ }),
283
+ },
284
+ access: async () => ({ status: "unconfigured" }),
285
+ installation: {} as never,
286
+ cache: {
287
+ listAgentSnapshots: async (a, r) => {
288
+ calls.push(`${a}:${r}`);
289
+ return [];
290
+ },
291
+ getAccessRows: async () => [],
292
+ },
190
293
  });
294
+ await svc.list("a1");
295
+ expect(calls).toEqual(["acct-1:7"]);
191
296
  });
192
297
 
193
298
  it("reports unknown agent and a missing GH_APP_ID distinctly", async () => {
@@ -198,3 +303,37 @@ describe("InstallationReposService.list", () => {
198
303
  expect(r.status).toBe("no_app_id");
199
304
  });
200
305
  });
306
+
307
+ describe("InstallationReposService.list debug detail", () => {
308
+ it("surfaces attempts, next attempt, live lease and last tick; drops an expired lease", async () => {
309
+ const future = new Date(Date.now() + 60_000);
310
+ const past = new Date(Date.now() - 60_000);
311
+ const live = build({
312
+ snapshots: [
313
+ snap({
314
+ attempts: 2,
315
+ nextAttemptAt: T0,
316
+ lastTickAt: T0,
317
+ leaseHolder: "w1",
318
+ leasedUntil: future,
319
+ lastError: "x",
320
+ }),
321
+ ],
322
+ });
323
+ const r = await live.svc.list("a1");
324
+ if (r.status !== "ok") throw new Error("expected ok");
325
+ expect(r.attempts).toBe(2);
326
+ expect(r.nextAttemptAt).toBe(T0.toISOString());
327
+ expect(r.lastTickAt).toBe(T0.toISOString());
328
+ expect(r.leaseHolder).toBe("w1");
329
+ expect(r.orgs[0]?.lastError).toBe("x");
330
+
331
+ const stale = build({
332
+ snapshots: [snap({ leaseHolder: "w1", leasedUntil: past })],
333
+ });
334
+ const r2 = await stale.svc.list("a1");
335
+ if (r2.status !== "ok") throw new Error("expected ok");
336
+ expect(r2.leaseHolder).toBeUndefined();
337
+ expect(r2.attempts).toBeUndefined();
338
+ });
339
+ });