@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 222 lines 8.3 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 CodeDeployTestFunctionLive, { CodeDeployTestFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "CodeDeployBindings");
15
16 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17 // over 60s on a fresh deploy under parallel-suite load.
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 // Retry transient 5xx from the shared Lambda fixture (cold re-init, IAM
28 // propagation on the freshly attached codedeploy policy surfaced as a 500
29 // by the handler's `Effect.orDie`). Genuine 4xx/assertion failures return
30 // 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 = (url: string) =>
49 send(HttpClientRequest.get(url)).pipe(Effect.flatMap((r) => r.json));
50
51 const postJson = (url: string, body: unknown) =>
52 send(HttpClientRequest.post(url).pipe(HttpClientRequest.bodyJsonUnsafe(body))).pipe(
53 Effect.flatMap((r) => r.json),
54 );
55
56 const post = (url: string) => send(HttpClientRequest.post(url)).pipe(Effect.flatMap((r) => r.json));
57
58 /**
59 * A route answered with a typed error tag. The tag being present proves the
60 * binding produced a typed failure (untyped errors crash into a 500), and it
61 * not being an authorization tag proves the IAM grant covered the call.
62 */
63 const expectTypedNonAuthz = (body: any) => {
64 expect(typeof body.errorTag).toBe("string");
65 expect(body.errorTag).not.toBe("AccessDeniedException");
66 };
67
68 /** A route that either succeeded or failed with a typed, authorized tag. */
69 const expectAuthorized = (body: any) => {
70 if (body.errorTag !== undefined) expectTypedNonAuthz(body);
71 };
72
73 // A syntactically plausible, nonexistent deployment id/target id.
74 const FAKE_DEPLOYMENT_ID = "d-AAAAAAAAA";
75 const FAKE_TARGET_ID = "alchemy-test-nonexistent-function";
76
77 describe.sequential(
78 "CodeDeploy Bindings",
79 {
80 tags: [
81 "provider:aws",
82 "provider:aws:batch",
83 "provider:aws:codedeploy",
84 "provider:aws:iam",
85 "provider:aws:lambda",
86 "live",
87 ],
88 },
89 () => {
90 beforeAll(
91 Effect.gen(function* () {
92 yield* Effect.logInfo("CodeDeploy test setup: destroying previous");
93 yield* sharedStack.destroy();
94
95 yield* Effect.logInfo("CodeDeploy test setup: deploying fixture");
96 const { functionUrl } = yield* sharedStack.deploy(
97 Effect.gen(function* () {
98 return yield* CodeDeployTestFunction;
99 }).pipe(Effect.provide(CodeDeployTestFunctionLive)),
100 );
101
102 expect(functionUrl).toBeTruthy();
103 baseUrl = functionUrl!.replace(/\/+$/, "");
104 const readinessUrl = `${baseUrl}/deployment/list`;
105
106 yield* Effect.logInfo(`CodeDeploy test setup: probing readiness at ${readinessUrl}`);
107 // Ready = the function answers 200 AND the freshly attached codedeploy
108 // policy has propagated (an AccessDeniedException errorTag means IAM
109 // is still converging — keep probing).
110 yield* HttpClient.get(readinessUrl).pipe(
111 Effect.flatMap((response) =>
112 response.status === 200
113 ? response.json
114 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
115 ),
116 Effect.flatMap((body: any) =>
117 body.errorTag === undefined
118 ? Effect.succeed(body)
119 : Effect.fail(new Error(`IAM not propagated: ${body.errorTag}`)),
120 ),
121 Effect.retry({ schedule: readinessPolicy }),
122 );
123 }),
124 { timeout: 300_000 },
125 );
126
127 afterAll.skipIf(!!process.env.NO_DESTROY)(sharedStack.destroy(), { timeout: 240_000 });
128
129 describe("CreateDeployment", () => {
130 test.provider("answers the typed RevisionRequiredException without a revision", (_stack) =>
131 Effect.gen(function* () {
132 // No revision — CodeDeploy rejects with the TYPED
133 // RevisionRequiredException, which proves the name injection,
134 // the binding wiring, and the IAM grant all work.
135 const body = (yield* post(`${baseUrl}/deployment/create`)) as any;
136 expect(body.errorTag).toBe("RevisionRequiredException");
137 }),
138 );
139 });
140
141 describe("ListDeployments + GetDeployment + BatchGetDeployments", () => {
142 test.provider("lists (empty) and answers typed for unknown ids", (_stack) =>
143 Effect.gen(function* () {
144 const listed = (yield* getJson(`${baseUrl}/deployment/list`)) as any;
145 expect(listed.errorTag).toBeUndefined();
146 expect(listed.deployments).toEqual([]);
147
148 const got = (yield* getJson(`${baseUrl}/deployment/get?id=${FAKE_DEPLOYMENT_ID}`)) as any;
149 expectTypedNonAuthz(got);
150
151 const batch = (yield* getJson(
152 `${baseUrl}/deployment/batch-get?id=${FAKE_DEPLOYMENT_ID}`,
153 )) as any;
154 expectAuthorized(batch);
155 }),
156 );
157 });
158
159 describe("StopDeployment + ContinueDeployment + PutLifecycleEventHookExecutionStatus", () => {
160 test.provider("answer typed for unknown deployments", (_stack) =>
161 Effect.gen(function* () {
162 const stopped = (yield* postJson(`${baseUrl}/deployment/stop`, {
163 id: FAKE_DEPLOYMENT_ID,
164 })) as any;
165 expectTypedNonAuthz(stopped);
166
167 const continued = (yield* postJson(`${baseUrl}/deployment/continue`, {
168 id: FAKE_DEPLOYMENT_ID,
169 })) as any;
170 expectTypedNonAuthz(continued);
171
172 const hook = (yield* postJson(`${baseUrl}/hook/status`, {
173 deploymentId: FAKE_DEPLOYMENT_ID,
174 executionId: "00000000-0000-0000-0000-000000000000",
175 })) as any;
176 expectTypedNonAuthz(hook);
177 }),
178 );
179 });
180
181 describe("Deployment targets", () => {
182 test.provider("target bindings answer typed for unknown ids", (_stack) =>
183 Effect.gen(function* () {
184 const got = (yield* getJson(
185 `${baseUrl}/target/get?deploymentId=${FAKE_DEPLOYMENT_ID}&targetId=${FAKE_TARGET_ID}`,
186 )) as any;
187 expectTypedNonAuthz(got);
188
189 const listed = (yield* getJson(
190 `${baseUrl}/target/list?deploymentId=${FAKE_DEPLOYMENT_ID}`,
191 )) as any;
192 expectTypedNonAuthz(listed);
193
194 const batch = (yield* getJson(
195 `${baseUrl}/target/batch-get?deploymentId=${FAKE_DEPLOYMENT_ID}&targetId=${FAKE_TARGET_ID}`,
196 )) as any;
197 expectTypedNonAuthz(batch);
198 }),
199 );
200 });
201
202 describe("Revisions", () => {
203 test.provider("register + get + list + batch-get against the bound application", (_stack) =>
204 Effect.gen(function* () {
205 // Registering an S3 revision records metadata only — the bundle
206 // is not fetched until deployment, so this succeeds.
207 const registered = (yield* post(`${baseUrl}/revision/register`)) as any;
208 expectAuthorized(registered);
209
210 const got = (yield* getJson(`${baseUrl}/revision/get`)) as any;
211 expectAuthorized(got);
212
213 const listed = (yield* getJson(`${baseUrl}/revision/list`)) as any;
214 expect(listed.errorTag).toBeUndefined();
215
216 const batch = (yield* getJson(`${baseUrl}/revision/batch-get`)) as any;
217 expectAuthorized(batch);
218 }),
219 );
220 });
221 },
222 );