import { spawn } from 'node:child_process'; import { EventEmitter } from 'node:events'; import { existsSync } from 'node:fs'; import { createCliInterruptWaiter, waitForCliInterrupt, } from '@midscene/shared/cli/interrupt'; import { afterEach, describe, expect, it, rs } from '@rstest/core'; import { checkXvfbInstalled, createXvfbSignalCleanup, findAvailableDisplay, needsXvfb, scheduleXvfbStopAfterProcessExit, } from '../../src/xvfb'; rs.mock('node:fs', () => ({ existsSync: rs.fn(() => false), })); rs.mock('node:child_process', () => ({ execSync: rs.fn(() => { throw new Error('not found'); }), spawn: rs.fn(), })); describe('needsXvfb', () => { const originalPlatform = process.platform; afterEach(() => { Object.defineProperty(process, 'platform', { value: originalPlatform }); }); it('should return false on non-Linux platforms', () => { Object.defineProperty(process, 'platform', { value: 'darwin' }); expect(needsXvfb(true)).toBe(false); }); it('should return false on Windows', () => { Object.defineProperty(process, 'platform', { value: 'win32' }); expect(needsXvfb(true)).toBe(false); }); it('should return true on Linux with explicit true', () => { Object.defineProperty(process, 'platform', { value: 'linux' }); expect(needsXvfb(true)).toBe(true); }); it('should return false on Linux with explicit false', () => { Object.defineProperty(process, 'platform', { value: 'linux' }); expect(needsXvfb(false)).toBe(false); }); it('should return false on Linux without explicit option', () => { Object.defineProperty(process, 'platform', { value: 'linux' }); expect(needsXvfb()).toBe(false); expect(needsXvfb(undefined)).toBe(false); }); }); describe('findAvailableDisplay', () => { it('should return startFrom when no lock files exist', () => { rs.mocked(existsSync).mockReturnValue(false); expect(findAvailableDisplay(99)).toBe(99); }); it('should skip occupied display numbers', () => { rs.mocked(existsSync) .mockReturnValueOnce(true) // :99 occupied .mockReturnValueOnce(true) // :100 occupied .mockReturnValueOnce(false); // :101 free expect(findAvailableDisplay(99)).toBe(101); }); it('should throw if no display is available', () => { rs.mocked(existsSync).mockReturnValue(true); expect(() => findAvailableDisplay(99)).toThrow( 'No available display number found', ); }); }); describe('checkXvfbInstalled', () => { it('should return false when Xvfb is not installed', () => { expect(checkXvfbInstalled()).toBe(false); }); it('should return true when Xvfb is installed', async () => { const { execSync } = await import('node:child_process'); rs.mocked(execSync).mockReturnValueOnce(Buffer.from('/usr/bin/Xvfb')); expect(checkXvfbInstalled()).toBe(true); }); }); describe('scheduleXvfbStopAfterProcessExit', () => { it('unrefs Xvfb and a detached process-exit monitor', () => { const xvfbUnref = rs.fn(); const monitorUnref = rs.fn(); const monitorOn = rs.fn(); rs.mocked(spawn).mockReturnValueOnce({ on: monitorOn, unref: monitorUnref, } as never); scheduleXvfbStopAfterProcessExit( { display: ':99', process: { pid: 4321, unref: xvfbUnref } as never, stop: rs.fn(), }, 1234, ); expect(spawn).toHaveBeenCalledWith( process.execPath, ['-e', expect.any(String), '1234', '4321'], { detached: true, stdio: 'ignore' }, ); expect(monitorOn).toHaveBeenCalledWith('error', expect.any(Function)); expect(xvfbUnref).toHaveBeenCalledOnce(); expect(monitorUnref).toHaveBeenCalledOnce(); }); }); describe('createXvfbSignalCleanup', () => { it('cleans up when the host only has unrelated SIGINT listeners', () => { const source = new EventEmitter(); const cleanup = rs.fn(); source.on('SIGINT', () => {}); source.on('SIGINT', createXvfbSignalCleanup(cleanup, source)); source.emit('SIGINT'); expect(cleanup).toHaveBeenCalledOnce(); }); it('defers cleanup while a foreground recorder is handling SIGINT', async () => { const source = new EventEmitter(); const cleanup = rs.fn(); source.on('SIGINT', createXvfbSignalCleanup(cleanup, source)); const stopped = waitForCliInterrupt(0, source); source.emit('SIGINT'); await expect(stopped).resolves.toBe('sigint'); expect(cleanup).not.toHaveBeenCalled(); source.emit('SIGINT'); expect(cleanup).toHaveBeenCalledOnce(); }); it('keeps Xvfb alive through a forwarded SIGTERM while saving', async () => { const source = new EventEmitter(); const cleanup = rs.fn(); const signalCleanup = createXvfbSignalCleanup(cleanup, source); source.on('SIGINT', signalCleanup); source.on('SIGTERM', signalCleanup); const waiter = createCliInterruptWaiter(0, { source }); source.emit('SIGINT'); await expect(waiter.result).resolves.toBe('sigint'); source.emit('SIGTERM'); expect(cleanup).not.toHaveBeenCalled(); waiter.dispose(); source.emit('SIGTERM'); expect(cleanup).toHaveBeenCalledOnce(); }); });