The best way to control Claude Code, Codex and more from any computer or phone

main
ts 344 lines 12.7 KB
Raw
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 Repos from "@distilled.cloud/github/repos";
5 import { adopt } from "alchemy/AdoptPolicy";
6 import * as Provider from "alchemy/Provider";
7 import { destroy } from "alchemy/RemovalPolicy";
8 import * as Effect from "effect/Effect";
9 import { MinimumLogLevel } from "effect/References";
10 import { gh } from "./scope.ts";
11
12 const { test } = Test.make({ providers: GitHub.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // These tests create, mutate, and delete real repositories, so they run
17 // against the dedicated test orgs (never a real one). Set GITHUB_TEST_OWNER=""
18 // to skip the owner-scoped tests entirely. The second org hosts the target
19 // side of owner-change (replacement) tests.
20 const owner = process.env.GITHUB_TEST_OWNER ?? "alchemy-run-test";
21 const owner2 = process.env.GITHUB_TEST_OWNER_2 ?? "alchemy-run-test-2";
22
23 // Deleting a repository requires the `delete_repo` scope (fine-grained:
24 // "Administration: write"). The default `gh` CLI token only carries
25 // `gist, read:org, repo, workflow`, so every repo delete 403s with
26 // "Must have admin rights to Repository." — the create/delete lifecycle
27 // tests are gated behind an explicit opt-in. To run them:
28 // gh auth refresh -h github.com -s delete_repo
29 // GITHUB_TEST_DELETE_REPO=1 bun run test test/GitHub/Repository.test.ts --profile testing
30 const canDeleteRepos = !!process.env.GITHUB_TEST_DELETE_REPO;
31
32 const getRepo = (repo: string, repoOwner: string = owner) =>
33 Effect.gen(function* () {
34 return yield* gh(
35 Repos.get({
36 owner: repoOwner,
37 repo,
38 }),
39 ).pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
40 });
41
42 test.provider.skipIf(!owner || !canDeleteRepos)(
43 "create, update, rename, and delete a repository",
44 (stack) =>
45 Effect.gen(function* () {
46 const name = "alchemy-effect-repo-test";
47 const renamed = "alchemy-effect-repo-test-renamed";
48
49 // Clean up any leftovers from a previous run before deploying.
50 yield* stack.destroy();
51
52 // Create
53 const created = yield* stack.deploy(
54 Effect.gen(function* () {
55 return yield* GitHub.Repository("Repo", {
56 owner,
57 name,
58 description: "alchemy-effect integration test",
59 visibility: "private",
60 autoInit: true,
61 // `hasIssues`, not `hasWiki`: wikis on PRIVATE repos are
62 // plan-gated for orgs — GitHub accepts `has_wiki: true` on the
63 // PATCH but silently keeps it false on a free-plan org, so a
64 // wiki toggle can never be asserted here.
65 hasIssues: false,
66 topics: ["alchemy", "test"],
67 }).pipe(destroy());
68 }),
69 );
70
71 expect(created.repoId).toBeGreaterThan(0);
72 expect(created.fullName).toEqual(`${owner}/${name}`);
73 expect(created.defaultBranch).toBeDefined();
74
75 const fetched = yield* getRepo(name);
76 expect(fetched?.id).toEqual(created.repoId);
77 expect(fetched?.description).toEqual("alchemy-effect integration test");
78 expect(fetched?.private).toEqual(true);
79 expect(fetched?.has_issues).toEqual(false);
80 expect(fetched?.topics).toEqual(expect.arrayContaining(["alchemy", "test"]));
81
82 // Update — change settings and topics, same logical ID → same repoId.
83 const updated = yield* stack.deploy(
84 Effect.gen(function* () {
85 return yield* GitHub.Repository("Repo", {
86 owner,
87 name,
88 description: "updated description",
89 visibility: "private",
90 hasIssues: true,
91 topics: ["alchemy"],
92 }).pipe(destroy());
93 }),
94 );
95
96 expect(updated.repoId).toEqual(created.repoId);
97 const afterUpdate = yield* getRepo(name);
98 expect(afterUpdate?.description).toEqual("updated description");
99 expect(afterUpdate?.has_issues).toEqual(true);
100 expect(afterUpdate?.topics).toEqual(["alchemy"]);
101
102 // Rename — new `name`, same logical ID → in-place rename, same repoId.
103 const wasRenamed = yield* stack.deploy(
104 Effect.gen(function* () {
105 return yield* GitHub.Repository("Repo", {
106 owner,
107 name: renamed,
108 description: "updated description",
109 visibility: "private",
110 }).pipe(destroy());
111 }),
112 );
113
114 expect(wasRenamed.repoId).toEqual(created.repoId);
115 expect(wasRenamed.fullName).toEqual(`${owner}/${renamed}`);
116 const afterRename = yield* getRepo(renamed);
117 expect(afterRename?.id).toEqual(created.repoId);
118 // GitHub keeps a permanent redirect from a renamed repository's old
119 // name, and fetch follows the 301 — so fetching the old name returns
120 // the SAME repository under its new name rather than a 404.
121 const oldName = yield* getRepo(name);
122 expect(oldName?.id).toEqual(created.repoId);
123 expect(oldName?.full_name).toEqual(`${owner}/${renamed}`);
124
125 // Delete
126 yield* stack.destroy();
127 const afterDestroy = yield* getRepo(renamed);
128 expect(afterDestroy).toBeUndefined();
129 }).pipe(logLevel),
130 {
131 tags: ["provider:github", "provider:github:repository", "live"],
132 timeout: 120_000,
133 },
134 );
135
136 test.provider.skipIf(!owner || !canDeleteRepos)(
137 "list enumerates the deployed repository",
138 (stack) =>
139 Effect.gen(function* () {
140 const name = "alchemy-effect-repo-list-test";
141
142 // Clean up any leftovers from a previous run before deploying.
143 yield* stack.destroy();
144
145 const deployed = yield* stack.deploy(
146 Effect.gen(function* () {
147 return yield* GitHub.Repository("ListRepo", {
148 owner,
149 name,
150 description: "alchemy-effect list integration test",
151 visibility: "private",
152 autoInit: true,
153 }).pipe(destroy());
154 }),
155 );
156
157 expect(deployed.repoId).toBeGreaterThan(0);
158
159 const provider = yield* Provider.findProvider(GitHub.Repository);
160 const all = yield* provider.list();
161
162 // The deployed repository must appear in the exhaustively-paginated result.
163 expect(all.some((r) => r.repoId === deployed.repoId)).toBe(true);
164 const found = all.find((r) => r.repoId === deployed.repoId);
165 expect(found?.fullName).toEqual(`${owner}/${name}`);
166
167 yield* stack.destroy();
168 const afterDestroy = yield* getRepo(name);
169 expect(afterDestroy).toBeUndefined();
170 }).pipe(logLevel),
171 {
172 tags: ["provider:github", "provider:github:repository", "live"],
173 timeout: 120_000,
174 },
175 );
176
177 // Owner changes are replacements, not moves (we never call GitHub's transfer
178 // API). With deletion opted in via `destroy()`, the replaced old-generation
179 // repo is deleted from the old org after the new one is created.
180 test.provider.skipIf(!owner || !owner2 || !canDeleteRepos)(
181 "changing the owner replaces the repository (destroy-opted)",
182 (stack) =>
183 Effect.gen(function* () {
184 const name = "alchemy-effect-repo-owner-test";
185
186 // Clean up any leftovers from a previous run. Repos a crashed run left
187 // behind converge back under management when the deploy below re-takes
188 // the same deterministic name (reconcile's create-race path).
189 yield* stack.destroy();
190
191 const created = yield* stack.deploy(
192 Effect.gen(function* () {
193 return yield* GitHub.Repository("Repo", {
194 owner,
195 name,
196 description: "alchemy-effect owner-change test",
197 visibility: "private",
198 autoInit: true,
199 }).pipe(destroy());
200 }),
201 );
202 expect(created.fullName).toEqual(`${owner}/${name}`);
203
204 // Same logical ID, new owner → replacement: fresh repoId under the new
205 // org, and (because deletion is opted in) the old repo is cleaned up.
206 const replaced = yield* stack.deploy(
207 Effect.gen(function* () {
208 return yield* GitHub.Repository("Repo", {
209 owner: owner2,
210 name,
211 description: "alchemy-effect owner-change test",
212 visibility: "private",
213 autoInit: true,
214 }).pipe(destroy());
215 }),
216 );
217 expect(replaced.fullName).toEqual(`${owner2}/${name}`);
218 expect(replaced.repoId).not.toEqual(created.repoId);
219
220 const oldRepo = yield* getRepo(name, owner);
221 expect(oldRepo).toBeUndefined();
222 const newRepo = yield* getRepo(name, owner2);
223 expect(newRepo?.id).toEqual(replaced.repoId);
224
225 yield* stack.destroy();
226 const afterDestroy = yield* getRepo(name, owner2);
227 expect(afterDestroy).toBeUndefined();
228 }).pipe(logLevel),
229 {
230 tags: ["provider:github", "provider:github:repository", "live"],
231 timeout: 120_000,
232 },
233 );
234
235 // The safety property: under the DEFAULT `retain` removal policy, an owner
236 // change creates the new repo but RETAINS the old one on GitHub — history is
237 // never destroyed by a replacement unless deletion was explicitly opted in.
238 test.provider.skipIf(!owner || !owner2 || !canDeleteRepos)(
239 "changing the owner retains the old repository by default",
240 (stack) =>
241 Effect.gen(function* () {
242 const name = "alchemy-effect-repo-retain-test";
243
244 // Clean up any leftovers from a previous run. Retained repos a prior
245 // run left behind converge back under management when the deploys
246 // below re-take the same deterministic names.
247 yield* stack.destroy();
248
249 // No `destroy()` pipe — the default `retain` policy applies.
250 const created = yield* stack.deploy(
251 Effect.gen(function* () {
252 return yield* GitHub.Repository("Repo", {
253 owner,
254 name,
255 description: "alchemy-effect retain-on-replace test",
256 visibility: "private",
257 autoInit: true,
258 });
259 }),
260 );
261
262 const replaced = yield* stack.deploy(
263 Effect.gen(function* () {
264 return yield* GitHub.Repository("Repo", {
265 owner: owner2,
266 name,
267 description: "alchemy-effect retain-on-replace test",
268 visibility: "private",
269 autoInit: true,
270 });
271 }),
272 );
273 expect(replaced.repoId).not.toEqual(created.repoId);
274 expect(replaced.fullName).toEqual(`${owner2}/${name}`);
275
276 // The replaced old generation must still exist in the old org.
277 const oldRepo = yield* getRepo(name, owner);
278 expect(oldRepo?.id).toEqual(created.repoId);
279
280 // Cleanup — all through the engine: adopt the retained repo back into
281 // state under a second logical ID and flip both resources to the
282 // `destroy` removal policy, so the final stack.destroy() deletes both.
283 const adopted = yield* stack.deploy(
284 Effect.gen(function* () {
285 const current = yield* GitHub.Repository("Repo", {
286 owner: owner2,
287 name,
288 description: "alchemy-effect retain-on-replace test",
289 visibility: "private",
290 }).pipe(destroy());
291
292 const old = yield* GitHub.Repository("OldRepo", {
293 owner,
294 name,
295 description: "alchemy-effect retain-on-replace test",
296 visibility: "private",
297 }).pipe(adopt(), destroy());
298
299 return { current, old };
300 }),
301 );
302
303 // The adopted resource converged onto the retained repo, not a new one.
304 expect(adopted.old.repoId).toEqual(created.repoId);
305 expect(adopted.current.repoId).toEqual(replaced.repoId);
306
307 yield* stack.destroy();
308 expect(yield* getRepo(name, owner)).toBeUndefined();
309 expect(yield* getRepo(name, owner2)).toBeUndefined();
310 }).pipe(logLevel),
311 {
312 tags: ["provider:github", "provider:github:repository", "live"],
313 timeout: 120_000,
314 },
315 );
316
317 // Read-only enumeration: `list()` needs no owner — it walks every repository the
318 // authenticated token can see. Gated on a non-interactive token so CI never
319 // stalls on the auth-method prompt; exercises the live, exhaustively-paginated
320 // list path without creating a repo.
321 const hasToken = !!(process.env.GITHUB_TOKEN ?? process.env.GH_TOKEN);
322 test.provider.skipIf(!hasToken)(
323 "list returns the authenticated user's repositories",
324 (stack) =>
325 Effect.gen(function* () {
326 yield* stack.destroy();
327
328 const provider = yield* Provider.findProvider(GitHub.Repository);
329 const all = yield* provider.list();
330
331 expect(Array.isArray(all)).toBe(true);
332 for (const repo of all) {
333 expect(typeof repo.repoId).toBe("number");
334 expect(typeof repo.fullName).toBe("string");
335 expect(repo.fullName).toContain("/");
336 }
337
338 yield* stack.destroy();
339 }).pipe(logLevel),
340 {
341 tags: ["provider:github", "provider:github:repository", "live"],
342 timeout: 120_000,
343 },
344 );