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

main
ts 167 lines 5.83 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 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 dsTags = (tags: readonly datasync.TagListEntry[] | undefined) =>
12 Object.fromEntries((tags ?? []).map((t) => [t.Key, t.Value]));
13
14 // DataSync surfaces a deleted location as the typed LocationNotFound (patched
15 // from InvalidRequestException + "…is not found").
16 const waitUntilLocationGone = (locationArn: string) =>
17 datasync.describeLocationS3({ LocationArn: locationArn }).pipe(
18 Effect.as(false),
19 Effect.catchTag("LocationNotFound", () => Effect.succeed(true)),
20 Effect.repeat({
21 schedule: Schedule.fixed("1 seconds"),
22 until: (gone) => gone,
23 times: 20,
24 }),
25 );
26
27 const trustPolicy = {
28 Version: "2012-10-17" as const,
29 Statement: [
30 {
31 Effect: "Allow" as const,
32 Principal: { Service: "datasync.amazonaws.com" },
33 Action: ["sts:AssumeRole"],
34 },
35 ],
36 };
37
38 test.provider(
39 "typed LocationNotFound on a nonexistent S3 location",
40 () =>
41 Effect.gen(function* () {
42 const result = yield* datasync
43 .describeLocationS3({
44 LocationArn: "arn:aws:datasync:us-west-2:391965393224:location/loc-00000000000000000",
45 })
46 .pipe(Effect.result);
47 expect(result._tag).toBe("Failure");
48 if (result._tag === "Failure") {
49 const err = result.failure as { _tag: string };
50 expect(err._tag).toBe("LocationNotFound");
51 }
52 }),
53 { tags: ["provider:aws", "provider:aws:datasync", "live"], timeout: 60_000 },
54 );
55
56 test.provider(
57 "create, update tags, list, delete an S3 location",
58 (stack) =>
59 Effect.gen(function* () {
60 yield* stack.destroy();
61
62 // --- create: bucket + access role + location ---
63 const created = yield* stack.deploy(
64 Effect.gen(function* () {
65 const bucket = yield* AWS.S3.Bucket("DsBucket", {
66 forceDestroy: true,
67 });
68 const role = yield* AWS.IAM.Role("DsRole", {
69 assumeRolePolicyDocument: trustPolicy,
70 inlinePolicies: {
71 DataSyncS3: {
72 Version: "2012-10-17",
73 Statement: [
74 {
75 Effect: "Allow",
76 Action: [
77 "s3:GetBucketLocation",
78 "s3:ListBucket",
79 "s3:ListBucketMultipartUploads",
80 ],
81 Resource: [bucket.bucketArn],
82 },
83 {
84 Effect: "Allow",
85 Action: [
86 "s3:AbortMultipartUpload",
87 "s3:DeleteObject",
88 "s3:GetObject",
89 "s3:ListMultipartUploadParts",
90 "s3:PutObject",
91 ],
92 Resource: [Output.interpolate`${bucket.bucketArn}/*`],
93 },
94 ],
95 },
96 },
97 });
98 const location = yield* AWS.DataSync.LocationS3("Loc", {
99 s3BucketArn: bucket.bucketArn,
100 bucketAccessRoleArn: role.roleArn,
101 subdirectory: "/data",
102 tags: { purpose: "alchemy-datasync-test" },
103 });
104 return { location };
105 }),
106 );
107 expect(created.location.locationArn).toContain(":location/loc-");
108 expect(created.location.locationUri).toContain("s3://");
109 expect(created.location.locationUri).toContain("/data");
110 const arn = created.location.locationArn;
111
112 const observed = yield* datasync.describeLocationS3({ LocationArn: arn });
113 expect(observed.S3Config?.BucketAccessRoleArn).toContain(":role/");
114
115 const tags = dsTags((yield* datasync.listTagsForResource({ ResourceArn: arn })).Tags);
116 expect(tags.purpose).toBe("alchemy-datasync-test");
117 expect(tags["alchemy::id"]).toBe("Loc");
118
119 // --- list: the S3 location is enumerable ---
120 const listed = yield* datasync.listLocations({});
121 expect((listed.Locations ?? []).some((l) => l.LocationArn === arn)).toBe(true);
122
123 // --- update: change tags in place (location itself is immutable) ---
124 const updated = yield* stack.deploy(
125 Effect.gen(function* () {
126 const bucket = yield* AWS.S3.Bucket("DsBucket", {
127 forceDestroy: true,
128 });
129 const role = yield* AWS.IAM.Role("DsRole", {
130 assumeRolePolicyDocument: trustPolicy,
131 inlinePolicies: {
132 DataSyncS3: {
133 Version: "2012-10-17",
134 Statement: [
135 {
136 Effect: "Allow",
137 Action: ["s3:GetBucketLocation", "s3:ListBucket"],
138 Resource: [bucket.bucketArn],
139 },
140 ],
141 },
142 },
143 });
144 const location = yield* AWS.DataSync.LocationS3("Loc", {
145 s3BucketArn: bucket.bucketArn,
146 bucketAccessRoleArn: role.roleArn,
147 subdirectory: "/data",
148 tags: { purpose: "alchemy-datasync-test", stage: "prod" },
149 });
150 return { location };
151 }),
152 );
153 // immutable identity is preserved across a tag-only update
154 expect(updated.location.locationArn).toBe(arn);
155 const tags2 = dsTags((yield* datasync.listTagsForResource({ ResourceArn: arn })).Tags);
156 expect(tags2.stage).toBe("prod");
157
158 // --- delete ---
159 yield* stack.destroy();
160 const gone = yield* waitUntilLocationGone(arn);
161 expect(gone).toBe(true);
162 }),
163 {
164 tags: ["provider:aws", "provider:aws:datasync", "provider:aws:iam", "provider:aws:s3", "live"],
165 timeout: 240_000,
166 },
167 );