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 * as AWS from "@alchemy.run/aws"; |
| 2 | import { encodeDurableEnvelope } from "@alchemy.run/aws/Lambda/DurableBridge"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as Core from "@alchemy.run/test/Core"; |
| 6 | import * as Lambda from "@distilled.cloud/aws/lambda"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Redacted from "effect/Redacted"; |
| 9 | import * as Result from "effect/Result"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { driftActions, repairDrift } from "../drift.ts"; |
| 12 | import DurableFlowLive, { DurableFlow } from "./fixtures/durable-handler"; |
| 13 | |
| 14 | const testOptions = { providers: AWS.providers() }; |
| 15 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 16 | const sharedStack = Core.scratchStack(testOptions, "LambdaDurable"); |
| 17 | |
| 18 | const unwrapSensitive = ( |
| 19 | value: string | Redacted.Redacted<string> | undefined, |
| 20 | ): string | undefined => |
| 21 | value === undefined ? undefined : Redacted.isRedacted(value) ? Redacted.value(value) : value; |
| 22 | |
| 23 | describe( |
| 24 | "Lambda DurableFunction", |
| 25 | { tags: ["provider:aws", "provider:aws:lambda", "live"] }, |
| 26 | () => { |
| 27 | // Ungated typed-error probe: proves the durable-execution management API is |
| 28 | // reachable in this region and that its errors are typed tags in the |
| 29 | // distilled union — at near-zero cost, forever. |
| 30 | test.provider( |
| 31 | "durable management API returns typed errors", |
| 32 | (_stack) => |
| 33 | Effect.gen(function* () { |
| 34 | const result = yield* Effect.result( |
| 35 | Lambda.listDurableExecutionsByFunction({ |
| 36 | FunctionName: "alchemy-durable-probe-nonexistent", |
| 37 | }), |
| 38 | ); |
| 39 | expect(Result.isFailure(result)).toBe(true); |
| 40 | if (Result.isFailure(result)) { |
| 41 | expect(result.failure._tag).toBe("ResourceNotFoundException"); |
| 42 | } |
| 43 | }), |
| 44 | { timeout: 30_000 }, |
| 45 | ); |
| 46 | |
| 47 | // Full lifecycle: a real deploy (bundling + vendoring the Durable Execution |
| 48 | // SDK + waiting out IAM role propagation) plus a live durable execution with |
| 49 | // a suspend/resume. |
| 50 | describe("lifecycle", () => { |
| 51 | let functionName: string; |
| 52 | let qualifier: string; |
| 53 | |
| 54 | beforeAll( |
| 55 | Effect.gen(function* () { |
| 56 | yield* Effect.logInfo("Durable test setup: destroying previous stack"); |
| 57 | yield* sharedStack.destroy(); |
| 58 | yield* Effect.logInfo("Durable test setup: deploying the DurableFunction"); |
| 59 | const outputs = yield* sharedStack.deploy( |
| 60 | Effect.gen(function* () { |
| 61 | const flow = yield* DurableFlow; |
| 62 | const version = yield* AWS.Lambda.Version("DurableFlowVersion", { |
| 63 | function: flow.function, |
| 64 | }); |
| 65 | const live = yield* AWS.Lambda.Alias("DurableFlowLive", { |
| 66 | version, |
| 67 | aliasName: "live", |
| 68 | }); |
| 69 | return { functionName: flow.functionName, qualifier: live.aliasName }; |
| 70 | }).pipe(Effect.provide(DurableFlowLive)), |
| 71 | ); |
| 72 | functionName = outputs.functionName; |
| 73 | qualifier = outputs.qualifier; |
| 74 | expect(functionName).toBeTruthy(); |
| 75 | expect(qualifier).toBe("live"); |
| 76 | }), |
| 77 | // ~35s in isolation, but the deploy bundles + npm-vendors the Durable |
| 78 | // Execution SDK in-process; under full-suite CPU saturation the |
| 79 | // reconcile alone has been observed to exceed 420s. |
| 80 | { timeout: 600_000 }, |
| 81 | ); |
| 82 | |
| 83 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 84 | |
| 85 | test.provider( |
| 86 | "runs a 2-step + sleep orchestration to completion", |
| 87 | (_stack) => |
| 88 | Effect.gen(function* () { |
| 89 | // Start: async Invoke with the alchemy envelope and an idempotent |
| 90 | // execution name (safe to re-run — same name + same payload |
| 91 | // reattaches to the existing execution). |
| 92 | const started = yield* Lambda.invoke({ |
| 93 | FunctionName: functionName, |
| 94 | Qualifier: qualifier, |
| 95 | InvocationType: "Event", |
| 96 | DurableExecutionName: "durable-test-flow-2", |
| 97 | Payload: encodeDurableEnvelope("DurableFlow", { orderId: "order-1" }), |
| 98 | }); |
| 99 | expect(started.StatusCode).toBe(202); |
| 100 | |
| 101 | // Resolve the execution ARN — from the Invoke response when |
| 102 | // present, otherwise via the list API (bounded). |
| 103 | const executionArn = started.DurableExecutionArn |
| 104 | ? started.DurableExecutionArn |
| 105 | : yield* Lambda.listDurableExecutionsByFunction({ |
| 106 | FunctionName: functionName, |
| 107 | DurableExecutionName: "durable-test-flow-2", |
| 108 | }).pipe( |
| 109 | Effect.map((r) => r.DurableExecutions?.[0]?.DurableExecutionArn), |
| 110 | Effect.repeat({ |
| 111 | schedule: Schedule.spaced("2 seconds"), |
| 112 | until: (arn) => arn !== undefined, |
| 113 | times: 10, |
| 114 | }), |
| 115 | Effect.map((arn) => arn!), |
| 116 | ); |
| 117 | |
| 118 | // The flow sleeps 5s mid-way (a real suspend/resume), so poll the |
| 119 | // execution to a terminal state with a bounded schedule. |
| 120 | const execution = yield* Lambda.getDurableExecution({ |
| 121 | DurableExecutionArn: executionArn, |
| 122 | }).pipe( |
| 123 | Effect.repeat({ |
| 124 | schedule: Schedule.spaced("3 seconds"), |
| 125 | until: (r) => r.Status !== "RUNNING", |
| 126 | times: 30, |
| 127 | }), |
| 128 | ); |
| 129 | |
| 130 | expect(execution.Status).toBe("SUCCEEDED"); |
| 131 | const result = JSON.parse(unwrapSensitive(execution.Result) ?? "{}"); |
| 132 | expect(result).toEqual({ |
| 133 | orderId: "order-1", |
| 134 | reserved: true, |
| 135 | total: 42, |
| 136 | runtime: "AWS.Lambda.Function", |
| 137 | }); |
| 138 | }), |
| 139 | { timeout: 150_000 }, |
| 140 | ); |
| 141 | }); |
| 142 | }, |
| 143 | ); |
| 144 | |
| 145 | // #2688: the durable execution timeout and history retention are managed |
| 146 | // settings, so `read` must observe them. |
| 147 | test.provider( |
| 148 | "drift detects and repairs out-of-band durable config", |
| 149 | (stack) => |
| 150 | Effect.gen(function* () { |
| 151 | yield* stack.destroy(); |
| 152 | const { functionName } = yield* stack.deploy( |
| 153 | Effect.gen(function* () { |
| 154 | const flow = yield* DurableFlow; |
| 155 | return { functionName: flow.functionName }; |
| 156 | }).pipe(Effect.provide(DurableFlowLive)), |
| 157 | ); |
| 158 | const allUnchanged = (actions: Record<string, string | undefined>) => |
| 159 | Object.values(actions).every((action) => action === "unchanged"); |
| 160 | expect(allUnchanged(yield* driftActions(stack))).toBe(true); |
| 161 | |
| 162 | const config = yield* Lambda.getFunctionConfiguration({ FunctionName: functionName }); |
| 163 | yield* Lambda.updateFunctionConfiguration({ |
| 164 | FunctionName: functionName, |
| 165 | DurableConfig: { ...config.DurableConfig, RetentionPeriodInDays: 7 }, |
| 166 | }).pipe( |
| 167 | Effect.retry({ |
| 168 | while: (e) => e._tag === "ResourceConflictException", |
| 169 | schedule: Schedule.spaced("2 seconds"), |
| 170 | times: 15, |
| 171 | }), |
| 172 | ); |
| 173 | |
| 174 | const drifted = yield* driftActions(stack).pipe( |
| 175 | Effect.repeat({ |
| 176 | until: (actions) => Object.values(actions).includes("drifted"), |
| 177 | schedule: Schedule.spaced("2 seconds"), |
| 178 | times: 15, |
| 179 | }), |
| 180 | ); |
| 181 | expect(Object.values(drifted)).toContain("drifted"); |
| 182 | |
| 183 | yield* repairDrift(stack); |
| 184 | expect(allUnchanged(yield* driftActions(stack))).toBe(true); |
| 185 | const restored = yield* Lambda.getFunctionConfiguration({ FunctionName: functionName }); |
| 186 | expect(restored.DurableConfig?.RetentionPeriodInDays).toBe(1); |
| 187 | |
| 188 | yield* stack.destroy(); |
| 189 | }), |
| 190 | { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 600_000 }, |
| 191 | ); |