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

main
ts 199 lines 6.6 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 datasync from "@distilled.cloud/aws/datasync";
5 import * as S3 from "@distilled.cloud/aws/s3";
6 import * as Output from "alchemy/Output";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 const waitUntilTaskGone = (taskArn: string) =>
13 datasync.describeTask({ TaskArn: taskArn }).pipe(
14 Effect.as(false),
15 Effect.catchTag("TaskNotFound", () => Effect.succeed(true)),
16 Effect.repeat({
17 schedule: Schedule.fixed("1 seconds"),
18 until: (gone) => gone,
19 times: 20,
20 }),
21 );
22
23 const trustPolicy = {
24 Version: "2012-10-17" as const,
25 Statement: [
26 {
27 Effect: "Allow" as const,
28 Principal: { Service: "datasync.amazonaws.com" },
29 Action: ["sts:AssumeRole"],
30 },
31 ],
32 };
33
34 const s3Policy = (arn: Output.Output<string>) => ({
35 Version: "2012-10-17" as const,
36 Statement: [
37 {
38 Effect: "Allow" as const,
39 Action: ["s3:GetBucketLocation", "s3:ListBucket", "s3:ListBucketMultipartUploads"],
40 Resource: [arn],
41 },
42 {
43 Effect: "Allow" as const,
44 Action: [
45 "s3:AbortMultipartUpload",
46 "s3:DeleteObject",
47 "s3:GetObject",
48 "s3:GetObjectTagging",
49 "s3:ListMultipartUploadParts",
50 "s3:PutObject",
51 "s3:PutObjectTagging",
52 ],
53 Resource: [Output.interpolate`${arn}/*`],
54 },
55 ],
56 });
57
58 const buildStack = (options?: datasync.Options) =>
59 Effect.gen(function* () {
60 const src = yield* AWS.S3.Bucket("TaskSrc", { forceDestroy: true });
61 const dst = yield* AWS.S3.Bucket("TaskDst", { forceDestroy: true });
62 const role = yield* AWS.IAM.Role("TaskRole", {
63 assumeRolePolicyDocument: trustPolicy,
64 inlinePolicies: {
65 Src: s3Policy(src.bucketArn),
66 Dst: s3Policy(dst.bucketArn),
67 },
68 });
69 const source = yield* AWS.DataSync.LocationS3("TaskSrcLoc", {
70 s3BucketArn: src.bucketArn,
71 bucketAccessRoleArn: role.roleArn,
72 });
73 const dest = yield* AWS.DataSync.LocationS3("TaskDstLoc", {
74 s3BucketArn: dst.bucketArn,
75 bucketAccessRoleArn: role.roleArn,
76 });
77 const task = yield* AWS.DataSync.Task("BackupTask", {
78 sourceLocationArn: source.locationArn,
79 destinationLocationArn: dest.locationArn,
80 ...(options !== undefined ? { options } : {}),
81 });
82 return { task, src, dst };
83 });
84
85 test.provider(
86 "typed TaskNotFound on a nonexistent task",
87 () =>
88 Effect.gen(function* () {
89 const result = yield* datasync
90 .describeTask({
91 TaskArn: "arn:aws:datasync:us-west-2:391965393224:task/task-00000000000000000",
92 })
93 .pipe(Effect.result);
94 expect(result._tag).toBe("Failure");
95 if (result._tag === "Failure") {
96 const err = result.failure as { _tag: string };
97 expect(err._tag).toBe("TaskNotFound");
98 }
99 }),
100 { tags: ["provider:aws", "provider:aws:datasync", "live"], timeout: 60_000 },
101 );
102
103 test.provider(
104 "create S3 -> S3 task, update options, delete",
105 (stack) =>
106 Effect.gen(function* () {
107 yield* stack.destroy();
108
109 // --- create: two S3 locations + a transfer task ---
110 const created = yield* stack.deploy(buildStack());
111 expect(created.task.taskArn).toContain(":task/task-");
112 expect(created.task.sourceLocationArn).toContain(":location/loc-");
113 expect(created.task.destinationLocationArn).toContain(":location/loc-");
114 const arn = created.task.taskArn;
115
116 const observed = yield* datasync.describeTask({ TaskArn: arn });
117 expect(observed.SourceLocationArn).toBe(created.task.sourceLocationArn);
118 const tags = Object.fromEntries(
119 ((yield* datasync.listTagsForResource({ ResourceArn: arn })).Tags ?? []).map((t) => [
120 t.Key,
121 t.Value,
122 ]),
123 );
124 expect(tags["alchemy::id"]).toBe("BackupTask");
125
126 // --- update: change transfer options in place ---
127 const updated = yield* stack.deploy(buildStack({ VerifyMode: "ONLY_FILES_TRANSFERRED" }));
128 expect(updated.task.taskArn).toBe(arn);
129 const observed2 = yield* datasync.describeTask({ TaskArn: arn });
130 expect(observed2.Options?.VerifyMode).toBe("ONLY_FILES_TRANSFERRED");
131
132 // --- delete ---
133 yield* stack.destroy();
134 const gone = yield* waitUntilTaskGone(arn);
135 expect(gone).toBe(true);
136 }),
137 {
138 tags: ["provider:aws", "provider:aws:datasync", "provider:aws:iam", "provider:aws:s3", "live"],
139 timeout: 240_000,
140 },
141 );
142
143 // A live BASIC-mode task execution has 1–4 minutes of launch/prepare
144 // overhead (~2.5 min observed), so it is skipped under --fast.
145 test.provider.skipIf(!!process.env.FAST)(
146 "live S3 -> S3 task execution transfers an object",
147 (stack) =>
148 Effect.gen(function* () {
149 yield* stack.destroy();
150
151 const created = yield* stack.deploy(buildStack({ VerifyMode: "ONLY_FILES_TRANSFERRED" }));
152
153 yield* S3.putObject({
154 Bucket: created.src.bucketName,
155 Key: "hello.txt",
156 Body: "hello datasync",
157 });
158
159 // The freshly-created IAM role's permissions take a while to propagate
160 // to DataSync's location access test (typed LocationAccessTestFailed,
161 // patched from InvalidRequestException + "location access test failed").
162 const started = yield* datasync.startTaskExecution({ TaskArn: created.task.taskArn }).pipe(
163 Effect.retry({
164 while: (e) => e._tag === "LocationAccessTestFailed",
165 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(12)]),
166 }),
167 );
168 const finished = yield* datasync
169 .describeTaskExecution({
170 TaskExecutionArn: started.TaskExecutionArn!,
171 })
172 .pipe(
173 Effect.repeat({
174 schedule: Schedule.spaced("15 seconds"),
175 until: (e) => e.Status === "SUCCESS" || e.Status === "ERROR",
176 times: 40,
177 }),
178 );
179 if (finished.Status !== "SUCCESS") {
180 // surface the execution failure detail before the assertion trips
181 yield* Effect.log("task execution result", finished.Result);
182 }
183 expect(finished.Status).toBe("SUCCESS");
184
185 // out-of-band: the object landed in the destination bucket
186 yield* S3.headObject({
187 Bucket: created.dst.bucketName,
188 Key: "hello.txt",
189 });
190
191 yield* stack.destroy();
192 const gone = yield* waitUntilTaskGone(created.task.taskArn);
193 expect(gone).toBe(true);
194 }),
195 {
196 tags: ["provider:aws", "provider:aws:datasync", "provider:aws:iam", "provider:aws:s3", "live"],
197 timeout: 900_000,
198 },
199 );