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 Effect from "effect/Effect"; |
| 5 | import * as HttpClient from "effect/http/HttpClient"; |
| 6 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import TimestreamTestFunctionLive, { TimestreamTestFunction } from "./handler"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: AWS.providers() }); |
| 11 | |
| 12 | // Amazon Timestream for LiveAnalytics is closed to new AWS customers; the |
| 13 | // testing account receives the typed `TimestreamNotOnboarded` gate on every |
| 14 | // operation (asserted ungated in Database.test.ts). The Lambda E2E — write |
| 15 | // time-series points through the WriteRecords binding, then read them back |
| 16 | // through the Query binding — is therefore gated behind AWS_TEST_TIMESTREAM=1 |
| 17 | // so an onboarded account can run it unchanged. |
| 18 | describe( |
| 19 | "AWS.Timestream Bindings", |
| 20 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:timestream", "live"] }, |
| 21 | () => { |
| 22 | test.provider.skipIf(!process.env.AWS_TEST_TIMESTREAM)( |
| 23 | "Lambda writes records and queries them back", |
| 24 | (stack) => |
| 25 | Effect.gen(function* () { |
| 26 | const { functionUrl } = yield* stack.deploy( |
| 27 | Effect.gen(function* () { |
| 28 | return yield* TimestreamTestFunction; |
| 29 | }).pipe(Effect.provide(TimestreamTestFunctionLive)), |
| 30 | ); |
| 31 | expect(functionUrl).toBeTruthy(); |
| 32 | const baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 33 | |
| 34 | // Write a point through the WriteRecords binding. Retry through |
| 35 | // function-URL cold start / IAM propagation. |
| 36 | const writeResponse = yield* HttpClient.execute( |
| 37 | HttpClientRequest.post(`${baseUrl}/write`).pipe( |
| 38 | HttpClientRequest.bodyJsonUnsafe({ host: "web-1", value: "42.0" }), |
| 39 | ), |
| 40 | ).pipe( |
| 41 | Effect.flatMap((response) => |
| 42 | response.status === 200 |
| 43 | ? Effect.succeed(response) |
| 44 | : Effect.fail(new Error(`write not ready: ${response.status}`)), |
| 45 | ), |
| 46 | Effect.retry({ |
| 47 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]), |
| 48 | }), |
| 49 | ); |
| 50 | const written = (yield* writeResponse.json) as { |
| 51 | recordsIngested: { Total?: number } | undefined; |
| 52 | }; |
| 53 | expect(written.recordsIngested?.Total).toBeGreaterThanOrEqual(1); |
| 54 | |
| 55 | // Query the point back through the Query binding. Memory-store writes |
| 56 | // are queryable near-immediately; retry a few times regardless. |
| 57 | const rows = yield* HttpClient.get(`${baseUrl}/query`).pipe( |
| 58 | Effect.flatMap((response) => |
| 59 | response.status === 200 |
| 60 | ? response.json |
| 61 | : Effect.fail(new Error(`query failed: ${response.status}`)), |
| 62 | ), |
| 63 | Effect.map( |
| 64 | (body) => (body as { rows: Array<{ Data: Array<{ ScalarValue?: string }> }> }).rows, |
| 65 | ), |
| 66 | Effect.flatMap((rows) => |
| 67 | Number(rows[0]?.Data[0]?.ScalarValue ?? "0") >= 1 |
| 68 | ? Effect.succeed(rows) |
| 69 | : Effect.fail(new Error("no rows counted yet")), |
| 70 | ), |
| 71 | Effect.retry({ |
| 72 | schedule: Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(10)]), |
| 73 | }), |
| 74 | ); |
| 75 | expect(Number(rows[0]!.Data[0]!.ScalarValue)).toBeGreaterThanOrEqual(1); |
| 76 | |
| 77 | // Validate the same SQL through the PrepareQuery binding. |
| 78 | const prepared = (yield* HttpClient.get(`${baseUrl}/prepare`).pipe( |
| 79 | Effect.flatMap((response) => |
| 80 | response.status === 200 |
| 81 | ? response.json |
| 82 | : Effect.fail(new Error(`prepare failed: ${response.status}`)), |
| 83 | ), |
| 84 | Effect.retry({ |
| 85 | schedule: Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(8)]), |
| 86 | }), |
| 87 | )) as { columns: Array<{ Name?: string }> }; |
| 88 | expect(prepared.columns[0]?.Name).toBe("c"); |
| 89 | |
| 90 | yield* stack.destroy(); |
| 91 | }), |
| 92 | { timeout: 600_000 }, |
| 93 | ); |
| 94 | }, |
| 95 | ); |