Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

appearancePreferences.test.ts

137 lines4.9 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 DEFAULT_BASE_FONT_SIZE,
5 deriveCodeFontSize,
6 deriveTerminalFontSize,
7 normalizeBaseFontSize,
8 normalizeCodeFontSize,
9 normalizeCodeWordBreak,
10 resolveAppearance,
11 resolveAppearancePreferences,
12 resolveMarkdownFontSizes,
13 resolveMobileCodeSurface,
14 resolveNativeMarkdownTypography,
15 resolveTextScaleVariables,
16 stepTerminalFontSize,
17} from "./appearancePreferences";
18​
19describe("appearancePreferences", () => {
20 it("resolves defaults for empty stored preferences", () => {
21 expect(resolveAppearancePreferences({})).toEqual({
22 baseFontSize: DEFAULT_BASE_FONT_SIZE,
23 terminalFontSize: null,
24 codeFontSize: null,
25 codeWordBreak: false,
26 });
27 });
28​
29 it("migrates the legacy markdownFontSize key to baseFontSize", () => {
30 expect(resolveAppearancePreferences({ markdownFontSize: 18 }).baseFontSize).toBe(18);
31 expect(
32 resolveAppearancePreferences({ baseFontSize: 16, markdownFontSize: 18 }).baseFontSize,
33 ).toBe(16);
34 });
35​
36 it("keeps explicit overrides and treats missing values as automatic", () => {
37 const preferences = resolveAppearancePreferences({ terminalFontSize: 12, codeFontSize: 14 });
38 expect(preferences.terminalFontSize).toBe(12);
39 expect(preferences.codeFontSize).toBe(14);
40 expect(resolveAppearancePreferences({ terminalFontSize: null }).terminalFontSize).toBe(null);
41 });
42​
43 it("derives terminal and code sizes from the base size when not overridden", () => {
44 const appearance = resolveAppearance(resolveAppearancePreferences({ baseFontSize: 15 }));
45 expect(appearance.terminalFontSize).toBe(10);
46 expect(appearance.codeFontSize).toBe(11);
47 expect(appearance.isTerminalFontSizeCustom).toBe(false);
48 expect(appearance.isCodeFontSizeCustom).toBe(false);
49​
50 const scaled = resolveAppearance(resolveAppearancePreferences({ baseFontSize: 22 }));
51 expect(scaled.terminalFontSize).toBe(deriveTerminalFontSize(22));
52 expect(scaled.codeFontSize).toBe(deriveCodeFontSize(22));
53 expect(scaled.terminalFontSize).toBeGreaterThan(10);
54 expect(scaled.codeFontSize).toBeGreaterThan(11);
55 });
56​
57 it("applies explicit overrides over derived values", () => {
58 const appearance = resolveAppearance(
59 resolveAppearancePreferences({ baseFontSize: 22, terminalFontSize: 8, codeFontSize: 9 }),
60 );
61 expect(appearance.terminalFontSize).toBe(8);
62 expect(appearance.codeFontSize).toBe(9);
63 expect(appearance.isTerminalFontSizeCustom).toBe(true);
64 expect(appearance.isCodeFontSizeCustom).toBe(true);
65 });
66​
67 it("clamps base and code font sizes", () => {
68 expect(normalizeBaseFontSize(4)).toBe(11);
69 expect(normalizeBaseFontSize(30)).toBe(22);
70 expect(normalizeCodeFontSize(4)).toBe(8);
71 expect(normalizeCodeFontSize(30)).toBe(18);
72 });
73​
74 it("steps terminal font size within bounds", () => {
75 expect(stepTerminalFontSize(6, -1)).toBe(6);
76 });
77​
78 it("scales markdown typography from the base size", () => {
79 expect(resolveMarkdownFontSizes(15)).toMatchObject({
80 m: 15,
81 h1: 20,
82 bodyLineHeight: 22,
83 codeBlockFontSize: 12,
84 codeBlockLineHeight: 18,
85 });
86 });
87​
88 it("scales code surface geometry from the code font size", () => {
89 expect(resolveMobileCodeSurface(11)).toMatchObject({
90 fontSize: 11,
91 rowHeight: 20,
92 });
93 });
94​
95 it("defaults code word break to false", () => {
96 expect(normalizeCodeWordBreak(undefined)).toBe(false);
97 expect(normalizeCodeWordBreak(true)).toBe(true);
98 });
99​
100 it("returns the authored text scale at the 16pt default", () => {
101 expect(DEFAULT_BASE_FONT_SIZE).toBe(16);
102​
103 const variables = resolveTextScaleVariables(DEFAULT_BASE_FONT_SIZE);
104 expect(variables["--text-base"]).toBe(16);
105 expect(variables["--text-base--line-height"]).toBe(23);
106 expect(variables["--text-sm"]).toBe(14);
107 expect(variables["--text-sm--line-height"]).toBe(19);
108 expect(variables["--text-lg"]).toBe(18);
109 expect(variables["--text-3xl"]).toBe(30);
110 });
111​
112 it("scales every text variable proportionally with the base size", () => {
113 const smallerVariables = resolveTextScaleVariables(15);
114 expect(smallerVariables["--text-base"]).toBe(15);
115 expect(smallerVariables["--text-sm"]).toBe(13);
116​
117 const variables = resolveTextScaleVariables(20);
118 expect(variables["--text-base"]).toBe(20);
119 expect(variables["--text-base--line-height"]).toBe(29);
120 expect(variables["--text-sm"]).toBe(18);
121 expect(variables["--text-xs"]).toBe(16);
122 expect(variables["--text-lg"]).toBe(23);
123​
124 const smaller = resolveTextScaleVariables(11);
125 expect(smaller["--text-base"]).toBe(11);
126 expect(smaller["--text-3xs"]).toBeGreaterThanOrEqual(8);
127 expect(smaller["--text-3xs--line-height"]).toBeGreaterThanOrEqual(10);
128 });
129​
130 it("derives native markdown typography from the base size", () => {
131 expect(resolveNativeMarkdownTypography(22)).toEqual({
132 fontSize: 22,
133 lineHeight: 32,
134 headingFontSizes: [29, 26, 23, 21, 21, 21],
135 });
136 });
137});