fix(cli): distinguish producer-absent from injector failure in font localization

Addresses review on #2264: the localization helper had one broad catch around
both dynamic producer resolution and injector execution, so it couldn't tell a
benign 'producer not in this environment' from a real injector/fetch failure,
and emitted no diagnostic. Split into loadFontInjector() (returns null when the
module is absent — silent fail-open) and localizeWithProducer() (warns ONCE per
distinct message when the injector itself throws, then fails open). Per-family
resolution failures remain the injector's own responsibility (producer's
warnUnresolvedFonts). The localizer seam is injectable; tests now cover success,
producer-unavailable, injector-throw, warn dedup, and call-site integration.
This commit is contained in:
Vance Ingalls
2026-07-11 16:39:50 -07:00
parent aa10d49234
commit fc2b905837
2 changed files with 108 additions and 25 deletions
@@ -4,13 +4,18 @@ vi.mock("@hyperframes/core/compiler", () => ({
bundleToSingleHtml: vi.fn(async () => "<html><body>bundled</body></html>"),
}));
import { bundleWithLocalizedFonts } from "./bundleWithLocalizedFonts.js";
import {
__resetFontLocalizationWarningsForTests,
bundleWithLocalizedFonts,
localizeWithProducer,
} from "./bundleWithLocalizedFonts.js";
afterEach(() => {
__resetFontLocalizationWarningsForTests();
vi.clearAllMocks();
});
describe("bundleWithLocalizedFonts", () => {
describe("bundleWithLocalizedFonts (call-site integration)", () => {
it("runs the injected font localizer over the plain bundle", async () => {
const localize = vi.fn(async (html: string) => html.replace("bundled", "bundled+fonts"));
const html = await bundleWithLocalizedFonts("/project", localize);
@@ -19,11 +24,45 @@ describe("bundleWithLocalizedFonts", () => {
expect(html).toBe("<html><body>bundled+fonts</body></html>");
});
it("returns the localizer's output verbatim (localization is the last step)", async () => {
const html = await bundleWithLocalizedFonts(
"/project",
async () => "<html>embedded-face</html>",
);
expect(html).toBe("<html>embedded-face</html>");
it("returns the localizer output verbatim (localization is the last step)", async () => {
const html = await bundleWithLocalizedFonts("/project", async () => "<html>embedded</html>");
expect(html).toBe("<html>embedded</html>");
});
});
describe("localizeWithProducer", () => {
it("embeds fonts when the injector is available", async () => {
const inject = vi.fn(async (html: string) => `${html}<!--fonts-->`);
const warn = vi.fn();
const out = await localizeWithProducer("<html></html>", async () => inject, warn);
expect(out).toBe("<html></html><!--fonts-->");
expect(warn).not.toHaveBeenCalled();
});
it("fails open silently when producer is unavailable (module absent → null)", async () => {
const warn = vi.fn();
const out = await localizeWithProducer("<html>plain</html>", async () => null, warn);
// Never worse than a plain bundle; benign absence is not a warning.
expect(out).toBe("<html>plain</html>");
expect(warn).not.toHaveBeenCalled();
});
it("fails open WITH a diagnostic when the injector itself throws", async () => {
const warn = vi.fn();
const boom: () => Promise<never> = () => Promise.reject(new Error("fetch layer down"));
const out = await localizeWithProducer("<html>plain</html>", async () => boom, warn);
expect(out).toBe("<html>plain</html>");
expect(warn).toHaveBeenCalledOnce();
expect(warn.mock.calls[0]?.[0]).toContain("fetch layer down");
});
it("dedups repeated identical injector failures across re-bundles", async () => {
const warn = vi.fn();
const boom: () => Promise<never> = () => Promise.reject(new Error("same failure"));
for (let i = 0; i < 5; i++) {
await localizeWithProducer("<html></html>", async () => boom, warn);
}
// snapshot/check re-bundle per grid point; the warning must fire once.
expect(warn).toHaveBeenCalledOnce();
});
});