Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { GHOSTTY_CELL_WIDE, type GhosttyCell, type GhosttySnapshot } from "./core";
4import {
5 ghosttyTextRunEnd,
6 measureGhosttyCell,
7 renderGhosttySnapshot,
8 terminalGridSize,
9} from "./renderer";
10
11const cell = (text: string, wide = 0): GhosttyCell => ({
12 text,
13 wide,
14 foreground: { r: 255, g: 255, b: 255 },
15 background: { r: 0, g: 0, b: 0 },
16 bold: false,
17 italic: false,
18 invisible: false,
19 strikethrough: false,
20 overline: false,
21 underline: false,
22 selected: false,
23});
24
25describe("terminalGridSize", () => {
26 it("matches the mobile renderer's cell-and-padding sizing model", () => {
27 expect(terminalGridSize(808, 408, { width: 10, height: 20, baseline: 15 }, 4)).toEqual({
28 cols: 80,
29 rows: 20,
30 });
31 });
32
33 it("never sends an invalid zero-sized terminal to libghostty", () => {
34 expect(terminalGridSize(0, 0, { width: 10, height: 20, baseline: 15 }, 4)).toEqual({
35 cols: 1,
36 rows: 1,
37 });
38 });
39});
40
41describe("measureGhosttyCell", () => {
42 it("uses descender-aware metrics and the mobile terminal line-height", () => {
43 const measureText = (text: string) =>
44 text === "M"
45 ? { width: 7.2, actualBoundingBoxAscent: 9, actualBoundingBoxDescent: 0 }
46 : { width: 14.4, actualBoundingBoxAscent: 9, actualBoundingBoxDescent: 3 };
47 const context = {
48 font: "",
49 measureText,
50 } as unknown as CanvasRenderingContext2D;
51
52 expect(measureGhosttyCell(context, 12, "monospace")).toEqual({
53 width: 7.2,
54 height: 16,
55 baseline: 11,
56 });
57 });
58});
59
60describe("ghosttyTextRunEnd", () => {
61 it("includes wide spacer tails in the visual clip without rendering spaces", () => {
62 const cells = [
63 cell("界", GHOSTTY_CELL_WIDE.wide),
64 cell("", GHOSTTY_CELL_WIDE.spacerTail),
65 cell("🙂", GHOSTTY_CELL_WIDE.wide),
66 cell("", GHOSTTY_CELL_WIDE.spacerTail),
67 cell(""),
68 ];
69 expect(ghosttyTextRunEnd(cells, 0, () => true)).toBe(4);
70 });
71});
72
73describe("renderGhosttySnapshot", () => {
74 it("draws solid Powerline separators to their cell edges instead of as text", () => {
75 const paths: number[][][] = [];
76 let path: number[][] = [];
77 const fillTextCalls: unknown[][] = [];
78 const context = {
79 canvas: { width: 200, height: 40 },
80 beginPath: () => {
81 path = [];
82 },
83 clip: () => {},
84 closePath: () => {},
85 fill: () => paths.push(path),
86 fillRect: () => {},
87 fillText: (...args: unknown[]) => fillTextCalls.push(args),
88 lineTo: (x: number, y: number) => path.push([x, y]),
89 moveTo: (x: number, y: number) => path.push([x, y]),
90 rect: () => {},
91 resetTransform: () => {},
92 restore: () => {},
93 save: () => {},
94 set fillStyle(_value: string) {},
95 set font(_value: string) {},
96 set textBaseline(_value: string) {},
97 } as unknown as CanvasRenderingContext2D;
98 const snapshot: GhosttySnapshot = {
99 cols: 3,
100 rows: 1,
101 foreground: { r: 255, g: 255, b: 255 },
102 background: { r: 0, g: 0, b: 0 },
103 cursor: { r: 255, g: 255, b: 255 },
104 cursorX: -1,
105 cursorY: -1,
106 cursorVisible: false,
107 cursorBlinking: false,
108 cursorStyle: 1,
109 dirtyRows: new Set([0]),
110 rowData: [
111 {
112 cells: [cell("a"), cell("\ue0b0"), cell("\ue0b2")],
113 text: "a\ue0b0\ue0b2",
114 isWrapContinuation: false,
115 wrapsToNext: false,
116 },
117 ],
118 };
119
120 renderGhosttySnapshot({
121 context,
122 snapshot,
123 metrics: { width: 10, height: 20, baseline: 15 },
124 fontSize: 12,
125 fontFamily: "monospace",
126 padding: 4,
127 forceFull: false,
128 cursorOn: false,
129 });
130
131 expect(fillTextCalls).toEqual([["a", 4, 19, 10]]);
132 expect(paths).toEqual([
133 [
134 [14, 4],
135 [24, 14],
136 [14, 24],
137 ],
138 [
139 [34, 4],
140 [24, 14],
141 [34, 24],
142 ],
143 ]);
144 });
145
146 it("underlines every cell in a hovered wrapped link", () => {
147 const fillRectCalls: number[][] = [];
148 const context = {
149 canvas: { width: 200, height: 80 },
150 beginPath: () => {},
151 clip: () => {},
152 fillRect: (...args: number[]) => fillRectCalls.push(args),
153 fillText: () => {},
154 rect: () => {},
155 resetTransform: () => {},
156 restore: () => {},
157 save: () => {},
158 set fillStyle(_value: string) {},
159 set font(_value: string) {},
160 set textBaseline(_value: string) {},
161 } as unknown as CanvasRenderingContext2D;
162 const snapshot: GhosttySnapshot = {
163 cols: 4,
164 rows: 2,
165 foreground: { r: 255, g: 255, b: 255 },
166 background: { r: 0, g: 0, b: 0 },
167 cursor: { r: 255, g: 255, b: 255 },
168 cursorX: -1,
169 cursorY: -1,
170 cursorVisible: false,
171 cursorBlinking: false,
172 cursorStyle: 1,
173 dirtyRows: new Set([0, 1]),
174 rowData: [0, 1].map(() => ({
175 cells: [cell("a"), cell("b"), cell("c"), cell("d")],
176 text: "abcd",
177 isWrapContinuation: false,
178 wrapsToNext: false,
179 })),
180 };
181
182 renderGhosttySnapshot({
183 context,
184 snapshot,
185 metrics: { width: 10, height: 20, baseline: 15 },
186 fontSize: 12,
187 fontFamily: "monospace",
188 padding: 4,
189 forceFull: false,
190 cursorOn: false,
191 hoveredLinkRange: { start: { x: 2, y: 0 }, end: { x: 1, y: 1 } },
192 });
193
194 expect(fillRectCalls.filter(([, , , height]) => height === 1)).toEqual([
195 [24, 22, 10, 1],
196 [34, 22, 10, 1],
197 [4, 42, 10, 1],
198 [14, 42, 10, 1],
199 ]);
200 });
201
202 it("constrains text runs and cursor glyphs to their terminal cells", () => {
203 const fillTextCalls: unknown[][] = [];
204 const context = {
205 canvas: { width: 200, height: 40 },
206 beginPath: () => {},
207 clip: () => {},
208 fillRect: () => {},
209 fillText: (...args: unknown[]) => fillTextCalls.push(args),
210 rect: () => {},
211 resetTransform: () => {},
212 restore: () => {},
213 save: () => {},
214 set fillStyle(_value: string) {},
215 set font(_value: string) {},
216 set textBaseline(_value: string) {},
217 } as unknown as CanvasRenderingContext2D;
218 const cells = [cell("a"), cell("b"), cell("x")];
219 const snapshot: GhosttySnapshot = {
220 cols: 3,
221 rows: 1,
222 foreground: { r: 255, g: 255, b: 255 },
223 background: { r: 0, g: 0, b: 0 },
224 cursor: { r: 255, g: 255, b: 255 },
225 cursorX: 2,
226 cursorY: 0,
227 cursorVisible: true,
228 cursorBlinking: false,
229 cursorStyle: 1,
230 dirtyRows: new Set([0]),
231 rowData: [{ cells, text: "abx", isWrapContinuation: false, wrapsToNext: false }],
232 };
233
234 renderGhosttySnapshot({
235 context,
236 snapshot,
237 metrics: { width: 7.2, height: 16, baseline: 11 },
238 fontSize: 12,
239 fontFamily: "monospace",
240 padding: 4,
241 forceFull: false,
242 cursorOn: true,
243 });
244
245 expect(fillTextCalls).toEqual([
246 ["abx", 4, 15, 21.6],
247 ["x", 18.4, 15, 7.2],
248 ]);
249 });
250
251 it("repaints the cell without an overlay during the blink off phase", () => {
252 const fillTextCalls: unknown[][] = [];
253 const context = {
254 canvas: { width: 200, height: 40 },
255 beginPath: () => {},
256 clip: () => {},
257 fillRect: () => {},
258 fillText: (...args: unknown[]) => fillTextCalls.push(args),
259 rect: () => {},
260 resetTransform: () => {},
261 restore: () => {},
262 save: () => {},
263 set fillStyle(_value: string) {},
264 set font(_value: string) {},
265 set textBaseline(_value: string) {},
266 } as unknown as CanvasRenderingContext2D;
267 const snapshot: GhosttySnapshot = {
268 cols: 3,
269 rows: 1,
270 foreground: { r: 255, g: 255, b: 255 },
271 background: { r: 0, g: 0, b: 0 },
272 cursor: { r: 255, g: 255, b: 255 },
273 cursorX: 2,
274 cursorY: 0,
275 cursorVisible: true,
276 cursorBlinking: true,
277 cursorStyle: 1,
278 dirtyRows: new Set(),
279 rowData: [
280 {
281 cells: [cell("a"), cell("b"), cell("x")],
282 text: "abx",
283 isWrapContinuation: false,
284 wrapsToNext: false,
285 },
286 ],
287 };
288
289 renderGhosttySnapshot({
290 context,
291 snapshot,
292 metrics: { width: 7.2, height: 16, baseline: 11 },
293 fontSize: 12,
294 fontFamily: "monospace",
295 padding: 4,
296 forceFull: false,
297 cursorOn: false,
298 });
299
300 expect(fillTextCalls).toEqual([["abx", 4, 15, 21.6]]);
301 });
302
303 it("repaints the previous cursor row after the cursor moves", () => {
304 const clearedRows: number[] = [];
305 const context = {
306 canvas: { width: 200, height: 80 },
307 beginPath: () => {},
308 clip: () => {},
309 fillRect: (_left: number, top: number, _width: number, height: number) => {
310 if (height === 16) clearedRows.push(top);
311 },
312 fillText: () => {},
313 rect: () => {},
314 resetTransform: () => {},
315 restore: () => {},
316 save: () => {},
317 set fillStyle(_value: string) {},
318 set font(_value: string) {},
319 set textBaseline(_value: string) {},
320 } as unknown as CanvasRenderingContext2D;
321 const snapshot: GhosttySnapshot = {
322 cols: 1,
323 rows: 3,
324 foreground: { r: 255, g: 255, b: 255 },
325 background: { r: 0, g: 0, b: 0 },
326 cursor: { r: 255, g: 255, b: 255 },
327 cursorX: 0,
328 cursorY: 2,
329 cursorVisible: true,
330 cursorBlinking: false,
331 cursorStyle: 1,
332 dirtyRows: new Set(),
333 rowData: [0, 1, 2].map(() => ({
334 cells: [cell("")],
335 text: "",
336 isWrapContinuation: false,
337 wrapsToNext: false,
338 })),
339 };
340
341 renderGhosttySnapshot({
342 context,
343 snapshot,
344 metrics: { width: 7.2, height: 16, baseline: 11 },
345 fontSize: 12,
346 fontFamily: "monospace",
347 padding: 4,
348 forceFull: false,
349 cursorOn: true,
350 previousCursorY: 0,
351 });
352
353 expect(clearedRows).toEqual([4, 36, 36]);
354 });
355});