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 GeoRoutesTestFunctionLive, { GeoRoutesTestFunction } from "./handler.ts"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "GeoRoutesBindings"); |
| 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 | "GeoRoutes Bindings", |
| 44 | { tags: ["provider:aws", "provider:aws:georoutes", "provider:aws:lambda", "live"] }, |
| 45 | () => { |
| 46 | beforeAll( |
| 47 | Effect.gen(function* () { |
| 48 | yield* Effect.logInfo("GeoRoutes test setup: destroying previous resources"); |
| 49 | yield* sharedStack.destroy(); |
| 50 | |
| 51 | yield* Effect.logInfo("GeoRoutes test setup: deploying fixture"); |
| 52 | const { functionUrl } = yield* sharedStack.deploy( |
| 53 | Effect.gen(function* () { |
| 54 | return yield* GeoRoutesTestFunction; |
| 55 | }).pipe(Effect.provide(GeoRoutesTestFunctionLive)), |
| 56 | ); |
| 57 | |
| 58 | expect(functionUrl).toBeTruthy(); |
| 59 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 60 | const readinessUrl = `${baseUrl}/ping`; |
| 61 | |
| 62 | yield* Effect.logInfo(`GeoRoutes 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(`GeoRoutes 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("GeoRoutes.CalculateRoutes", () => { |
| 81 | test.provider( |
| 82 | "computes a route between two points", |
| 83 | (_stack) => |
| 84 | Effect.gen(function* () { |
| 85 | const response = (yield* send( |
| 86 | HttpClientRequest.get(`${baseUrl}/calculate-routes`), |
| 87 | ).pipe(Effect.flatMap((r) => r.json))) as { |
| 88 | count: number; |
| 89 | distance?: number; |
| 90 | duration?: number; |
| 91 | }; |
| 92 | |
| 93 | expect(response.count).toBeGreaterThan(0); |
| 94 | expect(response.distance).toBeGreaterThan(0); |
| 95 | expect(response.duration).toBeGreaterThan(0); |
| 96 | }), |
| 97 | { timeout: 120_000 }, |
| 98 | ); |
| 99 | }); |
| 100 | |
| 101 | describe("GeoRoutes.CalculateIsolines", () => { |
| 102 | test.provider( |
| 103 | "computes a drive-time isoline around a point", |
| 104 | (_stack) => |
| 105 | Effect.gen(function* () { |
| 106 | const response = (yield* send( |
| 107 | HttpClientRequest.get(`${baseUrl}/calculate-isolines`), |
| 108 | ).pipe(Effect.flatMap((r) => r.json))) as { |
| 109 | count: number; |
| 110 | geometryCount: number; |
| 111 | pricingBucket?: string; |
| 112 | }; |
| 113 | |
| 114 | expect(response.count).toBeGreaterThan(0); |
| 115 | expect(response.geometryCount).toBeGreaterThan(0); |
| 116 | expect(typeof response.pricingBucket).toBe("string"); |
| 117 | }), |
| 118 | { timeout: 120_000 }, |
| 119 | ); |
| 120 | }); |
| 121 | |
| 122 | describe("GeoRoutes.CalculateRouteMatrix", () => { |
| 123 | test.provider( |
| 124 | "computes a 2x2 origin-destination matrix", |
| 125 | (_stack) => |
| 126 | Effect.gen(function* () { |
| 127 | const response = (yield* send( |
| 128 | HttpClientRequest.get(`${baseUrl}/calculate-route-matrix`), |
| 129 | ).pipe(Effect.flatMap((r) => r.json))) as { |
| 130 | rows: number; |
| 131 | columns: number; |
| 132 | firstDuration?: number; |
| 133 | errorCount: number; |
| 134 | pricingBucket?: string; |
| 135 | }; |
| 136 | |
| 137 | expect(response.rows).toBe(2); |
| 138 | expect(response.columns).toBe(2); |
| 139 | expect(response.errorCount).toBe(0); |
| 140 | expect(response.firstDuration).toBeGreaterThan(0); |
| 141 | expect(typeof response.pricingBucket).toBe("string"); |
| 142 | }), |
| 143 | { timeout: 120_000 }, |
| 144 | ); |
| 145 | }); |
| 146 | |
| 147 | describe("GeoRoutes.OptimizeWaypoints", () => { |
| 148 | test.provider( |
| 149 | "orders waypoints to minimize travel time", |
| 150 | (_stack) => |
| 151 | Effect.gen(function* () { |
| 152 | const response = (yield* send( |
| 153 | HttpClientRequest.get(`${baseUrl}/optimize-waypoints`), |
| 154 | ).pipe(Effect.flatMap((r) => r.json))) as { |
| 155 | optimizedCount: number; |
| 156 | order: string[]; |
| 157 | distance?: number; |
| 158 | duration?: number; |
| 159 | pricingBucket?: string; |
| 160 | }; |
| 161 | |
| 162 | // Origin + 2 waypoints + destination. |
| 163 | expect(response.optimizedCount).toBe(4); |
| 164 | expect(response.order).toContain("stop-1"); |
| 165 | expect(response.order).toContain("stop-2"); |
| 166 | expect(response.distance).toBeGreaterThan(0); |
| 167 | expect(response.duration).toBeGreaterThan(0); |
| 168 | expect(typeof response.pricingBucket).toBe("string"); |
| 169 | }), |
| 170 | { timeout: 120_000 }, |
| 171 | ); |
| 172 | }); |
| 173 | |
| 174 | describe("GeoRoutes.SnapToRoads", () => { |
| 175 | test.provider( |
| 176 | "snaps a GPS trace onto the road network", |
| 177 | (_stack) => |
| 178 | Effect.gen(function* () { |
| 179 | const response = (yield* send(HttpClientRequest.get(`${baseUrl}/snap-to-roads`)).pipe( |
| 180 | Effect.flatMap((r) => r.json), |
| 181 | )) as { snappedCount: number; firstConfidence?: number; pricingBucket?: string }; |
| 182 | |
| 183 | expect(response.snappedCount).toBeGreaterThan(0); |
| 184 | expect(response.firstConfidence).toBeGreaterThan(0); |
| 185 | expect(typeof response.pricingBucket).toBe("string"); |
| 186 | }), |
| 187 | { timeout: 120_000 }, |
| 188 | ); |
| 189 | }); |
| 190 | }, |
| 191 | ); |