import { describe, expect, it } from "bun:test"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { defaultConfig, loadGhostConfig, normalizeGhostConfig, } from "../config.ts"; describe("normalizeGhostConfig", () => { it("uses strict defaults for empty config", () => { expect(normalizeGhostConfig()).toEqual(defaultConfig); }); it("lets project config override global config field by field", () => { const config = normalizeGhostConfig( { model: { provider: "anthropic", id: "global-model" }, overlay: { anchor: "top-left", width: "50%", maxHeight: "40%", margin: { top: 1, bottom: 2, left: 3, right: 4 }, }, }, { model: { id: "project-model" }, overlay: { width: "90%", margin: { bottom: 9 } }, }, ); expect(config).toEqual({ model: { provider: "anthropic", id: "project-model" }, overlay: { anchor: "top-left", width: "90%", maxHeight: "40%", margin: { top: 1, bottom: 9, left: 3, right: 4 }, }, }); }); it("rejects invalid anchors, sizes, margins, and model values", () => { const config = normalizeGhostConfig( { model: { provider: "", id: 42 }, overlay: { anchor: "bottom", width: "wide", maxHeight: "0%", margin: { top: -1, bottom: Number.NaN, left: "2", right: Infinity }, }, }, {}, ); expect(config).toEqual(defaultConfig); }); it("accepts every real pi overlay anchor", () => { for (const anchor of [ "center", "top-left", "top-center", "top-right", "right-center", "bottom-right", "bottom-center", "bottom-left", "left-center", ]) { expect( normalizeGhostConfig({}, { overlay: { anchor } }).overlay.anchor, ).toBe(anchor); } }); it("accepts numeric and percentage sizes only", () => { expect( normalizeGhostConfig({}, { overlay: { width: 72, maxHeight: "33.5%" } }) .overlay, ).toMatchObject({ width: 72, maxHeight: "33.5%" }); expect( normalizeGhostConfig({}, { overlay: { width: 0, maxHeight: -1 } }) .overlay, ).toMatchObject({ width: defaultConfig.overlay.width, maxHeight: defaultConfig.overlay.maxHeight, }); }); it("ignores deleted legacy junk without shaping config", () => { const config = normalizeGhostConfig( {}, { documents: ["x"], interval: 10, behavior: { hideOnClose: false } }, ); expect(config).toEqual(defaultConfig); }); }); describe("loadGhostConfig", () => { it("reports invalid project JSON instead of silently swallowing it", () => { const cwd = mkdtempSync(join(tmpdir(), "ghost-config-")); const piDir = join(cwd, ".pi"); const errors: string[] = []; mkdirSync(piDir); writeFileSync(join(piDir, "ghost.json"), "{ nope", "utf-8"); try { const config = loadGhostConfig(cwd, (error) => errors.push(`${error.path}: ${error.message}`), ); expect(config).toEqual(defaultConfig); expect(errors).toHaveLength(1); expect(errors[0]).toContain("ghost.json"); } finally { rmSync(cwd, { recursive: true, force: true }); } }); });