The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { fileURLToPath } from "node:url"; |
| 2 | import * as AWS from "@alchemy.run/aws"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as Lambda from "@distilled.cloud/aws/lambda"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import { destroy } from "alchemy/RemovalPolicy"; |
| 8 | import { isResourceState, State, type ResourceState } from "alchemy/State"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | |
| 11 | const handlerV1Path = fileURLToPath(new URL("./fixtures/version-handler-v1.ts", import.meta.url)); |
| 12 | const handlerV2Path = fileURLToPath(new URL("./fixtures/version-handler-v2.ts", import.meta.url)); |
| 13 | |
| 14 | const { test } = Test.make({ providers: AWS.providers() }); |
| 15 | |
| 16 | test.provider( |
| 17 | "publish, recover, promote, retain, list, and explicitly delete versions", |
| 18 | (stack) => |
| 19 | Effect.gen(function* () { |
| 20 | yield* stack.destroy(); |
| 21 | |
| 22 | const program = ({ |
| 23 | handlerPath, |
| 24 | envVersion, |
| 25 | reservedConcurrentExecutions, |
| 26 | includeVersion = true, |
| 27 | includeAlias = true, |
| 28 | destroyVersion = false, |
| 29 | }: { |
| 30 | handlerPath: string; |
| 31 | envVersion: string; |
| 32 | reservedConcurrentExecutions?: number; |
| 33 | includeVersion?: boolean; |
| 34 | includeAlias?: boolean; |
| 35 | destroyVersion?: boolean; |
| 36 | }) => |
| 37 | Effect.gen(function* () { |
| 38 | const fn = yield* AWS.Lambda.Function("VersionedFn", { |
| 39 | main: handlerPath, |
| 40 | handler: "handler", |
| 41 | isExternal: true, |
| 42 | functionUrl: false, |
| 43 | env: { VERSION: envVersion }, |
| 44 | reservedConcurrentExecutions, |
| 45 | }); |
| 46 | |
| 47 | const version = includeVersion |
| 48 | ? yield* AWS.Lambda.Version("Release", { |
| 49 | function: fn, |
| 50 | }).pipe(destroy(destroyVersion)) |
| 51 | : undefined; |
| 52 | |
| 53 | const live = |
| 54 | includeAlias && version |
| 55 | ? yield* AWS.Lambda.Alias("Live", { |
| 56 | version, |
| 57 | aliasName: "live", |
| 58 | }) |
| 59 | : undefined; |
| 60 | |
| 61 | return { fn, version, live }; |
| 62 | }); |
| 63 | |
| 64 | // --- create --- |
| 65 | const created = yield* stack.deploy( |
| 66 | program({ handlerPath: handlerV1Path, envVersion: "one" }), |
| 67 | ); |
| 68 | const v1 = created.version!; |
| 69 | |
| 70 | expect(v1.version).toMatch(/^[1-9]\d*$/); |
| 71 | expect(v1.versionArn).toBe(`${v1.functionArn}:${v1.version}`); |
| 72 | expect(v1.codeSha256).toBeTruthy(); |
| 73 | expect(v1.configSha256).toBeTruthy(); |
| 74 | expect(v1.sourceHash).toMatch(/^[a-f0-9]{64}$/); |
| 75 | expect(created.live!.functionVersion).toBe(v1.version); |
| 76 | |
| 77 | const cloudV1 = yield* getVersionOrUndefined(v1.functionName, v1.version); |
| 78 | expect(cloudV1?.FunctionArn).toBe(v1.versionArn); |
| 79 | expect(cloudV1?.CodeSha256).toBe(v1.codeSha256); |
| 80 | const countV1 = (yield* numberedVersions(v1.functionName)).length; |
| 81 | |
| 82 | // --- noop --- |
| 83 | const unchanged = yield* stack.deploy( |
| 84 | program({ handlerPath: handlerV1Path, envVersion: "one" }), |
| 85 | ); |
| 86 | expect(unchanged.version!.version).toBe(v1.version); |
| 87 | expect((yield* numberedVersions(v1.functionName)).length).toBe(countV1); |
| 88 | |
| 89 | // --- code change + alias promotion --- |
| 90 | const codeChanged = yield* stack.deploy( |
| 91 | program({ handlerPath: handlerV2Path, envVersion: "one" }), |
| 92 | ); |
| 93 | const v2 = codeChanged.version!; |
| 94 | expect(v2.version).not.toBe(v1.version); |
| 95 | expect(v2.codeSha256).not.toBe(v1.codeSha256); |
| 96 | expect(codeChanged.live!.aliasArn).toBe(created.live!.aliasArn); |
| 97 | expect(codeChanged.live!.functionVersion).toBe(v2.version); |
| 98 | expect((yield* numberedVersions(v1.functionName)).length).toBe(countV1 + 1); |
| 99 | expect(yield* getVersionOrUndefined(v1.functionName, v1.version)).toBeDefined(); |
| 100 | |
| 101 | // --- versioned configuration change --- |
| 102 | const configChanged = yield* stack.deploy( |
| 103 | program({ handlerPath: handlerV2Path, envVersion: "two" }), |
| 104 | ); |
| 105 | const current = configChanged.version!; |
| 106 | expect(current.version).not.toBe(v2.version); |
| 107 | expect(current.codeSha256).toBe(v2.codeSha256); |
| 108 | expect(current.configSha256).not.toBe(v2.configSha256); |
| 109 | const countCurrent = (yield* numberedVersions(v1.functionName)).length; |
| 110 | expect(countCurrent).toBe(countV1 + 2); |
| 111 | |
| 112 | // --- operational-only change --- |
| 113 | const operational = yield* stack.deploy( |
| 114 | program({ |
| 115 | handlerPath: handlerV2Path, |
| 116 | envVersion: "two", |
| 117 | reservedConcurrentExecutions: 0, |
| 118 | }), |
| 119 | ); |
| 120 | expect(operational.version!.version).toBe(current.version); |
| 121 | expect((yield* numberedVersions(v1.functionName)).length).toBe(countCurrent); |
| 122 | |
| 123 | // --- crash after PublishVersion, before state write --- |
| 124 | // State resolves to an Effect that initializes and yields the concrete |
| 125 | // state-store service. |
| 126 | const state = yield* yield* State; |
| 127 | const stage = stack.stage; |
| 128 | const fqns = yield* state.list({ stack: stack.name, stage }); |
| 129 | const rows = yield* Effect.forEach(fqns, (fqn) => |
| 130 | state.get({ stack: stack.name, stage, fqn }).pipe(Effect.map((row) => ({ fqn, row }))), |
| 131 | ); |
| 132 | const versionRow = rows.find( |
| 133 | (row): row is { fqn: string; row: ResourceState } => |
| 134 | isResourceState(row.row) && row.row.resourceType === "AWS.Lambda.Version", |
| 135 | ); |
| 136 | if (!versionRow?.row.props) { |
| 137 | return yield* Effect.die( |
| 138 | new Error("no persisted AWS.Lambda.Version props found after deploy"), |
| 139 | ); |
| 140 | } |
| 141 | yield* state.set({ |
| 142 | stack: stack.name, |
| 143 | stage, |
| 144 | fqn: versionRow.fqn, |
| 145 | value: { |
| 146 | ...versionRow.row, |
| 147 | props: versionRow.row.props, |
| 148 | status: "creating", |
| 149 | attr: undefined, |
| 150 | }, |
| 151 | }); |
| 152 | |
| 153 | const recovered = yield* stack.deploy( |
| 154 | program({ |
| 155 | handlerPath: handlerV2Path, |
| 156 | envVersion: "two", |
| 157 | reservedConcurrentExecutions: 0, |
| 158 | }), |
| 159 | ); |
| 160 | expect(recovered.version!.version).toBe(current.version); |
| 161 | expect((yield* numberedVersions(v1.functionName)).length).toBe(countCurrent); |
| 162 | |
| 163 | // --- crash before the Function reference resolved --- |
| 164 | // An interrupted first create persists the Function reference as |
| 165 | // missing (it was still an unresolved Output at checkpoint time). The |
| 166 | // recovery read finds nothing to recover instead of crashing (#2002), |
| 167 | // and the re-driven create reuses the published version. |
| 168 | yield* state.set({ |
| 169 | stack: stack.name, |
| 170 | stage, |
| 171 | fqn: versionRow.fqn, |
| 172 | value: { |
| 173 | ...versionRow.row, |
| 174 | props: { ...versionRow.row.props, function: undefined }, |
| 175 | status: "creating", |
| 176 | attr: undefined, |
| 177 | }, |
| 178 | }); |
| 179 | const strippedRecovery = yield* stack.deploy( |
| 180 | program({ |
| 181 | handlerPath: handlerV2Path, |
| 182 | envVersion: "two", |
| 183 | reservedConcurrentExecutions: 0, |
| 184 | }), |
| 185 | ); |
| 186 | expect(strippedRecovery.version!.version).toBe(current.version); |
| 187 | expect((yield* numberedVersions(v1.functionName)).length).toBe(countCurrent); |
| 188 | |
| 189 | // --- retain on removal and recover on re-add --- |
| 190 | yield* stack.deploy( |
| 191 | program({ |
| 192 | handlerPath: handlerV2Path, |
| 193 | envVersion: "two", |
| 194 | reservedConcurrentExecutions: 0, |
| 195 | includeVersion: false, |
| 196 | includeAlias: false, |
| 197 | }), |
| 198 | ); |
| 199 | expect(yield* getVersionOrUndefined(v1.functionName, current.version)).toBeDefined(); |
| 200 | |
| 201 | const readded = yield* stack.deploy( |
| 202 | program({ |
| 203 | handlerPath: handlerV2Path, |
| 204 | envVersion: "two", |
| 205 | reservedConcurrentExecutions: 0, |
| 206 | }), |
| 207 | ); |
| 208 | expect(readded.version!.version).toBe(current.version); |
| 209 | expect((yield* numberedVersions(v1.functionName)).length).toBe(countCurrent); |
| 210 | |
| 211 | // --- list + nuke posture --- |
| 212 | const provider = yield* Provider.findProvider(AWS.Lambda.Version); |
| 213 | expect(provider.nuke?.skip).toBe(true); |
| 214 | const listed = yield* provider.list(); |
| 215 | expect(listed.some((version) => version.versionArn === current.versionArn)).toBe(true); |
| 216 | expect(listed.every((version) => version.version !== "$LATEST")).toBe(true); |
| 217 | |
| 218 | // --- explicitly authorized, qualified-only deletion --- |
| 219 | yield* stack.deploy( |
| 220 | program({ |
| 221 | handlerPath: handlerV2Path, |
| 222 | envVersion: "two", |
| 223 | // Force an operational-only Version update so the explicit removal |
| 224 | // policy is persisted before the resource is removed. |
| 225 | reservedConcurrentExecutions: 1, |
| 226 | destroyVersion: true, |
| 227 | }), |
| 228 | ); |
| 229 | yield* stack.deploy( |
| 230 | program({ |
| 231 | handlerPath: handlerV2Path, |
| 232 | envVersion: "two", |
| 233 | reservedConcurrentExecutions: 1, |
| 234 | includeVersion: false, |
| 235 | includeAlias: false, |
| 236 | }), |
| 237 | ); |
| 238 | |
| 239 | expect(yield* getVersionOrUndefined(v1.functionName, current.version)).toBeUndefined(); |
| 240 | expect( |
| 241 | yield* Lambda.getFunctionConfiguration({ |
| 242 | FunctionName: v1.functionName, |
| 243 | Qualifier: "$LATEST", |
| 244 | }), |
| 245 | ).toBeDefined(); |
| 246 | expect(yield* getVersionOrUndefined(v1.functionName, v1.version)).toBeDefined(); |
| 247 | |
| 248 | yield* stack.destroy(); |
| 249 | }).pipe( |
| 250 | Effect.tap(() => stack.destroy()), |
| 251 | Effect.onError(() => stack.destroy().pipe(Effect.ignore)), |
| 252 | ), |
| 253 | { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 360_000 }, |
| 254 | ); |
| 255 | |
| 256 | const numberedVersions = Effect.fn(function* (functionName: string) { |
| 257 | const response = yield* Lambda.listVersionsByFunction({ |
| 258 | FunctionName: functionName, |
| 259 | }); |
| 260 | return (response.Versions ?? []).filter((version) => version.Version !== "$LATEST"); |
| 261 | }); |
| 262 | |
| 263 | const getVersionOrUndefined = Effect.fn(function* (functionName: string, version: string) { |
| 264 | return yield* Lambda.getFunctionConfiguration({ |
| 265 | FunctionName: functionName, |
| 266 | Qualifier: version, |
| 267 | }).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined))); |
| 268 | }); |