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 { LogGroup } from "@alchemy.run/aws/Logs/LogGroup"; |
| 3 | import { LogStream } from "@alchemy.run/aws/Logs/LogStream"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as logs from "@distilled.cloud/aws/cloudwatch-logs"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: AWS.providers() }); |
| 12 | |
| 13 | const describeLogStream = Effect.fn(function* (logGroupName: string, logStreamName: string) { |
| 14 | const described = yield* logs |
| 15 | .describeLogStreams({ logGroupName, logStreamNamePrefix: logStreamName }) |
| 16 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed({ logStreams: [] }))); |
| 17 | return (described.logStreams ?? []).find((stream) => stream.logStreamName === logStreamName); |
| 18 | }); |
| 19 | |
| 20 | class LogStreamStillExists extends Data.TaggedError("LogStreamStillExists")<{ |
| 21 | readonly logStreamName: string; |
| 22 | }> {} |
| 23 | |
| 24 | const assertLogStreamDeleted = (logGroupName: string, logStreamName: string) => |
| 25 | describeLogStream(logGroupName, logStreamName).pipe( |
| 26 | Effect.flatMap((stream) => |
| 27 | stream === undefined ? Effect.void : Effect.fail(new LogStreamStillExists({ logStreamName })), |
| 28 | ), |
| 29 | Effect.retry({ |
| 30 | while: (e) => e._tag === "LogStreamStillExists", |
| 31 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]), |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | test.provider( |
| 36 | "create, replace on rename, delete log stream", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | yield* stack.destroy(); |
| 40 | |
| 41 | const created = yield* stack.deploy( |
| 42 | Effect.gen(function* () { |
| 43 | const logGroup = yield* LogGroup("StreamLogGroup", { |
| 44 | retention: "1 day", |
| 45 | }); |
| 46 | return yield* LogStream("AuditStream", { |
| 47 | logGroupName: logGroup.logGroupName, |
| 48 | }); |
| 49 | }), |
| 50 | ); |
| 51 | |
| 52 | expect(created.logStreamName).toBeDefined(); |
| 53 | expect(created.logStreamArn).toContain(":log-group:"); |
| 54 | |
| 55 | // out-of-band verification via distilled |
| 56 | expect(yield* describeLogStream(created.logGroupName, created.logStreamName)).toBeDefined(); |
| 57 | |
| 58 | // no-op deploy keeps the same stream |
| 59 | const noop = yield* stack.deploy( |
| 60 | Effect.gen(function* () { |
| 61 | const logGroup = yield* LogGroup("StreamLogGroup", { |
| 62 | retention: "1 day", |
| 63 | }); |
| 64 | return yield* LogStream("AuditStream", { |
| 65 | logGroupName: logGroup.logGroupName, |
| 66 | }); |
| 67 | }), |
| 68 | ); |
| 69 | expect(noop.logStreamName).toBe(created.logStreamName); |
| 70 | |
| 71 | // explicit logStreamName triggers a replacement |
| 72 | const replaced = yield* stack.deploy( |
| 73 | Effect.gen(function* () { |
| 74 | const logGroup = yield* LogGroup("StreamLogGroup", { |
| 75 | retention: "1 day", |
| 76 | }); |
| 77 | return yield* LogStream("AuditStream", { |
| 78 | logGroupName: logGroup.logGroupName, |
| 79 | logStreamName: "alchemy-test-audit-stream-renamed", |
| 80 | }); |
| 81 | }), |
| 82 | ); |
| 83 | expect(replaced.logStreamName).toBe("alchemy-test-audit-stream-renamed"); |
| 84 | expect(yield* describeLogStream(replaced.logGroupName, replaced.logStreamName)).toBeDefined(); |
| 85 | yield* assertLogStreamDeleted(created.logGroupName, created.logStreamName); |
| 86 | |
| 87 | yield* stack.destroy(); |
| 88 | yield* assertLogStreamDeleted(replaced.logGroupName, replaced.logStreamName); |
| 89 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 90 | { tags: ["provider:aws", "provider:aws:logs", "live"], timeout: 120_000 }, |
| 91 | ); |