The best way to control Claude Code, Codex and more from any computer or phone
| 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 Provider from "alchemy/Provider"; |
| 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 efsTagsToRecord = (tags: readonly efs.Tag[] | undefined): Record<string, string> => |
| 12 | Object.fromEntries((tags ?? []).map((t) => [t.Key, t.Value])); |
| 13 | |
| 14 | // Typed wait-until-gone: DescribeFileSystems surfaces the typed |
| 15 | // FileSystemNotFound once the deletion finishes; a still-visible file system |
| 16 | // in the `deleted` state also counts as gone. |
| 17 | const waitUntilFileSystemGone = (fileSystemId: string) => |
| 18 | efs.describeFileSystems({ FileSystemId: fileSystemId }).pipe( |
| 19 | Effect.map((r) => (r.FileSystems ?? []).every((f) => f.LifeCycleState === "deleted")), |
| 20 | Effect.catchTag("FileSystemNotFound", () => Effect.succeed(true)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.fixed("2 seconds"), |
| 23 | until: (gone) => gone, |
| 24 | times: 30, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "create, update, list, delete file system", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | yield* stack.destroy(); |
| 33 | |
| 34 | // --- create: encrypted by default, elastic throughput --- |
| 35 | const created = yield* stack.deploy( |
| 36 | Effect.gen(function* () { |
| 37 | const files = yield* AWS.EFS.FileSystem("Files", { |
| 38 | throughputMode: "elastic", |
| 39 | tags: { purpose: "alchemy-efs-test" }, |
| 40 | }); |
| 41 | return { files }; |
| 42 | }), |
| 43 | ); |
| 44 | expect(created.files.fileSystemId).toMatch(/^fs-/); |
| 45 | expect(created.files.fileSystemArn).toContain(":file-system/"); |
| 46 | |
| 47 | const observed = yield* efs |
| 48 | .describeFileSystems({ FileSystemId: created.files.fileSystemId }) |
| 49 | .pipe(Effect.map((r) => r.FileSystems![0])); |
| 50 | expect(observed.Encrypted).toBe(true); |
| 51 | expect(observed.PerformanceMode).toBe("generalPurpose"); |
| 52 | expect(observed.ThroughputMode).toBe("elastic"); |
| 53 | const observedTags = efsTagsToRecord(observed.Tags); |
| 54 | expect(observedTags.purpose).toBe("alchemy-efs-test"); |
| 55 | expect(observedTags["alchemy::id"]).toBe("Files"); |
| 56 | |
| 57 | // --- update: lifecycle policies + typed file system policy + backup |
| 58 | // policy + replication overwrite protection + tags, same file |
| 59 | // system --- |
| 60 | const updated = yield* stack.deploy( |
| 61 | Effect.gen(function* () { |
| 62 | const files = yield* AWS.EFS.FileSystem("Files", { |
| 63 | throughputMode: "elastic", |
| 64 | backup: true, |
| 65 | replicationOverwriteProtection: "DISABLED", |
| 66 | lifecyclePolicies: [ |
| 67 | { transitionToIA: "AFTER_30_DAYS" }, |
| 68 | { transitionToPrimaryStorageClass: "AFTER_1_ACCESS" }, |
| 69 | ], |
| 70 | policy: { |
| 71 | Version: "2012-10-17", |
| 72 | Statement: [ |
| 73 | { |
| 74 | Sid: "AllowMountViaMountTarget", |
| 75 | Effect: "Allow", |
| 76 | Principal: { AWS: "*" }, |
| 77 | Action: ["elasticfilesystem:ClientMount", "elasticfilesystem:ClientWrite"], |
| 78 | Condition: { |
| 79 | Bool: { |
| 80 | "elasticfilesystem:AccessedViaMountTarget": "true", |
| 81 | }, |
| 82 | }, |
| 83 | }, |
| 84 | ], |
| 85 | }, |
| 86 | tags: { purpose: "alchemy-efs-test-updated" }, |
| 87 | }); |
| 88 | return { files }; |
| 89 | }), |
| 90 | ); |
| 91 | expect(updated.files.fileSystemId).toBe(created.files.fileSystemId); |
| 92 | |
| 93 | const lifecycle = yield* efs.describeLifecycleConfiguration({ |
| 94 | FileSystemId: created.files.fileSystemId, |
| 95 | }); |
| 96 | expect(lifecycle.LifecyclePolicies ?? []).toHaveLength(2); |
| 97 | |
| 98 | // the PolicyDocument round-trips as the file system policy |
| 99 | const storedPolicy = yield* efs |
| 100 | .describeFileSystemPolicy({ |
| 101 | FileSystemId: created.files.fileSystemId, |
| 102 | }) |
| 103 | .pipe(Effect.map((r) => r.Policy ?? "")); |
| 104 | expect(storedPolicy).toContain("AllowMountViaMountTarget"); |
| 105 | |
| 106 | const retagged = yield* efs |
| 107 | .describeFileSystems({ FileSystemId: created.files.fileSystemId }) |
| 108 | .pipe(Effect.map((r) => efsTagsToRecord(r.FileSystems![0].Tags))); |
| 109 | expect(retagged.purpose).toBe("alchemy-efs-test-updated"); |
| 110 | |
| 111 | // backup: true converged (ENABLING settles to ENABLED asynchronously) |
| 112 | const backupStatus = yield* efs |
| 113 | .describeBackupPolicy({ FileSystemId: created.files.fileSystemId }) |
| 114 | .pipe(Effect.map((r) => r.BackupPolicy?.Status)); |
| 115 | expect(["ENABLED", "ENABLING"]).toContain(backupStatus); |
| 116 | |
| 117 | // replication overwrite protection converged to DISABLED |
| 118 | const protection = yield* efs |
| 119 | .describeFileSystems({ FileSystemId: created.files.fileSystemId }) |
| 120 | .pipe( |
| 121 | Effect.map((r) => r.FileSystems![0].FileSystemProtection?.ReplicationOverwriteProtection), |
| 122 | ); |
| 123 | expect(protection).toBe("DISABLED"); |
| 124 | |
| 125 | // --- re-deploy: an equivalent PolicyDocument (statement keys in a |
| 126 | // different order) is a no-op — the normalized comparison skips |
| 127 | // the put and the stored policy is untouched. The tag change |
| 128 | // forces the reconcile to actually run. |
| 129 | yield* stack.deploy( |
| 130 | Effect.gen(function* () { |
| 131 | const files = yield* AWS.EFS.FileSystem("Files", { |
| 132 | throughputMode: "elastic", |
| 133 | lifecyclePolicies: [ |
| 134 | { transitionToIA: "AFTER_30_DAYS" }, |
| 135 | { transitionToPrimaryStorageClass: "AFTER_1_ACCESS" }, |
| 136 | ], |
| 137 | policy: { |
| 138 | Version: "2012-10-17", |
| 139 | Statement: [ |
| 140 | { |
| 141 | Condition: { |
| 142 | Bool: { |
| 143 | "elasticfilesystem:AccessedViaMountTarget": "true", |
| 144 | }, |
| 145 | }, |
| 146 | Action: ["elasticfilesystem:ClientMount", "elasticfilesystem:ClientWrite"], |
| 147 | Principal: { AWS: "*" }, |
| 148 | Effect: "Allow", |
| 149 | Sid: "AllowMountViaMountTarget", |
| 150 | }, |
| 151 | ], |
| 152 | }, |
| 153 | tags: { purpose: "alchemy-efs-test-redeploy" }, |
| 154 | }); |
| 155 | return { files }; |
| 156 | }), |
| 157 | ); |
| 158 | const redeployedPolicy = yield* efs |
| 159 | .describeFileSystemPolicy({ |
| 160 | FileSystemId: created.files.fileSystemId, |
| 161 | }) |
| 162 | .pipe(Effect.map((r) => r.Policy ?? "")); |
| 163 | expect(redeployedPolicy).toBe(storedPolicy); |
| 164 | |
| 165 | // --- update: clearing lifecycle policies + policy converges to |
| 166 | // empty / the default policy; backup + protection revert --- |
| 167 | yield* stack.deploy( |
| 168 | Effect.gen(function* () { |
| 169 | const files = yield* AWS.EFS.FileSystem("Files", { |
| 170 | throughputMode: "elastic", |
| 171 | backup: false, |
| 172 | replicationOverwriteProtection: "ENABLED", |
| 173 | tags: { purpose: "alchemy-efs-test-updated" }, |
| 174 | }); |
| 175 | return { files }; |
| 176 | }), |
| 177 | ); |
| 178 | const cleared = yield* efs.describeLifecycleConfiguration({ |
| 179 | FileSystemId: created.files.fileSystemId, |
| 180 | }); |
| 181 | expect(cleared.LifecyclePolicies ?? []).toHaveLength(0); |
| 182 | |
| 183 | // backup: false converged (DISABLING settles asynchronously) |
| 184 | const backupDisabled = yield* efs |
| 185 | .describeBackupPolicy({ FileSystemId: created.files.fileSystemId }) |
| 186 | .pipe( |
| 187 | Effect.map((r) => r.BackupPolicy?.Status ?? "DISABLED"), |
| 188 | Effect.catchTag("PolicyNotFound", () => Effect.succeed("DISABLED")), |
| 189 | ); |
| 190 | expect(["DISABLED", "DISABLING"]).toContain(backupDisabled); |
| 191 | |
| 192 | // protection reverted to ENABLED |
| 193 | const protectionReverted = yield* efs |
| 194 | .describeFileSystems({ FileSystemId: created.files.fileSystemId }) |
| 195 | .pipe( |
| 196 | Effect.map( |
| 197 | (r) => |
| 198 | r.FileSystems![0].FileSystemProtection?.ReplicationOverwriteProtection ?? "ENABLED", |
| 199 | ), |
| 200 | ); |
| 201 | expect(protectionReverted).toBe("ENABLED"); |
| 202 | |
| 203 | // removing the policy prop reverts to the default policy — the |
| 204 | // explicit policy is gone (typed PolicyNotFound) |
| 205 | const policyGone = yield* efs |
| 206 | .describeFileSystemPolicy({ |
| 207 | FileSystemId: created.files.fileSystemId, |
| 208 | }) |
| 209 | .pipe( |
| 210 | Effect.map(() => false), |
| 211 | Effect.catchTag("PolicyNotFound", () => Effect.succeed(true)), |
| 212 | ); |
| 213 | expect(policyGone).toBe(true); |
| 214 | |
| 215 | // --- list --- |
| 216 | const provider = yield* Provider.findProvider(AWS.EFS.FileSystem); |
| 217 | const all = yield* provider.list(); |
| 218 | expect(all.some((f) => f.fileSystemId === created.files.fileSystemId)).toBe(true); |
| 219 | |
| 220 | // --- delete + typed wait-until-gone --- |
| 221 | yield* stack.destroy(); |
| 222 | const gone = yield* waitUntilFileSystemGone(created.files.fileSystemId); |
| 223 | expect(gone).toBe(true); |
| 224 | }), |
| 225 | { tags: ["provider:aws", "provider:aws:efs", "live"], timeout: 180_000 }, |
| 226 | ); |
| 227 | |
| 228 | test.provider( |
| 229 | "changing encryption replaces the file system", |
| 230 | (stack) => |
| 231 | Effect.gen(function* () { |
| 232 | yield* stack.destroy(); |
| 233 | |
| 234 | const first = yield* stack.deploy( |
| 235 | Effect.gen(function* () { |
| 236 | const files = yield* AWS.EFS.FileSystem("ReplaceFiles", { |
| 237 | encrypted: false, |
| 238 | }); |
| 239 | return { files }; |
| 240 | }), |
| 241 | ); |
| 242 | |
| 243 | const second = yield* stack.deploy( |
| 244 | Effect.gen(function* () { |
| 245 | const files = yield* AWS.EFS.FileSystem("ReplaceFiles", { |
| 246 | encrypted: true, |
| 247 | }); |
| 248 | return { files }; |
| 249 | }), |
| 250 | ); |
| 251 | |
| 252 | expect(second.files.fileSystemId).not.toBe(first.files.fileSystemId); |
| 253 | |
| 254 | const observed = yield* efs |
| 255 | .describeFileSystems({ FileSystemId: second.files.fileSystemId }) |
| 256 | .pipe(Effect.map((r) => r.FileSystems![0])); |
| 257 | expect(observed.Encrypted).toBe(true); |
| 258 | |
| 259 | // the replaced (unencrypted) file system must be gone |
| 260 | const oldGone = yield* waitUntilFileSystemGone(first.files.fileSystemId); |
| 261 | expect(oldGone).toBe(true); |
| 262 | |
| 263 | yield* stack.destroy(); |
| 264 | const gone = yield* waitUntilFileSystemGone(second.files.fileSystemId); |
| 265 | expect(gone).toBe(true); |
| 266 | }).pipe( |
| 267 | Effect.tap(() => stack.destroy()), |
| 268 | Effect.onError(() => stack.destroy().pipe(Effect.ignore)), |
| 269 | ), |
| 270 | { tags: ["provider:aws", "provider:aws:efs", "live"], timeout: 120_000 }, |
| 271 | ); |