|
1 | 1 | import fs from 'node:fs' |
2 | 2 | import path from 'node:path' |
3 | 3 | import crypto from 'node:crypto' |
4 | | -import { describe, expect, test } from 'vitest' |
| 4 | +import os, { type NetworkInterfaceInfoIPv4 } from 'node:os' |
| 5 | +import { describe, expect, test, vi, onTestFinished } from 'vitest' |
5 | 6 | import { fileURLToPath } from 'mlly' |
6 | 7 | import { |
7 | 8 | asyncFlatten, |
@@ -1096,4 +1097,59 @@ describe('resolveServerUrls', () => { |
1096 | 1097 |
|
1097 | 1098 | expect(result.local).toContain('https://localhost:3000/') |
1098 | 1099 | }) |
| 1100 | + |
| 1101 | + test('captures interface names aligned with the network URLs', () => { |
| 1102 | + const mockServer = createMockServer('IPv4', '0.0.0.0') |
| 1103 | + const networkInterfacesSpy = vi |
| 1104 | + .spyOn(os, 'networkInterfaces') |
| 1105 | + .mockReturnValue({ |
| 1106 | + lo: [ |
| 1107 | + { address: '127.0.0.1', family: 'IPv4' } as NetworkInterfaceInfoIPv4, |
| 1108 | + ], |
| 1109 | + eth0: [ |
| 1110 | + { |
| 1111 | + address: '192.168.1.10', |
| 1112 | + family: 'IPv4', |
| 1113 | + } as NetworkInterfaceInfoIPv4, |
| 1114 | + ], |
| 1115 | + wlan0: [ |
| 1116 | + { address: '10.0.0.5', family: 'IPv4' } as NetworkInterfaceInfoIPv4, |
| 1117 | + ], |
| 1118 | + }) |
| 1119 | + onTestFinished(() => { |
| 1120 | + networkInterfacesSpy.mockRestore() |
| 1121 | + }) |
| 1122 | + |
| 1123 | + const result = resolveServerUrls( |
| 1124 | + mockServer, |
| 1125 | + { https: false } as any, |
| 1126 | + { host: undefined, name: 'localhost' } as any, |
| 1127 | + {}, |
| 1128 | + { rawBase: '/' } as any, |
| 1129 | + ) |
| 1130 | + |
| 1131 | + expect(result.network).toEqual([ |
| 1132 | + 'http://192.168.1.10:3000/', |
| 1133 | + 'http://10.0.0.5:3000/', |
| 1134 | + ]) |
| 1135 | + expect(result.networkInterfaceNames).toStrictEqual(['eth0', 'wlan0']) |
| 1136 | + expect(result.networkInterfaceNames).toHaveLength(result.network.length) |
| 1137 | + // The loopback interface is reported as a local URL, not a network one. |
| 1138 | + expect(result.local).toContain('http://localhost:3000/') |
| 1139 | + }) |
| 1140 | + |
| 1141 | + test('uses an undefined interface name for an explicit network host', () => { |
| 1142 | + const mockServer = createMockServer('IPv4', '0.0.0.0') |
| 1143 | + |
| 1144 | + const result = resolveServerUrls( |
| 1145 | + mockServer, |
| 1146 | + { https: false } as any, |
| 1147 | + { host: 'example.com', name: 'example.com' } as any, |
| 1148 | + {}, |
| 1149 | + { rawBase: '/' } as any, |
| 1150 | + ) |
| 1151 | + |
| 1152 | + expect(result.network).toStrictEqual(['http://example.com:3000/']) |
| 1153 | + expect(result.networkInterfaceNames).toStrictEqual([undefined]) |
| 1154 | + }) |
1099 | 1155 | }) |
0 commit comments