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 | /** |
| 2 | * Per-file registration state. |
| 3 | * |
| 4 | * While a test file's module body is evaluating, `describe`/`test`/hook |
| 5 | * calls register nodes against that file's collector. The runner may call |
| 6 | * `collect` for many files at once, but collections run one at a time: each |
| 7 | * waits its turn on a process-wide queue, sets the collector in a plain slot, |
| 8 | * imports the file and flushes the microtasks it queued, then clears the slot. |
| 9 | * Attribution therefore never depends on the runtime carrying async context |
| 10 | * through `import()` (Bun 1.4 does not), including registrations deferred with |
| 11 | * `queueMicrotask` and module bodies that use top-level `await`. |
| 12 | * |
| 13 | * The state lives on `globalThis` so that a duplicated module instance |
| 14 | * (e.g. two resolutions of the package) still shares one registry. |
| 15 | */ |
| 16 | import { makeFileSuite, type FileSuite, type Suite } from "./Model.ts"; |
| 17 | |
| 18 | interface FileContext { |
| 19 | /** Suite that `describe`/`test` calls currently attach to. */ |
| 20 | current: Suite; |
| 21 | } |
| 22 | |
| 23 | interface RegistryState { |
| 24 | /** The collector of the file being collected, if any. */ |
| 25 | active: FileContext | undefined; |
| 26 | /** The tail of the collection queue. */ |
| 27 | tail: Promise<unknown>; |
| 28 | } |
| 29 | |
| 30 | const key = Symbol.for("alchemy-test/registry@2"); |
| 31 | |
| 32 | const state: RegistryState = ((globalThis as any)[key] ??= { |
| 33 | active: undefined, |
| 34 | tail: Promise.resolve(), |
| 35 | } satisfies RegistryState); |
| 36 | |
| 37 | /** |
| 38 | * Collect one file: once every earlier collection has finished, run `f` (the |
| 39 | * file's dynamic import + microtask flush) with `root` (a fresh one by |
| 40 | * default) as the active collector, and return it. |
| 41 | */ |
| 42 | export const collect = ( |
| 43 | file: string, |
| 44 | f: () => Promise<void>, |
| 45 | root: FileSuite = makeFileSuite(file), |
| 46 | ): Promise<FileSuite> => { |
| 47 | const run = state.tail.then(async () => { |
| 48 | state.active = { current: root }; |
| 49 | try { |
| 50 | await f(); |
| 51 | } finally { |
| 52 | state.active = undefined; |
| 53 | } |
| 54 | return root; |
| 55 | }); |
| 56 | state.tail = run.catch(() => undefined); |
| 57 | return run; |
| 58 | }; |
| 59 | |
| 60 | const currentContext = (): FileContext => { |
| 61 | const context = state.active; |
| 62 | if (context === undefined) { |
| 63 | throw new Error( |
| 64 | "alchemy-test: describe/test/hook called outside of a test file collection. " + |
| 65 | "Run tests with the `alchemy-test` CLI.", |
| 66 | ); |
| 67 | } |
| 68 | return context; |
| 69 | }; |
| 70 | |
| 71 | export const currentSuite = (): Suite => currentContext().current; |
| 72 | |
| 73 | export const currentFileSuite = (): FileSuite => { |
| 74 | let suite = currentSuite(); |
| 75 | while (suite.parent !== undefined) suite = suite.parent; |
| 76 | return suite as FileSuite; |
| 77 | }; |
| 78 | |
| 79 | /** |
| 80 | * The file currently being collected (path relative to the run root, e.g. |
| 81 | * `test/Cloudflare/R2/Bucket.test.ts`), or `undefined` when called outside |
| 82 | * of a collection (e.g. from a non-alchemy-test runner). Adapters use this |
| 83 | * at registration time to namespace per-test durable state by file. |
| 84 | */ |
| 85 | export const currentFile = (): string | undefined => { |
| 86 | let suite: Suite | undefined = state.active?.current; |
| 87 | while (suite?.parent !== undefined) suite = suite.parent; |
| 88 | return suite !== undefined && "file" in suite ? (suite as FileSuite).file : undefined; |
| 89 | }; |
| 90 | |
| 91 | /** Run `f` with `suite` as the current registration target. */ |
| 92 | export const withSuite = (suite: Suite, f: () => void): void => { |
| 93 | const context = currentContext(); |
| 94 | const previous = context.current; |
| 95 | context.current = suite; |
| 96 | try { |
| 97 | f(); |
| 98 | } finally { |
| 99 | context.current = previous; |
| 100 | } |
| 101 | }; |