The best way to control Claude Code, Codex and more from any computer or phone

main
ts 232 lines 7.87 KB
Raw
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 GeoTestFunctionLive, { GeoTestFunction } from "./handler.ts";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "GeoBindings");
15
16 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
17
18 let baseUrl: string;
19
20 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
21 readonly status: number;
22 readonly body: string;
23 }> {}
24
25 const send = (request: HttpClientRequest.HttpClientRequest) =>
26 HttpClient.execute(request).pipe(
27 Effect.flatMap((response) =>
28 response.status >= 500
29 ? response.text.pipe(
30 Effect.flatMap((body) =>
31 Effect.fail(new TransientUpstream({ status: response.status, body })),
32 ),
33 )
34 : Effect.succeed(response),
35 ),
36 Effect.retry({
37 while: (e) => e._tag === "TransientUpstream",
38 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(5)]),
39 }),
40 );
41
42 describe(
43 "Geo Bindings",
44 {
45 tags: [
46 "provider:aws",
47 "provider:aws:geomaps",
48 "provider:aws:geoplaces",
49 "provider:aws:georoutes",
50 "provider:aws:lambda",
51 "live",
52 ],
53 },
54 () => {
55 beforeAll(
56 Effect.gen(function* () {
57 yield* Effect.logInfo("Geo test setup: destroying previous resources");
58 yield* sharedStack.destroy();
59
60 yield* Effect.logInfo("Geo test setup: deploying fixture");
61 const { functionUrl } = yield* sharedStack.deploy(
62 Effect.gen(function* () {
63 return yield* GeoTestFunction;
64 }).pipe(Effect.provide(GeoTestFunctionLive)),
65 );
66
67 expect(functionUrl).toBeTruthy();
68 baseUrl = functionUrl!.replace(/\/+$/, "");
69 const readinessUrl = `${baseUrl}/ping`;
70
71 yield* Effect.logInfo(`Geo 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(`Geo 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("GeoPlaces.SearchText", () => {
90 test.provider(
91 "returns ranked results for a text query",
92 (_stack) =>
93 Effect.gen(function* () {
94 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/search-text`)).pipe(
95 Effect.flatMap((r) => r.json),
96 )) as { count: number; firstPosition?: number[] };
97
98 expect(response.count).toBeGreaterThan(0);
99 expect(Array.isArray(response.firstPosition)).toBe(true);
100 expect(response.firstPosition).toHaveLength(2);
101 }),
102 { timeout: 120_000 },
103 );
104 });
105
106 describe("GeoPlaces.Geocode", () => {
107 test.provider(
108 "geocodes an address into coordinates",
109 (_stack) =>
110 Effect.gen(function* () {
111 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/geocode`)).pipe(
112 Effect.flatMap((r) => r.json),
113 )) as { count: number; position?: number[] };
114
115 expect(response.count).toBeGreaterThan(0);
116 expect(Array.isArray(response.position)).toBe(true);
117 expect(response.position).toHaveLength(2);
118 }),
119 { timeout: 120_000 },
120 );
121 });
122
123 describe("GeoPlaces.ReverseGeocode", () => {
124 test.provider(
125 "reverse geocodes coordinates into an address",
126 (_stack) =>
127 Effect.gen(function* () {
128 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/reverse-geocode`)).pipe(
129 Effect.flatMap((r) => r.json),
130 )) as { count: number; label?: string };
131
132 expect(response.count).toBeGreaterThan(0);
133 expect(typeof response.label).toBe("string");
134 expect(response.label!.length).toBeGreaterThan(0);
135 }),
136 { timeout: 120_000 },
137 );
138 });
139
140 describe("GeoRoutes.CalculateRoutes", () => {
141 test.provider(
142 "computes a route between two points",
143 (_stack) =>
144 Effect.gen(function* () {
145 const response = (yield* send(
146 HttpClientRequest.get(`${baseUrl}/calculate-routes`),
147 ).pipe(Effect.flatMap((r) => r.json))) as {
148 count: number;
149 distance?: number;
150 duration?: number;
151 };
152
153 expect(response.count).toBeGreaterThan(0);
154 expect(response.distance).toBeGreaterThan(0);
155 expect(response.duration).toBeGreaterThan(0);
156 }),
157 { timeout: 120_000 },
158 );
159 });
160
161 describe("GeoPlaces.Autocomplete", () => {
162 test.provider(
163 "completes a partial address query",
164 (_stack) =>
165 Effect.gen(function* () {
166 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/autocomplete`)).pipe(
167 Effect.flatMap((r) => r.json),
168 )) as { count: number; firstTitle?: string };
169
170 expect(response.count).toBeGreaterThan(0);
171 expect(typeof response.firstTitle).toBe("string");
172 expect(response.firstTitle!.length).toBeGreaterThan(0);
173 }),
174 { timeout: 120_000 },
175 );
176 });
177
178 describe("GeoRoutes.CalculateIsolines", () => {
179 test.provider(
180 "computes a drive-time isoline around a point",
181 (_stack) =>
182 Effect.gen(function* () {
183 const response = (yield* send(
184 HttpClientRequest.get(`${baseUrl}/calculate-isolines`),
185 ).pipe(Effect.flatMap((r) => r.json))) as {
186 count: number;
187 geometryCount: number;
188 pricingBucket?: string;
189 };
190
191 expect(response.count).toBeGreaterThan(0);
192 expect(response.geometryCount).toBeGreaterThan(0);
193 expect(typeof response.pricingBucket).toBe("string");
194 }),
195 { timeout: 120_000 },
196 );
197 });
198
199 describe("GeoMaps.GetTile", () => {
200 test.provider(
201 "fetches a vector tile at z/x/y",
202 (_stack) =>
203 Effect.gen(function* () {
204 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/get-tile`)).pipe(
205 Effect.flatMap((r) => r.json),
206 )) as { byteLength: number; contentType?: string; pricingBucket?: string };
207
208 expect(response.byteLength).toBeGreaterThan(0);
209 expect(typeof response.pricingBucket).toBe("string");
210 }),
211 { timeout: 120_000 },
212 );
213 });
214
215 describe("GeoMaps.GetStaticMap", () => {
216 test.provider(
217 "renders a static map image",
218 (_stack) =>
219 Effect.gen(function* () {
220 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/get-static-map`)).pipe(
221 Effect.flatMap((r) => r.json),
222 )) as { byteLength: number; contentType?: string; pricingBucket?: string };
223
224 expect(response.byteLength).toBeGreaterThan(0);
225 expect(response.contentType).toContain("image");
226 expect(typeof response.pricingBucket).toBe("string");
227 }),
228 { timeout: 120_000 },
229 );
230 });
231 },
232 );