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
| 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 pricing from "@distilled.cloud/aws/pricing"; |
| 6 | import { Region as AwsRegion } from "@distilled.cloud/aws/Region"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 11 | import * as Result from "effect/Result"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import PricingTestFunctionLive, { PricingTestFunction } from "./handler"; |
| 14 | |
| 15 | const testOptions = { providers: AWS.providers() }; |
| 16 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 17 | const sharedStack = Core.scratchStack(testOptions, "PricingBindings"); |
| 18 | |
| 19 | // The Price List Query API is only served from us-east-1 — pin the region for |
| 20 | // out-of-band probes made directly from the test process. |
| 21 | const withPricingRegion = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 22 | effect.pipe(Effect.provideService(AwsRegion, Effect.succeed("us-east-1"))); |
| 23 | |
| 24 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 25 | // over 60s on a fresh deploy under parallel-suite load. |
| 26 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 27 | |
| 28 | let baseUrl: string; |
| 29 | |
| 30 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 31 | readonly status: number; |
| 32 | readonly body: string; |
| 33 | }> {} |
| 34 | |
| 35 | // Retry transient 5xx only; a genuine 4xx/assertion failure surfaces |
| 36 | // immediately. |
| 37 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 38 | HttpClient.execute(request).pipe( |
| 39 | Effect.flatMap((response) => |
| 40 | response.status >= 500 |
| 41 | ? response.text.pipe( |
| 42 | Effect.flatMap((body) => |
| 43 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 44 | ), |
| 45 | ) |
| 46 | : Effect.succeed(response), |
| 47 | ), |
| 48 | Effect.retry({ |
| 49 | while: (e) => e._tag === "TransientUpstream", |
| 50 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(5)]), |
| 51 | }), |
| 52 | ); |
| 53 | |
| 54 | // Ungated probes against the real API (read-only, near-zero cost). |
| 55 | // |
| 56 | // Observed live behavior: an unknown ServiceCode does NOT error on ANY |
| 57 | // pricing operation (GetProducts / DescribeServices / GetAttributeValues all |
| 58 | // succeed with empty results). The API's documented NotFoundException is not |
| 59 | // reachable via bogus codes; a constraint violation (MaxResults=0) is the |
| 60 | // cheap deterministic typed error, proving distilled's error mapping and the |
| 61 | // us-east-1 region pin. |
| 62 | test.provider( |
| 63 | "getProducts with a bogus ServiceCode succeeds with an empty PriceList", |
| 64 | (_stack) => |
| 65 | Effect.gen(function* () { |
| 66 | const result = yield* withPricingRegion( |
| 67 | pricing.getProducts({ ServiceCode: "AlchemyBogusServiceCodeProbe", MaxResults: 1 }), |
| 68 | ); |
| 69 | |
| 70 | expect(result.PriceList ?? []).toHaveLength(0); |
| 71 | }), |
| 72 | { tags: ["provider:aws", "provider:aws:pricing", "live"], timeout: 60_000 }, |
| 73 | ); |
| 74 | |
| 75 | test.provider( |
| 76 | "getProducts with an out-of-range MaxResults returns a typed InvalidParameterException", |
| 77 | (_stack) => |
| 78 | Effect.gen(function* () { |
| 79 | const result = yield* Effect.result( |
| 80 | withPricingRegion(pricing.getProducts({ ServiceCode: "AmazonEC2", MaxResults: 0 })), |
| 81 | ); |
| 82 | |
| 83 | expect(Result.isFailure(result)).toBe(true); |
| 84 | if (Result.isFailure(result)) { |
| 85 | expect(result.failure._tag).toBe("InvalidParameterException"); |
| 86 | } |
| 87 | }), |
| 88 | { tags: ["provider:aws", "provider:aws:pricing", "live"], timeout: 60_000 }, |
| 89 | ); |
| 90 | |
| 91 | describe( |
| 92 | "Pricing Bindings", |
| 93 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:pricing", "live"] }, |
| 94 | () => { |
| 95 | beforeAll( |
| 96 | Effect.gen(function* () { |
| 97 | yield* Effect.logInfo("Pricing test setup: destroying previous resources"); |
| 98 | yield* sharedStack.destroy(); |
| 99 | |
| 100 | yield* Effect.logInfo("Pricing test setup: deploying fixture"); |
| 101 | const { functionUrl } = yield* sharedStack.deploy( |
| 102 | Effect.gen(function* () { |
| 103 | return yield* PricingTestFunction; |
| 104 | }).pipe(Effect.provide(PricingTestFunctionLive)), |
| 105 | ); |
| 106 | |
| 107 | expect(functionUrl).toBeTruthy(); |
| 108 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 109 | const readinessUrl = `${baseUrl}/ping`; |
| 110 | |
| 111 | yield* Effect.logInfo(`Pricing test setup: probing readiness at ${readinessUrl}`); |
| 112 | yield* HttpClient.get(readinessUrl).pipe( |
| 113 | Effect.flatMap((response) => |
| 114 | response.status === 200 |
| 115 | ? Effect.succeed(response) |
| 116 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 117 | ), |
| 118 | Effect.tapError((error) => |
| 119 | Effect.logWarning(`Pricing test setup: fixture not ready yet (${String(error)})`), |
| 120 | ), |
| 121 | Effect.retry({ schedule: readinessPolicy }), |
| 122 | ); |
| 123 | }), |
| 124 | { timeout: 240_000 }, |
| 125 | ); |
| 126 | |
| 127 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 128 | |
| 129 | describe("GetProducts", () => { |
| 130 | test.provider( |
| 131 | "returns a non-empty price list for AmazonEC2 t3.micro", |
| 132 | (_stack) => |
| 133 | Effect.gen(function* () { |
| 134 | const response = (yield* send(HttpClientRequest.get(`${baseUrl}/products`)).pipe( |
| 135 | Effect.flatMap((r) => r.json), |
| 136 | )) as { |
| 137 | count: number; |
| 138 | formatVersion: string | undefined; |
| 139 | firstServiceCode: string | undefined; |
| 140 | firstInstanceType: string | undefined; |
| 141 | }; |
| 142 | |
| 143 | expect(response.count).toBeGreaterThan(0); |
| 144 | expect(response.firstServiceCode).toBe("AmazonEC2"); |
| 145 | expect(response.firstInstanceType).toBe("t3.micro"); |
| 146 | }), |
| 147 | { timeout: 120_000 }, |
| 148 | ); |
| 149 | }); |
| 150 | |
| 151 | describe("DescribeServices", () => { |
| 152 | test.provider( |
| 153 | "describes AmazonEC2 with its filterable attribute names", |
| 154 | (_stack) => |
| 155 | Effect.gen(function* () { |
| 156 | const response = (yield* send(HttpClientRequest.get(`${baseUrl}/services`)).pipe( |
| 157 | Effect.flatMap((r) => r.json), |
| 158 | )) as { services: Array<{ serviceCode: string; attributeNameCount: number }> }; |
| 159 | |
| 160 | expect(response.services.length).toBe(1); |
| 161 | expect(response.services[0].serviceCode).toBe("AmazonEC2"); |
| 162 | expect(response.services[0].attributeNameCount).toBeGreaterThan(0); |
| 163 | }), |
| 164 | { timeout: 120_000 }, |
| 165 | ); |
| 166 | }); |
| 167 | |
| 168 | describe("ListPriceLists + GetPriceListFileUrl", () => { |
| 169 | test.provider( |
| 170 | "lists EC2 price lists and presigns a bulk file URL", |
| 171 | (_stack) => |
| 172 | Effect.gen(function* () { |
| 173 | const response = (yield* send( |
| 174 | HttpClientRequest.get(`${baseUrl}/price-list-file-url`), |
| 175 | ).pipe(Effect.flatMap((r) => r.json))) as { |
| 176 | count: number; |
| 177 | priceListArn: string; |
| 178 | regionCode: string | undefined; |
| 179 | currencyCode: string | undefined; |
| 180 | fileFormats: string[]; |
| 181 | url: string | undefined; |
| 182 | }; |
| 183 | |
| 184 | expect(response.count).toBeGreaterThan(0); |
| 185 | expect(response.priceListArn).toContain("arn:aws:pricing:"); |
| 186 | expect(response.regionCode).toBe("us-east-1"); |
| 187 | expect(response.currencyCode).toBe("USD"); |
| 188 | expect(response.fileFormats.length).toBeGreaterThan(0); |
| 189 | expect(response.url).toMatch(/^https:\/\//); |
| 190 | }), |
| 191 | { timeout: 120_000 }, |
| 192 | ); |
| 193 | }); |
| 194 | |
| 195 | describe("GetAttributeValues", () => { |
| 196 | test.provider( |
| 197 | "lists volumeType attribute values for AmazonEC2", |
| 198 | (_stack) => |
| 199 | Effect.gen(function* () { |
| 200 | const response = (yield* send( |
| 201 | HttpClientRequest.get(`${baseUrl}/attribute-values`), |
| 202 | ).pipe(Effect.flatMap((r) => r.json))) as { values: string[] }; |
| 203 | |
| 204 | expect(response.values.length).toBeGreaterThan(0); |
| 205 | // gp2/gp3 are stable, long-standing EBS volume types. |
| 206 | expect(response.values.some((value) => value.startsWith("General Purpose"))).toBe(true); |
| 207 | }), |
| 208 | { timeout: 120_000 }, |
| 209 | ); |
| 210 | }); |
| 211 | }, |
| 212 | ); |