The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { driftActions, repairDrift, waitForDrift } from "../drift.ts"; |
| 9 | |
| 10 | const timeoutHandlerPath = fileURLToPath(new URL("./timeout-handler.ts", import.meta.url)); |
| 11 | |
| 12 | const { test } = Test.make({ providers: AWS.providers() }); |
| 13 | |
| 14 | test.provider( |
| 15 | "syncs event invoke config on function and alias", |
| 16 | (stack) => |
| 17 | Effect.gen(function* () { |
| 18 | yield* stack.destroy(); |
| 19 | |
| 20 | const program = ({ |
| 21 | functionConfig, |
| 22 | alias, |
| 23 | }: { |
| 24 | functionConfig?: AWS.Lambda.EventInvokeConfig; |
| 25 | alias?: { |
| 26 | eventInvokeConfig?: AWS.Lambda.EventInvokeConfig; |
| 27 | }; |
| 28 | }) => |
| 29 | Effect.gen(function* () { |
| 30 | const queue = yield* AWS.SQS.Queue("FailureQueue", { |
| 31 | visibilityTimeout: "30 seconds", |
| 32 | }); |
| 33 | |
| 34 | const fn = yield* AWS.Lambda.Function("AsyncFn", { |
| 35 | main: timeoutHandlerPath, |
| 36 | handler: "handler", |
| 37 | isExternal: true, |
| 38 | functionUrl: false, |
| 39 | eventInvokeConfig: functionConfig, |
| 40 | }); |
| 41 | |
| 42 | yield* fn.bind("AllowEventInvokeDestination", { |
| 43 | policyStatements: [ |
| 44 | { |
| 45 | Effect: "Allow", |
| 46 | Action: ["sqs:SendMessage"], |
| 47 | Resource: [queue.queueArn], |
| 48 | }, |
| 49 | ], |
| 50 | }); |
| 51 | |
| 52 | const version = yield* AWS.Lambda.Version("AsyncVersion", { |
| 53 | function: fn, |
| 54 | }); |
| 55 | |
| 56 | const live = alias |
| 57 | ? yield* AWS.Lambda.Alias("LiveAlias", { |
| 58 | version, |
| 59 | aliasName: "live", |
| 60 | eventInvokeConfig: alias.eventInvokeConfig, |
| 61 | }) |
| 62 | : undefined; |
| 63 | |
| 64 | return { fn, queue, live }; |
| 65 | }); |
| 66 | |
| 67 | // --- create with function-level config --- |
| 68 | const created = yield* stack.deploy( |
| 69 | program({ |
| 70 | functionConfig: { |
| 71 | maximumRetryAttempts: 0, |
| 72 | maximumEventAge: "1 minute", |
| 73 | }, |
| 74 | }), |
| 75 | ); |
| 76 | |
| 77 | const liveCreated = yield* expectConfig(created.fn.functionName, { |
| 78 | maximumRetryAttempts: 0, |
| 79 | maximumEventAgeInSeconds: 60, |
| 80 | }); |
| 81 | expect(liveCreated.MaximumRetryAttempts).toBe(0); |
| 82 | expect(liveCreated.MaximumEventAgeInSeconds).toBe(60); |
| 83 | expect(liveCreated.DestinationConfig?.OnFailure?.Destination).toBe(undefined); |
| 84 | |
| 85 | // --- update retry behavior and add a failure destination --- |
| 86 | const updated = yield* stack.deploy( |
| 87 | program({ |
| 88 | functionConfig: { |
| 89 | maximumRetryAttempts: 1, |
| 90 | maximumEventAge: "2 minutes", |
| 91 | destinationConfig: { |
| 92 | OnFailure: { |
| 93 | Destination: created.queue.queueArn, |
| 94 | }, |
| 95 | }, |
| 96 | }, |
| 97 | }), |
| 98 | ); |
| 99 | |
| 100 | const liveUpdated = yield* expectConfig(updated.fn.functionName, { |
| 101 | maximumRetryAttempts: 1, |
| 102 | maximumEventAgeInSeconds: 120, |
| 103 | onFailureDestination: created.queue.queueArn, |
| 104 | }); |
| 105 | expect(liveUpdated.MaximumRetryAttempts).toBe(1); |
| 106 | expect(liveUpdated.MaximumEventAgeInSeconds).toBe(120); |
| 107 | expect(liveUpdated.DestinationConfig?.OnFailure?.Destination).toBe(created.queue.queueArn); |
| 108 | |
| 109 | // --- omit the prop: the config is deleted, not left behind --- |
| 110 | const removed = yield* stack.deploy(program({})); |
| 111 | yield* expectNoConfig(removed.fn.functionName); |
| 112 | |
| 113 | // --- alias-scoped config --- |
| 114 | const withAlias = yield* stack.deploy( |
| 115 | program({ |
| 116 | alias: { |
| 117 | eventInvokeConfig: { |
| 118 | maximumRetryAttempts: 2, |
| 119 | maximumEventAge: "5 minutes", |
| 120 | destinationConfig: { |
| 121 | OnFailure: { |
| 122 | Destination: created.queue.queueArn, |
| 123 | }, |
| 124 | }, |
| 125 | }, |
| 126 | }, |
| 127 | }), |
| 128 | ); |
| 129 | |
| 130 | const liveAliasConfig = yield* expectConfig( |
| 131 | withAlias.fn.functionName, |
| 132 | { |
| 133 | maximumRetryAttempts: 2, |
| 134 | maximumEventAgeInSeconds: 300, |
| 135 | onFailureDestination: created.queue.queueArn, |
| 136 | }, |
| 137 | withAlias.live!.aliasName, |
| 138 | ); |
| 139 | expect(liveAliasConfig.MaximumRetryAttempts).toBe(2); |
| 140 | expect(liveAliasConfig.MaximumEventAgeInSeconds).toBe(300); |
| 141 | expect(liveAliasConfig.DestinationConfig?.OnFailure?.Destination).toBe( |
| 142 | created.queue.queueArn, |
| 143 | ); |
| 144 | // The unqualified function stays unconfigured. |
| 145 | yield* expectNoConfig(withAlias.fn.functionName); |
| 146 | |
| 147 | // --- omit the alias prop: the alias-scoped config is deleted --- |
| 148 | const aliasCleared = yield* stack.deploy(program({ alias: {} })); |
| 149 | yield* expectNoConfig(aliasCleared.fn.functionName, aliasCleared.live!.aliasName); |
| 150 | |
| 151 | yield* stack.destroy(); |
| 152 | |
| 153 | // Out-of-band proof the destroy removed the host function (and with it |
| 154 | // the event invoke configs) from the cloud. |
| 155 | yield* Lambda.getFunction({ |
| 156 | FunctionName: aliasCleared.fn.functionName, |
| 157 | }).pipe( |
| 158 | Effect.flatMap(() => |
| 159 | Effect.fail(new Error(`Function ${aliasCleared.fn.functionName} still exists`)), |
| 160 | ), |
| 161 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 162 | Effect.retry({ |
| 163 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]), |
| 164 | }), |
| 165 | ); |
| 166 | }).pipe( |
| 167 | Effect.tap(() => stack.destroy()), |
| 168 | Effect.onError(() => stack.destroy().pipe(Effect.ignore)), |
| 169 | ), |
| 170 | { |
| 171 | tags: ["provider:aws", "provider:aws:lambda", "provider:aws:sqs", "live"], |
| 172 | timeout: 360_000, |
| 173 | }, |
| 174 | ); |
| 175 | |
| 176 | const getConfigOrUndefined = Effect.fn(function* (functionName: string, qualifier?: string) { |
| 177 | return yield* Lambda.getFunctionEventInvokeConfig({ |
| 178 | FunctionName: functionName, |
| 179 | Qualifier: qualifier, |
| 180 | }).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined))); |
| 181 | }); |
| 182 | |
| 183 | // Reads until the config matches the expected shape (updates propagate |
| 184 | // eventually), failing after a bounded number of attempts. |
| 185 | const expectConfig = Effect.fn(function* ( |
| 186 | functionName: string, |
| 187 | expected: { |
| 188 | maximumRetryAttempts: number; |
| 189 | maximumEventAgeInSeconds: number; |
| 190 | onFailureDestination?: string; |
| 191 | }, |
| 192 | qualifier?: string, |
| 193 | ) { |
| 194 | return yield* getConfigOrUndefined(functionName, qualifier).pipe( |
| 195 | Effect.filterOrFail( |
| 196 | (config): config is Lambda.FunctionEventInvokeConfig => |
| 197 | config !== undefined && |
| 198 | config.MaximumRetryAttempts === expected.maximumRetryAttempts && |
| 199 | config.MaximumEventAgeInSeconds === expected.maximumEventAgeInSeconds && |
| 200 | config.DestinationConfig?.OnFailure?.Destination === expected.onFailureDestination, |
| 201 | () => new Error("Event invoke config update has not propagated yet"), |
| 202 | ), |
| 203 | Effect.retry({ |
| 204 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(10)]), |
| 205 | }), |
| 206 | ); |
| 207 | }); |
| 208 | |
| 209 | const expectNoConfig = Effect.fn(function* (functionName: string, qualifier?: string) { |
| 210 | yield* getConfigOrUndefined(functionName, qualifier).pipe( |
| 211 | Effect.filterOrFail( |
| 212 | (config) => config === undefined, |
| 213 | () => new Error("Event invoke config removal has not propagated yet"), |
| 214 | ), |
| 215 | Effect.retry({ |
| 216 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(10)]), |
| 217 | }), |
| 218 | ); |
| 219 | }); |
| 220 | |
| 221 | // #2688: Function and Alias `read` must observe the async invocation config, |
| 222 | // or retries or destinations changed out of band read as `unchanged`. |
| 223 | test.provider( |
| 224 | "drift detects and repairs out-of-band event invoke config on function and alias", |
| 225 | (stack) => |
| 226 | Effect.gen(function* () { |
| 227 | yield* stack.destroy(); |
| 228 | |
| 229 | const { fn, live } = yield* stack.deploy( |
| 230 | Effect.gen(function* () { |
| 231 | const fn = yield* AWS.Lambda.Function("DriftAsyncFn", { |
| 232 | main: timeoutHandlerPath, |
| 233 | handler: "handler", |
| 234 | isExternal: true, |
| 235 | functionUrl: false, |
| 236 | eventInvokeConfig: { maximumRetryAttempts: 0 }, |
| 237 | }); |
| 238 | const version = yield* AWS.Lambda.Version("DriftAsyncVersion", { function: fn }); |
| 239 | const live = yield* AWS.Lambda.Alias("DriftAsyncAlias", { |
| 240 | version, |
| 241 | aliasName: "live", |
| 242 | eventInvokeConfig: { maximumRetryAttempts: 1 }, |
| 243 | }); |
| 244 | return { fn, live }; |
| 245 | }), |
| 246 | ); |
| 247 | const actions = yield* driftActions(stack); |
| 248 | expect(Object.values(actions).every((action) => action === "unchanged")).toBe(true); |
| 249 | |
| 250 | yield* Lambda.putFunctionEventInvokeConfig({ |
| 251 | FunctionName: fn.functionName, |
| 252 | MaximumRetryAttempts: 2, |
| 253 | }); |
| 254 | yield* Lambda.putFunctionEventInvokeConfig({ |
| 255 | FunctionName: fn.functionName, |
| 256 | Qualifier: live.aliasName, |
| 257 | MaximumRetryAttempts: 2, |
| 258 | }); |
| 259 | |
| 260 | const fnDrift = yield* waitForDrift(stack, "DriftAsyncFn", "drifted"); |
| 261 | expect(fnDrift?.attr?.eventInvokeConfig?.maximumRetryAttempts).toBe(2); |
| 262 | const aliasDrift = yield* waitForDrift(stack, "DriftAsyncAlias", "drifted"); |
| 263 | expect(aliasDrift?.attr?.eventInvokeConfig?.maximumRetryAttempts).toBe(2); |
| 264 | |
| 265 | const repaired = yield* repairDrift(stack); |
| 266 | expect(repaired.resources.DriftAsyncFn?.action).toBe("repaired"); |
| 267 | expect(repaired.resources.DriftAsyncAlias?.action).toBe("repaired"); |
| 268 | const after = yield* driftActions(stack); |
| 269 | expect(Object.values(after).every((action) => action === "unchanged")).toBe(true); |
| 270 | const fnConfig = yield* Lambda.getFunctionEventInvokeConfig({ |
| 271 | FunctionName: fn.functionName, |
| 272 | }); |
| 273 | expect(fnConfig.MaximumRetryAttempts).toBe(0); |
| 274 | const aliasConfig = yield* Lambda.getFunctionEventInvokeConfig({ |
| 275 | FunctionName: fn.functionName, |
| 276 | Qualifier: live.aliasName, |
| 277 | }); |
| 278 | expect(aliasConfig.MaximumRetryAttempts).toBe(1); |
| 279 | |
| 280 | yield* stack.destroy(); |
| 281 | }), |
| 282 | { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 240_000 }, |
| 283 | ); |