The best way to control Claude Code, Codex and more from any computer or phone
| 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 GeoMapsTestFunctionLive, { GeoMapsTestFunction } from "./handler"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "GeoMapsBindings"); |
| 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 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 28 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 29 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 30 | // 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.retry({ |
| 43 | while: (e) => e._tag === "TransientUpstream", |
| 44 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | const getJson = (path: string) => |
| 49 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 50 | |
| 51 | describe.sequential( |
| 52 | "GeoMaps Bindings", |
| 53 | { tags: ["provider:aws", "provider:aws:geomaps", "provider:aws:lambda", "live"] }, |
| 54 | () => { |
| 55 | beforeAll( |
| 56 | Effect.gen(function* () { |
| 57 | yield* Effect.logInfo("GeoMaps test setup: destroying previous resources"); |
| 58 | yield* sharedStack.destroy(); |
| 59 | |
| 60 | yield* Effect.logInfo("GeoMaps test setup: deploying fixture"); |
| 61 | const { functionUrl } = yield* sharedStack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | return yield* GeoMapsTestFunction; |
| 64 | }).pipe(Effect.provide(GeoMapsTestFunctionLive)), |
| 65 | ); |
| 66 | |
| 67 | expect(functionUrl).toBeTruthy(); |
| 68 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 69 | |
| 70 | const readinessUrl = `${baseUrl}/bindings`; |
| 71 | yield* Effect.logInfo(`GeoMaps test setup: probing readiness at ${readinessUrl}`); |
| 72 | yield* HttpClient.get(readinessUrl).pipe( |
| 73 | Effect.flatMap((response) => |
| 74 | response.status === 200 |
| 75 | ? Effect.succeed(response) |
| 76 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 77 | ), |
| 78 | Effect.tapError((error) => |
| 79 | Effect.logWarning(`GeoMaps test setup: fixture not ready yet (${String(error)})`), |
| 80 | ), |
| 81 | Effect.retry({ schedule: readinessPolicy }), |
| 82 | ); |
| 83 | }), |
| 84 | { timeout: 240_000 }, |
| 85 | ); |
| 86 | |
| 87 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 88 | |
| 89 | describe("binding registration", () => { |
| 90 | test.provider("all 5 capabilities initialize in the runtime", (_stack) => |
| 91 | Effect.gen(function* () { |
| 92 | const response = yield* getJson("/bindings"); |
| 93 | expect((response as any).bound).toHaveLength(5); |
| 94 | }), |
| 95 | ); |
| 96 | }); |
| 97 | |
| 98 | describe("GetStaticMap", () => { |
| 99 | test.provider( |
| 100 | "renders a static map image (nonzero PNG payload)", |
| 101 | (_stack) => |
| 102 | Effect.gen(function* () { |
| 103 | const response = (yield* getJson("/static-map")) as any; |
| 104 | expect(response.bytes).toBeGreaterThan(0); |
| 105 | expect(response.contentType).toContain("image/"); |
| 106 | expect(typeof response.pricingBucket).toBe("string"); |
| 107 | }), |
| 108 | { timeout: 60_000 }, |
| 109 | ); |
| 110 | }); |
| 111 | |
| 112 | describe("GetTile", () => { |
| 113 | test.provider( |
| 114 | "fetches the 0/0/0 vector basemap tile", |
| 115 | (_stack) => |
| 116 | Effect.gen(function* () { |
| 117 | const response = (yield* getJson("/tile")) as any; |
| 118 | expect(response.bytes).toBeGreaterThan(0); |
| 119 | expect(typeof response.pricingBucket).toBe("string"); |
| 120 | }), |
| 121 | { timeout: 60_000 }, |
| 122 | ); |
| 123 | }); |
| 124 | |
| 125 | describe("GetStyleDescriptor", () => { |
| 126 | test.provider( |
| 127 | "fetches the Standard MapLibre style descriptor (valid JSON)", |
| 128 | (_stack) => |
| 129 | Effect.gen(function* () { |
| 130 | const response = (yield* getJson("/style-descriptor")) as any; |
| 131 | expect(response.bytes).toBeGreaterThan(0); |
| 132 | // MapLibre style spec version is always 8. |
| 133 | expect(response.version).toBe(8); |
| 134 | }), |
| 135 | { timeout: 60_000 }, |
| 136 | ); |
| 137 | }); |
| 138 | |
| 139 | describe("GetSprites", () => { |
| 140 | test.provider( |
| 141 | "fetches the Standard style's sprite sheet", |
| 142 | (_stack) => |
| 143 | Effect.gen(function* () { |
| 144 | const response = (yield* getJson("/sprites")) as any; |
| 145 | expect(response.bytes).toBeGreaterThan(0); |
| 146 | }), |
| 147 | { timeout: 60_000 }, |
| 148 | ); |
| 149 | }); |
| 150 | |
| 151 | describe("GetGlyphs", () => { |
| 152 | test.provider( |
| 153 | "fetches a glyph PBF range for Amazon Ember Regular", |
| 154 | (_stack) => |
| 155 | Effect.gen(function* () { |
| 156 | const response = (yield* getJson("/glyphs")) as any; |
| 157 | expect(response.bytes).toBeGreaterThan(0); |
| 158 | }), |
| 159 | { timeout: 60_000 }, |
| 160 | ); |
| 161 | }); |
| 162 | }, |
| 163 | ); |