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, 06:00 UTC
| 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 s3 from "@distilled.cloud/aws/s3"; |
| 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 DataBrewTestFunctionLive, { DataBrewTestFunction, foundation, SOURCE_KEY } from "./handler"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "DataBrewBindings"); |
| 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 | // Retry transient 5xx from the shared Lambda fixture (cold re-init, IAM |
| 29 | // propagation on the freshly attached databrew policy surfaced as a 500 by |
| 30 | // the handler's `Effect.orDie`). Genuine 4xx/assertion failures return |
| 31 | // immediately. |
| 32 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 33 | HttpClient.execute(request).pipe( |
| 34 | Effect.flatMap((response) => |
| 35 | response.status >= 500 |
| 36 | ? response.text.pipe( |
| 37 | Effect.flatMap((body) => |
| 38 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 39 | ), |
| 40 | ) |
| 41 | : Effect.succeed(response), |
| 42 | ), |
| 43 | Effect.retry({ |
| 44 | while: (e) => e._tag === "TransientUpstream", |
| 45 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | const getJson = (url: string) => |
| 50 | send(HttpClientRequest.get(url)).pipe(Effect.flatMap((r) => r.json)); |
| 51 | |
| 52 | const postJson = (url: string, body: unknown) => |
| 53 | send(HttpClientRequest.post(url).pipe(HttpClientRequest.bodyJsonUnsafe(body))).pipe( |
| 54 | Effect.flatMap((r) => r.json), |
| 55 | ); |
| 56 | |
| 57 | const post = (url: string) => send(HttpClientRequest.post(url)).pipe(Effect.flatMap((r) => r.json)); |
| 58 | |
| 59 | /** |
| 60 | * A route answered with a typed error tag. The tag being present proves the |
| 61 | * binding produced a typed failure (untyped errors crash into a 500), and it |
| 62 | * not being an authorization tag proves the IAM grant covered the call. |
| 63 | */ |
| 64 | const expectTypedNonAuthz = (body: any) => { |
| 65 | expect(typeof body.errorTag).toBe("string"); |
| 66 | expect(body.errorTag).not.toBe("AccessDeniedException"); |
| 67 | }; |
| 68 | |
| 69 | /** A route that either succeeded or failed with a typed, authorized tag. */ |
| 70 | const expectAuthorized = (body: any) => { |
| 71 | if (body.errorTag !== undefined) expectTypedNonAuthz(body); |
| 72 | }; |
| 73 | |
| 74 | class RunStillActive extends Data.TaggedError("RunStillActive") {} |
| 75 | |
| 76 | /** Poll a run until it leaves STARTING/RUNNING (bounded). */ |
| 77 | const waitForInactive = (runId: string) => |
| 78 | getJson(`${baseUrl}/run/get?id=${encodeURIComponent(runId)}`).pipe( |
| 79 | Effect.flatMap((body: any) => |
| 80 | body.state === undefined || body.state === "STARTING" || body.state === "RUNNING" |
| 81 | ? Effect.fail(new RunStillActive()) |
| 82 | : Effect.succeed(body.state as string), |
| 83 | ), |
| 84 | Effect.retry({ |
| 85 | while: (e): boolean => e._tag === "RunStillActive", |
| 86 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(12)]), |
| 87 | }), |
| 88 | ); |
| 89 | |
| 90 | describe.sequential( |
| 91 | "DataBrew Bindings", |
| 92 | { |
| 93 | tags: [ |
| 94 | "provider:aws", |
| 95 | "provider:aws:databrew", |
| 96 | "provider:aws:iam", |
| 97 | "provider:aws:lambda", |
| 98 | "provider:aws:s3", |
| 99 | "live", |
| 100 | ], |
| 101 | }, |
| 102 | () => { |
| 103 | beforeAll( |
| 104 | Effect.gen(function* () { |
| 105 | yield* Effect.logInfo("DataBrew test setup: destroying previous"); |
| 106 | yield* sharedStack.destroy(); |
| 107 | |
| 108 | // Stage 1: foundation only — the CSV must exist in the bucket before |
| 109 | // the job/project are created (DataBrew validates the dataset's |
| 110 | // source with the job role at Create{Profile}Job/CreateProject time). |
| 111 | yield* Effect.logInfo("DataBrew test setup: deploying foundation"); |
| 112 | const base = yield* sharedStack.deploy(foundation); |
| 113 | // `beforeAll` doesn't run inside `test.provider`'s environment, so |
| 114 | // provide the AWS providers (Credentials/Region) explicitly for the |
| 115 | // out-of-band distilled call. |
| 116 | yield* Core.withProviders( |
| 117 | s3.putObject({ |
| 118 | Bucket: base.bucket.bucketName, |
| 119 | Key: SOURCE_KEY, |
| 120 | Body: new TextEncoder().encode("id,name\n1,alice\n2,bob\n"), |
| 121 | ContentType: "text/csv", |
| 122 | }), |
| 123 | testOptions, |
| 124 | "DataBrewBindingsSeed", |
| 125 | ); |
| 126 | |
| 127 | // Stage 2: the full fixture (job, project, Lambda + bindings). |
| 128 | yield* Effect.logInfo("DataBrew test setup: deploying fixture"); |
| 129 | const { functionUrl } = yield* sharedStack.deploy( |
| 130 | Effect.gen(function* () { |
| 131 | return yield* DataBrewTestFunction; |
| 132 | }).pipe(Effect.provide(DataBrewTestFunctionLive)), |
| 133 | ); |
| 134 | |
| 135 | expect(functionUrl).toBeTruthy(); |
| 136 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 137 | const readinessUrl = `${baseUrl}/run/list`; |
| 138 | |
| 139 | yield* Effect.logInfo(`DataBrew test setup: probing readiness at ${readinessUrl}`); |
| 140 | // Ready = the function answers 200 AND the freshly attached databrew |
| 141 | // policy has propagated (an AccessDeniedException errorTag means IAM |
| 142 | // is still converging — keep probing). |
| 143 | yield* HttpClient.get(readinessUrl).pipe( |
| 144 | Effect.flatMap((response) => |
| 145 | response.status === 200 |
| 146 | ? response.json |
| 147 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 148 | ), |
| 149 | Effect.flatMap((body: any) => |
| 150 | body.errorTag === undefined |
| 151 | ? Effect.succeed(body) |
| 152 | : Effect.fail(new Error(`IAM not propagated: ${body.errorTag}`)), |
| 153 | ), |
| 154 | Effect.retry({ schedule: readinessPolicy }), |
| 155 | ); |
| 156 | }), |
| 157 | { timeout: 480_000 }, |
| 158 | ); |
| 159 | |
| 160 | afterAll.skipIf(!!process.env.NO_DESTROY)(sharedStack.destroy(), { timeout: 240_000 }); |
| 161 | |
| 162 | describe("StartJobRun + ListJobRuns + DescribeJobRun + StopJobRun", () => { |
| 163 | test.provider( |
| 164 | "starts a profile run, observes it, stops it", |
| 165 | (_stack) => |
| 166 | Effect.gen(function* () { |
| 167 | // StartJobRun |
| 168 | const started = (yield* post(`${baseUrl}/run/start`)) as any; |
| 169 | expect(started.errorTag).toBeUndefined(); |
| 170 | expect(typeof started.runId).toBe("string"); |
| 171 | const runId = started.runId as string; |
| 172 | |
| 173 | // ListJobRuns sees it |
| 174 | const listed = (yield* getJson(`${baseUrl}/run/list`)) as any; |
| 175 | expect(listed.errorTag).toBeUndefined(); |
| 176 | expect(listed.runIds).toContain(runId); |
| 177 | |
| 178 | // DescribeJobRun reports a state |
| 179 | const described = (yield* getJson( |
| 180 | `${baseUrl}/run/get?id=${encodeURIComponent(runId)}`, |
| 181 | )) as any; |
| 182 | expect(described.errorTag).toBeUndefined(); |
| 183 | expect(typeof described.state).toBe("string"); |
| 184 | |
| 185 | // StopJobRun — don't leave managed Spark capacity spinning. |
| 186 | const stopped = (yield* postJson(`${baseUrl}/run/stop`, { id: runId })) as any; |
| 187 | expectAuthorized(stopped); |
| 188 | |
| 189 | // The run leaves STARTING/RUNNING (bounded poll). |
| 190 | const state = yield* waitForInactive(runId); |
| 191 | expect(["STOPPING", "STOPPED", "FAILED", "TIMEOUT"]).toContain(state); |
| 192 | }), |
| 193 | { timeout: 240_000 }, |
| 194 | ); |
| 195 | }); |
| 196 | |
| 197 | describe("PublishRecipe", () => { |
| 198 | test.provider("publishes a new numbered version", (_stack) => |
| 199 | Effect.gen(function* () { |
| 200 | const body = (yield* post(`${baseUrl}/recipe/publish`)) as any; |
| 201 | expect(body.errorTag).toBeUndefined(); |
| 202 | expect(typeof body.name).toBe("string"); |
| 203 | }), |
| 204 | ); |
| 205 | }); |
| 206 | |
| 207 | describe("StartProjectSession + SendProjectSessionAction", () => { |
| 208 | test.provider( |
| 209 | "opens an interactive session and sends a preview action", |
| 210 | (_stack) => |
| 211 | Effect.gen(function* () { |
| 212 | const body = (yield* post(`${baseUrl}/session/run`)) as any; |
| 213 | // Session provisioning is asynchronous — success and a typed |
| 214 | // rejection both prove the IAM grants and the Redacted |
| 215 | // ClientSessionId round-trip. |
| 216 | expectAuthorized(body.started ?? body); |
| 217 | if (body.started?.hasSessionId === true) { |
| 218 | expect(body.started.name).toBeTruthy(); |
| 219 | expectAuthorized(body.action); |
| 220 | } |
| 221 | }), |
| 222 | { timeout: 120_000 }, |
| 223 | ); |
| 224 | }); |
| 225 | }, |
| 226 | ); |