|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import { describe, it, expect, vi } from "vitest"; |
| 3 | +import { |
| 4 | + computeImeDelta, |
| 5 | + isForwardedInputType, |
| 6 | + setupImeReplacementBridge, |
| 7 | +} from "@/lib/ime"; |
| 8 | + |
| 9 | +const DEL = 0x7f; |
| 10 | +const enc = (s: string) => Array.from(new TextEncoder().encode(s)); |
| 11 | + |
| 12 | +// ── computeImeDelta ──────────────────────────────────────────────────── |
| 13 | +// Diffs the textarea value (code-point aware) into DEL backspaces + the new |
| 14 | +// tail. This is the core of the WebKit IME fix. |
| 15 | + |
| 16 | +describe("computeImeDelta", () => { |
| 17 | + it("encodes a fresh append from empty", () => { |
| 18 | + expect(computeImeDelta("", "아")).toEqual(enc("아")); |
| 19 | + }); |
| 20 | + |
| 21 | + it("returns nothing when unchanged", () => { |
| 22 | + expect(computeImeDelta("안", "안")).toEqual([]); |
| 23 | + expect(computeImeDelta("", "")).toEqual([]); |
| 24 | + }); |
| 25 | + |
| 26 | + it("replaces the composing syllable (ㅇ -> 아)", () => { |
| 27 | + expect(computeImeDelta("ㅇ", "아")).toEqual([DEL, ...enc("아")]); |
| 28 | + }); |
| 29 | + |
| 30 | + it("keeps the common prefix and only rewrites the tail (안ㄴ -> 안녀)", () => { |
| 31 | + // common prefix "안"; backspace the trailing ㄴ, send 녀. |
| 32 | + expect(computeImeDelta("안ㄴ", "안녀")).toEqual([DEL, ...enc("녀")]); |
| 33 | + }); |
| 34 | + |
| 35 | + it("handles Korean final-consonant migration (안 -> 아나)", () => { |
| 36 | + // The trailing ㄴ of 안 migrates to start the next syllable. Diffing the |
| 37 | + // whole value (not just the last char) gets this right: DEL 안, send 아나. |
| 38 | + expect(computeImeDelta("안", "아나")).toEqual([DEL, ...enc("아나")]); |
| 39 | + }); |
| 40 | + |
| 41 | + it("emits a bare backspace when the value shrinks", () => { |
| 42 | + expect(computeImeDelta("안녕", "안")).toEqual([DEL]); |
| 43 | + }); |
| 44 | + |
| 45 | + it("counts a surrogate-pair grapheme as ONE backspace", () => { |
| 46 | + // 👍 and 👋 are each a single code point spanning two UTF-16 units. A |
| 47 | + // naive .length diff would emit two backspaces; code-point diffing emits one. |
| 48 | + expect(computeImeDelta("👍", "👋")).toEqual([DEL, ...enc("👋")]); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +// ── isForwardedInputType ─────────────────────────────────────────────── |
| 53 | +// xterm forwards only "insertText"; everything composition-related it drops, |
| 54 | +// so those are the events the bridge must forward. |
| 55 | + |
| 56 | +describe("isForwardedInputType", () => { |
| 57 | + it("does NOT forward insertText (xterm handles it)", () => { |
| 58 | + expect(isForwardedInputType("insertText")).toBe(false); |
| 59 | + }); |
| 60 | + |
| 61 | + it("does NOT forward paste or line breaks (xterm/keydown handle them)", () => { |
| 62 | + expect(isForwardedInputType("insertFromPaste")).toBe(false); |
| 63 | + expect(isForwardedInputType("insertLineBreak")).toBe(false); |
| 64 | + expect(isForwardedInputType("insertParagraph")).toBe(false); |
| 65 | + }); |
| 66 | + |
| 67 | + it("forwards composition refinements and composition deletes", () => { |
| 68 | + expect(isForwardedInputType("insertReplacementText")).toBe(true); |
| 69 | + expect(isForwardedInputType("insertCompositionText")).toBe(true); |
| 70 | + expect(isForwardedInputType("deleteContentBackward")).toBe(true); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +// ── setupImeReplacementBridge (DOM) ──────────────────────────────────── |
| 75 | + |
| 76 | +function mountTerminal() { |
| 77 | + const host = document.createElement("div"); |
| 78 | + host.className = "xterm"; |
| 79 | + const ta = document.createElement("textarea"); |
| 80 | + ta.className = "xterm-helper-textarea"; |
| 81 | + host.appendChild(ta); |
| 82 | + document.body.appendChild(host); |
| 83 | + return { host, ta }; |
| 84 | +} |
| 85 | + |
| 86 | +function fireInput(ta: HTMLTextAreaElement, inputType: string, value: string, data: string | null) { |
| 87 | + ta.value = value; |
| 88 | + ta.dispatchEvent(new InputEvent("input", { inputType, data, bubbles: true })); |
| 89 | +} |
| 90 | + |
| 91 | +describe("setupImeReplacementBridge", () => { |
| 92 | + const PID = "pty-1"; |
| 93 | + |
| 94 | + it("forwards only the dropped events while typing 안녕, reconstructing it on the PTY", () => { |
| 95 | + const { host, ta } = mountTerminal(); |
| 96 | + const write = vi.fn(); |
| 97 | + setupImeReplacementBridge(host, () => PID, write); |
| 98 | + |
| 99 | + // The exact WebKit event sequence captured from the live app. |
| 100 | + fireInput(ta, "insertText", "ㅇ", "ㅇ"); // xterm forwards → bridge skips |
| 101 | + fireInput(ta, "insertReplacementText", "아", "아"); // dropped → bridge forwards |
| 102 | + fireInput(ta, "insertReplacementText", "안", "안"); |
| 103 | + fireInput(ta, "insertText", "안ㄴ", "ㄴ"); // xterm forwards → bridge skips |
| 104 | + fireInput(ta, "insertReplacementText", "안녀", "녀"); |
| 105 | + fireInput(ta, "insertReplacementText", "안녕", "녕"); |
| 106 | + |
| 107 | + // Only the 4 replacement events produce writes. |
| 108 | + expect(write).toHaveBeenCalledTimes(4); |
| 109 | + expect(write.mock.calls.map(c => c[1])).toEqual([ |
| 110 | + [DEL, ...enc("아")], // ㅇ -> 아 |
| 111 | + [DEL, ...enc("안")], // 아 -> 안 |
| 112 | + [DEL, ...enc("녀")], // 안ㄴ -> 안녀 |
| 113 | + [DEL, ...enc("녕")], // 안녀 -> 안녕 |
| 114 | + ]); |
| 115 | + |
| 116 | + // Replaying xterm's insertText sends + the bridge's writes onto a model |
| 117 | + // PTY line yields exactly "안녕". |
| 118 | + const xtermSends = ["ㅇ", "ㄴ"]; // what xterm forwards for the insertText events |
| 119 | + let line = [...Array.from(xtermSends[0])]; |
| 120 | + // Apply: bridge(아), bridge(안), xterm(ㄴ), bridge(녀), bridge(녕) |
| 121 | + const apply = (bytes: number[]) => { |
| 122 | + const text = new TextDecoder().decode(Uint8Array.from(bytes.filter(b => b !== DEL))); |
| 123 | + const backs = bytes.filter(b => b === DEL).length; |
| 124 | + for (let i = 0; i < backs; i++) line.pop(); |
| 125 | + line.push(...Array.from(text)); |
| 126 | + }; |
| 127 | + apply([DEL, ...enc("아")]); |
| 128 | + apply([DEL, ...enc("안")]); |
| 129 | + line.push(...Array.from("ㄴ")); // xterm insertText |
| 130 | + apply([DEL, ...enc("녀")]); |
| 131 | + apply([DEL, ...enc("녕")]); |
| 132 | + expect(line.join("")).toBe("안녕"); |
| 133 | + |
| 134 | + host.remove(); |
| 135 | + }); |
| 136 | + |
| 137 | + it("resets its baseline on Enter so the next composition does not backspace the old line", () => { |
| 138 | + const { host, ta } = mountTerminal(); |
| 139 | + const write = vi.fn(); |
| 140 | + setupImeReplacementBridge(host, () => PID, write); |
| 141 | + |
| 142 | + fireInput(ta, "insertText", "안녕", "녕"); // baseline := "안녕" |
| 143 | + write.mockClear(); |
| 144 | + |
| 145 | + // Enter: xterm sends \r and clears the textarea WITHOUT an input event. |
| 146 | + ta.value = ""; |
| 147 | + ta.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true })); |
| 148 | + |
| 149 | + // Next composition starts fresh. |
| 150 | + fireInput(ta, "insertReplacementText", "하", "하"); |
| 151 | + // No DEL for the (gone) old line — just the new char. |
| 152 | + expect(write).toHaveBeenCalledTimes(1); |
| 153 | + expect(write.mock.calls[0][1]).toEqual([...enc("하")]); |
| 154 | + |
| 155 | + host.remove(); |
| 156 | + }); |
| 157 | + |
| 158 | + it("stops forwarding after cleanup", () => { |
| 159 | + const { host, ta } = mountTerminal(); |
| 160 | + const write = vi.fn(); |
| 161 | + const dispose = setupImeReplacementBridge(host, () => PID, write); |
| 162 | + |
| 163 | + dispose(); |
| 164 | + fireInput(ta, "insertReplacementText", "아", "아"); |
| 165 | + expect(write).not.toHaveBeenCalled(); |
| 166 | + |
| 167 | + host.remove(); |
| 168 | + }); |
| 169 | + |
| 170 | + it("does not write when there is no PTY yet", () => { |
| 171 | + const { host, ta } = mountTerminal(); |
| 172 | + const write = vi.fn(); |
| 173 | + setupImeReplacementBridge(host, () => null, write); |
| 174 | + |
| 175 | + fireInput(ta, "insertReplacementText", "아", "아"); |
| 176 | + expect(write).not.toHaveBeenCalled(); |
| 177 | + |
| 178 | + host.remove(); |
| 179 | + }); |
| 180 | +}); |
0 commit comments