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

main
ts 236 lines 8.33 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 Actions from "@distilled.cloud/github/actions";
5 import * as Repos from "@distilled.cloud/github/repos";
6 import * as Output from "alchemy/Output";
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 a real deployment environment, so
17 // they run against the dedicated test org (never a real one). Set
18 // GITHUB_TEST_OWNER="" to skip. The host repository is public because
19 // environments (and their protection rules) on private repositories are
20 // plan-gated.
21 const owner = process.env.GITHUB_TEST_OWNER ?? "alchemy-run-test";
22 const repo = process.env.GITHUB_TEST_ENVIRONMENT_REPOSITORY ?? "alchemy-effect-environment-test";
23
24 // Derive the repository name from the `fullName` output — referencing an
25 // output (rather than the `repo` constant) makes the engine order dependent
26 // resources after the repository exists.
27 const repoName = (repository: GitHub.Repository) =>
28 Output.map(repository.fullName, (fullName) => fullName.split("/")[1]!);
29
30 const getEnvironment = (name: string) =>
31 Effect.gen(function* () {
32 return yield* gh(
33 Repos.getEnvironment({
34 owner,
35 repo,
36 environment_name: name,
37 }),
38 ).pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
39 });
40
41 const listBranchPolicies = (name: string) =>
42 Effect.gen(function* () {
43 return yield* gh(
44 Repos.listDeploymentBranchPolicies({
45 owner,
46 repo,
47 environment_name: name,
48 per_page: 100,
49 }),
50 ).pipe(Effect.map((data) => (data.branch_policies ?? []).map((policy) => policy.name)));
51 });
52
53 test.provider.skipIf(!owner)(
54 "create, update, and delete an environment with protection rules",
55 (stack) =>
56 Effect.gen(function* () {
57 const name = "alchemy-test-production";
58
59 // Clean up any leftovers from a previous run before deploying.
60 yield* stack.destroy();
61
62 // Create — environment with a wait timer and custom branch patterns.
63 // `Repository` defaults to `retain`, so the host repo is created once
64 // and reused across runs (reconcile is idempotent).
65 const created = yield* stack.deploy(
66 Effect.gen(function* () {
67 const repository = yield* GitHub.Repository("Repo", {
68 owner,
69 name: repo,
70 description: "alchemy-effect environment test",
71 visibility: "public",
72 autoInit: true,
73 });
74
75 return yield* GitHub.Environment("Env", {
76 owner,
77 // Derive the repo name from a repository output so the engine
78 // orders the environment after the repository exists.
79 repository: repoName(repository),
80 name,
81 waitTimer: 5,
82 preventSelfReview: true,
83 deploymentBranchPolicy: {
84 customBranchPolicies: ["main", "release/*"],
85 },
86 }).pipe(destroy());
87 }),
88 );
89
90 expect(created.environmentId).toBeGreaterThan(0);
91 expect(created.name).toEqual(name);
92 expect(created.htmlUrl).toContain(repo);
93
94 const fetched = yield* getEnvironment(name);
95 expect(fetched?.id).toEqual(created.environmentId);
96 expect(fetched?.deployment_branch_policy?.custom_branch_policies).toBe(true);
97 const waitRule = fetched?.protection_rules?.find((rule) => rule.type === "wait_timer");
98 expect(
99 waitRule !== undefined && "wait_timer" in waitRule ? waitRule.wait_timer : undefined,
100 ).toEqual(5);
101
102 const patterns = yield* listBranchPolicies(name);
103 expect(patterns.sort()).toEqual(["main", "release/*"]);
104
105 // Update — drop the wait timer, converge the pattern list, same
106 // logical ID → same environmentId (update in place, not replace).
107 const updated = yield* stack.deploy(
108 Effect.gen(function* () {
109 const repository = yield* GitHub.Repository("Repo", {
110 owner,
111 name: repo,
112 description: "alchemy-effect environment test",
113 visibility: "public",
114 });
115
116 return yield* GitHub.Environment("Env", {
117 owner,
118 repository: repoName(repository),
119 name,
120 deploymentBranchPolicy: {
121 customBranchPolicies: ["main"],
122 },
123 }).pipe(destroy());
124 }),
125 );
126
127 expect(updated.environmentId).toEqual(created.environmentId);
128 const afterUpdate = yield* getEnvironment(name);
129 const waitAfterUpdate = afterUpdate?.protection_rules?.find(
130 (rule) => rule.type === "wait_timer",
131 );
132 expect(waitAfterUpdate).toBeUndefined();
133 expect(yield* listBranchPolicies(name)).toEqual(["main"]);
134
135 // Switch the policy mode to protected branches only.
136 const switched = yield* stack.deploy(
137 Effect.gen(function* () {
138 const repository = yield* GitHub.Repository("Repo", {
139 owner,
140 name: repo,
141 description: "alchemy-effect environment test",
142 visibility: "public",
143 });
144
145 return yield* GitHub.Environment("Env", {
146 owner,
147 repository: repoName(repository),
148 name,
149 deploymentBranchPolicy: { protectedBranches: true },
150 }).pipe(destroy());
151 }),
152 );
153
154 expect(switched.environmentId).toEqual(created.environmentId);
155 const afterSwitch = yield* getEnvironment(name);
156 expect(afterSwitch?.deployment_branch_policy?.protected_branches).toBe(true);
157
158 // Delete — the environment goes away; the retained repo stays.
159 yield* stack.destroy();
160 const afterDestroy = yield* getEnvironment(name);
161 expect(afterDestroy).toBeUndefined();
162 }).pipe(logLevel),
163 {
164 tags: ["provider:github", "provider:github:repository", "live"],
165 timeout: 120_000,
166 },
167 );
168
169 test.provider.skipIf(!owner)(
170 "environment-scoped variable lifecycle",
171 (stack) =>
172 Effect.gen(function* () {
173 const name = "alchemy-test-variables";
174
175 yield* stack.destroy();
176
177 const deployVariable = (value: string) =>
178 stack.deploy(
179 Effect.gen(function* () {
180 const repository = yield* GitHub.Repository("Repo", {
181 owner,
182 name: repo,
183 description: "alchemy-effect environment test",
184 visibility: "public",
185 autoInit: true,
186 });
187
188 const environment = yield* GitHub.Environment("Env", {
189 owner,
190 repository: repoName(repository),
191 name,
192 }).pipe(destroy());
193
194 // Pass the Environment resource itself — the `environment` prop
195 // accepts `string | Environment` and resolves the name.
196 return yield* GitHub.Variable("Variable", {
197 owner,
198 repository: repoName(repository),
199 environment,
200 name: "ALCHEMY_ENV_TEST",
201 value,
202 }).pipe(destroy());
203 }),
204 );
205
206 const readVariable = Effect.gen(function* () {
207 return yield* gh(
208 Actions.getEnvironmentVariable({
209 owner,
210 repo,
211 environment_name: name,
212 name: "ALCHEMY_ENV_TEST",
213 }),
214 ).pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
215 });
216
217 // Create — the variable lands in the environment, not the repo.
218 yield* deployVariable("one");
219 const fetched = yield* readVariable;
220 expect(fetched?.value).toEqual("one");
221
222 // Update — reconcile PATCHes the drifted value in place.
223 yield* deployVariable("two");
224 const afterUpdate = yield* readVariable;
225 expect(afterUpdate?.value).toEqual("two");
226
227 // Delete — destroying the stack removes the variable (and environment).
228 yield* stack.destroy();
229 const afterDestroy = yield* readVariable;
230 expect(afterDestroy).toBeUndefined();
231 }).pipe(logLevel),
232 {
233 tags: ["provider:github", "provider:github:repository", "provider:github:variable", "live"],
234 timeout: 120_000,
235 },
236 );