import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import type { Paths } from "./paths"; import { readGlobalAliases, writeGlobalAliases, readSiteAliases, writeSiteAliases, effectiveSiteAliases, } from "./aliasesStore"; import { DEFAULT_ALIASES } from "./searchAliases"; function tempPaths(): { paths: Paths; cleanup: () => void } { const dir = mkdtempSync(path.join(tmpdir(), "aliases-store-")); // Only the fields aliasesStore touches need to be real. const paths = { sitesDir: path.join(dir, "sites"), globalAliasesFile: path.join(dir, "search-aliases.json"), } as Paths; return { paths, cleanup: () => rmSync(dir, { recursive: true, force: true }) }; } test("readGlobalAliases returns the seeded defaults when no file exists", () => { const { paths, cleanup } = tempPaths(); try { assert.deepEqual(readGlobalAliases(paths).aliases, DEFAULT_ALIASES); } finally { cleanup(); } }); test("global aliases round-trip through write/read", () => { const { paths, cleanup } = tempPaths(); try { const cfg = { aliases: [ { id: "x", label: "x", triggers: ["a", "b"], suggestion: "s", useRegex: true, }, ], }; writeGlobalAliases(paths, cfg); const back = readGlobalAliases(paths); assert.equal(back.aliases.length, 1); assert.equal(back.aliases[0].id, "x"); assert.deepEqual(back.aliases[0].triggers, ["a", "b"]); } finally { cleanup(); } }); test("an empty-but-present global file yields no aliases (defaults deleted)", () => { const { paths, cleanup } = tempPaths(); try { writeGlobalAliases(paths, { aliases: [] }); assert.deepEqual(readGlobalAliases(paths).aliases, []); } finally { cleanup(); } }); test("readSiteAliases is empty when unauthored", () => { const { paths, cleanup } = tempPaths(); try { assert.deepEqual(readSiteAliases(paths, "mysite").aliases, []); } finally { cleanup(); } }); test("effectiveSiteAliases merges global with per-site overrides by id", () => { const { paths, cleanup } = tempPaths(); try { writeGlobalAliases(paths, { aliases: [ { id: "g1", label: "g1", triggers: ["g"], suggestion: "G", useRegex: true }, { id: "shared", label: "shared", triggers: ["s"], suggestion: "GLOBAL", useRegex: true }, ], }); writeSiteAliases(paths, "mysite", { aliases: [ { id: "shared", label: "shared", triggers: ["s"], suggestion: "SITE", useRegex: true }, { id: "s1", label: "s1", triggers: ["x"], suggestion: "S", useRegex: true }, ], }); const eff = effectiveSiteAliases(paths, "mysite"); assert.equal(eff.length, 3); assert.equal(eff.find((a) => a.id === "shared")?.suggestion, "SITE"); assert.ok(eff.some((a) => a.id === "g1")); assert.ok(eff.some((a) => a.id === "s1")); } finally { cleanup(); } });