import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { mkdtemp, rm, stat } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
describe("ensureConfigDirs", () => {
    let tempDir;
    let configDir;
    let logDir;
    beforeEach(async () => {
        tempDir = await mkdtemp(join(tmpdir(), "pocketcoder-config-test-"));
        configDir = join(tempDir, "config", "pocketcoder");
        logDir = join(tempDir, "logs");
    });
    afterEach(async () => {
        vi.restoreAllMocks();
        await rm(tempDir, { recursive: true, force: true });
    });
    async function importEnsureConfigDirs() {
        vi.resetModules();
        vi.doMock("../paths.js", () => ({
            getConfigDir: () => configDir,
            getLogDir: () => logDir,
        }));
        const mod = await import("../config.js");
        return mod.ensureConfigDirs;
    }
    it("should create config directory if it does not exist", async () => {
        const ensureConfigDirs = await importEnsureConfigDirs();
        await ensureConfigDirs();
        const configStat = await stat(configDir);
        expect(configStat.isDirectory()).toBe(true);
    });
    it("should create logs subdirectory", async () => {
        const ensureConfigDirs = await importEnsureConfigDirs();
        await ensureConfigDirs();
        const logStat = await stat(logDir);
        expect(logStat.isDirectory()).toBe(true);
    });
    it("should be idempotent when called multiple times", async () => {
        const ensureConfigDirs = await importEnsureConfigDirs();
        await ensureConfigDirs();
        await ensureConfigDirs();
        const configStat = await stat(configDir);
        expect(configStat.isDirectory()).toBe(true);
        const logStat = await stat(logDir);
        expect(logStat.isDirectory()).toBe(true);
    });
    it("should not throw if directories already exist", async () => {
        const ensureConfigDirs = await importEnsureConfigDirs();
        await ensureConfigDirs();
        await expect(ensureConfigDirs()).resolves.toBeUndefined();
    });
    it("should create directories with 0700 permissions on Unix", async () => {
        // Skip on Windows
        if (process.platform === "win32")
            return;
        const ensureConfigDirs = await importEnsureConfigDirs();
        await ensureConfigDirs();
        const configStat = await stat(configDir);
        const mode = configStat.mode & 0o777;
        expect(mode).toBe(0o700);
        const logStat = await stat(logDir);
        const logMode = logStat.mode & 0o777;
        expect(logMode).toBe(0o700);
    });
});
//# sourceMappingURL=config.test.js.map