Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

runtimeAbi.test.ts

717 lines33.4 KB

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));
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("formats a cell-drag selection installed from screen grid refs", 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 const options = alloc(8);
336 new DataView(memory.buffer, options, 8).setUint16(0, 80, true);
337 new DataView(memory.buffer, options, 8).setUint16(2, 24, true);
338 const terminalSlot = call("ghostty_wasm_alloc_opaque");
339 expect(call("ghostty_terminal_new", 0, terminalSlot, options)).toBe(0);
340 const terminal = new DataView(memory.buffer).getUint32(terminalSlot, true);
341 const input = new TextEncoder().encode("hello\r\nworld");
342 const inputPointer = alloc(input.length);
343 new Uint8Array(memory.buffer, inputPointer, input.length).set(input);
344 call("ghostty_terminal_vt_write", terminal, inputPointer, input.length);
345​
346 const gridRefAt = (x: number, y: number) => {
347 const point = alloc(24);
348 const pointView = new DataView(memory.buffer, point, 24);
349 pointView.setUint32(0, 2, true);
350 pointView.setUint16(8, x, true);
351 pointView.setUint32(12, y, true);
352 const ref = alloc(12);
353 new DataView(memory.buffer, ref, 12).setUint32(0, 12, true);
354 expect(call("ghostty_terminal_grid_ref", terminal, point, ref)).toBe(0);
355 free(point, 24);
356 return ref;
357 };
358 const start = gridRefAt(0, 0);
359 const end = gridRefAt(4, 1);
360 const selection = alloc(32);
361 const selectionBytes = new Uint8Array(memory.buffer, selection, 32);
362 selectionBytes.fill(0);
363 new DataView(memory.buffer, selection, 32).setUint32(0, 32, true);
364 selectionBytes.set(new Uint8Array(memory.buffer, start, 12), 4);
365 selectionBytes.set(new Uint8Array(memory.buffer, end, 12), 16);
366 expect(call("ghostty_terminal_set", terminal, 21, selection)).toBe(0);
367​
368 const formatOptions = alloc(16);
369 new Uint8Array(memory.buffer, formatOptions, 16).fill(0);
370 const formatView = new DataView(memory.buffer, formatOptions, 16);
371 formatView.setUint32(0, 16, true);
372 formatView.setUint8(8, 1);
373 formatView.setUint8(9, 1);
374 const written = call("ghostty_wasm_alloc_usize");
375 expect(
376 call("ghostty_terminal_selection_format_buf", terminal, formatOptions, 0, 0, written),
377 ).toBe(-3);
378 const outputSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
379 const output = alloc(outputSize);
380 expect(
381 call(
382 "ghostty_terminal_selection_format_buf",
383 terminal,
384 formatOptions,
385 output,
386 outputSize,
387 written,
388 ),
389 ).toBe(0);
390 expect(new TextDecoder().decode(new Uint8Array(memory.buffer, output, outputSize))).toBe(
391 "hello\nworld",
392 );
393​
394 free(output, outputSize);
395 call("ghostty_wasm_free_usize", written);
396 free(formatOptions, 16);
397 free(selection, 32);
398 free(end, 12);
399 free(start, 12);
400 free(inputPointer, input.length);
401 call("ghostty_terminal_free", terminal);
402 call("ghostty_wasm_free_opaque", terminalSlot);
403 free(options, 8);
404 });
405​
406 it("uses Ghostty for mouse encoding, word selection, and OSC 8 hit testing", async () => {
407 const result = await WebAssembly.instantiate(
408 decodeWasmDataUrl(wasmDataUrl).buffer as ArrayBuffer,
409 { env: { log: () => {} } },
410 );
411 const instance = result instanceof WebAssembly.Instance ? result : result.instance;
412 const memory = instance.exports.memory as WebAssembly.Memory;
413 const call = (name: string, ...args: number[]) =>
414 (instance.exports[name] as WasmFunction)(...args);
415 const alloc = (size: number) => call("ghostty_wasm_alloc_u8_array", size);
416 const free = (pointer: number, size: number) =>
417 call("ghostty_wasm_free_u8_array", pointer, size);
418​
419 const terminalOptions = alloc(8);
420 const terminalOptionsView = new DataView(memory.buffer, terminalOptions, 8);
421 terminalOptionsView.setUint16(0, 80, true);
422 terminalOptionsView.setUint16(2, 24, true);
423 const terminalSlot = call("ghostty_wasm_alloc_opaque");
424 expect(call("ghostty_terminal_new", 0, terminalSlot, terminalOptions)).toBe(0);
425 const terminal = new DataView(memory.buffer).getUint32(terminalSlot, true);
426 const input = new TextEncoder().encode(
427 "\u001b[?1000h\u001b[?1006h\u001b]8;;https://t3.codes/docs\u001b\\linked\u001b]8;;\u001b\\ plain",
428 );
429 const inputPointer = alloc(input.length);
430 new Uint8Array(memory.buffer, inputPointer, input.length).set(input);
431 call("ghostty_terminal_vt_write", terminal, inputPointer, input.length);
432​
433 const modeFlag = alloc(1);
434 new Uint8Array(memory.buffer, modeFlag, 1)[0] = 0;
435 expect(call("ghostty_terminal_get", terminal, 11, modeFlag)).toBe(0);
436 expect(new Uint8Array(memory.buffer, modeFlag, 1)[0]).toBe(1);
437 new Uint8Array(memory.buffer, modeFlag, 1)[0] = 1;
438 expect(call("ghostty_terminal_mode_get", terminal, 1003, modeFlag)).toBe(0);
439 expect(new Uint8Array(memory.buffer, modeFlag, 1)[0]).toBe(0);
440 const anyEventInput = new TextEncoder().encode("\u001b[?1003h");
441 const anyEventPointer = alloc(anyEventInput.length);
442 new Uint8Array(memory.buffer, anyEventPointer, anyEventInput.length).set(anyEventInput);
443 call("ghostty_terminal_vt_write", terminal, anyEventPointer, anyEventInput.length);
444 expect(call("ghostty_terminal_mode_get", terminal, 1003, modeFlag)).toBe(0);
445 expect(new Uint8Array(memory.buffer, modeFlag, 1)[0]).toBe(1);
446 const anyEventReset = new TextEncoder().encode("\u001b[?1003l\u001b[?1000h");
447 const anyEventResetPointer = alloc(anyEventReset.length);
448 new Uint8Array(memory.buffer, anyEventResetPointer, anyEventReset.length).set(anyEventReset);
449 call("ghostty_terminal_vt_write", terminal, anyEventResetPointer, anyEventReset.length);
450 expect(call("ghostty_terminal_mode_get", terminal, 1003, modeFlag)).toBe(0);
451 expect(new Uint8Array(memory.buffer, modeFlag, 1)[0]).toBe(0);
452 free(anyEventResetPointer, anyEventReset.length);
453 free(anyEventPointer, anyEventInput.length);
454 free(modeFlag, 1);
455​
456 const point = alloc(24);
457 const pointView = new DataView(memory.buffer, point, 24);
458 pointView.setUint32(0, 1, true);
459 pointView.setUint16(8, 1, true);
460 pointView.setUint32(12, 0, true);
461 const gridRef = alloc(12);
462 new DataView(memory.buffer, gridRef, 12).setUint32(0, 12, true);
463 expect(call("ghostty_terminal_grid_ref", terminal, point, gridRef)).toBe(0);
464​
465 const written = call("ghostty_wasm_alloc_usize");
466 expect(call("ghostty_grid_ref_hyperlink_uri", gridRef, 0, 0, written)).toBe(-3);
467 const hyperlinkSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
468 const hyperlink = alloc(hyperlinkSize);
469 expect(call("ghostty_grid_ref_hyperlink_uri", gridRef, hyperlink, hyperlinkSize, written)).toBe(
470 0,
471 );
472 expect(new TextDecoder().decode(new Uint8Array(memory.buffer, hyperlink, hyperlinkSize))).toBe(
473 "https://t3.codes/docs",
474 );
475​
476 const wordOptions = alloc(24);
477 const wordOptionsView = new DataView(memory.buffer, wordOptions, 24);
478 wordOptionsView.setUint32(0, 24, true);
479 new Uint8Array(memory.buffer, wordOptions + 4, 12).set(
480 new Uint8Array(memory.buffer, gridRef, 12),
481 );
482 const selection = alloc(32);
483 new DataView(memory.buffer, selection, 32).setUint32(0, 32, true);
484 expect(call("ghostty_terminal_select_word", terminal, wordOptions, selection)).toBe(0);
485 expect(call("ghostty_terminal_set", terminal, 21, selection)).toBe(0);
486 const formatOptions = alloc(16);
487 const formatView = new DataView(memory.buffer, formatOptions, 16);
488 formatView.setUint32(0, 16, true);
489 formatView.setUint8(8, 1);
490 formatView.setUint8(9, 1);
491 expect(
492 call("ghostty_terminal_selection_format_buf", terminal, formatOptions, 0, 0, written),
493 ).toBe(-3);
494 const selectionSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
495 const selectionText = alloc(selectionSize);
496 expect(
497 call(
498 "ghostty_terminal_selection_format_buf",
499 terminal,
500 formatOptions,
501 selectionText,
502 selectionSize,
503 written,
504 ),
505 ).toBe(0);
506 expect(
507 new TextDecoder().decode(new Uint8Array(memory.buffer, selectionText, selectionSize)),
508 ).toBe("linked");
509​
510 const lineOptions = alloc(28);
511 const lineOptionsView = new DataView(memory.buffer, lineOptions, 28);
512 lineOptionsView.setUint32(0, 28, true);
513 new Uint8Array(memory.buffer, lineOptions + 4, 12).set(
514 new Uint8Array(memory.buffer, gridRef, 12),
515 );
516 expect(call("ghostty_terminal_select_line", terminal, lineOptions, selection)).toBe(0);
517 expect(call("ghostty_terminal_set", terminal, 21, selection)).toBe(0);
518 expect(
519 call("ghostty_terminal_selection_format_buf", terminal, formatOptions, 0, 0, written),
520 ).toBe(-3);
521 const lineSelectionSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
522 const lineSelectionText = alloc(lineSelectionSize);
523 expect(
524 call(
525 "ghostty_terminal_selection_format_buf",
526 terminal,
527 formatOptions,
528 lineSelectionText,
529 lineSelectionSize,
530 written,
531 ),
532 ).toBe(0);
533 expect(
534 new TextDecoder().decode(new Uint8Array(memory.buffer, lineSelectionText, lineSelectionSize)),
535 ).toBe("linked plain");
536​
537 const mouseEncoderSlot = call("ghostty_wasm_alloc_opaque");
538 const mouseEventSlot = call("ghostty_wasm_alloc_opaque");
539 expect(call("ghostty_mouse_encoder_new", 0, mouseEncoderSlot)).toBe(0);
540 expect(call("ghostty_mouse_event_new", 0, mouseEventSlot)).toBe(0);
541 const view = new DataView(memory.buffer);
542 const mouseEncoder = view.getUint32(mouseEncoderSlot, true);
543 const mouseEvent = view.getUint32(mouseEventSlot, true);
544 call("ghostty_mouse_encoder_setopt_from_terminal", mouseEncoder, terminal);
545 const mouseSize = alloc(36);
546 const mouseSizeView = new DataView(memory.buffer, mouseSize, 36);
547 for (const [offset, value] of [
548 [0, 36],
549 [4, 800],
550 [8, 480],
551 [12, 10],
552 [16, 20],
553 ] as const) {
554 mouseSizeView.setUint32(offset, value, true);
555 }
556 call("ghostty_mouse_encoder_setopt", mouseEncoder, 2, mouseSize);
557 call("ghostty_mouse_event_set_action", mouseEvent, 0);
558 call("ghostty_mouse_event_set_button", mouseEvent, 1);
559 const mousePosition = new DataView(new ArrayBuffer(8));
560 mousePosition.setFloat32(0, 15, true);
561 mousePosition.setFloat32(4, 25, true);
562 const mousePositionPointer = alloc(8);
563 new Uint8Array(memory.buffer, mousePositionPointer, 8).set(
564 new Uint8Array(mousePosition.buffer),
565 );
566 call("ghostty_mouse_event_set_position", mouseEvent, mousePositionPointer);
567 const mouseOutput = alloc(128);
568 expect(
569 call("ghostty_mouse_encoder_encode", mouseEncoder, mouseEvent, mouseOutput, 128, written),
570 ).toBe(0);
571 const mouseOutputSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
572 expect(
573 new TextDecoder().decode(new Uint8Array(memory.buffer, mouseOutput, mouseOutputSize)),
574 ).toBe("\u001b[<0;2;2M");
575​
576 call("ghostty_mouse_event_free", mouseEvent);
577 call("ghostty_mouse_encoder_free", mouseEncoder);
578 call("ghostty_wasm_free_opaque", mouseEventSlot);
579 call("ghostty_wasm_free_opaque", mouseEncoderSlot);
580 free(mousePositionPointer, 8);
581 free(mouseOutput, 128);
582 free(mouseSize, 36);
583 free(lineSelectionText, lineSelectionSize);
584 free(lineOptions, 28);
585 free(selectionText, selectionSize);
586 free(formatOptions, 16);
587 free(selection, 32);
588 free(wordOptions, 24);
589 free(hyperlink, hyperlinkSize);
590 call("ghostty_wasm_free_usize", written);
591 free(gridRef, 12);
592 free(point, 24);
593 free(inputPointer, input.length);
594 call("ghostty_terminal_free", terminal);
595 call("ghostty_wasm_free_opaque", terminalSlot);
596 free(terminalOptions, 8);
597 });
598​
599 it("encodes modified printable keys in Kitty keyboard mode", async () => {
600 const result = await WebAssembly.instantiate(
601 decodeWasmDataUrl(wasmDataUrl).buffer as ArrayBuffer,
602 { env: { log: () => {} } },
603 );
604 const instance = result instanceof WebAssembly.Instance ? result : result.instance;
605 const memory = instance.exports.memory as WebAssembly.Memory;
606 const call = (name: string, ...args: number[]) =>
607 (instance.exports[name] as WasmFunction)(...args);
608 const alloc = (size: number) => call("ghostty_wasm_alloc_u8_array", size);
609 const free = (pointer: number, size: number) =>
610 call("ghostty_wasm_free_u8_array", pointer, size);
611​
612 const terminalOptions = alloc(8);
613 const terminalOptionsView = new DataView(memory.buffer, terminalOptions, 8);
614 terminalOptionsView.setUint16(0, 80, true);
615 terminalOptionsView.setUint16(2, 24, true);
616 const terminalSlot = call("ghostty_wasm_alloc_opaque");
617 expect(call("ghostty_terminal_new", 0, terminalSlot, terminalOptions)).toBe(0);
618 const terminal = new DataView(memory.buffer).getUint32(terminalSlot, true);
619 const kittyMode = new TextEncoder().encode("\u001b[>1u");
620 const kittyModePointer = alloc(kittyMode.length);
621 new Uint8Array(memory.buffer, kittyModePointer, kittyMode.length).set(kittyMode);
622 call("ghostty_terminal_vt_write", terminal, kittyModePointer, kittyMode.length);
623​
624 const encoderSlot = call("ghostty_wasm_alloc_opaque");
625 const eventSlot = call("ghostty_wasm_alloc_opaque");
626 expect(call("ghostty_key_encoder_new", 0, encoderSlot)).toBe(0);
627 expect(call("ghostty_key_event_new", 0, eventSlot)).toBe(0);
628 const view = new DataView(memory.buffer);
629 const keyEncoder = view.getUint32(encoderSlot, true);
630 const keyEvent = view.getUint32(eventSlot, true);
631 call("ghostty_key_encoder_setopt_from_terminal", keyEncoder, terminal);
632 call("ghostty_key_event_set_action", keyEvent, 1);
633 call("ghostty_key_event_set_key", keyEvent, ghosttyKeyForCode("KeyC"));
634 call("ghostty_key_event_set_mods", keyEvent, 1 << 1);
635 call("ghostty_key_event_set_consumed_mods", keyEvent, 0);
636 call("ghostty_key_event_set_composing", keyEvent, 0);
637 call("ghostty_key_event_set_unshifted_codepoint", keyEvent, "c".codePointAt(0)!);
638 const text = new TextEncoder().encode("c");
639 const textPointer = alloc(text.length);
640 new Uint8Array(memory.buffer, textPointer, text.length).set(text);
641 call("ghostty_key_event_set_utf8", keyEvent, textPointer, text.length);
642​
643 const written = call("ghostty_wasm_alloc_usize");
644 expect(call("ghostty_key_encoder_encode", keyEncoder, keyEvent, 0, 0, written)).toBe(-3);
645 const outputSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
646 const output = alloc(outputSize);
647 expect(
648 call("ghostty_key_encoder_encode", keyEncoder, keyEvent, output, outputSize, written),
649 ).toBe(0);
650 const outputLength = new DataView(memory.buffer, written, 4).getUint32(0, true);
651 expect(new TextDecoder().decode(new Uint8Array(memory.buffer, output, outputLength))).toBe(
652 "\u001b[99;5u",
653 );
654​
655 const remappedText = new TextEncoder().encode("j");
656 const remappedTextPointer = alloc(remappedText.length);
657 new Uint8Array(memory.buffer, remappedTextPointer, remappedText.length).set(remappedText);
658 call("ghostty_key_event_set_unshifted_codepoint", keyEvent, "j".codePointAt(0)!);
659 call("ghostty_key_event_set_utf8", keyEvent, remappedTextPointer, remappedText.length);
660 expect(call("ghostty_key_encoder_encode", keyEncoder, keyEvent, 0, 0, written)).toBe(-3);
661 const remappedOutputSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
662 const remappedOutput = alloc(remappedOutputSize);
663 expect(
664 call(
665 "ghostty_key_encoder_encode",
666 keyEncoder,
667 keyEvent,
668 remappedOutput,
669 remappedOutputSize,
670 written,
671 ),
672 ).toBe(0);
673 const remappedOutputLength = new DataView(memory.buffer, written, 4).getUint32(0, true);
674 expect(
675 new TextDecoder().decode(new Uint8Array(memory.buffer, remappedOutput, remappedOutputLength)),
676 ).toBe("\u001b[106;5u");
677​
678 // Without the Kitty report-event-types flag a release encodes nothing, so
679 // the surface's keyup handler stays silent for legacy sessions.
680 call("ghostty_key_event_set_action", keyEvent, 0);
681 expect(call("ghostty_key_encoder_encode", keyEncoder, keyEvent, 0, 0, written)).toBe(0);
682 expect(new DataView(memory.buffer, written, 4).getUint32(0, true)).toBe(0);
683​
684 // With report-event-types enabled the same release encodes an event-typed code.
685 const reportEvents = new TextEncoder().encode("\u001b[>3u");
686 const reportEventsPointer = alloc(reportEvents.length);
687 new Uint8Array(memory.buffer, reportEventsPointer, reportEvents.length).set(reportEvents);
688 call("ghostty_terminal_vt_write", terminal, reportEventsPointer, reportEvents.length);
689 call("ghostty_key_encoder_setopt_from_terminal", keyEncoder, terminal);
690 expect(call("ghostty_key_encoder_encode", keyEncoder, keyEvent, 0, 0, written)).toBe(-3);
691 const releaseSize = new DataView(memory.buffer, written, 4).getUint32(0, true);
692 const releaseOutput = alloc(releaseSize);
693 expect(
694 call("ghostty_key_encoder_encode", keyEncoder, keyEvent, releaseOutput, releaseSize, written),
695 ).toBe(0);
696 const releaseLength = new DataView(memory.buffer, written, 4).getUint32(0, true);
697 expect(
698 new TextDecoder().decode(new Uint8Array(memory.buffer, releaseOutput, releaseLength)),
699 ).toBe("\u001b[106;5:3u");
700 free(releaseOutput, releaseSize);
701 free(reportEventsPointer, reportEvents.length);
702​
703 free(remappedOutput, remappedOutputSize);
704 free(remappedTextPointer, remappedText.length);
705 free(output, outputSize);
706 call("ghostty_wasm_free_usize", written);
707 free(textPointer, text.length);
708 call("ghostty_key_event_free", keyEvent);
709 call("ghostty_key_encoder_free", keyEncoder);
710 call("ghostty_wasm_free_opaque", eventSlot);
711 call("ghostty_wasm_free_opaque", encoderSlot);
712 free(kittyModePointer, kittyMode.length);
713 call("ghostty_terminal_free", terminal);
714 call("ghostty_wasm_free_opaque", terminalSlot);
715 free(terminalOptions, 8);
716 });
717});