|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { runFontLocalize, type FontLocalizeIo } from "./fontLocalize.js"; |
| 3 | + |
| 4 | +function makeIo(input: string): { |
| 5 | + io: FontLocalizeIo; |
| 6 | + output: string[]; |
| 7 | + errors: string[]; |
| 8 | +} { |
| 9 | + const output: string[] = []; |
| 10 | + const errors: string[] = []; |
| 11 | + return { |
| 12 | + io: { |
| 13 | + readInput: async () => input, |
| 14 | + writeOutput: (value) => output.push(value), |
| 15 | + writeError: (value) => errors.push(value), |
| 16 | + }, |
| 17 | + output, |
| 18 | + errors, |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +describe("runFontLocalize", () => { |
| 23 | + it("writes only the localized document to stdout", async () => { |
| 24 | + const harness = makeIo("<html>source</html>"); |
| 25 | + const localize = vi.fn(async () => "<html>localized</html>"); |
| 26 | + |
| 27 | + const exitCode = await runFontLocalize(harness.io, localize); |
| 28 | + |
| 29 | + expect(exitCode).toBe(0); |
| 30 | + expect(localize).toHaveBeenCalledWith("<html>source</html>"); |
| 31 | + expect(harness.output).toEqual(["<html>localized</html>"]); |
| 32 | + expect(harness.errors).toEqual([]); |
| 33 | + }); |
| 34 | + |
| 35 | + it("rejects blank input without calling the resolver", async () => { |
| 36 | + const harness = makeIo(" \n"); |
| 37 | + const localize = vi.fn(async (html: string) => html); |
| 38 | + |
| 39 | + const exitCode = await runFontLocalize(harness.io, localize); |
| 40 | + |
| 41 | + expect(exitCode).toBe(2); |
| 42 | + expect(localize).not.toHaveBeenCalled(); |
| 43 | + expect(harness.output).toEqual([]); |
| 44 | + expect(harness.errors.join(" ")).toContain("input is empty"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("fails without echoing source HTML or resolver details", async () => { |
| 48 | + const source = '<html><img src="https://signed.example/secret"></html>'; |
| 49 | + const harness = makeIo(source); |
| 50 | + const localize = vi.fn(async () => { |
| 51 | + throw new Error(`fetch failed for ${source}`); |
| 52 | + }); |
| 53 | + |
| 54 | + const exitCode = await runFontLocalize(harness.io, localize); |
| 55 | + |
| 56 | + expect(exitCode).toBe(1); |
| 57 | + expect(harness.output).toEqual([]); |
| 58 | + expect(harness.errors.join(" ")).toContain("font localization failed (Error)"); |
| 59 | + expect(harness.errors.join(" ")).not.toContain("signed.example"); |
| 60 | + expect(harness.errors.join(" ")).not.toContain("<html>"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("fails closed when the resolver returns an empty document", async () => { |
| 64 | + const harness = makeIo("<html>source</html>"); |
| 65 | + |
| 66 | + const exitCode = await runFontLocalize(harness.io, async () => "\n"); |
| 67 | + |
| 68 | + expect(exitCode).toBe(1); |
| 69 | + expect(harness.output).toEqual([]); |
| 70 | + expect(harness.errors.join(" ")).toContain("empty output"); |
| 71 | + }); |
| 72 | +}); |
0 commit comments