import test from "node:test"; import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import { fileURLToPath } from "node:url"; import path from "node:path"; import { filterFontItems, groupFontItems, normalizeFontItems, } from "../app/static/font-picker.js"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); test("font catalog normalization, grouping, and search are stable", () => { const items = normalizeFontItems([ { id: "default", label: "自动多语言字体", family: "自动多语言字体", style: "自动回退", source: "automatic" }, { id: "user:abc:0", label: "测试宋体", family: "测试宋体", style: "Regular", source: "imported" }, { id: "system:def", label: "Noto Sans — Bold", family: "Noto Sans", style: "Bold", source: "system" }, { id: "system:def", label: "重复", family: "重复", style: "Bold", source: "system" }, { id: "bad", source: "unknown" }, ]); assert.deepEqual(items.map((item) => item.id), ["default", "user:abc:0", "system:def"]); assert.deepEqual(groupFontItems(items).map((group) => group.label), ["收藏字体", "自动字体", "已导入字体", "系统字体"]); assert.deepEqual(filterFontItems(items, "宋体").map((item) => item.id), ["user:abc:0"]); assert.deepEqual(filterFontItems(items, "bold").map((item) => item.id), ["system:def"]); assert.equal(filterFontItems(items, "没有").length, 0); }); test("font picker implements commit-only searchable combobox keyboard behavior", async () => { const source = await readFile(path.join(root, "app", "static", "font-picker.js"), "utf8"); assert.match(source, /setAttribute\(\"role\", \"option\"\)/); assert.match(source, /ArrowDown/); assert.match(source, /ArrowUp/); assert.match(source, /Home/); assert.match(source, /End/); assert.match(source, /Enter/); assert.match(source, /Escape/); assert.match(source, /onCommit\?\.\(item\)/); assert.doesNotMatch(source, /onCommit\?\.\([^)]*query/); }); test("favorites duplicate the original font faces and preserve catalog order", () => { const items = normalizeFontItems([ { id: "default", source: "automatic" }, { id: "bold", family: "Same", style: "Bold", source: "system" }, { id: "italic", family: "Same", style: "Italic", source: "system" }, ]); const groups = groupFontItems(items, ["italic", "bold", "missing"]); assert.deepEqual(groups[0].items.map(item => item.id), ["bold", "italic"]); assert.deepEqual(groups[2].items.map(item => item.id), ["bold", "italic"]); assert.equal(groupFontItems(items)[0].items.length, 0); assert.deepEqual(groupFontItems(filterFontItems(items, "Italic"), ["italic"])[0].items.map(item => item.id), ["italic"]); });