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

main
ts 138 lines 5.45 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { NotificationChannel } from "@alchemy.run/aws/DevOpsGuru/NotificationChannel";
3 import { Topic } from "@alchemy.run/aws/SNS/Topic";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as devopsguru from "@distilled.cloud/aws/devops-guru";
7 import * as SNS from "@distilled.cloud/aws/sns";
8 import * as Provider from "alchemy/Provider";
9 import * as Data from "effect/Data";
10 import * as Effect from "effect/Effect";
11 import * as Schedule from "effect/Schedule";
12 import * as Stream from "effect/Stream";
13
14 const { test } = Test.make({ providers: AWS.providers() });
15
16 // Ungated typed-error probe: prove distilled returns a typed tag (never an
17 // untyped catch-all). A not-yet-onboarded account answers a bogus insight id
18 // with ResourceNotFoundException; once DevOps Guru has been onboarded the
19 // same request is rejected earlier as ValidationException — both are typed.
20 test.provider(
21 "describeInsight on a nonexistent insight fails with a typed error",
22 () =>
23 Effect.gen(function* () {
24 const error = yield* Effect.flip(
25 devopsguru.describeInsight({ Id: "alchemy-nonexistent-devopsguru-insight-probe" }),
26 );
27 expect(["ResourceNotFoundException", "ValidationException"]).toContain(error._tag);
28 }),
29 { tags: ["provider:aws", "provider:aws:devopsguru", "live"] },
30 );
31
32 const listChannels = devopsguru.listNotificationChannels.items({}).pipe(
33 Stream.runCollect,
34 Effect.map((chunk) => Array.from(chunk)),
35 );
36
37 class TopicStillExists extends Data.TaggedError("TopicStillExists") {}
38
39 // Assert-gone for the SNS topic the test deploys alongside the channel:
40 // getTopicAttributes converges to the typed NotFoundException after destroy.
41 const assertTopicDeleted = Effect.fn(function* (topicArn: string) {
42 yield* SNS.getTopicAttributes({ TopicArn: topicArn }).pipe(
43 Effect.flatMap(() => Effect.fail(new TopicStillExists())),
44 Effect.retry({
45 while: (error) => error._tag === "TopicStillExists",
46 schedule: Schedule.exponential(100),
47 times: 8,
48 }),
49 Effect.catchTag("NotFoundException", () => Effect.void),
50 );
51 });
52
53 test.provider(
54 "lifecycle: add channel, converge filters, remove",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 // Create — an SNS topic and a channel with severity filters.
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 const topic = yield* Topic("Alerts", {});
63 const channel = yield* NotificationChannel("Channel", {
64 topicArn: topic.topicArn,
65 severities: ["HIGH"],
66 });
67 return { topic, channel };
68 }),
69 );
70 expect(created.channel.id).toBeDefined();
71 expect(created.channel.topicArn).toBe(created.topic.topicArn);
72
73 // Out-of-band verification via distilled.
74 const observed = yield* listChannels;
75 const live = observed.find((c) => c.Id === created.channel.id);
76 expect(live?.Config?.Sns?.TopicArn).toBe(created.topic.topicArn);
77 expect(live?.Config?.Filters?.Severities).toEqual(["HIGH"]);
78
79 // Canonical list() coverage.
80 const provider = yield* Provider.findProvider(NotificationChannel);
81 const all = yield* provider.list();
82 expect(all.some((c) => c.id === created.channel.id)).toBe(true);
83
84 // Update — change the filters. The AWS config is immutable, so the
85 // provider converges by remove + re-add (the channel id changes, the
86 // topic stays put).
87 const updated = yield* stack.deploy(
88 Effect.gen(function* () {
89 const topic = yield* Topic("Alerts", {});
90 const channel = yield* NotificationChannel("Channel", {
91 topicArn: topic.topicArn,
92 severities: ["HIGH", "MEDIUM"],
93 messageTypes: ["NEW_INSIGHT"],
94 });
95 return { topic, channel };
96 }),
97 );
98 expect(updated.channel.topicArn).toBe(created.topic.topicArn);
99 const afterUpdate = yield* listChannels;
100 const liveUpdated = afterUpdate.find((c) => c.Id === updated.channel.id);
101 expect(liveUpdated?.Config?.Sns?.TopicArn).toBe(created.topic.topicArn);
102 expect([...(liveUpdated?.Config?.Filters?.Severities ?? [])].sort()).toEqual([
103 "HIGH",
104 "MEDIUM",
105 ]);
106 expect(liveUpdated?.Config?.Filters?.MessageTypes).toEqual(["NEW_INSIGHT"]);
107 // The old channel is gone (not duplicated).
108 expect(
109 afterUpdate.filter((c) => c.Config?.Sns?.TopicArn === created.topic.topicArn),
110 ).toHaveLength(1);
111
112 // Idempotent redeploy — no drift, id is unchanged.
113 const redeployed = yield* stack.deploy(
114 Effect.gen(function* () {
115 const topic = yield* Topic("Alerts", {});
116 const channel = yield* NotificationChannel("Channel", {
117 topicArn: topic.topicArn,
118 severities: ["HIGH", "MEDIUM"],
119 messageTypes: ["NEW_INSIGHT"],
120 });
121 return { topic, channel };
122 }),
123 );
124 expect(redeployed.channel.id).toBe(updated.channel.id);
125
126 // Destroy — the channel is removed and the SNS topic is gone.
127 yield* stack.destroy();
128 const afterDestroy = yield* listChannels;
129 expect(afterDestroy.some((c) => c.Config?.Sns?.TopicArn === created.topic.topicArn)).toBe(
130 false,
131 );
132 yield* assertTopicDeleted(created.topic.topicArn);
133 }),
134 {
135 tags: ["provider:aws", "provider:aws:devopsguru", "provider:aws:sns", "live"],
136 timeout: 180_000,
137 },
138 );