import "tsconfig-paths/register" import { vi } from "bun:test" import { JSDOM } from "jsdom" // Provide default environment variables expected across the test suite. process.env.BETTER_AUTH_SECRET ??= "test-secret" process.env.NEXT_PUBLIC_APP_URL ??= "http://localhost:3000" process.env.BETTER_AUTH_URL ??= process.env.NEXT_PUBLIC_APP_URL const OriginalDate = Date let fixedTimestamp: number | null = null type MutableVi = typeof vi & { mocked?: (item: T) => T setSystemTime?: (value: number | Date) => void useFakeTimers?: (...args: Array) => void useRealTimers?: (...args: Array) => void } const viExtended = vi as MutableVi if (typeof window === "undefined" || typeof document === "undefined") { const dom = new JSDOM("", { url: process.env.NEXT_PUBLIC_APP_URL, }) const globals = dom.window as unknown as Record const globalTarget = globalThis as Record for (const key of ["window", "document", "navigator", "HTMLElement", "HTMLAnchorElement", "Node", "Text"]) { if (!(key in globalTarget)) { globalTarget[key] = globals[key] } } } const applyFixedDate = () => { if (fixedTimestamp === null) { globalThis.Date = OriginalDate return } const ts = fixedTimestamp class MockDate extends OriginalDate { constructor(...args: ConstructorParameters) { if (args.length < 1) { super(ts) return } super(...args) } static now(): number { return ts } } globalThis.Date = MockDate as unknown as DateConstructor } if (!viExtended.mocked) { viExtended.mocked = (item: T) => item } if (!viExtended.setSystemTime) { viExtended.setSystemTime = (value: number | Date) => { fixedTimestamp = typeof value === "number" ? value : value.getTime() applyFixedDate() } } if (!viExtended.useFakeTimers) { viExtended.useFakeTimers = () => { // Bun's fake timers are not required for our current tests. This is a noop // placeholder to keep compatibility with the previous Vitest API. } } else { const originalUseFakeTimers = viExtended.useFakeTimers.bind(vi) viExtended.useFakeTimers = (...args: Array) => { originalUseFakeTimers(...args) } } if (!viExtended.useRealTimers) { viExtended.useRealTimers = () => { fixedTimestamp = null applyFixedDate() } } else { const originalUseRealTimers = viExtended.useRealTimers.bind(vi) viExtended.useRealTimers = (...args: Array) => { originalUseRealTimers(...args) fixedTimestamp = null applyFixedDate() } } applyFixedDate()