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