Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

serverExposure.test.ts

139 lines3.5 KB

1import { describe, expect, it } from "vitest";
2​
3import { resolveDesktopServerExposure, resolveLanAdvertisedHost } from "./serverExposure.ts";
4​
5describe("resolveLanAdvertisedHost", () => {
6 it("prefers an explicit host override", () => {
7 expect(
8 resolveLanAdvertisedHost(
9 {
10 en0: [
11 {
12 address: "192.168.1.44",
13 family: "IPv4",
14 internal: false,
15 netmask: "255.255.255.0",
16 cidr: "192.168.1.44/24",
17 mac: "00:00:00:00:00:00",
18 },
19 ],
20 },
21 "10.0.0.9",
22 ),
23 ).toBe("10.0.0.9");
24 });
25​
26 it("returns the first usable non-internal IPv4 address", () => {
27 expect(
28 resolveLanAdvertisedHost(
29 {
30 lo0: [
31 {
32 address: "127.0.0.1",
33 family: "IPv4",
34 internal: true,
35 netmask: "255.0.0.0",
36 cidr: "127.0.0.1/8",
37 mac: "00:00:00:00:00:00",
38 },
39 ],
40 en0: [
41 {
42 address: "192.168.1.44",
43 family: "IPv4",
44 internal: false,
45 netmask: "255.255.255.0",
46 cidr: "192.168.1.44/24",
47 mac: "00:00:00:00:00:00",
48 },
49 ],
50 },
51 undefined,
52 ),
53 ).toBe("192.168.1.44");
54 });
55​
56 it("returns null when no usable network address is available", () => {
57 expect(
58 resolveLanAdvertisedHost(
59 {
60 lo0: [
61 {
62 address: "127.0.0.1",
63 family: "IPv4",
64 internal: true,
65 netmask: "255.0.0.0",
66 cidr: "127.0.0.1/8",
67 mac: "00:00:00:00:00:00",
68 },
69 ],
70 },
71 undefined,
72 ),
73 ).toBeNull();
74 });
75});
76​
77describe("resolveDesktopServerExposure", () => {
78 it("keeps the desktop server loopback-only when local-only mode is selected", () => {
79 expect(
80 resolveDesktopServerExposure({
81 mode: "local-only",
82 port: 3773,
83 networkInterfaces: {},
84 }),
85 ).toEqual({
86 mode: "local-only",
87 bindHost: "127.0.0.1",
88 localHttpUrl: "http://127.0.0.1:3773",
89 localWsUrl: "ws://127.0.0.1:3773",
90 endpointUrl: null,
91 advertisedHost: null,
92 });
93 });
94​
95 it("binds to all interfaces in network-accessible mode", () => {
96 expect(
97 resolveDesktopServerExposure({
98 mode: "network-accessible",
99 port: 3773,
100 networkInterfaces: {
101 en0: [
102 {
103 address: "192.168.1.44",
104 family: "IPv4",
105 internal: false,
106 netmask: "255.255.255.0",
107 cidr: "192.168.1.44/24",
108 mac: "00:00:00:00:00:00",
109 },
110 ],
111 },
112 }),
113 ).toEqual({
114 mode: "network-accessible",
115 bindHost: "0.0.0.0",
116 localHttpUrl: "http://127.0.0.1:3773",
117 localWsUrl: "ws://127.0.0.1:3773",
118 endpointUrl: "http://192.168.1.44:3773",
119 advertisedHost: "192.168.1.44",
120 });
121 });
122​
123 it("stays network-accessible even when no LAN address is currently detectable", () => {
124 expect(
125 resolveDesktopServerExposure({
126 mode: "network-accessible",
127 port: 3773,
128 networkInterfaces: {},
129 }),
130 ).toEqual({
131 mode: "network-accessible",
132 bindHost: "0.0.0.0",
133 localHttpUrl: "http://127.0.0.1:3773",
134 localWsUrl: "ws://127.0.0.1:3773",
135 endpointUrl: null,
136 advertisedHost: null,
137 });
138 });
139});