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

main
ts 315 lines 11 KB
Raw
1 import * as Fly from "@alchemy.run/fly";
2 import { attachBucketSecrets } from "@alchemy.run/fly/Bucket";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as machines from "@distilled.cloud/fly-io/machines";
6 import * as Effect from "effect/Effect";
7 import * as FileSystem from "effect/FileSystem";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as Layer from "effect/Layer";
10 import * as Path from "effect/Path";
11 import * as Redacted from "effect/Redacted";
12 import * as Result from "effect/Result";
13 import * as Schedule from "effect/Schedule";
14
15 const requests: Array<{ method: string; path: string }> = [];
16 const providers = Layer.effect(
17 HttpClient.HttpClient,
18 Effect.gen(function* () {
19 const client = yield* HttpClient.HttpClient;
20 return HttpClient.mapRequestEffect(client, (request) =>
21 Effect.sync(() => {
22 if (request.url.startsWith("https://api.machines.dev/")) {
23 requests.push({
24 method: request.method,
25 path: new URL(request.url).pathname,
26 });
27 }
28 return request;
29 }),
30 );
31 }),
32 ).pipe(Layer.provideMerge(Fly.providers()));
33
34 const { test } = Test.make({ providers });
35
36 const assertAppGone = (appName: string) =>
37 machines.getApp({ app_name: appName }).pipe(
38 Effect.as(false),
39 Effect.catchTag("NotFound", () => Effect.succeed(true)),
40 Effect.repeat({
41 schedule: Schedule.spaced("1 second"),
42 until: (gone) => gone,
43 times: 10,
44 }),
45 Effect.tap((gone) => Effect.sync(() => expect(gone).toBe(true))),
46 );
47
48 const secretMetadata = (appName: string) =>
49 machines
50 .listSecrets({ app_name: appName, show_secrets: false })
51 .pipe(
52 Effect.map((response) =>
53 (response.secrets ?? [])
54 .map((secret) => ({ name: secret.name, digest: secret.digest }))
55 .sort((a, b) => (a.name ?? "").localeCompare(b.name ?? "")),
56 ),
57 );
58
59 const scenarios: Array<{
60 name: string;
61 attachment: boolean;
62 env: Record<string, string>;
63 }> = [
64 { name: "missing add-on without credentials", attachment: true, env: {} },
65 {
66 name: "missing add-on with only an access key and endpoint",
67 attachment: true,
68 env: {
69 AWS_ACCESS_KEY_ID: "unused-negative-test-key",
70 AWS_ENDPOINT_URL_S3: "https://fly.storage.tigris.dev",
71 },
72 },
73 {
74 name: "missing add-on with credentials but no endpoint",
75 attachment: true,
76 env: {
77 AWS_ACCESS_KEY_ID: "unused-negative-test-key",
78 AWS_SECRET_ACCESS_KEY: "unused-negative-test-secret",
79 },
80 },
81 {
82 name: "bound-env-only credentials without an endpoint",
83 attachment: false,
84 env: {
85 AWS_ACCESS_KEY_ID: "unused-negative-test-key",
86 AWS_SECRET_ACCESS_KEY: "unused-negative-test-secret",
87 },
88 },
89 ];
90
91 for (const scenario of scenarios) {
92 test.provider(
93 `F12 ${scenario.name} fails before secret writes or candidate creation`,
94 (stack) =>
95 Effect.gen(function* () {
96 yield* stack.destroy();
97 const app = yield* stack.deploy(Fly.App("Site"));
98 yield* Effect.gen(function* () {
99 const missingName = `${app.appName}-absent-bucket`;
100 const before = yield* secretMetadata(app.appName);
101 const offset = yield* Effect.sync(() => requests.length);
102 const result = yield* stack
103 .deploy(
104 Effect.gen(function* () {
105 const site = yield* Fly.App("Site");
106 const service = yield* Fly.Service("Consumer", {
107 app: site,
108 // Attachment validation must fail before image resolution.
109 main: import.meta.url,
110 isExternal: true,
111 services: [],
112 deploy: { strategy: "bluegreen" },
113 checks: { ready: { type: "tcp", port: 3000 } },
114 });
115 yield* service.bind("BucketAttachment", {
116 bucket: scenario.attachment ? { name: missingName } : undefined,
117 env: { BUCKET_NAME: missingName, ...scenario.env },
118 });
119 return service;
120 }),
121 )
122 .pipe(Effect.result);
123 expect(Result.isFailure(result)).toBe(true);
124 if (Result.isFailure(result)) {
125 expect(result.failure).toMatchObject({
126 _tag: "Fly.TigrisCredentialsMissing",
127 });
128 }
129 const mutations = yield* Effect.sync(() => {
130 const prefix = `/v1/apps/${app.appName}/`;
131 return requests
132 .slice(offset)
133 .filter(
134 (request) =>
135 request.method !== "GET" &&
136 (request.path.startsWith(`${prefix}machines`) ||
137 request.path.startsWith(`${prefix}secrets`)),
138 );
139 });
140 expect(mutations).toEqual([]);
141 expect(yield* machines.listMachines({ app_name: app.appName })).toEqual([]);
142 expect(
143 yield* Effect.sync(() =>
144 requests
145 .slice(offset)
146 .some(
147 (request) =>
148 request.method === "GET" && request.path === `/v1/apps/${app.appName}/machines`,
149 ),
150 ),
151 ).toBe(true);
152 expect(yield* secretMetadata(app.appName)).toEqual(before);
153 }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie)));
154 yield* assertAppGone(app.appName);
155 }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))),
156 {
157 tags: [
158 "provider:fly",
159 "provider:fly:app",
160 "provider:fly:bucket",
161 "provider:fly:machine",
162 "provider:fly:service",
163 "live",
164 ],
165 timeout: 300_000,
166 },
167 );
168 }
169
170 const credential = (value: Redacted.Redacted<string> | undefined, field: string) =>
171 value === undefined
172 ? Effect.fail(new Error(`Real Tigris bucket did not return ${field}`))
173 : Effect.sync(() => Redacted.value(value));
174
175 test.provider(
176 "F12 complete real bound credentials survive a missing add-on reference",
177 (stack) =>
178 Effect.gen(function* () {
179 yield* stack.destroy();
180 const created = yield* stack.deploy(
181 Effect.gen(function* () {
182 const app = yield* Fly.App("Site");
183 const bucket = yield* Fly.Bucket("Data");
184 return { app, bucket };
185 }),
186 );
187 yield* Effect.gen(function* () {
188 const env = {
189 BUCKET_NAME: created.bucket.name,
190 AWS_ACCESS_KEY_ID: yield* credential(created.bucket.accessKeyId, "access key"),
191 AWS_SECRET_ACCESS_KEY: yield* credential(created.bucket.secretAccessKey, "secret key"),
192 AWS_ENDPOINT_URL_S3: yield* credential(created.bucket.endpoint, "endpoint"),
193 };
194 const floor = yield* attachBucketSecrets(
195 created.app.appName,
196 [{ name: "", id: `${created.bucket.addOnId}-absent` }],
197 env,
198 );
199 expect(floor).toEqual(expect.any(Number));
200 const fromEnvFloor = yield* attachBucketSecrets(created.app.appName, [], env);
201 expect(fromEnvFloor).toEqual(expect.any(Number));
202 if (floor !== undefined && fromEnvFloor !== undefined) {
203 expect(fromEnvFloor).toBeGreaterThanOrEqual(floor);
204 }
205 const names = (yield* secretMetadata(created.app.appName)).map((secret) => secret.name);
206 for (const name of [...Object.keys(env), "AWS_ENDPOINT_URL"]) {
207 expect(names).toContain(name);
208 }
209 const appRequestCount = Effect.sync(
210 () =>
211 requests.filter((request) =>
212 request.path.startsWith(`/v1/apps/${created.app.appName}/`),
213 ).length,
214 );
215 const beforeNoop = yield* appRequestCount;
216 expect(yield* attachBucketSecrets(created.app.appName, [], {})).toBeUndefined();
217 expect(yield* appRequestCount).toBe(beforeNoop);
218 expect(yield* machines.listMachines({ app_name: created.app.appName })).toEqual([]);
219 }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie)));
220 yield* assertAppGone(created.app.appName);
221 }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))),
222 {
223 tags: [
224 "provider:fly",
225 "provider:fly:app",
226 "provider:fly:bucket",
227 "provider:fly:machine",
228 "live",
229 ],
230 timeout: 300_000,
231 },
232 );
233
234 test.provider(
235 "ordinary AWS environment without a Bucket deploys a Service without attachment writes",
236 (stack) =>
237 Effect.gen(function* () {
238 yield* stack.destroy();
239 const fs = yield* FileSystem.FileSystem;
240 const path = yield* Path.Path;
241 const directory = yield* fs.makeTempDirectoryScoped({
242 prefix: "alchemy-fly-ordinary-aws-env-",
243 });
244 const main = path.join(directory, "main.mjs");
245 yield* fs.writeFileString(
246 main,
247 'if (!process.env.AWS_REGION) throw new Error("missing AWS region");\nsetInterval(() => {}, 1000);\n',
248 );
249 const app = yield* stack.deploy(Fly.App("Site"));
250 const before = yield* secretMetadata(app.appName);
251 const environments: Array<Record<string, string>> = [
252 { AWS_REGION: "us-east-1" },
253 {
254 AWS_REGION: "us-east-1",
255 AWS_ACCESS_KEY_ID: "ordinary-aws-test-key",
256 AWS_SECRET_ACCESS_KEY: "ordinary-aws-test-secret",
257 },
258 ];
259 for (const env of environments) {
260 const offset = yield* Effect.sync(() => requests.length);
261 expect(yield* attachBucketSecrets(app.appName, [], env)).toBeUndefined();
262 const service = yield* stack.deploy(
263 Effect.gen(function* () {
264 const site = yield* Fly.App("Site");
265 return yield* Fly.Service("Consumer", {
266 app: site,
267 main,
268 isExternal: true,
269 services: [],
270 env,
271 });
272 }),
273 );
274 const current = yield* machines.getMachine({
275 app_name: app.appName,
276 machine_id: service.machineId,
277 });
278 expect(current.state).toBe("started");
279 expect(current.config?.env).toMatchObject(env);
280 expect(yield* secretMetadata(app.appName)).toEqual(before);
281 const observed = yield* Effect.sync(() =>
282 requests
283 .slice(offset)
284 .filter((request) => request.path.startsWith(`/v1/apps/${app.appName}/`)),
285 );
286 expect(
287 observed.some(
288 (request) =>
289 request.method === "GET" &&
290 request.path === `/v1/apps/${app.appName}/machines/${service.machineId}`,
291 ),
292 ).toBe(true);
293 expect(
294 observed.filter(
295 (request) =>
296 request.method !== "GET" &&
297 request.path.startsWith(`/v1/apps/${app.appName}/secrets`),
298 ),
299 ).toEqual([]);
300 }
301 yield* stack.destroy();
302 yield* assertAppGone(app.appName);
303 }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie)), Effect.scoped),
304 {
305 tags: [
306 "provider:fly",
307 "provider:fly:app",
308 "provider:fly:bucket",
309 "provider:fly:machine",
310 "provider:fly:service",
311 "live",
312 ],
313 timeout: 600_000,
314 },
315 );