1import * as AWS from "@/AWS";
2import * as Core from "@/Test/Core";
3import * as Test from "@/Test/Alchemy";
4import { describe, expect } from "alchemy-test";
5import * as Data from "effect/Data";
6import * as Effect from "effect/Effect";
7import * as Schedule from "effect/Schedule";
8import * as HttpClient from "effect/http/HttpClient";
9import * as HttpClientRequest from "effect/http/HttpClientRequest";
10import GeoMapsTestFunctionLive, { GeoMapsTestFunction } from "./handler";
11
12const testOptions = { providers: AWS.providers() };
13const { test, beforeAll, afterAll } = Test.make(testOptions);
14const 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.
18const readinessPolicy = Schedule.max([
19 Schedule.fixed("2 seconds"),
20 Schedule.recurs(75),
21]);
22
23let baseUrl: string;
24
25class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
26 readonly status: number;
27 readonly body: string;
28}> {}
29
30// The shared Lambda fixture occasionally answers a transient 5xx under load
31// (cold re-init, IAM propagation on the freshly attached policy that the
32// handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
33// 4xx/assertion failure surfaces immediately.
34const send = (request: HttpClientRequest.HttpClientRequest) =>
35 HttpClient.execute(request).pipe(
36 Effect.flatMap((response) =>
37 response.status >= 500
38 ? response.text.pipe(
39 Effect.flatMap((body) =>
40 Effect.fail(
41 new TransientUpstream({ status: response.status, body }),
42 ),
43 ),
44 )
45 : Effect.succeed(response),
46 ),
47 Effect.retry({
48 while: (e) => e._tag === "TransientUpstream",
49 schedule: Schedule.max([
50 Schedule.exponential("500 millis"),
51 Schedule.recurs(6),
52 ]),
53 }),
54 );
55
56const getJson = (path: string) =>
57 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(
58 Effect.flatMap((r) => r.json),
59 );
60
61describe.sequential(
62 "GeoMaps Bindings",
63 {
64 tags: [
65 "provider:aws",
66 "provider:aws:geomaps",
67 "provider:aws:lambda",
68 "live",
69 ],
70 },
71 () => {
72 beforeAll(
73 Effect.gen(function* () {
74 yield* Effect.logInfo(
75 "GeoMaps test setup: destroying previous resources",
76 );
77 yield* sharedStack.destroy();
78
79 yield* Effect.logInfo("GeoMaps test setup: deploying fixture");
80 const { functionUrl } = yield* sharedStack.deploy(
81 Effect.gen(function* () {
82 return yield* GeoMapsTestFunction;
83 }).pipe(Effect.provide(GeoMapsTestFunctionLive)),
84 );
85
86 expect(functionUrl).toBeTruthy();
87 baseUrl = functionUrl!.replace(/\/+$/, "");
88
89 const readinessUrl = `${baseUrl}/bindings`;
90 yield* Effect.logInfo(
91 `GeoMaps test setup: probing readiness at ${readinessUrl}`,
92 );
93 yield* HttpClient.get(readinessUrl).pipe(
94 Effect.flatMap((response) =>
95 response.status === 200
96 ? Effect.succeed(response)
97 : Effect.fail(
98 new Error(`Function not ready: ${response.status}`),
99 ),
100 ),
101 Effect.tapError((error) =>
102 Effect.logWarning(
103 `GeoMaps test setup: fixture not ready yet (${String(error)})`,
104 ),
105 ),
106 Effect.retry({ schedule: readinessPolicy }),
107 );
108 }),
109 { timeout: 240_000 },
110 );
111
112 afterAll(sharedStack.destroy(), { timeout: 120_000 });
113
114 describe("binding registration", () => {
115 test.provider("all 5 capabilities initialize in the runtime", (_stack) =>
116 Effect.gen(function* () {
117 const response = yield* getJson("/bindings");
118 expect((response as any).bound).toHaveLength(5);
119 }),
120 );
121 });
122
123 describe("GetStaticMap", () => {
124 test.provider(
125 "renders a static map image (nonzero PNG payload)",
126 (_stack) =>
127 Effect.gen(function* () {
128 const response = (yield* getJson("/static-map")) as any;
129 expect(response.bytes).toBeGreaterThan(0);
130 expect(response.contentType).toContain("image/");
131 expect(typeof response.pricingBucket).toBe("string");
132 }),
133 { timeout: 60_000 },
134 );
135 });
136
137 describe("GetTile", () => {
138 test.provider(
139 "fetches the 0/0/0 vector basemap tile",
140 (_stack) =>
141 Effect.gen(function* () {
142 const response = (yield* getJson("/tile")) as any;
143 expect(response.bytes).toBeGreaterThan(0);
144 expect(typeof response.pricingBucket).toBe("string");
145 }),
146 { timeout: 60_000 },
147 );
148 });
149
150 describe("GetStyleDescriptor", () => {
151 test.provider(
152 "fetches the Standard MapLibre style descriptor (valid JSON)",
153 (_stack) =>
154 Effect.gen(function* () {
155 const response = (yield* getJson("/style-descriptor")) as any;
156 expect(response.bytes).toBeGreaterThan(0);
157 // MapLibre style spec version is always 8.
158 expect(response.version).toBe(8);
159 }),
160 { timeout: 60_000 },
161 );
162 });
163
164 describe("GetSprites", () => {
165 test.provider(
166 "fetches the Standard style's sprite sheet",
167 (_stack) =>
168 Effect.gen(function* () {
169 const response = (yield* getJson("/sprites")) as any;
170 expect(response.bytes).toBeGreaterThan(0);
171 }),
172 { timeout: 60_000 },
173 );
174 });
175
176 describe("GetGlyphs", () => {
177 test.provider(
178 "fetches a glyph PBF range for Amazon Ember Regular",
179 (_stack) =>
180 Effect.gen(function* () {
181 const response = (yield* getJson("/glyphs")) as any;
182 expect(response.bytes).toBeGreaterThan(0);
183 }),
184 { timeout: 60_000 },
185 );
186 });
187 },
188);