@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 624 lines 22.6 KB
Raw
1 import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2 import {
3 applyTerminalAttachStreamEvent,
4 DEFAULT_MAX_TERMINAL_BUFFER_BYTES,
5 INITIAL_TERMINAL_OUTPUT_CURSOR,
6 nextTerminalAttachSeedState,
7 readTerminalOutputUpdate,
8 terminalOutputText,
9 type TerminalBufferState,
10 } from "@t3tools/client-runtime/state/terminal";
11
12 import { writeTerminalOutputUpdate } from "../../components/ThreadTerminalDrawer";
13 import { GHOSTTY_CELL_WIDE, GhosttyTerminalCore, ghosttyCellText } from "./core";
14 import { loadGhosttyRuntime } from "./runtime";
15 import { terminalNavigationShortcutData } from "../../keybindings";
16
17 vi.mock("./vendor/ghostty-vt.wasm?url", async () => ({
18 default: (await import("./vendor/ghostty-vt.wasm?inline")).default,
19 }));
20 vi.mock("./vendor/ghostty-write-pty.wasm?url&no-inline", async () => ({
21 default: (await import("./vendor/ghostty-write-pty.wasm?inline")).default,
22 }));
23
24 function codepointView(codepoints: ReadonlyArray<number>): DataView {
25 const view = new DataView(new ArrayBuffer(codepoints.length * 4));
26 codepoints.forEach((codepoint, index) => view.setUint32(index * 4, codepoint, true));
27 return view;
28 }
29
30 describe("ghosttyCellText", () => {
31 it("converts oversized grapheme clusters without hitting engine spread limits", () => {
32 const graphemeLength = 130_000;
33 const view = new DataView(new ArrayBuffer(graphemeLength * 4));
34 for (let index = 0; index < graphemeLength; index += 1) {
35 view.setUint32(index * 4, index === 0 ? "a".codePointAt(0)! : 0x301, true);
36 }
37 const text = ghosttyCellText(view, graphemeLength);
38 expect(text.length).toBe(graphemeLength);
39 expect(text.codePointAt(0)).toBe("a".codePointAt(0));
40 expect(text.codePointAt(1)).toBe(0x301);
41 expect(text.codePointAt(graphemeLength - 1)).toBe(0x301);
42 });
43
44 // regression: #7809 oversized terminal graphemes crashed rendering
45 it("converts small clusters including astral codepoints", () => {
46 const text = ghosttyCellText(codepointView([0x1f642, 0x20e3]), 2);
47 expect([...text]).toEqual(["\u{1F642}", "\u{20E3}"]);
48 });
49 });
50
51 describe("GhosttyTerminalCore snapshots", () => {
52 const cores = new Set<GhosttyTerminalCore>();
53
54 async function createCore(onData: (data: string) => void = () => {}) {
55 const core = await GhosttyTerminalCore.create(
56 12,
57 3,
58 8,
59 16,
60 {
61 foreground: { r: 255, g: 255, b: 255 },
62 background: { r: 0, g: 0, b: 0 },
63 cursor: { r: 255, g: 255, b: 255 },
64 },
65 onData,
66 );
67 cores.add(core);
68 return core;
69 }
70
71 function createSession(history: string) {
72 return applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), {
73 type: "snapshot",
74 snapshot: {
75 threadId: "terminal-stream-test",
76 terminalId: "term-1",
77 cwd: "/repo",
78 worktreePath: null,
79 status: "running",
80 pid: 123,
81 history,
82 exitCode: null,
83 exitSignal: null,
84 label: "Terminal",
85 updatedAt: "2026-09-04T00:00:00.000Z",
86 },
87 });
88 }
89
90 function append(state: TerminalBufferState, data: string) {
91 return applyTerminalAttachStreamEvent(state, {
92 type: "output",
93 threadId: "terminal-stream-test",
94 terminalId: "term-1",
95 data,
96 });
97 }
98
99 afterEach(() => {
100 for (const core of cores) core.dispose();
101 cores.clear();
102 vi.restoreAllMocks();
103 });
104
105 it.each([
106 { key: "ArrowLeft", expected: "\x1b[1;5D" },
107 { key: "ArrowRight", expected: "\x1b[1;5C" },
108 ])("sends Ctrl+$key as a modified arrow instead of Alt+B/F", async ({ key, expected }) => {
109 const core = await createCore();
110 const event = {
111 code: key,
112 key,
113 repeat: false,
114 shiftKey: false,
115 ctrlKey: true,
116 altKey: false,
117 metaKey: false,
118 isComposing: false,
119 getModifierState: () => false,
120 } as unknown as KeyboardEvent;
121
122 expect(terminalNavigationShortcutData(event, "Win32") ?? core.encodeKey(event)).toBe(expected);
123 });
124
125 it("keeps a leading U+FEFF in an unbracketed paste", async () => {
126 const core = await createCore();
127 core.write("\x1b[?2004l");
128
129 expect(core.encodePaste("\ufeffsample")).toBe("\ufeffsample");
130 });
131
132 it.each([
133 { code: "Numpad1", key: "End", expected: "\x1b[F" },
134 { code: "Numpad5", key: "Clear", expected: "\x1b[E" },
135 { code: "Numpad8", key: "ArrowUp", expected: "\x1b[A" },
136 { code: "NumpadDecimal", key: "Delete", expected: "\x1b[3~" },
137 { mode: "\x1b[?1h", code: "Numpad8", key: "ArrowUp", expected: "\x1bOA" },
138 { mode: "\x1b[>1u", code: "Numpad8", key: "ArrowUp", expected: "\x1b[57419u" },
139 { mode: "\x1b=\x1b[?1035l", code: "Numpad8", key: "8", numLock: true, expected: "\x1bOx" },
140 { mode: "\x1b[>1u", code: "Numpad8", key: "8", numLock: true, expected: "8" },
141 { code: "", key: "Backspace", expected: "\x7f" },
142 { code: "Unidentified", key: "Backspace", expected: "\x7f" },
143 { code: "Backspace", key: "Backspace", expected: "\x7f" },
144 { code: "Delete", key: "Backspace", expected: "\x1b[3~" },
145 { code: "", key: "a", expected: "a" },
146 { code: "", key: "Unidentified", expected: "" },
147 ])(
148 "encodes key $key with physical code '$code'",
149 async ({ mode = "", code, key, numLock = false, expected }) => {
150 const core = await createCore();
151 core.write(mode);
152 const event = {
153 code,
154 key,
155 repeat: false,
156 shiftKey: false,
157 ctrlKey: false,
158 altKey: false,
159 metaKey: false,
160 isComposing: false,
161 getModifierState: (modifier: string) => modifier === "NumLock" && numLock,
162 } as unknown as KeyboardEvent;
163
164 expect(core.encodeKey(event)).toBe(expected);
165 },
166 );
167
168 // smkx is what ncurses keypad(True) sends; a raw-mode parser such as Crossterm never does.
169 it.each([
170 { mode: "", ctrlKey: true, expected: "\x1b[13;5~" },
171 { mode: "\x1b[?1h\x1b=", ctrlKey: true, expected: "\x1b[1;5R" },
172 { mode: "\x1b[?1h\x1b=", shiftKey: true, expected: "\x1b[1;2R" },
173 { mode: "\x1b[?1h\x1b=\x1b[?1l\x1b>", ctrlKey: true, expected: "\x1b[13;5~" },
174 { mode: "\x1b[?1h\x1b=\x1b[>1u", ctrlKey: true, expected: "\x1b[13;5~" },
175 ])(
176 "encodes modified F3 as $expected after $mode",
177 async ({ mode, ctrlKey = false, shiftKey = false, expected }) => {
178 const core = await createCore();
179 core.write(mode);
180 const event = {
181 code: "F3",
182 key: "F3",
183 repeat: false,
184 shiftKey,
185 ctrlKey,
186 altKey: false,
187 metaKey: false,
188 isComposing: false,
189 getModifierState: () => false,
190 } as unknown as KeyboardEvent;
191
192 expect(core.encodeKey(event)).toBe(expected);
193 },
194 );
195
196 it.each(["", "\x1b[>1u"])("encodes macOS Option text with keyboard mode %j", async (mode) => {
197 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel");
198 const core = await createCore();
199 core.write(mode);
200 const option = {
201 code: "KeyL",
202 key: "@",
203 repeat: false,
204 shiftKey: false,
205 ctrlKey: false,
206 altKey: true,
207 metaKey: false,
208 isComposing: false,
209 getModifierState: () => false,
210 } as unknown as KeyboardEvent;
211
212 expect(core.encodeKey(option)).toBe("@");
213 expect(core.encodeKey({ ...option, code: "KeyA", key: "Å", shiftKey: true })).toBe("Å");
214 });
215
216 it("preserves styles, wide cells, and selection after shared memory grows", async () => {
217 const core = await createCore();
218 const runtime = await loadGhosttyRuntime();
219 const grapheme = `e${"\u0301".repeat(64)}`;
220 core.write(`\x1b[1;3;4;8;9;53;38;2;123;45;67;48;2;9;8;7m${grapheme}\x1b[0m界🙂`);
221 const cells = core.snapshot().rowData[0]!.cells;
222 expect(cells[0]).toEqual({
223 text: grapheme,
224 wide: 0,
225 foreground: { r: 123, g: 45, b: 67 },
226 background: { r: 9, g: 8, b: 7 },
227 bold: true,
228 italic: true,
229 invisible: true,
230 strikethrough: true,
231 overline: true,
232 underline: true,
233 selected: false,
234 });
235 expect(cells.slice(1, 5).map(({ text, wide }) => ({ text, wide }))).toEqual([
236 { text: "界", wide: 0 },
237 { text: "", wide: GHOSTTY_CELL_WIDE.spacerTail },
238 { text: "🙂", wide: 0 },
239 { text: "", wide: GHOSTTY_CELL_WIDE.spacerTail },
240 ]);
241
242 runtime.memory.grow(1);
243 core.setSelection({ x: 0, y: 0 }, { x: 2, y: 0 });
244 expect(core.snapshot().rowData[0]!.cells[0]).toEqual({ ...cells[0], selected: true });
245 core.clearSelection();
246 expect(core.snapshot().rowData[0]!.cells[0]).toEqual(cells[0]);
247
248 core.resetAndWrite("\x1b[2;7;38;2;40;100;200;48;2;12;34;56mC\x1b[0m");
249 expect(core.snapshot().rowData[0]!.cells[0]).toMatchObject({
250 text: "C",
251 foreground: { r: 22, g: 59, b: 112 },
252 background: { r: 40, g: 100, b: 200 },
253 bold: false,
254 underline: false,
255 selected: false,
256 });
257 });
258
259 it("resolves foreground and background independently without retaining prior cell colors", async () => {
260 const core = await createCore();
261 core.write(
262 "\x1b[38;2;123;45;67mF\x1b[39;48;2;9;8;7mB\x1b[0mD\x1b[38;2;12;34;56;48;2;78;90;123mC\x1b[0mE",
263 );
264 expect(
265 core
266 .snapshot()
267 .rowData[0]!.cells.slice(0, 6)
268 .map(({ text, foreground, background }) => ({ text, foreground, background })),
269 ).toEqual([
270 { text: "F", foreground: { r: 123, g: 45, b: 67 }, background: { r: 0, g: 0, b: 0 } },
271 { text: "B", foreground: { r: 255, g: 255, b: 255 }, background: { r: 9, g: 8, b: 7 } },
272 { text: "D", foreground: { r: 255, g: 255, b: 255 }, background: { r: 0, g: 0, b: 0 } },
273 { text: "C", foreground: { r: 12, g: 34, b: 56 }, background: { r: 78, g: 90, b: 123 } },
274 { text: "E", foreground: { r: 255, g: 255, b: 255 }, background: { r: 0, g: 0, b: 0 } },
275 { text: "", foreground: { r: 255, g: 255, b: 255 }, background: { r: 0, g: 0, b: 0 } },
276 ]);
277 });
278
279 it("paints ANSI colors from the theme palette and repaints them when the theme or a program changes colors", async () => {
280 const core = await createCore();
281 const theme = {
282 foreground: { r: 255, g: 255, b: 255 },
283 background: { r: 0, g: 0, b: 0 },
284 cursor: { r: 255, g: 255, b: 255 },
285 };
286 const palette = Array.from({ length: 16 }, (_, index) => ({ r: index, g: 1, b: 2 }));
287 core.write("\x1b[31mR\x1b[38;5;196mX\x1b[0m");
288 core.setTheme({ ...theme, palette });
289 let cells = core.snapshot().rowData[0]!.cells;
290 expect(cells[0]!.foreground).toEqual({ r: 1, g: 1, b: 2 });
291 expect(cells[1]!.foreground).toEqual({ r: 255, g: 0, b: 0 });
292
293 core.setTheme({ ...theme, palette: palette.map((color) => ({ ...color, g: 9 })) });
294 cells = core.snapshot().rowData[0]!.cells;
295 expect(cells[0]!.foreground).toEqual({ r: 1, g: 9, b: 2 });
296 expect(cells[1]!.foreground).toEqual({ r: 255, g: 0, b: 0 });
297
298 // Inverse, default and explicit-background cells, cached before a program sets OSC 10/11.
299 core.resetAndWrite("\x1b[7mI\x1b[0mD\x1b[48;2;9;8;7mB\x1b[0m\r\n");
300 core.snapshot();
301 const colors = () => {
302 const { background, rowData } = core.snapshot();
303 return {
304 background,
305 cells: rowData[0]!.cells
306 .slice(0, 3)
307 .map(({ foreground, background }) => ({ foreground, background })),
308 };
309 };
310 const programForeground = { r: 17, g: 34, b: 51 };
311 const programBackground = { r: 68, g: 85, b: 102 };
312 const explicitBackground = { r: 9, g: 8, b: 7 };
313 core.write("\x1b]10;rgb:11/22/33\x07\x1b]11;rgb:44/55/66\x07");
314 expect(colors()).toEqual({
315 background: programBackground,
316 cells: [
317 { foreground: programBackground, background: programForeground },
318 { foreground: programForeground, background: programBackground },
319 { foreground: programForeground, background: explicitBackground },
320 ],
321 });
322
323 core.write("\x1b]110\x07\x1b]111\x07");
324 expect(colors()).toEqual({
325 background: theme.background,
326 cells: [
327 { foreground: theme.background, background: theme.foreground },
328 { foreground: theme.foreground, background: theme.background },
329 { foreground: theme.foreground, background: explicitBackground },
330 ],
331 });
332 });
333
334 it("preserves Unicode and split ANSI parser state across batched renderer reads", async () => {
335 const [core, reference] = await Promise.all([createCore(), createCore()]);
336 let state = createSession("");
337 const first = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR);
338 writeTerminalOutputUpdate(core, first);
339 let cursor = first.cursor;
340 const reset = vi.spyOn(core, "resetAndWrite");
341 const inputs = [
342 `${"a".repeat(16_383)}🙂`,
343 "\x1b[3",
344 "1m",
345 "e",
346 "\u0301界🙂",
347 "\x1b[0",
348 "m\r\n",
349 "\x1b]8;;https://t3.codes\x1b",
350 "\\link",
351 "\x1b]8;;\x1b",
352 "\\\x1b[?1049h",
353 "alternate",
354 "\x1b[?1049l",
355 "\r\nend",
356 ];
357 let received = "";
358 for (const [index, data] of inputs.entries()) {
359 state = append(state, data);
360 if (index % 3 !== 0 && index !== inputs.length - 1) continue;
361 const update = readTerminalOutputUpdate(state.output, cursor);
362 if (update.type !== "append") throw new Error(`Expected append, received ${update.type}`);
363 received += update.data;
364 writeTerminalOutputUpdate(core, update);
365 cursor = update.cursor;
366 }
367
368 reference.write(inputs.join(""));
369 expect(received).toBe(inputs.join(""));
370 expect(reset).not.toHaveBeenCalled();
371 expect(core.snapshot()).toEqual(reference.snapshot());
372 });
373
374 it("answers an OSC 11 background query with the theme background", async () => {
375 const replies: string[] = [];
376 const core = await createCore((data) => replies.push(data));
377 core.setTheme({
378 foreground: { r: 0, g: 0, b: 0 },
379 background: { r: 255, g: 255, b: 255 },
380 cursor: { r: 0, g: 0, b: 0 },
381 });
382 core.write("\x1b]11;?\x1b\\");
383 expect(replies.join("")).toMatch(/^\x1b\]11;rgb:ffff\/ffff\/ffff(\x07|\x1b\\)$/);
384 });
385
386 it("answers a live VT query when batched reads cross a chunk compaction", async () => {
387 const replies: string[] = [];
388 const core = await createCore((data) => replies.push(data));
389 core.write("\x1b[5n");
390 expect(replies).toEqual(["\x1b[0n"]);
391 replies.length = 0;
392
393 let state = createSession("");
394 const initial = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR);
395 writeTerminalOutputUpdate(core, initial);
396 let cursor = initial.cursor;
397 for (let index = 0; index < 1000; index += 1) {
398 state = append(state, "x");
399 const update = readTerminalOutputUpdate(state.output, cursor);
400 writeTerminalOutputUpdate(core, update);
401 cursor = update.cursor;
402 }
403 for (let index = 0; index < 24; index += 1) state = append(state, "x");
404 state = append(state, "\x1b[5n");
405 const update = readTerminalOutputUpdate(state.output, cursor);
406 writeTerminalOutputUpdate(core, update);
407
408 expect({ type: update.type, replies }).toEqual({ type: "append", replies: ["\x1b[0n"] });
409 });
410
411 it("recovers a lagging renderer once from bounded output and resumes appending", async () => {
412 const [core, reference] = await Promise.all([createCore(), createCore()]);
413 let state = createSession("\x1b[31mold");
414 const initial = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR);
415 writeTerminalOutputUpdate(core, initial);
416 const reset = vi.spyOn(core, "resetAndWrite");
417 const data = "line\r\n".repeat(8192);
418 for (let index = 0; index < 16; index += 1) state = append(state, data);
419
420 const recovery = readTerminalOutputUpdate(state.output, initial.cursor);
421 if (recovery.type !== "reset") throw new Error(`Expected reset, received ${recovery.type}`);
422 expect(new TextEncoder().encode(recovery.data).byteLength).toBe(
423 DEFAULT_MAX_TERMINAL_BUFFER_BYTES,
424 );
425 writeTerminalOutputUpdate(core, recovery);
426 reference.resetAndWrite(recovery.data);
427 expect(core.snapshot()).toEqual(reference.snapshot());
428
429 state = append(state, "\r\nlatest");
430 const next = readTerminalOutputUpdate(state.output, recovery.cursor);
431 expect(next.type).toBe("append");
432 writeTerminalOutputUpdate(core, next);
433 reference.write("\r\nlatest");
434 expect(reset).toHaveBeenCalledTimes(1);
435 expect(core.snapshot()).toEqual(reference.snapshot());
436 });
437
438 it("replays the latest retained output when WASM arrives after several events", async () => {
439 const pendingCore = createCore();
440 let state = createSession("before");
441 state = append(state, "\r\nduring ");
442 state = append(state, "🙂 load");
443 const core = await pendingCore;
444 const first = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR);
445 writeTerminalOutputUpdate(core, first);
446 const reference = await createCore();
447 reference.resetAndWrite(terminalOutputText(state.output));
448 const reset = vi.spyOn(core, "resetAndWrite");
449
450 state = append(state, "\r\nafter");
451 const next = readTerminalOutputUpdate(state.output, first.cursor);
452 expect(next).toMatchObject({ type: "append", data: "\r\nafter" });
453 writeTerminalOutputUpdate(core, next);
454 reference.write("\r\nafter");
455 expect(reset).not.toHaveBeenCalled();
456 expect(core.snapshot()).toEqual(reference.snapshot());
457 });
458
459 it("drops stale text in real Ghostty for a repeated snapshot, clear, restart, and a fresh attach", async () => {
460 const [core, reference] = await Promise.all([createCore(), createCore()]);
461 let state = createSession("hello");
462 const initial = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR);
463 writeTerminalOutputUpdate(core, initial);
464 let cursor = initial.cursor;
465
466 const snapshot = {
467 threadId: "terminal-stream-test",
468 terminalId: "term-1",
469 cwd: "/repo",
470 worktreePath: null,
471 status: "running" as const,
472 pid: 456,
473 history: "hello",
474 exitCode: null,
475 exitSignal: null,
476 label: "Terminal",
477 updatedAt: "2026-09-04T00:00:01.000Z",
478 };
479 const events = [
480 { type: "snapshot", snapshot },
481 { type: "cleared", threadId: snapshot.threadId, terminalId: snapshot.terminalId },
482 { type: "restarted", threadId: snapshot.threadId, terminalId: snapshot.terminalId, snapshot },
483 ] as const;
484 for (const event of events) {
485 core.write("\r\nstale local text");
486 state = applyTerminalAttachStreamEvent(state, event);
487 const update = readTerminalOutputUpdate(state.output, cursor);
488 expect(update.type).toBe(event.type === "cleared" ? "append" : "reset");
489 writeTerminalOutputUpdate(core, update);
490 cursor = update.cursor;
491 reference.resetAndWrite(event.type === "cleared" ? "" : "hello");
492 expect(core.snapshot()).toEqual(reference.snapshot());
493 }
494
495 core.write("\r\nstale local text");
496 state = createSession("hello");
497 const reattached = readTerminalOutputUpdate(state.output, cursor);
498 expect(reattached.type).toBe("reset");
499 writeTerminalOutputUpdate(core, reattached);
500 reference.resetAndWrite("hello");
501 expect(core.snapshot()).toEqual(reference.snapshot());
502 });
503
504 it("keeps a running program's screen and input modes when its history is cleared", async () => {
505 const replies: string[] = [];
506 const core = await createCore((data) => replies.push(data));
507 const modes = () => {
508 replies.length = 0;
509 core.write("\x1b[?u");
510 return {
511 alternateScreen: core.isAlternateScreen(),
512 applicationCursorKeys: core.isApplicationCursorKeys(),
513 mouseTracking: core.isMouseTracking(),
514 kittyFlags: replies.join(""),
515 };
516 };
517 let state = createSession("\x1b[?1049h\x1b[>7u\x1b[?1;1000;1006h");
518 const initial = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR);
519 writeTerminalOutputUpdate(core, initial);
520 const live = modes();
521 expect(live).toEqual({
522 alternateScreen: true,
523 applicationCursorKeys: true,
524 mouseTracking: true,
525 kittyFlags: "\x1b[?7u",
526 });
527
528 state = applyTerminalAttachStreamEvent(state, {
529 type: "cleared",
530 threadId: "terminal-stream-test",
531 terminalId: "term-1",
532 });
533 writeTerminalOutputUpdate(core, readTerminalOutputUpdate(state.output, initial.cursor));
534
535 expect(modes()).toEqual(live);
536 });
537 });
538
539 describe("GhosttyTerminalCore scrollback", () => {
540 it("retains thousands of rows of scrollback", async () => {
541 const core = await GhosttyTerminalCore.create(
542 80,
543 10,
544 8,
545 16,
546 {
547 foreground: { r: 255, g: 255, b: 255 },
548 background: { r: 0, g: 0, b: 0 },
549 cursor: { r: 255, g: 255, b: 255 },
550 },
551 () => {},
552 );
553 try {
554 core.write(Array.from({ length: 1_200 }, (_, index) => `${index}\r\n`).join(""));
555 const scrollbar = core.scrollbarState();
556 // max_scrollback is a byte budget in the pinned ABI, not a row count.
557 expect(scrollbar!.total - scrollbar!.len).toBeGreaterThan(1_000);
558 } finally {
559 core.dispose();
560 }
561 });
562 });
563
564 describe("GhosttyTerminalCore mouse encoding", () => {
565 it("reports the cell drawn under the pointer on a fractional grid", async () => {
566 // The default 13px font measures a fractional 7.82666015625px cell; rows are 18px.
567 const cellWidth = 7.82666015625;
568 const cellHeight = 18;
569 const core = await GhosttyTerminalCore.create(
570 54,
571 15,
572 cellWidth,
573 cellHeight,
574 {
575 foreground: { r: 255, g: 255, b: 255 },
576 background: { r: 0, g: 0, b: 0 },
577 cursor: { r: 255, g: 255, b: 255 },
578 },
579 () => {},
580 );
581 const report = (
582 x: number,
583 y: number,
584 paddingTop: number,
585 screenWidth = 437,
586 action: "press" | "release" = "press",
587 ) =>
588 core.encodeMouse({
589 action,
590 button: 1,
591 mods: 0,
592 x,
593 y,
594 screenWidth,
595 screenHeight: 279,
596 cellWidth,
597 cellHeight,
598 paddingLeft: 4,
599 paddingRight: 4,
600 paddingTop,
601 paddingBottom: 279 - paddingTop - 15 * cellHeight,
602 anyButtonPressed: true,
603 });
604 const mouse = (...args: Parameters<typeof report>) => report(...args).data;
605 try {
606 core.write("\x1b[?1002h\x1b[?1006h");
607 expect(mouse(218.5, 140, 4)).toBe("\x1b[<0;28;8M");
608 expect(mouse(4 + 53.5 * cellWidth, 140, 4)).toBe("\x1b[<0;54;8M");
609 expect(mouse(10, 4.5 + cellHeight + 0.2, 4.5)).toBe("\x1b[<0;1;2M");
610 expect(mouse(304, 140, 4, 305)).toBe("\x1b[<0;37;8M");
611 expect(mouse(500, 140, 4, 305, "release")).toBe("\x1b[<0;37;8m");
612 core.write("\x1b[?1016h");
613 expect(mouse(218.5, 22, 4.5)).toBe("\x1b[<0;215;17M");
614 core.resize(160, 15, cellWidth, cellHeight);
615 core.write("\x1b[?1002;1006;1016l\x1b[?1000h");
616 expect(report(4 + 99.5 * cellWidth, 140, 4, 1261)).toEqual({
617 data: "\x1b[M \x84(",
618 binary: true,
619 });
620 } finally {
621 core.dispose();
622 }
623 });
624 });