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 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 LogsTestFunctionLive, { LogsTestFunction } from "./handler.ts"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "LogsBindings"); |
| 15 | |
| 16 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 17 | // over 60s on a fresh deploy under parallel-suite load. Budget ~150s of |
| 18 | // readiness polling. |
| 19 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 20 | |
| 21 | let baseUrl: string; |
| 22 | |
| 23 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 24 | readonly status: number; |
| 25 | readonly body: string; |
| 26 | }> {} |
| 27 | |
| 28 | class MarkerNotVisible extends Data.TaggedError("MarkerNotVisible")<{ readonly marker: string }> {} |
| 29 | |
| 30 | // The shared Lambda fixture occasionally answers a transient 5xx under |
| 31 | // parallel load (cold re-init, IAM propagation). Retry 5xx only; a genuine |
| 32 | // 4xx/assertion failure surfaces immediately. |
| 33 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 34 | HttpClient.execute(request).pipe( |
| 35 | Effect.flatMap((response) => |
| 36 | response.status >= 500 |
| 37 | ? response.text.pipe( |
| 38 | Effect.flatMap((body) => |
| 39 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 40 | ), |
| 41 | ) |
| 42 | : Effect.succeed(response), |
| 43 | ), |
| 44 | Effect.retry({ |
| 45 | while: (e) => e._tag === "TransientUpstream", |
| 46 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | const putMarker = (marker: string) => |
| 51 | send(HttpClientRequest.post(`${baseUrl}/put?message=${marker}`)).pipe( |
| 52 | Effect.flatMap((r) => r.json), |
| 53 | ); |
| 54 | |
| 55 | // FilterLogEvents observes freshly-ingested events after a short delay — |
| 56 | // poll bounded (~40s). |
| 57 | const filterUntilVisible = (marker: string) => |
| 58 | send(HttpClientRequest.get(`${baseUrl}/filter?pattern=${marker}`)).pipe( |
| 59 | Effect.flatMap((r) => r.json), |
| 60 | Effect.flatMap((body) => { |
| 61 | const messages = (body as { messages: (string | undefined)[] }).messages; |
| 62 | return messages.some((message) => message?.includes(marker)) |
| 63 | ? Effect.succeed(messages) |
| 64 | : Effect.fail(new MarkerNotVisible({ marker })); |
| 65 | }), |
| 66 | Effect.retry({ |
| 67 | while: (e) => e._tag === "MarkerNotVisible", |
| 68 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(20)]), |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | describe( |
| 73 | "Logs Bindings", |
| 74 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:logs", "live"] }, |
| 75 | () => { |
| 76 | beforeAll( |
| 77 | Effect.gen(function* () { |
| 78 | yield* Effect.logInfo("Logs test setup: destroying previous resources"); |
| 79 | yield* sharedStack.destroy(); |
| 80 | |
| 81 | yield* Effect.logInfo("Logs test setup: deploying fixture"); |
| 82 | const { functionUrl } = yield* sharedStack.deploy( |
| 83 | Effect.gen(function* () { |
| 84 | return yield* LogsTestFunction; |
| 85 | }).pipe(Effect.provide(LogsTestFunctionLive)), |
| 86 | ); |
| 87 | |
| 88 | expect(functionUrl).toBeTruthy(); |
| 89 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 90 | const readinessUrl = `${baseUrl}/get-events`; |
| 91 | |
| 92 | yield* Effect.logInfo(`Logs test setup: probing readiness at ${readinessUrl}`); |
| 93 | yield* HttpClient.get(readinessUrl).pipe( |
| 94 | Effect.flatMap((response) => |
| 95 | response.status === 200 |
| 96 | ? Effect.succeed(response) |
| 97 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 98 | ), |
| 99 | Effect.tapError((error) => |
| 100 | Effect.logWarning(`Logs test setup: fixture not ready yet (${String(error)})`), |
| 101 | ), |
| 102 | Effect.retry({ schedule: readinessPolicy }), |
| 103 | ); |
| 104 | }), |
| 105 | { timeout: 240_000 }, |
| 106 | ); |
| 107 | |
| 108 | afterAll.skipIf(!!process.env.NO_DESTROY)(sharedStack.destroy(), { timeout: 120_000 }); |
| 109 | |
| 110 | describe("PutLogEvents", () => { |
| 111 | test.provider("writes a log event through the binding", (_stack) => |
| 112 | Effect.gen(function* () { |
| 113 | const marker = `put-${crypto.randomUUID()}`; |
| 114 | const response = yield* putMarker(marker); |
| 115 | expect((response as any).ok).toBe(true); |
| 116 | expect((response as any).rejected).toBeNull(); |
| 117 | }), |
| 118 | ); |
| 119 | }); |
| 120 | |
| 121 | describe("FilterLogEvents", () => { |
| 122 | test.provider( |
| 123 | "round-trips a marker written by the fixture", |
| 124 | (_stack) => |
| 125 | Effect.gen(function* () { |
| 126 | const marker = `filter-${crypto.randomUUID()}`; |
| 127 | yield* putMarker(marker); |
| 128 | const messages = yield* filterUntilVisible(marker); |
| 129 | expect(messages.some((m) => m?.includes(marker))).toBe(true); |
| 130 | }), |
| 131 | { timeout: 90_000 }, |
| 132 | ); |
| 133 | }); |
| 134 | |
| 135 | describe("GetLogEvents", () => { |
| 136 | test.provider( |
| 137 | "reads events back from the bound stream", |
| 138 | (_stack) => |
| 139 | Effect.gen(function* () { |
| 140 | const marker = `get-${crypto.randomUUID()}`; |
| 141 | yield* putMarker(marker); |
| 142 | |
| 143 | const messages = yield* send(HttpClientRequest.get(`${baseUrl}/get-events`)).pipe( |
| 144 | Effect.flatMap((r) => r.json), |
| 145 | Effect.flatMap((body) => { |
| 146 | const found = (body as { messages: (string | undefined)[] }).messages; |
| 147 | return found.some((message) => message?.includes(marker)) |
| 148 | ? Effect.succeed(found) |
| 149 | : Effect.fail(new MarkerNotVisible({ marker })); |
| 150 | }), |
| 151 | Effect.retry({ |
| 152 | while: (e) => e._tag === "MarkerNotVisible", |
| 153 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(20)]), |
| 154 | }), |
| 155 | ); |
| 156 | expect(messages.some((m) => m?.includes(marker))).toBe(true); |
| 157 | }), |
| 158 | { timeout: 90_000 }, |
| 159 | ); |
| 160 | }); |
| 161 | |
| 162 | describe("StartQuery", () => { |
| 163 | test.provider( |
| 164 | "starts an Insights query scoped to the bound group", |
| 165 | (_stack) => |
| 166 | Effect.gen(function* () { |
| 167 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/query`)).pipe( |
| 168 | Effect.flatMap((r) => r.json), |
| 169 | ); |
| 170 | expect((response as any).queryId).toBeTruthy(); |
| 171 | }), |
| 172 | { timeout: 90_000 }, |
| 173 | ); |
| 174 | }); |
| 175 | |
| 176 | describe("GetQueryResults", () => { |
| 177 | test.provider( |
| 178 | "polls the started query to completion", |
| 179 | (_stack) => |
| 180 | Effect.gen(function* () { |
| 181 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/query`)).pipe( |
| 182 | Effect.flatMap((r) => r.json), |
| 183 | ); |
| 184 | expect((response as any).status).toBe("Complete"); |
| 185 | expect((response as any).resultCount).toBeGreaterThanOrEqual(0); |
| 186 | }), |
| 187 | { timeout: 90_000 }, |
| 188 | ); |
| 189 | }); |
| 190 | |
| 191 | describe("StopQuery", () => { |
| 192 | test.provider( |
| 193 | "stops (or observes completion of) a started query", |
| 194 | (_stack) => |
| 195 | Effect.gen(function* () { |
| 196 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/stop-query`)).pipe( |
| 197 | Effect.flatMap((r) => r.json), |
| 198 | ); |
| 199 | const body = response as { ok: boolean; stopped: boolean; error: string | null }; |
| 200 | expect(body.ok).toBe(true); |
| 201 | // Either the stop landed, or a benign completion/registration race |
| 202 | // surfaced as one of the two typed tags — anything else (e.g. |
| 203 | // AccessDenied) dies in the fixture and fails the request. |
| 204 | expect( |
| 205 | body.stopped === true || |
| 206 | body.error === "InvalidParameterException" || |
| 207 | body.error === "ResourceNotFoundException", |
| 208 | ).toBe(true); |
| 209 | }), |
| 210 | { timeout: 90_000 }, |
| 211 | ); |
| 212 | }); |
| 213 | |
| 214 | describe("GetLogRecord", () => { |
| 215 | test.provider( |
| 216 | "fetches the full record behind a query-result @ptr", |
| 217 | (_stack) => |
| 218 | Effect.gen(function* () { |
| 219 | const marker = `record-${crypto.randomUUID()}`; |
| 220 | yield* putMarker(marker); |
| 221 | // Insights sees freshly-ingested events after a short delay — |
| 222 | // poll the query route until it returns rows (bounded, ~60s). |
| 223 | const ptr = yield* send(HttpClientRequest.get(`${baseUrl}/query`)).pipe( |
| 224 | Effect.flatMap((r) => r.json), |
| 225 | Effect.flatMap((body) => { |
| 226 | const found = (body as { ptr: string | null }).ptr; |
| 227 | return found |
| 228 | ? Effect.succeed(found) |
| 229 | : Effect.fail(new MarkerNotVisible({ marker })); |
| 230 | }), |
| 231 | Effect.retry({ |
| 232 | while: (e) => e._tag === "MarkerNotVisible", |
| 233 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(10)]), |
| 234 | }), |
| 235 | ); |
| 236 | |
| 237 | const record = yield* send( |
| 238 | HttpClientRequest.get(`${baseUrl}/record?ptr=${encodeURIComponent(ptr)}`), |
| 239 | ).pipe(Effect.flatMap((r) => r.json)); |
| 240 | expect((record as any).message).toBeTruthy(); |
| 241 | }), |
| 242 | { timeout: 180_000 }, |
| 243 | ); |
| 244 | }); |
| 245 | |
| 246 | describe("GetLogGroupFields", () => { |
| 247 | test.provider( |
| 248 | "discovers fields present in the bound group", |
| 249 | (_stack) => |
| 250 | Effect.gen(function* () { |
| 251 | const marker = `fields-${crypto.randomUUID()}`; |
| 252 | yield* putMarker(marker); |
| 253 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/fields`)).pipe( |
| 254 | Effect.flatMap((r) => r.json), |
| 255 | ); |
| 256 | expect(Array.isArray((response as any).fields)).toBe(true); |
| 257 | }), |
| 258 | { timeout: 90_000 }, |
| 259 | ); |
| 260 | }); |
| 261 | |
| 262 | describe("DescribeLogStreams", () => { |
| 263 | test.provider( |
| 264 | "lists the streams of the bound group", |
| 265 | (_stack) => |
| 266 | Effect.gen(function* () { |
| 267 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/streams`)).pipe( |
| 268 | Effect.flatMap((r) => r.json), |
| 269 | ); |
| 270 | const streams = (response as { streams: string[] }).streams; |
| 271 | expect(streams).toContain("alchemy-test-bindings-stream"); |
| 272 | }), |
| 273 | { timeout: 90_000 }, |
| 274 | ); |
| 275 | }); |
| 276 | |
| 277 | describe("CreateLogStream + DeleteLogStream", () => { |
| 278 | test.provider( |
| 279 | "creates and deletes a dynamic stream through the bindings", |
| 280 | (_stack) => |
| 281 | Effect.gen(function* () { |
| 282 | const name = "alchemy-test-bindings-dynamic-stream"; |
| 283 | const response = yield* send( |
| 284 | HttpClientRequest.post(`${baseUrl}/stream-lifecycle?name=${name}`), |
| 285 | ).pipe(Effect.flatMap((r) => r.json)); |
| 286 | expect((response as any).seen).toBe(true); |
| 287 | expect((response as any).gone).toBe(true); |
| 288 | }), |
| 289 | { timeout: 90_000 }, |
| 290 | ); |
| 291 | }); |
| 292 | }, |
| 293 | ); |