// on its own engine. The engine's correctness is proven in // packages/tablet/engine/*.test.ts; these tests drive the ELEMENT through // synthetic events (happy-dom has no editing engine of its own — which the // architecture makes irrelevant: the engine owns editing, so a canceled // beforeinput + the mirror IS the typing path). True native typing is the // headless-Chromium smoke's job (examples/tablet-smoke.ts). import { afterEach, expect, test } from "bun:test"; import { define } from "@pebble/pebble"; import { CHANGE_DEBOUNCE, Tablet, type TabletElement } from "../packages/tablet/index"; const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); /** Mounts a ; with `text`, a server-rendered textarea child named "body". */ const mount = (text?: string): { el: TabletElement; field: HTMLTextAreaElement } => { const el = document.createElement("pebble-tablet") as TabletElement; if (text !== undefined) { const field = document.createElement("textarea"); field.name = "body"; field.value = text; el.appendChild(field); } document.body.appendChild(el); return { el, field: el.querySelector("textarea")! }; }; /** A cancelable beforeinput, as the browser would fire it. */ const beforeinput = (inputType: string, data: string | null = null) => new InputEvent("beforeinput", { inputType, data, bubbles: true, cancelable: true }); /** Focus the field and put the caret at `pos`. */ const focusAt = (field: HTMLTextAreaElement, pos: number) => { field.focus(); field.setSelectionRange(pos, pos); document.dispatchEvent(new Event("selectionchange")); }; afterEach(async () => { document.body.textContent = ""; await sleep(0); // define() defers releases one macrotask; flush them }); test("enhances a server-rendered textarea: same visible field, data-enhanced, initial doc", () => { const { el, field } = mount("server text"); expect(field.hidden).toBe(false); // the textarea IS the surface now expect(field.getAttribute("part")).toBe("editor field"); expect(el.getAttribute("data-enhanced")).toBe(""); expect(el.editor?.doc).toBe("server text"); }); test("typing: a canceled beforeinput applies through the engine to the field", () => { const { el, field } = mount("hello"); focusAt(field, 5); const event = beforeinput("insertText", " world"); field.dispatchEvent(event); const value = field.value; // read in the listener's task, where the mirror has written expect(event.defaultPrevented).toBe(true); // the engine took it expect(value).toBe("hello world"); // the mirror wrote it expect(el.editor?.doc).toBe("hello world"); }); test("the caret is the model's: projected back after an edit", async () => { const { field } = mount("hello"); focusAt(field, 0); field.dispatchEvent(beforeinput("insertText", "x")); await sleep(0); // the projection runs once the edit's writes settle expect(field.selectionStart).toBe(1); expect(field.selectionEnd).toBe(1); }); test("an inputType the engine declines is the browser's to make (not canceled)", () => { const { field } = mount("hello"); focusAt(field, 5); const event = beforeinput("insertFromPasteAsQuotation", "quoted"); // not in the engine's vocabulary field.dispatchEvent(event); expect(event.defaultPrevented).toBe(false); // declined: the native edit and the input reconcile own it expect(field.value).toBe("hello"); // untouched by the engine // (happy-dom coerces data:null to "", so the unreadable-text decline — // same filter branch — is covered by the engine's carriesText tests and // owned end-to-end by the Chromium smoke.) }); test("doc → form: a plain FormData read sends what was typed", () => { const form = document.createElement("form"); document.body.appendChild(form); const el = document.createElement("pebble-tablet") as TabletElement; const field = document.createElement("textarea"); field.name = "body"; field.value = "hello"; el.appendChild(field); form.appendChild(el); focusAt(field, 5); field.dispatchEvent(beforeinput("insertText", " world")); expect(new FormData(form).get("body")).toBe("hello world"); }); test("textarea → doc: a programmatic write to the field reconciles by diff", () => { const { el, field } = mount("hello"); field.value = "replaced"; field.dispatchEvent(new Event("input")); expect(el.editor?.doc).toBe("replaced"); // and the field was not fought: the mirror sees an equal value, writes nothing expect(field.value).toBe("replaced"); }); test("undo reaches past a programmatic write into what was typed before it", () => { const { el, field } = mount(""); focusAt(field, 0); field.dispatchEvent(beforeinput("insertText", "mine")); expect(field.value).toBe("mine"); field.value = "theirs"; field.dispatchEvent(new Event("input")); expect(field.value).toBe("theirs"); // Native textarea undo would be dead here (the value write cleared it). field.dispatchEvent(new KeyboardEvent("keydown", { key: "z", ctrlKey: true, bubbles: true, cancelable: true })); expect(field.value).toBe("mine"); expect(el.editor?.doc).toBe("mine"); }); test("redo restores what undo took", () => { const { field } = mount(""); focusAt(field, 0); field.dispatchEvent(beforeinput("insertText", "typed")); field.dispatchEvent(new KeyboardEvent("keydown", { key: "z", ctrlKey: true, bubbles: true, cancelable: true })); expect(field.value).toBe(""); field.dispatchEvent(new KeyboardEvent("keydown", { key: "z", ctrlKey: true, shiftKey: true, bubbles: true, cancelable: true })); expect(field.value).toBe("typed"); }); test("a run of typing undoes as a run, not a character at a time", () => { const { field } = mount(""); focusAt(field, 0); for (const char of "abc") field.dispatchEvent(beforeinput("insertText", char)); expect(field.value).toBe("abc"); field.dispatchEvent(new KeyboardEvent("keydown", { key: "z", ctrlKey: true, bubbles: true, cancelable: true })); expect(field.value).toBe(""); }); test("Mod+A selects the document through the engine", async () => { const { field } = mount("hello world"); focusAt(field, 3); field.dispatchEvent(new KeyboardEvent("keydown", { key: "a", ctrlKey: true, bubbles: true, cancelable: true })); await sleep(0); expect(field.selectionStart).toBe(0); expect(field.selectionEnd).toBe(11); }); test("change leaves debounced: rapid edits emit once, with the latest text", async () => { const { el, field } = mount("start"); focusAt(field, 5); const events: string[] = []; el.addEventListener("change", e => events.push((e as CustomEvent<{ value: string }>).detail.value)); field.dispatchEvent(beforeinput("insertText", " a")); await sleep(50); field.dispatchEvent(beforeinput("insertText", "b")); await sleep(CHANGE_DEBOUNCE - 150); // still inside the silence window expect(events).toEqual([]); await sleep(CHANGE_DEBOUNCE); // past it expect(events).toEqual(["start ab"]); }); test("no change event for the initial value", async () => { const { el } = mount("start"); const events: string[] = []; el.addEventListener("change", e => events.push((e as CustomEvent).type)); await sleep(CHANGE_DEBOUNCE + 100); expect(events).toEqual([]); }); test("Mod+S emits save with the text and prevents the browser default", () => { const { el, field } = mount("abc"); const saves: string[] = []; el.addEventListener("save", e => saves.push((e as CustomEvent<{ value: string }>).detail.value)); const chord = new KeyboardEvent("keydown", { key: "s", ctrlKey: true, bubbles: true, cancelable: true }); field.dispatchEvent(chord); expect(saves).toEqual(["abc"]); expect(chord.defaultPrevented).toBe(true); }); test("no chord resolves while an IME composition is in progress", () => { const { el, field } = mount("abc"); const saves: string[] = []; el.addEventListener("save", () => saves.push("save")); const chord = new KeyboardEvent("keydown", { key: "s", ctrlKey: true, isComposing: true, bubbles: true, cancelable: true, }); field.dispatchEvent(chord); expect(saves).toEqual([]); expect(chord.defaultPrevented).toBe(false); }); test("IME: composing text is never canceled nor folded in; compositionend folds it as one step", async () => { const { el, field } = mount("a"); focusAt(field, 1); field.dispatchEvent(new CompositionEvent("compositionstart", { bubbles: true })); // The browser writes composed text straight into the field; the engine // must neither cancel insertCompositionText nor take it into the model. field.value = "aか"; const composing = new InputEvent("beforeinput", { inputType: "insertCompositionText", data: "か", isComposing: true, bubbles: true, cancelable: true, }); field.dispatchEvent(composing); expect(composing.defaultPrevented).toBe(false); expect(el.editor?.doc).toBe("a"); // the model stands still field.value = "aかな"; field.dispatchEvent(new CompositionEvent("compositionend", { bubbles: true })); expect(el.editor?.doc).toBe("aかな"); // folded in whole // one undo removes the composition, and only it field.dispatchEvent(new KeyboardEvent("keydown", { key: "z", ctrlKey: true, bubbles: true, cancelable: true })); expect(field.value).toBe("a"); }); test("paste: clipboard text applies as one edit; the event is canceled", () => { const { el, field } = mount("hello "); focusAt(field, 6); const event = new ClipboardEvent("paste", { bubbles: true, cancelable: true }); Object.defineProperty(event, "clipboardData", { value: { getData: (type: string) => (type === "text/plain" ? "world" : "") }, }); field.dispatchEvent(event); expect(event.defaultPrevented).toBe(true); expect(field.value).toBe("hello world"); expect(el.editor?.doc).toBe("hello world"); }); test("cut: the selection goes to the clipboard and out of the document", () => { const { field } = mount("hello world"); focusAt(field, 5); field.setSelectionRange(5, 11); document.dispatchEvent(new Event("selectionchange")); const store = new Map(); const event = new ClipboardEvent("cut", { bubbles: true, cancelable: true }); Object.defineProperty(event, "clipboardData", { value: { getData: (t: string) => store.get(t) ?? "", setData: (t: string, v: string) => void store.set(t, v) }, }); field.dispatchEvent(event); expect(event.defaultPrevented).toBe(true); expect(store.get("text/plain")).toBe(" world"); expect(field.value).toBe("hello"); }); test("a form reset restores the default value into the model", async () => { const form = document.createElement("form"); document.body.appendChild(form); const el = document.createElement("pebble-tablet") as TabletElement; const field = document.createElement("textarea"); field.name = "body"; field.textContent = "default"; // the server-rendered default: reset restores defaultValue el.appendChild(field); form.appendChild(el); focusAt(field, 7); field.dispatchEvent(beforeinput("insertText", " typed")); expect(field.value).toBe("default typed"); form.reset(); await sleep(10); // the reset read runs once the browser has settled the value expect(field.value).toBe("default"); expect(el.editor?.doc).toBe("default"); }); test("without a textarea child an empty one is rendered; the host's name passes through", () => { const el = document.createElement("pebble-tablet") as TabletElement; el.setAttribute("name", "note"); document.body.appendChild(el); const field = el.querySelector("textarea")!; expect(field).not.toBeNull(); expect(field.name).toBe("note"); expect(field.hidden).toBe(false); expect(el.editor?.doc).toBe(""); focusAt(field, 0); field.dispatchEvent(beforeinput("insertText", "typed")); expect(field.value).toBe("typed"); }); test("once-only: a second define() of the tag returns the first constructor, registers nothing twice", () => { // The double-import case (two vendored bundles, or a module graph that // reaches the package twice) ends here: define() sees the tag is taken and // hands back the existing constructor instead of throwing or replacing it. const again = define("pebble-tablet", { shadow: false }, () => null); expect(again).toBe(Tablet); const el = document.createElement("pebble-tablet") as TabletElement; document.body.appendChild(el); expect(el.editor).toBeDefined(); // the original render still runs }); test("disconnect releases the element's engine view", async () => { const { el } = mount("hello"); el.remove(); await sleep(0); // the release runs one macrotask after disconnect expect(el.editor).toBeUndefined(); expect(el.getAttribute("data-enhanced")).toBeNull(); });