@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 362 lines 14 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 codebuild from "@distilled.cloud/aws/codebuild";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as HttpClientRequest from "effect/http/HttpClientRequest";
10 import * as Schedule from "effect/Schedule";
11 import CodeBuildTestFunctionLive, {
12 CodeBuildTestFunction,
13 FIXTURE_PROJECT_NAME,
14 FIXTURE_REPORT_GROUP_NAME,
15 } from "./handler";
16
17 const testOptions = { providers: AWS.providers() };
18 const { test, beforeAll, afterAll } = Test.make(testOptions);
19 const sharedStack = Core.scratchStack(testOptions, "CodeBuildBindings");
20
21 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
22 // over 60s on a fresh deploy under parallel-suite load.
23 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
24
25 let baseUrl: string;
26
27 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
28 readonly status: number;
29 readonly body: string;
30 }> {}
31
32 // Retry transient 5xx from the shared Lambda fixture (cold re-init, IAM
33 // propagation on the freshly attached codebuild policy surfaced as a 500 by
34 // the handler's `Effect.orDie`). Genuine 4xx/assertion failures return
35 // immediately.
36 const send = (request: HttpClientRequest.HttpClientRequest) =>
37 HttpClient.execute(request).pipe(
38 Effect.flatMap((response) =>
39 response.status >= 500
40 ? response.text.pipe(
41 Effect.flatMap((body) =>
42 Effect.fail(new TransientUpstream({ status: response.status, body })),
43 ),
44 )
45 : Effect.succeed(response),
46 ),
47 Effect.retry({
48 while: (e) => e._tag === "TransientUpstream",
49 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
50 }),
51 );
52
53 const getJson = (url: string) =>
54 send(HttpClientRequest.get(url)).pipe(Effect.flatMap((r) => r.json));
55
56 const postJson = (url: string, body: unknown) =>
57 send(HttpClientRequest.post(url).pipe(HttpClientRequest.bodyJsonUnsafe(body))).pipe(
58 Effect.flatMap((r) => r.json),
59 );
60
61 const post = (url: string) => send(HttpClientRequest.post(url)).pipe(Effect.flatMap((r) => r.json));
62
63 /**
64 * A route answered with a typed error tag. The tag being present proves the
65 * binding produced a typed failure (untyped errors crash into a 500), and it
66 * not being an authorization tag proves the IAM grant covered the call.
67 */
68 const expectTypedNonAuthz = (body: any) => {
69 expect(typeof body.errorTag).toBe("string");
70 expect(body.errorTag).not.toBe("AccessDeniedException");
71 };
72
73 /** A route that either succeeded or failed with a typed, authorized tag. */
74 const expectAuthorized = (body: any) => {
75 if (body.errorTag !== undefined) expectTypedNonAuthz(body);
76 };
77
78 class BuildNotTerminal extends Data.TaggedError("BuildNotTerminal") {}
79
80 /** Poll a build until it leaves IN_PROGRESS (bounded). */
81 const waitForTerminal = (buildId: string) =>
82 getJson(`${baseUrl}/build/get?id=${encodeURIComponent(buildId)}`).pipe(
83 Effect.flatMap((body: any) =>
84 body.status === undefined || body.status === "IN_PROGRESS"
85 ? Effect.fail(new BuildNotTerminal())
86 : Effect.succeed(body.status as string),
87 ),
88 Effect.retry({
89 while: (e): boolean => e._tag === "BuildNotTerminal",
90 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(20)]),
91 }),
92 );
93
94 const FAKE_UUID = "00000000-0000-0000-0000-000000000000";
95
96 describe.sequential(
97 "CodeBuild Bindings",
98 {
99 tags: [
100 "provider:aws",
101 "provider:aws:batch",
102 "provider:aws:codebuild",
103 "provider:aws:iam",
104 "provider:aws:lambda",
105 "provider:aws:logs",
106 "live",
107 ],
108 },
109 () => {
110 beforeAll(
111 Effect.gen(function* () {
112 yield* Effect.logInfo("CodeBuild test setup: destroying previous");
113 yield* sharedStack.destroy();
114
115 yield* Effect.logInfo("CodeBuild test setup: deploying fixture");
116 const { functionUrl } = yield* sharedStack.deploy(
117 Effect.gen(function* () {
118 return yield* CodeBuildTestFunction;
119 }).pipe(Effect.provide(CodeBuildTestFunctionLive)),
120 );
121
122 expect(functionUrl).toBeTruthy();
123 baseUrl = functionUrl!.replace(/\/+$/, "");
124 const readinessUrl = `${baseUrl}/build/list`;
125
126 yield* Effect.logInfo(`CodeBuild test setup: probing readiness at ${readinessUrl}`);
127 // Ready = the function answers 200 AND the freshly attached codebuild
128 // policy has propagated (an AccessDeniedException errorTag means IAM
129 // is still converging — keep probing).
130 yield* HttpClient.get(readinessUrl).pipe(
131 Effect.flatMap((response) =>
132 response.status === 200
133 ? response.json
134 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
135 ),
136 Effect.flatMap((body: any) =>
137 body.errorTag === undefined
138 ? Effect.succeed(body)
139 : Effect.fail(new Error(`IAM not propagated: ${body.errorTag}`)),
140 ),
141 Effect.retry({ schedule: readinessPolicy }),
142 );
143 }),
144 { timeout: 300_000 },
145 );
146
147 afterAll.skipIf(!!process.env.NO_DESTROY)(sharedStack.destroy(), { timeout: 240_000 });
148
149 describe("StartBuild + ListBuildsForProject + BatchGetBuilds + StopBuild + RetryBuild + BatchDeleteBuilds", () => {
150 test.provider(
151 "starts a build, observes it, stops it, retries it",
152 (_stack) =>
153 Effect.gen(function* () {
154 // StartBuild
155 const started = (yield* post(`${baseUrl}/build/start`)) as any;
156 expect(started.errorTag).toBeUndefined();
157 expect(started.buildId).toContain(FIXTURE_PROJECT_NAME);
158 const buildId = started.buildId as string;
159
160 // ListBuildsForProject sees it
161 const listed = (yield* getJson(`${baseUrl}/build/list`)) as any;
162 expect(listed.ids).toContain(buildId);
163
164 // StopBuild — a just-started build is stoppable in any
165 // pre-terminal phase.
166 const stopped = (yield* postJson(`${baseUrl}/build/stop`, { id: buildId })) as any;
167 expectAuthorized(stopped);
168
169 // BatchGetBuilds — poll to a terminal status (bounded).
170 const terminal = yield* waitForTerminal(buildId);
171 expect(["STOPPED", "SUCCEEDED", "FAILED", "STOPPING"]).toContain(terminal);
172
173 // RetryBuild — restarts the finished build (typed error is
174 // acceptable if the build is still finalizing).
175 const retried = (yield* postJson(`${baseUrl}/build/retry`, { id: buildId })) as any;
176 expectAuthorized(retried);
177 if (retried.buildId !== undefined) {
178 // Don't leave the retried build running.
179 const restopped = (yield* postJson(`${baseUrl}/build/stop`, {
180 id: retried.buildId,
181 })) as any;
182 expectAuthorized(restopped);
183 }
184
185 // BatchDeleteBuilds — regular builds are typically reported in
186 // `buildsNotDeleted` (only batch-build members are deletable);
187 // the call completing proves the IAM + binding wiring.
188 const deleted = (yield* postJson(`${baseUrl}/build/delete`, { ids: [buildId] })) as any;
189 expectAuthorized(deleted);
190 }),
191 { timeout: 240_000 },
192 );
193 });
194
195 describe("InvalidateProjectCache", () => {
196 test.provider("invalidates the project's build cache", (_stack) =>
197 Effect.gen(function* () {
198 // The fixture project has no cache configured — success and a typed
199 // rejection both prove the wiring.
200 const body = (yield* post(`${baseUrl}/cache/invalidate`)) as any;
201 expectAuthorized(body);
202 }),
203 );
204 });
205
206 describe("Batch builds", () => {
207 test.provider("StartBuildBatch rejects a project without batch config (typed)", (_stack) =>
208 Effect.gen(function* () {
209 const body = (yield* post(`${baseUrl}/batch/start`)) as any;
210 // The fixture project has no build-batch configuration —
211 // CodeBuild must reject with the TYPED InvalidInputException.
212 expect(body.errorTag).toBe("InvalidInputException");
213 }),
214 );
215
216 test.provider("ListBuildBatchesForProject lists (empty)", (_stack) =>
217 Effect.gen(function* () {
218 const body = (yield* getJson(`${baseUrl}/batch/list`)) as any;
219 expect(body.errorTag).toBeUndefined();
220 expect(body.ids).toEqual([]);
221 }),
222 );
223
224 test.provider("BatchGetBuildBatches reports unknown ids", (_stack) =>
225 Effect.gen(function* () {
226 const fakeId = `${FIXTURE_PROJECT_NAME}:${FAKE_UUID}`;
227 const body = (yield* getJson(
228 `${baseUrl}/batch/get?id=${encodeURIComponent(fakeId)}`,
229 )) as any;
230 expect(body.errorTag).toBeUndefined();
231 // The unknown id is reported back (in `buildBatchesNotFound`) rather
232 // than yielding a batch.
233 expect(body.found).toEqual([]);
234 expect(JSON.stringify(body)).toContain(FAKE_UUID);
235 }),
236 );
237
238 test.provider("Stop/Retry/DeleteBuildBatch answer typed for unknown ids", (_stack) =>
239 Effect.gen(function* () {
240 const fakeId = `${FIXTURE_PROJECT_NAME}:${FAKE_UUID}`;
241 const stopped = (yield* postJson(`${baseUrl}/batch/stop`, { id: fakeId })) as any;
242 expectTypedNonAuthz(stopped);
243
244 const retried = (yield* postJson(`${baseUrl}/batch/retry`, { id: fakeId })) as any;
245 expectTypedNonAuthz(retried);
246
247 const deleted = (yield* postJson(`${baseUrl}/batch/delete`, { id: fakeId })) as any;
248 expectAuthorized(deleted);
249 }),
250 );
251 });
252
253 describe("Sandboxes", () => {
254 test.provider(
255 "StartSandbox + BatchGetSandboxes + ListSandboxesForProject + StopSandbox",
256 (_stack) =>
257 Effect.gen(function* () {
258 const started = (yield* post(`${baseUrl}/sandbox/start`)) as any;
259 expectAuthorized(started);
260
261 if (started.sandboxId !== undefined) {
262 // Stop it right away — sandboxes bill while running.
263 const stopped = (yield* postJson(`${baseUrl}/sandbox/stop`, {
264 id: started.sandboxId,
265 })) as any;
266 expectAuthorized(stopped);
267
268 const got = (yield* getJson(
269 `${baseUrl}/sandbox/get?id=${encodeURIComponent(started.sandboxId)}`,
270 )) as any;
271 expectAuthorized(got);
272
273 const listed = (yield* getJson(`${baseUrl}/sandbox/list`)) as any;
274 expect(listed.errorTag).toBeUndefined();
275 expect(listed.ids).toContain(started.sandboxId);
276 } else {
277 // Sandboxes are not available for every project configuration —
278 // the typed rejection still proves IAM + binding wiring.
279 const listed = (yield* getJson(`${baseUrl}/sandbox/list`)) as any;
280 expectAuthorized(listed);
281 }
282 }),
283 { timeout: 120_000 },
284 );
285
286 test.provider("command-execution bindings answer typed for an unknown sandbox", (_stack) =>
287 Effect.gen(function* () {
288 const fakeSandbox = `${FIXTURE_PROJECT_NAME}:${FAKE_UUID}`;
289 const command = (yield* postJson(`${baseUrl}/sandbox/command`, {
290 sandboxId: fakeSandbox,
291 command: "echo hello",
292 })) as any;
293 expectTypedNonAuthz(command);
294
295 const got = (yield* getJson(
296 `${baseUrl}/sandbox/command-get?sandboxId=${encodeURIComponent(fakeSandbox)}&commandId=${FAKE_UUID}`,
297 )) as any;
298 expectAuthorized(got);
299
300 const listed = (yield* getJson(
301 `${baseUrl}/sandbox/commands?sandboxId=${encodeURIComponent(fakeSandbox)}`,
302 )) as any;
303 expectAuthorized(listed);
304 }),
305 );
306 });
307
308 describe("Reports", () => {
309 test.provider(
310 "report-read bindings are wired against the bound group",
311 (_stack) =>
312 Effect.gen(function* () {
313 // The fixture group is fresh — no reports yet.
314 const listed = (yield* getJson(`${baseUrl}/reports/list`)) as any;
315 expect(listed.errorTag).toBeUndefined();
316 expect(listed.reports).toEqual([]);
317
318 // Fabricate a report ARN inside the bound group (IAM authorizes
319 // report reads against the report-group ARN).
320 const groupArn = yield* codebuild
321 .listReportGroups({})
322 .pipe(
323 Effect.map((res) =>
324 res.reportGroups?.find((arn) =>
325 arn.endsWith(`report-group/${FIXTURE_REPORT_GROUP_NAME}`),
326 ),
327 ),
328 );
329 expect(groupArn).toBeDefined();
330 const fakeReportArn = `${groupArn!.replace(":report-group/", ":report/")}:${FAKE_UUID}`;
331
332 const got = (yield* getJson(
333 `${baseUrl}/reports/get?arn=${encodeURIComponent(fakeReportArn)}`,
334 )) as any;
335 expectAuthorized(got);
336 if (got.errorTag === undefined) {
337 expect(got.notFound).toContain(fakeReportArn);
338 }
339
340 const testCases = (yield* getJson(
341 `${baseUrl}/reports/test-cases?arn=${encodeURIComponent(fakeReportArn)}`,
342 )) as any;
343 expectAuthorized(testCases);
344
345 const coverage = (yield* getJson(
346 `${baseUrl}/reports/coverage?arn=${encodeURIComponent(fakeReportArn)}`,
347 )) as any;
348 expectAuthorized(coverage);
349
350 const trend = (yield* getJson(`${baseUrl}/reports/trend`)) as any;
351 expectAuthorized(trend);
352
353 const deleted = (yield* postJson(`${baseUrl}/reports/delete`, {
354 arn: fakeReportArn,
355 })) as any;
356 expectAuthorized(deleted);
357 }),
358 { timeout: 120_000 },
359 );
360 });
361 },
362 );