The best way to control Claude Code, Codex and more from any computer or phone

main
ts 170 lines 6.62 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AnomalyMonitor } from "@alchemy.run/aws/CostExplorer/AnomalyMonitor";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as ce from "@distilled.cloud/aws/cost-explorer";
6 import { Region as AwsRegion } from "@distilled.cloud/aws/Region";
7 import * as sts from "@distilled.cloud/aws/sts";
8 import * as Provider from "alchemy/Provider";
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 // Cost Explorer is served exclusively from us-east-1.
15 const pin = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
16 effect.pipe(Effect.provideService(AwsRegion, Effect.succeed("us-east-1")));
17
18 const monitorName = "alchemy-test-anomaly-monitor";
19
20 // getAnomalyMonitors returns an empty list (NOT an error) for an unknown ARN,
21 // so absence is `AnomalyMonitors[0] === undefined`.
22 const getMonitor = (monitorArn: string) =>
23 pin(ce.getAnomalyMonitors({ MonitorArnList: [monitorArn] })).pipe(
24 Effect.map((r) => r.AnomalyMonitors[0]),
25 Effect.catchTag("UnknownMonitorException", () => Effect.succeed(undefined)),
26 );
27
28 // Typed wait-until-gone: deletion is synchronous but tolerate a short
29 // eventual-consistency window.
30 const assertMonitorGone = (monitorArn: string) =>
31 Effect.gen(function* () {
32 const found = yield* getMonitor(monitorArn);
33 if (found !== undefined) {
34 return yield* Effect.fail(new Error(`monitor '${monitorArn}' still exists`));
35 }
36 }).pipe(
37 Effect.retry({
38 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(5)]),
39 }),
40 );
41
42 // Ungated typed-error probe: prove the distilled error union carries the
43 // not-found tag this provider's delete path depends on. (GetAnomalyMonitors
44 // succeeds with an empty list for unknown ARNs — delete is the op that
45 // raises the typed tag.)
46 test.provider(
47 "deleteAnomalyMonitor on a nonexistent monitor ARN fails with UnknownMonitorException",
48 () =>
49 Effect.gen(function* () {
50 // The ARN must carry the caller's account — a foreign account ARN is
51 // rejected earlier with AccessDeniedException.
52 const identity = yield* sts.getCallerIdentity({});
53 const error = yield* Effect.flip(
54 pin(
55 ce.deleteAnomalyMonitor({
56 MonitorArn: `arn:aws:ce::${identity.Account}:anomalymonitor/00000000-0000-0000-0000-000000000000`,
57 }),
58 ),
59 );
60 expect(error._tag).toBe("UnknownMonitorException");
61 }),
62 {
63 tags: ["provider:aws", "provider:aws:costexplorer", "live"],
64 timeout: 60_000,
65 },
66 );
67
68 test.provider(
69 "lifecycle: create CUSTOM monitor with explicit name, rename in place, destroy",
70 (stack) =>
71 Effect.gen(function* () {
72 yield* stack.destroy();
73
74 // CUSTOM avoids the one-DIMENSIONAL-SERVICE-monitor-per-account quota.
75 const deployed = yield* stack.deploy(
76 Effect.gen(function* () {
77 const monitor = yield* AnomalyMonitor("Monitor", {
78 monitorName,
79 monitorType: "CUSTOM",
80 monitorSpecification: {
81 Tags: { Key: "CostCenter", Values: ["alchemy-test"] },
82 },
83 tags: { fixture: "cost-explorer-anomaly-monitor" },
84 });
85 return { monitorArn: monitor.monitorArn };
86 }),
87 );
88
89 // Out-of-band verification via distilled.
90 const created = yield* getMonitor(deployed.monitorArn);
91 expect(created?.MonitorName).toBe(monitorName);
92 expect(created?.MonitorType).toBe("CUSTOM");
93 const tags = yield* pin(ce.listTagsForResource({ ResourceArn: deployed.monitorArn }));
94 const tagRecord = Object.fromEntries((tags.ResourceTags ?? []).map((t) => [t.Key, t.Value]));
95 expect(tagRecord.fixture).toBe("cost-explorer-anomaly-monitor");
96 expect(tagRecord["alchemy::id"]).toBe("Monitor");
97
98 // Canonical list() coverage.
99 const provider = yield* Provider.findProvider(AnomalyMonitor);
100 const all = yield* provider.list();
101 expect(all.some((m) => m.monitorArn === deployed.monitorArn)).toBe(true);
102
103 // Rename — the name is mutable in place; the ARN must be stable.
104 const renamed = yield* stack.deploy(
105 Effect.gen(function* () {
106 const monitor = yield* AnomalyMonitor("Monitor", {
107 monitorName: `${monitorName}-renamed`,
108 monitorType: "CUSTOM",
109 monitorSpecification: {
110 Tags: { Key: "CostCenter", Values: ["alchemy-test"] },
111 },
112 tags: { fixture: "cost-explorer-anomaly-monitor" },
113 });
114 return { monitorArn: monitor.monitorArn };
115 }),
116 );
117 expect(renamed.monitorArn).toBe(deployed.monitorArn);
118 const afterRename = yield* getMonitor(deployed.monitorArn);
119 expect(afterRename?.MonitorName).toBe(`${monitorName}-renamed`);
120
121 // Destroy — the monitor is gone.
122 yield* stack.destroy();
123 yield* assertMonitorGone(deployed.monitorArn);
124 }),
125 {
126 tags: ["provider:aws", "provider:aws:costexplorer", "live"],
127 timeout: 120_000,
128 },
129 );
130
131 // Monitor names are unique per account and the specification is create-only,
132 // so replacement coverage uses the engine-generated physical name (a new
133 // instance ID yields a new name, letting create-before-delete succeed).
134 test.provider(
135 "replacement: changing the monitor specification replaces the monitor",
136 (stack) =>
137 Effect.gen(function* () {
138 yield* stack.destroy();
139
140 const makeMonitor = (costCenter: string) =>
141 Effect.gen(function* () {
142 const monitor = yield* AnomalyMonitor("ReplaceMonitor", {
143 monitorType: "CUSTOM",
144 monitorSpecification: {
145 Tags: { Key: "CostCenter", Values: [costCenter] },
146 },
147 });
148 return { monitorArn: monitor.monitorArn };
149 });
150
151 const deployed = yield* stack.deploy(makeMonitor("alchemy-replace-1"));
152 const created = yield* getMonitor(deployed.monitorArn);
153 expect(created?.MonitorType).toBe("CUSTOM");
154
155 // Change the specification — create-only, must replace (new ARN).
156 const replaced = yield* stack.deploy(makeMonitor("alchemy-replace-2"));
157 expect(replaced.monitorArn).not.toBe(deployed.monitorArn);
158 yield* assertMonitorGone(deployed.monitorArn);
159 const after = yield* getMonitor(replaced.monitorArn);
160 expect(after?.MonitorSpecification?.Tags?.Values).toEqual(["alchemy-replace-2"]);
161
162 // Destroy — the replacement monitor is gone.
163 yield* stack.destroy();
164 yield* assertMonitorGone(replaced.monitorArn);
165 }),
166 {
167 tags: ["provider:aws", "provider:aws:costexplorer", "live"],
168 timeout: 120_000,
169 },
170 );