The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 Issues from "@distilled.cloud/github/issues"; |
| 5 | import * as Output from "alchemy/Output"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import { destroy } from "alchemy/RemovalPolicy"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { gh, scopedToOwner } from "./scope.ts"; |
| 11 | |
| 12 | const owner = process.env.GITHUB_TEST_OWNER ?? "alchemy-run-test"; |
| 13 | if (owner !== "alchemy-run-test" && owner !== "alchemy-run-test-2") { |
| 14 | throw new Error(`Unsafe GITHUB_TEST_OWNER: ${owner}`); |
| 15 | } |
| 16 | |
| 17 | const { test } = Test.make({ |
| 18 | providers: GitHub.providers({ baseUrl: "github.com" }), |
| 19 | }); |
| 20 | |
| 21 | const repository = (id: string, name: string) => |
| 22 | GitHub.Repository(id, { |
| 23 | owner, |
| 24 | name, |
| 25 | description: "Alchemy PR 1568 Issue integration fixture (retained)", |
| 26 | visibility: "private", |
| 27 | hasIssues: true, |
| 28 | autoInit: true, |
| 29 | }); |
| 30 | |
| 31 | const repoName = (repo: GitHub.Repository) => |
| 32 | Output.map(repo.fullName, (fullName) => fullName.split("/")[1]!); |
| 33 | |
| 34 | const getIssue = (repo: string, issueNumber: number) => |
| 35 | Effect.gen(function* () { |
| 36 | const data = yield* gh(Issues.get({ owner, repo, issue_number: issueNumber })); |
| 37 | return data; |
| 38 | }); |
| 39 | |
| 40 | test.provider( |
| 41 | "create closed, update, list, and close an issue on destroy", |
| 42 | (stack) => |
| 43 | Effect.gen(function* () { |
| 44 | yield* stack.destroy(); |
| 45 | const name = "alchemy-pr-1568-issue-lifecycle"; |
| 46 | const deployIssue = (props: Omit<GitHub.IssueProps, "owner" | "repository">) => |
| 47 | stack.deploy( |
| 48 | Effect.gen(function* () { |
| 49 | // Repository deletion needs delete_repo; reuse this retained fixture. |
| 50 | const repo = yield* repository("Repo", name); |
| 51 | return yield* GitHub.Issue("Issue", { |
| 52 | owner, |
| 53 | repository: repoName(repo), |
| 54 | ...props, |
| 55 | }).pipe(destroy()); |
| 56 | }), |
| 57 | ); |
| 58 | |
| 59 | const created = yield* deployIssue({ |
| 60 | title: "PR 1568: initially closed", |
| 61 | body: "\n Initial body\n Second line\n", |
| 62 | labels: ["bug"], |
| 63 | state: "closed", |
| 64 | }); |
| 65 | expect(created.issueNumber).toBeGreaterThan(0); |
| 66 | expect(created.nodeId).toBeTruthy(); |
| 67 | expect(created.htmlUrl).toBe( |
| 68 | `https://github.com/${owner}/${name}/issues/${created.issueNumber}`, |
| 69 | ); |
| 70 | expect(created.state).toBe("closed"); |
| 71 | const initial = yield* getIssue(name, created.issueNumber); |
| 72 | expect(initial.state).toBe("closed"); |
| 73 | expect(initial.body).toBe("Initial body\nSecond line"); |
| 74 | expect( |
| 75 | initial.labels.map((label) => (typeof label === "string" ? label : label.name)), |
| 76 | ).toEqual(["bug"]); |
| 77 | |
| 78 | const updated = yield* deployIssue({ title: "PR 1568: reopened" }); |
| 79 | expect(updated.issueNumber).toBe(created.issueNumber); |
| 80 | expect(updated.nodeId).toBe(created.nodeId); |
| 81 | expect(updated.state).toBe("open"); |
| 82 | const fetched = yield* getIssue(name, updated.issueNumber); |
| 83 | expect(fetched.title).toBe("PR 1568: reopened"); |
| 84 | expect(fetched.body ?? "").toBe(""); |
| 85 | expect(fetched.labels).toEqual([]); |
| 86 | expect(fetched.assignees).toEqual([]); |
| 87 | expect(fetched.milestone).toBeNull(); |
| 88 | const provider = yield* Provider.findProvider(GitHub.Issue); |
| 89 | const listed = yield* provider.list().pipe( |
| 90 | scopedToOwner(owner, { repository: name, path: "issues" }), |
| 91 | Effect.repeat({ |
| 92 | schedule: Schedule.spaced("2 seconds"), |
| 93 | times: 10, |
| 94 | until: (issues) => issues.some((issue) => issue.nodeId === created.nodeId), |
| 95 | }), |
| 96 | ); |
| 97 | expect(listed.map((issue) => issue.nodeId)).toContain(created.nodeId); |
| 98 | |
| 99 | yield* stack.destroy(); |
| 100 | expect((yield* getIssue(name, created.issueNumber)).state).toBe("closed"); |
| 101 | yield* stack.destroy(); |
| 102 | }), |
| 103 | { |
| 104 | tags: ["provider:github", "provider:github:issue", "provider:github:repository", "live"], |
| 105 | timeout: 120_000, |
| 106 | }, |
| 107 | ); |
| 108 | |
| 109 | test.provider( |
| 110 | "repository changes replace the issue and close the old generation", |
| 111 | (stack) => |
| 112 | Effect.gen(function* () { |
| 113 | yield* stack.destroy(); |
| 114 | const firstName = "alchemy-pr-1568-issue-replace-a"; |
| 115 | const secondName = "alchemy-pr-1568-issue-replace-b"; |
| 116 | const deployIssue = (target: "first" | "second") => |
| 117 | stack.deploy( |
| 118 | Effect.gen(function* () { |
| 119 | // Keep both dependencies across replacement; retain the fixture repos. |
| 120 | const first = yield* repository("FirstRepo", firstName); |
| 121 | const second = yield* repository("SecondRepo", secondName); |
| 122 | return yield* GitHub.Issue("Issue", { |
| 123 | owner, |
| 124 | repository: repoName(target === "first" ? first : second), |
| 125 | title: "PR 1568: repository replacement", |
| 126 | }).pipe(destroy()); |
| 127 | }), |
| 128 | ); |
| 129 | |
| 130 | const created = yield* deployIssue("first"); |
| 131 | expect((yield* getIssue(firstName, created.issueNumber)).state).toBe("open"); |
| 132 | const replaced = yield* deployIssue("second"); |
| 133 | expect(replaced.nodeId).not.toBe(created.nodeId); |
| 134 | expect(replaced.htmlUrl).toContain(`/${secondName}/issues/`); |
| 135 | expect((yield* getIssue(firstName, created.issueNumber)).state).toBe("closed"); |
| 136 | expect((yield* getIssue(secondName, replaced.issueNumber)).state).toBe("open"); |
| 137 | |
| 138 | yield* stack.destroy(); |
| 139 | expect((yield* getIssue(secondName, replaced.issueNumber)).state).toBe("closed"); |
| 140 | yield* stack.destroy(); |
| 141 | }), |
| 142 | { |
| 143 | tags: ["provider:github", "provider:github:issue", "provider:github:repository", "live"], |
| 144 | timeout: 120_000, |
| 145 | }, |
| 146 | ); |