// @vitest-environment node import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' import { resolveConfig } from 'vite' import { afterEach, describe, expect, it, vi } from 'vitest' const CONFIG = fileURLToPath( new URL('../../vite/trial.config.ts', import.meta.url), ) const SANDBOX_SCRIPT = fileURLToPath( new URL('../../../../scripts/db-sandbox.sh', import.meta.url), ) const ROUTES = ['/auth/v1', '/rest/v1', '/storage/v1'] const HOST = '127.0.0.1' const b64 = (value: unknown) => Buffer.from(JSON.stringify(value)).toString('base64url') const key = (iss: string) => `${b64({ alg: 'HS256', typ: 'JWT' })}.${b64({ role: 'anon', iss })}.signature` function stubStack(gateway: string, appPort: number, iss: string) { vi.stubEnv('FAUNOSTEO_TRIAL_GATEWAY', gateway) vi.stubEnv('FAUNOSTEO_TRIAL_HOST', HOST) vi.stubEnv('VITE_SUPABASE_URL', `http://${HOST}:${appPort}`) vi.stubEnv('VITE_SUPABASE_ANON_KEY', key(iss)) } const resolveOn = (port: number) => resolveConfig( { configFile: CONFIG, server: { host: HOST, port, strictPort: true } }, 'serve', ) const proxyTargets = (config: Awaited>) => ROUTES.map((route) => { const entry = config.server.proxy?.[route] return typeof entry === 'string' ? entry : entry?.target }) afterEach(() => { vi.unstubAllEnvs() vi.unstubAllGlobals() }) describe('shared trial dev server config, as Vite resolves it', () => { it('db:sandbox trial: proxies to 57801 on 5193, never probes', async () => { const fetch = vi.fn() vi.stubGlobal('fetch', fetch) stubStack('http://127.0.0.1:57801', 5193, 'db-sandbox') const config = await resolveOn(5193) expect(proxyTargets(config)).toEqual( Array(3).fill('http://127.0.0.1:57801'), ) expect(config.server.port).toBe(5193) expect(fetch).not.toHaveBeenCalled() }) it('db:trial app: proxies to 58801 on 5194 once the gateway names itself', async () => { vi.stubGlobal( 'fetch', vi.fn(async () => new Response('faunosteo_trial\n')), ) stubStack('http://127.0.0.1:58801', 5194, 'faunosteo-trial') const config = await resolveOn(5194) expect(proxyTargets(config)).toEqual( Array(3).fill('http://127.0.0.1:58801'), ) }) it('refuses the trial stack on the sandbox port', async () => { vi.stubGlobal( 'fetch', vi.fn(async () => new Response('faunosteo_trial\n')), ) stubStack('http://127.0.0.1:58801', 5194, 'faunosteo-trial') await expect(resolveOn(5193)).rejects.toThrow(/Refused/) }) it('refuses a trial gateway that does not name itself', async () => { vi.stubGlobal( 'fetch', vi.fn(async () => new Response('other\n')), ) stubStack('http://127.0.0.1:58801', 5194, 'faunosteo-trial') await expect(resolveOn(5194)).rejects.toThrow(/does not answer/) }) }) describe('the sandbox values above are the ones scripts/db-sandbox.sh passes', () => { it('keeps its trial gateway, app port, key issuer and the way it passes them to Vite', () => { // Pins what `pnpm db:sandbox trial` passes (non-regression of the shared config): a failure // means the shared config and the script must be re-checked together. const script = readFileSync(SANDBOX_SCRIPT, 'utf8') expect(script).toContain('PORT_TRIAL_GATEWAY=57801') expect(script).toContain('PORT_TRIAL_APP=5193') expect(script).toContain('iss: "db-sandbox"') expect(script).toContain( 'FAUNOSTEO_TRIAL_GATEWAY="http://127.0.0.1:$PORT_TRIAL_GATEWAY"', ) expect(script).toContain( 'VITE_SUPABASE_URL="http://$TRIAL_HOST:$PORT_TRIAL_APP"', ) expect(script).toContain('--port $PORT_TRIAL_APP --strictPort') }) })