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 { Repository } from "@alchemy.run/aws/ECR/Repository"; |
| 3 | import type { PolicyDocument } from "@alchemy.run/aws/IAM/Policy"; |
| 4 | import { normalizePolicyDocument } from "@alchemy.run/aws/IAM/Policy"; |
| 5 | import { expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as ecr from "@distilled.cloud/aws/ecr"; |
| 8 | import * as Provider from "alchemy/Provider"; |
| 9 | import * as Data from "effect/Data"; |
| 10 | import * as Effect from "effect/Effect"; |
| 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 | class RepositoryStillExists extends Data.TaggedError("RepositoryStillExists") {} |
| 17 | |
| 18 | // Out-of-band proof that the trailing destroy actually removed the |
| 19 | // repository: describeRepositories must settle on the typed |
| 20 | // RepositoryNotFoundException. |
| 21 | const assertRepositoryDeleted = Effect.fn(function* (repositoryName: string) { |
| 22 | yield* ecr.describeRepositories({ repositoryNames: [repositoryName] }).pipe( |
| 23 | Effect.flatMap(() => Effect.fail(new RepositoryStillExists())), |
| 24 | Effect.retry({ |
| 25 | while: (e) => e._tag === "RepositoryStillExists", |
| 26 | schedule: Schedule.max([Schedule.exponential(250), Schedule.recurs(8)]), |
| 27 | }), |
| 28 | Effect.catchTag("RepositoryNotFoundException", () => Effect.void), |
| 29 | ); |
| 30 | }); |
| 31 | |
| 32 | // Canonical `list()` test (AWS account/region-scoped collection): deploy a real |
| 33 | // repository, resolve the provider from context via the typed `findProvider`, |
| 34 | // call `list()`, and assert the deployed repository appears in the |
| 35 | // exhaustively-paginated result. |
| 36 | test.provider( |
| 37 | "list enumerates the deployed repository", |
| 38 | (stack) => |
| 39 | Effect.gen(function* () { |
| 40 | yield* stack.destroy(); |
| 41 | |
| 42 | const repo = yield* stack.deploy( |
| 43 | Effect.gen(function* () { |
| 44 | return yield* Repository("ListRepository", { |
| 45 | repositoryName: "alchemy-test-ecr-repo-list", |
| 46 | }); |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | const provider = yield* Provider.findProvider(Repository); |
| 51 | const all = yield* provider.list(); |
| 52 | |
| 53 | expect(all.some((r) => r.repositoryName === repo.repositoryName)).toBe(true); |
| 54 | |
| 55 | yield* stack.destroy(); |
| 56 | yield* assertRepositoryDeleted(repo.repositoryName); |
| 57 | }), |
| 58 | { tags: ["provider:aws", "provider:aws:ecr", "live"] }, |
| 59 | ); |
| 60 | |
| 61 | test.provider( |
| 62 | "reconciles tag mutability and scan-on-push on an existing repository", |
| 63 | (stack) => |
| 64 | Effect.gen(function* () { |
| 65 | yield* stack.destroy(); |
| 66 | |
| 67 | const deployRepository = (imageTagMutability: "MUTABLE" | "IMMUTABLE", scanOnPush: boolean) => |
| 68 | stack.deploy( |
| 69 | Repository("SettingsRepository", { |
| 70 | repositoryName: "alchemy-test-ecr-repo-settings", |
| 71 | imageTagMutability, |
| 72 | scanOnPush, |
| 73 | }), |
| 74 | ); |
| 75 | |
| 76 | yield* deployRepository("MUTABLE", false); |
| 77 | const updated = yield* deployRepository("IMMUTABLE", true); |
| 78 | |
| 79 | const described = yield* ecr.describeRepositories({ |
| 80 | repositoryNames: [updated.repositoryName], |
| 81 | }); |
| 82 | const observed = described.repositories?.[0]; |
| 83 | expect(observed?.imageTagMutability).toBe("IMMUTABLE"); |
| 84 | expect(observed?.imageScanningConfiguration?.scanOnPush).toBe(true); |
| 85 | expect(updated.imageTagMutability).toBe("IMMUTABLE"); |
| 86 | expect(updated.scanOnPush).toBe(true); |
| 87 | |
| 88 | yield* stack.destroy(); |
| 89 | yield* assertRepositoryDeleted(updated.repositoryName); |
| 90 | }), |
| 91 | { tags: ["provider:aws", "provider:aws:ecr", "live"], timeout: 120_000 }, |
| 92 | ); |
| 93 | |
| 94 | const repositoryPolicy: PolicyDocument = { |
| 95 | Version: "2012-10-17", |
| 96 | Statement: [ |
| 97 | { |
| 98 | Sid: "LambdaECRImageRetrieval", |
| 99 | Effect: "Allow", |
| 100 | Principal: { Service: "lambda.amazonaws.com" }, |
| 101 | Action: ["ecr:BatchGetImage", "ecr:GetDownloadUrlForLayer"], |
| 102 | }, |
| 103 | ], |
| 104 | }; |
| 105 | |
| 106 | const readRepositoryPolicy = (repositoryName: string) => |
| 107 | ecr.getRepositoryPolicy({ repositoryName }).pipe( |
| 108 | Effect.map((response) => response.policyText), |
| 109 | Effect.catchTag("RepositoryPolicyNotFoundException", () => Effect.succeed(undefined)), |
| 110 | ); |
| 111 | |
| 112 | test.provider( |
| 113 | "PolicyDocument-valued policy deploys and re-deploys clean", |
| 114 | (stack) => |
| 115 | Effect.gen(function* () { |
| 116 | yield* stack.destroy(); |
| 117 | |
| 118 | const deployWithPolicy = () => |
| 119 | stack.deploy( |
| 120 | Effect.gen(function* () { |
| 121 | return yield* Repository("PolicyRepository", { |
| 122 | repositoryName: "alchemy-test-ecr-repo-policy", |
| 123 | policy: repositoryPolicy, |
| 124 | }); |
| 125 | }), |
| 126 | ); |
| 127 | |
| 128 | const repo = yield* deployWithPolicy(); |
| 129 | expect(repo.policy).toBeDefined(); |
| 130 | |
| 131 | // Out-of-band: the applied policy is equivalent to the structured |
| 132 | // document (normalized comparison — key order / whitespace agnostic). |
| 133 | const observed = yield* readRepositoryPolicy(repo.repositoryName); |
| 134 | expect(observed).toBeDefined(); |
| 135 | expect(normalizePolicyDocument(observed!)).toBe(normalizePolicyDocument(repositoryPolicy)); |
| 136 | |
| 137 | // Re-deploy the identical PolicyDocument — must converge cleanly (the |
| 138 | // provider diffs normalized observed vs desired and no-ops the set). |
| 139 | const again = yield* deployWithPolicy(); |
| 140 | expect(again.repositoryArn).toBe(repo.repositoryArn); |
| 141 | const observedAgain = yield* readRepositoryPolicy(repo.repositoryName); |
| 142 | expect(normalizePolicyDocument(observedAgain!)).toBe( |
| 143 | normalizePolicyDocument(repositoryPolicy), |
| 144 | ); |
| 145 | |
| 146 | // Removing the prop deletes the repository policy. |
| 147 | const removed = yield* stack.deploy( |
| 148 | Effect.gen(function* () { |
| 149 | return yield* Repository("PolicyRepository", { |
| 150 | repositoryName: "alchemy-test-ecr-repo-policy", |
| 151 | }); |
| 152 | }), |
| 153 | ); |
| 154 | expect(removed.policy).toBeUndefined(); |
| 155 | expect(yield* readRepositoryPolicy(repo.repositoryName)).toBeUndefined(); |
| 156 | |
| 157 | yield* stack.destroy(); |
| 158 | yield* assertRepositoryDeleted(repo.repositoryName); |
| 159 | }), |
| 160 | { tags: ["provider:aws", "provider:aws:ecr", "provider:aws:iam", "live"], timeout: 120_000 }, |
| 161 | ); |
| 162 | |
| 163 | // #2688: `read` must observe the live lifecycle policy and tags, not carry |
| 164 | // the persisted ones. |
| 165 | test.provider( |
| 166 | "drift detects and repairs out-of-band lifecycle policy and tags", |
| 167 | (stack) => |
| 168 | Effect.gen(function* () { |
| 169 | yield* stack.destroy(); |
| 170 | |
| 171 | const policy = (count: number) => |
| 172 | JSON.stringify({ |
| 173 | rules: [ |
| 174 | { |
| 175 | rulePriority: 1, |
| 176 | selection: { tagStatus: "any", countType: "imageCountMoreThan", countNumber: count }, |
| 177 | action: { type: "expire" }, |
| 178 | }, |
| 179 | ], |
| 180 | }); |
| 181 | const repo = yield* stack.deploy( |
| 182 | Repository("DriftRepo", { |
| 183 | lifecyclePolicyText: policy(10), |
| 184 | tags: { team: "declared" }, |
| 185 | }), |
| 186 | ); |
| 187 | expect((yield* driftOf(stack, "DriftRepo"))?.action).toBe("unchanged"); |
| 188 | |
| 189 | yield* ecr.putLifecyclePolicy({ |
| 190 | repositoryName: repo.repositoryName, |
| 191 | lifecyclePolicyText: policy(99), |
| 192 | }); |
| 193 | yield* ecr.tagResource({ |
| 194 | resourceArn: repo.repositoryArn, |
| 195 | tags: [{ Key: "team", Value: "out-of-band" }], |
| 196 | }); |
| 197 | |
| 198 | const drifted = yield* waitForDrift(stack, "DriftRepo", "drifted"); |
| 199 | expect(drifted?.attr?.lifecyclePolicyText).toContain("99"); |
| 200 | expect(drifted?.attr?.tags?.team).toBe("out-of-band"); |
| 201 | |
| 202 | const repaired = yield* repairDrift(stack); |
| 203 | expect(repaired.resources.DriftRepo?.action).toBe("repaired"); |
| 204 | expect((yield* driftOf(stack, "DriftRepo"))?.action).toBe("unchanged"); |
| 205 | const lifecycle = yield* ecr.getLifecyclePolicy({ repositoryName: repo.repositoryName }); |
| 206 | expect(lifecycle.lifecyclePolicyText).toContain('"countNumber":10'); |
| 207 | |
| 208 | yield* stack.destroy(); |
| 209 | yield* assertRepositoryDeleted(repo.repositoryName); |
| 210 | }), |
| 211 | { tags: ["provider:aws", "provider:aws:ecr", "live"], timeout: 120_000 }, |
| 212 | ); |