import { describe, expect, it } from 'vitest'; import { candidatePorts, WS_PORT_RANGE } from '../../src/background/transport/discovery.js'; describe('candidatePorts', () => { it('covers 52000-52016 inclusive by default', () => { const ports = candidatePorts(); expect(WS_PORT_RANGE).toEqual({ start: 52000, end: 52016 }); expect(ports).toHaveLength(17); expect(ports[0]).toBe(52000); expect(ports.at(-1)).toBe(52016); }); it('tries the last-known-good port first, then the rest ascending, with no duplicate', () => { const ports = candidatePorts(WS_PORT_RANGE, 52009); expect(ports[0]).toBe(52009); expect(ports).toHaveLength(17); expect(ports.filter((p) => p === 52009)).toHaveLength(1); expect(ports.slice(1)).toEqual([...ports.slice(1)].sort((a, b) => a - b)); }); it('ignores a preferred port outside the range', () => { expect(candidatePorts(WS_PORT_RANGE, 40000)).toEqual(candidatePorts()); }); });