Files
tstest/test/tapbundle/test.network-tools.node.ts

56 lines
1.7 KiB
TypeScript

import { tap, expect } from '../../ts_tapbundle/index.js';
import { tapNodeTools } from '../../ts_tapbundle_serverside/index.js';
tap.test('should find a single free port', async () => {
const port = await tapNodeTools.findFreePort();
expect(port).toBeTypeOf('number');
expect(port).toBeGreaterThanOrEqual(3000);
expect(port).toBeLessThanOrEqual(60000);
});
tap.test('should find a free port in a specific range', async () => {
const port = await tapNodeTools.findFreePort({
startPort: 8000,
endPort: 9000,
});
expect(port).toBeGreaterThanOrEqual(8000);
expect(port).toBeLessThanOrEqual(9000);
});
tap.test('should find a free port with exclusions', async () => {
const port = await tapNodeTools.findFreePort({
startPort: 10000,
endPort: 10100,
exclude: [10000, 10001, 10002],
});
expect(port).toBeGreaterThanOrEqual(10000);
expect(port).toBeLessThanOrEqual(10100);
expect(port).not.toEqual(10000);
expect(port).not.toEqual(10001);
expect(port).not.toEqual(10002);
});
tap.test('should find multiple free ports', async () => {
const ports = await tapNodeTools.findFreePorts(3);
expect(ports).toHaveLength(3);
// All ports should be distinct
const uniquePorts = new Set(ports);
expect(uniquePorts.size).toEqual(3);
for (const port of ports) {
expect(port).toBeGreaterThanOrEqual(3000);
expect(port).toBeLessThanOrEqual(60000);
}
});
tap.test('should find a consecutive port range', async () => {
const ports = await tapNodeTools.findFreePortRange(3, {
startPort: 20000,
endPort: 30000,
});
expect(ports).toHaveLength(3);
expect(ports[1]).toEqual(ports[0] + 1);
expect(ports[2]).toEqual(ports[0] + 2);
});
tap.start();