import { test, type TestOptions } from "bun:test" import { Cause, Effect, Exit, Layer, type Scope } from "effect" import { TestClock, TestConsole } from "effect/testing" import { FetchHttpClient } from "effect/http" type Body = Effect.Effect | (() => Effect.Effect) const body = (value: Body) => Effect.suspend(() => (typeof value === "function" ? value() : value)) const loopback = new Set(["127.0.0.1", "localhost", "[::1]"]) // Core is env-free, so nothing tells the default node graph to stay offline; // a test that boots it would phone home through FetchHttpClient. Every // FetchHttpClient reads this reference at request time, so refusing here covers // each node that shares the default client, while an explicit HttpClient // replacement never reaches it. Callers such as ModelsDev swallow request // failures, so the harness also records the attempt and fails the test itself. export const refuseNetwork = (violations: string[]): typeof fetch => Object.assign( (input: string | URL | Request, init?: RequestInit) => { const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url if (loopback.has(new URL(url).hostname)) return fetch(input, init) const method = init?.method ?? (input instanceof Request ? input.method : "GET") const message = `test attempted network request: ${method} ${url} — provide an explicit HttpClient or disable the fetch` violations.push(message) return Promise.reject(new Error(message)) }, { preconnect: fetch.preconnect }, ) const run = (value: Body, layer: Layer.Layer) => Effect.gen(function* () { const violations: string[] = [] const exit = yield* body(value).pipe( Effect.scoped, Effect.provide(layer), Effect.provideService(FetchHttpClient.Fetch, refuseNetwork(violations)), Effect.exit, ) if (Exit.isFailure(exit)) { for (const err of Cause.prettyErrors(exit.cause)) { yield* Effect.logError(err) } } if (violations.length > 0) return yield* Effect.fail(new Error(violations.join("\n"))) return yield* exit }).pipe(Effect.runPromise) const make = (testLayer: Layer.Layer, liveLayer: Layer.Layer) => { const effect = (name: string, value: Body, opts?: number | TestOptions) => test(name, () => run(value, testLayer), opts) effect.only = (name: string, value: Body, opts?: number | TestOptions) => test.only(name, () => run(value, testLayer), opts) effect.skip = (name: string, value: Body, opts?: number | TestOptions) => test.skip(name, () => run(value, testLayer), opts) const live = (name: string, value: Body, opts?: number | TestOptions) => test(name, () => run(value, liveLayer), opts) live.only = (name: string, value: Body, opts?: number | TestOptions) => test.only(name, () => run(value, liveLayer), opts) live.skip = (name: string, value: Body, opts?: number | TestOptions) => test.skip(name, () => run(value, liveLayer), opts) return { effect, live } } // Test environment with TestClock and TestConsole const testEnv = Layer.mergeAll(TestConsole.layer, TestClock.layer()) // Live environment - uses real clock, but keeps TestConsole for output capture const liveEnv = TestConsole.layer export const it = make(testEnv, liveEnv) export const testEffect = (layer: Layer.Layer) => make(Layer.provideMerge(layer, testEnv), Layer.provideMerge(layer, liveEnv))