import { afterEach, describe, expect, it, vi } from 'vitest' import { appPortProblem, checkGatewayIdentity, jwtClaims, targetPortGuard, TRIAL_TARGETS, trialTarget, } from '../../vite/trial-targets' const b64 = (value: unknown) => btoa(JSON.stringify(value)) .replace(/\+/g, '-') .replace(/\//g, '_') .replace(/=+$/, '') const key = (claims: Record) => `${b64({ alg: 'HS256', typ: 'JWT' })}.${b64(claims)}.signature` const HOST = '192.168.1.100' /** Exactly what scripts/db-sandbox.sh cmd_trial passes (non-regression, A12). */ const SANDBOX = { FAUNOSTEO_TRIAL_GATEWAY: 'http://127.0.0.1:57801', FAUNOSTEO_TRIAL_HOST: HOST, VITE_SUPABASE_URL: `http://${HOST}:5193`, VITE_SUPABASE_ANON_KEY: key({ role: 'anon', iss: 'db-sandbox' }), } /** What scripts/db-trial.sh cmd_app passes. */ const TRIAL = { FAUNOSTEO_TRIAL_GATEWAY: 'http://127.0.0.1:58801', FAUNOSTEO_TRIAL_HOST: HOST, VITE_SUPABASE_URL: `http://${HOST}:5194`, VITE_SUPABASE_ANON_KEY: key({ role: 'anon', iss: 'faunosteo-trial' }), } const [sandbox, trial] = TRIAL_TARGETS describe('trial dev server target', () => { it('holds exactly the two stacks, sandbox first', () => { expect(TRIAL_TARGETS.map((t) => t.name)).toEqual(['sandbox', 'trial']) }) it('accepts the values db:sandbox trial passes, unchanged, without a probe', () => { expect(trialTarget(SANDBOX)).toBe(sandbox) expect(sandbox.identity).toBeUndefined() expect(appPortProblem(sandbox, 5193, true)).toBeNull() }) it('accepts the values db:trial app passes, with the gateway identity', () => { expect(trialTarget(TRIAL)).toBe(trial) expect(trial.identity).toBe('faunosteo_trial\n') expect(appPortProblem(trial, 5194, true)).toBeNull() }) it.each([ [ 'the DEV gateway', { ...TRIAL, FAUNOSTEO_TRIAL_GATEWAY: 'http://127.0.0.1:54321' }, ], [ "the sandbox's other gateway (avatar and sheet-assets e2e)", { ...SANDBOX, FAUNOSTEO_TRIAL_GATEWAY: 'http://127.0.0.1:57800' }, ], [ 'another loopback port', { ...TRIAL, FAUNOSTEO_TRIAL_GATEWAY: 'http://127.0.0.1:58802' }, ], [ 'a trailing slash', { ...TRIAL, FAUNOSTEO_TRIAL_GATEWAY: 'http://127.0.0.1:58801/' }, ], [ 'localhost', { ...TRIAL, FAUNOSTEO_TRIAL_GATEWAY: 'http://localhost:58801' }, ], ['no gateway', { ...TRIAL, FAUNOSTEO_TRIAL_GATEWAY: undefined }], [ 'the trial gateway with the sandbox app URL', { ...TRIAL, VITE_SUPABASE_URL: `http://${HOST}:5193` }, ], [ 'the sandbox gateway with a trial key', { ...SANDBOX, VITE_SUPABASE_ANON_KEY: TRIAL.VITE_SUPABASE_ANON_KEY }, ], [ 'the trial gateway with a sandbox key', { ...TRIAL, VITE_SUPABASE_ANON_KEY: SANDBOX.VITE_SUPABASE_ANON_KEY }, ], ['a missing key', { ...TRIAL, VITE_SUPABASE_ANON_KEY: undefined }], ['no host', { ...TRIAL, FAUNOSTEO_TRIAL_HOST: undefined }], ['an empty host', { ...TRIAL, FAUNOSTEO_TRIAL_HOST: '' }], [ 'a service key', { ...TRIAL, VITE_SUPABASE_ANON_KEY: key({ role: 'service_role', iss: 'faunosteo-trial', }), }, ], [ 'an app URL on another host', { ...TRIAL, VITE_SUPABASE_URL: 'http://10.0.0.9:5194' }, ], ])('refuses %s', (_label, env) => { expect(() => trialTarget(env)).toThrow() }) it.each([ ['the trial gateway behind the sandbox app port', trial, 5193, true], ['the sandbox gateway behind the trial app port', sandbox, 5194, true], ['a port Vite may change', trial, 5194, false], ] as const)('refuses %s', (_label, target, port, strict) => { expect(appPortProblem(target, port, strict)).not.toBeNull() }) it('reads a JWT payload only when it is one', () => { expect(jwtClaims(TRIAL.VITE_SUPABASE_ANON_KEY)?.iss).toBe('faunosteo-trial') expect(jwtClaims('not-a-jwt')).toBeNull() expect(jwtClaims(`x.${b64(['iss'])}.y`)).toBeNull() }) }) /** A response of any 2xx status, even those a Response cannot carry a body with. */ const answerWith = (status: number, body: string) => ({ ok: true, status, text: async () => body }) as unknown as Response describe('trial gateway identity probe', () => { afterEach(() => { vi.useRealTimers() vi.unstubAllGlobals() }) it('accepts exactly faunosteo_trial and a newline, asked without redirects', async () => { const fetch = vi.fn(async () => new Response('faunosteo_trial\n')) vi.stubGlobal('fetch', fetch) await expect(checkGatewayIdentity(trial)).resolves.toBeUndefined() expect(fetch).toHaveBeenCalledWith( 'http://127.0.0.1:58801/__faunosteo_stack', expect.objectContaining({ redirect: 'error' }), ) }) it.each([ ['a body without its newline', async () => new Response('faunosteo_trial')], ['another service', async () => new Response('other\n')], [ 'a body with a second newline', async () => new Response('faunosteo_trial\n\n'), ], [ 'a body with a leading space', async () => new Response(' faunosteo_trial\n'), ], ['a body with CRLF', async () => new Response('faunosteo_trial\r\n')], [ 'a 204 carrying the exact body', async () => answerWith(204, 'faunosteo_trial\n'), ], [ 'a 206 carrying the exact body', async () => answerWith(206, 'faunosteo_trial\n'), ], ['a 404', async () => new Response('faunosteo_trial\n', { status: 404 })], [ 'a redirect', async () => new Response(null, { status: 302, headers: { location: 'http://127.0.0.1:57801/' }, }), ], [ 'a refused redirect', async () => Promise.reject(new TypeError('unexpected redirect')), ], ['no answer', async () => Promise.reject(new TypeError('fetch failed'))], ])('refuses %s', async (_label, answer) => { vi.stubGlobal('fetch', vi.fn(answer)) await expect(checkGatewayIdentity(trial)).rejects.toThrow(/does not answer/) }) it('gives up after 3 s on a gateway that never answers', async () => { vi.useFakeTimers() let init: RequestInit | undefined vi.stubGlobal( 'fetch', vi.fn( (_url: string, options: RequestInit) => new Promise((_resolve, reject) => { init = options options.signal?.addEventListener('abort', () => reject(new TypeError('aborted')), ) }), ), ) const outcome = expect(checkGatewayIdentity(trial)).rejects.toThrow( /does not answer/, ) await vi.advanceTimersByTimeAsync(3001) await outcome expect(init?.signal).toBeDefined() expect(init?.redirect).toBe('error') vi.useRealTimers() }) it('never probes the sandbox gateway (db-sandbox.sh unchanged, A12)', async () => { const fetch = vi.fn() vi.stubGlobal('fetch', fetch) await expect(checkGatewayIdentity(sandbox)).resolves.toBeUndefined() expect(fetch).not.toHaveBeenCalled() }) }) describe('trial dev server port guard (configResolved)', () => { const resolved = ( port: number | undefined, strictPort: boolean | undefined, ) => ({ server: { port, strictPort }, }) it('accepts each stack on its own port with strictPort', () => { expect(() => targetPortGuard(trial).configResolved(resolved(5194, true)), ).not.toThrow() expect(() => targetPortGuard(sandbox).configResolved(resolved(5193, true)), ).not.toThrow() }) it.each([ ['the trial stack on the sandbox port', trial, 5193, true], ['the sandbox on the trial port', sandbox, 5194, true], ['no strictPort', trial, 5194, false], ['no port', trial, undefined, true], ] as const)('refuses %s', (_label, target, port, strict) => { expect(() => targetPortGuard(target).configResolved(resolved(port, strict)), ).toThrow(/Refused/) }) })