|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { InputValidationError } from "../../errors"; |
| 3 | +import type { PortChecker } from "../../io"; |
| 4 | +import { resolveDevPort } from "./port"; |
| 5 | + |
| 6 | +const signal = new AbortController().signal; |
| 7 | + |
| 8 | +function checker(occupied: number[] = []): { check: PortChecker; checked: number[] } { |
| 9 | + const checked: number[] = []; |
| 10 | + return { |
| 11 | + checked, |
| 12 | + check: async (port) => { |
| 13 | + checked.push(port); |
| 14 | + return !occupied.includes(port); |
| 15 | + }, |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +describe("resolveDevPort", () => { |
| 20 | + test("uses the HTTP default when it is free", async () => { |
| 21 | + const { check, checked } = checker(); |
| 22 | + |
| 23 | + expect(await resolveDevPort("HTTP", undefined, check, signal)).toEqual({ |
| 24 | + port: 8080, |
| 25 | + requestedPort: 8080, |
| 26 | + }); |
| 27 | + expect(checked).toEqual([8080]); |
| 28 | + }); |
| 29 | + |
| 30 | + test("walks up from an occupied default HTTP port", async () => { |
| 31 | + const { check, checked } = checker([8080, 8081]); |
| 32 | + |
| 33 | + expect(await resolveDevPort("AGUI", undefined, check, signal)).toEqual({ |
| 34 | + port: 8082, |
| 35 | + requestedPort: 8080, |
| 36 | + }); |
| 37 | + expect(checked).toEqual([8080, 8081, 8082]); |
| 38 | + }); |
| 39 | + |
| 40 | + test("honors an explicit free HTTP port", async () => { |
| 41 | + const { check } = checker(); |
| 42 | + expect(await resolveDevPort("HTTP", 4567, check, signal)).toEqual({ |
| 43 | + port: 4567, |
| 44 | + requestedPort: 4567, |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + test("rejects an occupied explicit port with inspection commands", async () => { |
| 49 | + const { check } = checker([4567]); |
| 50 | + |
| 51 | + await expect(resolveDevPort("HTTP", 4567, check, signal)).rejects.toMatchObject({ |
| 52 | + name: InputValidationError.name, |
| 53 | + message: expect.stringContaining("lsof -i :4567"), |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + test.each([ |
| 58 | + ["MCP", 8000, 8001], |
| 59 | + ["A2A", 9000, 9001], |
| 60 | + ] as const)("requires the fixed %s port", async (protocol, required, invalid) => { |
| 61 | + const { check, checked } = checker(); |
| 62 | + |
| 63 | + await expect(resolveDevPort(protocol, invalid, check, signal)).rejects.toThrow( |
| 64 | + `${protocol} development servers require port ${required}`, |
| 65 | + ); |
| 66 | + expect(checked).toEqual([]); |
| 67 | + }); |
| 68 | + |
| 69 | + test.each([ |
| 70 | + ["MCP", 8000], |
| 71 | + ["A2A", 9000], |
| 72 | + ] as const)("rejects an occupied default %s port", async (protocol, port) => { |
| 73 | + const { check } = checker([port]); |
| 74 | + await expect(resolveDevPort(protocol, undefined, check, signal)).rejects.toThrow( |
| 75 | + `${protocol} development servers require this port`, |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + test("bounds the default HTTP search to 100 ports", async () => { |
| 80 | + const check: PortChecker = async () => false; |
| 81 | + await expect(resolveDevPort("HTTP", undefined, check, signal)).rejects.toThrow( |
| 82 | + "No free port found in range 8080-8179", |
| 83 | + ); |
| 84 | + }); |
| 85 | +}); |
0 commit comments