import { afterEach, expect, test, vi } from "vitest" import { Runtime } from "#Source/environment/index.ts" import { BrowserFetch, generalFetch, NodejsFetch } from "#Source/request/index.ts" afterEach(() => { vi.restoreAllMocks() }) const runNodejsHandler = (handlers: Runtime.UseRuntimesOptions): R => { return handlers.nodejs?.(undefined as never) as R } const runBrowserHandler = (handlers: Runtime.UseRuntimesOptions): R => { return handlers.browser?.(undefined as never) as R } test("generalFetch delegates fetch creation to the runtime-specific handler", () => { const options = { baseUrl: "https://api.example.com", path: "/users", method: "get", } const useRuntimesSpy = vi.spyOn(Runtime, "useRuntimes") useRuntimesSpy.mockImplementationOnce(runNodejsHandler) const nodejsFetcher = generalFetch(options) expect(nodejsFetcher).toBeInstanceOf(NodejsFetch) expect(useRuntimesSpy).toHaveBeenCalledTimes(1) useRuntimesSpy.mockImplementationOnce(runBrowserHandler) const browserFetcher = generalFetch(options) expect(browserFetcher).toBeInstanceOf(BrowserFetch) expect(useRuntimesSpy).toHaveBeenCalledTimes(2) })