|
| 1 | +/** |
| 2 | + * Tests for the repo-baseline cycle. |
| 3 | + * |
| 4 | + * All tests use a mock AppClient — no network calls. |
| 5 | + * Coverage: |
| 6 | + * - buildDesired: keeps repoBaselines |
| 7 | + * - listOrgRepoNames: pagination → presence map |
| 8 | + * - diff: create only for missing repos; no entry when present; no delete |
| 9 | + * - apply: POST create (empty) / template generate / private default |
| 10 | + * - runner integration: dry-run plan |
| 11 | + */ |
| 12 | + |
| 13 | +import { describe, it, expect } from "vitest"; |
| 14 | +import { repoBaselineCycle, listOrgRepoNames } from "./repo-baseline.js"; |
| 15 | +import type { RepoBaselineScope } from "./repo-baseline.js"; |
| 16 | +import type { AppClient } from "../auth/app-client.js"; |
| 17 | +import type { RateBudget } from "../reconcile/runner.js"; |
| 18 | +import { runReconcile, BudgetExhaustedError } from "../reconcile/runner.js"; |
| 19 | +import { diff } from "../reconcile/diff.js"; |
| 20 | +import type { LiveOrgState } from "../reconcile/diff.js"; |
| 21 | +import type { GovernanceConfig, OrgConfig } from "../config/types.js"; |
| 22 | + |
| 23 | +// --------------------------------------------------------------------------- |
| 24 | +// Mock helpers |
| 25 | +// --------------------------------------------------------------------------- |
| 26 | + |
| 27 | +interface MockCall { |
| 28 | + method: string; |
| 29 | + path: string; |
| 30 | + body?: unknown; |
| 31 | +} |
| 32 | + |
| 33 | +interface MockClient extends AppClient { |
| 34 | + calls: MockCall[]; |
| 35 | + responses: Map<string, unknown>; |
| 36 | +} |
| 37 | + |
| 38 | +function makeMockClient(responses: Record<string, unknown> = {}): MockClient { |
| 39 | + const calls: MockCall[] = []; |
| 40 | + const responseMap = new Map(Object.entries(responses)); |
| 41 | + return { |
| 42 | + calls, |
| 43 | + responses: responseMap, |
| 44 | + async request<T = unknown>(method: string, path: string, body?: unknown): Promise<T> { |
| 45 | + calls.push({ method, path, body }); |
| 46 | + const key = `${method} ${path}`; |
| 47 | + if (responseMap.has(key)) return responseMap.get(key) as T; |
| 48 | + if (method === "GET" && path.includes("/repos?")) return [] as T; |
| 49 | + return {} as T; |
| 50 | + }, |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +function makeBudget(initial = 100): RateBudget { |
| 55 | + let remaining = initial; |
| 56 | + return { |
| 57 | + get remaining() { |
| 58 | + return remaining; |
| 59 | + }, |
| 60 | + get exhausted() { |
| 61 | + return remaining <= 0; |
| 62 | + }, |
| 63 | + use(n = 1) { |
| 64 | + if (remaining <= 0) throw new BudgetExhaustedError(); |
| 65 | + remaining = Math.max(0, remaining - n); |
| 66 | + }, |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +const scope: RepoBaselineScope = {}; |
| 71 | + |
| 72 | +// --------------------------------------------------------------------------- |
| 73 | +// 1. buildDesired |
| 74 | +// --------------------------------------------------------------------------- |
| 75 | + |
| 76 | +describe("repoBaselineCycle.buildDesired", () => { |
| 77 | + it("returns empty when repoBaselines absent", () => { |
| 78 | + expect(repoBaselineCycle.buildDesired({ repos: {} }, "test-org", scope).repoBaselines).toBeUndefined(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("keeps only repoBaselines", () => { |
| 82 | + const orgConfig: OrgConfig = { repoBaselines: [{ name: "svc" }], members: [] }; |
| 83 | + expect(repoBaselineCycle.buildDesired(orgConfig, "test-org", scope)).toEqual({ |
| 84 | + repoBaselines: [{ name: "svc" }], |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +// --------------------------------------------------------------------------- |
| 90 | +// 2. listOrgRepoNames |
| 91 | +// --------------------------------------------------------------------------- |
| 92 | + |
| 93 | +describe("listOrgRepoNames", () => { |
| 94 | + it("paginates and returns a presence map", async () => { |
| 95 | + const full = Array.from({ length: 100 }, (_, i) => ({ name: `r${i}` })); |
| 96 | + const client = makeMockClient({ |
| 97 | + "GET /orgs/test-org/repos?per_page=100&page=1": full, |
| 98 | + "GET /orgs/test-org/repos?per_page=100&page=2": [{ name: "last" }], |
| 99 | + }); |
| 100 | + const repos = await listOrgRepoNames(client, "test-org", makeBudget()); |
| 101 | + expect(Object.keys(repos)).toHaveLength(101); |
| 102 | + expect(repos).toHaveProperty("last"); |
| 103 | + }); |
| 104 | +}); |
| 105 | + |
| 106 | +// --------------------------------------------------------------------------- |
| 107 | +// 3. diff |
| 108 | +// --------------------------------------------------------------------------- |
| 109 | + |
| 110 | +describe("diff integration with repo-baseline cycle", () => { |
| 111 | + const desiredConfig: OrgConfig = { repoBaselines: [{ name: "svc" }, { name: "new-repo" }] }; |
| 112 | + |
| 113 | + it("emits create only for the missing repo", () => { |
| 114 | + const live: LiveOrgState = { repos: { svc: {} } }; |
| 115 | + const desired = repoBaselineCycle.buildDesired(desiredConfig, "test-org", scope); |
| 116 | + const cs = diff("test-org", desired, live); |
| 117 | + expect(cs.entries).toHaveLength(1); |
| 118 | + expect(cs.entries[0]!.resourceType).toBe("repo-baseline"); |
| 119 | + expect(cs.entries[0]!.kind).toBe("create"); |
| 120 | + expect(cs.entries[0]!.key).toBe("new-repo"); |
| 121 | + }); |
| 122 | + |
| 123 | + it("emits nothing when all declared repos exist", () => { |
| 124 | + const live: LiveOrgState = { repos: { svc: {}, "new-repo": {} } }; |
| 125 | + const desired = repoBaselineCycle.buildDesired(desiredConfig, "test-org", scope); |
| 126 | + expect(diff("test-org", desired, live).entries).toHaveLength(0); |
| 127 | + }); |
| 128 | +}); |
| 129 | + |
| 130 | +// --------------------------------------------------------------------------- |
| 131 | +// 4. apply |
| 132 | +// --------------------------------------------------------------------------- |
| 133 | + |
| 134 | +describe("repoBaselineCycle.apply", () => { |
| 135 | + it("POSTs a new empty private repo by default", async () => { |
| 136 | + const client = makeMockClient(); |
| 137 | + await repoBaselineCycle.apply( |
| 138 | + client, |
| 139 | + { kind: "create", resourceType: "repo-baseline", key: "svc", after: { name: "svc" } }, |
| 140 | + "my-org", |
| 141 | + scope, |
| 142 | + makeBudget(), |
| 143 | + ); |
| 144 | + expect(client.calls[0]!.method).toBe("POST"); |
| 145 | + expect(client.calls[0]!.path).toBe("/orgs/my-org/repos"); |
| 146 | + expect(client.calls[0]!.body).toEqual({ name: "svc", private: true }); |
| 147 | + }); |
| 148 | + |
| 149 | + it("honours an explicit private:false", async () => { |
| 150 | + const client = makeMockClient(); |
| 151 | + await repoBaselineCycle.apply( |
| 152 | + client, |
| 153 | + { kind: "create", resourceType: "repo-baseline", key: "svc", after: { name: "svc", private: false } }, |
| 154 | + "my-org", |
| 155 | + scope, |
| 156 | + makeBudget(), |
| 157 | + ); |
| 158 | + expect(client.calls[0]!.body).toEqual({ name: "svc", private: false }); |
| 159 | + }); |
| 160 | + |
| 161 | + it("generates from a template when declared", async () => { |
| 162 | + const client = makeMockClient(); |
| 163 | + await repoBaselineCycle.apply( |
| 164 | + client, |
| 165 | + { kind: "create", resourceType: "repo-baseline", key: "svc", after: { name: "svc", template: "my-org/tmpl" } }, |
| 166 | + "my-org", |
| 167 | + scope, |
| 168 | + makeBudget(), |
| 169 | + ); |
| 170 | + expect(client.calls[0]!.path).toBe("/repos/my-org/tmpl/generate"); |
| 171 | + expect(client.calls[0]!.body).toEqual({ owner: "my-org", name: "svc", private: true }); |
| 172 | + }); |
| 173 | + |
| 174 | + it("throws on a malformed template", async () => { |
| 175 | + const client = makeMockClient(); |
| 176 | + await expect( |
| 177 | + repoBaselineCycle.apply( |
| 178 | + client, |
| 179 | + { kind: "create", resourceType: "repo-baseline", key: "svc", after: { name: "svc", template: "no-slash" } }, |
| 180 | + "my-org", |
| 181 | + scope, |
| 182 | + makeBudget(), |
| 183 | + ), |
| 184 | + ).rejects.toThrow("malformed template"); |
| 185 | + }); |
| 186 | + |
| 187 | + it("ignores foreign resource types", async () => { |
| 188 | + const client = makeMockClient(); |
| 189 | + await repoBaselineCycle.apply( |
| 190 | + client, |
| 191 | + { kind: "create", resourceType: "repo", key: "svc", after: {} }, |
| 192 | + "my-org", |
| 193 | + scope, |
| 194 | + makeBudget(), |
| 195 | + ); |
| 196 | + expect(client.calls).toHaveLength(0); |
| 197 | + }); |
| 198 | +}); |
| 199 | + |
| 200 | +// --------------------------------------------------------------------------- |
| 201 | +// 5. Runner integration |
| 202 | +// --------------------------------------------------------------------------- |
| 203 | + |
| 204 | +describe("repoBaselineCycle via runReconcile", () => { |
| 205 | + it("dry-run: reports a create for the missing repo", async () => { |
| 206 | + const client = makeMockClient({ |
| 207 | + "GET /orgs/test-org/repos?per_page=100&page=1": [{ name: "existing" }], |
| 208 | + }); |
| 209 | + const config: GovernanceConfig = { |
| 210 | + orgs: { "test-org": { repoBaselines: [{ name: "existing" }, { name: "fresh" }] } }, |
| 211 | + }; |
| 212 | + const result = await runReconcile({ config, client, cycles: [repoBaselineCycle], mode: "dry-run" }); |
| 213 | + expect(result.completed).toBe(true); |
| 214 | + expect(result.cycles[0]!.counts.create).toBe(1); |
| 215 | + expect(client.calls.every((c) => c.method === "GET")).toBe(true); |
| 216 | + }); |
| 217 | +}); |
0 commit comments