repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(super): harden mockup interactions

be62d155b2284a52993f96e1d330fed4bac027d2

Oliver Jan Krylow <oliver@bugabinga.net> ยท

unsigned

Browse filesCompare with HEAD

Changed files (2)

extensions/super/__tests__/mockup.test.ts

Modified
1 1 import assert from "node:assert";
2 +import { request as httpRequest } from "node:http";
2 3 import { describe, it } from "vitest";
3 4 import {
4 5 MOCKUP_FULL_DOCUMENT_ERROR,
149 150 assert.match(html, /armed = null;\n {2}sync\(\);\n {2}showBubble/);
153 + it("selects label-backed options only once", () => {
154 + const html = renderMockupPage(baseRequest, "tok");
155 + assert.match(html, /\$\("input", card\)\.addEventListener\("change"/);
156 + assert.match(html, /closest\("label,button"\)/);
157 + });
159 + it("places pins relative to scrolled fragment content", () => {
160 + const html = renderMockupPage(baseRequest, "tok");
161 + assert.match(
162 + html,
163 + /\(event\.clientX - rect\.left \+ frag\.scrollLeft\) \/ frag\.scrollWidth/,
164 + );
165 + assert.match(
166 + html,
167 + /\(event\.clientY - rect\.top \+ frag\.scrollTop\) \/ frag\.scrollHeight/,
168 + );
169 + });
153 172 describe("mockup browser opener", () => {
222 + it("preserves UTF-8 split across request chunks", async () => {
223 + const server = await startMockupServer(baseRequest, { openBrowser: false });
224 + try {
225 + const body = Buffer.from(
226 + JSON.stringify({
227 + action: "accept",
228 + selected: ["a"],
229 + notes: "before ๐Ÿ˜€ after",
230 + annotations: [],
231 + }),
232 + );
233 + const split = body.indexOf(Buffer.from("๐Ÿ˜€")) + 1;
234 + await new Promise<void>((resolve, reject) => {
235 + const url = new URL("/api/submit", server.url);
236 + const req = httpRequest(
237 + url,
239 + method: "POST",
240 + headers: {
241 + Authorization: `Bearer ${server.token}`,
242 + "Content-Type": "application/json",
243 + },
244 + },
245 + (res) => {
246 + res.resume();
247 + res.once("end", resolve);
248 + },
249 + );
250 + req.once("error", reject);
251 + req.write(body.subarray(0, split));
252 + setImmediate(() => req.end(body.subarray(split)));
253 + });
254 + assert.strictEqual((await server.result).notes, "before ๐Ÿ˜€ after");
255 + } finally {
256 + server.result.catch(() => undefined);
257 + server.close("test");
259 + });
203 261 it("blocks until browser submits action, selection, notes, and annotations", async () => {
204 262 const server = await startMockupServer(baseRequest, { openBrowser: false });

extensions/super/mockup.ts

Modified
405 405 async function readBody(req: IncomingMessage): Promise<string> {
406 + req.setEncoding("utf8");
406 407 let body = "";
407 408 for await (const chunk of req) {
408 409 body += String(chunk);
708 709 function addPin(id, event) {
709 710 const frag = event.currentTarget;
710 711 const rect = frag.getBoundingClientRect();
711 - const pin = { optionId:id, x:(event.clientX - rect.left) / rect.width, y:(event.clientY - rect.top) / rect.height, note:"" };
712 + const pin = { optionId:id, x:(event.clientX - rect.left + frag.scrollLeft) / frag.scrollWidth, y:(event.clientY - rect.top + frag.scrollTop) / frag.scrollHeight, note:"" };
712 713 annotations.push(pin);
713 714 armed = null;
714 715 sync();
780 781 $$(".option-card").forEach(card => {
781 782 const id = card.dataset.optionId;
782 - $("input", card).addEventListener("click", e => { e.stopPropagation(); select(id); });
783 - $(".option-head", card).addEventListener("click", e => { if (e.target.tagName !== "BUTTON") select(id); });
783 + $("input", card).addEventListener("change", () => select(id));
784 + $(".option-head", card).addEventListener("click", e => { if (!e.target.closest("label,button")) select(id); });
784 785 $(".pin-toggle", card).addEventListener("click", e => { e.stopPropagation(); armed = armed === id ? null : id; sync(); });
785 786 $(".fragment", card).addEventListener("click", e => { if (armed === id) addPin(id, e); else select(id); });