@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 124 lines 4.25 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Subscription } from "@alchemy.run/aws/SNS";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as SNS from "@distilled.cloud/aws/sns";
6 import * as Provider from "alchemy/Provider";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import {
11 SubscriptionTargetFunction,
12 SubscriptionTargetFunctionLive,
13 } from "./subscription-handler.ts";
14
15 const { test } = Test.make({ providers: AWS.providers() });
16
17 test.provider(
18 "create and delete lambda subscription",
19 (stack) =>
20 Effect.gen(function* () {
21 yield* stack.destroy();
22
23 const deployed = yield* stack.deploy(
24 Effect.gen(function* () {
25 const topic = yield* AWS.SNS.Topic("LambdaSubscriptionTopic");
26 const target = yield* SubscriptionTargetFunction;
27 const subscription = yield* Subscription("LambdaSubscription", {
28 topicArn: topic.topicArn,
29 protocol: "lambda",
30 endpoint: target.functionArn,
31 returnSubscriptionArn: true,
32 });
33
34 return {
35 topic,
36 subscription,
37 };
38 }).pipe(Effect.provide(SubscriptionTargetFunctionLive)),
39 );
40
41 expect(deployed.subscription.subscriptionArn).toBeDefined();
42
43 const attributes = yield* SNS.getSubscriptionAttributes({
44 SubscriptionArn: deployed.subscription.subscriptionArn,
45 }).pipe(
46 Effect.tapError((err) => Effect.logError(deployed.subscription.subscriptionArn, err)),
47 Effect.retry({
48 while: (error) => error._tag === "NotFoundException",
49 schedule: Schedule.fixed(300),
50 times: 10,
51 }),
52 );
53 expect(attributes.Attributes?.Protocol).toBe("lambda");
54 expect(attributes.Attributes?.TopicArn).toBe(deployed.topic.topicArn);
55
56 yield* stack.destroy();
57 yield* assertSubscriptionDeleted(deployed.subscription.subscriptionArn);
58 }),
59 // ~60s in isolation (Lambda bundling + role propagation + SNS eventual
60 // consistency); budget headroom for full-suite contention.
61 {
62 tags: ["provider:aws", "provider:aws:lambda", "provider:aws:sns", "live"],
63 timeout: 180_000,
64 },
65 );
66
67 // Canonical `list()` test (AWS account/region-scoped collection): deploy a
68 // topic + queue + SQS subscription, resolve the provider from context via the
69 // typed `findProvider`, call `list()`, and assert the deployed subscription
70 // appears in the exhaustively-paginated result.
71 test.provider(
72 "list enumerates the deployed subscription",
73 (stack) =>
74 Effect.gen(function* () {
75 yield* stack.destroy();
76
77 const deployed = yield* stack.deploy(
78 Effect.gen(function* () {
79 const topic = yield* AWS.SNS.Topic("ListTopic");
80 const queue = yield* AWS.SQS.Queue("ListQueue");
81 const subscription = yield* Subscription("ListSubscription", {
82 topicArn: topic.topicArn,
83 protocol: "sqs",
84 endpoint: queue.queueArn,
85 returnSubscriptionArn: true,
86 });
87 return { subscription };
88 }),
89 );
90
91 expect(deployed.subscription.subscriptionArn).toBeDefined();
92
93 const provider = yield* Provider.findProvider(Subscription);
94 const all = yield* provider.list();
95
96 expect(all.some((s) => s.subscriptionArn === deployed.subscription.subscriptionArn)).toBe(
97 true,
98 );
99
100 yield* stack.destroy();
101 yield* assertSubscriptionDeleted(deployed.subscription.subscriptionArn);
102 }),
103 {
104 tags: ["provider:aws", "provider:aws:sns", "provider:aws:sqs", "live"],
105 timeout: 180_000,
106 },
107 );
108
109 class SubscriptionStillExists extends Data.TaggedError("SubscriptionStillExists") {}
110
111 const assertSubscriptionDeleted = Effect.fn(function* (subscriptionArn: string) {
112 yield* SNS.getSubscriptionAttributes({
113 SubscriptionArn: subscriptionArn,
114 }).pipe(
115 Effect.flatMap(() => Effect.fail(new SubscriptionStillExists())),
116 Effect.retry({
117 while: (error) => error._tag === "SubscriptionStillExists",
118 schedule: Schedule.exponential(100),
119 times: 8,
120 }),
121 Effect.catchTag("NotFoundException", () => Effect.void),
122 Effect.catchTag("InvalidParameterException", () => Effect.void),
123 );
124 });