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

main
ts 201 lines 7.32 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AppMonitor } from "@alchemy.run/aws/RUM";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as rum from "@distilled.cloud/aws/rum";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 const findMonitor = (name: string) =>
13 rum.getAppMonitor({ Name: name }).pipe(
14 Effect.map((r) => r.AppMonitor),
15 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
16 );
17
18 class AppMonitorStillExists extends Data.TaggedError("AppMonitorStillExists")<{
19 readonly name: string;
20 }> {}
21
22 const assertMonitorDeleted = (name: string) =>
23 findMonitor(name).pipe(
24 Effect.flatMap((monitor) =>
25 monitor === undefined ? Effect.void : Effect.fail(new AppMonitorStillExists({ name })),
26 ),
27 Effect.retry({
28 while: (e) => e._tag === "AppMonitorStillExists",
29 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
30 }),
31 );
32
33 // Ungated typed-error probe: prove the distilled error union carries the
34 // not-found tag this provider's read/delete paths depend on.
35 test.provider(
36 "getAppMonitor on a nonexistent monitor fails with ResourceNotFoundException",
37 () =>
38 Effect.gen(function* () {
39 const error = yield* Effect.flip(
40 rum.getAppMonitor({ Name: "alchemy-nonexistent-rum-monitor-probe" }),
41 );
42 expect(error._tag).toBe("ResourceNotFoundException");
43 }),
44 { tags: ["provider:aws", "provider:aws:rum", "live"] },
45 );
46
47 test.provider(
48 "create, update in place, delete app monitor",
49 (stack) =>
50 Effect.gen(function* () {
51 yield* stack.destroy();
52
53 const monitor = yield* stack.deploy(
54 Effect.gen(function* () {
55 return yield* AppMonitor("TestMonitor", {
56 domain: "example.com",
57 appMonitorConfiguration: {
58 sessionSampleRate: 0.5,
59 telemetries: ["errors", "http"],
60 allowCookies: true,
61 },
62 tags: { fixture: "rum-app-monitor" },
63 });
64 }),
65 );
66
67 expect(monitor.appMonitorName).toBeDefined();
68 expect(monitor.appMonitorId).toBeDefined();
69 expect(monitor.appMonitorArn).toContain(":appmonitor/");
70
71 // out-of-band verification via distilled
72 const created = yield* findMonitor(monitor.appMonitorName);
73 expect(created?.Id).toBe(monitor.appMonitorId);
74 expect(created?.Domain).toBe("example.com");
75 expect(created?.AppMonitorConfiguration?.SessionSampleRate).toBe(0.5);
76 expect([...(created?.AppMonitorConfiguration?.Telemetries ?? [])].sort()).toEqual([
77 "errors",
78 "http",
79 ]);
80 expect(created?.AppMonitorConfiguration?.AllowCookies).toBe(true);
81 expect(created?.DataStorage?.CwLog?.CwLogEnabled ?? false).toBe(false);
82 expect(created?.CustomEvents?.Status ?? "DISABLED").toBe("DISABLED");
83 expect(created?.Tags?.fixture).toBe("rum-app-monitor");
84 expect(created?.Tags?.["alchemy::id"]).toBe("TestMonitor");
85
86 // update in place: domain, sample rate, cw logs, custom events, tags
87 const updated = yield* stack.deploy(
88 Effect.gen(function* () {
89 return yield* AppMonitor("TestMonitor", {
90 domain: "updated.example.com",
91 appMonitorConfiguration: {
92 sessionSampleRate: 1,
93 telemetries: ["errors"],
94 allowCookies: false,
95 },
96 cwLogEnabled: true,
97 customEvents: "ENABLED",
98 tags: { fixture: "rum-app-monitor", phase: "two" },
99 });
100 }),
101 );
102 // same physical monitor — update, not replace
103 expect(updated.appMonitorName).toBe(monitor.appMonitorName);
104 expect(updated.appMonitorId).toBe(monitor.appMonitorId);
105
106 const afterUpdate = yield* findMonitor(monitor.appMonitorName);
107 expect(afterUpdate?.Domain).toBe("updated.example.com");
108 expect(afterUpdate?.AppMonitorConfiguration?.SessionSampleRate).toBe(1);
109 expect(afterUpdate?.AppMonitorConfiguration?.Telemetries).toEqual(["errors"]);
110 expect(afterUpdate?.AppMonitorConfiguration?.AllowCookies).toBe(false);
111 expect(afterUpdate?.DataStorage?.CwLog?.CwLogEnabled).toBe(true);
112 expect(afterUpdate?.CustomEvents?.Status).toBe("ENABLED");
113 expect(afterUpdate?.Tags?.phase).toBe("two");
114
115 // removing a user tag converges (internal tags survive)
116 yield* stack.deploy(
117 Effect.gen(function* () {
118 return yield* AppMonitor("TestMonitor", {
119 domain: "updated.example.com",
120 appMonitorConfiguration: {
121 sessionSampleRate: 1,
122 telemetries: ["errors"],
123 allowCookies: false,
124 },
125 cwLogEnabled: true,
126 customEvents: "ENABLED",
127 tags: { fixture: "rum-app-monitor" },
128 });
129 }),
130 );
131 const afterTagRemoval = yield* findMonitor(monitor.appMonitorName);
132 expect(afterTagRemoval?.Tags?.phase).toBeUndefined();
133 expect(afterTagRemoval?.Tags?.fixture).toBe("rum-app-monitor");
134 expect(afterTagRemoval?.Tags?.["alchemy::id"]).toBe("TestMonitor");
135
136 yield* stack.destroy();
137 yield* assertMonitorDeleted(monitor.appMonitorName);
138 }),
139 { tags: ["provider:aws", "provider:aws:rum", "live"], timeout: 120_000 },
140 );
141
142 test.provider(
143 "custom name, domainList, and replacement on rename",
144 (stack) =>
145 Effect.gen(function* () {
146 yield* stack.destroy();
147
148 const first = yield* stack.deploy(
149 Effect.gen(function* () {
150 return yield* AppMonitor("NamedMonitor", {
151 appMonitorName: "alchemy-test-rum-monitor-a",
152 domainList: ["example.com", "app.example.com"],
153 });
154 }),
155 );
156 expect(first.appMonitorName).toBe("alchemy-test-rum-monitor-a");
157
158 const observed = yield* findMonitor(first.appMonitorName);
159 expect([...(observed?.DomainList ?? [])].sort()).toEqual(["app.example.com", "example.com"]);
160
161 // rename replaces the monitor
162 const second = yield* stack.deploy(
163 Effect.gen(function* () {
164 return yield* AppMonitor("NamedMonitor", {
165 appMonitorName: "alchemy-test-rum-monitor-b",
166 domainList: ["example.com", "app.example.com"],
167 });
168 }),
169 );
170 expect(second.appMonitorName).toBe("alchemy-test-rum-monitor-b");
171 expect(second.appMonitorId).not.toBe(first.appMonitorId);
172
173 // the old monitor is cleaned up by the replacement
174 yield* assertMonitorDeleted("alchemy-test-rum-monitor-a");
175
176 yield* stack.destroy();
177 yield* assertMonitorDeleted("alchemy-test-rum-monitor-b");
178 }),
179 { tags: ["provider:aws", "provider:aws:rum", "live"], timeout: 120_000 },
180 );
181
182 test.provider(
183 "rejects a monitor with both domain and domainList",
184 (stack) =>
185 Effect.gen(function* () {
186 yield* stack.destroy();
187 const result = yield* Effect.result(
188 stack.deploy(
189 Effect.gen(function* () {
190 return yield* AppMonitor("BadMonitor", {
191 domain: "example.com",
192 domainList: ["other.example.com"],
193 });
194 }),
195 ),
196 );
197 expect(result._tag).toBe("Failure");
198 yield* stack.destroy();
199 }),
200 { tags: ["provider:aws", "provider:aws:rum", "live"], timeout: 60_000 },
201 );