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 { Application } from "@alchemy.run/aws/EMRServerless"; |
| 3 | import { Role } from "@alchemy.run/aws/IAM"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as emr from "@distilled.cloud/aws/emr-serverless"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import * as Duration from "effect/Duration"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: AWS.providers() }); |
| 13 | |
| 14 | // Deterministic names — same on every run of the test case. |
| 15 | const APP_NAME = "alchemy-test-emrs-app"; |
| 16 | const JOB_APP_NAME = "alchemy-test-emrs-job"; |
| 17 | const RELEASE_LABEL = "emr-7.5.0"; |
| 18 | |
| 19 | // Ungated typed-error probe: proves the distilled error union carries the |
| 20 | // not-found tag the read/observe/delete paths depend on. |
| 21 | test.provider( |
| 22 | "typed error semantics on a nonexistent application", |
| 23 | () => |
| 24 | Effect.gen(function* () { |
| 25 | const error = yield* Effect.flip(emr.getApplication({ applicationId: "00abcdefabcdef01" })); |
| 26 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 27 | }), |
| 28 | { tags: ["provider:aws", "provider:aws:emrserverless", "live"] }, |
| 29 | ); |
| 30 | |
| 31 | // Full ungated lifecycle: an application in the CREATED state is free (billing |
| 32 | // only starts when the application starts workers), and creation is bounded |
| 33 | // (~1 min). create → CREATED, no-op redeploy, in-place update, destroy, |
| 34 | // verify gone. |
| 35 | test.provider( |
| 36 | "application lifecycle: create, no-op, update, destroy, verify gone", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | // Clean slate in case a previous run died mid-flight. |
| 40 | yield* stack.destroy(); |
| 41 | |
| 42 | const deployApp = (idleMinutes: number) => |
| 43 | stack.deploy( |
| 44 | Effect.gen(function* () { |
| 45 | return yield* Application("App", { |
| 46 | applicationName: APP_NAME, |
| 47 | releaseLabel: RELEASE_LABEL, |
| 48 | autoStartConfiguration: { enabled: true }, |
| 49 | autoStopConfiguration: { enabled: true, idleTimeout: Duration.minutes(idleMinutes) }, |
| 50 | tags: { purpose: "alchemy-test" }, |
| 51 | }); |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | const created = yield* deployApp(15); |
| 56 | expect(created.applicationName).toBe(APP_NAME); |
| 57 | // the API echoes the engine type in mixed case ("Spark"/"Hive") |
| 58 | expect(created.type).toBe("Spark"); |
| 59 | expect(created.releaseLabel).toBe(RELEASE_LABEL); |
| 60 | expect(created.applicationArn).toContain("/applications/"); |
| 61 | expect(created.state).toBe("CREATED"); |
| 62 | |
| 63 | // Out-of-band verification via distilled. |
| 64 | const observed = yield* emr.getApplication({ applicationId: created.applicationId }); |
| 65 | expect(observed.application.state).toBe("CREATED"); |
| 66 | expect(observed.application.autoStopConfiguration?.idleTimeoutMinutes).toBe(15); |
| 67 | expect(observed.application.tags?.purpose).toBe("alchemy-test"); |
| 68 | |
| 69 | // The provider's list() enumerates it. |
| 70 | const provider = yield* Provider.findProvider(Application); |
| 71 | const all = yield* provider.list(); |
| 72 | expect(all.some((a) => a.applicationId === created.applicationId)).toBe(true); |
| 73 | |
| 74 | // No-op redeploy: same application, no replacement. |
| 75 | const noop = yield* deployApp(15); |
| 76 | expect(noop.applicationId).toBe(created.applicationId); |
| 77 | |
| 78 | // In-place update: shrink the auto-stop idle timeout. |
| 79 | const updated = yield* deployApp(5); |
| 80 | expect(updated.applicationId).toBe(created.applicationId); |
| 81 | const afterUpdate = yield* emr.getApplication({ applicationId: created.applicationId }); |
| 82 | expect(afterUpdate.application.autoStopConfiguration?.idleTimeoutMinutes).toBe(5); |
| 83 | |
| 84 | // Destroy and verify deletion out-of-band (deleted applications either |
| 85 | // disappear or briefly linger as TERMINATED). |
| 86 | yield* stack.destroy(); |
| 87 | yield* assertApplicationGone(created.applicationId); |
| 88 | }), |
| 89 | { tags: ["provider:aws", "provider:aws:emrserverless", "live"], timeout: 240_000 }, |
| 90 | ); |
| 91 | |
| 92 | // Live JobRun (SparkPi from the EMR image itself — no S3 assets needed). |
| 93 | // Starting the application and running the job bills real worker-seconds and |
| 94 | // takes minutes, so it is skipped under `--fast`. |
| 95 | test.provider.skipIf(!!process.env.FAST)( |
| 96 | "live Spark job run reaches SUCCESS", |
| 97 | (stack) => |
| 98 | Effect.gen(function* () { |
| 99 | yield* stack.destroy(); |
| 100 | |
| 101 | const { app, role } = yield* stack.deploy( |
| 102 | Effect.gen(function* () { |
| 103 | const role = yield* Role("JobRole", { |
| 104 | assumeRolePolicyDocument: { |
| 105 | Version: "2012-10-17", |
| 106 | Statement: [ |
| 107 | { |
| 108 | Effect: "Allow", |
| 109 | Principal: { Service: "emr-serverless.amazonaws.com" }, |
| 110 | Action: ["sts:AssumeRole"], |
| 111 | }, |
| 112 | ], |
| 113 | }, |
| 114 | }); |
| 115 | const app = yield* Application("JobApp", { |
| 116 | applicationName: JOB_APP_NAME, |
| 117 | releaseLabel: RELEASE_LABEL, |
| 118 | autoStopConfiguration: { enabled: true, idleTimeout: "1 minute" }, |
| 119 | }); |
| 120 | return { app, role }; |
| 121 | }), |
| 122 | ); |
| 123 | |
| 124 | const jobToken = yield* Effect.sync(() => crypto.randomUUID()); |
| 125 | const started = yield* emr.startJobRun({ |
| 126 | clientToken: jobToken, |
| 127 | applicationId: app.applicationId, |
| 128 | executionRoleArn: role.roleArn, |
| 129 | jobDriver: { |
| 130 | sparkSubmit: { |
| 131 | entryPoint: "local:///usr/lib/spark/examples/src/main/python/pi.py", |
| 132 | sparkSubmitParameters: |
| 133 | "--conf spark.executor.cores=1 --conf spark.executor.memory=2g --conf spark.executor.instances=1 --conf spark.driver.cores=1 --conf spark.driver.memory=2g", |
| 134 | }, |
| 135 | }, |
| 136 | executionTimeoutMinutes: 10, |
| 137 | }); |
| 138 | |
| 139 | // Poll the job run to a terminal state (app cold-start ~1 min + SparkPi |
| 140 | // ~1-2 min; bounded at ~8 min). |
| 141 | const finished = yield* emr |
| 142 | .getJobRun({ applicationId: app.applicationId, jobRunId: started.jobRunId }) |
| 143 | .pipe( |
| 144 | Effect.map((response) => response.jobRun), |
| 145 | Effect.repeat({ |
| 146 | schedule: Schedule.spaced("10 seconds"), |
| 147 | until: (run) => |
| 148 | run.state === "SUCCESS" || run.state === "FAILED" || run.state === "CANCELLED", |
| 149 | times: 48, |
| 150 | }), |
| 151 | ); |
| 152 | expect(finished.stateDetails).toBeDefined(); |
| 153 | expect(finished.state).toBe("SUCCESS"); |
| 154 | |
| 155 | // Destroy immediately (the provider stops the STARTED application |
| 156 | // before deleting it) and verify gone. |
| 157 | yield* stack.destroy(); |
| 158 | yield* assertApplicationGone(app.applicationId); |
| 159 | }), |
| 160 | { |
| 161 | tags: ["provider:aws", "provider:aws:emrserverless", "provider:aws:iam", "live"], |
| 162 | timeout: 900_000, |
| 163 | }, |
| 164 | ); |
| 165 | |
| 166 | const assertApplicationGone = (applicationId: string) => |
| 167 | Effect.gen(function* () { |
| 168 | const state = yield* emr.getApplication({ applicationId }).pipe( |
| 169 | Effect.map((response) => response.application.state), |
| 170 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone")), |
| 171 | ); |
| 172 | if (state !== "gone" && state !== "TERMINATED") { |
| 173 | return yield* Effect.fail(new Error(`application ${applicationId} still exists (${state})`)); |
| 174 | } |
| 175 | }).pipe( |
| 176 | Effect.retry({ schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(12)]) }), |
| 177 | ); |