import { describe, expect, it } from 'vitest' import { FALLBACK_TERMINAL_FONT, parseMonospaceFont } from '../src/main/desktop' describe('parseMonospaceFont', () => { it('splits a family from its size', () => { expect(parseMonospaceFont("'Ubuntu Mono 13'")).toEqual({ family: '"Ubuntu Mono"', sizePx: 17.33, }) }) it('converts points to pixels', () => { // Pango reports points and CSS wants pixels: 1pt = 1/72in, 1px = 1/96in. // Getting this wrong makes the terminal a third too small, which reads as // "the font is wrong" rather than "the size is wrong". expect(parseMonospaceFont("'Monospace 12'")?.sizePx).toBe(16) expect(parseMonospaceFont("'Monospace 9'")?.sizePx).toBe(12) expect(parseMonospaceFont("'Monospace 10.5'")?.sizePx).toBe(14) }) it('keeps multi-word families intact', () => { // The failure this guards against is the family being read as a font stack: // "Noto Sans Mono" is one family, not three. expect(parseMonospaceFont("'Noto Sans Mono 12'")?.family).toBe('"Noto Sans Mono"') }) it('treats the trailing number as the size, not the first one', () => { // Families legitimately contain digits. expect(parseMonospaceFont("'Fira Code 2 12'")).toEqual({ family: '"Fira Code 2"', sizePx: 16, }) }) it('accepts an unquoted description', () => { expect(parseMonospaceFont('Ubuntu Mono 13')).toEqual({ family: '"Ubuntu Mono"', sizePx: 17.33, }) }) it('accepts a bare family name, sizing it with the default', () => { // Some desktops report only the family. Dropping it would be worse than // using a sensible size. expect(parseMonospaceFont("'Ubuntu Mono'")).toEqual({ family: '"Ubuntu Mono"', sizePx: 13, }) }) it('strips quotes that would otherwise end up inside the CSS value', () => { expect(parseMonospaceFont(`"Ubuntu Mono 13"`)?.family).toBe('"Ubuntu Mono"') }) it('rejects nonsense rather than producing a broken font string', () => { for (const bad of ['', ' ', "''", '0', 'DejaVu 0', 'DejaVu -3']) { const parsed = parseMonospaceFont(bad) // Either null, or a usable family — never a zero or negative size, which // xterm would take and render as nothing. if (parsed) expect(parsed.sizePx).toBeGreaterThan(0) } expect(parseMonospaceFont('')).toBeNull() expect(parseMonospaceFont(' ')).toBeNull() }) it('never returns an empty family', () => { for (const input of ["'Ubuntu Mono 13'", 'Ubuntu Mono 13', "'X'", 'X 12']) { expect(parseMonospaceFont(input)?.family.length ?? 0).toBeGreaterThan(0) } }) }) describe('FALLBACK_TERMINAL_FONT', () => { it('asks the engine for the system monospace face', () => { // The same intent as the GSettings lookup, resolved one layer down — so a // desktop that will not answer still gets a sensible font. expect(FALLBACK_TERMINAL_FONT.family).toContain('ui-monospace') expect(FALLBACK_TERMINAL_FONT.family).toContain('monospace') expect(FALLBACK_TERMINAL_FONT.sizePx).toBeGreaterThan(0) }) })