Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import wasmDataUrl from "./vendor/ghostty-vt.wasm?inline";
4import writePtyWasmDataUrl from "./vendor/ghostty-write-pty.wasm?inline";
5import pinnedVersion from "../../../../../native/libghostty-vt/VERSION?raw";
6import { ghosttyKeyForCode } from "./keyCodes";
7
8type WasmFunction = (...args: number[]) => number;
9
10function decodeWasmDataUrl(dataUrl: string): Uint8Array {
11 const encoded = dataUrl.split(",", 2)[1];
12 if (!encoded) throw new Error("The vendored Ghostty WASM data URL is invalid");
13 return Uint8Array.from(atob(encoded), (character) => character.charCodeAt(0));
14}
15
16describe("vendored libghostty-vt WebAssembly", () => {
17 it("stays pinned to the canonical revision and size budget", async () => {
18 const wasm = decodeWasmDataUrl(wasmDataUrl);
19 expect(wasm.byteLength).toBeLessThan(750_000);
20
21 // The artifact carries its own provenance: the build embeds the pinned
22 // revision as semver build metadata, so the repository's canonical VERSION
23 // file is the single source of truth and drift is caught here without a copy.
24 const result = await WebAssembly.instantiate(wasm.buffer as ArrayBuffer, {
25 env: { log: () => {} },
26 });
27 const instance = result instanceof WebAssembly.Instance ? result : result.instance;
28 const memory = instance.exports.memory as WebAssembly.Memory;
29 const call = (name: string, ...args: number[]) =>
30 (instance.exports[name] as WasmFunction)(...args);
31 const out = call("ghostty_wasm_alloc_u8_array", 8);
32 expect(call("ghostty_build_info", 10, out)).toBe(0);
33 const view = new DataView(memory.buffer, out, 8);
34 const embeddedRevision = new TextDecoder().decode(
35 new Uint8Array(memory.buffer, view.getUint32(0, true), view.getUint32(4, true)),
36 );
37 call("ghostty_wasm_free_u8_array", out, 8);
38 expect(embeddedRevision).toBe(pinnedVersion.trim());
39 });
40
41 it("creates, writes multi-codepoint graphemes, and frees repeated terminals", async () => {
42 const bytes = decodeWasmDataUrl(wasmDataUrl);
43 let memory: WebAssembly.Memory | null = null;
44 const instantiated = await WebAssembly.instantiate(bytes.buffer as ArrayBuffer, {
45 env: {
46 log: () => {},
47 },
48 });
49 const instance =
50 instantiated instanceof WebAssembly.Instance ? instantiated : instantiated.instance;
51 const exports = instance.exports;
52 memory = exports.memory as WebAssembly.Memory;
53 const call = (name: string, ...args: number[]) => (exports[name] as WasmFunction)(...args);
54 const jsonPointer = call("ghostty_type_json");
55 const jsonBytes = new Uint8Array(memory.buffer, jsonPointer);
56 const jsonEnd = jsonBytes.indexOf(0);
57 const layouts = JSON.parse(new TextDecoder().decode(jsonBytes.subarray(0, jsonEnd))) as Record<
58 string,
59 { size: number }
60 >;
61 const optionsSize = layouts.GhosttyTerminalOptions?.size;
62 expect(optionsSize).toBe(8);
63 if (optionsSize === undefined) throw new Error("GhosttyTerminalOptions layout is missing");
64
65 for (let iteration = 0; iteration < 25; iteration += 1) {
66 const options = call("ghostty_wasm_alloc_u8_array", optionsSize);
67 const optionsView = new DataView(memory.buffer, options, optionsSize);
68 optionsView.setUint16(0, 80, true);
69 optionsView.setUint16(2, 24, true);
70 optionsView.setUint32(4, 5_000, true);
71 const terminalSlot = call("ghostty_wasm_alloc_opaque");
72 expect(call("ghostty_terminal_new", 0, terminalSlot, options)).toBe(0);
73 const terminal = new DataView(memory.buffer).getUint32(terminalSlot, true);
74
75 const input = new TextEncoder().encode("e\u0301 👨👩👧👦 العربية\r\n");
76 const inputPointer = call("ghostty_wasm_alloc_u8_array", input.length);
77 new Uint8Array(memory.buffer, inputPointer, input.length).set(input);
78 call("ghostty_terminal_vt_write", terminal, inputPointer, input.length);
79 call("ghostty_wasm_free_u8_array", inputPointer, input.length);
80 call("ghostty_terminal_free", terminal);
81 call("ghostty_wasm_free_opaque", terminalSlot);
82 call("ghostty_wasm_free_u8_array", options, optionsSize);
83 }
84 });
85
86 it("blinks the default cursor until a program asks for a steady one", async () => {
87 const result = await WebAssembly.instantiate(
88 decodeWasmDataUrl(wasmDataUrl).buffer as ArrayBuffer,
89 { env: { log: () => {} } },
90 );
91 const instance = result instanceof WebAssembly.Instance ? result : result.instance;
92 const memory = instance.exports.memory as WebAssembly.Memory;
93 const call = (name: string, ...args: number[]) =>
94 (instance.exports[name] as WasmFunction)(...args);
95 const alloc = (size: number) => call("ghostty_wasm_alloc_u8_array", size);
96 const options = alloc(8);
97 const optionsView = new DataView(memory.buffer, options, 8);
98 optionsView.setUint16(0, 80, true);
99 optionsView.setUint16(2, 24, true);
100 const terminalSlot = call("ghostty_wasm_alloc_opaque");
101 expect(call("ghostty_terminal_new", 0, terminalSlot, options)).toBe(0);
102 const terminal = new DataView(memory.buffer).getUint32(terminalSlot, true);
103 const renderStateSlot = call("ghostty_wasm_alloc_opaque");
104 expect(call("ghostty_render_state_new", 0, renderStateSlot)).toBe(0);
105 const renderState = new DataView(memory.buffer).getUint32(renderStateSlot, true);
106 const scratch = alloc(4);
107
108 const blinking = () => {
109 expect(call("ghostty_render_state_update", renderState, terminal)).toBe(0);
110 expect(call("ghostty_render_state_get", renderState, 12, scratch)).toBe(0);
111 return new DataView(memory.buffer, scratch, 4).getUint8(0) !== 0;
112 };
113 const write = (data: string) => {
114 const bytes = new TextEncoder().encode(data);
115 const pointer = alloc(bytes.length);
116 new Uint8Array(memory.buffer, pointer, bytes.length).set(bytes);
117 call("ghostty_terminal_vt_write", terminal, pointer, bytes.length);
118 call("ghostty_wasm_free_u8_array", pointer, bytes.length);
119 };
120 const setDefaultCursorBlink = (blink: boolean) => {
121 const value = alloc(1);
122 new Uint8Array(memory.buffer, value, 1)[0] = blink ? 1 : 0;
123 expect(call("ghostty_terminal_set", terminal, 23, value)).toBe(0);
124 call("ghostty_wasm_free_u8_array", value, 1);
125 };
126
127 // Ghostty's own default is a steady cursor, so the blink the web terminal
128 // inherited from xterm.js only exists because option 23 asks for it.
129 expect(blinking()).toBe(false);
130 setDefaultCursorBlink(true);
131 expect(blinking()).toBe(true);
132
133 // Programs still own the cursor: DECSCUSR steady block and DEC mode 12 both
134 // stop the blink, and DECSCUSR reset returns to the embedder default.
135 write("\u001b[2 q");
136 expect(blinking()).toBe(false);
137 write("\u001b[0 q");
138 expect(blinking()).toBe(true);
139 write("\u001b[?12l");
140 expect(blinking()).toBe(false);
141 write("\u001b[?12h");
142 expect(blinking()).toBe(true);
143
144 // RIS restores Ghostty's built-in steady default rather than the embedder's,
145 // which is why the core reapplies the option around a session replay.
146 call("ghostty_terminal_reset", terminal);
147 expect(blinking()).toBe(false);
148 setDefaultCursorBlink(true);
149 expect(blinking()).toBe(true);
150
151 call("ghostty_wasm_free_u8_array", scratch, 4);
152 call("ghostty_render_state_free", renderState);
153 call("ghostty_wasm_free_opaque", renderStateSlot);
154 call("ghostty_terminal_free", terminal);
155 call("ghostty_wasm_free_opaque", terminalSlot);
156 call("ghostty_wasm_free_u8_array", options, 8);
157 });
158
159 it("reports and scrolls the viewport with Ghostty's scrollbar state", async () => {
160 const result = await WebAssembly.instantiate(
161 decodeWasmDataUrl(wasmDataUrl).buffer as ArrayBuffer,
162 { env: { log: () => {} } },
163 );
164 const instance = result instanceof WebAssembly.Instance ? result : result.instance;
165 const memory = instance.exports.memory as WebAssembly.Memory;
166 const call = (name: string, ...args: number[]) =>
167 (instance.exports[name] as WasmFunction)(...args);
168 const alloc = (size: number) => call("ghostty_wasm_alloc_u8_array", size);
169 const options = alloc(8);
170 const optionsView = new DataView(memory.buffer, options, 8);
171 optionsView.setUint16(0, 80, true);
172 optionsView.setUint16(2, 10, true);
173 optionsView.setUint32(4, 1_000, true);
174 const terminalSlot = call("ghostty_wasm_alloc_opaque");
175 expect(call("ghostty_terminal_new", 0, terminalSlot, options)).toBe(0);
176 const terminal = new DataView(memory.buffer).getUint32(terminalSlot, true);
177 const input = new TextEncoder().encode(
178 Array.from({ length: 50 }, (_, index) => `${index + 1}\r\n`).join(""),
179 );
180 const inputPointer = alloc(input.length);
181 new Uint8Array(memory.buffer, inputPointer, input.length).set(input);
182 call("ghostty_terminal_vt_write", terminal, inputPointer, input.length);
183
184 const scrollbar = alloc(24);
185 const scrollbarView = new DataView(memory.buffer, scrollbar, 24);
186 expect(call("ghostty_terminal_get", terminal, 9, scrollbar)).toBe(0);
187 expect([0, 8, 16].map((offset) => Number(scrollbarView.getBigUint64(offset, true)))).toEqual([
188 51, 41, 10,
189 ]);
190
191 const scroll = alloc(24);
192 const scrollView = new DataView(memory.buffer, scroll, 24);
193 scrollView.setUint32(0, 2, true);
194 scrollView.setInt32(8, -5, true);
195 call("ghostty_terminal_scroll_viewport", terminal, scroll);
196 expect(call("ghostty_terminal_get", terminal, 9, scrollbar)).toBe(0);
197 expect(Number(scrollbarView.getBigUint64(8, true))).toBe(36);
198
199 call("ghostty_wasm_free_u8_array", scroll, 24);
200 call("ghostty_wasm_free_u8_array", scrollbar, 24);
201 call("ghostty_wasm_free_u8_array", inputPointer, input.length);
202 call("ghostty_terminal_free", terminal);
203 call("ghostty_wasm_free_opaque", terminalSlot);
204 call("ghostty_wasm_free_u8_array", options, 8);
205 });
206
207 it("routes terminal-generated replies through the shared callback table", async () => {
208 const mainResult = await WebAssembly.instantiate(
209 decodeWasmDataUrl(wasmDataUrl).buffer as ArrayBuffer,
210 { env: { log: () => {} } },
211 );
212 const main = mainResult instanceof WebAssembly.Instance ? mainResult : mainResult.instance;
213 const memory = main.exports.memory as WebAssembly.Memory;
214 let reply = "";
215 const trampolineResult = await WebAssembly.instantiate(
216 decodeWasmDataUrl(writePtyWasmDataUrl).buffer as ArrayBuffer,
217 {
218 env: {
219 t3_write_pty: (_terminal: number, _userdata: number, pointer: number, length: number) => {
220 reply += new TextDecoder().decode(new Uint8Array(memory.buffer, pointer, length));
221 },
222 },
223 },
224 );
225 const trampoline =
226 trampolineResult instanceof WebAssembly.Instance
227 ? trampolineResult
228 : trampolineResult.instance;
229 const table = main.exports.__indirect_function_table as WebAssembly.Table;
230 const callbackIndex = table.length;
231 table.grow(1, trampoline.exports.ghostty_write_pty as CallableFunction);
232 const call = (name: string, ...args: number[]) => (main.exports[name] as WasmFunction)(...args);
233 const options = call("ghostty_wasm_alloc_u8_array", 8);
234 const optionsView = new DataView(memory.buffer, options, 8);
235 optionsView.setUint16(0, 80, true);
236 optionsView.setUint16(2, 24, true);
237 const terminalSlot = call("ghostty_wasm_alloc_opaque");
238 expect(call("ghostty_terminal_new", 0, terminalSlot, options)).toBe(0);
239 const terminal = new DataView(memory.buffer).getUint32(terminalSlot, true);
240 call("ghostty_terminal_set", terminal, 0, 1);
241 call("ghostty_terminal_set", terminal, 1, callbackIndex);
242 const query = new TextEncoder().encode("\u001b[5n");
243 const queryPointer = call("ghostty_wasm_alloc_u8_array", query.length);
244 new Uint8Array(memory.buffer, queryPointer, query.length).set(query);
245 call("ghostty_terminal_vt_write", terminal, queryPointer, query.length);
246
247 expect(reply).toBe("\u001b[0n");
248 reply = "";
249 call("ghostty_terminal_set", terminal, 1, 0);
250 call("ghostty_terminal_set", terminal, 0, 0);
251 call("ghostty_terminal_vt_write", terminal, queryPointer, query.length);
252 expect(reply).toBe("");
253 call("ghostty_terminal_set", terminal, 0, 1);
254 call("ghostty_terminal_set", terminal, 1, callbackIndex);
255 call("ghostty_terminal_vt_write", terminal, queryPointer, query.length);
256 expect(reply).toBe("\u001b[0n");
257 call("ghostty_wasm_free_u8_array", queryPointer, query.length);
258 call("ghostty_terminal_free", terminal);
259 call("ghostty_wasm_free_opaque", terminalSlot);
260 call("ghostty_wasm_free_u8_array", options, 8);
261 });
262
263 it("formats the active selection with Ghostty's copy semantics", async () => {
264 const result = await WebAssembly.instantiate(
265 decodeWasmDataUrl(wasmDataUrl).buffer as ArrayBuffer,
266 { env: { log: () => {} } },
267 );
268 const instance = result instanceof WebAssembly.Instance ? result : result.instance;
269 const memory = instance.exports.memory as WebAssembly.Memory;
270 const call = (name: string, ...args: number[]) =>
271 (instance.exports[name] as WasmFunction)(...args);
272 const options = call("ghostty_wasm_alloc_u8_array", 8);
273 const optionsView = new DataView(memory.buffer, options, 8);
274 optionsView.setUint16(0, 80, true);
275 optionsView.setUint16(2, 24, true);
276 const terminalSlot = call("ghostty_wasm_alloc_opaque");
277 expect(call("ghostty_terminal_new", 0, terminalSlot, options)).toBe(0);
278 const terminal = new DataView(memory.buffer).getUint32(terminalSlot, true);
279 const input = new TextEncoder().encode("a\r\n\r\nb");
280 const inputPointer = call("ghostty_wasm_alloc_u8_array", input.length);
281 new Uint8Array(memory.buffer, inputPointer, input.length).set(input);
282 call("ghostty_terminal_vt_write", terminal, inputPointer, input.length);
283
284 const selection = call("ghostty_wasm_alloc_u8_array", 32);
285 new DataView(memory.buffer, selection, 32).setUint32(0, 32, true);
286 expect(call("ghostty_terminal_select_all", terminal, selection)).toBe(0);
287 expect(call("ghostty_terminal_set", terminal, 21, selection)).toBe(0);
288 const formatOptions = call("ghostty_wasm_alloc_u8_array", 16);
289 const formatView = new DataView(memory.buffer, formatOptions, 16);
290 formatView.setUint32(0, 16, true);
291 formatView.setUint8(8, 1);
292 formatView.setUint8(9, 1);
293 const written = call("ghostty_wasm_alloc_usize");
294 expect(
295 call("ghostty_terminal_selection_format_buf", terminal, formatOptions, 0, 0, written),
296 ).toBe(-3);
297 const outputSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
298 const output = call("ghostty_wasm_alloc_u8_array", outputSize);
299 expect(
300 call(
301 "ghostty_terminal_selection_format_buf",
302 terminal,
303 formatOptions,
304 output,
305 outputSize,
306 written,
307 ),
308 ).toBe(0);
309 expect(new TextDecoder().decode(new Uint8Array(memory.buffer, output, outputSize))).toBe(
310 "a\n\nb",
311 );
312
313 call("ghostty_wasm_free_u8_array", output, outputSize);
314 call("ghostty_wasm_free_usize", written);
315 call("ghostty_wasm_free_u8_array", formatOptions, 16);
316 call("ghostty_wasm_free_u8_array", selection, 32);
317 call("ghostty_wasm_free_u8_array", inputPointer, input.length);
318 call("ghostty_terminal_free", terminal);
319 call("ghostty_wasm_free_opaque", terminalSlot);
320 call("ghostty_wasm_free_u8_array", options, 8);
321 });
322
323 it("uses Ghostty for mouse encoding, word selection, and OSC 8 hit testing", async () => {
324 const result = await WebAssembly.instantiate(
325 decodeWasmDataUrl(wasmDataUrl).buffer as ArrayBuffer,
326 { env: { log: () => {} } },
327 );
328 const instance = result instanceof WebAssembly.Instance ? result : result.instance;
329 const memory = instance.exports.memory as WebAssembly.Memory;
330 const call = (name: string, ...args: number[]) =>
331 (instance.exports[name] as WasmFunction)(...args);
332 const alloc = (size: number) => call("ghostty_wasm_alloc_u8_array", size);
333 const free = (pointer: number, size: number) =>
334 call("ghostty_wasm_free_u8_array", pointer, size);
335
336 const terminalOptions = alloc(8);
337 const terminalOptionsView = new DataView(memory.buffer, terminalOptions, 8);
338 terminalOptionsView.setUint16(0, 80, true);
339 terminalOptionsView.setUint16(2, 24, true);
340 const terminalSlot = call("ghostty_wasm_alloc_opaque");
341 expect(call("ghostty_terminal_new", 0, terminalSlot, terminalOptions)).toBe(0);
342 const terminal = new DataView(memory.buffer).getUint32(terminalSlot, true);
343 const input = new TextEncoder().encode(
344 "\u001b[?1000h\u001b[?1006h\u001b]8;;https://t3.codes/docs\u001b\\linked\u001b]8;;\u001b\\ plain",
345 );
346 const inputPointer = alloc(input.length);
347 new Uint8Array(memory.buffer, inputPointer, input.length).set(input);
348 call("ghostty_terminal_vt_write", terminal, inputPointer, input.length);
349
350 const modeFlag = alloc(1);
351 new Uint8Array(memory.buffer, modeFlag, 1)[0] = 0;
352 expect(call("ghostty_terminal_get", terminal, 11, modeFlag)).toBe(0);
353 expect(new Uint8Array(memory.buffer, modeFlag, 1)[0]).toBe(1);
354 new Uint8Array(memory.buffer, modeFlag, 1)[0] = 1;
355 expect(call("ghostty_terminal_mode_get", terminal, 1003, modeFlag)).toBe(0);
356 expect(new Uint8Array(memory.buffer, modeFlag, 1)[0]).toBe(0);
357 const anyEventInput = new TextEncoder().encode("\u001b[?1003h");
358 const anyEventPointer = alloc(anyEventInput.length);
359 new Uint8Array(memory.buffer, anyEventPointer, anyEventInput.length).set(anyEventInput);
360 call("ghostty_terminal_vt_write", terminal, anyEventPointer, anyEventInput.length);
361 expect(call("ghostty_terminal_mode_get", terminal, 1003, modeFlag)).toBe(0);
362 expect(new Uint8Array(memory.buffer, modeFlag, 1)[0]).toBe(1);
363 const anyEventReset = new TextEncoder().encode("\u001b[?1003l\u001b[?1000h");
364 const anyEventResetPointer = alloc(anyEventReset.length);
365 new Uint8Array(memory.buffer, anyEventResetPointer, anyEventReset.length).set(anyEventReset);
366 call("ghostty_terminal_vt_write", terminal, anyEventResetPointer, anyEventReset.length);
367 expect(call("ghostty_terminal_mode_get", terminal, 1003, modeFlag)).toBe(0);
368 expect(new Uint8Array(memory.buffer, modeFlag, 1)[0]).toBe(0);
369 free(anyEventResetPointer, anyEventReset.length);
370 free(anyEventPointer, anyEventInput.length);
371 free(modeFlag, 1);
372
373 const point = alloc(24);
374 const pointView = new DataView(memory.buffer, point, 24);
375 pointView.setUint32(0, 1, true);
376 pointView.setUint16(8, 1, true);
377 pointView.setUint32(12, 0, true);
378 const gridRef = alloc(12);
379 new DataView(memory.buffer, gridRef, 12).setUint32(0, 12, true);
380 expect(call("ghostty_terminal_grid_ref", terminal, point, gridRef)).toBe(0);
381
382 const written = call("ghostty_wasm_alloc_usize");
383 expect(call("ghostty_grid_ref_hyperlink_uri", gridRef, 0, 0, written)).toBe(-3);
384 const hyperlinkSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
385 const hyperlink = alloc(hyperlinkSize);
386 expect(call("ghostty_grid_ref_hyperlink_uri", gridRef, hyperlink, hyperlinkSize, written)).toBe(
387 0,
388 );
389 expect(new TextDecoder().decode(new Uint8Array(memory.buffer, hyperlink, hyperlinkSize))).toBe(
390 "https://t3.codes/docs",
391 );
392
393 const wordOptions = alloc(24);
394 const wordOptionsView = new DataView(memory.buffer, wordOptions, 24);
395 wordOptionsView.setUint32(0, 24, true);
396 new Uint8Array(memory.buffer, wordOptions + 4, 12).set(
397 new Uint8Array(memory.buffer, gridRef, 12),
398 );
399 const selection = alloc(32);
400 new DataView(memory.buffer, selection, 32).setUint32(0, 32, true);
401 expect(call("ghostty_terminal_select_word", terminal, wordOptions, selection)).toBe(0);
402 expect(call("ghostty_terminal_set", terminal, 21, selection)).toBe(0);
403 const formatOptions = alloc(16);
404 const formatView = new DataView(memory.buffer, formatOptions, 16);
405 formatView.setUint32(0, 16, true);
406 formatView.setUint8(8, 1);
407 formatView.setUint8(9, 1);
408 expect(
409 call("ghostty_terminal_selection_format_buf", terminal, formatOptions, 0, 0, written),
410 ).toBe(-3);
411 const selectionSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
412 const selectionText = alloc(selectionSize);
413 expect(
414 call(
415 "ghostty_terminal_selection_format_buf",
416 terminal,
417 formatOptions,
418 selectionText,
419 selectionSize,
420 written,
421 ),
422 ).toBe(0);
423 expect(
424 new TextDecoder().decode(new Uint8Array(memory.buffer, selectionText, selectionSize)),
425 ).toBe("linked");
426
427 const lineOptions = alloc(28);
428 const lineOptionsView = new DataView(memory.buffer, lineOptions, 28);
429 lineOptionsView.setUint32(0, 28, true);
430 new Uint8Array(memory.buffer, lineOptions + 4, 12).set(
431 new Uint8Array(memory.buffer, gridRef, 12),
432 );
433 expect(call("ghostty_terminal_select_line", terminal, lineOptions, selection)).toBe(0);
434 expect(call("ghostty_terminal_set", terminal, 21, selection)).toBe(0);
435 expect(
436 call("ghostty_terminal_selection_format_buf", terminal, formatOptions, 0, 0, written),
437 ).toBe(-3);
438 const lineSelectionSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
439 const lineSelectionText = alloc(lineSelectionSize);
440 expect(
441 call(
442 "ghostty_terminal_selection_format_buf",
443 terminal,
444 formatOptions,
445 lineSelectionText,
446 lineSelectionSize,
447 written,
448 ),
449 ).toBe(0);
450 expect(
451 new TextDecoder().decode(new Uint8Array(memory.buffer, lineSelectionText, lineSelectionSize)),
452 ).toBe("linked plain");
453
454 const mouseEncoderSlot = call("ghostty_wasm_alloc_opaque");
455 const mouseEventSlot = call("ghostty_wasm_alloc_opaque");
456 expect(call("ghostty_mouse_encoder_new", 0, mouseEncoderSlot)).toBe(0);
457 expect(call("ghostty_mouse_event_new", 0, mouseEventSlot)).toBe(0);
458 const view = new DataView(memory.buffer);
459 const mouseEncoder = view.getUint32(mouseEncoderSlot, true);
460 const mouseEvent = view.getUint32(mouseEventSlot, true);
461 call("ghostty_mouse_encoder_setopt_from_terminal", mouseEncoder, terminal);
462 const mouseSize = alloc(36);
463 const mouseSizeView = new DataView(memory.buffer, mouseSize, 36);
464 for (const [offset, value] of [
465 [0, 36],
466 [4, 800],
467 [8, 480],
468 [12, 10],
469 [16, 20],
470 ] as const) {
471 mouseSizeView.setUint32(offset, value, true);
472 }
473 call("ghostty_mouse_encoder_setopt", mouseEncoder, 2, mouseSize);
474 call("ghostty_mouse_event_set_action", mouseEvent, 0);
475 call("ghostty_mouse_event_set_button", mouseEvent, 1);
476 const mousePosition = new DataView(new ArrayBuffer(8));
477 mousePosition.setFloat32(0, 15, true);
478 mousePosition.setFloat32(4, 25, true);
479 const mousePositionPointer = alloc(8);
480 new Uint8Array(memory.buffer, mousePositionPointer, 8).set(
481 new Uint8Array(mousePosition.buffer),
482 );
483 call("ghostty_mouse_event_set_position", mouseEvent, mousePositionPointer);
484 const mouseOutput = alloc(128);
485 expect(
486 call("ghostty_mouse_encoder_encode", mouseEncoder, mouseEvent, mouseOutput, 128, written),
487 ).toBe(0);
488 const mouseOutputSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
489 expect(
490 new TextDecoder().decode(new Uint8Array(memory.buffer, mouseOutput, mouseOutputSize)),
491 ).toBe("\u001b[<0;2;2M");
492
493 call("ghostty_mouse_event_free", mouseEvent);
494 call("ghostty_mouse_encoder_free", mouseEncoder);
495 call("ghostty_wasm_free_opaque", mouseEventSlot);
496 call("ghostty_wasm_free_opaque", mouseEncoderSlot);
497 free(mousePositionPointer, 8);
498 free(mouseOutput, 128);
499 free(mouseSize, 36);
500 free(lineSelectionText, lineSelectionSize);
501 free(lineOptions, 28);
502 free(selectionText, selectionSize);
503 free(formatOptions, 16);
504 free(selection, 32);
505 free(wordOptions, 24);
506 free(hyperlink, hyperlinkSize);
507 call("ghostty_wasm_free_usize", written);
508 free(gridRef, 12);
509 free(point, 24);
510 free(inputPointer, input.length);
511 call("ghostty_terminal_free", terminal);
512 call("ghostty_wasm_free_opaque", terminalSlot);
513 free(terminalOptions, 8);
514 });
515
516 it("encodes modified printable keys in Kitty keyboard mode", async () => {
517 const result = await WebAssembly.instantiate(
518 decodeWasmDataUrl(wasmDataUrl).buffer as ArrayBuffer,
519 { env: { log: () => {} } },
520 );
521 const instance = result instanceof WebAssembly.Instance ? result : result.instance;
522 const memory = instance.exports.memory as WebAssembly.Memory;
523 const call = (name: string, ...args: number[]) =>
524 (instance.exports[name] as WasmFunction)(...args);
525 const alloc = (size: number) => call("ghostty_wasm_alloc_u8_array", size);
526 const free = (pointer: number, size: number) =>
527 call("ghostty_wasm_free_u8_array", pointer, size);
528
529 const terminalOptions = alloc(8);
530 const terminalOptionsView = new DataView(memory.buffer, terminalOptions, 8);
531 terminalOptionsView.setUint16(0, 80, true);
532 terminalOptionsView.setUint16(2, 24, true);
533 const terminalSlot = call("ghostty_wasm_alloc_opaque");
534 expect(call("ghostty_terminal_new", 0, terminalSlot, terminalOptions)).toBe(0);
535 const terminal = new DataView(memory.buffer).getUint32(terminalSlot, true);
536 const kittyMode = new TextEncoder().encode("\u001b[>1u");
537 const kittyModePointer = alloc(kittyMode.length);
538 new Uint8Array(memory.buffer, kittyModePointer, kittyMode.length).set(kittyMode);
539 call("ghostty_terminal_vt_write", terminal, kittyModePointer, kittyMode.length);
540
541 const encoderSlot = call("ghostty_wasm_alloc_opaque");
542 const eventSlot = call("ghostty_wasm_alloc_opaque");
543 expect(call("ghostty_key_encoder_new", 0, encoderSlot)).toBe(0);
544 expect(call("ghostty_key_event_new", 0, eventSlot)).toBe(0);
545 const view = new DataView(memory.buffer);
546 const keyEncoder = view.getUint32(encoderSlot, true);
547 const keyEvent = view.getUint32(eventSlot, true);
548 call("ghostty_key_encoder_setopt_from_terminal", keyEncoder, terminal);
549 call("ghostty_key_event_set_action", keyEvent, 1);
550 call("ghostty_key_event_set_key", keyEvent, ghosttyKeyForCode("KeyC"));
551 call("ghostty_key_event_set_mods", keyEvent, 1 << 1);
552 call("ghostty_key_event_set_consumed_mods", keyEvent, 0);
553 call("ghostty_key_event_set_composing", keyEvent, 0);
554 call("ghostty_key_event_set_unshifted_codepoint", keyEvent, "c".codePointAt(0)!);
555 const text = new TextEncoder().encode("c");
556 const textPointer = alloc(text.length);
557 new Uint8Array(memory.buffer, textPointer, text.length).set(text);
558 call("ghostty_key_event_set_utf8", keyEvent, textPointer, text.length);
559
560 const written = call("ghostty_wasm_alloc_usize");
561 expect(call("ghostty_key_encoder_encode", keyEncoder, keyEvent, 0, 0, written)).toBe(-3);
562 const outputSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
563 const output = alloc(outputSize);
564 expect(
565 call("ghostty_key_encoder_encode", keyEncoder, keyEvent, output, outputSize, written),
566 ).toBe(0);
567 const outputLength = new DataView(memory.buffer, written, 4).getUint32(0, true);
568 expect(new TextDecoder().decode(new Uint8Array(memory.buffer, output, outputLength))).toBe(
569 "\u001b[99;5u",
570 );
571
572 const remappedText = new TextEncoder().encode("j");
573 const remappedTextPointer = alloc(remappedText.length);
574 new Uint8Array(memory.buffer, remappedTextPointer, remappedText.length).set(remappedText);
575 call("ghostty_key_event_set_unshifted_codepoint", keyEvent, "j".codePointAt(0)!);
576 call("ghostty_key_event_set_utf8", keyEvent, remappedTextPointer, remappedText.length);
577 expect(call("ghostty_key_encoder_encode", keyEncoder, keyEvent, 0, 0, written)).toBe(-3);
578 const remappedOutputSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
579 const remappedOutput = alloc(remappedOutputSize);
580 expect(
581 call(
582 "ghostty_key_encoder_encode",
583 keyEncoder,
584 keyEvent,
585 remappedOutput,
586 remappedOutputSize,
587 written,
588 ),
589 ).toBe(0);
590 const remappedOutputLength = new DataView(memory.buffer, written, 4).getUint32(0, true);
591 expect(
592 new TextDecoder().decode(new Uint8Array(memory.buffer, remappedOutput, remappedOutputLength)),
593 ).toBe("\u001b[106;5u");
594
595 // Without the Kitty report-event-types flag a release encodes nothing, so
596 // the surface's keyup handler stays silent for legacy sessions.
597 call("ghostty_key_event_set_action", keyEvent, 0);
598 expect(call("ghostty_key_encoder_encode", keyEncoder, keyEvent, 0, 0, written)).toBe(0);
599 expect(new DataView(memory.buffer, written, 4).getUint32(0, true)).toBe(0);
600
601 // With report-event-types enabled the same release encodes an event-typed code.
602 const reportEvents = new TextEncoder().encode("\u001b[>3u");
603 const reportEventsPointer = alloc(reportEvents.length);
604 new Uint8Array(memory.buffer, reportEventsPointer, reportEvents.length).set(reportEvents);
605 call("ghostty_terminal_vt_write", terminal, reportEventsPointer, reportEvents.length);
606 call("ghostty_key_encoder_setopt_from_terminal", keyEncoder, terminal);
607 expect(call("ghostty_key_encoder_encode", keyEncoder, keyEvent, 0, 0, written)).toBe(-3);
608 const releaseSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
609 const releaseOutput = alloc(releaseSize);
610 expect(
611 call("ghostty_key_encoder_encode", keyEncoder, keyEvent, releaseOutput, releaseSize, written),
612 ).toBe(0);
613 const releaseLength = new DataView(memory.buffer, written, 4).getUint32(0, true);
614 expect(
615 new TextDecoder().decode(new Uint8Array(memory.buffer, releaseOutput, releaseLength)),
616 ).toBe("\u001b[106;5:3u");
617 free(releaseOutput, releaseSize);
618 free(reportEventsPointer, reportEvents.length);
619
620 free(remappedOutput, remappedOutputSize);
621 free(remappedTextPointer, remappedText.length);
622 free(output, outputSize);
623 call("ghostty_wasm_free_usize", written);
624 free(textPointer, text.length);
625 call("ghostty_key_event_free", keyEvent);
626 call("ghostty_key_encoder_free", keyEncoder);
627 call("ghostty_wasm_free_opaque", eventSlot);
628 call("ghostty_wasm_free_opaque", encoderSlot);
629 free(kittyModePointer, kittyMode.length);
630 call("ghostty_terminal_free", terminal);
631 call("ghostty_wasm_free_opaque", terminalSlot);
632 free(terminalOptions, 8);
633 });
634});