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("underlines every cell in a hovered wrapped link", () => {
75 const fillRectCalls: number[][] = [];
76 const context = {
77 canvas: { width: 200, height: 80 },
78 beginPath: () => {},
79 clip: () => {},
80 fillRect: (...args: number[]) => fillRectCalls.push(args),
81 fillText: () => {},
82 rect: () => {},
83 resetTransform: () => {},
84 restore: () => {},
85 save: () => {},
86 set fillStyle(_value: string) {},
87 set font(_value: string) {},
88 set textBaseline(_value: string) {},
89 } as unknown as CanvasRenderingContext2D;
90 const snapshot: GhosttySnapshot = {
91 cols: 4,
92 rows: 2,
93 foreground: { r: 255, g: 255, b: 255 },
94 background: { r: 0, g: 0, b: 0 },
95 cursor: { r: 255, g: 255, b: 255 },
96 cursorX: -1,
97 cursorY: -1,
98 cursorVisible: false,
99 cursorBlinking: false,
100 cursorStyle: 1,
101 dirtyRows: new Set([0, 1]),
102 rowData: [0, 1].map(() => ({
103 cells: [cell("a"), cell("b"), cell("c"), cell("d")],
104 text: "abcd",
105 isWrapContinuation: false,
106 wrapsToNext: false,
107 })),
108 };
109
110 renderGhosttySnapshot({
111 context,
112 snapshot,
113 metrics: { width: 10, height: 20, baseline: 15 },
114 fontSize: 12,
115 fontFamily: "monospace",
116 padding: 4,
117 forceFull: false,
118 cursorOn: false,
119 hoveredLinkRange: { start: { x: 2, y: 0 }, end: { x: 1, y: 1 } },
120 });
121
122 expect(fillRectCalls.filter(([, , , height]) => height === 1)).toEqual([
123 [24, 22, 10, 1],
124 [34, 22, 10, 1],
125 [4, 42, 10, 1],
126 [14, 42, 10, 1],
127 ]);
128 });
129
130 it("constrains text runs and cursor glyphs to their terminal cells", () => {
131 const fillTextCalls: unknown[][] = [];
132 const context = {
133 canvas: { width: 200, height: 40 },
134 beginPath: () => {},
135 clip: () => {},
136 fillRect: () => {},
137 fillText: (...args: unknown[]) => fillTextCalls.push(args),
138 rect: () => {},
139 resetTransform: () => {},
140 restore: () => {},
141 save: () => {},
142 set fillStyle(_value: string) {},
143 set font(_value: string) {},
144 set textBaseline(_value: string) {},
145 } as unknown as CanvasRenderingContext2D;
146 const cells = [cell("a"), cell("b"), cell("x")];
147 const snapshot: GhosttySnapshot = {
148 cols: 3,
149 rows: 1,
150 foreground: { r: 255, g: 255, b: 255 },
151 background: { r: 0, g: 0, b: 0 },
152 cursor: { r: 255, g: 255, b: 255 },
153 cursorX: 2,
154 cursorY: 0,
155 cursorVisible: true,
156 cursorBlinking: false,
157 cursorStyle: 1,
158 dirtyRows: new Set([0]),
159 rowData: [{ cells, text: "abx", isWrapContinuation: false, wrapsToNext: false }],
160 };
161
162 renderGhosttySnapshot({
163 context,
164 snapshot,
165 metrics: { width: 7.2, height: 16, baseline: 11 },
166 fontSize: 12,
167 fontFamily: "monospace",
168 padding: 4,
169 forceFull: false,
170 cursorOn: true,
171 });
172
173 expect(fillTextCalls).toEqual([
174 ["abx", 4, 15, 21.6],
175 ["x", 18.4, 15, 7.2],
176 ]);
177 });
178
179 it("repaints the cell without an overlay during the blink off phase", () => {
180 const fillTextCalls: unknown[][] = [];
181 const context = {
182 canvas: { width: 200, height: 40 },
183 beginPath: () => {},
184 clip: () => {},
185 fillRect: () => {},
186 fillText: (...args: unknown[]) => fillTextCalls.push(args),
187 rect: () => {},
188 resetTransform: () => {},
189 restore: () => {},
190 save: () => {},
191 set fillStyle(_value: string) {},
192 set font(_value: string) {},
193 set textBaseline(_value: string) {},
194 } as unknown as CanvasRenderingContext2D;
195 const snapshot: GhosttySnapshot = {
196 cols: 3,
197 rows: 1,
198 foreground: { r: 255, g: 255, b: 255 },
199 background: { r: 0, g: 0, b: 0 },
200 cursor: { r: 255, g: 255, b: 255 },
201 cursorX: 2,
202 cursorY: 0,
203 cursorVisible: true,
204 cursorBlinking: true,
205 cursorStyle: 1,
206 dirtyRows: new Set(),
207 rowData: [
208 {
209 cells: [cell("a"), cell("b"), cell("x")],
210 text: "abx",
211 isWrapContinuation: false,
212 wrapsToNext: false,
213 },
214 ],
215 };
216
217 renderGhosttySnapshot({
218 context,
219 snapshot,
220 metrics: { width: 7.2, height: 16, baseline: 11 },
221 fontSize: 12,
222 fontFamily: "monospace",
223 padding: 4,
224 forceFull: false,
225 cursorOn: false,
226 });
227
228 // The cursor row still repaints so the block disappears, but the inverted
229 // glyph the on phase draws over the cell is gone.
230 expect(fillTextCalls).toEqual([["abx", 4, 15, 21.6]]);
231 });
232
233 it("repaints the previous cursor row after the cursor moves", () => {
234 const clearedRows: number[] = [];
235 const context = {
236 canvas: { width: 200, height: 80 },
237 beginPath: () => {},
238 clip: () => {},
239 fillRect: (_left: number, top: number, _width: number, height: number) => {
240 if (height === 16) clearedRows.push(top);
241 },
242 fillText: () => {},
243 rect: () => {},
244 resetTransform: () => {},
245 restore: () => {},
246 save: () => {},
247 set fillStyle(_value: string) {},
248 set font(_value: string) {},
249 set textBaseline(_value: string) {},
250 } as unknown as CanvasRenderingContext2D;
251 const snapshot: GhosttySnapshot = {
252 cols: 1,
253 rows: 3,
254 foreground: { r: 255, g: 255, b: 255 },
255 background: { r: 0, g: 0, b: 0 },
256 cursor: { r: 255, g: 255, b: 255 },
257 cursorX: 0,
258 cursorY: 2,
259 cursorVisible: true,
260 cursorBlinking: false,
261 cursorStyle: 1,
262 dirtyRows: new Set(),
263 rowData: [0, 1, 2].map(() => ({
264 cells: [cell("")],
265 text: "",
266 isWrapContinuation: false,
267 wrapsToNext: false,
268 })),
269 };
270
271 renderGhosttySnapshot({
272 context,
273 snapshot,
274 metrics: { width: 7.2, height: 16, baseline: 11 },
275 fontSize: 12,
276 fontFamily: "monospace",
277 padding: 4,
278 forceFull: false,
279 cursorOn: true,
280 previousCursorY: 0,
281 });
282
283 expect(clearedRows).toEqual([4, 36, 36]);
284 });
285});