import { mkdtemp, writeFile, appendFile, rm } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
vi.mock("../../../utils/paths.js", () => ({
    getLogDir: vi.fn(),
}));
import { getLogDir } from "../../../utils/paths.js";
import { logsCommand } from "../logs.js";
const mockGetLogDir = vi.mocked(getLogDir);
describe("logs command", () => {
    let tempDir;
    beforeEach(async () => {
        vi.clearAllMocks();
        process.exitCode = undefined;
        tempDir = await mkdtemp(join(tmpdir(), "logs-test-"));
        mockGetLogDir.mockReturnValue(tempDir);
    });
    afterEach(async () => {
        await rm(tempDir, { recursive: true, force: true });
    });
    it("should show last 50 lines by default", async () => {
        // Create a log file with 100 lines
        const lines = Array.from({ length: 100 }, (_, i) => `Line ${i + 1}`);
        await writeFile(join(tempDir, "daemon.log"), lines.join("\n") + "\n");
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await logsCommand({ tail: "50" });
        // Should show lines 51–100
        expect(consoleSpy).toHaveBeenCalledTimes(50);
        expect(consoleSpy).toHaveBeenCalledWith("Line 51");
        expect(consoleSpy).toHaveBeenCalledWith("Line 100");
        // Should NOT have line 50
        expect(consoleSpy).not.toHaveBeenCalledWith("Line 50");
        consoleSpy.mockRestore();
    });
    it("should show specified number of lines with --tail", async () => {
        const lines = Array.from({ length: 20 }, (_, i) => `Entry ${i + 1}`);
        await writeFile(join(tempDir, "daemon.log"), lines.join("\n") + "\n");
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await logsCommand({ tail: "5" });
        expect(consoleSpy).toHaveBeenCalledTimes(5);
        expect(consoleSpy).toHaveBeenCalledWith("Entry 16");
        expect(consoleSpy).toHaveBeenCalledWith("Entry 20");
        consoleSpy.mockRestore();
    });
    it("should follow log file with -f flag", async () => {
        const logFile = join(tempDir, "daemon.log");
        await writeFile(logFile, "Initial line\n");
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        // Start follow in background, then append and signal
        const followPromise = logsCommand({ tail: "50", follow: true });
        // Wait a bit, then append new content
        await new Promise((resolve) => setTimeout(resolve, 100));
        await appendFile(logFile, "New line from follow\n");
        // Wait for the poll interval to pick it up
        await new Promise((resolve) => setTimeout(resolve, 700));
        // Send SIGINT to stop following
        process.emit("SIGINT", "SIGINT");
        await followPromise;
        // Should have printed the initial line (from tail) and the new line (from follow)
        expect(consoleSpy).toHaveBeenCalledWith("Initial line");
        expect(consoleSpy).toHaveBeenCalledWith("New line from follow");
        consoleSpy.mockRestore();
    });
    it("should handle missing log file", async () => {
        // Don't create the log file
        mockGetLogDir.mockReturnValue(join(tempDir, "nonexistent"));
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await logsCommand({ tail: "50" });
        expect(consoleSpy).toHaveBeenCalledWith("No log file found. Is the daemon running?");
        consoleSpy.mockRestore();
    });
    it("should show all lines when file has fewer than tail count", async () => {
        const lines = ["Line A", "Line B", "Line C"];
        await writeFile(join(tempDir, "daemon.log"), lines.join("\n") + "\n");
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await logsCommand({ tail: "50" });
        expect(consoleSpy).toHaveBeenCalledTimes(3);
        expect(consoleSpy).toHaveBeenCalledWith("Line A");
        expect(consoleSpy).toHaveBeenCalledWith("Line B");
        expect(consoleSpy).toHaveBeenCalledWith("Line C");
        consoleSpy.mockRestore();
    });
});
//# sourceMappingURL=cli-logs.test.js.map