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

surface.test.ts

407 lines14.5 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import type { GhosttyCell, GhosttyRow } from "./core";
4import {
5 DEFAULT_TERMINAL_FONT_FAMILY,
6 DEFAULT_TERMINAL_FONT_SIZE,
7 advanceTerminalSelectionClickSequence,
8 ghosttyMouseButton,
9 isTerminalAltGraphText,
10 isTerminalCompositionCommitInput,
11 isTerminalCopyShortcut,
12 isTerminalLinkPointerGesture,
13 isTerminalPasteShortcut,
14 shouldBlinkTerminalCursor,
15 shouldReportTerminalMouse,
16 terminalScrollbarGeometry,
17 terminalScrollbarOffsetAtPointer,
18 terminalLinkAtColumn,
19 terminalLinkAtPosition,
20 terminalContentOriginY,
21 terminalFontFamily,
22 terminalFontSize,
23 terminalWheelArrowData,
24 terminalWheelDeltaRows,
25} from "./surface";
26​
27const cell = (text: string): GhosttyCell => ({
28 text,
29 wide: 0,
30 foreground: { r: 255, g: 255, b: 255 },
31 background: { r: 0, g: 0, b: 0 },
32 bold: false,
33 italic: false,
34 invisible: false,
35 strikethrough: false,
36 overline: false,
37 underline: false,
38 selected: false,
39});
40​
41describe("isTerminalAltGraphText", () => {
42 it("defers printable AltGr output to the textarea input event", () => {
43 expect(
44 isTerminalAltGraphText({
45 key: "@",
46 getModifierState: (modifier) => modifier === "AltGraph",
47 }),
48 ).toBe(true);
49 expect(
50 isTerminalAltGraphText({
51 key: "ArrowRight",
52 getModifierState: (modifier) => modifier === "AltGraph",
53 }),
54 ).toBe(false);
55 });
56});
57​
58describe("shouldBlinkTerminalCursor", () => {
59 const blinking = {
60 focused: true,
61 cursorBlinking: true,
62 cursorVisible: true,
63 reducedMotion: false,
64 };
65​
66 it("blinks a focused visible cursor the terminal asked to blink", () => {
67 expect(shouldBlinkTerminalCursor(blinking)).toBe(true);
68 });
69​
70 it("holds the cursor steady when blinking would be unwanted", () => {
71 // Unfocused surfaces draw a steady hollow cursor, DECSCUSR steady styles and
72 // DEC mode 12 turn blinking off, a hidden cursor has nothing to toggle, and
73 // reduced-motion readers get no permanently animating element.
74 expect(shouldBlinkTerminalCursor({ ...blinking, focused: false })).toBe(false);
75 expect(shouldBlinkTerminalCursor({ ...blinking, cursorBlinking: false })).toBe(false);
76 expect(shouldBlinkTerminalCursor({ ...blinking, cursorVisible: false })).toBe(false);
77 expect(shouldBlinkTerminalCursor({ ...blinking, reducedMotion: true })).toBe(false);
78 });
79});
80​
81describe("terminalLinkAtColumn", () => {
82 it("maps terminal cells to UTF-16 offsets after a wide emoji", () => {
83 const cells = [
84 cell("πŸ™‚"),
85 cell(""),
86 ...Array.from("https://t3.codes", (character) => cell(character)),
87 ];
88 const row: GhosttyRow = {
89 cells,
90 text: cells
91 .map((value) => value.text || " ")
92 .join("")
93 .trimEnd(),
94 isWrapContinuation: false,
95 wrapsToNext: false,
96 };
97​
98 expect(terminalLinkAtColumn(row, 2)).toBe("https://t3.codes");
99 expect(terminalLinkAtColumn(row, cells.length - 1)).toBe("https://t3.codes");
100 expect(terminalLinkAtColumn(row, 0)).toBeNull();
101 });
102​
103 it("uses shared path matching and reconstructs soft-wrapped links", () => {
104 const row = (text: string, isWrapContinuation: boolean, wrapsToNext = false): GhosttyRow => ({
105 cells: Array.from(text.padEnd(16), (character) => cell(character)),
106 text: text.trimEnd(),
107 isWrapContinuation,
108 wrapsToNext,
109 });
110 const rows = [
111 row("https://example.", false),
112 row("com/reference", true),
113 row("~/project/file", false),
114 row("C:\\repo\\file.ts", false),
115 ];
116​
117 expect(terminalLinkAtPosition(rows, 0, 8)).toBe("https://example.com/reference");
118 expect(terminalLinkAtPosition(rows, 1, 4)).toBe("https://example.com/reference");
119 expect(terminalLinkAtPosition(rows, 2, 2)).toBe("~/project/file");
120 expect(terminalLinkAtPosition(rows, 3, 4)).toBe("C:\\repo\\file.ts");
121 });
122​
123 it("refuses links truncated at the viewport edges instead of mis-resolving", () => {
124 const row = (text: string, isWrapContinuation: boolean, wrapsToNext = false): GhosttyRow => ({
125 cells: Array.from(text.padEnd(16), (character) => cell(character)),
126 text: text.trimEnd(),
127 isWrapContinuation,
128 wrapsToNext,
129 });
130 // The head of the wrapped line scrolled above the viewport.
131 const headCut = [row("ple.com/missing", true), row("head", true)];
132 expect(terminalLinkAtPosition(headCut, 0, 4)).toBeNull();
133 // The bottom row soft-wraps on below the viewport.
134 const tailCut = [row("https://t3.codes", false, true)];
135 expect(terminalLinkAtPosition(tailCut, 0, 8)).toBeNull();
136 // A partial bottom row is provably complete and still resolves.
137 const complete = [row("https://t3.codes", false), row("", false)];
138 expect(terminalLinkAtPosition(complete, 0, 8)).toBe("https://t3.codes");
139 // A wide grapheme earlier in the row must not break truncation detection:
140 // the soft-wrap flag decides, not string-length-versus-cell-count.
141 const wideFull: GhosttyRow = {
142 cells: [
143 { ...cell("πŸ™‚"), wide: 1 },
144 { ...cell(""), wide: 2 },
145 ...Array.from("https://t3.code", (character) => cell(character)),
146 ],
147 text: "πŸ™‚ https://t3.code",
148 isWrapContinuation: false,
149 wrapsToNext: true,
150 };
151 expect(terminalLinkAtPosition([wideFull], 0, 8)).toBeNull();
152 // Unwritten trailing cells prove the bottom row is complete.
153 const unwrittenTail: GhosttyRow = {
154 cells: [
155 ...Array.from("https://t3.codes", (character) => cell(character)),
156 cell(""),
157 cell(""),
158 ],
159 text: "https://t3.codes",
160 isWrapContinuation: false,
161 wrapsToNext: false,
162 };
163 expect(terminalLinkAtPosition([unwrittenTail], 0, 8)).toBe("https://t3.codes");
164 });
165});
166​
167describe("isTerminalCopyShortcut", () => {
168 const event = (overrides: Partial<Parameters<typeof isTerminalCopyShortcut>[0]> = {}) => ({
169 ctrlKey: false,
170 key: "c",
171 metaKey: false,
172 shiftKey: false,
173 ...overrides,
174 });
175​
176 it("keeps Ctrl+C available for SIGINT on macOS", () => {
177 expect(isTerminalCopyShortcut(event({ ctrlKey: true }), "MacIntel")).toBe(false);
178 expect(isTerminalCopyShortcut(event({ metaKey: true }), "MacIntel")).toBe(true);
179 });
180​
181 it("uses the conventional Ctrl+Shift+C shortcut elsewhere", () => {
182 expect(isTerminalCopyShortcut(event({ ctrlKey: true }), "Linux x86_64")).toBe(false);
183 expect(isTerminalCopyShortcut(event({ ctrlKey: true, shiftKey: true }), "Linux x86_64")).toBe(
184 true,
185 );
186 });
187​
188 it("uses the produced character instead of the physical key position", () => {
189 expect(isTerminalCopyShortcut(event({ key: "C", metaKey: true }), "MacIntel")).toBe(true);
190 expect(isTerminalCopyShortcut(event({ key: "j", metaKey: true }), "MacIntel")).toBe(false);
191 });
192});
193​
194describe("isTerminalPasteShortcut", () => {
195 const event = (overrides: Partial<Parameters<typeof isTerminalPasteShortcut>[0]> = {}) => ({
196 ctrlKey: false,
197 key: "v",
198 metaKey: false,
199 shiftKey: false,
200 ...overrides,
201 });
202​
203 it("uses Cmd+V on macOS", () => {
204 expect(isTerminalPasteShortcut(event({ metaKey: true }), "MacIntel")).toBe(true);
205 expect(isTerminalPasteShortcut(event({ ctrlKey: true }), "MacIntel")).toBe(false);
206 });
207​
208 it("preserves Ctrl+V and uses Ctrl+Shift+V elsewhere", () => {
209 expect(isTerminalPasteShortcut(event({ ctrlKey: true }), "Linux x86_64")).toBe(false);
210 expect(isTerminalPasteShortcut(event({ ctrlKey: true, shiftKey: true }), "Linux x86_64")).toBe(
211 true,
212 );
213 });
214});
215​
216describe("isTerminalCompositionCommitInput", () => {
217 it("identifies browser composition follow-up input", () => {
218 expect(isTerminalCompositionCommitInput({ inputType: "" })).toBe(true);
219 expect(isTerminalCompositionCommitInput({ inputType: "insertCompositionText" })).toBe(true);
220 expect(isTerminalCompositionCommitInput({ inputType: "insertFromComposition" })).toBe(true);
221 });
222​
223 it("keeps a fast repeated input as legitimate text", () => {
224 expect(isTerminalCompositionCommitInput({ inputType: "insertText" })).toBe(false);
225 });
226});
227​
228describe("application mouse reporting", () => {
229 const event = (overrides: Partial<Parameters<typeof shouldReportTerminalMouse>[1]> = {}) => ({
230 ctrlKey: false,
231 metaKey: false,
232 shiftKey: false,
233 ...overrides,
234 });
235​
236 it("keeps Shift and link activation modifiers available to the browser", () => {
237 expect(shouldReportTerminalMouse(true, event())).toBe(true);
238 expect(shouldReportTerminalMouse(true, event({ shiftKey: true }))).toBe(false);
239 expect(shouldReportTerminalMouse(true, event({ ctrlKey: true }))).toBe(false);
240 expect(shouldReportTerminalMouse(true, event({ metaKey: true }))).toBe(false);
241 expect(shouldReportTerminalMouse(false, event())).toBe(false);
242 });
243​
244 it("maps browser buttons to Ghostty's button enum", () => {
245 expect([0, 1, 2, 3, 4, 5].map(ghosttyMouseButton)).toEqual([1, 3, 2, 4, 5, null]);
246 });
247});
248​
249describe("terminal font resolution", () => {
250 it("keeps the glyph fallbacks behind a custom text face", () => {
251 expect(terminalFontFamily()).toBe(DEFAULT_TERMINAL_FONT_FAMILY);
252 expect(terminalFontFamily(" ")).toBe(DEFAULT_TERMINAL_FONT_FAMILY);
253 const custom = terminalFontFamily('"Fira Code"');
254 expect(custom.startsWith('"Fira Code", ')).toBe(true);
255 expect(custom).toContain('"Symbols Nerd Font Mono"');
256 expect(custom.endsWith("monospace")).toBe(true);
257 });
258​
259 it("clamps requested font sizes to the supported range", () => {
260 expect(terminalFontSize()).toBe(DEFAULT_TERMINAL_FONT_SIZE);
261 expect(terminalFontSize(Number.NaN)).toBe(DEFAULT_TERMINAL_FONT_SIZE);
262 expect(terminalFontSize(13.4)).toBe(13);
263 expect(terminalFontSize(2)).toBe(6);
264 expect(terminalFontSize(90)).toBe(32);
265 });
266});
267​
268describe("terminalContentOriginY", () => {
269 it("stays top-anchored like a fresh terminal until scrollback exists", () => {
270 expect(terminalContentOriginY(100, 4, 5, 16, false)).toBe(4);
271 });
272​
273 it("pins the grid to the bottom by moving the sub-row slack above row 0", () => {
274 // 100px mount, 4px padding, 5 rows of 16px: 92 - 80 = 12px slack on top.
275 expect(terminalContentOriginY(100, 4, 5, 16, true)).toBe(16);
276 // Exact fit keeps the origin at the padding.
277 expect(terminalContentOriginY(88, 4, 5, 16, true)).toBe(4);
278 // A mount smaller than the grid never pushes the origin above the padding.
279 expect(terminalContentOriginY(80, 4, 5, 16, true)).toBe(4);
280 });
281​
282 it("keeps the prompt stationary while a drag crosses row boundaries", () => {
283 // Growing the mount pixel by pixel: the bottom edge (origin + rows*cell)
284 // tracks the mount bottom exactly until a new row fits.
285 for (let height = 88; height < 104; height += 1) {
286 const rows = Math.max(1, Math.floor((height - 8) / 16));
287 const origin = terminalContentOriginY(height, 4, rows, 16, true);
288 expect(origin + rows * 16).toBe(height - 4);
289 }
290 });
291});
292​
293describe("terminalWheelDeltaRows", () => {
294 it("converts line-mode deltas into whole rows", () => {
295 const result = terminalWheelDeltaRows({ deltaY: 3, deltaMode: 1 }, 16, 24, 0);
296 expect(result.rows).toBe(3);
297 expect(result.remainder).toBe(0);
298 });
299​
300 it("accumulates fractional pixel deltas across events", () => {
301 let remainder = 0;
302 let scrolled = 0;
303 for (let index = 0; index < 4; index += 1) {
304 const result = terminalWheelDeltaRows({ deltaY: 5, deltaMode: 0 }, 16, 24, remainder);
305 remainder = result.remainder;
306 scrolled += result.rows;
307 }
308 expect(scrolled).toBe(1);
309 expect(remainder).toBeCloseTo(4 / 16);
310 });
311​
312 it("keeps direction for negative page-mode deltas", () => {
313 const result = terminalWheelDeltaRows({ deltaY: -1, deltaMode: 2 }, 16, 24, 0);
314 expect(result.rows).toBe(-24);
315 expect(result.remainder).toBe(0);
316 });
317});
318​
319describe("terminalWheelArrowData", () => {
320 it("emits one arrow per row honoring application cursor keys", () => {
321 expect(terminalWheelArrowData(-2, false)).toBe("\u001b[A\u001b[A");
322 expect(terminalWheelArrowData(3, false)).toBe("\u001b[B\u001b[B\u001b[B");
323 expect(terminalWheelArrowData(-1, true)).toBe("\u001bOA");
324 expect(terminalWheelArrowData(0, true)).toBe("");
325 });
326});
327​
328describe("isTerminalLinkPointerGesture", () => {
329 it("uses Command on macOS and Control elsewhere", () => {
330 expect(isTerminalLinkPointerGesture({ ctrlKey: false, metaKey: true }, "MacIntel")).toBe(true);
331 expect(isTerminalLinkPointerGesture({ ctrlKey: true, metaKey: false }, "MacIntel")).toBe(false);
332 expect(isTerminalLinkPointerGesture({ ctrlKey: true, metaKey: false }, "Linux x86_64")).toBe(
333 true,
334 );
335 expect(isTerminalLinkPointerGesture({ ctrlKey: false, metaKey: true }, "Linux x86_64")).toBe(
336 false,
337 );
338 });
339});
340​
341describe("advanceTerminalSelectionClickSequence", () => {
342 it("recognizes stationary double and triple pointer presses without PointerEvent.detail", () => {
343 const first = advanceTerminalSelectionClickSequence(null, {
344 clientX: 20,
345 clientY: 30,
346 timeStamp: 1_000,
347 });
348 const second = advanceTerminalSelectionClickSequence(first, {
349 clientX: 22,
350 clientY: 29,
351 timeStamp: 1_200,
352 });
353 const third = advanceTerminalSelectionClickSequence(second, {
354 clientX: 21,
355 clientY: 31,
356 timeStamp: 1_400,
357 });
358​
359 expect([first.count, second.count, third.count]).toEqual([1, 2, 3]);
360 });
361​
362 it("starts over after movement, delay, or a completed triple click", () => {
363 const previous = { count: 3, time: 1_000, x: 20, y: 30 };
364 expect(
365 advanceTerminalSelectionClickSequence(previous, {
366 clientX: 20,
367 clientY: 30,
368 timeStamp: 1_100,
369 }).count,
370 ).toBe(1);
371 expect(
372 advanceTerminalSelectionClickSequence(previous, {
373 clientX: 30,
374 clientY: 30,
375 timeStamp: 1_100,
376 }).count,
377 ).toBe(1);
378 expect(
379 advanceTerminalSelectionClickSequence(previous, {
380 clientX: 20,
381 clientY: 30,
382 timeStamp: 1_501,
383 }).count,
384 ).toBe(1);
385 });
386});
387​
388describe("terminal scrollbar", () => {
389 it("maps Ghostty's viewport state to a proportional thumb", () => {
390 expect(terminalScrollbarGeometry({ total: 100, offset: 40, len: 20 }, 200)).toEqual({
391 thumbHeight: 40,
392 thumbTop: 80,
393 maxOffset: 80,
394 });
395 expect(terminalScrollbarGeometry({ total: 100, offset: 0, len: 100 }, 200)).toBeNull();
396 });
397​
398 it("keeps the thumb usable for large scrollback and maps dragging back to rows", () => {
399 const state = { total: 10_000, offset: 0, len: 20 };
400 expect(terminalScrollbarGeometry(state, 200)).toEqual({
401 thumbHeight: 18,
402 thumbTop: 0,
403 maxOffset: 9_980,
404 });
405 expect(terminalScrollbarOffsetAtPointer(state, 200, 191, 9)).toBe(9_980);
406 });
407});