import { test } from "node:test"; import assert from "node:assert/strict"; import { createHash } from "node:crypto"; import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { WORDMARK_METRICS } from "./wordmarkMetrics"; import { wordmarkWidthEm } from "./wordmarkWidth"; // Run with: pnpm --filter yt-dlp-transcript-common test const FONT = path.resolve( path.dirname(fileURLToPath(import.meta.url)), "..", "..", "umtool", "report-to-video", "fonts", WORDMARK_METRICS.font, ); test("the table was generated from the vendored Archivo", () => { const sha = createHash("sha256").update(fs.readFileSync(FONT)).digest("hex"); assert.equal(WORDMARK_METRICS.sha256, sha, "re-run common/bin/gen-wordmark-metrics.py"); const size = WORDMARK_METRICS.ranges.reduce((n, [a, b]) => n + b - a + 1, 0); assert.equal(WORDMARK_METRICS.advances[720].length, size); assert.equal(WORDMARK_METRICS.advances[380].length, size); }); test("the lead is set heavier, so wider, than the same letters as suffix", () => { const heavy = wordmarkWidthEm("Wwww", "Wwww"); const light = wordmarkWidthEm("xWwww", "x") - wordmarkWidthEm("x", "x"); assert.ok(heavy > light, `${heavy} vs ${light}`); }); test("longer titles are wider; tracking narrows by its value per character", () => { assert.ok(wordmarkWidthEm("Rekietalyzer", "Rekieta") > wordmarkWidthEm("Jeralyzer", "Jer")); const plain = wordmarkWidthEm("Archilyzer", "Archi"); const tracked = wordmarkWidthEm("Archilyzer", "Archi", -0.01); assert.ok(Math.abs(plain - tracked - 0.1 * 1.03) < 0.002, `${plain} → ${tracked}`); }); test("a character the table does not hold counts wide, not zero", () => { const latin = wordmarkWidthEm("Ab", "Ab"); assert.ok(wordmarkWidthEm("Ab中", "Ab") - latin > 1.2); });