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("constrains text runs and cursor glyphs to their terminal cells", () => {
75 const fillTextCalls: unknown[][] = [];
76 const context = {
77 canvas: { width: 200, height: 40 },
78 beginPath: () => {},
79 clip: () => {},
80 fillRect: () => {},
81 fillText: (...args: unknown[]) => fillTextCalls.push(args),
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 cells = [cell("a"), cell("b"), cell("x")];
91 const snapshot: GhosttySnapshot = {
92 cols: 3,
93 rows: 1,
94 foreground: { r: 255, g: 255, b: 255 },
95 background: { r: 0, g: 0, b: 0 },
96 cursor: { r: 255, g: 255, b: 255 },
97 cursorX: 2,
98 cursorY: 0,
99 cursorVisible: true,
100 cursorBlinking: false,
101 cursorStyle: 1,
102 dirtyRows: new Set([0]),
103 rowData: [{ cells, text: "abx", isWrapContinuation: false, wrapsToNext: false }],
104 };
105
106 renderGhosttySnapshot({
107 context,
108 snapshot,
109 metrics: { width: 7.2, height: 16, baseline: 11 },
110 fontSize: 12,
111 fontFamily: "monospace",
112 padding: 4,
113 forceFull: false,
114 cursorOn: true,
115 });
116
117 expect(fillTextCalls).toEqual([
118 ["abx", 4, 15, 21.6],
119 ["x", 18.4, 15, 7.2],
120 ]);
121 });
122
123 it("repaints the cell without an overlay during the blink off phase", () => {
124 const fillTextCalls: unknown[][] = [];
125 const context = {
126 canvas: { width: 200, height: 40 },
127 beginPath: () => {},
128 clip: () => {},
129 fillRect: () => {},
130 fillText: (...args: unknown[]) => fillTextCalls.push(args),
131 rect: () => {},
132 resetTransform: () => {},
133 restore: () => {},
134 save: () => {},
135 set fillStyle(_value: string) {},
136 set font(_value: string) {},
137 set textBaseline(_value: string) {},
138 } as unknown as CanvasRenderingContext2D;
139 const snapshot: GhosttySnapshot = {
140 cols: 3,
141 rows: 1,
142 foreground: { r: 255, g: 255, b: 255 },
143 background: { r: 0, g: 0, b: 0 },
144 cursor: { r: 255, g: 255, b: 255 },
145 cursorX: 2,
146 cursorY: 0,
147 cursorVisible: true,
148 cursorBlinking: true,
149 cursorStyle: 1,
150 dirtyRows: new Set(),
151 rowData: [
152 {
153 cells: [cell("a"), cell("b"), cell("x")],
154 text: "abx",
155 isWrapContinuation: false,
156 wrapsToNext: false,
157 },
158 ],
159 };
160
161 renderGhosttySnapshot({
162 context,
163 snapshot,
164 metrics: { width: 7.2, height: 16, baseline: 11 },
165 fontSize: 12,
166 fontFamily: "monospace",
167 padding: 4,
168 forceFull: false,
169 cursorOn: false,
170 });
171
172 // The cursor row still repaints so the block disappears, but the inverted
173 // glyph the on phase draws over the cell is gone.
174 expect(fillTextCalls).toEqual([["abx", 4, 15, 21.6]]);
175 });
176
177 it("repaints the previous cursor row after the cursor moves", () => {
178 const clearedRows: number[] = [];
179 const context = {
180 canvas: { width: 200, height: 80 },
181 beginPath: () => {},
182 clip: () => {},
183 fillRect: (_left: number, top: number, _width: number, height: number) => {
184 if (height === 16) clearedRows.push(top);
185 },
186 fillText: () => {},
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: 1,
197 rows: 3,
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: 0,
202 cursorY: 2,
203 cursorVisible: true,
204 cursorBlinking: false,
205 cursorStyle: 1,
206 dirtyRows: new Set(),
207 rowData: [0, 1, 2].map(() => ({
208 cells: [cell("")],
209 text: "",
210 isWrapContinuation: false,
211 wrapsToNext: false,
212 })),
213 };
214
215 renderGhosttySnapshot({
216 context,
217 snapshot,
218 metrics: { width: 7.2, height: 16, baseline: 11 },
219 fontSize: 12,
220 fontFamily: "monospace",
221 padding: 4,
222 forceFull: false,
223 cursorOn: true,
224 previousCursorY: 0,
225 });
226
227 expect(clearedRows).toEqual([4, 36, 36]);
228 });
229});