|
| 1 | +// Unit tests for bind-failure diagnostics. These exist because the two failure |
| 2 | +// modes they cover are invisible at runtime: Effect's `ServeError` has an empty |
| 3 | +// `message`, and Bun's `node:http` reports every listen error as EADDRINUSE. A |
| 4 | +// regression here degrades silently to "Unexpected error / ServeError". |
| 5 | +import { describe, expect, test } from "bun:test" |
| 6 | +import type { networkInterfaces } from "node:os" |
| 7 | +import { assertBindable, bindError, localAddresses } from "../../src/server/bind" |
| 8 | + |
| 9 | +type Interfaces = ReturnType<typeof networkInterfaces> |
| 10 | + |
| 11 | +// Only `address` is read; the rest of NetworkInterfaceInfo is noise here. |
| 12 | +// oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- partial fixture, `address` is the only field under test |
| 13 | +const INTERFACES = { |
| 14 | + lo0: [ |
| 15 | + { address: "127.0.0.1", family: "IPv4", internal: true }, |
| 16 | + { address: "::1", family: "IPv6", internal: true }, |
| 17 | + ], |
| 18 | + en0: [ |
| 19 | + { address: "192.168.0.40", family: "IPv4", internal: false }, |
| 20 | + { address: "fe80::1c52:8bff:fe2e:29fe", family: "IPv6", internal: false }, |
| 21 | + ], |
| 22 | +} as unknown as Interfaces |
| 23 | + |
| 24 | +// Mirrors Effect's `HttpServerError.ServeError`: a tagged error with an empty |
| 25 | +// message that hides the real failure on `.cause`. |
| 26 | +function serveError(cause: unknown) { |
| 27 | + const error = new Error("") |
| 28 | + error.name = "ServeError" |
| 29 | + Object.assign(error, { _tag: "ServeError", cause }) |
| 30 | + return error |
| 31 | +} |
| 32 | + |
| 33 | +function errno(code: string, message: string) { |
| 34 | + return Object.assign(new Error(message), { code }) |
| 35 | +} |
| 36 | + |
| 37 | +describe("server.bind", () => { |
| 38 | + describe("assertBindable", () => { |
| 39 | + test("allows wildcard and loopback hosts", () => { |
| 40 | + for (const hostname of ["0.0.0.0", "::", "localhost", "127.0.0.1", "::1"]) { |
| 41 | + expect(() => assertBindable({ hostname, port: 4096 }, INTERFACES)).not.toThrow() |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + test("allows an address owned by a local interface", () => { |
| 46 | + expect(() => assertBindable({ hostname: "192.168.0.40", port: 4096 }, INTERFACES)).not.toThrow() |
| 47 | + }) |
| 48 | + |
| 49 | + test("rejects an IP no interface owns, and lists what is available", () => { |
| 50 | + // A Tailscale CGNAT address with the interface down — the reported case. |
| 51 | + const bind = () => assertBindable({ hostname: "100.68.120.26", port: 4096 }, INTERFACES) |
| 52 | + expect(bind).toThrow(/not an address on any local network interface/) |
| 53 | + // The message has to be actionable, not just accurate. |
| 54 | + expect(bind).toThrow(/192\.168\.0\.40/) |
| 55 | + expect(bind).toThrow(/0\.0\.0\.0/) |
| 56 | + }) |
| 57 | + |
| 58 | + test("ignores DNS names — listen() resolves those", () => { |
| 59 | + expect(() => assertBindable({ hostname: "example.internal", port: 4096 }, INTERFACES)).not.toThrow() |
| 60 | + }) |
| 61 | + |
| 62 | + test("matches link-local IPv6 written with a zone id", () => { |
| 63 | + expect(() => |
| 64 | + assertBindable({ hostname: "fe80::1c52:8bff:fe2e:29fe%en0", port: 4096 }, INTERFACES), |
| 65 | + ).not.toThrow() |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + test("localAddresses dedupes and flattens", () => { |
| 70 | + expect(localAddresses(INTERFACES)).toEqual(["127.0.0.1", "::1", "192.168.0.40", "fe80::1c52:8bff:fe2e:29fe"]) |
| 71 | + }) |
| 72 | + |
| 73 | + describe("bindError", () => { |
| 74 | + const target = { hostname: "127.0.0.1", port: 4096 } |
| 75 | + |
| 76 | + test("unwraps an errno buried under ServeError", () => { |
| 77 | + const error = bindError(serveError(errno("EADDRINUSE", "address already in use")), target) |
| 78 | + expect(error.message).toContain("127.0.0.1:4096 is already in use") |
| 79 | + expect(error.message).not.toContain("ServeError") |
| 80 | + }) |
| 81 | + |
| 82 | + test("blames privileges, not a conflict, for ports below 1024", () => { |
| 83 | + // Bun folds EACCES into EADDRINUSE, so the errno alone would mislead here. |
| 84 | + const error = bindError(serveError(errno("EADDRINUSE", "in use?")), { hostname: "127.0.0.1", port: 80 }) |
| 85 | + expect(error.message).toContain("privileged") |
| 86 | + }) |
| 87 | + |
| 88 | + test("maps EADDRNOTAVAIL to the hostname, not the port", () => { |
| 89 | + const error = bindError(serveError(errno("EADDRNOTAVAIL", "address not available")), { |
| 90 | + hostname: "100.68.120.26", |
| 91 | + port: 4096, |
| 92 | + }) |
| 93 | + expect(error.message).toContain("not an address on this machine") |
| 94 | + }) |
| 95 | + |
| 96 | + test("maps EACCES", () => { |
| 97 | + const error = bindError(serveError(errno("EACCES", "permission denied")), { hostname: "127.0.0.1", port: 443 }) |
| 98 | + expect(error.message).toContain("Permission denied") |
| 99 | + }) |
| 100 | + |
| 101 | + test("falls back to the deepest cause message when the errno is unknown", () => { |
| 102 | + const error = bindError(serveError(new Error("socket exploded")), target) |
| 103 | + expect(error.message).toBe("Failed to start server on 127.0.0.1:4096: socket exploded") |
| 104 | + }) |
| 105 | + |
| 106 | + test("never loses the original error", () => { |
| 107 | + const original = serveError(errno("EADDRINUSE", "in use")) |
| 108 | + expect(bindError(original, target).cause).toBe(original) |
| 109 | + }) |
| 110 | + |
| 111 | + test("terminates on a self-referential cause chain", () => { |
| 112 | + const looped: { cause?: unknown } = {} |
| 113 | + looped.cause = looped |
| 114 | + // Would hang without the `seen` guard; reaching the assertion is the test. |
| 115 | + expect(bindError(looped, target).message).toBe("Failed to start server on 127.0.0.1:4096") |
| 116 | + }) |
| 117 | + }) |
| 118 | +}) |
0 commit comments