|
| 1 | +/*! |
| 2 | + * UDPClusterManager.selectNetworkInterface() branch coverage tests |
| 3 | + */ |
| 4 | +import './mocks'; |
| 5 | +import { expect } from 'chai'; |
| 6 | +import * as mock from 'mock-require'; |
| 7 | + |
| 8 | +describe('UDPClusterManager.selectNetworkInterface()', () => { |
| 9 | + it('should return default when broadcastAddress is undefined', async () => { |
| 10 | + const { UDPClusterManager } = await import('../src'); |
| 11 | + const select = (UDPClusterManager as any).selectNetworkInterface as Function; |
| 12 | + const res = select({}); |
| 13 | + expect(res).to.equal('0.0.0.0'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should return default when broadcastAddress equals limitedBroadcastAddress', async () => { |
| 17 | + const { UDPClusterManager } = await import('../src'); |
| 18 | + const select = (UDPClusterManager as any).selectNetworkInterface as Function; |
| 19 | + const res = select({ broadcastAddress: '127.0.0.255', limitedBroadcastAddress: '127.0.0.255' }); |
| 20 | + expect(res).to.equal('0.0.0.0'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should continue on undefined interface entry and still select matching address', () => { |
| 24 | + // Re-mock os.networkInterfaces to include an undefined entry |
| 25 | + const os = require('node:os'); |
| 26 | + const networkInterfaces = () => ({ bad: undefined, lo: [{ address: '127.0.0.1', family: 'IPv4' }] }); |
| 27 | + mock.stop('os'); |
| 28 | + mock('os', Object.assign({}, os, { networkInterfaces })); |
| 29 | + // Re-require the module to capture new binding |
| 30 | + const { UDPClusterManager } = mock.reRequire('../src/UDPClusterManager'); |
| 31 | + const res = (UDPClusterManager as any).selectNetworkInterface({ broadcastAddress: '127.0.0.255', limitedBroadcastAddress: '255.255.255.255' }); |
| 32 | + expect(res).to.equal('127.0.0.1'); |
| 33 | + |
| 34 | + // restore to base mocks for other tests |
| 35 | + mock.stop('os'); |
| 36 | + mock.reRequire('./mocks/os'); |
| 37 | + mock.reRequire('../src/UDPClusterManager'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should select matching interface address when not equal to limited broadcast', async () => { |
| 41 | + const { UDPClusterManager } = await import('../src'); |
| 42 | + const select = (UDPClusterManager as any).selectNetworkInterface as Function; |
| 43 | + const res = select({ broadcastAddress: '127.0.0.255', limitedBroadcastAddress: '255.255.255.255' }); |
| 44 | + expect(res).to.equal('127.0.0.1'); |
| 45 | + }); |
| 46 | +}); |
0 commit comments