import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { mkdtemp, readFile, rm, stat } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
// Mock os.platform and os.homedir for cross-platform testing
const mockPlatform = vi.fn();
const mockHomedir = vi.fn();
vi.mock("node:os", async (importOriginal) => {
    const orig = await importOriginal();
    return {
        ...orig,
        platform: () => mockPlatform(),
        homedir: () => mockHomedir(),
    };
});
vi.mock("../../utils/paths.js", () => ({
    getLogDir: () => "/mock/logs",
}));
import { detectPlatform, generateLaunchdPlist, generateSystemdService, enableAutostart, disableAutostart, autostartStatus, getLaunchdPlistPath, getSystemdServicePath, } from "../autostart.js";
let tempDir;
beforeEach(async () => {
    tempDir = await mkdtemp(join(tmpdir(), "autostart-test-"));
    mockHomedir.mockReturnValue(tempDir);
    vi.clearAllMocks();
});
afterEach(async () => {
    await rm(tempDir, { recursive: true, force: true });
});
describe("detectPlatform", () => {
    it("should detect macOS", () => {
        mockPlatform.mockReturnValue("darwin");
        expect(detectPlatform()).toBe("macos");
    });
    it("should detect Linux", () => {
        mockPlatform.mockReturnValue("linux");
        expect(detectPlatform()).toBe("linux");
    });
    it("should detect Windows", () => {
        mockPlatform.mockReturnValue("win32");
        expect(detectPlatform()).toBe("windows");
    });
});
describe("generateLaunchdPlist", () => {
    it("should generate correct launchd plist for macOS", () => {
        const plist = generateLaunchdPlist("/usr/local/bin/pocketcoder");
        expect(plist).toContain("com.pocketcoder.daemon");
        expect(plist).toContain("<string>/usr/local/bin/pocketcoder</string>");
        expect(plist).toContain("<string>start</string>");
        expect(plist).toContain("<string>--foreground</string>");
        expect(plist).toContain("RunAtLoad");
        expect(plist).toContain("KeepAlive");
        expect(plist).toContain("SuccessfulExit");
        expect(plist).toContain("daemon.log");
        expect(plist).toContain("daemon.err");
        expect(plist).toContain('<?xml version="1.0"');
        expect(plist).toContain("<!DOCTYPE plist");
    });
});
describe("generateSystemdService", () => {
    it("should generate correct systemd service for Linux", () => {
        const service = generateSystemdService("/usr/local/bin/pocketcoder");
        expect(service).toContain("Description=PocketCoder Daemon");
        expect(service).toContain("After=network.target");
        expect(service).toContain("ExecStart=/usr/local/bin/pocketcoder start --foreground");
        expect(service).toContain("Restart=on-failure");
        expect(service).toContain("RestartSec=5");
        expect(service).toContain("WantedBy=default.target");
        expect(service).toContain("%h/.pocketcoder/logs/daemon.log");
        expect(service).toContain("%h/.pocketcoder/logs/daemon.err");
    });
});
describe("enableAutostart", () => {
    it("should create launchd plist on macOS", async () => {
        mockPlatform.mockReturnValue("darwin");
        const result = await enableAutostart();
        expect(result).toContain("Created launchd plist");
        const plistPath = getLaunchdPlistPath();
        const content = await readFile(plistPath, "utf-8");
        expect(content).toContain("com.pocketcoder.daemon");
    });
    it("should create systemd service on Linux", async () => {
        mockPlatform.mockReturnValue("linux");
        const result = await enableAutostart();
        expect(result).toContain("Created systemd service");
        expect(result).toContain("systemctl --user enable");
        const servicePath = getSystemdServicePath();
        const content = await readFile(servicePath, "utf-8");
        expect(content).toContain("PocketCoder Daemon");
    });
    it("should show manual instructions on Windows", async () => {
        mockPlatform.mockReturnValue("win32");
        const result = await enableAutostart();
        expect(result).toContain("Windows");
        expect(result).toContain("Task Scheduler");
    });
    it("should create parent directories if missing", async () => {
        mockPlatform.mockReturnValue("darwin");
        await enableAutostart();
        const plistPath = getLaunchdPlistPath();
        const s = await stat(plistPath);
        expect(s.isFile()).toBe(true);
    });
});
describe("disableAutostart", () => {
    it("should remove launchd plist on macOS", async () => {
        mockPlatform.mockReturnValue("darwin");
        // First enable to create the file
        await enableAutostart();
        // Then disable
        const result = await disableAutostart();
        expect(result).toContain("Removed launchd plist");
        // File should be gone
        const plistPath = getLaunchdPlistPath();
        await expect(stat(plistPath)).rejects.toThrow();
    });
    it("should remove systemd service on Linux", async () => {
        mockPlatform.mockReturnValue("linux");
        // First enable to create the file
        await enableAutostart();
        // Then disable
        const result = await disableAutostart();
        expect(result).toContain("Removed systemd service");
        expect(result).toContain("systemctl --user disable");
        const servicePath = getSystemdServicePath();
        await expect(stat(servicePath)).rejects.toThrow();
    });
    it("should handle already-removed config gracefully on macOS", async () => {
        mockPlatform.mockReturnValue("darwin");
        const result = await disableAutostart();
        expect(result).toContain("not configured");
    });
    it("should handle already-removed config gracefully on Linux", async () => {
        mockPlatform.mockReturnValue("linux");
        const result = await disableAutostart();
        expect(result).toContain("not configured");
    });
    it("should show manual instructions on Windows", async () => {
        mockPlatform.mockReturnValue("win32");
        const result = await disableAutostart();
        expect(result).toContain("Windows");
        expect(result).toContain("Task Scheduler");
    });
});
describe("autostartStatus", () => {
    it("should report enabled on macOS when plist exists", async () => {
        mockPlatform.mockReturnValue("darwin");
        await enableAutostart();
        const result = await autostartStatus();
        expect(result).toContain("Autostart is enabled");
        expect(result).toContain("launchd plist");
    });
    it("should report not configured on macOS when no plist", async () => {
        mockPlatform.mockReturnValue("darwin");
        const result = await autostartStatus();
        expect(result).toBe("Autostart is not configured");
    });
    it("should report enabled on Linux when service exists", async () => {
        mockPlatform.mockReturnValue("linux");
        await enableAutostart();
        const result = await autostartStatus();
        expect(result).toContain("Autostart is enabled");
        expect(result).toContain("systemd service");
    });
    it("should report not configured on Linux when no service", async () => {
        mockPlatform.mockReturnValue("linux");
        const result = await autostartStatus();
        expect(result).toBe("Autostart is not configured");
    });
    it("should show instructions on Windows", async () => {
        mockPlatform.mockReturnValue("win32");
        const result = await autostartStatus();
        expect(result).toContain("Windows");
        expect(result).toContain("Task Scheduler");
    });
});
//# sourceMappingURL=autostart.test.js.map