@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 157 lines 6.27 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { HostedZone, QueryLoggingConfig } from "@alchemy.run/aws/Route53";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as logs from "@distilled.cloud/aws/cloudwatch-logs";
6 import { Region as AwsRegion } from "@distilled.cloud/aws/Region";
7 import * as route53 from "@distilled.cloud/aws/route-53";
8 import * as sts from "@distilled.cloud/aws/sts";
9 import * as Effect from "effect/Effect";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 // Route 53 only delivers query logs to us-east-1 log groups, and the
15 // CloudWatch Logs resource policy that authorizes it must live there too.
16 const inUsEast1 = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
17 effect.pipe(Effect.provideService(AwsRegion, Effect.succeed("us-east-1")));
18
19 // Deterministic, test-owned names. The account resource-policy quota is 10
20 // per region and cannot be raised — this suite is quota-aware by owning a
21 // single dedicated policy (net-zero after cleanup) instead of touching any
22 // shared policy.
23 const POLICY_NAME = "alchemy-test-route53-query-logging";
24 const LOG_GROUP_A = "/aws/route53/alchemy-test-qlc-a";
25 const LOG_GROUP_B = "/aws/route53/alchemy-test-qlc-b";
26
27 const ensureLogGroup = (logGroupName: string) =>
28 inUsEast1(
29 logs
30 .createLogGroup({ logGroupName })
31 .pipe(Effect.catchTag("ResourceAlreadyExistsException", () => Effect.void)),
32 );
33
34 const removeLogGroup = (logGroupName: string) =>
35 inUsEast1(
36 logs
37 .deleteLogGroup({ logGroupName })
38 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)),
39 );
40
41 const removePolicy = inUsEast1(
42 logs
43 .deleteResourcePolicy({ policyName: POLICY_NAME })
44 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)),
45 );
46
47 const assertConfigGone = (id: string) =>
48 route53.getQueryLoggingConfig({ Id: id }).pipe(
49 Effect.flatMap(() => Effect.fail(new Error("query logging config still exists"))),
50 Effect.catchTag("NoSuchQueryLoggingConfig", () => Effect.void),
51 Effect.retry({
52 while: (e) => e instanceof Error,
53 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
54 }),
55 );
56
57 test.provider(
58 "create, replace on log-group change, and delete query logging config",
59 (stack) =>
60 Effect.gen(function* () {
61 yield* stack.destroy();
62
63 const { Account: accountId } = yield* sts.getCallerIdentity({});
64 const logGroupArnA = `arn:aws:logs:us-east-1:${accountId}:log-group:${LOG_GROUP_A}`;
65 const logGroupArnB = `arn:aws:logs:us-east-1:${accountId}:log-group:${LOG_GROUP_B}`;
66
67 const cleanup = Effect.gen(function* () {
68 yield* stack.destroy();
69 yield* removeLogGroup(LOG_GROUP_A);
70 yield* removeLogGroup(LOG_GROUP_B);
71 yield* removePolicy;
72 });
73
74 // Everything from the first out-of-band create onwards runs under
75 // `Effect.ensuring(cleanup)` so a failure at ANY point (including while
76 // provisioning the prerequisites themselves) still tears down the log
77 // groups + resource policy. All ops are idempotent (AlreadyExists /
78 // NotFound caught), so re-runs reclaim any leftovers from a crash.
79 const replacedId = yield* Effect.gen(function* () {
80 // Out-of-band us-east-1 prerequisites: two log groups and the resource
81 // policy that lets Route 53 write to them. (The Logs LogGroup /
82 // ResourcePolicy resources follow the ambient region, so the test
83 // provisions these via distilled directly.)
84 yield* ensureLogGroup(LOG_GROUP_A);
85 yield* ensureLogGroup(LOG_GROUP_B);
86 yield* inUsEast1(
87 logs.putResourcePolicy({
88 policyName: POLICY_NAME,
89 policyDocument: JSON.stringify({
90 Version: "2012-10-17",
91 Statement: [
92 {
93 Effect: "Allow",
94 Principal: { Service: "route53.amazonaws.com" },
95 Action: ["logs:CreateLogStream", "logs:PutLogEvents"],
96 Resource: `arn:aws:logs:us-east-1:${accountId}:log-group:/aws/route53/alchemy-test-qlc-*`,
97 },
98 ],
99 }),
100 }),
101 );
102
103 // Create.
104 const created = yield* stack.deploy(
105 Effect.gen(function* () {
106 const zone = yield* HostedZone("QueryLogZone", {
107 name: "alchemy-test-qlc.alchemy.",
108 forceDestroy: true,
109 });
110 return yield* QueryLoggingConfig("QueryLogging", {
111 hostedZoneId: zone.id,
112 cloudWatchLogsLogGroupArn: logGroupArnA,
113 });
114 }),
115 );
116
117 expect(created.id).toBeDefined();
118 expect(created.cloudWatchLogsLogGroupArn).toBe(logGroupArnA);
119
120 // Out-of-band verification.
121 const observed = yield* route53.getQueryLoggingConfig({
122 Id: created.id,
123 });
124 expect(observed.QueryLoggingConfig.HostedZoneId).toBe(created.hostedZoneId);
125 expect(observed.QueryLoggingConfig.CloudWatchLogsLogGroupArn).toBe(logGroupArnA);
126
127 // Changing the log group replaces the configuration (no update API).
128 const replaced = yield* stack.deploy(
129 Effect.gen(function* () {
130 const zone = yield* HostedZone("QueryLogZone", {
131 name: "alchemy-test-qlc.alchemy.",
132 forceDestroy: true,
133 });
134 return yield* QueryLoggingConfig("QueryLogging", {
135 hostedZoneId: zone.id,
136 cloudWatchLogsLogGroupArn: logGroupArnB,
137 });
138 }),
139 );
140
141 expect(replaced.id).not.toBe(created.id);
142 expect(replaced.cloudWatchLogsLogGroupArn).toBe(logGroupArnB);
143 expect(replaced.hostedZoneId).toBe(created.hostedZoneId);
144
145 // The old configuration must be gone.
146 yield* assertConfigGone(created.id);
147
148 return replaced.id;
149 }).pipe(Effect.ensuring(cleanup.pipe(Effect.ignore)));
150
151 // `cleanup` owns the successful destroy as well as failure cleanup. Do
152 // not destroy twice on the success path: under a saturated account the
153 // redundant refresh can consume the test's remaining timeout budget.
154 yield* assertConfigGone(replacedId);
155 }),
156 { tags: ["provider:aws", "provider:aws:route53", "live"], timeout: 120_000 },
157 );