96 lines
2.8 KiB
JavaScript
96 lines
2.8 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { describe, it } from "node:test";
|
|
import {
|
|
bandForScore,
|
|
buildSubmitPayload,
|
|
detectFontPackagesFromList,
|
|
scoreLanguageSignal,
|
|
scoreTimezoneSignal,
|
|
} from "../src/scoring.mjs";
|
|
|
|
describe("scoring", () => {
|
|
it("maps score bands", () => {
|
|
assert.equal(bandForScore(0), "low");
|
|
assert.equal(bandForScore(30), "low");
|
|
assert.equal(bandForScore(31), "medium");
|
|
assert.equal(bandForScore(60), "medium");
|
|
assert.equal(bandForScore(61), "high");
|
|
});
|
|
|
|
it("scores China timezone signals", () => {
|
|
assert.deepEqual(scoreTimezoneSignal("Asia/Shanghai"), {
|
|
raw: "Asia/Shanghai",
|
|
score: 1,
|
|
});
|
|
assert.equal(scoreTimezoneSignal("Asia/Hong_Kong").score, 0.6);
|
|
assert.equal(scoreTimezoneSignal("America/Los_Angeles").score, 0);
|
|
});
|
|
|
|
it("scores language preference order", () => {
|
|
assert.equal(scoreLanguageSignal(["zh-CN", "en-US"]).score, 1);
|
|
assert.equal(scoreLanguageSignal(["en-US", "zh-Hans-CN"]).score, 0.7);
|
|
assert.equal(scoreLanguageSignal(["zh-TW", "en-US"]).score, 0.5);
|
|
assert.equal(scoreLanguageSignal(["en-US"]).score, 0);
|
|
});
|
|
|
|
it("summarizes installed font packages by category", () => {
|
|
const summary = detectFontPackagesFromList([
|
|
"Microsoft YaHei",
|
|
"PingFang SC",
|
|
"JetBrains Mono",
|
|
"Fira Code",
|
|
"Aptos",
|
|
"Adobe Garamond Pro",
|
|
"Noto Sans",
|
|
]);
|
|
|
|
assert.equal(summary.groups.find((group) => group.id === "cjk")?.count, 2);
|
|
assert.equal(summary.groups.find((group) => group.id === "developer")?.count, 2);
|
|
assert.equal(summary.groups.find((group) => group.id === "office")?.count, 1);
|
|
assert.equal(summary.groups.find((group) => group.id === "design")?.count, 1);
|
|
assert.equal(summary.groups.find((group) => group.id === "linux")?.count, 1);
|
|
assert.ok(summary.score > 0.55);
|
|
assert.match(summary.raw, /中文\/CJK=2/);
|
|
assert.match(summary.raw, /开发字体=2/);
|
|
assert.match(summary.raw, /sample=/);
|
|
});
|
|
|
|
it("builds a compact anonymous payload", () => {
|
|
const payload = buildSubmitPayload({
|
|
score: 62,
|
|
band: "high",
|
|
locale: "zh-CN",
|
|
timezone: "Asia/Shanghai",
|
|
timezoneOffsetMinutes: -480,
|
|
userAgentFamily: "Apple",
|
|
signals: [
|
|
{
|
|
id: "timezone",
|
|
weight: 30,
|
|
raw: "Asia/Shanghai",
|
|
score: 1,
|
|
contribution: 30,
|
|
},
|
|
],
|
|
});
|
|
|
|
assert.deepEqual(payload, {
|
|
score: 62,
|
|
band: "high",
|
|
locale: "zh-CN",
|
|
timezone: "Asia/Shanghai",
|
|
timezoneOffsetMinutes: -480,
|
|
userAgentFamily: "Apple",
|
|
signals: [
|
|
{
|
|
id: "timezone",
|
|
weight: 30,
|
|
raw: "Asia/Shanghai",
|
|
score: 1,
|
|
contribution: 30,
|
|
},
|
|
],
|
|
});
|
|
});
|
|
});
|