1import * as AWS from "@/AWS";
2import { TelemetryConfig } from "@/AWS/ObservabilityAdmin";
3import * as Test from "@/Test/Alchemy";
4import * as obs from "@distilled.cloud/aws/observabilityadmin";
5import { describe, expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { makeObservabilityAdminTestLease } from "./TestLease.ts";
8
9const { test, beforeAll, afterAll } = Test.make({
10 providers: AWS.providers(),
11});
12const testLease = makeObservabilityAdminTestLease();
13
14beforeAll(testLease.acquire, { timeout: 3_600_000 });
15afterAll(testLease.release);
16
17const readStatus = obs
18 .getTelemetryEvaluationStatus({})
19 .pipe(Effect.map((r) => r.Status ?? "NOT_STARTED"));
20
21const isOn = (status: string) => status === "RUNNING" || status === "STARTING";
22
23// The telemetry config feature is a single account-wide switch shared by
24// every test in this account — run sequentially and capture-and-restore.
25describe.sequential(
26 "AWS.ObservabilityAdmin.TelemetryConfig",
27 { tags: ["provider:aws", "provider:aws:observabilityadmin", "live"] },
28 () => {
29 test.provider(
30 "onboards, disables, re-enables, and restores the prior state on destroy",
31 (stack) =>
32 Effect.gen(function* () {
33 // Capture the account's pre-test state out-of-band; every assertion
34 // below is relative to it and destroy must restore it.
35 const before = yield* readStatus;
36
37 yield* stack.destroy();
38
39 const deployTelemetry = (enabled: boolean) =>
40 stack.deploy(
41 Effect.gen(function* () {
42 const telemetry = yield* TelemetryConfig("Telemetry", {
43 enabled,
44 });
45 return {
46 status: telemetry.status,
47 priorStatus: telemetry.priorStatus,
48 };
49 }),
50 );
51
52 // Onboard.
53 const created = yield* deployTelemetry(true);
54 expect(created.status).toBe("RUNNING");
55 expect(created.priorStatus).toBe(before);
56 expect(yield* readStatus).toBe("RUNNING");
57
58 // Disable in place — the singleton is synced, not replaced, and the
59 // captured prior status survives the update.
60 const disabled = yield* deployTelemetry(false);
61 expect(disabled.status).toBe("STOPPED");
62 expect(disabled.priorStatus).toBe(before);
63 expect(yield* readStatus).toBe("STOPPED");
64
65 // Re-enable.
66 const reenabled = yield* deployTelemetry(true);
67 expect(reenabled.status).toBe("RUNNING");
68 expect(yield* readStatus).toBe("RUNNING");
69
70 // Destroy restores the captured pre-test state: an account that was
71 // already onboarded stays RUNNING; otherwise the feature is stopped.
72 yield* stack.destroy();
73 const after = yield* readStatus;
74 if (isOn(before)) {
75 expect(after).toBe("RUNNING");
76 } else {
77 expect(["STOPPED", "NOT_STARTED"]).toContain(after);
78 }
79 }),
80 { timeout: 180_000 },
81 );
82 },
83);