Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 areFontAdvancesMonospace,
5 clampCodeFontSize,
6 clampInterfaceFontSize,
7 clampPromptFontSize,
8 DEFAULT_CODE_FONT_STACK,
9 DEFAULT_SANS_FONT_STACK,
10 appearanceFontStack,
11 cssFontFamilies,
12 resolveDefaultFamilyLabel,
13 resolveTerminalFontPreference,
14} from "./appearanceFonts";
15
16describe("areFontAdvancesMonospace", () => {
17 it("accepts a fixed advance and rejects any proportional glyph", () => {
18 expect(areFontAdvancesMonospace([10, 10, 10, 10])).toBe(true);
19 expect(areFontAdvancesMonospace([10, 10, 7, 10])).toBe(false);
20 expect(areFontAdvancesMonospace([10, 10.02])).toBe(false);
21 });
22
23 it("fails open when canvas metrics are unavailable", () => {
24 expect(areFontAdvancesMonospace([])).toBe(true);
25 expect(areFontAdvancesMonospace([Number.NaN, Number.NaN])).toBe(true);
26 });
27});
28
29describe("cssFontFamilies", () => {
30 it("returns null for effectively empty input", () => {
31 expect(cssFontFamilies("")).toBeNull();
32 expect(cssFontFamilies(" ")).toBeNull();
33 expect(cssFontFamilies(" , , ")).toBeNull();
34 });
35
36 it("quotes names with spaces and keeps single idents bare", () => {
37 expect(cssFontFamilies("Fira Code")).toBe('"Fira Code"');
38 expect(cssFontFamilies("monospace")).toBe("monospace");
39 expect(cssFontFamilies('"Comic Mono"')).toBe('"Comic Mono"');
40 });
41
42 it("normalizes comma-separated lists and strips embedded quotes", () => {
43 expect(cssFontFamilies(" Fira Code , Menlo ")).toBe('"Fira Code", Menlo');
44 expect(cssFontFamilies('Bad"Name')).toBe('"BadName"');
45 });
46
47 it("quotes names that are not single CSS idents", () => {
48 expect(cssFontFamilies("3270 Nerd Font")).toBe('"3270 Nerd Font"');
49 expect(cssFontFamilies("M+ 1m")).toBe('"M+ 1m"');
50 });
51});
52
53describe("resolveDefaultFamilyLabel", () => {
54 it("skips generic keywords and returns null for a stack of only generics", () => {
55 expect(resolveDefaultFamilyLabel("system-ui, sans-serif")).toBeNull();
56 expect(resolveDefaultFamilyLabel("ui-monospace, monospace")).toBeNull();
57 });
58});
59
60describe("appearanceFontStack", () => {
61 it("prepends the custom family to the default stack", () => {
62 expect(appearanceFontStack("Fira Code", DEFAULT_CODE_FONT_STACK)).toBe(
63 `"Fira Code", ${DEFAULT_CODE_FONT_STACK}`,
64 );
65 });
66
67 it("falls back to the default stack when unset", () => {
68 expect(appearanceFontStack("", DEFAULT_SANS_FONT_STACK)).toBe(DEFAULT_SANS_FONT_STACK);
69 });
70});
71
72describe("resolveTerminalFontPreference", () => {
73 it("inherits the code font in simple mode", () => {
74 expect(
75 resolveTerminalFontPreference({ advanced: false, code: "Fira Code", terminal: "" }),
76 ).toBe("Fira Code");
77 expect(
78 resolveTerminalFontPreference({
79 advanced: false,
80 code: "Fira Code",
81 terminal: "Berkeley Mono",
82 }),
83 ).toBe("Fira Code");
84 });
85
86 it("keeps code and terminal fonts independent in advanced mode", () => {
87 expect(resolveTerminalFontPreference({ advanced: true, code: "Fira Code", terminal: "" })).toBe(
88 "",
89 );
90 expect(
91 resolveTerminalFontPreference({
92 advanced: true,
93 code: "Fira Code",
94 terminal: "Berkeley Mono",
95 }),
96 ).toBe("Berkeley Mono");
97 });
98});
99
100describe("font size clamping", () => {
101 it("keeps sizes inside the ranges the UI can absorb", () => {
102 expect(clampInterfaceFontSize(16)).toBe(16);
103 expect(clampInterfaceFontSize(2)).toBe(12);
104 expect(clampInterfaceFontSize(96)).toBe(20);
105 expect(clampPromptFontSize(40)).toBe(20);
106 expect(clampCodeFontSize(1)).toBe(10);
107 });
108
109 it("rounds fractional values and falls back for unusable input", () => {
110 expect(clampCodeFontSize(13.4)).toBe(13);
111 expect(clampInterfaceFontSize(Number.NaN)).toBe(16);
112 expect(clampPromptFontSize(Number.POSITIVE_INFINITY)).toBe(14);
113 });
114});