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

runtimeAbi.test.ts

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