@app-vitals/agent-admin 1.26.0 → 1.27.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.
@@ -0,0 +1,191 @@
1
+ /**
2
+ * admin/src/reconcile-apply-service.unit.test.ts
3
+ * Unit tests for the open-work port call and the apply guards (fakes only).
4
+ */
5
+
6
+ import { describe, expect, it } from "bun:test";
7
+ import type { GithubAccessPreview } from "./github-access-preview.ts";
8
+ import {
9
+ createInstallationPort,
10
+ type ReconcileApplyDeps,
11
+ ReconcileApplyService,
12
+ } from "./reconcile-apply-service.ts";
13
+ import type { OpenWorkPort } from "./reconcile-open-work.ts";
14
+ import type { UserRepoAccessResult } from "./user-repo-access-resolver.ts";
15
+
16
+ function access(pushable: string[]): UserRepoAccessResult {
17
+ return {
18
+ username: "dev",
19
+ outcome: "ok",
20
+ userVerified: true,
21
+ complete: true,
22
+ unresolved: 0,
23
+ checkedAt: "2026-11-11T00:00:00.000Z",
24
+ orgs: [
25
+ {
26
+ org: "acme",
27
+ status: "ok",
28
+ unresolved: 0,
29
+ repos: pushable.map((fullName) => ({
30
+ fullName,
31
+ level: "write" as const,
32
+ canPush: true,
33
+ })),
34
+ },
35
+ ],
36
+ };
37
+ }
38
+
39
+ function build(opts: {
40
+ repos: string[];
41
+ pushable: string[];
42
+ installed?: string[];
43
+ openWork?: OpenWorkPort;
44
+ preview?: GithubAccessPreview;
45
+ }) {
46
+ const state = { repos: [...opts.repos] };
47
+ const calls: string[] = [];
48
+ const audits: unknown[] = [];
49
+ const deps: ReconcileApplyDeps = {
50
+ agents: {
51
+ get: async () => ({
52
+ id: "a1",
53
+ repos: state.repos,
54
+ githubUsername: "dev",
55
+ }),
56
+ setRepos: async (_id, repos) => {
57
+ calls.push("config");
58
+ state.repos = repos;
59
+ },
60
+ },
61
+ access: async () =>
62
+ opts.preview ?? { status: "ok", access: access(opts.pushable) },
63
+ installation: {
64
+ listRepos: async () => ({ acme: opts.installed ?? opts.repos }),
65
+ addRepos: async () => {
66
+ calls.push("inst-add");
67
+ },
68
+ removeRepos: async () => {
69
+ calls.push("inst-remove");
70
+ },
71
+ },
72
+ audit: {
73
+ write: async (e) => {
74
+ audits.push(e);
75
+ return "audit-1";
76
+ },
77
+ },
78
+ openWork: opts.openWork,
79
+ };
80
+ return { svc: new ReconcileApplyService(deps), state, calls, audits };
81
+ }
82
+
83
+ describe("ReconcileApplyService (unit)", () => {
84
+ it("calls blockOpenWork with the reconcile reason for each removed repo", async () => {
85
+ const blocked: { repos: readonly string[]; reason: string }[] = [];
86
+ const openWork: OpenWorkPort = {
87
+ listOpenWork: async (_a, repos) =>
88
+ repos.map((repo) => ({ id: `t-${repo}`, repo })),
89
+ blockOpenWork: async (_a, repos, reason) => {
90
+ blocked.push({ repos, reason });
91
+ },
92
+ };
93
+ const { svc } = build({
94
+ repos: ["acme/a", "acme/b"],
95
+ pushable: ["acme/a"],
96
+ openWork,
97
+ });
98
+ const plan = await svc.plan("a1");
99
+ const res = await svc.apply({
100
+ agentId: "a1",
101
+ caller: "dan@x.io",
102
+ planHash: plan.planHash,
103
+ });
104
+ expect(res.status).toBe("applied");
105
+ expect(blocked).toEqual([
106
+ {
107
+ repos: ["acme/b"],
108
+ reason: "repo removed from agent by GitHub reconcile (acme/b)",
109
+ },
110
+ ]);
111
+ });
112
+
113
+ it("succeeds with no port and does not block", async () => {
114
+ const { svc } = build({
115
+ repos: ["acme/a", "acme/b"],
116
+ pushable: ["acme/a"],
117
+ });
118
+ const plan = await svc.plan("a1");
119
+ const res = await svc.apply({
120
+ agentId: "a1",
121
+ caller: "c",
122
+ planHash: plan.planHash,
123
+ });
124
+ expect(res.status).toBe("applied");
125
+ });
126
+
127
+ it("a port failure does not fail an applied reconcile", async () => {
128
+ const openWork: OpenWorkPort = {
129
+ listOpenWork: async () => [],
130
+ blockOpenWork: async () => {
131
+ throw new Error("store down");
132
+ },
133
+ };
134
+ const { svc } = build({
135
+ repos: ["acme/a", "acme/b"],
136
+ pushable: ["acme/a"],
137
+ openWork,
138
+ });
139
+ const plan = await svc.plan("a1");
140
+ const res = await svc.apply({
141
+ agentId: "a1",
142
+ caller: "c",
143
+ planHash: plan.planHash,
144
+ });
145
+ expect(res.status).toBe("applied");
146
+ expect(res.status === "applied" && res.warnings.join()).toContain(
147
+ "store down",
148
+ );
149
+ });
150
+
151
+ it("skipped plan (unconfigured) changes nothing and writes no audit", async () => {
152
+ const { svc, calls, audits } = build({
153
+ repos: ["acme/a"],
154
+ pushable: [],
155
+ preview: { status: "unconfigured" },
156
+ });
157
+ const res = await svc.apply({ agentId: "a1", caller: "c", planHash: "x" });
158
+ expect(res.status).toBe("skipped");
159
+ expect(calls).toEqual([]);
160
+ expect(audits).toEqual([]);
161
+ });
162
+ });
163
+
164
+ describe("createInstallationPort (unit)", () => {
165
+ it("scopes to the agent App installation and sends simple names", async () => {
166
+ const seen: string[] = [];
167
+ const port = createInstallationPort(
168
+ {
169
+ listOrgInstallations: async () => [
170
+ { id: 1, appId: 10 },
171
+ { id: 2, appId: 20 },
172
+ ],
173
+ listInstallationRepositories: async (_o, id) => {
174
+ seen.push(`list:${id}`);
175
+ return [{ fullName: "acme/a" }];
176
+ },
177
+ addRepositories: async (_o, id, names) => {
178
+ seen.push(`add:${id}:${names.join(",")}`);
179
+ },
180
+ removeRepositories: async (_o, id, names) => {
181
+ seen.push(`rm:${id}:${names.join(",")}`);
182
+ },
183
+ },
184
+ 20,
185
+ );
186
+ expect(await port.listRepos(["acme"])).toEqual({ acme: ["acme/a"] });
187
+ await port.addRepos("acme", ["acme/b"]);
188
+ await port.removeRepos("acme", ["acme/c", "acme/d"]);
189
+ expect(seen).toEqual(["list:2", "add:2:b", "rm:2:c,d"]);
190
+ });
191
+ });
@@ -0,0 +1,33 @@
1
+ /**
2
+ * admin/src/reconcile-open-work.ts
3
+ * Optional open-work port for the GitHub reconcile (GHE-3.3). The admin
4
+ * package knows nothing about task stores; a consumer that does (shipwright)
5
+ * may supply this port so work on repos removed from an agent gets blocked.
6
+ * Additive and optional: with no port, nothing is listed or blocked.
7
+ */
8
+
9
+ export interface OpenWorkItem {
10
+ /** Consumer-defined id (e.g. a task id). */
11
+ id: string;
12
+ /** owner/repo the item belongs to. */
13
+ repo: string;
14
+ title?: string;
15
+ }
16
+
17
+ export interface OpenWorkPort {
18
+ /** Open work the agent holds on any of `repos` (owner/repo). */
19
+ listOpenWork(
20
+ agentId: string,
21
+ repos: readonly string[],
22
+ ): Promise<OpenWorkItem[]>;
23
+ /** Block the agent's open work on `repos`; `reason` is shown to humans. */
24
+ blockOpenWork(
25
+ agentId: string,
26
+ repos: readonly string[],
27
+ reason: string,
28
+ ): Promise<void>;
29
+ }
30
+
31
+ export function removedRepoBlockReason(repo: string): string {
32
+ return `repo removed from agent by GitHub reconcile (${repo})`;
33
+ }