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

appearancePreferences.test.ts

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