|
| 1 | +import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { dirname, join } from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import { createServer } from 'vite'; |
| 6 | +import { afterEach, describe, expect, it, vi } from 'vite-plus/test'; |
| 7 | +import { voidPlugin } from '../../src/index.ts'; |
| 8 | + |
| 9 | +const repoRoot = fileURLToPath(new URL('../../../..', import.meta.url)); |
| 10 | +const roots: Array<string> = []; |
| 11 | + |
| 12 | +afterEach(() => { |
| 13 | + vi.restoreAllMocks(); |
| 14 | + for (const root of roots.splice(0, roots.length)) { |
| 15 | + rmSync(root, { recursive: true, force: true }); |
| 16 | + } |
| 17 | +}); |
| 18 | + |
| 19 | +describe('void dev better-auth module', () => { |
| 20 | + it('loads one OAuth state module for the worker runtime and app code', async () => { |
| 21 | + const root = realpathSync.native(mkdtempSync(join(tmpdir(), 'void-better-auth-'))); |
| 22 | + roots.push(root); |
| 23 | + // The package export is `dist/index.mjs`. |
| 24 | + const betterAuthEntry = fileURLToPath(import.meta.resolve('better-auth')); |
| 25 | + const betterAuthRoot = realpathSync(dirname(dirname(betterAuthEntry))); |
| 26 | + mkdirSync(join(root, 'node_modules', 'void'), { recursive: true }); |
| 27 | + symlinkSync(betterAuthRoot, join(root, 'node_modules', 'better-auth'), 'dir'); |
| 28 | + writeFileSync( |
| 29 | + join(root, 'package.json'), |
| 30 | + JSON.stringify({ type: 'module', dependencies: { 'better-auth': '*', void: '*' } }), |
| 31 | + ); |
| 32 | + // Auth apps enable nodejs_compat because Better Auth hashes passwords with node:crypto. |
| 33 | + writeFileSync( |
| 34 | + join(root, 'void.json'), |
| 35 | + JSON.stringify({ |
| 36 | + worker: { compatibility_date: '2026-02-24', compatibility_flags: ['nodejs_compat'] }, |
| 37 | + }), |
| 38 | + ); |
| 39 | + // An installed `void` is excluded from dependency optimization, so its import of |
| 40 | + // better-auth stays on the package source. This probe is that file: the relative path |
| 41 | + // is the module the runtime reaches, and the route imports the public subpath a plugin uses. |
| 42 | + writeFileSync( |
| 43 | + join(root, 'node_modules', 'void', 'package.json'), |
| 44 | + JSON.stringify({ |
| 45 | + name: 'void', |
| 46 | + type: 'module', |
| 47 | + exports: { './oauth-probe': './oauth-probe.mjs' }, |
| 48 | + }), |
| 49 | + ); |
| 50 | + writeFileSync( |
| 51 | + join(root, 'node_modules', 'void', 'oauth-probe.mjs'), |
| 52 | + "export { getOAuthState } from '../better-auth/dist/api/state/oauth.mjs';\n", |
| 53 | + ); |
| 54 | + mkdirSync(join(root, 'routes', 'api'), { recursive: true }); |
| 55 | + writeFileSync( |
| 56 | + join(root, 'routes', 'api', 'oauth-state.ts'), |
| 57 | + [ |
| 58 | + "import { getOAuthState as fromApp } from 'better-auth/api';", |
| 59 | + "import { getOAuthState as fromRuntime } from 'void/oauth-probe';", |
| 60 | + 'export const GET = () => Response.json({ same: fromApp === fromRuntime });', |
| 61 | + '', |
| 62 | + ].join('\n'), |
| 63 | + ); |
| 64 | + vi.spyOn(process, 'cwd').mockReturnValue(root); |
| 65 | + |
| 66 | + const server = await createServer({ |
| 67 | + root, |
| 68 | + configFile: false, |
| 69 | + logLevel: 'silent', |
| 70 | + server: { host: '127.0.0.1', port: 0, fs: { allow: [root, repoRoot] } }, |
| 71 | + plugins: [voidPlugin()], |
| 72 | + }); |
| 73 | + try { |
| 74 | + await server.listen(); |
| 75 | + const response = await fetch(new URL('/api/oauth-state', server.resolvedUrls!.local[0]!)); |
| 76 | + const body = await response.text(); |
| 77 | + expect(response.status, body).toBe(200); |
| 78 | + expect(JSON.parse(body)).toEqual({ same: true }); |
| 79 | + } finally { |
| 80 | + await server.close(); |
| 81 | + } |
| 82 | + }, 60_000); |
| 83 | +}); |
0 commit comments