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 Alchemy from "alchemy"; |
| 5 | import * as State from "alchemy/State"; |
| 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 KinesisApiFunctionLive, { KinesisApiFunction, StreamAndConsumer } from "./handler.ts"; |
| 11 | |
| 12 | const providers = AWS.providers(); |
| 13 | const state = State.localState(); |
| 14 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ providers, state }); |
| 15 | |
| 16 | const Stack = Alchemy.Stack( |
| 17 | "kinesis-bindings", |
| 18 | { providers, state }, |
| 19 | Effect.gen(function* () { |
| 20 | // Share one stream/consumer between the deployed function and the stack |
| 21 | // outputs so the test can assert the live API responses against the known |
| 22 | // names without round-tripping them through the fixture's runtime. |
| 23 | const { stream, consumer } = yield* StreamAndConsumer; |
| 24 | const fn = yield* KinesisApiFunction; |
| 25 | return { |
| 26 | url: fn.functionUrl.as<string>(), |
| 27 | streamName: stream.streamName.as<string>(), |
| 28 | consumerName: consumer.consumerName.as<string>(), |
| 29 | }; |
| 30 | }).pipe(Effect.provide(KinesisApiFunctionLive)), |
| 31 | ); |
| 32 | |
| 33 | const stack = beforeAll( |
| 34 | Effect.gen(function* () { |
| 35 | yield* Effect.logInfo("Kinesis test setup: destroying previous resources"); |
| 36 | yield* destroy(Stack); |
| 37 | |
| 38 | yield* Effect.logInfo("Kinesis test setup: deploying fixture"); |
| 39 | return yield* deploy(Stack); |
| 40 | }), |
| 41 | { timeout: 240_000 }, |
| 42 | ); |
| 43 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { timeout: 300_000 }); |
| 44 | |
| 45 | // Lambda Function URLs cold-start (DNS, init) and a fresh role's IAM grants |
| 46 | // (eventual consistency) can both take a while on the first hit. Retrying on |
| 47 | // any non-200 lets the first request wait through that window; warm calls |
| 48 | // return on the first try and never retry. |
| 49 | const readinessSchedule = Schedule.max([Schedule.fixed("4 seconds"), Schedule.recurs(10)]); |
| 50 | |
| 51 | // Lambda Function URLs come back with a trailing slash (`https://…on.aws/`). |
| 52 | // Naively concatenating `${baseUrl}${path}` would yield a double slash |
| 53 | // (`…on.aws//stream`), whose pathname (`//stream`) never matches the fixture's |
| 54 | // `/stream` route, so every request 404s and the readiness retry spins until |
| 55 | // the test times out. Strip the trailing slash before joining. |
| 56 | const urlOf = (baseUrl: string, path: string) => `${baseUrl.replace(/\/+$/, "")}${path}`; |
| 57 | |
| 58 | const getJson = (baseUrl: string, path: string) => |
| 59 | HttpClient.get(urlOf(baseUrl, path)).pipe( |
| 60 | Effect.timeout("5 seconds"), |
| 61 | Effect.flatMap((response) => |
| 62 | response.status === 200 |
| 63 | ? response.json |
| 64 | : Effect.fail(new Error(`Request failed: ${response.status}`)), |
| 65 | ), |
| 66 | Effect.retry({ schedule: readinessSchedule }), |
| 67 | ); |
| 68 | |
| 69 | const postJson = (baseUrl: string, path: string, body: unknown) => |
| 70 | HttpClient.execute( |
| 71 | HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(urlOf(baseUrl, path)), body), |
| 72 | ).pipe( |
| 73 | // Runtime routes are normally sub-second. The record-polling and sink |
| 74 | // routes can legitimately spend several seconds on bounded retries. |
| 75 | Effect.timeout("15 seconds"), |
| 76 | Effect.flatMap((response) => |
| 77 | response.status === 200 |
| 78 | ? response.json |
| 79 | : Effect.fail(new Error(`Request failed: ${response.status}`)), |
| 80 | ), |
| 81 | Effect.retry({ schedule: readinessSchedule }), |
| 82 | ); |
| 83 | |
| 84 | const getFirstShardId = (baseUrl: string) => |
| 85 | getJson(baseUrl, "/shards").pipe( |
| 86 | Effect.map((response) => (response as any).Shards?.[0]?.ShardId as string), |
| 87 | ); |
| 88 | |
| 89 | describe.sequential( |
| 90 | "Kinesis Bindings", |
| 91 | { tags: ["provider:aws", "provider:aws:kinesis", "provider:aws:lambda", "live"] }, |
| 92 | () => { |
| 93 | describe("DescribeAccountSettings", () => { |
| 94 | test( |
| 95 | "returns the account settings payload", |
| 96 | Effect.gen(function* () { |
| 97 | const { url } = yield* stack; |
| 98 | const response = yield* getJson(url, "/account-settings"); |
| 99 | if ((response as any).ok === false) { |
| 100 | expect((response as any).error).toBeTruthy(); |
| 101 | } else { |
| 102 | expect((response as any).value).toBeDefined(); |
| 103 | } |
| 104 | }), |
| 105 | ); |
| 106 | }); |
| 107 | |
| 108 | describe("DescribeLimits", () => { |
| 109 | test( |
| 110 | "returns shard and stream limits", |
| 111 | Effect.gen(function* () { |
| 112 | const { url } = yield* stack; |
| 113 | const response = yield* getJson(url, "/limits"); |
| 114 | if ((response as any).ok === false) { |
| 115 | expect((response as any).error).toBeTruthy(); |
| 116 | } else { |
| 117 | expect((response as any).value.ShardLimit).toBeGreaterThan(0); |
| 118 | } |
| 119 | }), |
| 120 | ); |
| 121 | }); |
| 122 | |
| 123 | describe("ListStreams", () => { |
| 124 | test( |
| 125 | "lists the deployed stream", |
| 126 | Effect.gen(function* () { |
| 127 | // Kinesis ListStreams is paginated and the alchemy binding wraps |
| 128 | // the single-page operation. On an account with > 100 streams our |
| 129 | // brand-new stream may simply not be on page 1. Just verify the |
| 130 | // binding returns an Array; the specific stream is verified via |
| 131 | // DescribeStream below. |
| 132 | const { url } = yield* stack; |
| 133 | const response = yield* getJson(url, "/streams"); |
| 134 | const names = (response as any).StreamNames ?? []; |
| 135 | expect(Array.isArray(names)).toBe(true); |
| 136 | }), |
| 137 | ); |
| 138 | }); |
| 139 | |
| 140 | describe("DescribeStream", () => { |
| 141 | test( |
| 142 | "describes the bound stream", |
| 143 | Effect.gen(function* () { |
| 144 | const { url, streamName } = yield* stack; |
| 145 | const response = yield* getJson(url, "/stream"); |
| 146 | expect((response as any).StreamDescription.StreamName).toBe(streamName); |
| 147 | }), |
| 148 | ); |
| 149 | }); |
| 150 | |
| 151 | describe("DescribeStreamSummary", () => { |
| 152 | test( |
| 153 | "describes the bound stream summary", |
| 154 | Effect.gen(function* () { |
| 155 | const { url, streamName } = yield* stack; |
| 156 | const response = yield* getJson(url, "/stream-summary"); |
| 157 | expect((response as any).StreamDescriptionSummary.StreamName).toBe(streamName); |
| 158 | }), |
| 159 | ); |
| 160 | }); |
| 161 | |
| 162 | describe("GetResourcePolicy", () => { |
| 163 | test( |
| 164 | "returns the stream policy or a structured error", |
| 165 | Effect.gen(function* () { |
| 166 | const { url } = yield* stack; |
| 167 | const response = yield* getJson(url, "/resource-policy"); |
| 168 | if ((response as any).ok === false) { |
| 169 | expect((response as any).error).toBeTruthy(); |
| 170 | } else { |
| 171 | expect((response as any).value).toBeDefined(); |
| 172 | } |
| 173 | }), |
| 174 | ); |
| 175 | }); |
| 176 | |
| 177 | describe("ListShards", () => { |
| 178 | test( |
| 179 | "lists shards for the stream", |
| 180 | Effect.gen(function* () { |
| 181 | const { url } = yield* stack; |
| 182 | const response = yield* getJson(url, "/shards"); |
| 183 | expect(((response as any).Shards ?? []).length).toBeGreaterThan(0); |
| 184 | }), |
| 185 | ); |
| 186 | }); |
| 187 | |
| 188 | describe("GetShardIterator", () => { |
| 189 | test( |
| 190 | "returns a shard iterator for the first shard", |
| 191 | Effect.gen(function* () { |
| 192 | const { url } = yield* stack; |
| 193 | const shardId = yield* getFirstShardId(url); |
| 194 | const response = yield* postJson(url, "/iterator", { shardId }); |
| 195 | expect((response as any).ShardIterator).toBeTruthy(); |
| 196 | }), |
| 197 | ); |
| 198 | }); |
| 199 | |
| 200 | describe("GetRecords", () => { |
| 201 | test( |
| 202 | "reads a just-written record through the shard iterator", |
| 203 | Effect.gen(function* () { |
| 204 | const { url } = yield* stack; |
| 205 | const shardId = yield* getFirstShardId(url); |
| 206 | const marker = `records-${crypto.randomUUID()}`; |
| 207 | const response = yield* postJson(url, "/records", { |
| 208 | shardId, |
| 209 | partitionKey: "records-test", |
| 210 | data: marker, |
| 211 | }); |
| 212 | const records = (response as any).records ?? []; |
| 213 | expect(records.some((record: any) => record.data === marker)).toBe(true); |
| 214 | }), |
| 215 | ); |
| 216 | }); |
| 217 | |
| 218 | describe("ListStreamConsumers", () => { |
| 219 | test( |
| 220 | "lists the registered consumer", |
| 221 | Effect.gen(function* () { |
| 222 | const { url, consumerName } = yield* stack; |
| 223 | const response = yield* getJson(url, "/stream-consumers"); |
| 224 | const consumers = (response as any).Consumers ?? []; |
| 225 | expect(consumers.some((consumer: any) => consumer.ConsumerName === consumerName)).toBe( |
| 226 | true, |
| 227 | ); |
| 228 | }), |
| 229 | ); |
| 230 | }); |
| 231 | |
| 232 | describe("DescribeStreamConsumer", () => { |
| 233 | test( |
| 234 | "describes the registered consumer", |
| 235 | Effect.gen(function* () { |
| 236 | const { url, consumerName } = yield* stack; |
| 237 | const response = yield* getJson(url, "/consumer"); |
| 238 | expect((response as any).ConsumerDescription.ConsumerName).toBe(consumerName); |
| 239 | }), |
| 240 | ); |
| 241 | }); |
| 242 | |
| 243 | describe("SubscribeToShard", () => { |
| 244 | test( |
| 245 | "opens a subscribe-to-shard stream", |
| 246 | Effect.gen(function* () { |
| 247 | const { url } = yield* stack; |
| 248 | const shardId = yield* getFirstShardId(url); |
| 249 | const response = yield* postJson(url, "/subscribe", { shardId }); |
| 250 | expect((response as any).ok).toBe(true); |
| 251 | }), |
| 252 | ); |
| 253 | }); |
| 254 | |
| 255 | describe("ListTagsForResource", () => { |
| 256 | test( |
| 257 | "lists the stream ownership tags", |
| 258 | Effect.gen(function* () { |
| 259 | const { url } = yield* stack; |
| 260 | const response = yield* getJson(url, "/tags"); |
| 261 | const keys = ((response as any).Tags ?? []).map((tag: any) => tag.Key); |
| 262 | expect(keys).toContain("alchemy::stack"); |
| 263 | expect(keys).toContain("alchemy::stage"); |
| 264 | expect(keys).toContain("alchemy::id"); |
| 265 | expect(keys).toContain("fixture"); |
| 266 | }), |
| 267 | ); |
| 268 | }); |
| 269 | |
| 270 | describe("PutRecord", () => { |
| 271 | test( |
| 272 | "writes a single record", |
| 273 | Effect.gen(function* () { |
| 274 | const { url } = yield* stack; |
| 275 | const response = yield* postJson(url, "/put-record", { |
| 276 | partitionKey: "put-record", |
| 277 | data: `put-record-${crypto.randomUUID()}`, |
| 278 | }); |
| 279 | expect((response as any).ShardId).toBeTruthy(); |
| 280 | expect((response as any).SequenceNumber).toBeTruthy(); |
| 281 | }), |
| 282 | ); |
| 283 | }); |
| 284 | |
| 285 | describe("PutRecords", () => { |
| 286 | test( |
| 287 | "writes a batch of records", |
| 288 | Effect.gen(function* () { |
| 289 | const { url } = yield* stack; |
| 290 | const response = yield* postJson(url, "/put-records", { |
| 291 | records: [ |
| 292 | { partitionKey: "put-records", data: `batch-1-${crypto.randomUUID()}` }, |
| 293 | { partitionKey: "put-records", data: `batch-2-${crypto.randomUUID()}` }, |
| 294 | ], |
| 295 | }); |
| 296 | expect((response as any).FailedRecordCount ?? 0).toBe(0); |
| 297 | expect(((response as any).Records ?? []).length).toBe(2); |
| 298 | }), |
| 299 | ); |
| 300 | }); |
| 301 | |
| 302 | describe("StreamSink", () => { |
| 303 | test( |
| 304 | "writes records through the sink helper", |
| 305 | Effect.gen(function* () { |
| 306 | const { url } = yield* stack; |
| 307 | const response = yield* postJson(url, "/sink", { |
| 308 | records: [ |
| 309 | { partitionKey: "sink", data: `sink-1-${crypto.randomUUID()}` }, |
| 310 | { partitionKey: "sink", data: `sink-2-${crypto.randomUUID()}` }, |
| 311 | ], |
| 312 | }); |
| 313 | expect((response as any).ok).toBe(true); |
| 314 | }), |
| 315 | ); |
| 316 | |
| 317 | test( |
| 318 | "splits more than 500 records into multiple PutRecords calls", |
| 319 | Effect.gen(function* () { |
| 320 | const { url } = yield* stack; |
| 321 | // 501 records > the PutRecords limit of 500, so the batched sink |
| 322 | // must split the chunk into 2 sequential API calls (500 + 1). Any |
| 323 | // per-record throttling failures are retried by the sink engine. |
| 324 | const marker = crypto.randomUUID(); |
| 325 | const response = yield* postJson(url, "/sink", { |
| 326 | records: Array.from({ length: 501 }, (_, i) => ({ |
| 327 | partitionKey: `sink-${i % 7}`, |
| 328 | data: `sink-${marker}-${i}`, |
| 329 | })), |
| 330 | }); |
| 331 | expect((response as any).ok).toBe(true); |
| 332 | }), |
| 333 | { timeout: 120_000 }, |
| 334 | ); |
| 335 | }); |
| 336 | |
| 337 | // Shard-topology tests run LAST (describe.sequential) because they change |
| 338 | // the stream's shard layout: earlier tests assume `Shards[0]` is the |
| 339 | // single open shard the fixture was created with. |
| 340 | describe("SplitShard", () => { |
| 341 | test( |
| 342 | "splits the open shard into two children", |
| 343 | Effect.gen(function* () { |
| 344 | const { url } = yield* stack; |
| 345 | |
| 346 | const openShards = yield* getOpenShards(url); |
| 347 | expect(openShards.length).toBe(1); |
| 348 | const shard = openShards[0]!; |
| 349 | |
| 350 | // Split at the midpoint of the shard's hash-key range. |
| 351 | const start = BigInt(shard.HashKeyRange.StartingHashKey); |
| 352 | const end = BigInt(shard.HashKeyRange.EndingHashKey); |
| 353 | const midpoint = (start + end) / 2n + 1n; |
| 354 | |
| 355 | const response = yield* postJson(url, "/split-shard", { |
| 356 | shardToSplit: shard.ShardId, |
| 357 | newStartingHashKey: midpoint.toString(), |
| 358 | }); |
| 359 | expect((response as any).ok).toBe(true); |
| 360 | |
| 361 | // The split completes asynchronously: the stream transitions |
| 362 | // UPDATING -> ACTIVE and the parent shard closes, leaving two |
| 363 | // open children. |
| 364 | const children = yield* waitForOpenShardCount(url, 2); |
| 365 | for (const child of children) { |
| 366 | expect(child.ParentShardId).toBe(shard.ShardId); |
| 367 | } |
| 368 | }), |
| 369 | { timeout: 120_000 }, |
| 370 | ); |
| 371 | }); |
| 372 | |
| 373 | describe("MergeShards", () => { |
| 374 | test( |
| 375 | "merges the two adjacent children back into one shard", |
| 376 | Effect.gen(function* () { |
| 377 | const { url } = yield* stack; |
| 378 | |
| 379 | const openShards = yield* waitForOpenShardCount(url, 2); |
| 380 | // Order the two children by hash-key range so they are passed as |
| 381 | // (shard, adjacent-shard) the way MergeShards expects. |
| 382 | const [low, high] = [...openShards].sort((a, b) => |
| 383 | BigInt(a.HashKeyRange.StartingHashKey) < BigInt(b.HashKeyRange.StartingHashKey) |
| 384 | ? -1 |
| 385 | : 1, |
| 386 | ); |
| 387 | |
| 388 | const response = yield* postJson(url, "/merge-shards", { |
| 389 | shardToMerge: low!.ShardId, |
| 390 | adjacentShardToMerge: high!.ShardId, |
| 391 | }); |
| 392 | expect((response as any).ok).toBe(true); |
| 393 | |
| 394 | const merged = yield* waitForOpenShardCount(url, 1); |
| 395 | expect(merged[0]!.ParentShardId).toBe(low!.ShardId); |
| 396 | }), |
| 397 | { timeout: 120_000 }, |
| 398 | ); |
| 399 | }); |
| 400 | }, |
| 401 | ); |
| 402 | |
| 403 | interface ShardInfo { |
| 404 | ShardId: string; |
| 405 | ParentShardId?: string; |
| 406 | HashKeyRange: { StartingHashKey: string; EndingHashKey: string }; |
| 407 | SequenceNumberRange: { EndingSequenceNumber?: string }; |
| 408 | } |
| 409 | |
| 410 | // A shard is open while its sequence-number range has no upper bound. |
| 411 | const getOpenShards = (baseUrl: string) => |
| 412 | getJson(baseUrl, "/shards").pipe( |
| 413 | Effect.map((response) => |
| 414 | (((response as any).Shards ?? []) as ShardInfo[]).filter( |
| 415 | (shard) => shard.SequenceNumberRange?.EndingSequenceNumber === undefined, |
| 416 | ), |
| 417 | ), |
| 418 | ); |
| 419 | |
| 420 | const waitForOpenShardCount = (baseUrl: string, count: number) => |
| 421 | getOpenShards(baseUrl).pipe( |
| 422 | Effect.repeat({ |
| 423 | schedule: Schedule.spaced("5 seconds"), |
| 424 | until: (shards): boolean => shards.length === count, |
| 425 | times: 20, |
| 426 | }), |
| 427 | Effect.flatMap((shards) => |
| 428 | shards.length === count |
| 429 | ? Effect.succeed(shards) |
| 430 | : Effect.fail( |
| 431 | new Error(`expected ${count} open shards, saw ${shards.length} after polling`), |
| 432 | ), |
| 433 | ), |
| 434 | ); |