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

main
ts 203 lines 7.38 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 GeoPlacesTestFunctionLive, { GeoPlacesTestFunction } from "./handler.ts";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "GeoPlacesBindings");
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 "GeoPlaces Bindings",
44 { tags: ["provider:aws", "provider:aws:geoplaces", "provider:aws:lambda", "live"] },
45 () => {
46 beforeAll(
47 Effect.gen(function* () {
48 yield* Effect.logInfo("GeoPlaces test setup: destroying previous resources");
49 yield* sharedStack.destroy();
50
51 yield* Effect.logInfo("GeoPlaces test setup: deploying fixture");
52 const { functionUrl } = yield* sharedStack.deploy(
53 Effect.gen(function* () {
54 return yield* GeoPlacesTestFunction;
55 }).pipe(Effect.provide(GeoPlacesTestFunctionLive)),
56 );
57
58 expect(functionUrl).toBeTruthy();
59 baseUrl = functionUrl!.replace(/\/+$/, "");
60 const readinessUrl = `${baseUrl}/ping`;
61
62 yield* Effect.logInfo(`GeoPlaces test setup: probing readiness at ${readinessUrl}`);
63 yield* HttpClient.get(readinessUrl).pipe(
64 Effect.flatMap((response) =>
65 response.status === 200
66 ? Effect.succeed(response)
67 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
68 ),
69 Effect.tapError((error) =>
70 Effect.logWarning(`GeoPlaces test setup: fixture not ready yet (${String(error)})`),
71 ),
72 Effect.retry({ schedule: readinessPolicy }),
73 );
74 }),
75 { timeout: 240_000 },
76 );
77
78 afterAll(sharedStack.destroy(), { timeout: 120_000 });
79
80 describe("GeoPlaces.Autocomplete", () => {
81 test.provider(
82 "completes a partial address query",
83 (_stack) =>
84 Effect.gen(function* () {
85 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/autocomplete`)).pipe(
86 Effect.flatMap((r) => r.json),
87 )) as { count: number; firstTitle?: string };
88
89 expect(response.count).toBeGreaterThan(0);
90 expect(typeof response.firstTitle).toBe("string");
91 expect(response.firstTitle!.length).toBeGreaterThan(0);
92 }),
93 { timeout: 120_000 },
94 );
95 });
96
97 describe("GeoPlaces.Geocode", () => {
98 test.provider(
99 "geocodes an address into coordinates",
100 (_stack) =>
101 Effect.gen(function* () {
102 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/geocode`)).pipe(
103 Effect.flatMap((r) => r.json),
104 )) as { count: number; position?: number[] };
105
106 expect(response.count).toBeGreaterThan(0);
107 expect(Array.isArray(response.position)).toBe(true);
108 expect(response.position).toHaveLength(2);
109 }),
110 { timeout: 120_000 },
111 );
112 });
113
114 describe("GeoPlaces.GetPlace", () => {
115 test.provider(
116 "fetches place details by PlaceId",
117 (_stack) =>
118 Effect.gen(function* () {
119 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/get-place`)).pipe(
120 Effect.flatMap((r) => r.json),
121 )) as { placeId?: string; label?: string; pricingBucket?: string };
122
123 expect(typeof response.placeId).toBe("string");
124 expect(response.placeId!.length).toBeGreaterThan(0);
125 expect(typeof response.label).toBe("string");
126 expect(response.label!.length).toBeGreaterThan(0);
127 expect(typeof response.pricingBucket).toBe("string");
128 }),
129 { timeout: 120_000 },
130 );
131 });
132
133 describe("GeoPlaces.ReverseGeocode", () => {
134 test.provider(
135 "reverse geocodes coordinates into an address",
136 (_stack) =>
137 Effect.gen(function* () {
138 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/reverse-geocode`)).pipe(
139 Effect.flatMap((r) => r.json),
140 )) as { count: number; label?: string };
141
142 expect(response.count).toBeGreaterThan(0);
143 expect(typeof response.label).toBe("string");
144 expect(response.label!.length).toBeGreaterThan(0);
145 }),
146 { timeout: 120_000 },
147 );
148 });
149
150 describe("GeoPlaces.SearchNearby", () => {
151 test.provider(
152 "finds places around a position",
153 (_stack) =>
154 Effect.gen(function* () {
155 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/search-nearby`)).pipe(
156 Effect.flatMap((r) => r.json),
157 )) as { count: number; firstTitle?: string; pricingBucket?: string };
158
159 expect(response.count).toBeGreaterThan(0);
160 expect(typeof response.firstTitle).toBe("string");
161 expect(response.firstTitle!.length).toBeGreaterThan(0);
162 expect(typeof response.pricingBucket).toBe("string");
163 }),
164 { timeout: 120_000 },
165 );
166 });
167
168 describe("GeoPlaces.SearchText", () => {
169 test.provider(
170 "returns ranked results for a text query",
171 (_stack) =>
172 Effect.gen(function* () {
173 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/search-text`)).pipe(
174 Effect.flatMap((r) => r.json),
175 )) as { count: number; firstPosition?: number[] };
176
177 expect(response.count).toBeGreaterThan(0);
178 expect(Array.isArray(response.firstPosition)).toBe(true);
179 expect(response.firstPosition).toHaveLength(2);
180 }),
181 { timeout: 120_000 },
182 );
183 });
184
185 describe("GeoPlaces.Suggest", () => {
186 test.provider(
187 "suggests places for a free-form query",
188 (_stack) =>
189 Effect.gen(function* () {
190 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/suggest`)).pipe(
191 Effect.flatMap((r) => r.json),
192 )) as { count: number; firstTitle?: string; firstType?: string };
193
194 expect(response.count).toBeGreaterThan(0);
195 expect(typeof response.firstTitle).toBe("string");
196 expect(response.firstTitle!.length).toBeGreaterThan(0);
197 expect(typeof response.firstType).toBe("string");
198 }),
199 { timeout: 120_000 },
200 );
201 });
202 },
203 );