|
| 1 | +import { networkInterfaces } from 'node:os' |
| 2 | + |
| 3 | +import { describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +import { getNetworkAddresses } from '../../src/dev/listen' |
| 6 | + |
| 7 | +vi.mock('node:os', () => ({ |
| 8 | + networkInterfaces: vi.fn(), |
| 9 | + release: () => 'test', |
| 10 | +})) |
| 11 | + |
| 12 | +const mocked = vi.mocked(networkInterfaces) |
| 13 | + |
| 14 | +describe('getNetworkAddresses', () => { |
| 15 | + it('should return external IPv4 addresses', () => { |
| 16 | + mocked.mockReturnValue({ |
| 17 | + lo0: [{ address: '127.0.0.1', family: 'IPv4', internal: true } as any], |
| 18 | + en0: [ |
| 19 | + { address: '192.168.1.20', family: 'IPv4', internal: false } as any, |
| 20 | + { address: 'fe80::1', family: 'IPv6', internal: false } as any, |
| 21 | + ], |
| 22 | + }) |
| 23 | + |
| 24 | + expect(getNetworkAddresses()).toEqual(['192.168.1.20']) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should ignore IPv4 link-local addresses', () => { |
| 28 | + mocked.mockReturnValue({ |
| 29 | + bridge0: [{ address: '169.254.13.37', family: 'IPv4', internal: false } as any], |
| 30 | + en0: [{ address: '192.168.1.20', family: 'IPv4', internal: false } as any], |
| 31 | + }) |
| 32 | + |
| 33 | + expect(getNetworkAddresses()).toEqual(['192.168.1.20']) |
| 34 | + }) |
| 35 | +}) |
0 commit comments