@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 231 lines 8.27 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import NotificationsTestFunctionLive, { NotificationsTestFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "NotificationsBindings");
15
16 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17 // over 60s on a fresh deploy.
18 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
19
20 let baseUrl: string;
21
22 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
23 readonly status: number;
24 readonly body: string;
25 }> {}
26
27 // The shared Lambda fixture occasionally answers a transient 5xx under load
28 // (cold re-init, IAM propagation on the freshly attached policy that the
29 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
30 // 4xx/assertion failure surfaces immediately.
31 const send = (request: HttpClientRequest.HttpClientRequest) =>
32 HttpClient.execute(request).pipe(
33 Effect.flatMap((response) =>
34 response.status >= 500
35 ? response.text.pipe(
36 Effect.flatMap((body) =>
37 Effect.fail(new TransientUpstream({ status: response.status, body })),
38 ),
39 )
40 : Effect.succeed(response),
41 ),
42 Effect.retry({
43 while: (e) => e._tag === "TransientUpstream",
44 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
45 }),
46 );
47
48 const getJson = (path: string) =>
49 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
50
51 describe.sequential(
52 "Notifications Bindings",
53 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:notifications", "live"] },
54 () => {
55 beforeAll(
56 Effect.gen(function* () {
57 yield* Effect.logInfo("Notifications test setup: destroying previous resources");
58 yield* sharedStack.destroy();
59
60 yield* Effect.logInfo("Notifications test setup: deploying fixture");
61 const { functionUrl } = yield* sharedStack.deploy(
62 Effect.gen(function* () {
63 return yield* NotificationsTestFunction;
64 }).pipe(Effect.provide(NotificationsTestFunctionLive)),
65 );
66
67 expect(functionUrl).toBeTruthy();
68 baseUrl = functionUrl!.replace(/\/+$/, "");
69
70 const readinessUrl = `${baseUrl}/bindings`;
71 yield* Effect.logInfo(`Notifications test setup: probing readiness at ${readinessUrl}`);
72 yield* HttpClient.get(readinessUrl).pipe(
73 Effect.flatMap((response) =>
74 response.status === 200
75 ? Effect.succeed(response)
76 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
77 ),
78 Effect.tapError((error) =>
79 Effect.logWarning(`Notifications test setup: fixture not ready yet (${String(error)})`),
80 ),
81 Effect.retry({ schedule: readinessPolicy }),
82 );
83 }),
84 { timeout: 240_000 },
85 );
86
87 afterAll(sharedStack.destroy(), { timeout: 120_000 });
88
89 describe("binding registration", () => {
90 test.provider("all 10 capabilities initialize in the runtime", (_stack) =>
91 Effect.gen(function* () {
92 const response = yield* getJson("/bindings");
93 expect((response as any).bound).toHaveLength(10);
94 }),
95 );
96 });
97
98 describe("ListNotificationEvents", () => {
99 test.provider(
100 "lists the last week's notification events",
101 (_stack) =>
102 Effect.gen(function* () {
103 const response = (yield* getJson("/events")) as any;
104 expect(response.count).toBeGreaterThanOrEqual(0);
105 }),
106 { timeout: 60_000 },
107 );
108 });
109
110 describe("GetNotificationEvent", () => {
111 test.provider(
112 "surfaces the typed not-found for a nonexistent event (proving the grant)",
113 (_stack) =>
114 Effect.gen(function* () {
115 const response = (yield* getJson("/event-nonexistent")) as any;
116 // Either typed rejection proves the grant reached the API — an
117 // IAM gap would surface AccessDeniedException and 500 the route.
118 expect(["ResourceNotFoundException", "ValidationException"]).toContain(response.tag);
119 }),
120 { timeout: 60_000 },
121 );
122 });
123
124 describe("ListManagedNotificationConfigurations", () => {
125 test.provider(
126 "lists the AWS Health managed configurations",
127 (_stack) =>
128 Effect.gen(function* () {
129 const response = (yield* getJson("/managed-configs")) as any;
130 // Every account has the AWS Health categories.
131 expect(response.count).toBeGreaterThan(0);
132 }),
133 { timeout: 60_000 },
134 );
135 });
136
137 describe("GetManagedNotificationConfiguration", () => {
138 test.provider(
139 "fetches the first managed configuration",
140 (_stack) =>
141 Effect.gen(function* () {
142 const response = (yield* getJson("/managed-config")) as any;
143 expect(response.tag).toBe("Ok");
144 expect(typeof response.name).toBe("string");
145 }),
146 { timeout: 60_000 },
147 );
148 });
149
150 describe("ListManagedNotificationEvents", () => {
151 test.provider(
152 "lists the last week's managed notification events",
153 (_stack) =>
154 Effect.gen(function* () {
155 const response = (yield* getJson("/managed-events")) as any;
156 expect(response.count).toBeGreaterThanOrEqual(0);
157 }),
158 { timeout: 60_000 },
159 );
160 });
161
162 describe("GetManagedNotificationEvent", () => {
163 test.provider(
164 "fetches the first managed event (or reports none, both proving the grant)",
165 (_stack) =>
166 Effect.gen(function* () {
167 const response = (yield* getJson("/managed-event")) as any;
168 expect(["Ok", "NoEvents"]).toContain(response.tag);
169 if (response.tag === "Ok") {
170 expect(response.configurationArn).toContain(":managed-notification-configuration/");
171 }
172 }),
173 { timeout: 60_000 },
174 );
175 });
176
177 describe("ListManagedNotificationChildEvents", () => {
178 test.provider(
179 "lists an aggregate's children (or the typed rejection for a non-aggregate)",
180 (_stack) =>
181 Effect.gen(function* () {
182 const response = (yield* getJson("/managed-child-events")) as any;
183 expect([
184 "Ok",
185 "ValidationException",
186 "ResourceNotFoundException",
187 "NoEvents",
188 ]).toContain(response.tag);
189 }),
190 { timeout: 60_000 },
191 );
192 });
193
194 describe("GetManagedNotificationChildEvent", () => {
195 test.provider(
196 "surfaces the typed not-found for a nonexistent child event (proving the grant)",
197 (_stack) =>
198 Effect.gen(function* () {
199 const response = (yield* getJson("/managed-child-event-nonexistent")) as any;
200 expect(["ResourceNotFoundException", "ValidationException"]).toContain(response.tag);
201 }),
202 { timeout: 60_000 },
203 );
204 });
205
206 describe("ListManagedNotificationChannelAssociations", () => {
207 test.provider(
208 "lists the first managed configuration's channel associations",
209 (_stack) =>
210 Effect.gen(function* () {
211 const response = (yield* getJson("/managed-channel-associations")) as any;
212 expect(response.tag).toBe("Ok");
213 expect(response.count).toBeGreaterThanOrEqual(0);
214 }),
215 { timeout: 60_000 },
216 );
217 });
218
219 describe("ListChannels", () => {
220 test.provider(
221 "lists the fixture configuration's channels (proving ARN injection + the scoped grant)",
222 (_stack) =>
223 Effect.gen(function* () {
224 const response = (yield* getJson("/channels")) as any;
225 expect(response.count).toBeGreaterThanOrEqual(0);
226 }),
227 { timeout: 60_000 },
228 );
229 });
230 },
231 );