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";
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"\)/);
159 + it(
"places pins relative to scrolled fragment content", ()
=> {
160 + const html
= renderMockupPage(baseRequest,
"tok");
163 + /\(event\.clientX - rect\.left \+ frag\.scrollLeft\) \/ frag\.scrollWidth/,
167 + /\(event\.clientY - rect\.top \+ frag\.scrollTop\) \/ frag\.scrollHeight/,
153 172 describe(
"mockup browser opener", ()
=> {
222 + it(
"preserves UTF-8 split across request chunks",
async ()
=> {
223 + const server
= await startMockupServer(baseRequest, { openBrowser:
false });
225 + const body
= Buffer.
from(
229 + notes:
"before ๐ after",
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(
241 + Authorization:
`Bearer ${server.token}`,
242 + "Content-Type":
"application/json",
247 + res.
once(
"end", resolve);
250 + req.
once(
"error", reject);
251 + req.
write(body.
subarray(
0, split));
252 + setImmediate(()
=> req.
end(body.
subarray(split)));
254 + assert.
strictEqual((
await server.result).notes,
"before ๐ after");
256 + server.result.
catch(()
=> undefined);
257 + server.
close(
"test");
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
Modified405 405 async function readBody(req
: IncomingMessage)
: Promise<
string> {
406 + req.
setEncoding(
"utf8");
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);
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); });