import { afterEach, describe, expect, it, vi } from 'vitest'; const { execSyncMock, isRootMock } = vi.hoisted(() => ({ execSyncMock: vi.fn(), isRootMock: vi.fn(() => false), })); vi.mock('child_process', () => ({ execSync: execSyncMock, })); vi.mock('./platform.js', () => ({ isRoot: isRootMock, })); import { checkSystemdService, checkSystemdServiceInScope, getServiceChecks, } from './verify-services.js'; import type { ServiceDef } from './service-defs.js'; describe('verify service checks', () => { afterEach(() => { execSyncMock.mockReset(); isRootMock.mockReset(); isRootMock.mockReturnValue(false); vi.restoreAllMocks(); }); it('checks systemd user services with the user prefix', () => { isRootMock.mockReturnValue(false); execSyncMock.mockReturnValue(undefined); expect(checkSystemdService('ejclaw')).toBe('running'); expect(execSyncMock).toHaveBeenCalledWith( 'systemctl --user is-active ejclaw', { stdio: 'ignore', }, ); }); it('checks systemd services in both explicit scopes', () => { execSyncMock.mockImplementation((cmd: string) => { if (cmd === 'systemctl is-active ejclaw-codex') { return undefined; } if (cmd === 'systemctl --user is-active ejclaw-codex') { throw new Error('inactive'); } if (cmd === 'systemctl --user list-unit-files') { return ''; } throw new Error(`unexpected command: ${cmd}`); }); expect(checkSystemdServiceInScope('ejclaw-codex', 'system')).toBe( 'running', ); expect(checkSystemdServiceInScope('ejclaw-codex', 'user')).toBe( 'not_found', ); }); it('treats known but inactive systemd services as stopped', () => { execSyncMock .mockImplementationOnce(() => { throw new Error('inactive'); }) .mockReturnValueOnce('ejclaw.service enabled\n'); expect(checkSystemdService('ejclaw')).toBe('stopped'); }); it('builds per-service status checks from service definitions', () => { const defs: ServiceDef[] = [ { kind: 'primary', name: 'ejclaw', description: 'EJClaw', logName: 'ejclaw', }, { kind: 'primary', name: 'ejclaw-secondary', description: 'Secondary', logName: 'ejclaw-secondary', }, ]; execSyncMock.mockImplementation((cmd: string) => { if (cmd === 'systemctl --user is-active ejclaw') return undefined; if (cmd === 'systemctl --user list-unit-files') return ''; throw new Error('inactive'); }); expect(getServiceChecks(defs, 'systemd')).toEqual([ { name: 'ejclaw', status: 'running' }, { name: 'ejclaw-secondary', status: 'not_found' }, ]); expect(getServiceChecks(defs, 'none')).toEqual([ { name: 'ejclaw', status: 'not_found' }, { name: 'ejclaw-secondary', status: 'not_found' }, ]); }); });