Skip to content

Commit cd95643

Browse files
matt2eclaude
andcommitted
test(composer): pin Shift+Arrow selection across mention tokens
Root cause: the reported sequence (caret at the end of "@imp say hello to @jitter ", Shift+Left selecting the trailing space, a second Shift+Left selecting the whole message) did not reproduce in Playwright Chromium or WebKit, in a system WKWebView driven by real NSEvent Shift+Left key events (single press, held Shift with key repeat, and a restored draft), or in jsdom with the browser's first press modelled on the DOM selection. The second press extends over the mention and keeps the anchor at the end. No product code is changed; this commit adds the regression coverage that was missing so the contract is pinned at both test layers. Reviewed and found correct in src/features/messages/EditableInput.tsx: `adjacent()` keeps `anchor` and moves only `head` to `token.from` or `token.to`, so direction is preserved; `syncNativeSelection()` only falls back to AllSelection when the DOM range already spans the whole document, which no Shift+Arrow press produces. Sibling cases broken and fixed: none. Sibling cases checked and already correct: - Shift+Right from directly before a mention selects the whole mention, then the trailing space; Shift+Left then shrinks it back. - Shift+Left directly after a mention with no trailing space. - Backward extension keeps a backward direction and anchor; forward extension keeps a forward direction and anchor; shrinking off a mention in either direction collapses to the mention edge. - Mention at position 0 (Shift+Left stops at 0, Shift+Right from 0 selects it) and mention at the very end of the document. - Two adjacent @-mentions cannot exist: the mention boundary rule in mention-draft.ts demotes the second to plain text. Adjacent atoms are covered by tests/browser/emoji.spec.mjs. - A selection started in prose crossing a mention in both directions. - Shift+Home, Shift+End and Option/Ctrl+Shift+Arrow word jumps stay native and treat the mention as one word. - Mouse drag from the end back into the prose selects the mention. The jsdom cases drive ArrowLeft/ArrowRight keydown directly because user-event cannot extend a selection. The browser spec covers the parts jsdom cannot: native caret movement through prose, words and lines, and pointer drag, handing off to the editor at the mention edge. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent fefbfd0 commit cd95643

2 files changed

Lines changed: 251 additions & 3 deletions

File tree

‎src/features/messages/EditableInput.test.tsx‎

Lines changed: 142 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import { mentionDraft, type MentionDraft } from "./mention-draft";
1919
composerDOMFixture();
2020
afterEach(cleanup);
2121

22-
function mount(text = "") {
22+
function mount(initial: string | MentionDraft = "") {
2323
const ref = createRef<ComposerInputElement>();
24-
let draft: MentionDraft = mentionDraft(text);
24+
let draft: MentionDraft = mentionDraft(initial);
25+
const text = draft.text;
2526
function Editor() {
2627
const [value, setValue] = useState(draft);
2728
return (
@@ -32,7 +33,14 @@ function mount(text = "") {
3233
disabled={false}
3334
placeholder="Draft"
3435
maxLength={16000}
35-
decorationsFor={() => []}
36+
// Explicit recipients render as mention tokens, as in RichComposerInput.
37+
decorationsFor={(current) =>
38+
current.recipients.map(({ start, end, name }) => ({
39+
start,
40+
end,
41+
content: <span data-mention>@{name}</span>,
42+
}))
43+
}
3644
onFormatsChange={() => {}}
3745
onDraftChange={(next) => {
3846
draft = next;
@@ -256,6 +264,137 @@ it.each([true, false])(
256264
},
257265
);
258266

267+
const imp = { pubkey: "a".repeat(64), name: "Imp" };
268+
const jitter = { pubkey: "b".repeat(64), name: "Jitter" };
269+
/** Every `@Imp`/`@Jitter` in the text is an explicit, resolved recipient. */
270+
function mentioned(text: string): MentionDraft {
271+
return {
272+
text,
273+
recipients: [imp, jitter].flatMap((recipient) => {
274+
const start = text.indexOf(`@${recipient.name}`);
275+
return start < 0
276+
? []
277+
: [{ ...recipient, start, end: start + recipient.name.length + 1 }];
278+
}),
279+
};
280+
}
281+
const selection = (input: ComposerInputElement) =>
282+
[input.selectionStart, input.selectionEnd, input.selectionDirection] as const;
283+
/** Arrow keys are deliberately low-level: user-event cannot extend a selection. */
284+
const shiftArrow = (
285+
input: ComposerInputElement,
286+
key: "ArrowLeft" | "ArrowRight",
287+
) =>
288+
act(() => {
289+
fireEvent.keyDown(input, { key: "Shift", keyCode: 16, shiftKey: true });
290+
fireEvent.keyDown(input, {
291+
key,
292+
code: key,
293+
keyCode: key === "ArrowLeft" ? 37 : 39,
294+
shiftKey: true,
295+
});
296+
});
297+
298+
it("extends a backward selection over the mention before it and keeps the anchor", () => {
299+
const text = "@Imp say hello to @Jitter ";
300+
const h = mount(mentioned(text));
301+
expect(h.input.querySelectorAll("[data-mention]")).toHaveLength(2);
302+
act(() => h.input.setSelectionRange(text.length, text.length));
303+
// The browser itself answers the first Shift+Left by selecting the trailing
304+
// space. Model that native result, then let the editor handle the second.
305+
act(() => {
306+
const space = h.input.querySelector("p")?.lastChild;
307+
if (!(space instanceof Text)) throw new Error("Missing trailing text");
308+
document.getSelection()?.setBaseAndExtent(space, 1, space, 0);
309+
});
310+
shiftArrow(h.input, "ArrowLeft");
311+
expect(selection(h.input)).toEqual([18, 26, "backward"]);
312+
expect(text.slice(18, 26)).toBe("@Jitter ");
313+
shiftArrow(h.input, "ArrowRight");
314+
expect(selection(h.input)).toEqual([25, 26, "backward"]);
315+
});
316+
317+
it.each([
318+
[
319+
"after a mention with no trailing space",
320+
"@Imp say hello to @Jitter",
321+
25,
322+
"ArrowLeft",
323+
[18, 25, "backward"],
324+
],
325+
[
326+
"before a mention",
327+
"@Imp say hello to @Jitter ",
328+
18,
329+
"ArrowRight",
330+
[18, 25, "forward"],
331+
],
332+
[
333+
"after the mention at the start",
334+
"@Imp say hello to @Jitter ",
335+
4,
336+
"ArrowLeft",
337+
[0, 4, "backward"],
338+
],
339+
[
340+
"the document start",
341+
"@Imp say hello to @Jitter ",
342+
0,
343+
"ArrowRight",
344+
[0, 4, "forward"],
345+
],
346+
["the document end", "hello @Jitter", 13, "ArrowLeft", [6, 13, "backward"]],
347+
] as const)(
348+
"Shift+Arrow from %s selects the whole mention",
349+
(_, text, caret, key, expected) => {
350+
const h = mount(mentioned(text));
351+
act(() => h.input.setSelectionRange(caret, caret));
352+
shiftArrow(h.input, key);
353+
expect(selection(h.input)).toEqual(expected);
354+
},
355+
);
356+
357+
it.each([
358+
[
359+
"extends a forward selection from prose over a mention",
360+
16,
361+
18,
362+
"forward",
363+
"ArrowRight",
364+
[16, 25, "forward"],
365+
],
366+
[
367+
"extends a backward selection from prose over a mention",
368+
4,
369+
8,
370+
"backward",
371+
"ArrowLeft",
372+
[0, 8, "backward"],
373+
],
374+
[
375+
"shrinks a forward selection off a mention",
376+
18,
377+
25,
378+
"forward",
379+
"ArrowLeft",
380+
[18, 18, "forward"],
381+
],
382+
[
383+
"shrinks a backward selection off a mention",
384+
18,
385+
26,
386+
"backward",
387+
"ArrowRight",
388+
[25, 26, "backward"],
389+
],
390+
] as const)("%s", (_, start, end, direction, key, expected) => {
391+
const h = mount(mentioned("@Imp say hello to @Jitter "));
392+
act(() => h.input.setSelectionRange(start, end, direction));
393+
expect(selection(h.input)).toEqual([start, end, direction]);
394+
shiftArrow(h.input, key);
395+
expect(selection(h.input)).toEqual(expected);
396+
});
397+
259398
it("retains the native caret after replacing text with an unchanged suffix", async () => {
260399
const h = mount(":unknown:");
261400
act(() => {
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { test, expect } from "./source-fixture.mjs";
2+
3+
// The browser moves the caret through prose, words and lines; the editor takes
4+
// over only at a mention's edge. That handoff is a native selection contract.
5+
test("keyboard and pointer selection cross a mention as one unit in both directions", async ({
6+
page,
7+
}) => {
8+
await page.goto("/tests/fixtures/mentions.html");
9+
const keys = await page.evaluate(() => ({
10+
first: window.mentionFixture.first,
11+
second: window.mentionFixture.second,
12+
}));
13+
const input = page.getByRole("textbox", { name: "Message #General" });
14+
const mention = async (name) => {
15+
await page
16+
.getByRole("button", { name: "Mention a member", exact: true })
17+
.click();
18+
await page.getByRole("button", { name, exact: true }).click();
19+
// The picker restores focus and the caret on the next animation frame.
20+
await page.evaluate(() => new Promise(requestAnimationFrame));
21+
await expect(input).toBeFocused();
22+
};
23+
await input.click();
24+
await mention(`Honey ${keys.first}`);
25+
await page.keyboard.type("say hello to ");
26+
await mention(`Honey (agent) ${keys.second}`);
27+
await expect(input).toHaveJSProperty("value", "@Honey say hello to @Honey ");
28+
const selection = () =>
29+
input.evaluate((el) => [
30+
el.selectionStart,
31+
el.selectionEnd,
32+
el.selectionDirection,
33+
]);
34+
const caret = (offset) =>
35+
input.evaluate(
36+
(el, offset) => el.setSelectionRange(offset, offset),
37+
offset,
38+
);
39+
40+
// The browser selects the trailing space; the editor extends over the mention.
41+
await expect.poll(selection).toEqual([27, 27, "forward"]);
42+
await input.press("Shift+ArrowLeft");
43+
await expect.poll(selection).toEqual([26, 27, "backward"]);
44+
await input.press("Shift+ArrowLeft");
45+
await expect.poll(selection).toEqual([20, 27, "backward"]);
46+
await input.press("Shift+ArrowLeft");
47+
await expect.poll(selection).toEqual([19, 27, "backward"]);
48+
await input.press("Shift+ArrowRight");
49+
await expect.poll(selection).toEqual([20, 27, "backward"]);
50+
await input.press("Shift+ArrowRight");
51+
await expect.poll(selection).toEqual([26, 27, "backward"]);
52+
53+
await caret(20);
54+
await input.press("Shift+ArrowRight");
55+
await expect.poll(selection).toEqual([20, 26, "forward"]);
56+
await input.press("Shift+ArrowRight");
57+
await expect.poll(selection).toEqual([20, 27, "forward"]);
58+
await input.press("Shift+ArrowLeft");
59+
await expect.poll(selection).toEqual([20, 26, "forward"]);
60+
await input.press("Shift+ArrowLeft");
61+
await expect.poll(selection).toEqual([20, 20, "forward"]);
62+
63+
await caret(26);
64+
await input.press("Shift+ArrowLeft");
65+
await expect.poll(selection).toEqual([20, 26, "backward"]);
66+
await caret(6);
67+
await input.press("Shift+ArrowLeft");
68+
await expect.poll(selection).toEqual([0, 6, "backward"]);
69+
await input.press("Shift+ArrowRight");
70+
await expect.poll(selection).toEqual([6, 6, "forward"]);
71+
72+
// Word and line jumps stay native and never split a mention.
73+
const word = process.platform === "darwin" ? "Alt" : "Control";
74+
await caret(27);
75+
await input.press(`${word}+Shift+ArrowLeft`);
76+
await expect.poll(selection).toEqual([20, 27, "backward"]);
77+
await input.press(`${word}+Shift+ArrowLeft`);
78+
await expect.poll(selection).toEqual([17, 27, "backward"]);
79+
await input.press("Shift+Home");
80+
await expect.poll(selection).toEqual([0, 27, "backward"]);
81+
await caret(4);
82+
await input.press("Shift+End");
83+
await expect.poll(selection).toEqual([4, 27, "forward"]);
84+
85+
// Dragging from the end back into the prose selects the mention in between.
86+
const points = await input.evaluate((el) => {
87+
const paragraph = el.querySelector("p");
88+
const prose = [...paragraph.childNodes].find(
89+
(node) => node.nodeType === Node.TEXT_NODE && node.data.includes("hello"),
90+
);
91+
const line = document.createRange();
92+
line.selectNodeContents(paragraph);
93+
const edge = line.getBoundingClientRect();
94+
const boundary = document.createRange();
95+
boundary.setStart(prose, 12);
96+
boundary.setEnd(prose, 12);
97+
const at = boundary.getBoundingClientRect();
98+
return {
99+
y: edge.top + edge.height / 2,
100+
end: edge.right + 2,
101+
inProse: at.left,
102+
};
103+
});
104+
await page.mouse.move(points.end, points.y);
105+
await page.mouse.down();
106+
await page.mouse.move(points.inProse, points.y, { steps: 8 });
107+
await page.mouse.up();
108+
await expect.poll(selection).toEqual([18, 27, "backward"]);
109+
});

0 commit comments

Comments
 (0)