-
Notifications
You must be signed in to change notification settings - Fork 962
fix(desktop): recover terminal from non-monospace font crash (#3513) #3554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4d23df4
fix(desktop): recover terminal from non-monospace font crash (#3513)
Kitenite a0c584d
fix(desktop): reject all-proportional generic terminal font stacks
Kitenite b2e6a04
refactor(desktop): append ", monospace" fallback + cleaner font preview
Kitenite 5b9936d
refactor(desktop): show Nerd Fonts in the editor picker too
Kitenite 8404892
fix(desktop): validate the actual CSS primary font, not the first con…
Kitenite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
apps/desktop/src/renderer/lib/terminal/appearance/appearance.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import { afterEach, describe, expect, mock, test } from "bun:test"; | ||
| import { | ||
| DEFAULT_TERMINAL_FONT_FAMILY, | ||
| sanitizeTerminalFontFamily, | ||
| } from "./index"; | ||
|
|
||
| type MeasureFn = (text: string) => { width: number }; | ||
|
|
||
| /** | ||
| * Stub `document.createElement("canvas")` so `getContext("2d").measureText` | ||
| * returns widths from `measureForFont`. Non-canvas tags defer to the | ||
| * existing test-setup stub. | ||
| */ | ||
| function stubCanvas(measureForFont: (font: string) => MeasureFn) { | ||
| const originalCreate = document.createElement; | ||
| // biome-ignore lint/suspicious/noExplicitAny: bun:test `mock` wraps arbitrary fns | ||
| (document as any).createElement = mock((tag: string) => { | ||
| if (tag !== "canvas") { | ||
| // biome-ignore lint/suspicious/noExplicitAny: delegating stub accepts any tag | ||
| return (originalCreate as any).call(document, tag); | ||
| } | ||
| let currentFont = ""; | ||
| return { | ||
| getContext: (kind: string) => { | ||
| if (kind !== "2d") return null; | ||
| return { | ||
| set font(value: string) { | ||
| currentFont = value; | ||
| }, | ||
| get font() { | ||
| return currentFont; | ||
| }, | ||
| measureText: (text: string) => measureForFont(currentFont)(text), | ||
| }; | ||
| }, | ||
| }; | ||
| }); | ||
| return () => { | ||
| // biome-ignore lint/suspicious/noExplicitAny: restoring stubbed method | ||
| (document as any).createElement = originalCreate; | ||
| }; | ||
| } | ||
|
|
||
| const equalWidths: MeasureFn = (text) => ({ width: text.length * 10 }); | ||
| const proportionalWidths: MeasureFn = (text) => { | ||
| let width = 0; | ||
| for (const ch of text) width += ch === "M" ? 16 : 6; | ||
| return { width }; | ||
| }; | ||
|
|
||
| describe("sanitizeTerminalFontFamily", () => { | ||
| let restore: (() => void) | null = null; | ||
|
|
||
| afterEach(() => { | ||
| restore?.(); | ||
| restore = null; | ||
| }); | ||
|
|
||
| test("returns default for null / empty / whitespace", () => { | ||
| expect(sanitizeTerminalFontFamily(null)).toBe(DEFAULT_TERMINAL_FONT_FAMILY); | ||
| expect(sanitizeTerminalFontFamily(undefined)).toBe( | ||
| DEFAULT_TERMINAL_FONT_FAMILY, | ||
| ); | ||
| expect(sanitizeTerminalFontFamily("")).toBe(DEFAULT_TERMINAL_FONT_FAMILY); | ||
| expect(sanitizeTerminalFontFamily(" ")).toBe( | ||
| DEFAULT_TERMINAL_FONT_FAMILY, | ||
| ); | ||
| }); | ||
|
|
||
| test("trusts all-generic monospace values without canvas", () => { | ||
| expect(sanitizeTerminalFontFamily("monospace")).toBe("monospace"); | ||
| expect(sanitizeTerminalFontFamily("ui-monospace")).toBe("ui-monospace"); | ||
| }); | ||
|
|
||
| test("falls back when the primary family is a proportional generic", () => { | ||
| expect(sanitizeTerminalFontFamily("sans-serif")).toBe( | ||
| DEFAULT_TERMINAL_FONT_FAMILY, | ||
| ); | ||
| expect(sanitizeTerminalFontFamily("serif")).toBe( | ||
| DEFAULT_TERMINAL_FONT_FAMILY, | ||
| ); | ||
| expect(sanitizeTerminalFontFamily("cursive")).toBe( | ||
| DEFAULT_TERMINAL_FONT_FAMILY, | ||
| ); | ||
| // CSS resolves the first generic, so a later monospace entry never wins. | ||
| expect(sanitizeTerminalFontFamily("cursive, monospace")).toBe( | ||
| DEFAULT_TERMINAL_FONT_FAMILY, | ||
| ); | ||
| }); | ||
|
|
||
| test("passes through a stack whose primary generic is monospace", () => { | ||
| // The browser resolves the first generic, so "monospace, sans-serif" | ||
| // actually renders as monospace — safe. | ||
| expect(sanitizeTerminalFontFamily("monospace, sans-serif")).toBe( | ||
| "monospace, sans-serif", | ||
| ); | ||
| }); | ||
|
|
||
| test("falls back when a concrete mono follows a proportional generic", () => { | ||
| // Regression: earlier logic picked the first non-generic as the primary, | ||
| // letting `sans-serif, "JetBrains Mono"` slip through even though CSS | ||
| // renders sans-serif. Validate the actual CSS primary instead. | ||
| expect(sanitizeTerminalFontFamily('sans-serif, "JetBrains Mono"')).toBe( | ||
| DEFAULT_TERMINAL_FONT_FAMILY, | ||
| ); | ||
| }); | ||
|
|
||
| test("passes a monospace font through when the stack already ends with monospace", () => { | ||
| restore = stubCanvas(() => equalWidths); | ||
| expect(sanitizeTerminalFontFamily('"JetBrains Mono", monospace')).toBe( | ||
| '"JetBrains Mono", monospace', | ||
| ); | ||
| }); | ||
|
|
||
| test("appends a monospace fallback when the stack lacks one", () => { | ||
| // If the primary isn't installed, the browser otherwise falls back to a | ||
| // proportional default — appending "monospace" forces OS monospace. | ||
| restore = stubCanvas(() => equalWidths); | ||
| expect(sanitizeTerminalFontFamily('"JetBrains Mono"')).toBe( | ||
| '"JetBrains Mono", monospace', | ||
| ); | ||
| expect(sanitizeTerminalFontFamily("Menlo")).toBe("Menlo, monospace"); | ||
| }); | ||
|
|
||
| test("falls back to default for a proportional primary family (quoted)", () => { | ||
| restore = stubCanvas(() => proportionalWidths); | ||
| expect(sanitizeTerminalFontFamily('"Inter", sans-serif')).toBe( | ||
| DEFAULT_TERMINAL_FONT_FAMILY, | ||
| ); | ||
| }); | ||
|
|
||
| test("falls back to default for a proportional primary family (bare)", () => { | ||
| restore = stubCanvas(() => proportionalWidths); | ||
| expect(sanitizeTerminalFontFamily("Inter")).toBe( | ||
| DEFAULT_TERMINAL_FONT_FAMILY, | ||
| ); | ||
| }); | ||
|
|
||
| test("trusts the value when canvas measurement throws", () => { | ||
| restore = stubCanvas(() => () => { | ||
| throw new Error("canvas unsupported"); | ||
| }); | ||
| // Use a unique family so the module-level monospace cache doesn't mask | ||
| // the canvas error path. | ||
| expect(sanitizeTerminalFontFamily('"UnmeasurableFont-ABC-123"')).toBe( | ||
| '"UnmeasurableFont-ABC-123", monospace', | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| import { useQuery } from "@tanstack/react-query"; | ||
| import { useMemo } from "react"; | ||
| import { | ||
| DEFAULT_TERMINAL_FONT_FAMILY, | ||
| DEFAULT_TERMINAL_FONT_SIZE, | ||
| getDefaultTerminalAppearance, | ||
| sanitizeTerminalFontFamily, | ||
| type TerminalAppearance, | ||
| } from "renderer/lib/terminal/appearance"; | ||
| import { electronTrpcClient } from "renderer/lib/trpc-client"; | ||
|
|
@@ -21,8 +21,9 @@ export function useTerminalAppearance(): TerminalAppearance { | |
|
|
||
| return useMemo(() => { | ||
| const theme = terminalTheme ?? fallbackTheme; | ||
| const fontFamily = | ||
| fontSettings?.terminalFontFamily || DEFAULT_TERMINAL_FONT_FAMILY; | ||
| const fontFamily = sanitizeTerminalFontFamily( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Sanitization still lets all-generic non-monospace font stacks through, so the terminal can still be launched with a proportional font. Prompt for AI agents |
||
| fontSettings?.terminalFontFamily, | ||
| ); | ||
| const fontSize = | ||
| fontSettings?.terminalFontSize ?? DEFAULT_TERMINAL_FONT_SIZE; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.