Files
OneOS1.2/vite-plugins/portOccupancy.test.ts
王冕 7ef8852919 Initial commit: OneOS prototype workspace baseline.
Include weekly report and other prototype sources with project tooling config.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-26 15:36:52 +08:00

45 lines
1.1 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import {
findListeningPidsOnPort,
releaseListeningProcessesOnPort,
} from './utils/portOccupancy';
describe('client port occupancy helpers', () => {
it('reads listening PIDs with lsof on macOS and Linux', () => {
const spawnSync = vi.fn(() => ({
stdout: '123\n456\n123\n',
stderr: '',
status: 0,
})) as any;
expect(findListeningPidsOnPort(51720, {
platform: 'darwin',
spawnSync,
})).toEqual([123, 456]);
expect(spawnSync).toHaveBeenCalledWith('lsof', [
'-tiTCP',
':51720',
'-sTCP:LISTEN',
], expect.objectContaining({ encoding: 'utf8' }));
});
it('terminates other listening processes while ignoring the current process', () => {
const spawnSync = vi.fn(() => ({
stdout: '111\n222\n',
stderr: '',
status: 0,
})) as any;
const killPid = vi.fn();
expect(releaseListeningProcessesOnPort(51720, {
platform: 'linux',
spawnSync,
killPid,
currentPid: 111,
waitMs: 0,
})).toEqual([222]);
expect(killPid).toHaveBeenCalledWith(222, 'SIGTERM');
});
});