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 keyspaces from "@distilled.cloud/aws/keyspaces"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as HttpClient from "effect/http/HttpClient"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import KeyspacesRestoreTestFunctionLive, { |
| 9 | KeyspacesRestoreTestFunction, |
| 10 | } from "./restore-handler.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: AWS.providers() }); |
| 13 | |
| 14 | const RESTORE_KS = "alchemy_restore_test_ks"; |
| 15 | const RESTORED_TABLE = "orders_restored"; |
| 16 | |
| 17 | // The restored table is created out-of-band by the Lambda (RestoreTable |
| 18 | // provisions a brand-new table), so the test owns its cleanup. A table that |
| 19 | // is still RESTORING rejects delete with ConflictException, and a restore can |
| 20 | // stay RESTORING for 10+ minutes; retry bounded (~12 min). |
| 21 | const deleteRestoredTable = keyspaces |
| 22 | .deleteTable({ keyspaceName: RESTORE_KS, tableName: RESTORED_TABLE }) |
| 23 | .pipe( |
| 24 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 25 | Effect.retry({ |
| 26 | while: (e): boolean => e._tag === "ConflictException", |
| 27 | schedule: Schedule.max([Schedule.fixed("6 seconds"), Schedule.recurs(120)]), |
| 28 | }), |
| 29 | // Wait (bounded) until it is actually gone so the keyspace can be |
| 30 | // destroyed without a ConflictException. |
| 31 | Effect.andThen( |
| 32 | keyspaces.getTable({ keyspaceName: RESTORE_KS, tableName: RESTORED_TABLE }).pipe( |
| 33 | Effect.flatMap(() => Effect.fail(new Error(`'${RESTORED_TABLE}' still deleting`))), |
| 34 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 35 | Effect.retry({ |
| 36 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(36)]), |
| 37 | }), |
| 38 | ), |
| 39 | ), |
| 40 | ); |
| 41 | |
| 42 | // keyspaces RestoreTable creates a new table from a source table's |
| 43 | // point-in-time-recovery backup. The ungated probe asserts the distilled |
| 44 | // wiring surfaces typed errors; the live restore (which needs a PITR table + |
| 45 | // a deployed Lambda exercising the RestoreTable binding) is skipped under |
| 46 | // `--fast`. |
| 47 | describe( |
| 48 | "AWS.Keyspaces.RestoreTable", |
| 49 | { tags: ["provider:aws", "provider:aws:keyspaces", "live"] }, |
| 50 | () => { |
| 51 | test.provider( |
| 52 | "restore of a nonexistent source table yields a typed error", |
| 53 | (_stack) => |
| 54 | Effect.gen(function* () { |
| 55 | const error = yield* keyspaces |
| 56 | .restoreTable({ |
| 57 | sourceKeyspaceName: "alchemy_nonexistent_ks", |
| 58 | sourceTableName: "nonexistent_tbl", |
| 59 | targetKeyspaceName: "alchemy_nonexistent_ks", |
| 60 | targetTableName: "restored_tbl", |
| 61 | }) |
| 62 | .pipe(Effect.flip); |
| 63 | expect(["ResourceNotFoundException", "ValidationException"]).toContain(error._tag); |
| 64 | }), |
| 65 | { timeout: 60_000 }, |
| 66 | ); |
| 67 | |
| 68 | test.provider.skipIf(!!process.env.FAST)( |
| 69 | "Lambda restores a PITR table into a new table via the binding", |
| 70 | (stack) => |
| 71 | Effect.gen(function* () { |
| 72 | yield* stack.destroy(); |
| 73 | |
| 74 | const { functionUrl } = yield* stack.deploy( |
| 75 | Effect.gen(function* () { |
| 76 | return yield* KeyspacesRestoreTestFunction; |
| 77 | }).pipe(Effect.provide(KeyspacesRestoreTestFunctionLive)), |
| 78 | ); |
| 79 | expect(functionUrl).toBeTruthy(); |
| 80 | const baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 81 | |
| 82 | // Pre-clean any restored table left over from a previous failed run |
| 83 | // — RestoreTable rejects when the target already exists. |
| 84 | yield* deleteRestoredTable; |
| 85 | |
| 86 | // Drive the deployed Lambda through the RestoreTable binding — |
| 87 | // proves the cassandra:Restore/Select IAM actions and the client |
| 88 | // wiring. Retry through function-URL cold start / IAM propagation. |
| 89 | const body = yield* HttpClient.post(`${baseUrl}/restore`).pipe( |
| 90 | Effect.flatMap((response) => |
| 91 | response.status === 200 |
| 92 | ? response.json |
| 93 | : Effect.fail(new Error(`restore not ready: ${response.status}`)), |
| 94 | ), |
| 95 | Effect.retry({ |
| 96 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]), |
| 97 | }), |
| 98 | ); |
| 99 | const restored = body as { restoredTableARN?: string; error?: string }; |
| 100 | expect(restored.error).toBeUndefined(); |
| 101 | expect(restored.restoredTableARN).toContain( |
| 102 | `/keyspace/${RESTORE_KS}/table/${RESTORED_TABLE}`, |
| 103 | ); |
| 104 | |
| 105 | // Out-of-band cleanup of the restored table, then tear down the |
| 106 | // stack-managed keyspace/table/function. |
| 107 | yield* deleteRestoredTable; |
| 108 | yield* stack.destroy(); |
| 109 | }).pipe(Effect.ensuring(Effect.ignore(deleteRestoredTable))), |
| 110 | { tags: ["provider:aws:lambda"], timeout: 1_500_000 }, |
| 111 | ); |
| 112 | }, |
| 113 | ); |