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 { Bucket } from "@alchemy.run/aws/S3"; |
| 3 | import { AssetDeployment } from "@alchemy.run/aws/Website/AssetDeployment"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as s3 from "@distilled.cloud/aws/s3"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Path from "effect/Path"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import { driftOf, repairDrift, waitForDrift } from "../drift.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: AWS.providers() }); |
| 15 | |
| 16 | test.provider( |
| 17 | "list returns [] for the non-listable AssetDeployment", |
| 18 | () => |
| 19 | Effect.gen(function* () { |
| 20 | const provider = yield* Provider.findProvider(AssetDeployment); |
| 21 | const all = yield* provider.list(); |
| 22 | expect(all).toEqual([]); |
| 23 | }), |
| 24 | { tags: ["provider:aws", "provider:aws:website", "live"] }, |
| 25 | ); |
| 26 | |
| 27 | test.provider( |
| 28 | "create, update, and delete a purged asset deployment", |
| 29 | (stack) => |
| 30 | Effect.gen(function* () { |
| 31 | const path = yield* Path.Path; |
| 32 | const v1 = path.join(import.meta.dirname, "fixtures", "asset-deployment-v1"); |
| 33 | const v2 = path.join(import.meta.dirname, "fixtures", "asset-deployment-v2"); |
| 34 | |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | const deployed = yield* stack.deploy( |
| 38 | Effect.gen(function* () { |
| 39 | const bucket = yield* Bucket("AssetBucket", { |
| 40 | forceDestroy: true, |
| 41 | }); |
| 42 | const files = yield* AssetDeployment("WebsiteFiles", { |
| 43 | bucket, |
| 44 | sourcePath: v1, |
| 45 | prefix: "site", |
| 46 | purge: true, |
| 47 | }); |
| 48 | return { bucket, files }; |
| 49 | }), |
| 50 | ); |
| 51 | |
| 52 | expect(deployed.files.bucketName).toBe(deployed.bucket.bucketName); |
| 53 | expect(deployed.files.prefix).toBe("site"); |
| 54 | expect(deployed.files.fileCount).toBe(3); |
| 55 | expect(deployed.files.files).toEqual(["about.html", "index.html", "robots.txt"]); |
| 56 | |
| 57 | yield* assertObjectKeys(deployed.bucket.bucketName, [ |
| 58 | "site/about.html", |
| 59 | "site/index.html", |
| 60 | "site/robots.txt", |
| 61 | ]); |
| 62 | |
| 63 | const updated = yield* stack.deploy( |
| 64 | Effect.gen(function* () { |
| 65 | const bucket = yield* Bucket("AssetBucket", { |
| 66 | forceDestroy: true, |
| 67 | }); |
| 68 | const files = yield* AssetDeployment("WebsiteFiles", { |
| 69 | bucket, |
| 70 | sourcePath: v2, |
| 71 | prefix: "site", |
| 72 | purge: true, |
| 73 | }); |
| 74 | return { bucket, files }; |
| 75 | }), |
| 76 | ); |
| 77 | |
| 78 | expect(updated.bucket.bucketName).toBe(deployed.bucket.bucketName); |
| 79 | expect(updated.files.fileCount).toBe(3); |
| 80 | expect(updated.files.files).toEqual(["extra.css", "index.html", "robots.txt"]); |
| 81 | expect(updated.files.version).not.toBe(deployed.files.version); |
| 82 | |
| 83 | yield* assertObjectKeys(updated.bucket.bucketName, [ |
| 84 | "site/extra.css", |
| 85 | "site/index.html", |
| 86 | "site/robots.txt", |
| 87 | ]); |
| 88 | |
| 89 | const bucketName = deployed.bucket.bucketName; |
| 90 | yield* stack.destroy(); |
| 91 | yield* assertBucketDeleted(bucketName); |
| 92 | }), |
| 93 | { |
| 94 | tags: ["provider:aws", "provider:aws:s3", "provider:aws:website", "live"], |
| 95 | timeout: 120_000, |
| 96 | }, |
| 97 | ); |
| 98 | |
| 99 | const listObjectKeys = (bucketName: string) => |
| 100 | Effect.gen(function* () { |
| 101 | const listed = yield* s3.listObjectsV2({ Bucket: bucketName }); |
| 102 | return (listed.Contents ?? []) |
| 103 | .flatMap((object) => (object.Key !== undefined ? [object.Key] : [])) |
| 104 | .sort(); |
| 105 | }); |
| 106 | |
| 107 | const keysMatch = (got: string[], expected: string[]) => |
| 108 | got.length === expected.length && expected.every((key, index) => got[index] === key); |
| 109 | |
| 110 | const assertObjectKeys = (bucketName: string, expected: string[]) => |
| 111 | Effect.gen(function* () { |
| 112 | const keys = yield* listObjectKeys(bucketName).pipe( |
| 113 | Effect.repeat({ |
| 114 | schedule: Schedule.spaced("200 millis"), |
| 115 | until: (got) => keysMatch(got, expected), |
| 116 | times: 15, |
| 117 | }), |
| 118 | ); |
| 119 | expect(keys).toEqual(expected); |
| 120 | }); |
| 121 | |
| 122 | class BucketStillExists extends Data.TaggedError("BucketStillExists") {} |
| 123 | |
| 124 | const assertBucketDeleted = (bucketName: string) => |
| 125 | s3.headBucket({ Bucket: bucketName }).pipe( |
| 126 | Effect.flatMap(() => Effect.fail(new BucketStillExists())), |
| 127 | Effect.retry({ |
| 128 | while: (e) => e._tag === "BucketStillExists", |
| 129 | schedule: Schedule.max([Schedule.exponential(100), Schedule.recurs(10)]), |
| 130 | }), |
| 131 | Effect.catchTag("NotFound", () => Effect.void), |
| 132 | ); |
| 133 | |
| 134 | // #2688: `read` must observe the uploaded objects, or an edited or deleted |
| 135 | // file reads as `unchanged`. |
| 136 | test.provider( |
| 137 | "drift detects and repairs edited, deleted and stray objects", |
| 138 | (stack) => |
| 139 | Effect.gen(function* () { |
| 140 | const path = yield* Path.Path; |
| 141 | const v1 = path.join(import.meta.dirname, "fixtures", "asset-deployment-v1"); |
| 142 | |
| 143 | yield* stack.destroy(); |
| 144 | |
| 145 | const { bucket } = yield* stack.deploy( |
| 146 | Effect.gen(function* () { |
| 147 | const bucket = yield* Bucket("DriftAssetBucket", { forceDestroy: true }); |
| 148 | const files = yield* AssetDeployment("DriftFiles", { |
| 149 | bucket, |
| 150 | sourcePath: v1, |
| 151 | prefix: "site", |
| 152 | purge: true, |
| 153 | }); |
| 154 | return { bucket, files }; |
| 155 | }), |
| 156 | ); |
| 157 | expect((yield* driftOf(stack, "DriftFiles"))?.action).toBe("unchanged"); |
| 158 | |
| 159 | yield* s3.putObject({ |
| 160 | Bucket: bucket.bucketName, |
| 161 | Key: "site/index.html", |
| 162 | Body: "<h1>out of band</h1>", |
| 163 | }); |
| 164 | yield* s3.deleteObject({ Bucket: bucket.bucketName, Key: "site/robots.txt" }); |
| 165 | yield* s3.putObject({ Bucket: bucket.bucketName, Key: "site/stray.txt", Body: "stray" }); |
| 166 | |
| 167 | const drifted = yield* waitForDrift(stack, "DriftFiles", "drifted"); |
| 168 | expect(drifted?.attr?.objectHashes?.["robots.txt"]).toBeUndefined(); |
| 169 | expect(drifted?.attr?.strayKeys).toEqual(["site/stray.txt"]); |
| 170 | |
| 171 | yield* repairDrift(stack); |
| 172 | expect((yield* driftOf(stack, "DriftFiles"))?.action).toBe("unchanged"); |
| 173 | yield* assertObjectKeys(bucket.bucketName, [ |
| 174 | "site/about.html", |
| 175 | "site/index.html", |
| 176 | "site/robots.txt", |
| 177 | ]); |
| 178 | |
| 179 | yield* stack.destroy(); |
| 180 | }), |
| 181 | { tags: ["provider:aws", "provider:aws:s3", "provider:aws:website", "live"], timeout: 180_000 }, |
| 182 | ); |