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 amp from "@distilled.cloud/aws/amp"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import AmpTestFunctionLive, { AmpTestFunction } from "./handler"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "AMPBindings"); |
| 16 | |
| 17 | // Deterministic metric name — same on every run of this test file. |
| 18 | const METRIC = "alchemy_amp_bindings_test_total"; |
| 19 | |
| 20 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 21 | |
| 22 | let baseUrl: string; |
| 23 | |
| 24 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 25 | readonly status: number; |
| 26 | readonly body: string; |
| 27 | }> {} |
| 28 | |
| 29 | // Retry transient 5xx (cold re-init, IAM propagation on a fresh deploy — |
| 30 | // aps permissions can take a few seconds to become effective). |
| 31 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 32 | HttpClient.execute(request).pipe( |
| 33 | Effect.flatMap((response) => |
| 34 | response.status >= 500 |
| 35 | ? response.text.pipe( |
| 36 | Effect.flatMap((body) => |
| 37 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 38 | ), |
| 39 | ) |
| 40 | : Effect.succeed(response), |
| 41 | ), |
| 42 | Effect.retry({ |
| 43 | while: (e) => e._tag === "TransientUpstream", |
| 44 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(6)]), |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | const getJson = (path: string) => |
| 49 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 50 | |
| 51 | describe( |
| 52 | "AMP Bindings", |
| 53 | { tags: ["provider:aws", "provider:aws:amp", "provider:aws:lambda", "live"] }, |
| 54 | () => { |
| 55 | beforeAll( |
| 56 | Effect.gen(function* () { |
| 57 | yield* Effect.logInfo("AMP test setup: destroying previous resources"); |
| 58 | yield* sharedStack.destroy(); |
| 59 | |
| 60 | yield* Effect.logInfo("AMP test setup: deploying fixture"); |
| 61 | const { functionUrl } = yield* sharedStack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | return yield* AmpTestFunction; |
| 64 | }).pipe(Effect.provide(AmpTestFunctionLive)), |
| 65 | ); |
| 66 | |
| 67 | expect(functionUrl).toBeTruthy(); |
| 68 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 69 | const readinessUrl = `${baseUrl}/health`; |
| 70 | |
| 71 | yield* Effect.logInfo(`AMP 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(`AMP test setup: fixture not ready yet (${String(error)})`), |
| 80 | ), |
| 81 | Effect.retry({ schedule: readinessPolicy }), |
| 82 | ); |
| 83 | yield* Effect.logInfo("AMP test setup: fixture responded successfully"); |
| 84 | }), |
| 85 | { timeout: 240_000 }, |
| 86 | ); |
| 87 | |
| 88 | afterAll( |
| 89 | Effect.gen(function* () { |
| 90 | yield* sharedStack.destroy(); |
| 91 | // Assert gone: no workspace with the fixture's alias survives the |
| 92 | // destroy (AMP deletes asynchronously — DELETING counts as gone; a |
| 93 | // fresh run's leading destroy would reconcile any remnant anyway). |
| 94 | // The out-of-band distilled call needs the AWS providers layer |
| 95 | // (credentials), which afterAll does not provide by default. |
| 96 | const { workspaces } = yield* Core.withProviders( |
| 97 | amp.listWorkspaces({ alias: "alchemy-test-amp-bindings" }), |
| 98 | testOptions, |
| 99 | "AMPBindings", |
| 100 | ); |
| 101 | const alive = workspaces.filter( |
| 102 | (w) => w.status.statusCode !== "DELETING" && w.status.statusCode !== "DELETED", |
| 103 | ); |
| 104 | expect(alive).toHaveLength(0); |
| 105 | }), |
| 106 | { timeout: 120_000 }, |
| 107 | ); |
| 108 | |
| 109 | describe("RemoteWrite", () => { |
| 110 | test.provider("pushes a sample into the workspace", (_stack) => |
| 111 | Effect.gen(function* () { |
| 112 | const body = (yield* send( |
| 113 | HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(`${baseUrl}/remote-write`), { |
| 114 | name: METRIC, |
| 115 | labels: { source: "bindings-test" }, |
| 116 | value: 1, |
| 117 | }), |
| 118 | ).pipe(Effect.flatMap((r) => r.json))) as { success: boolean }; |
| 119 | expect(body.success).toBe(true); |
| 120 | }), |
| 121 | ); |
| 122 | }); |
| 123 | |
| 124 | describe("QueryMetrics", () => { |
| 125 | test.provider( |
| 126 | "instant query reads back the remote-written sample", |
| 127 | (_stack) => |
| 128 | Effect.gen(function* () { |
| 129 | // Write, then poll the query API until ingestion catches up. |
| 130 | yield* send( |
| 131 | HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(`${baseUrl}/remote-write`), { |
| 132 | name: METRIC, |
| 133 | labels: { source: "bindings-test" }, |
| 134 | value: 2, |
| 135 | }), |
| 136 | ); |
| 137 | |
| 138 | const body = (yield* getJson(`/query?query=${METRIC}`).pipe( |
| 139 | Effect.repeat({ |
| 140 | schedule: Schedule.spaced("3 seconds"), |
| 141 | until: (b): boolean => { |
| 142 | const result = (b as { result: { resultType: string; result: unknown[] } }) |
| 143 | .result; |
| 144 | return result.resultType === "vector" && result.result.length > 0; |
| 145 | }, |
| 146 | times: 20, |
| 147 | }), |
| 148 | )) as { |
| 149 | result: { |
| 150 | resultType: string; |
| 151 | result: Array<{ metric: Record<string, string>; value: [number, string] }>; |
| 152 | }; |
| 153 | }; |
| 154 | |
| 155 | expect(body.result.resultType).toBe("vector"); |
| 156 | const sample = body.result.result.find((s) => s.metric.__name__ === METRIC); |
| 157 | expect(sample).toBeDefined(); |
| 158 | expect(sample!.metric.source).toBe("bindings-test"); |
| 159 | expect(Number(sample!.value[1])).toBeGreaterThan(0); |
| 160 | }), |
| 161 | { timeout: 120_000 }, |
| 162 | ); |
| 163 | |
| 164 | test.provider("range query returns a matrix", (_stack) => |
| 165 | Effect.gen(function* () { |
| 166 | const body = (yield* getJson(`/query-range?query=${METRIC}`)) as { |
| 167 | result: { resultType: string }; |
| 168 | }; |
| 169 | expect(body.result.resultType).toBe("matrix"); |
| 170 | }), |
| 171 | ); |
| 172 | }); |
| 173 | |
| 174 | describe("GetLabels", () => { |
| 175 | test.provider("lists label names", (_stack) => |
| 176 | Effect.gen(function* () { |
| 177 | const body = (yield* getJson("/labels")) as { names: string[] }; |
| 178 | expect(body.names).toContain("__name__"); |
| 179 | }), |
| 180 | ); |
| 181 | |
| 182 | test.provider("lists metric names via label values", (_stack) => |
| 183 | Effect.gen(function* () { |
| 184 | const body = (yield* getJson("/label-values?label=__name__")) as { values: string[] }; |
| 185 | expect(body.values).toContain(METRIC); |
| 186 | }), |
| 187 | ); |
| 188 | }); |
| 189 | |
| 190 | describe("GetSeries", () => { |
| 191 | test.provider("matches the remote-written series", (_stack) => |
| 192 | Effect.gen(function* () { |
| 193 | const body = (yield* getJson( |
| 194 | `/series?match=${encodeURIComponent(`{__name__="${METRIC}"}`)}`, |
| 195 | )) as { series: Array<Record<string, string>> }; |
| 196 | const series = body.series.find((s) => s.__name__ === METRIC); |
| 197 | expect(series).toBeDefined(); |
| 198 | expect(series!.source).toBe("bindings-test"); |
| 199 | }), |
| 200 | ); |
| 201 | }); |
| 202 | |
| 203 | describe("DescribeWorkspace", () => { |
| 204 | test.provider("describes the bound workspace", (_stack) => |
| 205 | Effect.gen(function* () { |
| 206 | const body = (yield* getJson("/describe-workspace")) as { |
| 207 | workspaceId: string; |
| 208 | status: string; |
| 209 | }; |
| 210 | expect(body.workspaceId).toMatch(/^ws-/); |
| 211 | expect(body.status).toBe("ACTIVE"); |
| 212 | }), |
| 213 | ); |
| 214 | }); |
| 215 | |
| 216 | describe("ListWorkspaces", () => { |
| 217 | test.provider("lists workspaces including the bound one", (_stack) => |
| 218 | Effect.gen(function* () { |
| 219 | const described = (yield* getJson("/describe-workspace")) as { workspaceId: string }; |
| 220 | const body = (yield* getJson("/list-workspaces")) as { workspaceIds: string[] }; |
| 221 | expect(body.workspaceIds).toContain(described.workspaceId); |
| 222 | }), |
| 223 | ); |
| 224 | }); |
| 225 | |
| 226 | describe("GetDefaultScraperConfiguration", () => { |
| 227 | test.provider("reads the default scraper configuration YAML", (_stack) => |
| 228 | Effect.gen(function* () { |
| 229 | const body = (yield* getJson("/default-scraper-config")) as { configuration: string }; |
| 230 | // The default configuration is Prometheus scrape-config YAML. |
| 231 | expect(body.configuration).toContain("scrape_configs"); |
| 232 | }), |
| 233 | ); |
| 234 | }); |
| 235 | |
| 236 | describe("GetMetricMetadata", () => { |
| 237 | test.provider("reads metric metadata", (_stack) => |
| 238 | Effect.gen(function* () { |
| 239 | // Remote-write carries no metadata, so the map may be empty — the |
| 240 | // assertion is that the signed call succeeds and returns an object. |
| 241 | const body = (yield* getJson("/metadata")) as { metadata: Record<string, unknown> }; |
| 242 | expect(typeof body.metadata).toBe("object"); |
| 243 | }), |
| 244 | ); |
| 245 | }); |
| 246 | }, |
| 247 | ); |