/** * UF-044 — requests the sidecar sends to its own routes must carry the session * token (the server does not trust loopback callers) and target the port the * server is actually bound to. */ import { describe, it, expect, afterEach } from 'vitest'; import type { FastifyInstance } from 'fastify'; import { selfBaseUrl, selfRequestHeaders } from '../../src/local/self-request.js'; function fakeServer(address: unknown, token: string | undefined): FastifyInstance { return { server: { address: () => address }, agentState: { wsSessionToken: token }, } as unknown as FastifyInstance; } describe('self requests (UF-044)', () => { const priorPort = process.env.WAGGLE_PORT; afterEach(() => { if (priorPort === undefined) delete process.env.WAGGLE_PORT; else process.env.WAGGLE_PORT = priorPort; }); it('targets the port the server is bound to', () => { process.env.WAGGLE_PORT = '3333'; const server = fakeServer({ address: '127.0.0.1', family: 'IPv4', port: 53204 }, 't'); expect(selfBaseUrl(server)).toBe('http://127.0.0.1:53204'); }); it('falls back to WAGGLE_PORT, then 3333, before the server listens', () => { process.env.WAGGLE_PORT = '4100'; expect(selfBaseUrl(fakeServer(null, 't'))).toBe('http://127.0.0.1:4100'); delete process.env.WAGGLE_PORT; expect(selfBaseUrl(fakeServer(null, 't'))).toBe('http://127.0.0.1:3333'); }); it('sends the session token as a bearer credential', () => { expect(selfRequestHeaders(fakeServer(null, 'session-123'))).toEqual({ 'Content-Type': 'application/json', Authorization: 'Bearer session-123', }); }); });