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 Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import GaTestFunctionLive, { GaTestFunction } from "./handler"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "GABindings"); |
| 15 | |
| 16 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 17 | // over 60s on a fresh deploy. |
| 18 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 19 | |
| 20 | let baseUrl: string; |
| 21 | |
| 22 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 23 | readonly status: number; |
| 24 | readonly body: string; |
| 25 | }> {} |
| 26 | |
| 27 | // Retry only 5xx (cold re-init, IAM propagation, and Global Accelerator's |
| 28 | // per-accelerator transaction serialization surfacing as |
| 29 | // TransactionInProgressException through the handler's `Effect.orDie`); a |
| 30 | // genuine 4xx/assertion failure surfaces immediately. |
| 31 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 32 | HttpClient.execute(request).pipe( |
| 33 | Effect.flatMap((response) => |
| 34 | response.status >= 500 |
| 35 | ? response.text.pipe( |
| 36 | Effect.flatMap((body) => |
| 37 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 38 | ), |
| 39 | ) |
| 40 | : Effect.succeed(response), |
| 41 | ), |
| 42 | Effect.tapError((e) => |
| 43 | e._tag === "TransientUpstream" |
| 44 | ? Effect.logWarning(`transient upstream ${e.status}: ${e.body.slice(0, 500)}`) |
| 45 | : Effect.void, |
| 46 | ), |
| 47 | Effect.retry({ |
| 48 | while: (e): boolean => e._tag === "TransientUpstream", |
| 49 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(8)]), |
| 50 | }), |
| 51 | ); |
| 52 | |
| 53 | const getJson = (path: string) => |
| 54 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 55 | |
| 56 | const postJson = (path: string) => |
| 57 | send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 58 | |
| 59 | describe.sequential( |
| 60 | "GlobalAccelerator Bindings", |
| 61 | { |
| 62 | tags: [ |
| 63 | "provider:aws", |
| 64 | "provider:aws:ec2", |
| 65 | "provider:aws:globalaccelerator", |
| 66 | "provider:aws:lambda", |
| 67 | "live", |
| 68 | ], |
| 69 | }, |
| 70 | () => { |
| 71 | beforeAll( |
| 72 | Effect.gen(function* () { |
| 73 | yield* Effect.logInfo("GA test setup: destroying previous resources"); |
| 74 | yield* sharedStack.destroy(); |
| 75 | |
| 76 | yield* Effect.logInfo("GA test setup: deploying fixture"); |
| 77 | const attrs = yield* sharedStack.deploy( |
| 78 | Effect.gen(function* () { |
| 79 | return yield* GaTestFunction; |
| 80 | }).pipe(Effect.provide(GaTestFunctionLive)), |
| 81 | ); |
| 82 | |
| 83 | expect(attrs.functionUrl).toBeTruthy(); |
| 84 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 85 | |
| 86 | const readinessUrl = `${baseUrl}/bindings`; |
| 87 | yield* Effect.logInfo(`GA test setup: probing readiness at ${readinessUrl}`); |
| 88 | yield* HttpClient.get(readinessUrl).pipe( |
| 89 | Effect.flatMap((response) => |
| 90 | response.status === 200 |
| 91 | ? Effect.succeed(response) |
| 92 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 93 | ), |
| 94 | Effect.tapError((error) => |
| 95 | Effect.logWarning(`GA test setup: fixture not ready yet (${String(error)})`), |
| 96 | ), |
| 97 | Effect.retry({ schedule: readinessPolicy }), |
| 98 | ); |
| 99 | }), |
| 100 | { timeout: 240_000 }, |
| 101 | ); |
| 102 | |
| 103 | afterAll(sharedStack.destroy(), { timeout: 300_000 }); |
| 104 | |
| 105 | describe("binding registration", () => { |
| 106 | test.provider("all four capabilities initialize in the runtime", (_stack) => |
| 107 | Effect.gen(function* () { |
| 108 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 109 | expect(response.bound).toHaveLength(4); |
| 110 | }), |
| 111 | ); |
| 112 | }); |
| 113 | |
| 114 | describe("DescribeAccelerator", () => { |
| 115 | test.provider("reads the bound accelerator's state (injected ARN)", (_stack) => |
| 116 | Effect.gen(function* () { |
| 117 | const response = (yield* getJson("/accelerator")) as { |
| 118 | name: string; |
| 119 | status: string; |
| 120 | dnsName: string; |
| 121 | enabled: boolean; |
| 122 | }; |
| 123 | expect(response.dnsName).toContain(".awsglobalaccelerator.com"); |
| 124 | expect(response.enabled).toBe(true); |
| 125 | expect(["DEPLOYED", "IN_PROGRESS"]).toContain(response.status); |
| 126 | }), |
| 127 | ); |
| 128 | }); |
| 129 | |
| 130 | describe("DescribeEndpointGroup", () => { |
| 131 | test.provider("reads the bound endpoint group's state (injected ARN)", (_stack) => |
| 132 | Effect.gen(function* () { |
| 133 | const response = (yield* getJson("/group")) as { |
| 134 | region: string; |
| 135 | endpoints: { endpointId: string; healthState: string }[]; |
| 136 | }; |
| 137 | expect(response.region).toEqual("us-west-2"); |
| 138 | }), |
| 139 | ); |
| 140 | }); |
| 141 | |
| 142 | describe("AddEndpoints / RemoveEndpoints", () => { |
| 143 | test.provider( |
| 144 | "registers and deregisters an Elastic IP endpoint at runtime", |
| 145 | (_stack) => |
| 146 | Effect.gen(function* () { |
| 147 | // Global Accelerator serializes changes per accelerator: mutating |
| 148 | // while the create/update transaction from the deploy is still |
| 149 | // IN_PROGRESS fails with TransactionInProgressException. Wait for |
| 150 | // DEPLOYED (bounded) before driving Add/RemoveEndpoints. |
| 151 | const accelerator = yield* getJson("/accelerator").pipe( |
| 152 | Effect.map((a) => a as { status: string }), |
| 153 | Effect.repeat({ |
| 154 | schedule: Schedule.spaced("5 seconds"), |
| 155 | until: (a): boolean => a.status === "DEPLOYED", |
| 156 | times: 36, |
| 157 | }), |
| 158 | ); |
| 159 | expect(accelerator.status).toEqual("DEPLOYED"); |
| 160 | |
| 161 | const { allocationId } = (yield* getJson("/context")) as { allocationId: string }; |
| 162 | expect(allocationId).toMatch(/^eipalloc-/); |
| 163 | |
| 164 | // Register — the response echoes the endpoint back. |
| 165 | const added = (yield* postJson("/endpoints/add")) as { added: string[] }; |
| 166 | expect(added.added, JSON.stringify(added)).toContain(allocationId); |
| 167 | |
| 168 | // The group's observed state now includes the Elastic IP. |
| 169 | const group = yield* getJson("/group").pipe( |
| 170 | Effect.map((g) => g as { endpoints: { endpointId: string }[] }), |
| 171 | Effect.repeat({ |
| 172 | schedule: Schedule.spaced("3 seconds"), |
| 173 | until: (g): boolean => |
| 174 | g.endpoints.some((endpoint) => endpoint.endpointId === allocationId), |
| 175 | times: 10, |
| 176 | }), |
| 177 | ); |
| 178 | expect(group.endpoints.map((endpoint) => endpoint.endpointId)).toContain(allocationId); |
| 179 | |
| 180 | // Deregister and observe it gone. |
| 181 | const removed = (yield* postJson("/endpoints/remove")) as { removed: string }; |
| 182 | expect(removed.removed, JSON.stringify(removed)).toEqual(allocationId); |
| 183 | |
| 184 | const drained = yield* getJson("/group").pipe( |
| 185 | Effect.map((g) => g as { endpoints: { endpointId: string }[] }), |
| 186 | Effect.repeat({ |
| 187 | schedule: Schedule.spaced("3 seconds"), |
| 188 | until: (g): boolean => |
| 189 | g.endpoints.every((endpoint) => endpoint.endpointId !== allocationId), |
| 190 | times: 10, |
| 191 | }), |
| 192 | ); |
| 193 | expect(drained.endpoints.map((endpoint) => endpoint.endpointId)).not.toContain( |
| 194 | allocationId, |
| 195 | ); |
| 196 | }), |
| 197 | // Covers the (bounded) wait for the accelerator's deploy transaction |
| 198 | // to reach DEPLOYED plus the add/observe/remove/observe cycle. |
| 199 | { timeout: 300_000 }, |
| 200 | ); |
| 201 | }); |
| 202 | }, |
| 203 | ); |