import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { mkdtempSync, mkdirSync, copyFileSync, writeFileSync, rmSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import zlib from "node:zlib"; import test from "node:test"; const here = path.dirname(fileURLToPath(import.meta.url)); const sourceRoot = path.resolve(here, ".."); const helper = path.join(here, "get-nerdfont-codepoint.js"); const bundledDb = path.join(sourceRoot, "references", "nerdfont-icons.txt.br"); function fixture(rows) { return zlib.brotliCompressSync(Buffer.from(`${rows.join("\n")}\n`)); } function sandbox(db, fetchMode = "fail") { const root = mkdtempSync(path.join(os.tmpdir(), "nerdfont-icons-test-")); const scripts = path.join(root, "scripts"); const references = path.join(root, "references"); mkdirSync(scripts); mkdirSync(references); copyFileSync(helper, path.join(scripts, path.basename(helper))); if (db) writeFileSync(path.join(references, "nerdfont-icons.txt.br"), db); const preload = path.join(root, "fetch.mjs"); writeFileSync(preload, ` const mode = ${JSON.stringify(fetchMode)}; globalThis.fetch = async () => { if (mode === "fail") throw new Error("unexpected fetch"); if (mode === "http-error") return { ok: false, status: 503, statusText: "Unavailable" }; return { ok: true, text: async () => '' }; }; `); return { root, run(...args) { return spawnSync(process.execPath, ["--import", preload, path.join(scripts, path.basename(helper)), ...args], { encoding: "utf8" }); }, close() { rmSync(root, { recursive: true, force: true }); }, }; } function withSandbox(db, mode, fn) { const box = sandbox(db, mode); try { fn(box); } finally { box.close(); } } test("renders bundled non-BMP icon, with glyph and JSON formats", () => { const box = sandbox(null); try { copyFileSync(bundledDb, path.join(box.root, "references", "nerdfont-icons.txt.br")); const all = box.run("nf-md-home", "all"); assert.equal(all.status, 0); assert.match(all.stdout, /^nf-md-home\tf02dc\tU\+F02DC\t/); const json = box.run("nf-md-home", "json"); assert.equal(JSON.parse(json.stdout).glyph, String.fromCodePoint(0xf02dc)); } finally { box.close(); } }); test("formats BMP escapes and non-BMP Java surrogate pairs from isolated fixtures", () => { withSandbox(fixture(["nf-test-bmp\teb99", "nf-test-astral\t1f600"]), "fail", (box) => { const bmp = box.run("nf-test-bmp", "java"); assert.equal(bmp.status, 0); assert.equal(bmp.stdout.trim(), "\\uEB99"); const result = box.run("nf-test-astral", "java"); assert.equal(result.status, 0); assert.equal(result.stdout.trim(), "\\uD83D\\uDE00"); }); }); test("reports ambiguous matches without choosing", () => { withSandbox(fixture(["nf-md-alpha\tf0001", "nf-fa-alpha\tf0002"]), "fail", (box) => { const result = box.run("alpha", "hex"); assert.equal(result.status, 0); assert.equal(result.stdout.trim(), "f0001\nf0002"); }); }); test("usage and invalid format exit through usage boundary", () => { withSandbox(fixture(["nf-md-home\tf02dc"]), "fail", (box) => { assert.equal(box.run().status, 2); const invalid = box.run("home", "unknown"); assert.equal(invalid.status, 2); assert.match(invalid.stderr, /usage:/); }); }); test("no-match refresh retries using fetched data", () => { withSandbox(fixture([]), "success", (box) => { const result = box.run("nf-md-fresh", "hex"); assert.equal(result.status, 0); assert.equal(result.stdout.trim(), "f0123"); }); }); test("refresh HTTP errors are reported without escaping CLI boundary", () => { withSandbox(null, "http-error", (box) => { const result = box.run("missing", "hex"); assert.equal(result.status, 1); assert.match(result.stderr, /fetch failed: 503 Unavailable/); }); });