Files
matrix-screen-controller/核桃派软件源代码/tests/test_font_picker.mjs
T

42 lines
2.0 KiB
JavaScript

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/);
});