@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, 06:00 UTC

main
ts 273 lines 10.1 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
3 import { Topic } from "@alchemy.run/aws/SNS";
4 import { describe, expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as SNS from "@distilled.cloud/aws/sns";
7 import { adopt } from "alchemy/AdoptPolicy";
8 import * as Drift from "alchemy/Drift";
9 import * as Provider from "alchemy/Provider";
10 import { State } from "alchemy/State";
11 import * as Data from "effect/Data";
12 import * as Effect from "effect/Effect";
13 import * as Schedule from "effect/Schedule";
14
15 const { test } = Test.make({ providers: AWS.providers() });
16
17 // Idempotent out-of-band delete for topics that adoption tests leave
18 // unmanaged (state wiped) mid-test. DeleteTopic is idempotent per AWS, but
19 // tolerate NotFoundException defensively.
20 const deleteTopicIdempotent = (topicArn: string) =>
21 SNS.deleteTopic({ TopicArn: topicArn }).pipe(
22 Effect.catchTag("NotFoundException", () => Effect.void),
23 Effect.catchTag("InvalidParameterException", () => Effect.void),
24 // Best-effort out-of-band cleanup finalizer: any unexpected error is a
25 // defect (surfaces a genuinely stuck delete) so the error channel is
26 // `never` and this is a valid `Effect.ensuring` finalizer.
27 Effect.orDie,
28 );
29
30 const topicArnFor = Effect.fn(function* (topicName: string) {
31 const { accountId, region } = yield* AWSEnvironment.current;
32 return `arn:aws:sns:${region}:${accountId}:${topicName}`;
33 });
34
35 describe("AWS.SNS.Topic", { tags: ["provider:aws", "provider:aws:sns", "live"] }, () => {
36 test.provider("create and delete topic with default props", (stack) =>
37 Effect.gen(function* () {
38 yield* stack.destroy();
39
40 const topic = yield* stack.deploy(
41 Effect.gen(function* () {
42 return yield* Topic("DefaultTopic");
43 }),
44 );
45
46 expect(topic.topicName).toBeDefined();
47 expect(topic.topicArn).toBeDefined();
48
49 const attributes = yield* SNS.getTopicAttributes({ TopicArn: topic.topicArn });
50 expect(attributes.Attributes?.TopicArn).toBe(topic.topicArn);
51
52 yield* stack.destroy();
53 yield* assertTopicDeleted(topic.topicArn);
54 }),
55 );
56
57 test.provider("create, update, delete topic attributes and tags", (stack) =>
58 Effect.gen(function* () {
59 yield* stack.destroy();
60
61 const topic = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* Topic("ManagedTopic", {
64 attributes: { DisplayName: "managed-topic-v1" },
65 tags: { env: "test" },
66 });
67 }),
68 );
69
70 const initialAttributes = yield* SNS.getTopicAttributes({ TopicArn: topic.topicArn });
71 expect(initialAttributes.Attributes?.DisplayName).toBe("managed-topic-v1");
72
73 // #1968: SNS-filled attributes (Policy, Owner, counters) are not drift,
74 // but an out-of-band change to a declared attribute is.
75 const target = { name: stack.name, stage: stack.stage };
76 const clean = yield* Drift.detect(target).pipe(Effect.provide(stack.state));
77 expect(clean.resources.ManagedTopic?.action).toBe("unchanged");
78 yield* SNS.setTopicAttributes({
79 TopicArn: topic.topicArn,
80 AttributeName: "DisplayName",
81 AttributeValue: "changed-out-of-band",
82 });
83 const drifted = yield* Drift.detect(target).pipe(Effect.provide(stack.state));
84 expect(drifted.resources.ManagedTopic).toMatchObject({
85 action: "drifted",
86 attr: { attributes: { DisplayName: "changed-out-of-band" } },
87 });
88
89 const updatedTopic = yield* stack.deploy(
90 Effect.gen(function* () {
91 return yield* Topic("ManagedTopic", {
92 attributes: { DisplayName: "managed-topic-v2" },
93 tags: { updated: "true" },
94 });
95 }),
96 );
97
98 const updatedAttributes = yield* SNS.getTopicAttributes({ TopicArn: updatedTopic.topicArn });
99 expect(updatedAttributes.Attributes?.DisplayName).toBe("managed-topic-v2");
100
101 const tagResponse = yield* SNS.listTagsForResource({ ResourceArn: updatedTopic.topicArn });
102 const tags = Object.fromEntries((tagResponse.Tags ?? []).map((tag) => [tag.Key, tag.Value]));
103 expect(tags.updated).toBe("true");
104 expect(tags.env).toBeUndefined();
105
106 yield* stack.destroy();
107 yield* assertTopicDeleted(updatedTopic.topicArn);
108 }),
109 );
110
111 test.provider("create and delete fifo topic", (stack) =>
112 Effect.gen(function* () {
113 yield* stack.destroy();
114
115 const topic = yield* stack.deploy(
116 Effect.gen(function* () {
117 return yield* Topic("FifoTopic", {
118 fifo: true,
119 attributes: { ContentBasedDeduplication: "true" },
120 });
121 }),
122 );
123
124 expect(topic.topicName).toContain(".fifo");
125
126 const attributes = yield* SNS.getTopicAttributes({ TopicArn: topic.topicArn });
127 expect(attributes.Attributes?.FifoTopic).toBe("true");
128 expect(attributes.Attributes?.ContentBasedDeduplication).toBe("true");
129
130 // #1968: SNS rejects the data protection policy read on FIFO topics;
131 // that must not make the topic read as missing.
132 const clean = yield* Drift.detect({ name: stack.name, stage: stack.stage }).pipe(
133 Effect.provide(stack.state),
134 );
135 expect(clean.resources.FifoTopic?.action).toBe("unchanged");
136
137 yield* stack.destroy();
138 yield* assertTopicDeleted(topic.topicArn);
139 }),
140 );
141
142 // Engine-level adoption tests for SNS Topic.
143 test.provider(
144 "owned topic (matching alchemy tags) is silently adopted without --adopt",
145 (stack) =>
146 Effect.gen(function* () {
147 yield* stack.destroy();
148
149 // Deterministic name so a re-run reclaims any orphan from a
150 // previously-killed run instead of creating a new one.
151 const topicName = "alchemy-test-sns-adopt";
152 const topicArn = yield* topicArnFor(topicName);
153
154 // Pre-clean: reclaim a leftover unmanaged topic from a prior run.
155 yield* deleteTopicIdempotent(topicArn);
156
157 yield* Effect.gen(function* () {
158 const initial = yield* stack.deploy(
159 Effect.gen(function* () {
160 return yield* Topic("AdoptableTopic", { topicName });
161 }),
162 );
163 expect(initial.topicName).toEqual(topicName);
164
165 // Wipe state — the topic stays in SNS (unmanaged from here until
166 // the re-deploy below adopts it; the ensuring below covers that
167 // window on failure/interruption).
168 yield* Effect.gen(function* () {
169 const state = yield* yield* State;
170 yield* state.delete({ stack: stack.name, stage: stack.stage, fqn: "AdoptableTopic" });
171 }).pipe(Effect.provide(stack.state));
172
173 const adopted = yield* stack.deploy(
174 Effect.gen(function* () {
175 return yield* Topic("AdoptableTopic", { topicName });
176 }),
177 );
178
179 expect(adopted.topicArn).toEqual(initial.topicArn);
180
181 yield* stack.destroy();
182 yield* assertTopicDeleted(initial.topicArn);
183 }).pipe(Effect.ensuring(deleteTopicIdempotent(topicArn)));
184 }),
185 );
186
187 test.provider("foreign-tagged topic requires adopt(true) to take over", (stack) =>
188 Effect.gen(function* () {
189 yield* stack.destroy();
190
191 // Deterministic name so a re-run reclaims any orphan from a
192 // previously-killed run instead of creating a new one.
193 const topicName = "alchemy-test-sns-takeover";
194 const topicArn = yield* topicArnFor(topicName);
195
196 // Pre-clean: reclaim a leftover unmanaged topic from a prior run.
197 yield* deleteTopicIdempotent(topicArn);
198
199 yield* Effect.gen(function* () {
200 const original = yield* stack.deploy(
201 Effect.gen(function* () {
202 return yield* Topic("Original", { topicName });
203 }),
204 );
205
206 yield* Effect.gen(function* () {
207 const state = yield* yield* State;
208 yield* state.delete({ stack: stack.name, stage: stack.stage, fqn: "Original" });
209 }).pipe(Effect.provide(stack.state));
210
211 const takenOver = yield* stack
212 .deploy(
213 Effect.gen(function* () {
214 return yield* Topic("Different", { topicName });
215 }),
216 )
217 .pipe(adopt(true));
218
219 expect(takenOver.topicArn).toEqual(original.topicArn);
220
221 const tagsResp = yield* SNS.listTagsForResource({ ResourceArn: takenOver.topicArn });
222 const tagMap = Object.fromEntries(
223 (tagsResp.Tags ?? [])
224 .filter((t): t is { Key: string; Value: string } => typeof t.Value === "string")
225 .map((t) => [t.Key, t.Value]),
226 );
227 expect(tagMap["alchemy::id"]).toEqual("Different");
228
229 yield* stack.destroy();
230 yield* assertTopicDeleted(takenOver.topicArn);
231 }).pipe(Effect.ensuring(deleteTopicIdempotent(topicArn)));
232 }),
233 );
234
235 // Canonical `list()` test (AWS account/region-scoped collection): deploy a
236 // real topic, resolve the provider from context via the typed
237 // `Provider.findProvider`, call `list()`, and assert the deployed topic
238 // appears in the exhaustively-paginated result.
239 test.provider("list enumerates the deployed topic", (stack) =>
240 Effect.gen(function* () {
241 yield* stack.destroy();
242
243 const topic = yield* stack.deploy(
244 Effect.gen(function* () {
245 return yield* Topic("ListTopic", { topicName: "alchemy-test-sns-topic-list" });
246 }),
247 );
248
249 const provider = yield* Provider.findProvider(Topic);
250 const all = yield* provider.list();
251
252 expect(all.some((t) => t.topicArn === topic.topicArn)).toBe(true);
253
254 yield* stack.destroy();
255 yield* assertTopicDeleted(topic.topicArn);
256 }),
257 );
258
259 class TopicStillExists extends Data.TaggedError("TopicStillExists") {}
260
261 const assertTopicDeleted = Effect.fn(function* (topicArn: string) {
262 yield* SNS.getTopicAttributes({ TopicArn: topicArn }).pipe(
263 Effect.flatMap(() => Effect.fail(new TopicStillExists())),
264 Effect.retry({
265 while: (error) => error._tag === "TopicStillExists",
266 schedule: Schedule.exponential(100),
267 times: 8,
268 }),
269 Effect.catchTag("NotFoundException", () => Effect.void),
270 Effect.catchTag("InvalidParameterException", () => Effect.void),
271 );
272 });
273 });