{"version":3,"file":"vitest.cjs","names":["setTestLifecycleHooks","createInertiaReactMock"],"sources":["../src/vitest.ts"],"sourcesContent":["import { afterEach, beforeEach, vi } from 'vitest'\nimport type { PropsWithChildren, ReactElement } from 'react'\nimport { createInertiaReactMock, resetInertiaPage } from './inertia'\nimport { setTestLifecycleHooks } from './lifecycle'\n\n// Importing this module wires vitest's lifecycle hooks into the runner-agnostic\n// helpers on the main entry (useDatabaseTransactions, useTruncateTables).\nsetTestLifecycleHooks({ beforeEach, afterEach })\n\n/**\n * scrypt cost for the `Bun.password` stand-in. Two orders of magnitude below\n * the framework default, which is the point: a test suite pays this per login.\n */\nconst TEST_SCRYPT_COST = 1024\n\n/**\n * A working stand-in for `Bun.password`, which does not exist off Bun. A real hasher, not\n * a throwing stub that would force every password test to hand-write a double (how a\n * `verify(plain, hashed)` inversion shipped with a green suite). Delegates to the\n * framework's own scrypt so format, parameter parsing and malformed-hash rejection cannot\n * drift; lazy import because this module is also emitted as CJS and `@guren/server` is ESM-only.\n */\nconst testPasswordApi = {\n  async hash(password: string): Promise<string> {\n    const { hashPassword } = await import('@guren/server/encryption')\n    return hashPassword(password, { cost: TEST_SCRYPT_COST })\n  },\n\n  /**\n   * Throws on a hash it cannot parse, as `Bun.password.verify` does. Returning\n   * `false` would make a swapped `verify(plain, hashed)` call look like a wrong\n   * password in tests while it is a 500 in production.\n   */\n  async verify(password: string, hashed: string): Promise<boolean> {\n    const { verifyPassword } = await import('@guren/server/encryption')\n    return verifyPassword(password, hashed)\n  },\n}\n\nexport interface ConfigureInertiaVitestOptions {\n  Head?: (props: PropsWithChildren) => ReactElement | null\n  stubBun?: boolean\n}\n\nlet configured = false\n\nexport function configureInertiaVitest(\n  options: ConfigureInertiaVitestOptions = {},\n): void {\n  if (configured) {\n    return\n  }\n\n  configured = true\n\n  const {\n    Head = () => null,\n    stubBun = true,\n  } = options\n\n  if (stubBun && typeof globalThis.Bun === 'undefined') {\n    ;(globalThis as Record<string, unknown>).Bun = {\n      env: {},\n      password: testPasswordApi,\n      file(path: string | URL) {\n        if (typeof path === 'string' || path instanceof URL) {\n          return {\n            exists: false,\n            type: 'file',\n            size: 0,\n            text: async () => '',\n            arrayBuffer: async () => new ArrayBuffer(0),\n          }\n        }\n\n        throw new TypeError(\n          'Unsupported Bun.file argument in test environment.',\n        )\n      },\n    }\n  }\n\n  vi.doMock('@inertiajs/react', async () => {\n    const actual = await import('@inertiajs/react')\n    return createInertiaReactMock(actual, { Head })\n  })\n\n  beforeEach(() => {\n    resetInertiaPage()\n  })\n\n  afterEach(() => {\n    vi.clearAllMocks()\n    resetInertiaPage()\n  })\n}\n"],"mappings":";;;;AAOAA,kBAAAA,sBAAsB;CAAE,YAAA,OAAA;CAAY,WAAA,OAAA;AAAU,CAAC;;;;;AAM/C,MAAM,mBAAmB;;;;;;;;AASzB,MAAM,kBAAkB;CACtB,MAAM,KAAK,UAAmC;EAC5C,MAAM,EAAE,iBAAiB,MAAM,OAAO;EACtC,OAAO,aAAa,UAAU,EAAE,MAAM,iBAAiB,CAAC;CAC1D;;;;;;CAOA,MAAM,OAAO,UAAkB,QAAkC;EAC/D,MAAM,EAAE,mBAAmB,MAAM,OAAO;EACxC,OAAO,eAAe,UAAU,MAAM;CACxC;AACF;AAOA,IAAI,aAAa;AAEjB,SAAgB,uBACd,UAAyC,CAAC,GACpC;CACN,IAAI,YACF;CAGF,aAAa;CAEb,MAAM,EACJ,aAAa,MACb,UAAU,SACR;CAEJ,IAAI,WAAW,OAAO,WAAW,QAAQ,aACtC,WAAwC,MAAM;EAC7C,KAAK,CAAC;EACN,UAAU;EACV,KAAK,MAAoB;GACvB,IAAI,OAAO,SAAS,YAAY,gBAAgB,KAC9C,OAAO;IACL,QAAQ;IACR,MAAM;IACN,MAAM;IACN,MAAM,YAAY;IAClB,aAAa,4BAAY,IAAI,YAAY,CAAC;GAC5C;GAGF,MAAM,IAAI,UACR,oDACF;EACF;CACF;CAGF,OAAA,GAAG,OAAO,oBAAoB,YAAY;EACxC,MAAM,SAAS,MAAM,OAAO;EAC5B,OAAOC,kBAAAA,uBAAuB,QAAQ,EAAE,KAAK,CAAC;CAChD,CAAC;CAED,CAAA,GAAA,OAAA,WAAA,OAAiB;EACf,kBAAA,iBAAiB;CACnB,CAAC;CAED,CAAA,GAAA,OAAA,UAAA,OAAgB;EACd,OAAA,GAAG,cAAc;EACjB,kBAAA,iBAAiB;CACnB,CAAC;AACH"}