@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 267 lines 9.81 KB
Raw
1 import * as Amplify from "@alchemy.run/aws/Amplify";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
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 * as Stream from "effect/Stream";
9 import path from "pathe";
10
11 const main = path.resolve(import.meta.dirname, "handler.ts");
12
13 export class AmplifyTestFunction extends Lambda.Function<Lambda.Function>()(
14 "AmplifyTestFunction",
15 ) {}
16
17 export default AmplifyTestFunction.make(
18 {
19 main,
20 functionUrl: true,
21 // GenerateAccessLogs can take longer than Lambda's 3s default while
22 // Amplify prepares the pre-signed archive URL. Keep the invocation alive
23 // long enough for the handler to return either the typed service result or
24 // its structured diagnostic response instead of a raw Function URL 502.
25 timeout: Duration.seconds(30),
26 },
27 Effect.gen(function* () {
28 const app = yield* Amplify.App("BindingsTestApp", {
29 description: "Amplify bindings fixture (manual deploys, no repo)",
30 platform: "WEB",
31 });
32
33 const AppId = yield* app.appId;
34 const DefaultDomain = yield* app.defaultDomain;
35
36 const createDeployment = yield* Amplify.CreateDeployment(app);
37 const startDeployment = yield* Amplify.StartDeployment(app);
38 const startJob = yield* Amplify.StartJob(app);
39 const stopJob = yield* Amplify.StopJob(app);
40 const getJob = yield* Amplify.GetJob(app);
41 const listJobs = yield* Amplify.ListJobs(app);
42 const deleteJob = yield* Amplify.DeleteJob(app);
43 const listArtifacts = yield* Amplify.ListArtifacts(app);
44 const getArtifactUrl = yield* Amplify.GetArtifactUrl(app);
45 const generateAccessLogs = yield* Amplify.GenerateAccessLogs(app);
46
47 // Event source: creates the EventBridge rule + Lambda permission at
48 // deploy; at runtime, delivered events are recorded in instance memory
49 // (best-effort — a different instance may serve /events) and exposed on
50 // the /events route.
51 const deploymentEvents: Amplify.DeploymentStatusChangeDetail[] = [];
52 yield* Amplify.consumeDeploymentStatusChanges({ id: "BindingsTestApp" }, (events) =>
53 Stream.runForEach(events, (event) =>
54 Effect.sync(() => {
55 deploymentEvents.push(event.detail);
56 }),
57 ),
58 );
59
60 return {
61 fetch: Effect.gen(function* () {
62 const request = yield* HttpServerRequest;
63 const url = new URL(request.originalUrl);
64 const pathname = url.pathname;
65
66 if (request.method === "GET" && pathname === "/meta") {
67 return yield* HttpServerResponse.json({
68 appId: yield* AppId,
69 defaultDomain: yield* DefaultDomain,
70 });
71 }
72
73 if (request.method === "POST" && pathname === "/deployments") {
74 const body = (yield* request.json) as { branchName: string };
75 const result = yield* createDeployment({
76 branchName: body.branchName,
77 });
78 return yield* HttpServerResponse.json({
79 jobId: result.jobId,
80 zipUploadUrl: result.zipUploadUrl,
81 });
82 }
83
84 if (request.method === "POST" && pathname === "/deployments/start") {
85 const body = (yield* request.json) as {
86 branchName: string;
87 jobId?: string;
88 sourceUrl?: string;
89 };
90 const result = yield* startDeployment({
91 branchName: body.branchName,
92 jobId: body.jobId,
93 sourceUrl: body.sourceUrl,
94 });
95 return yield* HttpServerResponse.json({
96 jobSummary: result.jobSummary,
97 });
98 }
99
100 if (request.method === "GET" && pathname === "/job") {
101 const branchName = url.searchParams.get("branchName")!;
102 const jobId = url.searchParams.get("jobId")!;
103 const result = yield* getJob({ branchName, jobId });
104 return yield* HttpServerResponse.json({
105 status: result.job.summary.status,
106 stepCount: result.job.steps.length,
107 });
108 }
109
110 if (request.method === "GET" && pathname === "/jobs") {
111 const branchName = url.searchParams.get("branchName")!;
112 const result = yield* listJobs({ branchName });
113 return yield* HttpServerResponse.json({
114 jobSummaries: result.jobSummaries,
115 });
116 }
117
118 if (request.method === "POST" && pathname === "/jobs/start") {
119 const body = (yield* request.json) as {
120 branchName: string;
121 jobType: string;
122 };
123 // A manual-deploy branch has no connected repo, so RELEASE jobs are
124 // rejected by the service — surface the typed tag instead of dying
125 // so the test can assert the binding + IAM wiring end-to-end.
126 const result = yield* startJob({
127 branchName: body.branchName,
128 jobType: body.jobType as "RELEASE",
129 }).pipe(
130 Effect.map((r) => ({
131 started: true as const,
132 jobId: r.jobSummary.jobId,
133 })),
134 Effect.catchTag(["BadRequestException", "NotFoundException"], (e) =>
135 Effect.succeed({ started: false as const, errorTag: e._tag }),
136 ),
137 );
138 return yield* HttpServerResponse.json(result);
139 }
140
141 if (request.method === "POST" && pathname === "/jobs/stop") {
142 const body = (yield* request.json) as {
143 branchName: string;
144 jobId: string;
145 };
146 // Stopping a job that already settled is a BadRequest/NotFound —
147 // report the tag rather than 500 so the race stays assertable.
148 const result = yield* stopJob({
149 branchName: body.branchName,
150 jobId: body.jobId,
151 }).pipe(
152 Effect.map((r) => ({
153 stopped: true as const,
154 status: r.jobSummary.status,
155 })),
156 Effect.catchTag(
157 ["BadRequestException", "NotFoundException", "LimitExceededException"],
158 (e) => Effect.succeed({ stopped: false as const, errorTag: e._tag }),
159 ),
160 );
161 return yield* HttpServerResponse.json(result);
162 }
163
164 if (request.method === "POST" && pathname === "/jobs/delete") {
165 const body = (yield* request.json) as {
166 branchName: string;
167 jobId: string;
168 };
169 const result = yield* deleteJob({
170 branchName: body.branchName,
171 jobId: body.jobId,
172 }).pipe(
173 Effect.map((r) => ({
174 deleted: true as const,
175 status: r.jobSummary.status,
176 })),
177 Effect.catchTag(["BadRequestException", "NotFoundException"], (e) =>
178 Effect.succeed({
179 deleted: false as const,
180 errorTag: e._tag,
181 message: e.message,
182 }),
183 ),
184 );
185 return yield* HttpServerResponse.json(result);
186 }
187
188 if (request.method === "GET" && pathname === "/artifacts") {
189 const branchName = url.searchParams.get("branchName")!;
190 const jobId = url.searchParams.get("jobId")!;
191 const result = yield* listArtifacts({ branchName, jobId });
192 return yield* HttpServerResponse.json({
193 artifacts: result.artifacts,
194 });
195 }
196
197 if (request.method === "GET" && pathname === "/artifact-url") {
198 const artifactId = url.searchParams.get("artifactId")!;
199 const result = yield* getArtifactUrl({ artifactId });
200 return yield* HttpServerResponse.json({
201 artifactUrl: result.artifactUrl,
202 });
203 }
204
205 if (request.method === "POST" && pathname === "/access-logs") {
206 const body = (yield* request.json) as { domainName: string };
207 const result = yield* generateAccessLogs({
208 domainName: body.domainName,
209 }).pipe(
210 Effect.map((r) => ({ ok: true as const, logUrl: r.logUrl })),
211 Effect.catchTag(["BadRequestException", "NotFoundException"], (e) =>
212 Effect.succeed({
213 ok: false as const,
214 errorTag: e._tag,
215 message: e.message,
216 }),
217 ),
218 Effect.catchCause((cause) =>
219 Effect.succeed({
220 ok: false as const,
221 errorTag: "UnexpectedCause",
222 cause: String(cause),
223 }),
224 ),
225 );
226 return yield* HttpServerResponse.json(result);
227 }
228
229 if (request.method === "GET" && pathname === "/events") {
230 return yield* HttpServerResponse.json({ events: deploymentEvents });
231 }
232
233 if (request.method === "GET" && pathname === "/ping") {
234 return yield* HttpServerResponse.json({ ok: true });
235 }
236
237 return yield* HttpServerResponse.json(
238 { error: "Not found", method: request.method, pathname },
239 { status: 404 },
240 );
241 }).pipe(
242 // Surface unexpected failures in the response body — the fixture
243 // Lambda has no CloudWatch logs permission, so the HTTP body is the
244 // only diagnostic channel the test can observe.
245 Effect.catchCause((cause) =>
246 HttpServerResponse.json({ error: "unhandled", cause: String(cause) }, { status: 500 }),
247 ),
248 ),
249 };
250 }).pipe(
251 Effect.provide(
252 Layer.mergeAll(
253 Lambda.EventSource,
254 Amplify.CreateDeploymentHttp,
255 Amplify.StartDeploymentHttp,
256 Amplify.StartJobHttp,
257 Amplify.StopJobHttp,
258 Amplify.GetJobHttp,
259 Amplify.ListJobsHttp,
260 Amplify.DeleteJobHttp,
261 Amplify.ListArtifactsHttp,
262 Amplify.GetArtifactUrlHttp,
263 Amplify.GenerateAccessLogsHttp,
264 ),
265 ),
266 ),
267 );