The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { validateFunctionPackageProps } from "@alchemy.run/aws/Lambda/Function"; |
| 2 | import { |
| 3 | decodeFunctionImageSource, |
| 4 | functionImagePlatform, |
| 5 | hashFunctionImageBuild, |
| 6 | parseFunctionImageUri, |
| 7 | } from "@alchemy.run/aws/Lambda/FunctionImage"; |
| 8 | import { expect, layer } from "@alchemy.run/test"; |
| 9 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as FileSystem from "effect/FileSystem"; |
| 12 | import * as Path from "effect/Path"; |
| 13 | import * as Result from "effect/Result"; |
| 14 | |
| 15 | const describe = layer(NodeServices.layer); |
| 16 | |
| 17 | describe("Lambda Function images", (it) => { |
| 18 | it.effect( |
| 19 | "maps Lambda architectures to Docker platforms", |
| 20 | () => |
| 21 | Effect.sync(() => { |
| 22 | expect(functionImagePlatform("x86_64")).toBe("linux/amd64"); |
| 23 | expect(functionImagePlatform("arm64")).toBe("linux/arm64"); |
| 24 | }), |
| 25 | { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] }, |
| 26 | ); |
| 27 | |
| 28 | it.effect( |
| 29 | "parses tagged and digest-pinned private ECR image URIs", |
| 30 | () => |
| 31 | Effect.gen(function* () { |
| 32 | const tagged = yield* parseFunctionImageUri( |
| 33 | "Tagged", |
| 34 | "123456789012.dkr.ecr.eu-west-3.amazonaws.com/team/worker:release", |
| 35 | ); |
| 36 | expect(tagged).toMatchObject({ |
| 37 | registryId: "123456789012", |
| 38 | region: "eu-west-3", |
| 39 | repositoryName: "team/worker", |
| 40 | imageId: { imageTag: "release" }, |
| 41 | }); |
| 42 | |
| 43 | const digest = `sha256:${"a".repeat(64)}`; |
| 44 | const pinned = yield* parseFunctionImageUri( |
| 45 | "Pinned", |
| 46 | `123456789012.dkr.ecr.us-east-1.amazonaws.com/worker@${digest}`, |
| 47 | ); |
| 48 | expect(pinned.imageId).toEqual({ imageDigest: digest }); |
| 49 | expect(pinned.repositoryUri).toBe("123456789012.dkr.ecr.us-east-1.amazonaws.com/worker"); |
| 50 | }), |
| 51 | { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] }, |
| 52 | ); |
| 53 | |
| 54 | it.effect( |
| 55 | "rejects unsupported or incomplete ECR image URIs", |
| 56 | () => |
| 57 | Effect.gen(function* () { |
| 58 | const fips = yield* Effect.result( |
| 59 | parseFunctionImageUri( |
| 60 | "Fips", |
| 61 | "123456789012.dkr.ecr-fips.us-east-1.amazonaws.com/worker:latest", |
| 62 | ), |
| 63 | ); |
| 64 | expect(Result.isFailure(fips)).toBe(true); |
| 65 | if (Result.isFailure(fips)) { |
| 66 | expect(fips.failure.message).toContain("do not support ECR FIPS"); |
| 67 | } |
| 68 | |
| 69 | const untagged = yield* Effect.result( |
| 70 | parseFunctionImageUri("Untagged", "123456789012.dkr.ecr.us-east-1.amazonaws.com/worker"), |
| 71 | ); |
| 72 | expect(Result.isFailure(untagged)).toBe(true); |
| 73 | if (Result.isFailure(untagged)) { |
| 74 | expect(untagged.failure.message).toContain("explicit ECR tag or digest"); |
| 75 | } |
| 76 | }), |
| 77 | { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] }, |
| 78 | ); |
| 79 | |
| 80 | it.effect( |
| 81 | "rejects mixed image sources and ZIP-only image options", |
| 82 | () => |
| 83 | Effect.gen(function* () { |
| 84 | const mixedSource = yield* Effect.result( |
| 85 | decodeFunctionImageSource("MixedSource", { |
| 86 | uri: "123456789012.dkr.ecr.us-east-1.amazonaws.com/worker:latest", |
| 87 | context: "./lambda", |
| 88 | dockerfile: "Dockerfile", |
| 89 | }), |
| 90 | ); |
| 91 | expect(Result.isFailure(mixedSource)).toBe(true); |
| 92 | if (Result.isFailure(mixedSource)) { |
| 93 | expect(mixedSource.failure.message).toContain("exactly one image source"); |
| 94 | } |
| 95 | |
| 96 | const mixedPackage = yield* Effect.result( |
| 97 | validateFunctionPackageProps("MixedPackage", { |
| 98 | image: { uri: "example" }, |
| 99 | main: "./handler.ts", |
| 100 | runtime: "nodejs22.x", |
| 101 | layers: ["arn:aws:lambda:us-east-1:123456789012:layer:example:1"], |
| 102 | }), |
| 103 | ); |
| 104 | expect(Result.isFailure(mixedPackage)).toBe(true); |
| 105 | if (Result.isFailure(mixedPackage)) { |
| 106 | expect(mixedPackage.failure.message).toContain("main, runtime, layers"); |
| 107 | } |
| 108 | }), |
| 109 | { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] }, |
| 110 | ); |
| 111 | |
| 112 | it.effect( |
| 113 | "does not hash files excluded by .dockerignore", |
| 114 | () => |
| 115 | Effect.gen(function* () { |
| 116 | const fs = yield* FileSystem.FileSystem; |
| 117 | const path = yield* Path.Path; |
| 118 | const context = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-lambda-image-hash-" }); |
| 119 | yield* fs.writeFileString(path.join(context, "Dockerfile"), "FROM scratch\nCOPY . /app\n"); |
| 120 | yield* fs.makeDirectory(path.join(context, "ignored"), { recursive: true }); |
| 121 | yield* fs.writeFileString( |
| 122 | path.join(context, ".dockerignore"), |
| 123 | "ignored/**\n!ignored/included.txt\n", |
| 124 | ); |
| 125 | yield* fs.writeFileString(path.join(context, "ignored", "excluded.txt"), "one"); |
| 126 | yield* fs.writeFileString(path.join(context, "ignored", "included.txt"), "one"); |
| 127 | |
| 128 | const source = { context, dockerfile: "Dockerfile" }; |
| 129 | const initial = yield* hashFunctionImageBuild(source, "x86_64"); |
| 130 | yield* fs.writeFileString(path.join(context, "ignored", "excluded.txt"), "two"); |
| 131 | expect(yield* hashFunctionImageBuild(source, "x86_64")).toBe(initial); |
| 132 | |
| 133 | yield* fs.writeFileString(path.join(context, "ignored", "included.txt"), "two"); |
| 134 | expect(yield* hashFunctionImageBuild(source, "x86_64")).not.toBe(initial); |
| 135 | }), |
| 136 | { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] }, |
| 137 | ); |
| 138 | |
| 139 | it.effect( |
| 140 | "preserves escaped Docker ignore wildcards", |
| 141 | () => |
| 142 | Effect.gen(function* () { |
| 143 | const fs = yield* FileSystem.FileSystem; |
| 144 | const path = yield* Path.Path; |
| 145 | const context = yield* fs.makeTempDirectoryScoped({ |
| 146 | prefix: "alchemy-lambda-image-ignore-escape-", |
| 147 | }); |
| 148 | yield* fs.writeFileString(path.join(context, "Dockerfile"), "FROM scratch\nCOPY . /app\n"); |
| 149 | yield* fs.writeFileString(path.join(context, ".dockerignore"), "file\\?.txt\n"); |
| 150 | yield* fs.writeFileString(path.join(context, "file?.txt"), "one"); |
| 151 | yield* fs.makeDirectory(path.join(context, "file")); |
| 152 | yield* fs.writeFileString(path.join(context, "file", "a.txt"), "one"); |
| 153 | |
| 154 | const source = { context, dockerfile: "Dockerfile" }; |
| 155 | const initial = yield* hashFunctionImageBuild(source, "x86_64"); |
| 156 | yield* fs.writeFileString(path.join(context, "file?.txt"), "two"); |
| 157 | expect(yield* hashFunctionImageBuild(source, "x86_64")).toBe(initial); |
| 158 | |
| 159 | yield* fs.writeFileString(path.join(context, "file", "a.txt"), "two"); |
| 160 | expect(yield* hashFunctionImageBuild(source, "x86_64")).not.toBe(initial); |
| 161 | }), |
| 162 | { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] }, |
| 163 | ); |
| 164 | |
| 165 | it.effect( |
| 166 | "hashes Dockerfile, build args, architecture, and relative context contents", |
| 167 | () => |
| 168 | Effect.gen(function* () { |
| 169 | const fs = yield* FileSystem.FileSystem; |
| 170 | const path = yield* Path.Path; |
| 171 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-lambda-image-inputs-" }); |
| 172 | const first = path.join(root, "first"); |
| 173 | const second = path.join(root, "second"); |
| 174 | yield* fs.makeDirectory(first, { recursive: true }); |
| 175 | yield* fs.makeDirectory(second, { recursive: true }); |
| 176 | |
| 177 | for (const context of [first, second]) { |
| 178 | yield* fs.writeFileString( |
| 179 | path.join(context, "Dockerfile"), |
| 180 | "FROM scratch\nCOPY app.txt /app.txt\n", |
| 181 | ); |
| 182 | yield* fs.writeFileString(path.join(context, "app.txt"), "hello"); |
| 183 | } |
| 184 | |
| 185 | const source = { |
| 186 | context: first, |
| 187 | dockerfile: "Dockerfile", |
| 188 | buildArgs: { B: "two", A: "one" }, |
| 189 | }; |
| 190 | const initial = yield* hashFunctionImageBuild(source, "x86_64"); |
| 191 | expect( |
| 192 | yield* hashFunctionImageBuild( |
| 193 | { context: second, dockerfile: "Dockerfile", buildArgs: { A: "one", B: "two" } }, |
| 194 | "x86_64", |
| 195 | ), |
| 196 | ).toBe(initial); |
| 197 | expect( |
| 198 | yield* hashFunctionImageBuild( |
| 199 | { ...source, buildArgs: { A: "changed", B: "two" } }, |
| 200 | "x86_64", |
| 201 | ), |
| 202 | ).not.toBe(initial); |
| 203 | expect(yield* hashFunctionImageBuild(source, "arm64")).not.toBe(initial); |
| 204 | |
| 205 | yield* fs.writeFileString( |
| 206 | path.join(first, "Dockerfile"), |
| 207 | "FROM scratch\nCOPY app.txt /renamed.txt\n", |
| 208 | ); |
| 209 | expect(yield* hashFunctionImageBuild(source, "x86_64")).not.toBe(initial); |
| 210 | }), |
| 211 | { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] }, |
| 212 | ); |
| 213 | |
| 214 | it.effect( |
| 215 | "hashes copied filesystem metadata", |
| 216 | () => |
| 217 | Effect.gen(function* () { |
| 218 | const fs = yield* FileSystem.FileSystem; |
| 219 | const path = yield* Path.Path; |
| 220 | const context = yield* fs.makeTempDirectoryScoped({ |
| 221 | prefix: "alchemy-lambda-image-metadata-", |
| 222 | }); |
| 223 | const bootstrap = path.join(context, "bootstrap"); |
| 224 | yield* fs.writeFileString(path.join(context, "Dockerfile"), "FROM scratch\nCOPY . /app\n"); |
| 225 | yield* fs.writeFileString(bootstrap, "#!/bin/sh\n"); |
| 226 | yield* fs.chmod(bootstrap, 0o644); |
| 227 | |
| 228 | const source = { context, dockerfile: "Dockerfile" }; |
| 229 | const initial = yield* hashFunctionImageBuild(source, "x86_64"); |
| 230 | |
| 231 | yield* fs.chmod(bootstrap, 0o755); |
| 232 | const executable = yield* hashFunctionImageBuild(source, "x86_64"); |
| 233 | expect(executable).not.toBe(initial); |
| 234 | |
| 235 | yield* fs.makeDirectory(path.join(context, "empty")); |
| 236 | expect(yield* hashFunctionImageBuild(source, "x86_64")).not.toBe(executable); |
| 237 | }), |
| 238 | { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] }, |
| 239 | ); |
| 240 | |
| 241 | it.effect( |
| 242 | "hashes symbolic link targets without following them", |
| 243 | () => |
| 244 | Effect.gen(function* () { |
| 245 | const fs = yield* FileSystem.FileSystem; |
| 246 | const path = yield* Path.Path; |
| 247 | const context = yield* fs.makeTempDirectoryScoped({ |
| 248 | prefix: "alchemy-lambda-image-symlink-", |
| 249 | }); |
| 250 | yield* fs.writeFileString(path.join(context, "Dockerfile"), "FROM scratch\nCOPY . /app\n"); |
| 251 | yield* fs.writeFileString(path.join(context, "target-a"), "same"); |
| 252 | yield* fs.writeFileString(path.join(context, "target-b"), "same"); |
| 253 | const link = path.join(context, "current"); |
| 254 | yield* fs.symlink("target-a", link); |
| 255 | |
| 256 | const source = { context, dockerfile: "Dockerfile" }; |
| 257 | const initial = yield* hashFunctionImageBuild(source, "x86_64"); |
| 258 | |
| 259 | yield* fs.remove(link); |
| 260 | yield* fs.symlink("target-b", link); |
| 261 | expect(yield* hashFunctionImageBuild(source, "x86_64")).not.toBe(initial); |
| 262 | }), |
| 263 | { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] }, |
| 264 | ); |
| 265 | |
| 266 | it.effect( |
| 267 | "uses a Dockerfile-specific ignore file instead of the context root", |
| 268 | () => |
| 269 | Effect.gen(function* () { |
| 270 | const fs = yield* FileSystem.FileSystem; |
| 271 | const path = yield* Path.Path; |
| 272 | const context = yield* fs.makeTempDirectoryScoped({ |
| 273 | prefix: "alchemy-lambda-image-ignore-", |
| 274 | }); |
| 275 | yield* fs.writeFileString( |
| 276 | path.join(context, "Lambda.Dockerfile"), |
| 277 | "FROM scratch\nCOPY . /app\n", |
| 278 | ); |
| 279 | yield* fs.writeFileString(path.join(context, ".dockerignore"), "root-only.txt\n"); |
| 280 | yield* fs.writeFileString( |
| 281 | path.join(context, "Lambda.Dockerfile.dockerignore"), |
| 282 | "specific-only.txt\n", |
| 283 | ); |
| 284 | yield* fs.writeFileString(path.join(context, "specific-only.txt"), "one"); |
| 285 | yield* fs.writeFileString(path.join(context, "root-only.txt"), "one"); |
| 286 | |
| 287 | const source = { context, dockerfile: "Lambda.Dockerfile" }; |
| 288 | const initial = yield* hashFunctionImageBuild(source, "x86_64"); |
| 289 | yield* fs.writeFileString(path.join(context, "specific-only.txt"), "two"); |
| 290 | expect(yield* hashFunctionImageBuild(source, "x86_64")).toBe(initial); |
| 291 | |
| 292 | yield* fs.writeFileString(path.join(context, "root-only.txt"), "two"); |
| 293 | expect(yield* hashFunctionImageBuild(source, "x86_64")).not.toBe(initial); |
| 294 | }), |
| 295 | { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] }, |
| 296 | ); |
| 297 | |
| 298 | it.effect( |
| 299 | "does not alias an external Docker ignore file into the context", |
| 300 | () => |
| 301 | Effect.gen(function* () { |
| 302 | const fs = yield* FileSystem.FileSystem; |
| 303 | const path = yield* Path.Path; |
| 304 | const root = yield* fs.makeTempDirectoryScoped({ |
| 305 | prefix: "alchemy-lambda-image-external-ignore-", |
| 306 | }); |
| 307 | const context = path.join(root, "context"); |
| 308 | const dockerfile = path.join(root, "Lambda.Dockerfile"); |
| 309 | yield* fs.makeDirectory(context); |
| 310 | yield* fs.writeFileString(dockerfile, "FROM scratch\nCOPY . /app\n"); |
| 311 | yield* fs.writeFileString(`${dockerfile}.dockerignore`, "ignored.txt\n"); |
| 312 | yield* fs.writeFileString(path.join(context, "ignored.txt"), "one"); |
| 313 | yield* fs.writeFileString(path.join(context, "Lambda.Dockerfile.dockerignore"), "one"); |
| 314 | |
| 315 | const source = { context, dockerfile }; |
| 316 | const initial = yield* hashFunctionImageBuild(source, "x86_64"); |
| 317 | yield* fs.writeFileString(path.join(context, "ignored.txt"), "two"); |
| 318 | expect(yield* hashFunctionImageBuild(source, "x86_64")).toBe(initial); |
| 319 | |
| 320 | yield* fs.writeFileString(path.join(context, "Lambda.Dockerfile.dockerignore"), "two"); |
| 321 | expect(yield* hashFunctionImageBuild(source, "x86_64")).not.toBe(initial); |
| 322 | }), |
| 323 | { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] }, |
| 324 | ); |
| 325 | |
| 326 | it.effect( |
| 327 | "requires an explicit Dockerfile for local image sources", |
| 328 | () => |
| 329 | Effect.gen(function* () { |
| 330 | const fs = yield* FileSystem.FileSystem; |
| 331 | const context = yield* fs.makeTempDirectoryScoped({ |
| 332 | prefix: "alchemy-lambda-image-schema-", |
| 333 | }); |
| 334 | const result = yield* Effect.result( |
| 335 | decodeFunctionImageSource("MissingDockerfile", { context }), |
| 336 | ); |
| 337 | expect(Result.isFailure(result)).toBe(true); |
| 338 | }), |
| 339 | { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] }, |
| 340 | ); |
| 341 | }); |