|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import * as path from 'node:path'; |
| 5 | +import { forceStopDaemon } from '../daemon-control.ts'; |
| 6 | +import { |
| 7 | + readDaemonRegistryEntry, |
| 8 | + type DaemonRegistryEntry, |
| 9 | + writeDaemonRegistryEntry, |
| 10 | +} from '../../daemon/daemon-registry.ts'; |
| 11 | +import { |
| 12 | + daemonDirForWorkspaceKey, |
| 13 | + setDaemonRunDirOverrideForTests, |
| 14 | +} from '../../daemon/socket-path.ts'; |
| 15 | +import { setXcodeBuildMCPAppDirOverrideForTests } from '../../utils/log-paths.ts'; |
| 16 | + |
| 17 | +const daemonPid = 123_456; |
| 18 | + |
| 19 | +function createMissingPidError(): NodeJS.ErrnoException { |
| 20 | + const error = new Error('no such process') as NodeJS.ErrnoException; |
| 21 | + error.code = 'ESRCH'; |
| 22 | + return error; |
| 23 | +} |
| 24 | + |
| 25 | +function createEntry(overrides: Partial<DaemonRegistryEntry> = {}): DaemonRegistryEntry { |
| 26 | + const workspaceKey = overrides.workspaceKey ?? 'workspace-a'; |
| 27 | + return { |
| 28 | + workspaceKey, |
| 29 | + workspaceRoot: `/workspaces/${workspaceKey}`, |
| 30 | + socketPath: path.join(daemonDirForWorkspaceKey(workspaceKey), 'd.sock'), |
| 31 | + pid: daemonPid, |
| 32 | + startedAt: '2026-05-05T00:00:00.000Z', |
| 33 | + enabledWorkflows: ['build'], |
| 34 | + version: '1.0.0', |
| 35 | + instanceId: 'daemon-instance-a', |
| 36 | + ...overrides, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +describe('daemon control', () => { |
| 41 | + let appDir: string; |
| 42 | + let daemonRunDir: string; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + appDir = mkdtempSync(path.join(tmpdir(), 'xcodebuildmcp-daemon-control-app-')); |
| 46 | + daemonRunDir = mkdtempSync(path.join(tmpdir(), 'xcodebuildmcp-daemon-control-run-')); |
| 47 | + setXcodeBuildMCPAppDirOverrideForTests(appDir); |
| 48 | + setDaemonRunDirOverrideForTests(daemonRunDir); |
| 49 | + }); |
| 50 | + |
| 51 | + afterEach(() => { |
| 52 | + vi.useRealTimers(); |
| 53 | + vi.restoreAllMocks(); |
| 54 | + setXcodeBuildMCPAppDirOverrideForTests(null); |
| 55 | + setDaemonRunDirOverrideForTests(null); |
| 56 | + rmSync(appDir, { recursive: true, force: true }); |
| 57 | + rmSync(daemonRunDir, { recursive: true, force: true }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('does not unlink sockets without registry metadata', async () => { |
| 61 | + const socketPath = path.join(daemonRunDir, 'missing-registry.sock'); |
| 62 | + mkdirSync(path.dirname(socketPath), { recursive: true, mode: 0o700 }); |
| 63 | + writeFileSync(socketPath, 'socket placeholder'); |
| 64 | + |
| 65 | + await expect(forceStopDaemon(socketPath)).rejects.toThrow('registry metadata is missing'); |
| 66 | + |
| 67 | + expect(existsSync(socketPath)).toBe(true); |
| 68 | + }); |
| 69 | + |
| 70 | + it('unregisters daemon files only after SIGTERM stops the process', async () => { |
| 71 | + const entry = createEntry(); |
| 72 | + writeDaemonRegistryEntry(entry); |
| 73 | + mkdirSync(path.dirname(entry.socketPath), { recursive: true, mode: 0o700 }); |
| 74 | + writeFileSync(entry.socketPath, 'socket placeholder'); |
| 75 | + let alive = true; |
| 76 | + const kill = vi.spyOn(process, 'kill').mockImplementation((( |
| 77 | + _pid: number, |
| 78 | + signal?: string | number, |
| 79 | + ) => { |
| 80 | + if (signal === 0) { |
| 81 | + if (alive) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + throw createMissingPidError(); |
| 85 | + } |
| 86 | + if (signal === 'SIGTERM') { |
| 87 | + alive = false; |
| 88 | + } |
| 89 | + return true; |
| 90 | + }) as typeof process.kill); |
| 91 | + |
| 92 | + await forceStopDaemon(entry.socketPath); |
| 93 | + |
| 94 | + expect(kill).toHaveBeenCalledWith(entry.pid, 'SIGTERM'); |
| 95 | + expect(readDaemonRegistryEntry(entry.workspaceKey)).toBeNull(); |
| 96 | + expect(existsSync(entry.socketPath)).toBe(false); |
| 97 | + }); |
| 98 | + |
| 99 | + it('cleans daemon files when the stopped PID is reused before cleanup', async () => { |
| 100 | + const entry = createEntry(); |
| 101 | + writeDaemonRegistryEntry(entry); |
| 102 | + mkdirSync(path.dirname(entry.socketPath), { recursive: true, mode: 0o700 }); |
| 103 | + writeFileSync(entry.socketPath, 'socket placeholder'); |
| 104 | + let zeroSignalChecksAfterTerm = 0; |
| 105 | + const kill = vi.spyOn(process, 'kill').mockImplementation((( |
| 106 | + _pid: number, |
| 107 | + signal?: string | number, |
| 108 | + ) => { |
| 109 | + if (signal === 0) { |
| 110 | + zeroSignalChecksAfterTerm += 1; |
| 111 | + if (zeroSignalChecksAfterTerm === 1) { |
| 112 | + throw createMissingPidError(); |
| 113 | + } |
| 114 | + return true; |
| 115 | + } |
| 116 | + return true; |
| 117 | + }) as typeof process.kill); |
| 118 | + |
| 119 | + await forceStopDaemon(entry.socketPath); |
| 120 | + |
| 121 | + expect(kill).toHaveBeenCalledWith(entry.pid, 'SIGTERM'); |
| 122 | + expect(readDaemonRegistryEntry(entry.workspaceKey)).toBeNull(); |
| 123 | + expect(existsSync(entry.socketPath)).toBe(false); |
| 124 | + }); |
| 125 | + |
| 126 | + it('uses SIGKILL when the process stays alive after SIGTERM', async () => { |
| 127 | + vi.useFakeTimers(); |
| 128 | + const entry = createEntry(); |
| 129 | + writeDaemonRegistryEntry(entry); |
| 130 | + mkdirSync(path.dirname(entry.socketPath), { recursive: true, mode: 0o700 }); |
| 131 | + writeFileSync(entry.socketPath, 'socket placeholder'); |
| 132 | + let alive = true; |
| 133 | + const kill = vi.spyOn(process, 'kill').mockImplementation((( |
| 134 | + _pid: number, |
| 135 | + signal?: string | number, |
| 136 | + ) => { |
| 137 | + if (signal === 0) { |
| 138 | + if (alive) { |
| 139 | + return true; |
| 140 | + } |
| 141 | + throw createMissingPidError(); |
| 142 | + } |
| 143 | + if (signal === 'SIGKILL') { |
| 144 | + alive = false; |
| 145 | + } |
| 146 | + return true; |
| 147 | + }) as typeof process.kill); |
| 148 | + |
| 149 | + const stopped = forceStopDaemon(entry.socketPath); |
| 150 | + await vi.advanceTimersByTimeAsync(1500); |
| 151 | + await stopped; |
| 152 | + |
| 153 | + expect(kill).toHaveBeenCalledWith(entry.pid, 'SIGTERM'); |
| 154 | + expect(kill).toHaveBeenCalledWith(entry.pid, 'SIGKILL'); |
| 155 | + expect(readDaemonRegistryEntry(entry.workspaceKey)).toBeNull(); |
| 156 | + expect(existsSync(entry.socketPath)).toBe(false); |
| 157 | + }); |
| 158 | + |
| 159 | + it('preserves registry metadata and socket when the process remains alive', async () => { |
| 160 | + vi.useFakeTimers(); |
| 161 | + const entry = createEntry(); |
| 162 | + writeDaemonRegistryEntry(entry); |
| 163 | + mkdirSync(path.dirname(entry.socketPath), { recursive: true, mode: 0o700 }); |
| 164 | + writeFileSync(entry.socketPath, 'socket placeholder'); |
| 165 | + vi.spyOn(process, 'kill').mockImplementation((() => true) as typeof process.kill); |
| 166 | + |
| 167 | + const stopped = forceStopDaemon(entry.socketPath); |
| 168 | + const stoppedExpectation = expect(stopped).rejects.toThrow( |
| 169 | + `Daemon PID ${entry.pid} did not exit after SIGKILL`, |
| 170 | + ); |
| 171 | + await vi.advanceTimersByTimeAsync(3000); |
| 172 | + |
| 173 | + await stoppedExpectation; |
| 174 | + expect(readDaemonRegistryEntry(entry.workspaceKey)).toEqual(entry); |
| 175 | + expect(existsSync(entry.socketPath)).toBe(true); |
| 176 | + }); |
| 177 | +}); |
0 commit comments