import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { mkdtemp, rm, readFile, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
describe("PID file management", () => {
    let tempDir;
    let pidPath;
    beforeEach(async () => {
        tempDir = await mkdtemp(join(tmpdir(), "pocketcoder-pid-test-"));
        pidPath = join(tempDir, "daemon.pid");
    });
    afterEach(async () => {
        vi.restoreAllMocks();
        await rm(tempDir, { recursive: true, force: true });
    });
    async function importPidModule() {
        vi.resetModules();
        vi.doMock("../paths.js", () => ({
            getPidPath: () => pidPath,
        }));
        return await import("../pid.js");
    }
    it("should write PID file with current process ID", async () => {
        const { writePidFile } = await importPidModule();
        await writePidFile();
        const content = await readFile(pidPath, "utf-8");
        expect(content.trim()).toBe(String(process.pid));
    });
    it("should read PID from file", async () => {
        const { readPidFile } = await importPidModule();
        await writeFile(pidPath, "12345");
        const pid = await readPidFile();
        expect(pid).toBe(12345);
    });
    it("should return null when PID file does not exist", async () => {
        const { readPidFile } = await importPidModule();
        const pid = await readPidFile();
        expect(pid).toBeNull();
    });
    it("should remove PID file", async () => {
        const { writePidFile, removePidFile, readPidFile } = await importPidModule();
        await writePidFile();
        await removePidFile();
        const pid = await readPidFile();
        expect(pid).toBeNull();
    });
    it("should not throw when removing non-existent PID file", async () => {
        const { removePidFile } = await importPidModule();
        await expect(removePidFile()).resolves.toBeUndefined();
    });
    it("should detect running process by PID", async () => {
        const { isProcessRunning } = await importPidModule();
        // Current process should be detected as running
        expect(isProcessRunning(process.pid)).toBe(true);
    });
    it("should return false for non-running process", async () => {
        const { isProcessRunning } = await importPidModule();
        // Use a very high PID that is unlikely to be running
        expect(isProcessRunning(999999)).toBe(false);
    });
    it("should detect existing daemon via PID file and running process", async () => {
        const { writePidFile, checkExistingDaemon } = await importPidModule();
        await writePidFile();
        const running = await checkExistingDaemon();
        expect(running).toBe(true);
    });
    it("should return false when PID file references dead process", async () => {
        const { checkExistingDaemon } = await importPidModule();
        await writeFile(pidPath, "999999");
        const running = await checkExistingDaemon();
        expect(running).toBe(false);
    });
    it("should return false when no PID file exists", async () => {
        const { checkExistingDaemon } = await importPidModule();
        const running = await checkExistingDaemon();
        expect(running).toBe(false);
    });
});
//# sourceMappingURL=pid.test.js.map