The best way to control Claude Code, Codex and more from any computer or phone
| 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 backupsearch from "@distilled.cloud/aws/backupsearch"; |
| 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 BackupSearchTestFunctionLive, { |
| 12 | BackupSearchTestFunction, |
| 13 | FIXTURE_SEARCH_JOB_NAME, |
| 14 | } from "./handler.ts"; |
| 15 | |
| 16 | const testOptions = { providers: AWS.providers() }; |
| 17 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 18 | const sharedStack = Core.scratchStack(testOptions, "BackupSearchBindings"); |
| 19 | |
| 20 | // Every deploy starts a real search job, and search jobs CANNOT be deleted — |
| 21 | // terminal records are retained server-side for ~7 days. The suite is gated |
| 22 | // behind AWS_TEST_BACKUP_SEARCH=1 alongside the SearchJob lifecycle test so |
| 23 | // routine CI runs don't accumulate un-deletable job records. |
| 24 | const gated = !process.env.AWS_TEST_BACKUP_SEARCH; |
| 25 | |
| 26 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 27 | // over 60s on a fresh deploy under parallel-suite load. |
| 28 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 29 | |
| 30 | let baseUrl: string; |
| 31 | |
| 32 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 33 | readonly status: number; |
| 34 | readonly body: string; |
| 35 | }> {} |
| 36 | |
| 37 | // Retry transient 5xx from cold re-inits; genuine 4xx fails immediately. |
| 38 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 39 | HttpClient.execute(request).pipe( |
| 40 | Effect.flatMap((response) => |
| 41 | response.status >= 500 |
| 42 | ? response.text.pipe( |
| 43 | Effect.flatMap((body) => |
| 44 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 45 | ), |
| 46 | ) |
| 47 | : Effect.succeed(response), |
| 48 | ), |
| 49 | Effect.retry({ |
| 50 | while: (e) => e._tag === "TransientUpstream", |
| 51 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | // Search jobs cannot be deleted; "orphan-free" means never leaving one |
| 56 | // RUNNING. Stop any RUNNING job with the fixture's deterministic name |
| 57 | // (crash-safe — covers a run that died before the afterAll destroy). |
| 58 | const stopLeakedSearchJobs = Effect.gen(function* () { |
| 59 | const page = yield* backupsearch.listSearchJobs({ ByStatus: "RUNNING", MaxResults: 25 }); |
| 60 | yield* Effect.forEach( |
| 61 | (page.SearchJobs ?? []).filter((job) => job.Name === FIXTURE_SEARCH_JOB_NAME), |
| 62 | (job) => |
| 63 | backupsearch.stopSearchJob({ SearchJobIdentifier: job.SearchJobIdentifier! }).pipe( |
| 64 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 65 | Effect.catchTag("ConflictException", () => Effect.void), |
| 66 | ), |
| 67 | ); |
| 68 | }).pipe(Effect.orDie); |
| 69 | |
| 70 | describe.skipIf(gated)( |
| 71 | "BackupSearch Bindings", |
| 72 | { tags: ["provider:aws", "provider:aws:backupsearch", "provider:aws:lambda", "live"] }, |
| 73 | () => { |
| 74 | beforeAll( |
| 75 | Effect.gen(function* () { |
| 76 | yield* sharedStack.destroy(); |
| 77 | // Pre-clean: the scratch state is in-memory, so the destroy above |
| 78 | // cannot see a job leaked by a crashed prior run. |
| 79 | yield* Core.withProviders(stopLeakedSearchJobs, testOptions, sharedStack.name); |
| 80 | |
| 81 | const { functionUrl } = yield* sharedStack.deploy( |
| 82 | Effect.gen(function* () { |
| 83 | return yield* BackupSearchTestFunction; |
| 84 | }).pipe(Effect.provide(BackupSearchTestFunctionLive)), |
| 85 | ); |
| 86 | |
| 87 | expect(functionUrl).toBeTruthy(); |
| 88 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 89 | |
| 90 | yield* HttpClient.get(`${baseUrl}/bindings`).pipe( |
| 91 | Effect.flatMap((response) => |
| 92 | response.status === 200 |
| 93 | ? Effect.succeed(response) |
| 94 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 95 | ), |
| 96 | Effect.retry({ schedule: readinessPolicy }), |
| 97 | ); |
| 98 | }), |
| 99 | { timeout: 240_000 }, |
| 100 | ); |
| 101 | |
| 102 | afterAll( |
| 103 | sharedStack |
| 104 | .destroy() |
| 105 | .pipe( |
| 106 | Effect.ensuring(Core.withProviders(stopLeakedSearchJobs, testOptions, sharedStack.name)), |
| 107 | ), |
| 108 | { timeout: 120_000 }, |
| 109 | ); |
| 110 | |
| 111 | describe("binding registration", () => { |
| 112 | test.provider("all capabilities initialize in the runtime", (_stack) => |
| 113 | Effect.gen(function* () { |
| 114 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/bindings`)).pipe( |
| 115 | Effect.flatMap((r) => r.json), |
| 116 | ); |
| 117 | expect((response as any).bound).toEqual([ |
| 118 | "listSearchJobResults", |
| 119 | "listSearchJobBackups", |
| 120 | "getSearchJob", |
| 121 | ]); |
| 122 | }), |
| 123 | ); |
| 124 | }); |
| 125 | |
| 126 | describe("GetSearchJob", () => { |
| 127 | test.provider("reads the fixture search job's status", (_stack) => |
| 128 | Effect.gen(function* () { |
| 129 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/job`)).pipe( |
| 130 | Effect.flatMap((r) => r.json), |
| 131 | ); |
| 132 | expect((response as any).name).toBe(FIXTURE_SEARCH_JOB_NAME); |
| 133 | expect(["RUNNING", "COMPLETED", "STOPPING", "STOPPED", "FAILED"]).toContain( |
| 134 | (response as any).status, |
| 135 | ); |
| 136 | }), |
| 137 | ); |
| 138 | }); |
| 139 | |
| 140 | describe("ListSearchJobResults", () => { |
| 141 | test.provider("lists the fixture search job's results", (_stack) => |
| 142 | Effect.gen(function* () { |
| 143 | // The fixture job matches nothing on purpose — assert the call |
| 144 | // round-trips (IAM grant + identifier injection) with a well-formed |
| 145 | // page rather than any particular hit count. |
| 146 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/results`)).pipe( |
| 147 | Effect.flatMap((r) => r.json), |
| 148 | ); |
| 149 | expect(typeof (response as any).count).toBe("number"); |
| 150 | }), |
| 151 | ); |
| 152 | }); |
| 153 | |
| 154 | describe("ListSearchJobBackups", () => { |
| 155 | test.provider("lists the recovery points the search job covered", (_stack) => |
| 156 | Effect.gen(function* () { |
| 157 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/backups`)).pipe( |
| 158 | Effect.flatMap((r) => r.json), |
| 159 | ); |
| 160 | expect(typeof (response as any).count).toBe("number"); |
| 161 | expect(Array.isArray((response as any).statuses)).toBe(true); |
| 162 | }), |
| 163 | ); |
| 164 | }); |
| 165 | }, |
| 166 | ); |