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

main
ts 111 lines 4.54 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 efs from "@distilled.cloud/aws/efs";
5 import * as Effect from "effect/Effect";
6 import * as Schedule from "effect/Schedule";
7
8 const { test } = Test.make({ providers: AWS.providers() });
9
10 const efsTagsToRecord = (tags: readonly efs.Tag[] | undefined): Record<string, string> =>
11 Object.fromEntries((tags ?? []).map((t) => [t.Key, t.Value]));
12
13 // Typed wait-until-gone for an access point.
14 const waitUntilAccessPointGone = (accessPointId: string) =>
15 efs.describeAccessPoints({ AccessPointId: accessPointId }).pipe(
16 Effect.map((r) => (r.AccessPoints ?? []).every((ap) => ap.LifeCycleState === "deleted")),
17 Effect.catchTag("AccessPointNotFound", () => Effect.succeed(true)),
18 Effect.repeat({
19 schedule: Schedule.fixed("2 seconds"),
20 until: (gone) => gone,
21 times: 30,
22 }),
23 );
24
25 test.provider(
26 "create, update tags, replace, delete access point",
27 (stack) =>
28 Effect.gen(function* () {
29 yield* stack.destroy();
30
31 const infra = (posixUid: number, tag: string) =>
32 Effect.gen(function* () {
33 const files = yield* AWS.EFS.FileSystem("ApFiles");
34 const accessPoint = yield* AWS.EFS.AccessPoint("ApAccess", {
35 fileSystemId: files.fileSystemId,
36 posixUser: { uid: posixUid, gid: 1000 },
37 rootDirectory: {
38 path: "/app",
39 creationInfo: {
40 ownerUid: posixUid,
41 ownerGid: 1000,
42 permissions: "750",
43 },
44 },
45 tags: { purpose: tag },
46 });
47 return { files, accessPoint };
48 });
49
50 // --- create ---
51 const created = yield* stack.deploy(infra(1000, "alchemy-efs-ap-test"));
52 expect(created.accessPoint.accessPointId).toMatch(/^fsap-/);
53 expect(created.accessPoint.accessPointArn).toContain(":access-point/");
54 expect(created.accessPoint.fileSystemId).toBe(created.files.fileSystemId);
55
56 const observed = yield* efs
57 .describeAccessPoints({
58 AccessPointId: created.accessPoint.accessPointId,
59 })
60 .pipe(Effect.map((r) => r.AccessPoints![0]));
61 expect(observed.LifeCycleState).toBe("available");
62 expect(observed.PosixUser?.Uid).toBe(1000);
63 expect(observed.RootDirectory?.Path).toBe("/app");
64 const observedTags = efsTagsToRecord(observed.Tags);
65 expect(observedTags.purpose).toBe("alchemy-efs-ap-test");
66 expect(observedTags["alchemy::id"]).toBe("ApAccess");
67
68 // --- update tags in place ---
69 const retagged = yield* stack.deploy(infra(1000, "alchemy-efs-ap-retag"));
70 expect(retagged.accessPoint.accessPointId).toBe(created.accessPoint.accessPointId);
71 const retaggedObserved = yield* efs
72 .describeAccessPoints({
73 AccessPointId: created.accessPoint.accessPointId,
74 })
75 .pipe(Effect.map((r) => efsTagsToRecord(r.AccessPoints![0].Tags)));
76 expect(retaggedObserved.purpose).toBe("alchemy-efs-ap-retag");
77
78 // --- posixUser change replaces the access point ---
79 const replaced = yield* stack.deploy(infra(1001, "alchemy-efs-ap-retag"));
80 expect(replaced.accessPoint.accessPointId).not.toBe(created.accessPoint.accessPointId);
81 const replacedObserved = yield* efs
82 .describeAccessPoints({
83 AccessPointId: replaced.accessPoint.accessPointId,
84 })
85 .pipe(Effect.map((r) => r.AccessPoints![0]));
86 expect(replacedObserved.PosixUser?.Uid).toBe(1001);
87 const oldGone = yield* waitUntilAccessPointGone(created.accessPoint.accessPointId);
88 expect(oldGone).toBe(true);
89
90 // --- destroy: access point and file system both gone ---
91 yield* stack.destroy();
92 const apGone = yield* waitUntilAccessPointGone(replaced.accessPoint.accessPointId);
93 expect(apGone).toBe(true);
94 const fsGone = yield* efs
95 .describeFileSystems({ FileSystemId: created.files.fileSystemId })
96 .pipe(
97 Effect.map((r) => (r.FileSystems ?? []).every((f) => f.LifeCycleState === "deleted")),
98 Effect.catchTag("FileSystemNotFound", () => Effect.succeed(true)),
99 Effect.repeat({
100 schedule: Schedule.fixed("2 seconds"),
101 until: (gone) => gone,
102 times: 30,
103 }),
104 );
105 expect(fsGone).toBe(true);
106 }).pipe(
107 Effect.tap(() => stack.destroy()),
108 Effect.onError(() => stack.destroy().pipe(Effect.ignore)),
109 ),
110 { tags: ["provider:aws", "provider:aws:efs", "live"], timeout: 120_000 },
111 );