@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, 04:45 UTC

main
ts 123 lines 5 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { TelemetryRule } from "@alchemy.run/aws/ObservabilityAdmin";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as obs from "@distilled.cloud/aws/observabilityadmin";
6 import type * as Duration from "effect/Duration";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import { makeObservabilityAdminTestLease } from "./TestLease.ts";
10
11 const { test, beforeAll, afterAll } = Test.make({ providers: AWS.providers() });
12 const testLease = makeObservabilityAdminTestLease();
13
14 beforeAll(testLease.acquire, { timeout: 3_600_000 });
15 afterAll(testLease.release);
16
17 const readStatus = obs
18 .getTelemetryEvaluationStatus({})
19 .pipe(Effect.map((r) => r.Status ?? "NOT_STARTED"));
20
21 // Bounded wait for the account-wide telemetry config toggle to settle.
22 const awaitSettled = readStatus.pipe(
23 Effect.repeat({
24 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
25 until: (status): boolean => status !== "STARTING" && status !== "STOPPING",
26 }),
27 );
28
29 const isOn = (status: string): boolean => status === "RUNNING" || status === "STARTING";
30
31 /** Restore the account's telemetry config onboarding to its pre-test state. */
32 const restoreTo = (prior: string) =>
33 Effect.gen(function* () {
34 const settled = yield* awaitSettled;
35 if (isOn(prior) && !isOn(settled)) {
36 yield* obs.startTelemetryEvaluation({});
37 yield* awaitSettled;
38 }
39 if (!isOn(prior) && isOn(settled)) {
40 yield* obs.stopTelemetryEvaluation({});
41 yield* awaitSettled;
42 }
43 });
44
45 // Telemetry rules require the account-wide telemetry config feature, so run
46 // sequentially and capture-and-restore the onboarding state.
47 describe.sequential(
48 "AWS.ObservabilityAdmin.TelemetryRule",
49 { tags: ["provider:aws", "provider:aws:observabilityadmin", "live"] },
50 () => {
51 test.provider(
52 "creates, updates in place, and deletes a telemetry rule",
53 (stack) =>
54 Effect.gen(function* () {
55 // Capture the account's pre-test onboarding state; rules need the
56 // feature RUNNING, and destroy must restore whatever was there.
57 const before = yield* awaitSettled;
58
59 yield* Effect.gen(function* () {
60 if (!isOn(before)) {
61 yield* obs.startTelemetryEvaluation({});
62 yield* awaitSettled;
63 }
64
65 yield* stack.destroy();
66
67 const deployRule = (retention: Duration.Input) =>
68 stack.deploy(
69 Effect.gen(function* () {
70 // The rule exists for well under the ~24h AWS Config
71 // discovery window, so it never actually configures flow
72 // logs (same shape as the terraform-provider-aws
73 // acceptance tests).
74 const rule = yield* TelemetryRule("Rule", {
75 resourceType: "AWS::EC2::VPC",
76 telemetryType: "Logs",
77 telemetrySourceTypes: ["VPC_FLOW_LOGS"],
78 destinationConfiguration: {
79 DestinationType: "cloud-watch-logs",
80 Retention: retention,
81 },
82 });
83 return {
84 ruleName: rule.ruleName,
85 ruleArn: rule.ruleArn,
86 telemetryType: rule.telemetryType,
87 resourceType: rule.resourceType,
88 };
89 }),
90 );
91
92 // Create.
93 const created = yield* deployRule("30 days");
94 expect(created.ruleName).toBeTruthy();
95 expect(created.ruleArn).toContain("telemetry-rule");
96 expect(created.telemetryType).toBe("Logs");
97 expect(created.resourceType).toBe("AWS::EC2::VPC");
98 const got = yield* obs.getTelemetryRule({ RuleIdentifier: created.ruleName });
99 expect(got.TelemetryRule?.DestinationConfiguration?.RetentionInDays).toBe(30);
100
101 // Update in place — the Duration prop lands as whole wire days and
102 // the ARN is stable (no replacement).
103 const updated = yield* deployRule("60 days");
104 expect(updated.ruleArn).toBe(created.ruleArn);
105 const got2 = yield* obs.getTelemetryRule({ RuleIdentifier: created.ruleName });
106 expect(got2.TelemetryRule?.DestinationConfiguration?.RetentionInDays).toBe(60);
107
108 // Destroy — the rule is gone (typed check, not a catch-all).
109 yield* stack.destroy();
110 const gone = yield* obs.getTelemetryRule({ RuleIdentifier: created.ruleName }).pipe(
111 Effect.map(() => false),
112 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
113 );
114 expect(gone).toBe(true);
115 }).pipe(
116 // Always restore the account's onboarding state, even on failure.
117 Effect.ensuring(restoreTo(before).pipe(Effect.ignore)),
118 );
119 }),
120 { timeout: 180_000 },
121 );
122 },
123 );