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 { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Core from "@alchemy.run/test/Core"; |
| 5 | import * as eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import DlmTestFunctionLive, { DlmTestFunction } from "./handler"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "DLMBindings"); |
| 16 | |
| 17 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 18 | // over 60s on a fresh deploy. |
| 19 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 20 | |
| 21 | let baseUrl: string; |
| 22 | let functionArn: string; |
| 23 | |
| 24 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 25 | readonly status: number; |
| 26 | readonly body: string; |
| 27 | }> {} |
| 28 | |
| 29 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 30 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 31 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 32 | // 4xx/assertion failure surfaces immediately. |
| 33 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 34 | HttpClient.execute(request).pipe( |
| 35 | Effect.flatMap((response) => |
| 36 | response.status >= 500 |
| 37 | ? response.text.pipe( |
| 38 | Effect.flatMap((body) => |
| 39 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 40 | ), |
| 41 | ) |
| 42 | : Effect.succeed(response), |
| 43 | ), |
| 44 | Effect.retry({ |
| 45 | while: (e) => e._tag === "TransientUpstream", |
| 46 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | const getJson = (path: string) => |
| 51 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 52 | |
| 53 | describe.sequential( |
| 54 | "DLM Bindings", |
| 55 | { tags: ["provider:aws", "provider:aws:dlm", "provider:aws:lambda", "live"] }, |
| 56 | () => { |
| 57 | beforeAll( |
| 58 | Effect.gen(function* () { |
| 59 | yield* Effect.logInfo("DLM test setup: destroying previous resources"); |
| 60 | yield* sharedStack.destroy(); |
| 61 | |
| 62 | yield* Effect.logInfo("DLM test setup: deploying fixture"); |
| 63 | const attrs = yield* sharedStack.deploy( |
| 64 | Effect.gen(function* () { |
| 65 | return yield* DlmTestFunction; |
| 66 | }).pipe(Effect.provide(DlmTestFunctionLive)), |
| 67 | ); |
| 68 | |
| 69 | expect(attrs.functionUrl).toBeTruthy(); |
| 70 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 71 | functionArn = attrs.functionArn; |
| 72 | |
| 73 | const readinessUrl = `${baseUrl}/bindings`; |
| 74 | yield* Effect.logInfo(`DLM test setup: probing readiness at ${readinessUrl}`); |
| 75 | yield* HttpClient.get(readinessUrl).pipe( |
| 76 | Effect.flatMap((response) => |
| 77 | response.status === 200 |
| 78 | ? Effect.succeed(response) |
| 79 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 80 | ), |
| 81 | Effect.tapError((error) => |
| 82 | Effect.logWarning(`DLM test setup: fixture not ready yet (${String(error)})`), |
| 83 | ), |
| 84 | Effect.retry({ schedule: readinessPolicy }), |
| 85 | ); |
| 86 | }), |
| 87 | { timeout: 240_000 }, |
| 88 | ); |
| 89 | |
| 90 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 91 | |
| 92 | describe("binding registration", () => { |
| 93 | test.provider("both capabilities initialize in the runtime", (_stack) => |
| 94 | Effect.gen(function* () { |
| 95 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 96 | expect(response.bound).toHaveLength(2); |
| 97 | }), |
| 98 | ); |
| 99 | }); |
| 100 | |
| 101 | describe("GetLifecyclePolicy", () => { |
| 102 | test.provider("reads the bound policy's detail (injected policy id)", (_stack) => |
| 103 | Effect.gen(function* () { |
| 104 | const response = (yield* getJson("/policy")) as { |
| 105 | policyId: string; |
| 106 | state: string; |
| 107 | policyType: string; |
| 108 | }; |
| 109 | expect(response.policyId).toMatch(/^policy-/); |
| 110 | expect(response.state).toBe("DISABLED"); |
| 111 | expect(response.policyType).toBe("EBS_SNAPSHOT_MANAGEMENT"); |
| 112 | }), |
| 113 | ); |
| 114 | }); |
| 115 | |
| 116 | describe("GetLifecyclePolicies", () => { |
| 117 | test.provider("enumerates the account's policies including the fixture's", (_stack) => |
| 118 | Effect.gen(function* () { |
| 119 | const policy = (yield* getJson("/policy")) as { policyId: string }; |
| 120 | const response = (yield* getJson("/policies")) as { ids: string[] }; |
| 121 | expect(response.ids).toContain(policy.policyId); |
| 122 | }), |
| 123 | ); |
| 124 | }); |
| 125 | |
| 126 | describe("consumePolicyEvents", { tags: ["provider:aws:eventbridge"] }, () => { |
| 127 | test.provider("the deploy created an EventBridge rule targeting the function", (_stack) => |
| 128 | Effect.gen(function* () { |
| 129 | // Out-of-band via distilled: the fixture's consumePolicyEvents |
| 130 | // must have materialized as a rule on the default bus with the |
| 131 | // Lambda as target. |
| 132 | const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({ |
| 133 | TargetArn: functionArn, |
| 134 | }); |
| 135 | expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1); |
| 136 | }), |
| 137 | ); |
| 138 | }); |
| 139 | }, |
| 140 | ); |