@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 179 lines 6.48 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 s3files from "@distilled.cloud/aws/s3files";
5 import * as Output from "alchemy/Output";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: AWS.providers() });
10
11 const tagRecord = (tags: readonly s3files.Tag[] | undefined) =>
12 Object.fromEntries((tags ?? []).map((t) => [t.key, t.value]));
13
14 // Ungated typed-error probe: prove the distilled error union carries the
15 // not-found tag this provider's observe/read/delete paths depend on. Runs
16 // in every CI pass at near-zero cost, unlike the gated lifecycle below.
17 test.provider(
18 "getFileSystem on a nonexistent file system fails with ResourceNotFoundException",
19 () =>
20 Effect.gen(function* () {
21 const error = yield* Effect.flip(
22 s3files.getFileSystem({ fileSystemId: "fs-0123456789abcdef0" }),
23 );
24 expect(error._tag).toBe("ResourceNotFoundException");
25 }),
26 { tags: ["provider:aws", "provider:aws:s3files", "live"], timeout: 60_000 },
27 );
28
29 // Ungated reachability probe: the account can enumerate file systems in the
30 // test region (proves endpoint + auth + response decoding).
31 test.provider(
32 "listFileSystems succeeds",
33 () =>
34 Effect.gen(function* () {
35 const listed = yield* s3files.listFileSystems({ maxResults: 5 });
36 expect(Array.isArray(listed.fileSystems)).toBe(true);
37 }),
38 { tags: ["provider:aws", "provider:aws:s3files", "live"], timeout: 60_000 },
39 );
40
41 // S3 Files runs on EFS infrastructure — the file system role is assumed by
42 // the elasticfilesystem service principal, not an s3files one.
43 // https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-files-prereq-policies.html
44 const trustPolicy = {
45 Version: "2012-10-17" as const,
46 Statement: [
47 {
48 Effect: "Allow" as const,
49 Principal: { Service: "elasticfilesystem.amazonaws.com" },
50 Action: ["sts:AssumeRole"],
51 },
52 ],
53 };
54
55 const waitUntilFileSystemGone = (fileSystemId: string) =>
56 s3files.getFileSystem({ fileSystemId }).pipe(
57 Effect.as(false),
58 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
59 Effect.repeat({
60 schedule: Schedule.fixed("5 seconds"),
61 until: (gone) => gone,
62 times: 24,
63 }),
64 );
65
66 test.provider(
67 "create file system + access point, verify, destroy",
68 (stack) =>
69 Effect.gen(function* () {
70 yield* stack.destroy();
71
72 const created = yield* stack.deploy(
73 Effect.gen(function* () {
74 // S3 Files requires versioning on the source bucket.
75 const bucket = yield* AWS.S3.Bucket("FilesBucket", {
76 forceDestroy: true,
77 versioning: "Enabled",
78 });
79 const role = yield* AWS.IAM.Role("FilesRole", {
80 assumeRolePolicyDocument: trustPolicy,
81 inlinePolicies: {
82 S3FilesBucketAccess: {
83 Version: "2012-10-17",
84 Statement: [
85 {
86 Effect: "Allow",
87 Action: ["s3:ListBucket", "s3:ListBucketVersions"],
88 Resource: [bucket.bucketArn],
89 },
90 {
91 Effect: "Allow",
92 Action: [
93 "s3:GetObject*",
94 "s3:PutObject*",
95 "s3:DeleteObject*",
96 "s3:List*",
97 "s3:AbortMultipartUpload",
98 ],
99 Resource: [Output.interpolate`${bucket.bucketArn}/*`],
100 },
101 {
102 Effect: "Allow",
103 Action: [
104 "events:DeleteRule",
105 "events:DisableRule",
106 "events:EnableRule",
107 "events:PutRule",
108 "events:PutTargets",
109 "events:RemoveTargets",
110 ],
111 Resource: ["arn:aws:events:*:*:rule/DO-NOT-DELETE-S3-Files*"],
112 },
113 {
114 Effect: "Allow",
115 Action: [
116 "events:DescribeRule",
117 "events:ListRuleNamesByTarget",
118 "events:ListRules",
119 "events:ListTargetsByRule",
120 ],
121 Resource: ["arn:aws:events:*:*:rule/*"],
122 },
123 ],
124 },
125 },
126 });
127 const fileSystem = yield* AWS.S3Files.FileSystem("Files", {
128 bucket: bucket.bucketArn,
129 roleArn: role.roleArn,
130 tags: { fixture: "s3files-filesystem" },
131 });
132 const accessPoint = yield* AWS.S3Files.AccessPoint("Access", {
133 fileSystemId: fileSystem.fileSystemId,
134 posixUser: { uid: 1000, gid: 1000 },
135 rootDirectory: {
136 path: "/app",
137 creationPermissions: {
138 ownerUid: 1000,
139 ownerGid: 1000,
140 permissions: "0755",
141 },
142 },
143 });
144 return { fileSystem, accessPoint };
145 }),
146 );
147
148 expect(created.fileSystem.fileSystemId).toContain("fs-");
149 expect(created.fileSystem.fileSystemArn).toContain(":file-system/");
150 expect(created.fileSystem.status).toBe("available");
151 expect(created.accessPoint.fileSystemId).toBe(created.fileSystem.fileSystemId);
152 expect(created.accessPoint.status).toBe("available");
153
154 // Out-of-band verification via distilled.
155 const observed = yield* s3files.getFileSystem({
156 fileSystemId: created.fileSystem.fileSystemId,
157 });
158 expect(observed.status).toBe("available");
159 expect(observed.bucket).toBe(created.fileSystem.bucket);
160 const tags = tagRecord(observed.tags);
161 expect(tags.fixture).toBe("s3files-filesystem");
162 expect(tags["alchemy::id"]).toBe("Files");
163
164 const observedAp = yield* s3files.getAccessPoint({
165 accessPointId: created.accessPoint.accessPointId,
166 });
167 expect(observedAp.posixUser?.uid).toBe(1000);
168 expect(observedAp.rootDirectory?.path).toBe("/app");
169
170 // Destroy and verify the file system is gone out-of-band.
171 yield* stack.destroy();
172 const gone = yield* waitUntilFileSystemGone(created.fileSystem.fileSystemId);
173 expect(gone).toBe(true);
174 }),
175 {
176 tags: ["provider:aws", "provider:aws:iam", "provider:aws:s3", "provider:aws:s3files", "live"],
177 timeout: 600_000,
178 },
179 );