The best way to control Claude Code, Codex and more from any computer or phone

main
ts 297 lines 11.1 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 backup from "@distilled.cloud/aws/backup";
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 BackupTestFunctionLive, { BackupTestFunction, FIXTURE_VAULT_NAME } from "./handler";
12
13 const testOptions = { providers: AWS.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "BackupBindings");
16
17 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
18 // over 60s on a fresh deploy under parallel-suite load.
19 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
20
21 let baseUrl: string;
22
23 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
24 readonly status: number;
25 readonly body: string;
26 }> {}
27
28 // The shared Lambda fixture occasionally answers a transient 5xx under
29 // full-suite parallel load (cold re-init, IAM propagation on the freshly
30 // attached backup policy that the handler's `Effect.orDie` surfaces as a
31 // 500). Retry only 5xx; a genuine 4xx/assertion failure surfaces
32 // immediately.
33 const send = (request: HttpClientRequest.HttpClientRequest) =>
34 HttpClient.execute(request).pipe(
35 Effect.flatMap((response) =>
36 response.status >= 500
37 ? response.text.pipe(
38 Effect.flatMap((body) =>
39 Effect.fail(new TransientUpstream({ status: response.status, body })),
40 ),
41 )
42 : Effect.succeed(response),
43 ),
44 Effect.retry({
45 while: (e) => e._tag === "TransientUpstream",
46 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
47 }),
48 );
49
50 describe.sequential(
51 "Backup Bindings",
52 {
53 tags: [
54 "provider:aws",
55 "provider:aws:backup",
56 "provider:aws:iam",
57 "provider:aws:lambda",
58 "live",
59 ],
60 },
61 () => {
62 beforeAll(
63 Effect.gen(function* () {
64 yield* Effect.logInfo("Backup test setup: destroying previous resources");
65 yield* sharedStack.destroy();
66
67 yield* Effect.logInfo("Backup test setup: deploying fixture");
68 const { functionUrl } = yield* sharedStack.deploy(
69 Effect.gen(function* () {
70 return yield* BackupTestFunction;
71 }).pipe(Effect.provide(BackupTestFunctionLive)),
72 );
73
74 expect(functionUrl).toBeTruthy();
75 baseUrl = functionUrl!.replace(/\/+$/, "");
76
77 const readinessUrl = `${baseUrl}/bindings`;
78 yield* Effect.logInfo(`Backup test setup: probing readiness at ${readinessUrl}`);
79 yield* HttpClient.get(readinessUrl).pipe(
80 Effect.flatMap((response) =>
81 response.status === 200
82 ? Effect.succeed(response)
83 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
84 ),
85 Effect.tapError((error) =>
86 Effect.logWarning(`Backup test setup: fixture not ready yet (${String(error)})`),
87 ),
88 Effect.retry({ schedule: readinessPolicy }),
89 );
90 }),
91 { timeout: 240_000 },
92 );
93
94 afterAll(
95 Effect.gen(function* () {
96 yield* sharedStack.destroy();
97 // Out-of-band proof the fixture vault is gone (AWS Backup reports a
98 // missing vault as AccessDeniedException, not ResourceNotFoundException).
99 // The raw afterAll context has no providers layer (only `test.provider`
100 // bodies do), so the distilled call must be wrapped in `withProviders`
101 // to satisfy AWS credentials.
102 const vault = yield* Core.withProviders(
103 backup
104 .describeBackupVault({ BackupVaultName: FIXTURE_VAULT_NAME })
105 .pipe(
106 Effect.catchTag(["ResourceNotFoundException", "AccessDeniedException"], () =>
107 Effect.succeed(undefined),
108 ),
109 ),
110 testOptions,
111 "BackupBindings",
112 );
113 expect(vault).toBeUndefined();
114 }),
115 { timeout: 120_000 },
116 );
117
118 describe("binding registration", () => {
119 test.provider("all 20 capabilities initialize in the runtime", (_stack) =>
120 Effect.gen(function* () {
121 const response = yield* send(HttpClientRequest.get(`${baseUrl}/bindings`)).pipe(
122 Effect.flatMap((r) => r.json),
123 );
124 expect((response as any).bound).toHaveLength(20);
125 }),
126 );
127 });
128
129 describe("ListRecoveryPointsByBackupVault", () => {
130 test.provider("lists the fixture vault's recovery points", (_stack) =>
131 Effect.gen(function* () {
132 const response = yield* send(
133 HttpClientRequest.get(`${baseUrl}/list-recovery-points`),
134 ).pipe(Effect.flatMap((r) => r.json));
135 // A fresh vault has zero recovery points — assert the call
136 // round-trips (BackupVaultName injection + vault-scoped IAM).
137 expect((response as any).count).toBe(0);
138 }),
139 );
140 });
141
142 describe("ListBackupJobs", () => {
143 test.provider("lists the account's backup jobs", (_stack) =>
144 Effect.gen(function* () {
145 const response = yield* send(HttpClientRequest.get(`${baseUrl}/list-backup-jobs`)).pipe(
146 Effect.flatMap((r) => r.json),
147 );
148 expect(typeof (response as any).count).toBe("number");
149 }),
150 );
151 });
152
153 describe("ListRestoreJobs", () => {
154 test.provider("lists the account's restore jobs", (_stack) =>
155 Effect.gen(function* () {
156 const response = yield* send(HttpClientRequest.get(`${baseUrl}/list-restore-jobs`)).pipe(
157 Effect.flatMap((r) => r.json),
158 );
159 expect(typeof (response as any).count).toBe("number");
160 }),
161 );
162 });
163
164 describe("ListCopyJobs", () => {
165 test.provider("lists the account's copy jobs", (_stack) =>
166 Effect.gen(function* () {
167 const response = yield* send(HttpClientRequest.get(`${baseUrl}/list-copy-jobs`)).pipe(
168 Effect.flatMap((r) => r.json),
169 );
170 expect(typeof (response as any).count).toBe("number");
171 }),
172 );
173 });
174
175 describe("ListProtectedResources", () => {
176 test.provider("lists the account's protected resources", (_stack) =>
177 Effect.gen(function* () {
178 const response = yield* send(
179 HttpClientRequest.get(`${baseUrl}/list-protected-resources`),
180 ).pipe(Effect.flatMap((r) => r.json));
181 expect(typeof (response as any).count).toBe("number");
182 }),
183 );
184 });
185
186 describe("GetSupportedResourceTypes", () => {
187 test.provider("returns the supported resource types", (_stack) =>
188 Effect.gen(function* () {
189 const response = yield* send(
190 HttpClientRequest.get(`${baseUrl}/supported-resource-types`),
191 ).pipe(Effect.flatMap((r) => r.json));
192 expect((response as any).resourceTypes).toContain("DynamoDB");
193 }),
194 );
195 });
196
197 describe("DescribeBackupJob", () => {
198 test.provider("returns the typed not-found error for an unknown job id", (_stack) =>
199 Effect.gen(function* () {
200 const response = yield* send(
201 HttpClientRequest.get(`${baseUrl}/describe-backup-job-not-found`),
202 ).pipe(Effect.flatMap((r) => r.json));
203 expect((response as any).found).toBe(false);
204 }),
205 );
206 });
207
208 describe("DescribeRecoveryPoint", () => {
209 test.provider("returns the typed not-found error for an unknown recovery point", (_stack) =>
210 Effect.gen(function* () {
211 const response = yield* send(
212 HttpClientRequest.get(`${baseUrl}/describe-recovery-point-not-found`),
213 ).pipe(Effect.flatMap((r) => r.json));
214 expect((response as any).found).toBe(false);
215 }),
216 );
217 });
218
219 describe("GetRecoveryPointRestoreMetadata", () => {
220 test.provider("returns the typed not-found error for an unknown recovery point", (_stack) =>
221 Effect.gen(function* () {
222 const response = yield* send(
223 HttpClientRequest.get(`${baseUrl}/restore-metadata-not-found`),
224 ).pipe(Effect.flatMap((r) => r.json));
225 expect((response as any).found).toBe(false);
226 }),
227 );
228 });
229
230 describe("GetRestoreJobMetadata", () => {
231 test.provider("returns the typed not-found error for an unknown restore job", (_stack) =>
232 Effect.gen(function* () {
233 const response = yield* send(
234 HttpClientRequest.get(`${baseUrl}/restore-job-metadata-not-found`),
235 ).pipe(Effect.flatMap((r) => r.json));
236 expect((response as any).found).toBe(false);
237 }),
238 );
239 });
240
241 describe("PutRestoreValidationResult", () => {
242 // A verdict can only be posted against a live restore-test job (minutes
243 // of provisioning); the fixture drives the binding through its typed
244 // error path — an IAM gap would surface AccessDeniedException (500)
245 // instead of a typed tag.
246 test.provider("surfaces a typed error for an unknown restore job", (_stack) =>
247 Effect.gen(function* () {
248 const response = yield* send(
249 HttpClientRequest.post(`${baseUrl}/put-restore-validation-not-found`),
250 ).pipe(Effect.flatMap((r) => r.json));
251 expect([
252 "ResourceNotFoundException",
253 "InvalidParameterValueException",
254 "MissingParameterValueException",
255 "InvalidRequestException",
256 ]).toContain((response as any).tag);
257 }),
258 );
259 });
260
261 describe("StopBackupJob", () => {
262 test.provider("surfaces a typed error for an unknown job id", (_stack) =>
263 Effect.gen(function* () {
264 const response = yield* send(
265 HttpClientRequest.post(`${baseUrl}/stop-backup-job-not-found`),
266 ).pipe(Effect.flatMap((r) => r.json));
267 expect([
268 "ResourceNotFoundException",
269 "InvalidParameterValueException",
270 "InvalidRequestException",
271 ]).toContain((response as any).tag);
272 }),
273 );
274 });
275
276 describe("StartRestoreJob", () => {
277 // A real restore needs a live recovery point (a completed backup job —
278 // minutes of provisioning). The fixture drives the binding through its
279 // typed error path: role injection + backup:StartRestoreJob +
280 // iam:PassRole are all exercised, and an IAM gap would surface
281 // AccessDeniedException (a 500) instead of the typed tag.
282 test.provider("surfaces a typed error for an unknown recovery point", (_stack) =>
283 Effect.gen(function* () {
284 const response = yield* send(
285 HttpClientRequest.post(`${baseUrl}/start-restore-not-found`),
286 ).pipe(Effect.flatMap((r) => r.json));
287 expect([
288 "ResourceNotFoundException",
289 "InvalidParameterValueException",
290 "MissingParameterValueException",
291 "InvalidRequestException",
292 ]).toContain((response as any).tag);
293 }),
294 );
295 });
296 },
297 );