The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** @effect-diagnostics anyUnknownInErrorContext:off */ |
| 2 | import { it } from "@effect/vitest"; |
| 3 | import type { AlchemyContext } from "alchemy/AlchemyContext"; |
| 4 | import type { CompiledStack, StackDefinition } from "alchemy/Stack"; |
| 5 | import type { Stage } from "alchemy/Stage"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Exit from "effect/Exit"; |
| 8 | import * as Scope from "effect/Scope"; |
| 9 | import { |
| 10 | afterAll as vitestAfterAll, |
| 11 | afterEach as vitestAfterEach, |
| 12 | beforeAll as vitestBeforeAll, |
| 13 | beforeEach as vitestBeforeEach, |
| 14 | } from "vitest"; |
| 15 | import * as Core from "./Core.ts"; |
| 16 | |
| 17 | export { |
| 18 | executeWhenReady, |
| 19 | getWhenReady, |
| 20 | guardContentType, |
| 21 | guardedFetchLayer, |
| 22 | rpcClientLayer, |
| 23 | WorkerNotReady, |
| 24 | type EdgeGuardOptions, |
| 25 | type WhenReadyOptions, |
| 26 | } from "./Http.ts"; |
| 27 | |
| 28 | export type MakeOptions<ROut = any> = Core.MakeOptions<ROut>; |
| 29 | export type ScratchStack = Core.ScratchStack; |
| 30 | export type TestEffect<A, R = never> = Core.TestEffect<A, R>; |
| 31 | export const defaultStage = Core.defaultStage; |
| 32 | export const resolveStage = Core.resolveStage; |
| 33 | |
| 34 | type TestOptions = number | { timeout?: number }; |
| 35 | |
| 36 | const timeoutOf = (opts: TestOptions | undefined): number | undefined => |
| 37 | typeof opts === "number" ? opts : opts?.timeout; |
| 38 | |
| 39 | interface TestFn { |
| 40 | (name: string, eff: TestEffect<void>, options?: TestOptions): void; |
| 41 | skip: (name: string, eff: TestEffect<void>, options?: TestOptions) => void; |
| 42 | skipIf: ( |
| 43 | condition: boolean, |
| 44 | ) => (name: string, eff: TestEffect<void>, options?: TestOptions) => void; |
| 45 | only: (name: string, eff: TestEffect<void>, options?: TestOptions) => void; |
| 46 | todo: (name: string, eff: TestEffect<void>, options?: TestOptions) => void; |
| 47 | provider: ProviderFn; |
| 48 | } |
| 49 | |
| 50 | interface ProviderFn { |
| 51 | ( |
| 52 | name: string, |
| 53 | fn: (stack: ScratchStack) => Effect.Effect<void, any, any>, |
| 54 | options?: TestOptions, |
| 55 | ): void; |
| 56 | skip: ( |
| 57 | name: string, |
| 58 | fn: (stack: ScratchStack) => Effect.Effect<void, any, any>, |
| 59 | options?: TestOptions, |
| 60 | ) => void; |
| 61 | skipIf: ( |
| 62 | condition: boolean, |
| 63 | ) => ( |
| 64 | name: string, |
| 65 | fn: (stack: ScratchStack) => Effect.Effect<void, any, any>, |
| 66 | options?: TestOptions, |
| 67 | ) => void; |
| 68 | } |
| 69 | |
| 70 | interface BeforeAllFn { |
| 71 | <A>(eff: TestEffect<A>, options?: TestOptions): Effect.Effect<A>; |
| 72 | } |
| 73 | |
| 74 | interface BeforeEachFn { |
| 75 | (eff: TestEffect<void>, options?: TestOptions): void; |
| 76 | } |
| 77 | |
| 78 | interface AfterAllFn { |
| 79 | (eff: TestEffect<any>, options?: TestOptions): void; |
| 80 | skipIf: (predicate: boolean) => (eff: TestEffect<any>, options?: TestOptions) => void; |
| 81 | } |
| 82 | |
| 83 | interface AfterEachFn { |
| 84 | (eff: TestEffect<void>, options?: TestOptions): void; |
| 85 | } |
| 86 | |
| 87 | export interface TestApi { |
| 88 | test: TestFn; |
| 89 | beforeAll: BeforeAllFn; |
| 90 | beforeEach: BeforeEachFn; |
| 91 | afterAll: AfterAllFn; |
| 92 | afterEach: AfterEachFn; |
| 93 | deploy: Core.Deploy; |
| 94 | destroy: <ROut>( |
| 95 | stack: StackDefinition<ROut>, |
| 96 | options?: { stage?: string; include?: never; exclude?: never }, |
| 97 | ) => ReturnType<typeof Core.destroy>; |
| 98 | } |
| 99 | |
| 100 | const DEFAULT_TIMEOUT = 120_000; |
| 101 | |
| 102 | /** |
| 103 | * Build the per-file test API. See {@link "./Bun.ts"} for the same shape |
| 104 | * over `bun:test`. Vitest variant uses `@effect/vitest`'s `it.live` so |
| 105 | * Effect-aware tests stay first-class. |
| 106 | */ |
| 107 | export const make = <ROut = any>(options: MakeOptions<ROut>): TestApi => { |
| 108 | // See `Test/Bun.ts` for the rationale: the dev sidecar must outlive a |
| 109 | // single `runPromise` boundary, so all hooks share one scope that's only |
| 110 | // closed after `destroy(...)` runs. |
| 111 | const sharedScope = Scope.makeUnsafe("sequential"); |
| 112 | // In dev mode, run local providers behind one file-scoped RPC sidecar |
| 113 | // (the `alchemy dev` topology). Lives in its own scope — NOT sharedScope, |
| 114 | // which `destroy(Stack)` closes mid-file in self-contained tests — and is |
| 115 | // closed by the fallback afterAll below. |
| 116 | const sidecar = Core.makeSidecarHandle(options); |
| 117 | const wrap = <A>(eff: TestEffect<A>) => Core.toEffect(eff, options, sharedScope, sidecar); |
| 118 | const runEff = <A>(eff: TestEffect<A>) => Core.run(eff, options, sharedScope, sidecar); |
| 119 | |
| 120 | const test = ((name, eff, opts) => { |
| 121 | it.live(name, () => wrap(eff), timeoutOf(opts)); |
| 122 | }) as TestFn; |
| 123 | |
| 124 | test.skip = (name, _eff, opts) => { |
| 125 | it.skip(name, () => {}, timeoutOf(opts)); |
| 126 | }; |
| 127 | test.skipIf = (condition) => (name, eff, opts) => { |
| 128 | if (condition) { |
| 129 | it.skip(name, () => {}, timeoutOf(opts)); |
| 130 | } else { |
| 131 | it.live(name, () => wrap(eff), timeoutOf(opts)); |
| 132 | } |
| 133 | }; |
| 134 | test.only = (name, eff, opts) => { |
| 135 | it.live.only(name, () => wrap(eff), timeoutOf(opts)); |
| 136 | }; |
| 137 | test.todo = (name, _eff, _opts) => { |
| 138 | it.todo(name); |
| 139 | }; |
| 140 | |
| 141 | const wrapProvider = ( |
| 142 | name: string, |
| 143 | fn: (stack: ScratchStack) => Effect.Effect<void, any, any>, |
| 144 | ) => { |
| 145 | const scratch = Core.scratchStack(options, name); |
| 146 | // Guarantee teardown. `test.provider` has no built-in cleanup, so a body |
| 147 | // that fails (assertion, API error like a 409/Unauthorized) or is |
| 148 | // interrupted (vitest timeout) BEFORE its trailing `stack.destroy()` would |
| 149 | // otherwise leak every cloud resource it deployed: the scratch's in-memory |
| 150 | // state is discarded with the process, so no later run can reclaim the |
| 151 | // orphan (only an account-wide `nuke` can). `scratch.destroy()` is |
| 152 | // idempotent (empty-plan apply against the shared scratch state) — a no-op |
| 153 | // when the body already destroyed, and it reclaims the orphans otherwise. |
| 154 | // `Effect.ensuring` runs the finalizer on success, failure, AND interruption. |
| 155 | const body = Core.withProviders(fn(scratch), options, scratch.name).pipe( |
| 156 | Effect.ensuring(scratch.destroy().pipe(Effect.ignore)), |
| 157 | ); |
| 158 | return Core.toEffect(body, { ...options, state: scratch.state }, sharedScope, sidecar); |
| 159 | }; |
| 160 | |
| 161 | const provider = ((name, fn, opts) => { |
| 162 | it.live(name, () => wrapProvider(name, fn), timeoutOf(opts)); |
| 163 | }) as ProviderFn; |
| 164 | provider.skip = (name, _fn, opts) => { |
| 165 | it.skip(name, () => {}, timeoutOf(opts)); |
| 166 | }; |
| 167 | provider.skipIf = (condition) => (name, fn, opts) => { |
| 168 | if (condition) { |
| 169 | it.skip(name, () => {}, timeoutOf(opts)); |
| 170 | } else { |
| 171 | it.live(name, () => wrapProvider(name, fn), timeoutOf(opts)); |
| 172 | } |
| 173 | }; |
| 174 | test.provider = provider; |
| 175 | |
| 176 | const beforeAll: BeforeAllFn = <A>(eff: TestEffect<A>, hookOptions?: TestOptions) => { |
| 177 | let result: A; |
| 178 | vitestBeforeAll( |
| 179 | () => runEff(eff).then((v) => (result = v)), |
| 180 | timeoutOf(hookOptions) ?? DEFAULT_TIMEOUT, |
| 181 | ); |
| 182 | return Effect.sync(() => result); |
| 183 | }; |
| 184 | |
| 185 | const beforeEach: BeforeEachFn = (eff, hookOptions) => { |
| 186 | vitestBeforeEach(() => runEff(eff), timeoutOf(hookOptions)); |
| 187 | }; |
| 188 | |
| 189 | const afterAll = ((eff, hookOptions) => { |
| 190 | vitestAfterAll(() => runEff(eff), timeoutOf(hookOptions) ?? DEFAULT_TIMEOUT); |
| 191 | }) as AfterAllFn; |
| 192 | afterAll.skipIf = (predicate) => (eff, hookOptions) => { |
| 193 | if (predicate) return; |
| 194 | vitestAfterAll(() => runEff(eff), timeoutOf(hookOptions) ?? DEFAULT_TIMEOUT); |
| 195 | }; |
| 196 | |
| 197 | const afterEach: AfterEachFn = (eff, hookOptions) => { |
| 198 | vitestAfterEach(() => runEff(eff), timeoutOf(hookOptions)); |
| 199 | }; |
| 200 | |
| 201 | // `destroy(Stack)` needs the dev sidecar alive so it can call `sidecar.stop`. |
| 202 | // `Scope.close` on an already-closed scope is a no-op, so the destroy |
| 203 | // wrapper AND the fallback cleanup hook below can both call it safely. |
| 204 | const closeScope = Effect.suspend(() => Scope.close(sharedScope, Exit.void)).pipe(Effect.ignore); |
| 205 | |
| 206 | // Fallback cleanup: if the user never calls `destroy(Stack)` (e.g. |
| 207 | // `NO_DESTROY=1`), nothing else closes the shared scope and the sidecar |
| 208 | // child process leaks past the test process. The RPC sidecar lives in its |
| 209 | // own scope so that mid-file `destroy(Stack)` calls can't kill it for later |
| 210 | // tests. |
| 211 | // |
| 212 | // The cleanup must run after every user `afterAll` (including |
| 213 | // `destroy(Stack)`, which needs the sidecar). Vitest's default |
| 214 | // `sequence.hooks: "stack"` runs `afterAll` hooks in reverse registration |
| 215 | // order and `"list"` runs them in registration order, so register the |
| 216 | // cleanup both before (now) and after (next microtask) the file's |
| 217 | // `afterAll` calls, and only close on the second invocation: whichever |
| 218 | // order is configured, that one runs last. |
| 219 | const closeAll = sidecar ? Effect.andThen(closeScope, sidecar.close) : closeScope; |
| 220 | let cleanupCalls = 0; |
| 221 | const cleanup = () => (++cleanupCalls === 2 ? Effect.runPromise(closeAll) : undefined); |
| 222 | vitestAfterAll(cleanup, DEFAULT_TIMEOUT); |
| 223 | queueMicrotask(() => { |
| 224 | vitestAfterAll(cleanup, DEFAULT_TIMEOUT); |
| 225 | }); |
| 226 | |
| 227 | return { |
| 228 | test, |
| 229 | beforeAll, |
| 230 | beforeEach, |
| 231 | afterAll, |
| 232 | afterEach, |
| 233 | deploy: Core.makeDeploy(options, sharedScope), |
| 234 | destroy: (stack, callOpts) => |
| 235 | Core.destroy(options, stack, { ...callOpts, scope: sharedScope }).pipe( |
| 236 | Effect.ensuring(closeScope), |
| 237 | ), |
| 238 | }; |
| 239 | }; |