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 serverless from "@distilled.cloud/aws/redshift-serverless"; |
| 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 SnapshotFunctionLive, { SnapshotFunction } from "./fixtures/snapshot-handler"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "RedshiftSnapshotBindings"); |
| 15 | |
| 16 | // Ungated typed-error probes: prove the distilled error union carries the |
| 17 | // not-found tag the snapshot bindings can surface. Runs in every CI pass at |
| 18 | // near-zero cost. |
| 19 | test.provider( |
| 20 | "getSnapshot on a nonexistent snapshot fails with ResourceNotFoundException", |
| 21 | () => |
| 22 | Effect.gen(function* () { |
| 23 | const error = yield* Effect.flip( |
| 24 | serverless.getSnapshot({ snapshotName: "alchemy-nonexistent-rssnap-probe" }), |
| 25 | ); |
| 26 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 27 | }), |
| 28 | { tags: ["provider:aws", "provider:aws:redshiftserverless", "live"] }, |
| 29 | ); |
| 30 | |
| 31 | test.provider( |
| 32 | "deleteSnapshot on a nonexistent snapshot fails with ResourceNotFoundException", |
| 33 | () => |
| 34 | Effect.gen(function* () { |
| 35 | const error = yield* Effect.flip( |
| 36 | serverless.deleteSnapshot({ snapshotName: "alchemy-nonexistent-rssnap-probe" }), |
| 37 | ); |
| 38 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 39 | }), |
| 40 | { tags: ["provider:aws", "provider:aws:redshiftserverless", "live"] }, |
| 41 | ); |
| 42 | |
| 43 | const SNAPSHOT_NAME = "alchemy-test-rssnap-snapshot"; |
| 44 | |
| 45 | let baseUrl: string; |
| 46 | |
| 47 | // The full lifecycle deploys a Redshift Serverless namespace + workgroup |
| 48 | // (CreateSnapshot requires the namespace to have a workgroup; it bills an RPU |
| 49 | // floor while it exists, ~1-2 min to provision), so it is gated behind |
| 50 | // AWS_TEST_REDSHIFT=1 and destroys everything in afterAll. |
| 51 | describe.skipIf(!process.env.AWS_TEST_REDSHIFT)( |
| 52 | "RedshiftServerless Snapshot Bindings", |
| 53 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:redshiftserverless", "live"] }, |
| 54 | () => { |
| 55 | beforeAll( |
| 56 | Effect.gen(function* () { |
| 57 | yield* Effect.logInfo("Snapshot bindings setup: destroying previous run"); |
| 58 | yield* sharedStack.destroy(); |
| 59 | |
| 60 | yield* Effect.logInfo("Snapshot bindings setup: deploying fixture"); |
| 61 | const { functionUrl } = yield* sharedStack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | return yield* SnapshotFunction; |
| 64 | }).pipe(Effect.provide(SnapshotFunctionLive)), |
| 65 | ); |
| 66 | |
| 67 | expect(functionUrl).toBeTruthy(); |
| 68 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 69 | yield* Effect.logInfo(`Snapshot bindings setup: function URL ready (${functionUrl})`); |
| 70 | }), |
| 71 | // namespace (~1 min) + workgroup (~1-2 min) + Lambda deploy. |
| 72 | { timeout: 600_000 }, |
| 73 | ); |
| 74 | |
| 75 | afterAll(sharedStack.destroy(), { timeout: 600_000 }); |
| 76 | |
| 77 | const json = ( |
| 78 | request: HttpClientRequest.HttpClientRequest, |
| 79 | ): Effect.Effect<unknown, unknown, HttpClient.HttpClient> => |
| 80 | HttpClient.execute(request).pipe( |
| 81 | Effect.flatMap((res) => |
| 82 | res.status === 200 |
| 83 | ? res.json |
| 84 | : Effect.flatMap(res.text, (body) => |
| 85 | Effect.fail(new Error(`${request.url} -> ${res.status}: ${body}`)), |
| 86 | ), |
| 87 | ), |
| 88 | ); |
| 89 | |
| 90 | test.provider( |
| 91 | "create, poll, list, update, and delete a snapshot through the deployed bindings", |
| 92 | (_stack) => |
| 93 | Effect.gen(function* () { |
| 94 | // Create — retried while the fresh function URL warms up. |
| 95 | const created = (yield* json( |
| 96 | HttpClientRequest.post(`${baseUrl}/snapshot?name=${SNAPSHOT_NAME}`), |
| 97 | ).pipe(Effect.retry({ schedule: Schedule.exponential("1 second"), times: 6 }))) as { |
| 98 | snapshotName?: string; |
| 99 | status?: string; |
| 100 | }; |
| 101 | expect(created.snapshotName).toBe(SNAPSHOT_NAME); |
| 102 | |
| 103 | // Poll via GetSnapshot until the snapshot settles to AVAILABLE. |
| 104 | const settled = (yield* json( |
| 105 | HttpClientRequest.get(`${baseUrl}/snapshot?name=${SNAPSHOT_NAME}`), |
| 106 | ).pipe( |
| 107 | Effect.repeat({ |
| 108 | schedule: Schedule.spaced("5 seconds"), |
| 109 | until: (body): boolean => |
| 110 | (body as { status?: string }).status?.toUpperCase() === "AVAILABLE", |
| 111 | times: 36, |
| 112 | }), |
| 113 | )) as { |
| 114 | snapshotName?: string; |
| 115 | namespaceName?: string; |
| 116 | retentionPeriod?: number; |
| 117 | status?: string; |
| 118 | }; |
| 119 | expect(settled.status?.toUpperCase()).toBe("AVAILABLE"); |
| 120 | expect(settled.namespaceName).toBe("alchemy-test-rssnap-ns"); |
| 121 | expect(settled.retentionPeriod).toBe(1); |
| 122 | |
| 123 | // ListSnapshots filtered by the bound namespace sees it. |
| 124 | const listed = (yield* json(HttpClientRequest.get(`${baseUrl}/snapshots`))) as { |
| 125 | names: (string | undefined)[]; |
| 126 | }; |
| 127 | expect(listed.names).toContain(SNAPSHOT_NAME); |
| 128 | |
| 129 | // UpdateSnapshot extends the retention period. |
| 130 | const updated = (yield* json( |
| 131 | HttpClientRequest.post(`${baseUrl}/snapshot/retention?name=${SNAPSHOT_NAME}`), |
| 132 | )) as { retentionPeriod?: number }; |
| 133 | expect(updated.retentionPeriod).toBe(2); |
| 134 | |
| 135 | // Recovery-point + table-restore listings respond (may be empty |
| 136 | // for a fresh, never-computed namespace). |
| 137 | const recovery = (yield* json(HttpClientRequest.get(`${baseUrl}/recovery-points`))) as { |
| 138 | count: number; |
| 139 | }; |
| 140 | expect(recovery.count).toBeGreaterThanOrEqual(0); |
| 141 | const restores = (yield* json(HttpClientRequest.get(`${baseUrl}/table-restores`))) as { |
| 142 | count: number; |
| 143 | }; |
| 144 | expect(restores.count).toBeGreaterThanOrEqual(0); |
| 145 | |
| 146 | // DeleteSnapshot removes it; out-of-band read confirms it is gone. |
| 147 | yield* json(HttpClientRequest.delete(`${baseUrl}/snapshot?name=${SNAPSHOT_NAME}`)); |
| 148 | const gone = yield* serverless.getSnapshot({ snapshotName: SNAPSHOT_NAME }).pipe( |
| 149 | Effect.map((r) => r.snapshot?.status ?? "PRESENT"), |
| 150 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("GONE" as const)), |
| 151 | Effect.repeat({ |
| 152 | schedule: Schedule.spaced("5 seconds"), |
| 153 | until: (status): boolean => status === "GONE" || status === "DELETING", |
| 154 | times: 12, |
| 155 | }), |
| 156 | ); |
| 157 | expect(["GONE", "DELETING"]).toContain(gone); |
| 158 | }), |
| 159 | { timeout: 420_000 }, |
| 160 | ); |
| 161 | }, |
| 162 | ); |