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

main
ts 118 lines 4.31 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as EC2 from "@distilled.cloud/aws/ec2";
5 import * as fsx from "@distilled.cloud/aws/fsx";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import { getDefaultVpc } from "../DefaultVpc.ts";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 // FSx surfaces a missing file system as the typed FileSystemNotFound.
13 test.provider(
14 "typed FileSystemNotFound on a nonexistent file system",
15 () =>
16 Effect.gen(function* () {
17 const result = yield* fsx
18 .describeFileSystems({ FileSystemIds: ["fs-00000000000000000"] })
19 .pipe(Effect.result);
20 expect(result._tag).toBe("Failure");
21 if (result._tag === "Failure") {
22 const err = result.failure as { _tag: string };
23 expect(err._tag).toBe("FileSystemNotFound");
24 }
25 }),
26 { tags: ["provider:aws", "provider:aws:fsx", "live"], timeout: 60_000 },
27 );
28
29 const describeById = (fileSystemId: string) =>
30 fsx.describeFileSystems({ FileSystemIds: [fileSystemId] }).pipe(
31 Effect.map((r) => r.FileSystems?.[0]),
32 Effect.catchTag("FileSystemNotFound", () => Effect.succeed(undefined)),
33 );
34
35 // FSx create → AVAILABLE takes ~5 minutes; slow polling is allowed only
36 // because the whole lifecycle is skipped under `--fast`.
37 const waitUntilAvailable = (fileSystemId: string) =>
38 describeById(fileSystemId).pipe(
39 Effect.repeat({
40 schedule: Schedule.spaced("15 seconds"),
41 until: (fs) => fs?.Lifecycle === "AVAILABLE" || fs?.Lifecycle === "FAILED",
42 times: 60,
43 }),
44 );
45
46 const waitUntilGone = (fileSystemId: string) =>
47 describeById(fileSystemId).pipe(
48 Effect.repeat({
49 schedule: Schedule.spaced("15 seconds"),
50 until: (fs) => fs === undefined,
51 times: 60,
52 }),
53 );
54
55 const fsxTags = (tags: readonly fsx.Tag[] | undefined) =>
56 Object.fromEntries((tags ?? []).map((t) => [t.Key, t.Value]));
57
58 // A Lustre SCRATCH_2 file system takes several minutes to provision and a few
59 // more to delete (and bills hourly while up), so the live lifecycle is skipped
60 // under `--fast`.
61 test.provider.skipIf(!!process.env.FAST)(
62 "create Lustre scratch file system, update tags, delete",
63 (stack) =>
64 Effect.gen(function* () {
65 yield* stack.destroy();
66
67 const vpc = yield* getDefaultVpc;
68 const subnets = yield* EC2.describeSubnets({
69 Filters: [
70 { Name: "vpc-id", Values: [vpc.vpcId] },
71 { Name: "default-for-az", Values: ["true"] },
72 ],
73 });
74 const subnetId = subnets.Subnets![0]!.SubnetId!;
75
76 const build = (tags: Record<string, string>) =>
77 Effect.gen(function* () {
78 const scratch = yield* AWS.FSx.FileSystem("Scratch", {
79 fileSystemType: "LUSTRE",
80 storageCapacity: 1200,
81 subnetIds: [subnetId],
82 lustreConfiguration: { DeploymentType: "SCRATCH_2" },
83 tags,
84 });
85 return { scratch };
86 });
87
88 // --- create ---
89 const created = yield* stack.deploy(build({ purpose: "alchemy-fsx-test" }));
90 expect(created.scratch.fileSystemId).toContain("fs-");
91 expect(created.scratch.fileSystemArn).toContain(":file-system/fs-");
92 expect(created.scratch.fileSystemType).toBe("LUSTRE");
93 const fileSystemId = created.scratch.fileSystemId;
94
95 const available = yield* waitUntilAvailable(fileSystemId);
96 expect(available?.Lifecycle).toBe("AVAILABLE");
97 expect(available?.FileSystemType).toBe("LUSTRE");
98 expect(available?.StorageCapacity).toBe(1200);
99 const tags = fsxTags(available?.Tags);
100 expect(tags.purpose).toBe("alchemy-fsx-test");
101 expect(tags["alchemy::id"]).toBe("Scratch");
102
103 // --- update: tags sync in place, identity preserved ---
104 const updated = yield* stack.deploy(build({ purpose: "alchemy-fsx-test", stage: "prod" }));
105 expect(updated.scratch.fileSystemId).toBe(fileSystemId);
106 const observed = yield* describeById(fileSystemId);
107 expect(fsxTags(observed?.Tags).stage).toBe("prod");
108
109 // --- delete ---
110 yield* stack.destroy();
111 const gone = yield* waitUntilGone(fileSystemId);
112 expect(gone).toBeUndefined();
113 }),
114 {
115 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:fsx", "live"],
116 timeout: 1_500_000,
117 },
118 );