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("uses the conventional Ctrl+Shift+C shortcut elsewhere", () => {
223 expect(isTerminalCopyShortcut(event({ ctrlKey: true }), "Linux x86_64")).toBe(false);
224 expect(isTerminalCopyShortcut(event({ ctrlKey: true, shiftKey: true }), "Linux x86_64")).toBe(
225 true,
226 );
227 });
228β
229 it("uses the produced character instead of the physical key position", () => {
230 expect(isTerminalCopyShortcut(event({ key: "C", metaKey: true }), "MacIntel")).toBe(true);
231 expect(isTerminalCopyShortcut(event({ key: "j", metaKey: true }), "MacIntel")).toBe(false);
232 });
233});
234β
235describe("isTerminalPasteShortcut", () => {
236 const event = (overrides: Partial<Parameters<typeof isTerminalPasteShortcut>[0]> = {}) => ({
237 ctrlKey: false,
238 key: "v",
239 metaKey: false,
240 shiftKey: false,
241 ...overrides,
242 });
243β
244 it("uses Cmd+V on macOS", () => {
245 expect(isTerminalPasteShortcut(event({ metaKey: true }), "MacIntel")).toBe(true);
246 expect(isTerminalPasteShortcut(event({ ctrlKey: true }), "MacIntel")).toBe(false);
247 });
248β
249 it("preserves Ctrl+V and uses Ctrl+Shift+V elsewhere", () => {
250 expect(isTerminalPasteShortcut(event({ ctrlKey: true }), "Linux x86_64")).toBe(false);
251 expect(isTerminalPasteShortcut(event({ ctrlKey: true, shiftKey: true }), "Linux x86_64")).toBe(
252 true,
253 );
254 });
255});
256β
257describe("isTerminalCompositionCommitInput", () => {
258 it("identifies browser composition follow-up input", () => {
259 expect(isTerminalCompositionCommitInput({ inputType: "" })).toBe(true);
260 expect(isTerminalCompositionCommitInput({ inputType: "insertCompositionText" })).toBe(true);
261 expect(isTerminalCompositionCommitInput({ inputType: "insertFromComposition" })).toBe(true);
262 });
263β
264 it("keeps a fast repeated input as legitimate text", () => {
265 expect(isTerminalCompositionCommitInput({ inputType: "insertText" })).toBe(false);
266 });
267});
268β
269describe("application mouse reporting", () => {
270 const event = (overrides: Partial<Parameters<typeof shouldReportTerminalMouse>[1]> = {}) => ({
271 ctrlKey: false,
272 metaKey: false,
273 shiftKey: false,
274 ...overrides,
275 });
276β
277 it("keeps Shift and link activation modifiers available to the browser", () => {
278 expect(shouldReportTerminalMouse(true, event())).toBe(true);
279 expect(shouldReportTerminalMouse(true, event({ shiftKey: true }))).toBe(false);
280 expect(shouldReportTerminalMouse(true, event({ ctrlKey: true }))).toBe(false);
281 expect(shouldReportTerminalMouse(true, event({ metaKey: true }))).toBe(false);
282 expect(shouldReportTerminalMouse(false, event())).toBe(false);
283 });
284β
285 it("maps browser buttons to Ghostty's button enum", () => {
286 expect([0, 1, 2, 3, 4, 5].map(ghosttyMouseButton)).toEqual([1, 3, 2, 4, 5, null]);
287 });
288β
289 it("only shows link hover during mouse tracking when the link modifier is held", () => {
290 expect(shouldShowTerminalLinkHover(false, false)).toBe(true);
291 expect(shouldShowTerminalLinkHover(false, true)).toBe(true);
292 expect(shouldShowTerminalLinkHover(true, false)).toBe(false);
293 expect(shouldShowTerminalLinkHover(true, true)).toBe(true);
294 });
295});
296β
297describe("terminal font resolution", () => {
298 it("validates the requested face after its styles load", async () => {
299 let loaded = false;
300 const load = vi.fn(async () => {
301 loaded = true;
302 return [];
303 });
304 const resolve = vi.fn(() => {
305 expect(loaded).toBe(true);
306 return DEFAULT_TERMINAL_FONT_FAMILY;
307 });
308β
309 await expect(
310 loadTerminalFontFamily("Proportional Test", 12, {
311 load,
312 resolve,
313 }),
314 ).resolves.toBe(DEFAULT_TERMINAL_FONT_FAMILY);
315 expect(load).toHaveBeenCalledTimes(4);
316 expect(resolve).toHaveBeenCalledWith("Proportional Test");
317 });
318β
319 it("keeps the glyph fallbacks behind a custom text face", () => {
320 expect(terminalFontFamily()).toBe(DEFAULT_TERMINAL_FONT_FAMILY);
321 expect(terminalFontFamily(" ")).toBe(DEFAULT_TERMINAL_FONT_FAMILY);
322 const custom = terminalFontFamily('"Fira Code"');
323 expect(custom.startsWith('"Fira Code", ')).toBe(true);
324 expect(custom).toContain('"Symbols Nerd Font Mono"');
325 expect(custom.endsWith("monospace")).toBe(true);
326 });
327β
328 it("ignores proportional families the cell grid cannot lay out", () => {
329 // jsdom has no canvas metrics, so the probe answers "monospace" and the
330 // family is kept; the guard is exercised in the browser instead. Assert the
331 // shape stays intact so a rejected face still yields a usable stack.
332 const stack = terminalFontFamily("Helvetica Neue");
333 expect(stack.endsWith("monospace")).toBe(true);
334 });
335β
336 it("quotes families the canvas font shorthand would otherwise reject", () => {
337 expect(terminalFontFamily("3270 Nerd Font").startsWith('"3270 Nerd Font", ')).toBe(true);
338 expect(terminalFontFamily("M+ 1m").startsWith('"M+ 1m", ')).toBe(true);
339 expect(terminalFontFamily("Cascadia Code, Menlo").startsWith('"Cascadia Code", Menlo, ')).toBe(
340 true,
341 );
342 expect(terminalFontFamily(" , ")).toBe(DEFAULT_TERMINAL_FONT_FAMILY);
343 });
344β
345 it("clamps requested font sizes to the supported range", () => {
346 expect(terminalFontSize()).toBe(DEFAULT_TERMINAL_FONT_SIZE);
347 expect(terminalFontSize(Number.NaN)).toBe(DEFAULT_TERMINAL_FONT_SIZE);
348 expect(terminalFontSize(13.4)).toBe(13);
349 expect(terminalFontSize(2)).toBe(6);
350 expect(terminalFontSize(90)).toBe(32);
351 });
352});
353β
354describe("terminalContentOriginY", () => {
355 it("stays top-anchored like a fresh terminal until scrollback exists", () => {
356 expect(terminalContentOriginY(100, 4, 5, 16, false)).toBe(4);
357 });
358β
359 it("pins the grid to the bottom by moving the sub-row slack above row 0", () => {
360 // 100px mount, 4px padding, 5 rows of 16px: 92 - 80 = 12px slack on top.
361 expect(terminalContentOriginY(100, 4, 5, 16, true)).toBe(16);
362 // Exact fit keeps the origin at the padding.
363 expect(terminalContentOriginY(88, 4, 5, 16, true)).toBe(4);
364 // A mount smaller than the grid never pushes the origin above the padding.
365 expect(terminalContentOriginY(80, 4, 5, 16, true)).toBe(4);
366 });
367β
368 it("keeps the prompt stationary while a drag crosses row boundaries", () => {
369 // Growing the mount pixel by pixel: the bottom edge (origin + rows*cell)
370 // tracks the mount bottom exactly until a new row fits.
371 for (let height = 88; height < 104; height += 1) {
372 const rows = Math.max(1, Math.floor((height - 8) / 16));
373 const origin = terminalContentOriginY(height, 4, rows, 16, true);
374 expect(origin + rows * 16).toBe(height - 4);
375 }
376 });
377});
378β
379describe("terminalWheelDeltaRows", () => {
380 it("converts line-mode deltas into whole rows", () => {
381 const result = terminalWheelDeltaRows({ deltaY: 3, deltaMode: 1 }, 16, 24, 0);
382 expect(result.rows).toBe(3);
383 expect(result.remainder).toBe(0);
384 });
385β
386 it("accumulates fractional pixel deltas across events", () => {
387 let remainder = 0;
388 let scrolled = 0;
389 for (let index = 0; index < 4; index += 1) {
390 const result = terminalWheelDeltaRows({ deltaY: 5, deltaMode: 0 }, 16, 24, remainder);
391 remainder = result.remainder;
392 scrolled += result.rows;
393 }
394 expect(scrolled).toBe(1);
395 expect(remainder).toBeCloseTo(4 / 16);
396 });
397β
398 it("keeps direction for negative page-mode deltas", () => {
399 const result = terminalWheelDeltaRows({ deltaY: -1, deltaMode: 2 }, 16, 24, 0);
400 expect(result.rows).toBe(-24);
401 expect(result.remainder).toBe(0);
402 });
403});
404β
405describe("terminalWheelArrowData", () => {
406 it("emits one arrow per row honoring application cursor keys", () => {
407 expect(terminalWheelArrowData(-2, false)).toBe("\u001b[A\u001b[A");
408 expect(terminalWheelArrowData(3, false)).toBe("\u001b[B\u001b[B\u001b[B");
409 expect(terminalWheelArrowData(-1, true)).toBe("\u001bOA");
410 expect(terminalWheelArrowData(0, true)).toBe("");
411 });
412});
413β
414describe("isTerminalLinkPointerGesture", () => {
415 it("uses Command on macOS and Control elsewhere", () => {
416 expect(isTerminalLinkPointerGesture({ ctrlKey: false, metaKey: true }, "MacIntel")).toBe(true);
417 expect(isTerminalLinkPointerGesture({ ctrlKey: true, metaKey: false }, "MacIntel")).toBe(false);
418 expect(isTerminalLinkPointerGesture({ ctrlKey: true, metaKey: false }, "Linux x86_64")).toBe(
419 true,
420 );
421 expect(isTerminalLinkPointerGesture({ ctrlKey: false, metaKey: true }, "Linux x86_64")).toBe(
422 false,
423 );
424 });
425});
426β
427describe("advanceTerminalSelectionClickSequence", () => {
428 it("recognizes stationary double and triple pointer presses without PointerEvent.detail", () => {
429 const first = advanceTerminalSelectionClickSequence(null, {
430 clientX: 20,
431 clientY: 30,
432 timeStamp: 1_000,
433 });
434 const second = advanceTerminalSelectionClickSequence(first, {
435 clientX: 22,
436 clientY: 29,
437 timeStamp: 1_200,
438 });
439 const third = advanceTerminalSelectionClickSequence(second, {
440 clientX: 21,
441 clientY: 31,
442 timeStamp: 1_400,
443 });
444β
445 expect([first.count, second.count, third.count]).toEqual([1, 2, 3]);
446 });
447β
448 it("starts over after movement, delay, or a completed triple click", () => {
449 const previous = { count: 3, time: 1_000, x: 20, y: 30 };
450 expect(
451 advanceTerminalSelectionClickSequence(previous, {
452 clientX: 20,
453 clientY: 30,
454 timeStamp: 1_100,
455 }).count,
456 ).toBe(1);
457 expect(
458 advanceTerminalSelectionClickSequence(previous, {
459 clientX: 30,
460 clientY: 30,
461 timeStamp: 1_100,
462 }).count,
463 ).toBe(1);
464 expect(
465 advanceTerminalSelectionClickSequence(previous, {
466 clientX: 20,
467 clientY: 30,
468 timeStamp: 1_501,
469 }).count,
470 ).toBe(1);
471 });
472});
473β
474describe("terminal scrollbar", () => {
475 it("maps Ghostty's viewport state to a proportional thumb", () => {
476 expect(terminalScrollbarGeometry({ total: 100, offset: 40, len: 20 }, 200)).toEqual({
477 thumbHeight: 40,
478 thumbTop: 80,
479 maxOffset: 80,
480 });
481 expect(terminalScrollbarGeometry({ total: 100, offset: 0, len: 100 }, 200)).toBeNull();
482 });
483β
484 it("keeps the thumb usable for large scrollback and maps dragging back to rows", () => {
485 const state = { total: 10_000, offset: 0, len: 20 };
486 expect(terminalScrollbarGeometry(state, 200)).toEqual({
487 thumbHeight: 18,
488 thumbTop: 0,
489 maxOffset: 9_980,
490 });
491 expect(terminalScrollbarOffsetAtPointer(state, 200, 191, 9)).toBe(9_980);
492 });
493});