@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, 04:45 UTC

main
ts 246 lines 9.81 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as Notifications from "@alchemy.run/aws/Notifications";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 /** A Date `days` days in the past (UTC). */
13 const daysAgo = (days: number) => Effect.sync(() => new Date(Date.now() - days * 24 * 3600 * 1000));
14
15 /**
16 * Fabricate a well-formed same-account notification-event ARN that cannot
17 * exist — drives the typed not-found path for GetNotificationEvent /
18 * GetManagedNotificationChildEvent, proving the grant reaches the API (an
19 * IAM gap would surface AccessDeniedException instead).
20 */
21 const fakeEventArn = (accountId: string) =>
22 `arn:aws:notifications::${accountId}:configuration/a01000000000000000000000000/event/a01000000000000000000000000`;
23
24 export class NotificationsTestFunction extends Lambda.Function<Lambda.Function>()(
25 "NotificationsTestFunction",
26 ) {}
27
28 export default NotificationsTestFunction.make(
29 {
30 main,
31 functionUrl: true,
32 timeout: Duration.seconds(30),
33 },
34 Effect.gen(function* () {
35 // A configuration owned by the fixture — the ListChannels binding is
36 // scoped to it (proving ARN injection + the configuration-scoped grant).
37 const config = yield* Notifications.NotificationConfiguration("BindingsConfig", {
38 description: "notifications bindings fixture",
39 });
40
41 // --- account-level bindings ---
42 const getNotificationEvent = yield* Notifications.GetNotificationEvent();
43 const listNotificationEvents = yield* Notifications.ListNotificationEvents();
44 const getManagedNotificationEvent = yield* Notifications.GetManagedNotificationEvent();
45 const listManagedNotificationEvents = yield* Notifications.ListManagedNotificationEvents();
46 const getManagedNotificationChildEvent =
47 yield* Notifications.GetManagedNotificationChildEvent();
48 const listManagedNotificationChildEvents =
49 yield* Notifications.ListManagedNotificationChildEvents();
50 const getManagedNotificationConfiguration =
51 yield* Notifications.GetManagedNotificationConfiguration();
52 const listManagedNotificationConfigurations =
53 yield* Notifications.ListManagedNotificationConfigurations();
54 const listManagedNotificationChannelAssociations =
55 yield* Notifications.ListManagedNotificationChannelAssociations();
56
57 // --- configuration-scoped bindings ---
58 const listChannels = yield* Notifications.ListChannels(config);
59
60 const bound = {
61 getNotificationEvent,
62 listNotificationEvents,
63 getManagedNotificationEvent,
64 listManagedNotificationEvents,
65 getManagedNotificationChildEvent,
66 listManagedNotificationChildEvents,
67 getManagedNotificationConfiguration,
68 listManagedNotificationConfigurations,
69 listManagedNotificationChannelAssociations,
70 listChannels,
71 };
72
73 // The first managed configuration ARN doubles as the account-id source
74 // for fabricated ARNs (every account has the AWS Health categories).
75 const firstManagedConfigArn = listManagedNotificationConfigurations().pipe(
76 Effect.map((r) => r.managedNotificationConfigurations[0]?.arn),
77 );
78
79 return {
80 fetch: Effect.gen(function* () {
81 const request = yield* HttpServerRequest;
82 const url = new URL(request.originalUrl);
83 const pathname = url.pathname;
84
85 if (request.method === "GET" && pathname === "/bindings") {
86 return yield* HttpServerResponse.json({
87 bound: Object.keys(bound),
88 });
89 }
90
91 if (request.method === "GET" && pathname === "/events") {
92 const result = yield* listNotificationEvents({
93 startTime: yield* daysAgo(7),
94 });
95 return yield* HttpServerResponse.json({
96 count: result.notificationEvents.length,
97 });
98 }
99
100 if (request.method === "GET" && pathname === "/event-nonexistent") {
101 // Typed not-found path — proves the GetNotificationEvent grant.
102 const managedArn = yield* firstManagedConfigArn;
103 const accountId = managedArn?.split(":")[4] ?? "000000000000";
104 const result = yield* getNotificationEvent({
105 arn: fakeEventArn(accountId),
106 }).pipe(
107 Effect.map(() => "Ok"),
108 Effect.catchTag(["ResourceNotFoundException", "ValidationException"], (e) =>
109 Effect.succeed(e._tag),
110 ),
111 );
112 return yield* HttpServerResponse.json({ tag: result });
113 }
114
115 if (request.method === "GET" && pathname === "/managed-configs") {
116 const result = yield* listManagedNotificationConfigurations();
117 return yield* HttpServerResponse.json({
118 count: result.managedNotificationConfigurations.length,
119 names: result.managedNotificationConfigurations.map((c) => c.name),
120 });
121 }
122
123 if (request.method === "GET" && pathname === "/managed-config") {
124 const arn = yield* firstManagedConfigArn;
125 if (arn === undefined) {
126 return yield* HttpServerResponse.json({ tag: "NoConfigs" });
127 }
128 const result = yield* getManagedNotificationConfiguration({ arn });
129 return yield* HttpServerResponse.json({
130 tag: "Ok",
131 name: result.name,
132 });
133 }
134
135 if (request.method === "GET" && pathname === "/managed-events") {
136 const result = yield* listManagedNotificationEvents({
137 startTime: yield* daysAgo(7),
138 });
139 return yield* HttpServerResponse.json({
140 count: result.managedNotificationEvents.length,
141 });
142 }
143
144 if (request.method === "GET" && pathname === "/managed-event") {
145 const listed = yield* listManagedNotificationEvents({
146 startTime: yield* daysAgo(7),
147 maxResults: 5,
148 });
149 const first = listed.managedNotificationEvents[0];
150 if (first === undefined) {
151 return yield* HttpServerResponse.json({ tag: "NoEvents" });
152 }
153 const result = yield* getManagedNotificationEvent({
154 arn: first.arn,
155 });
156 return yield* HttpServerResponse.json({
157 tag: "Ok",
158 configurationArn: result.managedNotificationConfigurationArn,
159 });
160 }
161
162 if (request.method === "GET" && pathname === "/managed-child-events") {
163 const listed = yield* listManagedNotificationEvents({
164 startTime: yield* daysAgo(7),
165 maxResults: 5,
166 });
167 const first = listed.managedNotificationEvents[0];
168 if (first === undefined) {
169 return yield* HttpServerResponse.json({ tag: "NoEvents" });
170 }
171 // Most managed events are not aggregates — a ValidationException
172 // for a non-aggregate parent still proves the grant + typed union.
173 const result = yield* listManagedNotificationChildEvents({
174 aggregateManagedNotificationEventArn: first.arn,
175 }).pipe(
176 Effect.map((r) => ({
177 tag: "Ok",
178 count: r.managedNotificationChildEvents.length,
179 })),
180 Effect.catchTag(["ValidationException", "ResourceNotFoundException"], (e) =>
181 Effect.succeed({ tag: e._tag, count: 0 }),
182 ),
183 );
184 return yield* HttpServerResponse.json(result);
185 }
186
187 if (request.method === "GET" && pathname === "/managed-child-event-nonexistent") {
188 // Typed not-found path — proves the grant reaches the API.
189 const managedArn = yield* firstManagedConfigArn;
190 const accountId = managedArn?.split(":")[4] ?? "000000000000";
191 const result = yield* getManagedNotificationChildEvent({
192 arn: fakeEventArn(accountId),
193 }).pipe(
194 Effect.map(() => "Ok"),
195 Effect.catchTag(["ResourceNotFoundException", "ValidationException"], (e) =>
196 Effect.succeed(e._tag),
197 ),
198 );
199 return yield* HttpServerResponse.json({ tag: result });
200 }
201
202 if (request.method === "GET" && pathname === "/managed-channel-associations") {
203 const arn = yield* firstManagedConfigArn;
204 if (arn === undefined) {
205 return yield* HttpServerResponse.json({ tag: "NoConfigs" });
206 }
207 const result = yield* listManagedNotificationChannelAssociations({
208 managedNotificationConfigurationArn: arn,
209 });
210 return yield* HttpServerResponse.json({
211 tag: "Ok",
212 count: result.channelAssociations.length,
213 });
214 }
215
216 if (request.method === "GET" && pathname === "/channels") {
217 const result = yield* listChannels();
218 return yield* HttpServerResponse.json({
219 count: result.channels.length,
220 });
221 }
222
223 return yield* HttpServerResponse.json(
224 { error: "Not found", method: request.method, pathname },
225 { status: 404 },
226 );
227 }).pipe(Effect.orDie),
228 };
229 }).pipe(
230 Effect.provide(
231 Layer.mergeAll(
232 Lambda.EventSource,
233 Notifications.GetNotificationEventHttp,
234 Notifications.ListNotificationEventsHttp,
235 Notifications.GetManagedNotificationEventHttp,
236 Notifications.ListManagedNotificationEventsHttp,
237 Notifications.GetManagedNotificationChildEventHttp,
238 Notifications.ListManagedNotificationChildEventsHttp,
239 Notifications.GetManagedNotificationConfigurationHttp,
240 Notifications.ListManagedNotificationConfigurationsHttp,
241 Notifications.ListManagedNotificationChannelAssociationsHttp,
242 Notifications.ListChannelsHttp,
243 ),
244 ),
245 ),
246 );