// What every test starts from, put back by the preload before each one — the per-TEST half of // isolation, beside `file-boundary.ts`'s per-file half: the jobs event bus (it STORES what it is // handed), the installed rate-limit store (a drained bucket was the next test's 429) and the // once-per-operation wire-dates warn (a rerun in one process never saw its warn). import { beforeEach } from 'bun:test'; import type { clearDefaultRateLimitBuckets } from '@ultimat3/http/adapter'; import type { freshDefaultEventBus, resetEventBus } from '@ultimat3/jobs/adapter'; import { onFileBoundary } from './file-boundary'; import { installTestScope } from './test-scope'; let installed = false; /** * `file` of `pkg`, when something in this process has already loaded it — and only then. A test * that never touched the package has nothing to reset, and loading it to reset would put its graph * into every `packages/core` test. Resolved once per process: the path does not move. */ export function loaded(pkg: string, file: string): string | undefined { const key = `${pkg}/${file}`; let path = paths.get(key); if (path === undefined) { path = resolvedFrom(pkg)?.replace(/index\.ts$/, file) ?? null; paths.set(key, path); } return path !== null && path in require.cache ? path : undefined; } /** * Where `pkg` resolves from this package, or `null` when it does not. A package testing declares no * dependency on (`@ultimat3/flags`) is unresolvable from here in an app's isolated install, and one * this process cannot resolve here is one it did not load through here: absent, never a throw. */ function resolvedFrom(pkg: string): string | null { try { return Bun.resolveSync(pkg, import.meta.dir); } catch { return null; } } const paths = new Map(); /** * Before each test: a fresh DEFAULT event bus, the DEFAULT rate-limit buckets emptied and the * wire-dates warn forgotten — each only when its package is loaded. Already evaluated, so each * import answers from the registry, the same instance. Only the defaults: a bus or store installed for the file (`beforeAll`, a booted * app's) stays installed, and carries across that file's tests as it would in production. */ export async function resetPerTestState(): Promise { const events = loaded('@ultimat3/jobs', 'events.ts'); if (events !== undefined) { const jobs = (await import(events)) as { readonly freshDefaultEventBus: typeof freshDefaultEventBus; }; jobs.freshDefaultEventBus(); } const limits = loaded('@ultimat3/http', 'rate-limit-installed.ts'); if (limits !== undefined) { const http = (await import(limits)) as { readonly clearDefaultRateLimitBuckets: typeof clearDefaultRateLimitBuckets; }; http.clearDefaultRateLimitBuckets(); } const wireDates = loaded('@ultimat3/http', 'wire-dates-cap.ts'); if (wireDates !== undefined) { // An internal seam, off the package's API on purpose: an app forgets nothing it warned about. // Its name is held by `per-test-reset.test.ts`, which reds if this stops resetting the set. const http = (await import(wireDates)) as { readonly resetWireDatesCap: () => void }; http.resetWireDatesCap(); } } /** * Called by the app preload (`./preload`), once per process. In a preload, `beforeEach` runs * before every test of every file; idempotent so a second caller cannot register it twice. */ export function installPerTestReset(): void { if (installed) return; installed = true; beforeEach(resetPerTestState); // The test doubles' scope rides the same call, so both preloads — the app's and this repo's own // `scripts/test-setup.ts` — put every stub and swapped adapter back with no second install line. installTestScope(); // AFTER the scope's own file hook, so a file's swaps are released first: then the whole bus slot // goes back to boot state. A frame a file installed and never released — an `afterEach` that // "restored" by installing again — would otherwise outlive the file and hand every later test // its events, which the per-test fresh DEFAULT bus underneath cannot reach. onFileBoundary(resetEventBusAtFileEnd); } /** Synchronous, as a file boundary is: the module is already loaded, so `require` answers it. */ function resetEventBusAtFileEnd(): void { const events = loaded('@ultimat3/jobs', 'events.ts'); if (events === undefined) return; const jobs = require(events) as { readonly resetEventBus: typeof resetEventBus }; jobs.resetEventBus(); }