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 { RegistryPolicy } from "@alchemy.run/aws/ECR"; |
| 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 { Region } from "@distilled.cloud/aws/Region"; |
| 9 | import * as sts from "@distilled.cloud/aws/sts"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: AWS.providers() }); |
| 13 | |
| 14 | const readRegistryPolicy = ecr.getRegistryPolicy({}).pipe( |
| 15 | Effect.map((response) => response.policyText), |
| 16 | Effect.catchTag("RegistryPolicyNotFoundException", () => Effect.succeed(undefined)), |
| 17 | ); |
| 18 | |
| 19 | // The registry policy is an account/region SINGLETON — capture any |
| 20 | // pre-existing policy up front and restore it after the test so a shared |
| 21 | // testing account is left exactly as we found it. |
| 22 | test.provider( |
| 23 | "PolicyDocument-valued registry policy deploys and re-deploys clean", |
| 24 | (stack) => |
| 25 | Effect.gen(function* () { |
| 26 | const prior = yield* readRegistryPolicy; |
| 27 | |
| 28 | const identity = yield* sts.getCallerIdentity({}); |
| 29 | const accountId = identity.Account!; |
| 30 | const region = yield* yield* Region; |
| 31 | |
| 32 | const registryPolicy: PolicyDocument = { |
| 33 | Version: "2012-10-17", |
| 34 | Statement: [ |
| 35 | { |
| 36 | Sid: "AlchemyTestReplication", |
| 37 | Effect: "Allow", |
| 38 | Principal: { AWS: `arn:aws:iam::${accountId}:root` }, |
| 39 | Action: ["ecr:ReplicateImage"], |
| 40 | Resource: `arn:aws:ecr:${region}:${accountId}:repository/*`, |
| 41 | }, |
| 42 | ], |
| 43 | }; |
| 44 | |
| 45 | const run = Effect.gen(function* () { |
| 46 | yield* stack.destroy(); |
| 47 | |
| 48 | const deployPolicy = () => |
| 49 | stack.deploy( |
| 50 | Effect.gen(function* () { |
| 51 | return yield* RegistryPolicy("TestRegistryPolicy", { |
| 52 | policy: registryPolicy, |
| 53 | }); |
| 54 | }), |
| 55 | ); |
| 56 | |
| 57 | const deployed = yield* deployPolicy(); |
| 58 | expect(deployed.registryId).toBe(accountId); |
| 59 | |
| 60 | // Out-of-band: the applied policy is equivalent to the structured |
| 61 | // document (normalized comparison). |
| 62 | const observed = yield* readRegistryPolicy; |
| 63 | expect(observed).toBeDefined(); |
| 64 | expect(normalizePolicyDocument(observed!)).toBe(normalizePolicyDocument(registryPolicy)); |
| 65 | |
| 66 | // Re-deploy the identical PolicyDocument — must converge cleanly |
| 67 | // (normalized observed vs desired no-ops the put). |
| 68 | const again = yield* deployPolicy(); |
| 69 | expect(again.registryId).toBe(accountId); |
| 70 | const observedAgain = yield* readRegistryPolicy; |
| 71 | expect(normalizePolicyDocument(observedAgain!)).toBe( |
| 72 | normalizePolicyDocument(registryPolicy), |
| 73 | ); |
| 74 | |
| 75 | yield* stack.destroy(); |
| 76 | expect(yield* readRegistryPolicy).toBeUndefined(); |
| 77 | }); |
| 78 | |
| 79 | // Restore whatever registry policy existed before the test ran, |
| 80 | // whether the body succeeded or failed. |
| 81 | yield* run.pipe( |
| 82 | Effect.ensuring( |
| 83 | prior === undefined |
| 84 | ? Effect.void |
| 85 | : ecr.putRegistryPolicy({ policyText: prior }).pipe(Effect.ignore), |
| 86 | ), |
| 87 | ); |
| 88 | }), |
| 89 | { |
| 90 | tags: ["provider:aws", "provider:aws:ecr", "provider:aws:iam", "live"], |
| 91 | timeout: 120_000, |
| 92 | }, |
| 93 | ); |