The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GitHub from "@alchemy.run/github"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Orgs from "@distilled.cloud/github/orgs"; |
| 5 | import * as Repos from "@distilled.cloud/github/repos"; |
| 6 | import * as Output from "alchemy/Output"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import { destroy } from "alchemy/RemovalPolicy"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Redacted from "effect/Redacted"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import * as Stream from "effect/Stream"; |
| 14 | import { gh, mockGitHub, scopedToOwner } from "./scope.ts"; |
| 15 | |
| 16 | const requireTestOwner = (owner: string) => { |
| 17 | if (owner !== "alchemy-run-test" && owner !== "alchemy-run-test-2") { |
| 18 | throw new Error(`Refusing GitHub collaborator tests for owner ${owner}`); |
| 19 | } |
| 20 | return owner; |
| 21 | }; |
| 22 | |
| 23 | const owner = requireTestOwner(process.env.GITHUB_TEST_OWNER ?? "alchemy-run-test"); |
| 24 | const fixtureNames = [ |
| 25 | "alchemy-pr-1571-collaborator-lifecycle", |
| 26 | "alchemy-pr-1571-collaborator-replacement", |
| 27 | "alchemy-pr-1571-collaborator-list", |
| 28 | ]; |
| 29 | |
| 30 | const { test } = Test.make({ |
| 31 | providers: GitHub.providers({ baseUrl: "github.com" }), |
| 32 | }); |
| 33 | |
| 34 | const verifiedMember = Effect.gen(function* () { |
| 35 | const requested = process.env.GITHUB_TEST_COLLABORATOR_USERNAME; |
| 36 | if (!requested) { |
| 37 | const members = yield* gh( |
| 38 | Stream.runCollect( |
| 39 | Orgs.listMembers.items({ |
| 40 | org: owner, |
| 41 | role: "member", |
| 42 | per_page: 100, |
| 43 | }), |
| 44 | ), |
| 45 | ); |
| 46 | return yield* Effect.fail( |
| 47 | new Error( |
| 48 | `Set GITHUB_TEST_COLLABORATOR_USERNAME to an explicitly authorized active non-owner member of ${owner}; eligible members: ${members.map((member) => member.login).join(", ") || "none"}. No invitations or owner access changes are permitted.`, |
| 49 | ), |
| 50 | ); |
| 51 | } |
| 52 | const membership = yield* gh( |
| 53 | Orgs.getMembershipForUser({ |
| 54 | org: owner, |
| 55 | username: requested, |
| 56 | }), |
| 57 | ); |
| 58 | if (membership.state !== "active" || membership.role !== "member") { |
| 59 | return yield* Effect.fail( |
| 60 | new Error( |
| 61 | `Refusing collaborator ${requested}: ${owner} membership is ${membership.state}/${membership.role}; an authorized active non-owner member is required.`, |
| 62 | ), |
| 63 | ); |
| 64 | } |
| 65 | return requested; |
| 66 | }); |
| 67 | |
| 68 | const repository = (index: number) => |
| 69 | GitHub.Repository(`Repo${index}`, { |
| 70 | owner, |
| 71 | name: fixtureNames[index]!, |
| 72 | description: |
| 73 | "PR 1571 collaborator test fixture; retained because the test token lacks delete_repo", |
| 74 | visibility: "private", |
| 75 | autoInit: true, |
| 76 | }); |
| 77 | |
| 78 | const directCollaborators = (repo: string) => |
| 79 | Effect.gen(function* () { |
| 80 | return yield* gh( |
| 81 | Stream.runCollect( |
| 82 | Repos.listCollaborators.items({ |
| 83 | owner, |
| 84 | repo, |
| 85 | affiliation: "direct", |
| 86 | per_page: 100, |
| 87 | }), |
| 88 | ), |
| 89 | ); |
| 90 | }); |
| 91 | |
| 92 | const assertRemoved = (repo: string, username: string) => |
| 93 | Effect.gen(function* () { |
| 94 | const remaining = yield* directCollaborators(repo).pipe( |
| 95 | Effect.repeat({ |
| 96 | until: (collaborators) => |
| 97 | !collaborators.some((collaborator) => collaborator.login === username), |
| 98 | schedule: Schedule.spaced("1 second"), |
| 99 | times: 8, |
| 100 | }), |
| 101 | ); |
| 102 | expect(remaining.some((collaborator) => collaborator.login === username)).toBe(false); |
| 103 | const invitations = yield* gh( |
| 104 | Stream.runCollect( |
| 105 | Repos.listInvitations.items({ |
| 106 | owner, |
| 107 | repo, |
| 108 | per_page: 100, |
| 109 | }), |
| 110 | ), |
| 111 | ); |
| 112 | expect(invitations.some((invitation) => invitation.invitee?.login === username)).toBe(false); |
| 113 | }); |
| 114 | |
| 115 | // Permission changes require an explicitly authorized non-owner test member. |
| 116 | test.provider.skipIf(!process.env.GITHUB_TEST_COLLABORATOR_USERNAME)( |
| 117 | "add, update, replace, and remove an authorized collaborator", |
| 118 | (stack) => |
| 119 | Effect.gen(function* () { |
| 120 | // Verify authorization before even replaying persisted deletes. |
| 121 | const username = yield* verifiedMember; |
| 122 | yield* stack.destroy(); |
| 123 | const deploy = (index: number, permission?: GitHub.CollaboratorProps["permission"]) => |
| 124 | stack.deploy( |
| 125 | Effect.gen(function* () { |
| 126 | // Keep both dependencies present while replacing the collaborator. |
| 127 | const first = yield* repository(0); |
| 128 | const second = yield* repository(1); |
| 129 | return yield* GitHub.Collaborator("Collab", { |
| 130 | owner, |
| 131 | repository: Output.map( |
| 132 | (index === 0 ? first : second).fullName, |
| 133 | (fullName) => fullName.split("/")[1]!, |
| 134 | ), |
| 135 | username, |
| 136 | permission, |
| 137 | }).pipe(destroy()); |
| 138 | }), |
| 139 | ); |
| 140 | const created = yield* deploy(0); |
| 141 | expect(created.username).toBe(username); |
| 142 | expect(created.permission).toBe("push"); |
| 143 | expect( |
| 144 | (yield* directCollaborators(fixtureNames[0]!)).find((member) => member.login === username) |
| 145 | ?.permissions?.push, |
| 146 | ).toBe(true); |
| 147 | |
| 148 | const updated = yield* deploy(0, "admin"); |
| 149 | expect(updated.permission).toBe("admin"); |
| 150 | expect( |
| 151 | (yield* directCollaborators(fixtureNames[0]!)).find((member) => member.login === username) |
| 152 | ?.permissions?.admin, |
| 153 | ).toBe(true); |
| 154 | |
| 155 | const replaced = yield* deploy(1, "triage"); |
| 156 | expect(replaced.permission).toBe("triage"); |
| 157 | yield* assertRemoved(fixtureNames[0]!, username); |
| 158 | expect( |
| 159 | (yield* directCollaborators(fixtureNames[1]!)).some((member) => member.login === username), |
| 160 | ).toBe(true); |
| 161 | |
| 162 | // Remove access independently while both retained repositories still exist. |
| 163 | yield* stack.deploy( |
| 164 | Effect.gen(function* () { |
| 165 | yield* repository(0); |
| 166 | yield* repository(1); |
| 167 | }), |
| 168 | ); |
| 169 | yield* assertRemoved(fixtureNames[1]!, username); |
| 170 | yield* stack.destroy(); |
| 171 | }), |
| 172 | { |
| 173 | tags: ["provider:github", "provider:github:collaborator", "provider:github:repository", "live"], |
| 174 | timeout: 120_000, |
| 175 | }, |
| 176 | ); |
| 177 | |
| 178 | test.provider( |
| 179 | "list enumerates collaborators only in dedicated test repositories", |
| 180 | (stack) => |
| 181 | Effect.gen(function* () { |
| 182 | yield* stack.destroy(); |
| 183 | yield* stack.deploy(repository(2)); |
| 184 | const expected = yield* gh( |
| 185 | Repos.listCollaborators({ |
| 186 | owner, |
| 187 | repo: fixtureNames[2]!, |
| 188 | per_page: 100, |
| 189 | }), |
| 190 | ); |
| 191 | expect(expected.length).toBeGreaterThan(0); |
| 192 | const provider = yield* Provider.findProvider(GitHub.Collaborator); |
| 193 | // list() enumerates /user/repos; confine it to this suite's fixture. |
| 194 | const listed = yield* provider |
| 195 | .list() |
| 196 | .pipe(scopedToOwner(owner, { repository: fixtureNames[2]!, path: "collaborators" })); |
| 197 | for (const member of expected) { |
| 198 | expect(listed.some((collaborator) => collaborator.username === member.login)).toBe(true); |
| 199 | } |
| 200 | yield* stack.destroy(); |
| 201 | }), |
| 202 | { |
| 203 | tags: ["provider:github", "provider:github:collaborator", "provider:github:repository", "live"], |
| 204 | timeout: 120_000, |
| 205 | }, |
| 206 | ); |
| 207 | |
| 208 | const mockFetch = |
| 209 | (calls: string[], access: Map<string, string>) => |
| 210 | (url: string | URL | Request, options?: RequestInit) => |
| 211 | Effect.runPromise( |
| 212 | Effect.sync(() => { |
| 213 | const path = new URL(String(url)).pathname; |
| 214 | const method = options?.method ?? "GET"; |
| 215 | calls.push(`${method} ${path}`); |
| 216 | if ( |
| 217 | !/^\/repos\/alchemy-run-test\/alchemy-pr-1571-unit(?:-replacement)?\/collaborators\/test-member$/.test( |
| 218 | path, |
| 219 | ) |
| 220 | ) { |
| 221 | throw new Error(`Unexpected mock request ${method} ${path}`); |
| 222 | } |
| 223 | if (method === "PUT") { |
| 224 | access.set(path, JSON.parse(String(options?.body)).permission); |
| 225 | return new Response(null, { status: 204 }); |
| 226 | } |
| 227 | if (method === "DELETE") { |
| 228 | const existed = access.delete(path); |
| 229 | return existed |
| 230 | ? new Response(null, { status: 204 }) |
| 231 | : new Response(JSON.stringify({ message: "Not Found" }), { |
| 232 | status: 404, |
| 233 | headers: { "content-type": "application/json" }, |
| 234 | }); |
| 235 | } |
| 236 | throw new Error(`Unexpected mock method ${method}`); |
| 237 | }), |
| 238 | ); |
| 239 | |
| 240 | const unitTest = ( |
| 241 | name: string, |
| 242 | body: ( |
| 243 | stack: Test.ScratchStack, |
| 244 | calls: string[], |
| 245 | access: Map<string, string>, |
| 246 | ) => Effect.Effect<void, any, any>, |
| 247 | ) => { |
| 248 | const calls: string[] = []; |
| 249 | const access = new Map<string, string>(); |
| 250 | const { test } = Test.make({ |
| 251 | providers: mockGitHub(mockFetch(calls, access)).pipe( |
| 252 | Layer.provideMerge(GitHub.providers({ baseUrl: "github.com" })), |
| 253 | ), |
| 254 | }); |
| 255 | test.provider(name, (stack) => body(stack, calls, access), { |
| 256 | tags: ["provider:github", "provider:github:collaborator", "live"], |
| 257 | }); |
| 258 | }; |
| 259 | |
| 260 | unitTest( |
| 261 | "unit: default permission, updates, replacement, and idempotent deletion", |
| 262 | (stack, calls, access) => |
| 263 | Effect.gen(function* () { |
| 264 | yield* stack.destroy(); |
| 265 | const deploy = (repo: string, permission?: GitHub.CollaboratorProps["permission"]) => |
| 266 | stack.deploy( |
| 267 | GitHub.Collaborator("Collab", { |
| 268 | owner: "alchemy-run-test", |
| 269 | repository: repo, |
| 270 | username: "test-member", |
| 271 | permission, |
| 272 | }).pipe(destroy()), |
| 273 | ); |
| 274 | const created = yield* deploy("alchemy-pr-1571-unit"); |
| 275 | expect(created.permission).toBe("push"); |
| 276 | expect([...access.values()]).toEqual(["push"]); |
| 277 | const updated = yield* deploy("alchemy-pr-1571-unit", "admin"); |
| 278 | expect(updated.permission).toBe("admin"); |
| 279 | expect([...access.values()]).toEqual(["admin"]); |
| 280 | yield* deploy("alchemy-pr-1571-unit-replacement", "pull"); |
| 281 | expect([...access.keys()]).toEqual([ |
| 282 | "/repos/alchemy-run-test/alchemy-pr-1571-unit-replacement/collaborators/test-member", |
| 283 | ]); |
| 284 | expect([...access.values()]).toEqual(["pull"]); |
| 285 | // Simulate out-of-band removal; the provider's DELETE must tolerate 404. |
| 286 | access.clear(); |
| 287 | yield* stack.destroy(); |
| 288 | expect(calls.filter((call) => call.startsWith("DELETE "))).toHaveLength(2); |
| 289 | expect(access.size).toBe(0); |
| 290 | }), |
| 291 | ); |
| 292 | |
| 293 | unitTest("unit: collaborator defaults to retain on destroy", (stack, calls, access) => |
| 294 | Effect.gen(function* () { |
| 295 | yield* stack.destroy(); |
| 296 | yield* stack.deploy( |
| 297 | GitHub.Collaborator("Collab", { |
| 298 | owner: "alchemy-run-test", |
| 299 | repository: "alchemy-pr-1571-unit", |
| 300 | username: "test-member", |
| 301 | }), |
| 302 | ); |
| 303 | yield* stack.destroy(); |
| 304 | expect(access.size).toBe(1); |
| 305 | expect(calls.some((call) => call.startsWith("DELETE "))).toBe(false); |
| 306 | }), |
| 307 | ); |
| 308 | |
| 309 | test( |
| 310 | "unit: owner allowlist rejects production and unrelated owners", |
| 311 | Effect.sync(() => { |
| 312 | expect(requireTestOwner("alchemy-run-test")).toBe("alchemy-run-test"); |
| 313 | expect(requireTestOwner("alchemy-run-test-2")).toBe("alchemy-run-test-2"); |
| 314 | for (const unsafe of ["alchemy-run", "sam-goodwin", "", "other-org"]) { |
| 315 | expect(() => requireTestOwner(unsafe)).toThrow("Refusing GitHub collaborator tests"); |
| 316 | } |
| 317 | }), |
| 318 | { |
| 319 | tags: ["unit", "provider:github", "provider:github:collaborator", "local"], |
| 320 | }, |
| 321 | ); |