@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 209 lines 7.95 KB
Raw
1 import * as AppRegistry from "@alchemy.run/aws/AppRegistry";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Layer from "effect/Layer";
7 import path from "pathe";
8
9 const main = path.resolve(import.meta.dirname, "handler.ts");
10
11 // Deterministic name of a stack that never exists — drives the typed
12 // ResourceNotFoundException paths for GetAssociatedResource / SyncResource.
13 const NONEXISTENT_STACK = "alchemy-appregistry-bindings-nonexistent-stack";
14
15 export class AppRegistryTestFunction extends Lambda.Function<Lambda.Function>()(
16 "AppRegistryTestFunction",
17 ) {}
18
19 export default AppRegistryTestFunction.make(
20 {
21 main,
22 functionUrl: true,
23 },
24 Effect.gen(function* () {
25 const app = yield* AppRegistry.Application("BindingsApp", {
26 description: "AppRegistry bindings fixture",
27 });
28 const group = yield* AppRegistry.AttributeGroup("BindingsGroup", {
29 attributes: { owner: "alchemy-test", tier: "bindings" },
30 });
31 yield* AppRegistry.AttributeGroupAssociation("BindingsGroupAssoc", {
32 application: app.applicationId,
33 attributeGroup: group.attributeGroupId,
34 });
35
36 // --- application-scoped bindings ---
37 const getApplication = yield* AppRegistry.GetApplication(app);
38 const getAssociatedResource = yield* AppRegistry.GetAssociatedResource(app);
39 const listAssociatedResources = yield* AppRegistry.ListAssociatedResources(app);
40 const listAssociatedAttributeGroups = yield* AppRegistry.ListAssociatedAttributeGroups(app);
41 const listAttributeGroupsForApplication =
42 yield* AppRegistry.ListAttributeGroupsForApplication(app);
43
44 // --- attribute-group-scoped bindings ---
45 const getAttributeGroup = yield* AppRegistry.GetAttributeGroup(group);
46
47 // --- account-level bindings ---
48 const syncResource = yield* AppRegistry.SyncResource();
49 const listApplications = yield* AppRegistry.ListApplications();
50 const listAttributeGroups = yield* AppRegistry.ListAttributeGroups();
51
52 const bound = {
53 getApplication,
54 getAssociatedResource,
55 listAssociatedResources,
56 listAssociatedAttributeGroups,
57 listAttributeGroupsForApplication,
58 getAttributeGroup,
59 syncResource,
60 listApplications,
61 listAttributeGroups,
62 };
63
64 return {
65 fetch: Effect.gen(function* () {
66 const request = yield* HttpServerRequest;
67 const url = new URL(request.originalUrl);
68 const pathname = url.pathname;
69
70 if (request.method === "GET" && pathname === "/bindings") {
71 return yield* HttpServerResponse.json({
72 bound: Object.keys(bound),
73 });
74 }
75
76 if (request.method === "GET" && pathname === "/application") {
77 const result = yield* getApplication();
78 return yield* HttpServerResponse.json({
79 name: result.name ?? null,
80 associatedResourceCount: result.associatedResourceCount ?? 0,
81 });
82 }
83
84 if (request.method === "GET" && pathname === "/attribute-group") {
85 const result = yield* getAttributeGroup();
86 return yield* HttpServerResponse.json({
87 name: result.name ?? null,
88 attributes: JSON.parse(result.attributes ?? "{}"),
89 });
90 }
91
92 if (request.method === "GET" && pathname === "/associated-attribute-groups") {
93 const result = yield* listAssociatedAttributeGroups({
94 maxResults: 20,
95 });
96 return yield* HttpServerResponse.json({
97 count: (result.attributeGroups ?? []).length,
98 attributeGroups: result.attributeGroups ?? [],
99 });
100 }
101
102 if (request.method === "GET" && pathname === "/attribute-groups-details") {
103 const result = yield* listAttributeGroupsForApplication({
104 maxResults: 20,
105 });
106 return yield* HttpServerResponse.json({
107 names: (result.attributeGroupsDetails ?? []).map((detail) => detail.name ?? null),
108 });
109 }
110
111 if (request.method === "GET" && pathname === "/associated-resources") {
112 const result = yield* listAssociatedResources({ maxResults: 20 });
113 return yield* HttpServerResponse.json({
114 count: (result.resources ?? []).length,
115 });
116 }
117
118 if (request.method === "GET" && pathname === "/associated-resource-not-found") {
119 // Exercises `application` injection + the typed not-found path.
120 const result = yield* getAssociatedResource({
121 resourceType: "CFN_STACK",
122 resource: NONEXISTENT_STACK,
123 }).pipe(
124 Effect.map(() => ({ found: true })),
125 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed({ found: false })),
126 );
127 return yield* HttpServerResponse.json(result);
128 }
129
130 if (request.method === "GET" && pathname === "/applications") {
131 // The fixture's own application must appear in the account listing.
132 // Bounded pagination (5 pages) keeps the assertion robust as the
133 // account accumulates applications.
134 const names: (string | null)[] = [];
135 let nextToken: string | undefined;
136 for (let page = 0; page < 5; page++) {
137 const result = yield* listApplications({
138 maxResults: 25,
139 nextToken,
140 });
141 names.push(...(result.applications ?? []).map((a) => a.name ?? null));
142 nextToken = result.nextToken;
143 if (!nextToken) break;
144 }
145 return yield* HttpServerResponse.json({ names });
146 }
147
148 if (request.method === "GET" && pathname === "/attribute-groups") {
149 // The fixture's own attribute group must appear in the account
150 // listing. Bounded pagination (5 pages) as above.
151 const names: (string | null)[] = [];
152 let nextToken: string | undefined;
153 for (let page = 0; page < 5; page++) {
154 const result = yield* listAttributeGroups({
155 maxResults: 25,
156 nextToken,
157 });
158 names.push(...(result.attributeGroups ?? []).map((g) => g.name ?? null));
159 nextToken = result.nextToken;
160 if (!nextToken) break;
161 }
162 return yield* HttpServerResponse.json({ names });
163 }
164
165 if (request.method === "POST" && pathname === "/sync-resource") {
166 // A nonexistent stack proves the servicecatalog:SyncResource grant
167 // end-to-end via its typed error (an IAM gap would surface
168 // AccessDeniedException instead).
169 const tag = yield* syncResource({
170 resourceType: "CFN_STACK",
171 resource: NONEXISTENT_STACK,
172 }).pipe(
173 Effect.map(() => "Synced"),
174 Effect.catchTag(
175 [
176 "ResourceNotFoundException",
177 "ValidationException",
178 // Not expected — surfaced (and failed on) by the test with a
179 // readable tag instead of an opaque 500 if the grant is wrong.
180 "AccessDeniedException",
181 ],
182 (e) => Effect.succeed(e._tag),
183 ),
184 );
185 return yield* HttpServerResponse.json({ tag });
186 }
187
188 return yield* HttpServerResponse.json(
189 { error: "Not found", method: request.method, pathname },
190 { status: 404 },
191 );
192 }).pipe(Effect.orDie),
193 };
194 }).pipe(
195 Effect.provide(
196 Layer.mergeAll(
197 AppRegistry.GetApplicationHttp,
198 AppRegistry.GetAssociatedResourceHttp,
199 AppRegistry.ListAssociatedResourcesHttp,
200 AppRegistry.ListAssociatedAttributeGroupsHttp,
201 AppRegistry.ListAttributeGroupsForApplicationHttp,
202 AppRegistry.GetAttributeGroupHttp,
203 AppRegistry.SyncResourceHttp,
204 AppRegistry.ListApplicationsHttp,
205 AppRegistry.ListAttributeGroupsHttp,
206 ),
207 ),
208 ),
209 );