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 Git from "@distilled.cloud/github/git"; |
| 5 | import * as Issues from "@distilled.cloud/github/issues"; |
| 6 | import * as Pulls from "@distilled.cloud/github/pulls"; |
| 7 | import * as Repos from "@distilled.cloud/github/repos"; |
| 8 | import * as Users from "@distilled.cloud/github/users"; |
| 9 | import * as Output from "alchemy/Output"; |
| 10 | import { destroy } from "alchemy/RemovalPolicy"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import { gh } from "./scope.ts"; |
| 13 | |
| 14 | const owner = process.env.GITHUB_TEST_OWNER ?? "alchemy-run-test"; |
| 15 | if (owner !== "alchemy-run-test" && owner !== "alchemy-run-test-2") { |
| 16 | throw new Error(`Unsafe GITHUB_TEST_OWNER: ${owner}`); |
| 17 | } |
| 18 | |
| 19 | const { test } = Test.make({ |
| 20 | providers: GitHub.providers({ baseUrl: "github.com" }), |
| 21 | }); |
| 22 | |
| 23 | const repo = "alchemy-pr-1569-pull-request"; |
| 24 | |
| 25 | const repository = () => |
| 26 | GitHub.Repository("Repo", { |
| 27 | owner, |
| 28 | name: repo, |
| 29 | description: "Retained deterministic fixture for alchemy PR #1569", |
| 30 | visibility: "public", |
| 31 | autoInit: true, |
| 32 | }); |
| 33 | |
| 34 | const repoName = (repository: GitHub.Repository) => |
| 35 | Output.map(repository.fullName, (fullName) => fullName.split("/")[1]!); |
| 36 | |
| 37 | const branches = ["alchemy-pr-1569-a", "alchemy-pr-1569-b"]; |
| 38 | |
| 39 | const prepareBranches = Effect.gen(function* () { |
| 40 | const scope = { owner, repo }; |
| 41 | const repository = yield* gh(Repos.get(scope)); |
| 42 | const base = repository.default_branch; |
| 43 | const ref = yield* gh(Git.getRef({ ...scope, ref: `heads/${base}` })); |
| 44 | for (const branch of branches) { |
| 45 | const existing = yield* gh(Git.getRef({ ...scope, ref: `heads/${branch}` })).pipe( |
| 46 | Effect.catchTag("NotFound", () => Effect.succeed(undefined)), |
| 47 | ); |
| 48 | if (existing === undefined) { |
| 49 | yield* gh( |
| 50 | Git.createRef({ |
| 51 | ...scope, |
| 52 | ref: `refs/heads/${branch}`, |
| 53 | sha: ref.object.sha, |
| 54 | }), |
| 55 | ); |
| 56 | yield* gh( |
| 57 | Repos.createOrUpdateFileContents({ |
| 58 | ...scope, |
| 59 | branch, |
| 60 | path: "alchemy-pr-1569.txt", |
| 61 | message: "Add deterministic PR test fixture", |
| 62 | content: "YWxjaGVteSBQUiBmaXh0dXJlCg==", |
| 63 | }), |
| 64 | ); |
| 65 | } |
| 66 | } |
| 67 | return base; |
| 68 | }); |
| 69 | |
| 70 | const deleteBranches = Effect.gen(function* () { |
| 71 | for (const branch of branches) { |
| 72 | yield* gh(Git.deleteRef({ owner, repo, ref: `heads/${branch}` })); |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | test.provider( |
| 77 | "create, update, replace, and close a pull request", |
| 78 | (stack) => |
| 79 | Effect.gen(function* () { |
| 80 | yield* stack.destroy(); |
| 81 | yield* stack.deploy(repository()); |
| 82 | const base = yield* prepareBranches; |
| 83 | const user = yield* gh(Users.getAuthenticated({})); |
| 84 | const milestone = yield* gh( |
| 85 | Issues.createMilestone({ |
| 86 | owner, |
| 87 | repo, |
| 88 | title: "alchemy-pr-1569-lifecycle", |
| 89 | }), |
| 90 | ); |
| 91 | const deploy = (props: Partial<GitHub.PullRequestProps> = {}) => |
| 92 | stack.deploy( |
| 93 | Effect.gen(function* () { |
| 94 | const repo = yield* repository(); |
| 95 | return yield* GitHub.PullRequest("PR", { |
| 96 | owner, |
| 97 | repository: repoName(repo), |
| 98 | title: "Alchemy PR #1569 lifecycle", |
| 99 | head: "alchemy-pr-1569-a", |
| 100 | base, |
| 101 | ...props, |
| 102 | }).pipe(destroy()); |
| 103 | }), |
| 104 | ); |
| 105 | const get = (number: number) => gh(Pulls.get({ owner, repo, pull_number: number })); |
| 106 | const created = yield* deploy({ |
| 107 | body: "\n Initial body\n ", |
| 108 | draft: true, |
| 109 | labels: ["bug"], |
| 110 | assignees: [user.login], |
| 111 | milestone: milestone.number, |
| 112 | }); |
| 113 | expect(created.prNumber).toBeGreaterThan(0); |
| 114 | expect(created.htmlUrl).toBe(`https://github.com/${owner}/${repo}/pull/${created.prNumber}`); |
| 115 | expect(created.draft).toBe(true); |
| 116 | expect(created.merged).toBe(false); |
| 117 | const initial = yield* get(created.prNumber); |
| 118 | expect(initial.body).toBe("Initial body"); |
| 119 | expect(initial.assignees?.map((assignee) => assignee.login)).toEqual([user.login]); |
| 120 | expect(initial.milestone?.number).toBe(milestone.number); |
| 121 | expect(initial.labels.map((label) => label.name)).toEqual(["bug"]); |
| 122 | |
| 123 | const updated = yield* deploy({ |
| 124 | title: "Updated PR", |
| 125 | body: "", |
| 126 | draft: false, |
| 127 | labels: ["enhancement"], |
| 128 | assignees: [], |
| 129 | milestone: null, |
| 130 | reviewers: [], |
| 131 | teamReviewers: [], |
| 132 | }); |
| 133 | expect(updated.prNumber).toBe(created.prNumber); |
| 134 | expect(updated.nodeId).toBe(created.nodeId); |
| 135 | expect(updated.draft).toBe(false); |
| 136 | const afterUpdate = yield* get(created.prNumber); |
| 137 | expect(afterUpdate.title).toBe("Updated PR"); |
| 138 | expect(afterUpdate.body ?? "").toBe(""); |
| 139 | expect(afterUpdate.draft).toBe(false); |
| 140 | expect(afterUpdate.assignees).toEqual([]); |
| 141 | expect(afterUpdate.milestone).toBeNull(); |
| 142 | expect(afterUpdate.labels.map((label) => label.name)).toEqual(["enhancement"]); |
| 143 | expect(afterUpdate.requested_reviewers).toEqual([]); |
| 144 | expect(afterUpdate.requested_teams).toEqual([]); |
| 145 | |
| 146 | const draft = yield* deploy({ draft: true, labels: [] }); |
| 147 | expect(draft.draft).toBe(true); |
| 148 | expect((yield* get(draft.prNumber)).draft).toBe(true); |
| 149 | expect((yield* get(draft.prNumber)).labels).toEqual([]); |
| 150 | const closed = yield* deploy({ state: "closed", draft: true }); |
| 151 | expect(closed.state).toBe("closed"); |
| 152 | expect((yield* get(closed.prNumber)).state).toBe("closed"); |
| 153 | const reopened = yield* deploy(); |
| 154 | expect(reopened.prNumber).toBe(created.prNumber); |
| 155 | expect(reopened.state).toBe("open"); |
| 156 | expect(reopened.draft).toBe(false); |
| 157 | |
| 158 | const replaced = yield* deploy({ |
| 159 | head: "alchemy-pr-1569-b", |
| 160 | state: "closed", |
| 161 | }); |
| 162 | expect(replaced.prNumber).not.toBe(created.prNumber); |
| 163 | expect(replaced.nodeId).not.toBe(created.nodeId); |
| 164 | expect(replaced.state).toBe("closed"); |
| 165 | expect((yield* get(created.prNumber)).state).toBe("closed"); |
| 166 | expect((yield* get(replaced.prNumber)).state).toBe("closed"); |
| 167 | const final = yield* deploy({ head: "alchemy-pr-1569-b" }); |
| 168 | expect(final.prNumber).toBe(replaced.prNumber); |
| 169 | expect(final.state).toBe("open"); |
| 170 | |
| 171 | // Verify closure before releasing the retained repository fixture. |
| 172 | yield* stack.deploy(repository()); |
| 173 | expect((yield* get(final.prNumber)).state).toBe("closed"); |
| 174 | const open = yield* gh(Pulls.list({ owner, repo, state: "open" })); |
| 175 | expect(open).toEqual([]); |
| 176 | yield* deleteBranches; |
| 177 | yield* gh( |
| 178 | Issues.deleteMilestone({ |
| 179 | owner, |
| 180 | repo, |
| 181 | milestone_number: milestone.number, |
| 182 | }), |
| 183 | ); |
| 184 | yield* stack.destroy(); |
| 185 | }), |
| 186 | { |
| 187 | tags: ["provider:github", "provider:github:pullrequest", "provider:github:repository", "live"], |
| 188 | timeout: 120_000, |
| 189 | }, |
| 190 | ); |