@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 537 lines 17.4 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 LocationTestFunctionLive, { LocationTestFunction } from "./handler.ts";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "LocationBindings");
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 /**
26 * GET a fixture route, retrying transient 5xx (cold starts, Location
27 * eventual-consistency windows surfaced as handler failures).
28 */
29 const send = (route: string) =>
30 HttpClient.execute(HttpClientRequest.get(`${baseUrl}${route}`)).pipe(
31 Effect.flatMap((response) =>
32 response.status >= 500
33 ? response.text.pipe(
34 Effect.flatMap((body) =>
35 Effect.fail(new TransientUpstream({ status: response.status, body })),
36 ),
37 )
38 : Effect.succeed(response),
39 ),
40 Effect.retry({
41 while: (e) => e._tag === "TransientUpstream",
42 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(6)]),
43 }),
44 Effect.flatMap((response) => response.json),
45 );
46
47 /**
48 * GET a fixture route repeatedly until `until` holds — bounded. Location's
49 * list APIs (geofences, device positions, history) are eventually consistent
50 * with respect to writes made moments earlier.
51 */
52 const sendUntil = <T>(route: string, until: (response: T) => boolean) =>
53 send(route).pipe(
54 Effect.map((response) => response as T),
55 Effect.repeat({ schedule: Schedule.spaced("3 seconds"), until, times: 10 }),
56 );
57
58 // Sequential: the tracker tests are write-then-read (BatchUpdateDevicePosition
59 // must land before GetDevicePosition/List/BatchGet observe it), and the global
60 // vitest config runs tests concurrently by default.
61 describe.skipIf(!!process.env.FAST).sequential(
62 "AWS.Location Bindings",
63 {
64 tags: [
65 "provider:aws",
66 "provider:aws:batch",
67 "provider:aws:forecast",
68 "provider:aws:iam",
69 "provider:aws:lambda",
70 "provider:aws:location",
71 "live",
72 ],
73 },
74 () => {
75 beforeAll(
76 Effect.gen(function* () {
77 yield* Effect.logInfo("Location test setup: destroying previous resources");
78 yield* sharedStack.destroy();
79
80 yield* Effect.logInfo("Location test setup: deploying fixture");
81 const attrs = yield* sharedStack.deploy(
82 Effect.gen(function* () {
83 return yield* LocationTestFunction;
84 }).pipe(Effect.provide(LocationTestFunctionLive)),
85 );
86
87 expect(attrs.functionUrl).toBeTruthy();
88 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
89 const readinessUrl = `${baseUrl}/ping`;
90
91 yield* Effect.logInfo(`Location test setup: probing readiness at ${readinessUrl}`);
92 yield* HttpClient.get(readinessUrl).pipe(
93 Effect.flatMap((response) =>
94 response.status === 200
95 ? Effect.succeed(response)
96 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
97 ),
98 Effect.tapError((error) =>
99 Effect.logWarning(`Location test setup: fixture not ready yet (${String(error)})`),
100 ),
101 Effect.retry({ schedule: readinessPolicy }),
102 );
103
104 // AWS canary: the fresh role's inline policy can take minutes to
105 // propagate — every geo:* call 500s until it does. Poll a cheap
106 // Location-backed route until it succeeds so tests start only once
107 // IAM is live.
108 yield* Effect.logInfo("Location test setup: waiting for IAM propagation");
109 yield* HttpClient.get(`${baseUrl}/geofence/list`).pipe(
110 Effect.flatMap((response) =>
111 response.status === 200
112 ? response.json
113 : Effect.fail(new Error(`IAM not propagated: ${response.status}`)),
114 ),
115 Effect.flatMap((body) =>
116 (body as { ok?: boolean }).ok === true
117 ? Effect.void
118 : Effect.fail(new Error(`geo:* not ready: ${JSON.stringify(body)}`)),
119 ),
120 Effect.tapError((error) =>
121 Effect.logWarning(`Location test setup: geo:* not authorized yet (${String(error)})`),
122 ),
123 // IAM propagation of the fresh execution-role policy to geo:* has
124 // been observed to take >150s — give it up to ~5 minutes.
125 Effect.retry({
126 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(150)]),
127 }),
128 );
129 }),
130 { timeout: 600_000 },
131 );
132
133 afterAll(sharedStack.destroy(), { timeout: 300_000 });
134
135 describe("Location.BatchUpdateDevicePosition", () => {
136 test.provider(
137 "uploads a device position to the tracker",
138 (_stack) =>
139 Effect.gen(function* () {
140 const response = (yield* send("/tracker/update")) as { errors: number };
141 expect(response.errors).toBe(0);
142 }),
143 { timeout: 120_000 },
144 );
145 });
146
147 describe("Location.GetDevicePosition", () => {
148 test.provider(
149 "reads the device's latest position",
150 (_stack) =>
151 Effect.gen(function* () {
152 const response = (yield* send("/tracker/latest")) as {
153 deviceId?: string;
154 position?: number[];
155 };
156 expect(response.deviceId).toBe("device-1");
157 expect(response.position).toHaveLength(2);
158 }),
159 { timeout: 120_000 },
160 );
161 });
162
163 describe("Location.BatchGetDevicePosition", () => {
164 test.provider(
165 "reads several devices in one call",
166 (_stack) =>
167 Effect.gen(function* () {
168 const response = yield* sendUntil<{ found: number; errors: number }>(
169 "/tracker/batch-get",
170 (r) => r.found > 0,
171 );
172 expect(response.found).toBe(1);
173 expect(response.errors).toBe(0);
174 }),
175 { timeout: 120_000 },
176 );
177 });
178
179 describe("Location.GetDevicePositionHistory", () => {
180 test.provider(
181 "reads the device's position history",
182 (_stack) =>
183 Effect.gen(function* () {
184 const response = yield* sendUntil<{ count: number }>(
185 "/tracker/history",
186 (r) => r.count > 0,
187 );
188 expect(response.count).toBeGreaterThan(0);
189 }),
190 { timeout: 120_000 },
191 );
192 });
193
194 describe("Location.ListDevicePositions", () => {
195 test.provider(
196 "lists the tracker's device positions",
197 (_stack) =>
198 Effect.gen(function* () {
199 const response = yield* sendUntil<{ count: number; deviceIds: string[] }>(
200 "/tracker/list",
201 (r) => r.count > 0,
202 );
203 expect(response.count).toBeGreaterThan(0);
204 expect(response.deviceIds).toContain("device-1");
205 }),
206 { timeout: 120_000 },
207 );
208 });
209
210 describe("Location.VerifyDevicePosition", () => {
211 test.provider(
212 "verifies a position (or returns the typed validation error)",
213 (_stack) =>
214 Effect.gen(function* () {
215 const response = (yield* send("/tracker/verify")) as {
216 inferredState?: boolean;
217 deviceId?: string;
218 validationError?: string;
219 };
220 // Fabricated Wi-Fi signals either verify (InferredState) or are
221 // rejected with the typed ValidationException — both prove the
222 // binding, IAM grant, and typed error union.
223 expect(
224 response.inferredState === true || typeof response.validationError === "string",
225 ).toBe(true);
226 }),
227 { timeout: 120_000 },
228 );
229 });
230
231 describe("Location.PutGeofence", () => {
232 test.provider(
233 "stores a circular geofence",
234 (_stack) =>
235 Effect.gen(function* () {
236 const response = (yield* send("/geofence/put")) as { geofenceId: string };
237 expect(response.geofenceId).toBe("fence-1");
238 }),
239 { timeout: 120_000 },
240 );
241 });
242
243 describe("Location.GetGeofence", () => {
244 test.provider(
245 "reads the geofence back",
246 (_stack) =>
247 Effect.gen(function* () {
248 const response = (yield* send("/geofence/get")) as {
249 geofenceId: string;
250 status: string;
251 };
252 expect(response.geofenceId).toBe("fence-1");
253 expect(typeof response.status).toBe("string");
254 }),
255 { timeout: 120_000 },
256 );
257 });
258
259 describe("Location.ListGeofences", () => {
260 test.provider(
261 "lists the collection's geofences",
262 (_stack) =>
263 Effect.gen(function* () {
264 const response = yield* sendUntil<{ ok: boolean; count?: number }>(
265 "/geofence/list",
266 (r) => r.ok && (r.count ?? 0) > 0,
267 );
268 expect(response).toMatchObject({ ok: true });
269 expect(response.count).toBeGreaterThan(0);
270 }),
271 { timeout: 120_000 },
272 );
273 });
274
275 describe("Location.BatchPutGeofence", () => {
276 test.provider(
277 "stores several geofences in one call",
278 (_stack) =>
279 Effect.gen(function* () {
280 const response = (yield* send("/geofence/batch-put")) as {
281 successes: number;
282 errors: number;
283 };
284 expect(response.successes).toBe(1);
285 expect(response.errors).toBe(0);
286 }),
287 { timeout: 120_000 },
288 );
289 });
290
291 describe("Location.BatchEvaluateGeofences", () => {
292 test.provider(
293 "evaluates a device position against the collection",
294 (_stack) =>
295 Effect.gen(function* () {
296 const response = (yield* send("/geofence/evaluate")) as {
297 ok: boolean;
298 errors?: number;
299 };
300 expect(response).toMatchObject({ ok: true, errors: 0 });
301 }),
302 { timeout: 120_000 },
303 );
304 });
305
306 describe("Location.ForecastGeofenceEvents", () => {
307 test.provider(
308 "forecasts upcoming geofence events",
309 (_stack) =>
310 Effect.gen(function* () {
311 const response = (yield* send("/geofence/forecast")) as {
312 forecasted: number;
313 distanceUnit: string;
314 };
315 expect(response.forecasted).toBeGreaterThanOrEqual(0);
316 expect(typeof response.distanceUnit).toBe("string");
317 }),
318 { timeout: 120_000 },
319 );
320 });
321
322 describe("Location.BatchDeleteGeofence", () => {
323 test.provider(
324 "deletes geofences in one call",
325 (_stack) =>
326 Effect.gen(function* () {
327 const response = (yield* send("/geofence/batch-delete")) as { errors: number };
328 expect(response.errors).toBe(0);
329 }),
330 { timeout: 120_000 },
331 );
332 });
333
334 describe("Location.BatchDeleteDevicePositionHistory", () => {
335 test.provider(
336 "purges the device's position history",
337 (_stack) =>
338 Effect.gen(function* () {
339 const response = (yield* send("/tracker/delete-history")) as { errors: number };
340 expect(response.errors).toBe(0);
341 }),
342 { timeout: 120_000 },
343 );
344 });
345
346 describe("Location.SearchPlaceIndexForText", () => {
347 test.provider(
348 "geocodes a text query",
349 (_stack) =>
350 Effect.gen(function* () {
351 const response = (yield* send("/places/search-text")) as {
352 count: number;
353 firstLabel?: string;
354 };
355 expect(response.count).toBeGreaterThan(0);
356 expect(typeof response.firstLabel).toBe("string");
357 }),
358 { timeout: 120_000 },
359 );
360 });
361
362 describe("Location.SearchPlaceIndexForPosition", () => {
363 test.provider(
364 "reverse-geocodes a coordinate",
365 (_stack) =>
366 Effect.gen(function* () {
367 const response = (yield* send("/places/search-position")) as {
368 count: number;
369 firstLabel?: string;
370 };
371 expect(response.count).toBeGreaterThan(0);
372 expect(typeof response.firstLabel).toBe("string");
373 }),
374 { timeout: 120_000 },
375 );
376 });
377
378 describe("Location.SearchPlaceIndexForSuggestions", () => {
379 test.provider(
380 "returns typeahead suggestions",
381 (_stack) =>
382 Effect.gen(function* () {
383 const response = (yield* send("/places/suggestions")) as {
384 count: number;
385 firstText?: string;
386 };
387 expect(response.count).toBeGreaterThan(0);
388 expect(typeof response.firstText).toBe("string");
389 }),
390 { timeout: 120_000 },
391 );
392 });
393
394 describe("Location.GetPlace", () => {
395 test.provider(
396 "fetches place details by PlaceId",
397 (_stack) =>
398 Effect.gen(function* () {
399 const response = (yield* send("/places/get-place")) as {
400 ok: boolean;
401 label?: string;
402 point?: number[];
403 };
404 expect(response).toMatchObject({ ok: true });
405 expect(typeof response.label).toBe("string");
406 expect(response.point).toHaveLength(2);
407 }),
408 { timeout: 120_000 },
409 );
410 });
411
412 describe("Location.CalculateRoute", () => {
413 test.provider(
414 "calculates a route between two positions",
415 (_stack) =>
416 Effect.gen(function* () {
417 const response = (yield* send("/routes/calculate")) as {
418 distance?: number;
419 duration?: number;
420 };
421 expect(response.distance).toBeGreaterThan(0);
422 expect(response.duration).toBeGreaterThan(0);
423 }),
424 { timeout: 120_000 },
425 );
426 });
427
428 describe("Location.CalculateRouteMatrix", () => {
429 test.provider(
430 "calculates a distance matrix",
431 (_stack) =>
432 Effect.gen(function* () {
433 const response = (yield* send("/routes/matrix")) as {
434 rows: number;
435 distance?: number;
436 };
437 expect(response.rows).toBe(1);
438 expect(response.distance).toBeGreaterThan(0);
439 }),
440 { timeout: 120_000 },
441 );
442 });
443
444 describe("Location.GetMapStyleDescriptor", () => {
445 test.provider(
446 "serves the style descriptor",
447 (_stack) =>
448 Effect.gen(function* () {
449 const response = (yield* send("/map/style")) as {
450 bytes: number;
451 contentType?: string;
452 };
453 expect(response.bytes).toBeGreaterThan(0);
454 }),
455 { timeout: 120_000 },
456 );
457 });
458
459 describe("Location.GetMapGlyphs", () => {
460 test.provider(
461 "serves a glyph range",
462 (_stack) =>
463 Effect.gen(function* () {
464 const response = (yield* send("/map/glyphs")) as { bytes: number };
465 expect(response.bytes).toBeGreaterThan(0);
466 }),
467 { timeout: 120_000 },
468 );
469 });
470
471 describe("Location.GetMapSprites", () => {
472 test.provider(
473 "serves the sprite index",
474 (_stack) =>
475 Effect.gen(function* () {
476 const response = (yield* send("/map/sprites")) as { bytes: number };
477 expect(response.bytes).toBeGreaterThan(0);
478 }),
479 { timeout: 120_000 },
480 );
481 });
482
483 describe("Location.GetMapTile", () => {
484 test.provider(
485 "serves a map tile",
486 (_stack) =>
487 Effect.gen(function* () {
488 const response = (yield* send("/map/tile")) as { bytes: number };
489 expect(response.bytes).toBeGreaterThan(0);
490 }),
491 { timeout: 120_000 },
492 );
493 });
494
495 describe("Location.ListJobs", () => {
496 test.provider(
497 "lists batch metadata jobs",
498 (_stack) =>
499 Effect.gen(function* () {
500 const response = (yield* send("/jobs/list")) as { count: number };
501 expect(response.count).toBeGreaterThanOrEqual(0);
502 }),
503 { timeout: 120_000 },
504 );
505 });
506
507 describe("Location.GetJob", () => {
508 test.provider(
509 "returns the typed ResourceNotFoundException for a missing job",
510 (_stack) =>
511 Effect.gen(function* () {
512 const response = (yield* send("/jobs/get-missing")) as {
513 tag: string;
514 message?: string;
515 };
516 expect(response).toMatchObject({ tag: "ResourceNotFoundException" });
517 }),
518 { timeout: 120_000 },
519 );
520 });
521
522 describe("Location.CancelJob", () => {
523 test.provider(
524 "returns the typed ResourceNotFoundException for a missing job",
525 (_stack) =>
526 Effect.gen(function* () {
527 const response = (yield* send("/jobs/cancel-missing")) as {
528 tag: string;
529 message?: string;
530 };
531 expect(response).toMatchObject({ tag: "ResourceNotFoundException" });
532 }),
533 { timeout: 120_000 },
534 );
535 });
536 },
537 );