Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(mcp-host): test the font fallback without trusting it, and run it on the image's fonts
marcoteb1bbc27399d23
diff --git a/.github/workflows/ci-public.yml b/.github/workflows/ci-public.ymlindex 886372784..83d680bb0 100644--- a/.github/workflows/ci-public.yml+++ b/.github/workflows/ci-public.yml@@ -113,6 +113,14 @@ jobs:working-directory: ${{ matrix.service }}run: npm ci+ # The mcp-host images ship these faces, and the tests that set Arabic,+ # Hebrew and Han text in a PDF fail in CI instead of skipping without them.+ - name: Install the mcp-host image fonts+ if: matrix.service == 'mcp-host'+ run: |+ sudo apt-get update+ sudo apt-get install -y --no-install-recommends fonts-dejavu-core fonts-noto-core fonts-noto-cjk+- name: Verify Electron runtimeif: matrix.service == 'desktop-app'working-directory: desktop-appdiff --git a/mcp-host/src/workflow/__tests__/fonts.ready.test.ts b/mcp-host/src/workflow/__tests__/fonts.ready.test.tsnew file mode 100644index 000000000..aa31b07fb--- /dev/null+++ b/mcp-host/src/workflow/__tests__/fonts.ready.test.ts@@ -0,0 +1,35 @@+/**+ * Font registration on an image whose bundled faces cannot be read: the failure+ * is reported once, and the system faces are still registered for charts.+ */+import { afterEach, describe, expect, it, vi } from 'vitest'+import { GlobalFonts } from '@napi-rs/canvas'++afterEach(() => {+ vi.restoreAllMocks()+})++describe('ensureFontsReady', () => {+ it('reports bundled faces it cannot register and still registers the system faces', async () => {+ vi.resetModules()+ const { logger } = await import('../../logger')+ const error = vi.spyOn(logger, 'error').mockImplementation(() => undefined)+ vi.spyOn(GlobalFonts, 'register').mockImplementationOnce(() => {+ throw new Error('unreadable face')+ })+ const system = vi.spyOn(GlobalFonts, 'registerFromPath')+ const fonts = await import('../fonts')++ fonts.ensureFontsReady()+ expect(error).toHaveBeenCalledTimes(1)+ expect(error).toHaveBeenCalledWith(+ expect.objectContaining({ err: 'unreadable face' }),+ 'Bundled Roboto faces could not be loaded'+ )+ // The system faces (macOS /System/Library/Fonts, Linux /usr/share/fonts) still register.+ expect(system).toHaveBeenCalled()++ fonts.ensureFontsReady()+ expect(error).toHaveBeenCalledTimes(1)+ })+})diff --git a/mcp-host/src/workflow/__tests__/fonts.test.ts b/mcp-host/src/workflow/__tests__/fonts.test.tsindex dfa987a9f..a09600023 100644--- a/mcp-host/src/workflow/__tests__/fonts.test.ts+++ b/mcp-host/src/workflow/__tests__/fonts.test.ts@@ -106,6 +106,15 @@ describe('sanitizeForFont', () => {expect(out === 'cost A → B' || out === 'cost A -> B').toBe(true)})+ it('tells a character no face draws from one the bundled face draws', () => {+ // U+0378 is unassigned, so no face on any image maps it; Roboto, which+ // ships with the package, draws é. Neither depends on canRender's answer.+ expect(canRender('\u0378')).toBe(false)+ expect(canRender('é')).toBe(true)+ expect(sanitizeForFont('x\u0378y')).toBe('xy')+ expect(sanitizeForFont('café\u0378')).toBe('café')+ })+it('drops zero-width characters that would confuse measurement', () => {expect(sanitizeForFont('a\u200Bb\uFEFFc')).toBe('abc')})@@ -176,8 +185,11 @@ describe('per-character fallback for PDFs', () => {const family = glyphs.familyFor(0x76f4, PDF_FONT_FAMILY)const faces = family ? glyphs.descriptors([family])[family] : undefinedconst face = faces?.normal- // Only an image with the Noto CJK collection can run this.- if (!Array.isArray(face) || !/CJKsc-/.test(face[1])) return+ // Only an image with the Noto CJK collection can run this; CI installs it.+ if (!Array.isArray(face) || !/CJKsc-/.test(face[1])) {+ expect(process.env.CI, 'CI installs the Noto CJK collection').not.toBe('true')+ return+ }expect((glyphs.descriptors([family!], 'ja')[family!].normal as string[])[1]).toMatch(/CJKjp-/)expect((glyphs.descriptors([family!], 'ko')[family!].normal as string[])[1]).toMatch(/CJKkr-/)@@ -208,7 +220,9 @@ describe('per-character fallback for PDFs', () => {})// Only images that ship Arabic and Hebrew faces can run this; elsewhere the-// typesetter tests cover the ordering with made-up faces.+// typesetter tests cover the ordering with made-up faces. CI installs the+// image's font packages, so there a missing face fails instead of skipping.+const IN_CI = process.env.CI === 'true'const hasRtlFaces =pdfGlyphSource().familyFor(0x0645, PDF_FONT_FAMILY) !== undefined &&pdfGlyphSource().familyFor(0x05e9, PDF_FONT_FAMILY) !== undefined@@ -238,8 +252,9 @@ async function embeddedFaces(family: string, sample: string, dir: string): Promireturn [...new Set(pages[0].fragments.map(f => untagged(f.font)))]}-describe.skipIf(!hasRtlFaces)('Arabic and Hebrew in a PDF', () => {+describe.skipIf(!hasRtlFaces && !IN_CI)('Arabic and Hebrew in a PDF', () => {it('sets each script in the face chosen for it, in display order, leaving nothing out', async () => {+ expect(hasRtlFaces, 'CI installs faces for Arabic and Hebrew').toBe(true)// Which faces those are depends on the image: script faces where Noto is// installed, a broad face such as DejaVu Sans or the body face elsewhere.const outputDir = fs.mkdtempSync(path.join(os.tmpdir(), 'clerum-rtl-'))diff --git a/mcp-host/src/workflow/fonts.ts b/mcp-host/src/workflow/fonts.tsindex 29e8229a8..34114a2a4 100644--- a/mcp-host/src/workflow/fonts.ts+++ b/mcp-host/src/workflow/fonts.ts@@ -15,6 +15,7 @@import { GlobalFonts, type SKRSContext2D, createCanvas } from '@napi-rs/canvas'import * as fs from 'fs'import * as path from 'path'+import { logger } from '../logger'/** Family the bundled Roboto is registered under. */export const CHART_FONT_FAMILY = 'Clerum Sans'@@ -101,8 +102,9 @@ function registerSystemFonts(): void {for (const file of files) {try {GlobalFonts.registerFromPath(file)- } catch {- // A face the renderer cannot parse is simply not available.+ } catch (err) {+ // A face the renderer cannot parse is not available; the image's package may be damaged.+ logger.warn({ file, err: errorText(err) }, 'Font face could not be registered')}}}@@ -115,12 +117,21 @@ function registerSystemFonts(): void {export function ensureFontsReady(): void {if (ready) returnready = true- const f = loadRobotoFaces()- GlobalFonts.register(f.normal, CHART_FONT_FAMILY)- GlobalFonts.register(f.bold, `${CHART_FONT_FAMILY} Bold`)+ try {+ const f = loadRobotoFaces()+ GlobalFonts.register(f.normal, CHART_FONT_FAMILY)+ GlobalFonts.register(f.bold, `${CHART_FONT_FAMILY} Bold`)+ } catch (err) {+ // The system faces still register, so charts draw in them rather than in nothing.+ logger.error({ err: errorText(err) }, 'Bundled Roboto faces could not be loaded')+ }registerSystemFonts()}+function errorText(err: unknown): string {+ return err instanceof Error ? err.message : String(err)+}+/*** Families a PDF can be built from, widest coverage first.*@@ -651,8 +662,12 @@ function resolvePdfFaces(): PdfFaces {if (typeof file === 'string') GlobalFonts.registerFromPath(file, PDF_FONT_FAMILY)pdfFaces = chosenreturn pdfFaces- } catch {- // Unparseable face: fall through to the bundled one.+ } catch (err) {+ // Unparseable face: fall through to the bundled one, which covers less.+ logger.warn(+ { file, err: errorText(err) },+ 'PDF body face could not be registered; the bundled Roboto face is used'+ )}}const roboto = loadRobotoFaces()@@ -960,8 +975,12 @@ class GlyphSource implements PdfGlyphSource {try {if (Buffer.isBuffer(face)) GlobalFonts.register(face, alias)else GlobalFonts.registerFromPath(faceFile(face)!, alias)- } catch {+ } catch (err) {// Measured with the canvas default instead; only line breaking is approximate.+ logger.warn(+ { alias, err: errorText(err) },+ 'Font face could not be registered for measuring'+ )}}// Words repeat, and a width never changes for a face, size and text.@@ -1057,8 +1076,10 @@ export function canRender(ch: string): boolean {ensureFontsReady()missingSignature ??= renderSignature(MISSING_PROBE, CHART_FONT_STACK)ok = renderSignature(ch, CHART_FONT_STACK) !== missingSignature- } catch {- ok = false+ } catch (err) {+ // Not remembered: a failed render says nothing about the character.+ logger.warn({ codePoint: cp, err: errorText(err) }, 'Glyph coverage could not be checked')+ return false}coverage.set(cp, ok)return ok