Repository navigation
Expand file tree
/
Copy pathchatInputEditorCursorMatrix.spec.ts
More file actions
583 lines (534 loc) · 21.1 KB
/
Copy pathchatInputEditorCursorMatrix.spec.ts
File metadata and controls
583 lines (534 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import {openEditor, settleNativeSelection, setEditorDocument as setDocument} from './fixtures/chatInputEditorTest';
import {expect, type Page} from '@playwright/test';
import {test} from './workerContext';
import type {JSONContent} from '@tiptap/core';
import type {ChatInputEditorBrowserHarness, TextblockDescriptor} from './fixtures/chatInputEditor';
declare global {
interface Window {
chatInputEditorHarness: ChatInputEditorBrowserHarness
}
}
const text = (value: string): JSONContent => ({type: 'text', text: value});
const paragraph = (value = ''): JSONContent => ({
type: 'paragraph',
content: value ? [text(value)] : undefined
});
async function textblocks(page: Page) {
return page.evaluate(() => window.chatInputEditorHarness.textblocks());
}
function blockIndex(blocks: TextblockDescriptor[], value: string) {
const index = blocks.findIndex(({text}) => text === value);
expect(index, `Missing textblock ${JSON.stringify(value)}`).toBeGreaterThanOrEqual(0);
return index;
}
async function setCaretInBlock(
page: Page,
blocks: TextblockDescriptor[],
value: string,
edge: 'end' | 'start'
) {
expect(await page.evaluate(({edge, index}) => (
window.chatInputEditorHarness.setCaretInTextblock(index, edge)
), {edge, index: blockIndex(blocks, value)})).toBe(true);
}
async function expectTextSelection(page: Page, value: string, offset?: number) {
const selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.type).toBe('text');
expect(selection.text).toBe(value);
if(offset !== undefined) expect(selection.parentOffset).toBe(offset);
}
test.beforeEach(async({page}) => {
await openEditor(page);
});
test('virtual Backspace deletes a Unicode grapheme across formatting boundaries', async({page}) => {
for(const grapheme of ['e\u0301', '👩🏽💻', '🇦🇪']) {
const document: JSONContent = {
type: 'doc',
content: [{
type: 'paragraph',
content: [
text('A'),
...Array.from(grapheme).map((value, index) => ({
type: 'text', text: value, marks: [{type: index % 2 ? 'italic' : 'bold'}]
})),
text('B')
]
}]
};
await setDocument(page, document);
expect(await page.evaluate((offset) => (
window.chatInputEditorHarness.setCaretInNode('paragraph', 0, offset)
), 1 + grapheme.length)).toBe(true);
expect(await page.evaluate(() => window.chatInputEditorHarness.deleteBackward())).toBe(true);
await settleNativeSelection(page);
expect(await page.evaluate(() => window.chatInputEditorHarness.document())).toEqual({type: 'doc', content: [paragraph('AB')]});
await page.keyboard.press('ControlOrMeta+z');
await settleNativeSelection(page);
expect(await page.evaluate(() => window.chatInputEditorHarness.document())).toEqual(document);
}
});
test('moves exactly between adjacent plain paragraphs in every arrow direction', async({page}) => {
await setDocument(page, {
type: 'doc',
content: [paragraph('Alpha'), paragraph('Bravo')]
});
const blocks = await textblocks(page);
await setCaretInBlock(page, blocks, 'Bravo', 'start');
await page.keyboard.press('ArrowLeft');
await settleNativeSelection(page);
await expectTextSelection(page, 'Alpha', 'Alpha'.length);
await setCaretInBlock(page, blocks, 'Alpha', 'end');
await page.keyboard.press('ArrowRight');
await settleNativeSelection(page);
await expectTextSelection(page, 'Bravo', 0);
await setCaretInBlock(page, blocks, 'Bravo', 'start');
await page.keyboard.press('ArrowUp');
await settleNativeSelection(page);
await expectTextSelection(page, 'Alpha');
await setCaretInBlock(page, blocks, 'Alpha', 'end');
await page.keyboard.press('ArrowDown');
await settleNativeSelection(page);
await expectTextSelection(page, 'Bravo');
});
test('visits every visible rich-container surface in exact document order', async({page}) => {
test.setTimeout(60_000);
await setDocument(page, {
type: 'doc',
content: [
paragraph('Before'),
{
type: 'blockquote',
content: [
paragraph('Quote one'),
paragraph('Quote two'),
{type: 'blockquoteCaption', content: [text('Quote author')]}
]
},
{
type: 'pullquote',
content: [
{type: 'pullquoteText', content: [text('Pullquote')]},
{type: 'pullquoteCaption', content: [text('Pullquote author')]}
]
},
{
type: 'details',
attrs: {open: true},
content: [
{type: 'detailsSummary', content: [text('Open details')]},
{
type: 'detailsBody',
content: [paragraph('Body one'), paragraph('Body two')]
}
]
},
{
type: 'details',
attrs: {open: false},
content: [
{type: 'detailsSummary', content: [text('Closed details')]},
{type: 'detailsBody', content: [paragraph('Hidden body')]}
]
},
{
type: 'bulletList',
content: ['Bullet one', 'Bullet two'].map((value) => ({
type: 'listItem',
content: [paragraph(value)]
}))
},
paragraph('After')
]
});
const blocks = await textblocks(page);
const order = [
'Before',
'Quote one',
'Quote two',
'Quote author',
'Pullquote',
'Pullquote author',
'Open details',
'Body one',
'Body two',
'Closed details',
'Bullet one',
'Bullet two',
'After'
];
expect(blocks.find(({text}) => text === 'Hidden body')?.visible).toBe(false);
for(let index = 0; index < order.length - 1; ++index) {
const source = order[index];
const target = order[index + 1];
await test.step(`ArrowDown ${source} → ${target}`, async() => {
await setCaretInBlock(page, blocks, source, 'end');
await page.keyboard.press('ArrowDown');
await settleNativeSelection(page);
await expectTextSelection(page, target);
});
await test.step(`ArrowRight ${source} → ${target}`, async() => {
await setCaretInBlock(page, blocks, source, 'end');
await page.keyboard.press('ArrowRight');
await settleNativeSelection(page);
await expectTextSelection(page, target, 0);
});
}
for(let index = order.length - 1; index > 0; --index) {
const source = order[index];
const target = order[index - 1];
await test.step(`ArrowUp ${source} → ${target}`, async() => {
await setCaretInBlock(page, blocks, source, 'start');
await page.keyboard.press('ArrowUp');
await settleNativeSelection(page);
await expectTextSelection(page, target);
});
await test.step(`ArrowLeft ${source} → ${target}`, async() => {
await setCaretInBlock(page, blocks, source, 'start');
await page.keyboard.press('ArrowLeft');
await settleNativeSelection(page);
await expectTextSelection(page, target, target.length);
});
}
});
test('stops on every top-level atom instead of skipping it', async({page}) => {
await setDocument(page, {
type: 'doc',
content: [
paragraph('Before'),
{type: 'blockMath', attrs: {source: 'x^2'}},
{type: 'richDivider'},
{type: 'opaqueRichBlock', attrs: {block: {_: 'pageBlockUnsupported'}}},
paragraph('After')
]
});
const blocks = await textblocks(page);
await setCaretInBlock(page, blocks, 'Before', 'end');
for(const type of ['blockMath', 'richDivider', 'opaqueRichBlock']) {
await page.keyboard.press('ArrowRight');
await settleNativeSelection(page);
const selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.type).toBe('node');
expect(selection.nodeType).toBe(type);
}
await page.keyboard.press('ArrowRight');
await settleNativeSelection(page);
await expectTextSelection(page, 'After', 0);
for(const type of ['opaqueRichBlock', 'richDivider', 'blockMath']) {
await page.keyboard.press('ArrowLeft');
await settleNativeSelection(page);
const selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.type).toBe('node');
expect(selection.nodeType).toBe(type);
}
await page.keyboard.press('ArrowLeft');
await settleNativeSelection(page);
await expectTextSelection(page, 'Before', 'Before'.length);
});
test('moves by complete Unicode graphemes without splitting them', async({page}) => {
const graphemes = ['🙂', 'e\u0301', '👨👩👧👦', '🇦🇪'];
for(const grapheme of graphemes) {
const value = `A${grapheme}B`;
await setDocument(page, {type: 'doc', content: [paragraph(value)]});
const blocks = await textblocks(page);
const block = blocks.find(({text}) => text === value)!;
const before = block.from + 1;
const after = before + grapheme.length;
expect(await page.evaluate((position) => (
window.chatInputEditorHarness.setCaret(position)
), after)).toBe(true);
await page.keyboard.press('ArrowLeft');
await settleNativeSelection(page);
expect((await page.evaluate(() => window.chatInputEditorHarness.selection())).from)
.toBe(before);
expect(await page.evaluate((position) => (
window.chatInputEditorHarness.setCaret(position)
), before)).toBe(true);
await page.keyboard.press('ArrowRight');
await settleNativeSelection(page);
expect((await page.evaluate(() => window.chatInputEditorHarness.selection())).from)
.toBe(after);
}
});
test('extends selections across inline atoms and paragraph boundaries', async({page}) => {
await setDocument(page, {
type: 'doc',
content: [{
type: 'paragraph',
content: [text('A'), {type: 'inlineMath', attrs: {source: 'x'}}, text('B')]
}, paragraph('Next')]
});
const atomPosition = (await page.evaluate(() => (
window.chatInputEditorHarness.nodePositions('inlineMath')
)))[0];
expect(await page.evaluate((position) => (
window.chatInputEditorHarness.setCaret(position)
), atomPosition)).toBe(true);
await page.keyboard.press('Shift+ArrowRight');
await settleNativeSelection(page);
let selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.from).toBe(atomPosition);
expect(selection.to).toBe(atomPosition + 1);
await page.keyboard.press('Shift+ArrowRight');
await settleNativeSelection(page);
selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.from).toBe(atomPosition);
expect(selection.to).toBe(atomPosition + 2);
expect(await page.evaluate((position) => (
window.chatInputEditorHarness.setCaret(position)
), atomPosition + 1)).toBe(true);
await page.keyboard.press('Shift+ArrowLeft');
await settleNativeSelection(page);
selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.from).toBe(atomPosition);
expect(selection.to).toBe(atomPosition + 1);
await page.keyboard.press('Shift+ArrowLeft');
await settleNativeSelection(page);
selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.from).toBe(atomPosition - 1);
expect(selection.to).toBe(atomPosition + 1);
const blocks = await textblocks(page);
const inlineBlock = blocks[0];
const nextBlock = blocks.find(({text}) => text === 'Next')!;
expect(await page.evaluate(({index}) => (
window.chatInputEditorHarness.setCaretInTextblock(index, 'end')
), {index: 0})).toBe(true);
await page.keyboard.press('Shift+ArrowRight');
await settleNativeSelection(page);
selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.type).toBe('text');
expect(selection.from).toBe(inlineBlock.to);
expect(selection.to).toBe(nextBlock.from);
await page.keyboard.press('Shift+ArrowRight');
await settleNativeSelection(page);
selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.from).toBe(inlineBlock.to);
expect(selection.to).toBe(nextBlock.from + 1);
});
test('Backspace and Delete join adjacent non-empty paragraphs symmetrically', async({page}) => {
const document: JSONContent = {
type: 'doc',
content: [paragraph('Alpha'), paragraph('Bravo')]
};
await setDocument(page, document);
let blocks = await textblocks(page);
await setCaretInBlock(page, blocks, 'Bravo', 'start');
await page.keyboard.press('Backspace');
await settleNativeSelection(page);
await expectTextSelection(page, 'AlphaBravo', 'Alpha'.length);
await setDocument(page, document);
blocks = await textblocks(page);
await setCaretInBlock(page, blocks, 'Alpha', 'end');
await page.keyboard.press('Delete');
await settleNativeSelection(page);
await expectTextSelection(page, 'AlphaBravo', 'Alpha'.length);
});
test('enters and leaves an empty paragraph without skipping its placeholder', async({page}) => {
await setDocument(page, {
type: 'doc',
content: [paragraph('Before'), paragraph(), paragraph('After')]
});
const blocks = await textblocks(page);
const before = blockIndex(blocks, 'Before');
const empty = blocks.findIndex((block, index) => (
index > before && !block.text && block.visible
));
const after = blockIndex(blocks, 'After');
expect(empty).toBeGreaterThan(before);
expect(empty).toBeLessThan(after);
for(const {edge, key, source, target} of [
{edge: 'end', key: 'ArrowDown', source: before, target: empty},
{edge: 'end', key: 'ArrowRight', source: before, target: empty},
{edge: 'start', key: 'ArrowDown', source: empty, target: after},
{edge: 'start', key: 'ArrowRight', source: empty, target: after},
{edge: 'start', key: 'ArrowUp', source: after, target: empty},
{edge: 'start', key: 'ArrowLeft', source: after, target: empty},
{edge: 'start', key: 'ArrowUp', source: empty, target: before},
{edge: 'start', key: 'ArrowLeft', source: empty, target: before}
] as const) {
expect(await page.evaluate(({edge, index}) => (
window.chatInputEditorHarness.setCaretInTextblock(index, edge)
), {edge, index: source})).toBe(true);
await page.keyboard.press(key);
await settleNativeSelection(page);
const selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.type).toBe('text');
expect(selection.from).toBeGreaterThanOrEqual(blocks[target].from);
expect(selection.to).toBeLessThanOrEqual(blocks[target].to);
}
});
test('uses visual horizontal directions in an RTL editor', async({page}) => {
await setDocument(page, {
type: 'doc',
content: [paragraph('ألف'), paragraph('باء')]
});
await page.evaluate(() => window.chatInputEditorHarness.setDirection('rtl'));
const blocks = await textblocks(page);
await setCaretInBlock(page, blocks, 'ألف', 'end');
await page.keyboard.press('ArrowLeft');
await settleNativeSelection(page);
await expectTextSelection(page, 'باء', 0);
await setCaretInBlock(page, blocks, 'باء', 'start');
await page.keyboard.press('ArrowRight');
await settleNativeSelection(page);
await expectTextSelection(page, 'ألف', 'ألف'.length);
});
test('crosses inline atoms in mixed-direction text by visual direction', async({page}) => {
await setDocument(page, {
type: 'doc',
content: [{
type: 'paragraph',
content: [
text('Latin אבג '),
{type: 'inlineMath', attrs: {source: 'x'}},
text(' مرحبا Latin')
]
}]
});
const atomPosition = (await page.evaluate(() => (
window.chatInputEditorHarness.nodePositions('inlineMath')
)))[0];
await page.evaluate(() => window.chatInputEditorHarness.setDirection('ltr'));
expect(await page.evaluate((position) => (
window.chatInputEditorHarness.setCaret(position)
), atomPosition)).toBe(true);
await page.keyboard.press('ArrowRight');
await settleNativeSelection(page);
expect((await page.evaluate(() => window.chatInputEditorHarness.selection())).from)
.toBe(atomPosition + 1);
await page.evaluate(() => window.chatInputEditorHarness.setDirection('rtl'));
expect(await page.evaluate((position) => (
window.chatInputEditorHarness.setCaret(position)
), atomPosition)).toBe(true);
await page.keyboard.press('ArrowLeft');
await settleNativeSelection(page);
expect((await page.evaluate(() => window.chatInputEditorHarness.selection())).from)
.toBe(atomPosition + 1);
await page.keyboard.press('Shift+ArrowRight');
await settleNativeSelection(page);
const selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.from).toBe(atomPosition);
expect(selection.to).toBe(atomPosition + 1);
});
test('moves one character across adjacent mark boundaries', async({page}) => {
await setDocument(page, {
type: 'doc',
content: [{
type: 'paragraph',
content: [
{type: 'text', text: 'A', marks: [{type: 'bold'}]},
{type: 'text', text: 'B', marks: [{type: 'italic'}]},
{type: 'text', text: 'C'}
]
}]
});
const block = (await textblocks(page))[0];
expect(await page.evaluate((position) => (
window.chatInputEditorHarness.setCaret(position)
), block.from + 1)).toBe(true);
await page.keyboard.press('ArrowLeft');
await settleNativeSelection(page);
expect((await page.evaluate(() => window.chatInputEditorHarness.selection())).from)
.toBe(block.from);
expect(await page.evaluate((position) => (
window.chatInputEditorHarness.setCaret(position)
), block.from + 1)).toBe(true);
await page.keyboard.press('ArrowRight');
await settleNativeSelection(page);
expect((await page.evaluate(() => window.chatInputEditorHarness.selection())).from)
.toBe(block.from + 2);
});
test('deletes exactly one inline atom with Backspace or Delete', async({page}) => {
const document: JSONContent = {
type: 'doc',
content: [{
type: 'paragraph',
content: [text('A'), {type: 'inlineMath', attrs: {source: 'x'}}, text('B')]
}]
};
for(const {key, side} of [
{key: 'Delete', side: 0},
{key: 'Backspace', side: 1}
] as const) {
await setDocument(page, document);
const position = (await page.evaluate(() => (
window.chatInputEditorHarness.nodePositions('inlineMath')
)))[0];
expect(await page.evaluate((position) => (
window.chatInputEditorHarness.setCaret(position)
), position + side)).toBe(true);
await page.keyboard.press(key);
await settleNativeSelection(page);
expect(await page.evaluate(() => (
window.chatInputEditorHarness.nodePositions('inlineMath')
))).toEqual([]);
await expectTextSelection(page, 'AB', 1);
}
});
test('deletes each selected top-level atom without taking neighboring text with it', async({page}) => {
const atoms: JSONContent[] = [
{type: 'blockMath', attrs: {source: 'x'}},
{type: 'richDivider'},
{type: 'opaqueRichBlock', attrs: {block: {_: 'pageBlockUnsupported'}}}
];
for(const atom of atoms) {
for(const key of ['Backspace', 'Delete']) {
await setDocument(page, {
type: 'doc',
content: [paragraph('Before'), atom, paragraph('After')]
});
expect(await page.evaluate((type) => (
window.chatInputEditorHarness.setNodeSelection(type)
), atom.type!)).toBe(true);
await page.keyboard.press(key);
await settleNativeSelection(page);
expect(await page.evaluate((type) => (
window.chatInputEditorHarness.nodePositions(type)
), atom.type!)).toEqual([]);
const document = await page.evaluate(() => window.chatInputEditorHarness.document());
expect(JSON.stringify(document)).toContain('Before');
expect(JSON.stringify(document)).toContain('After');
expect((await page.evaluate(() => window.chatInputEditorHarness.selection())).type)
.toBe('text');
}
}
});
test('Backspace moves from Details body to title and unwraps an entirely empty Details', async({page}) => {
const details = (title: string): JSONContent => ({
type: 'details',
attrs: {open: true},
content: [
{type: 'detailsSummary', content: title ? [text(title)] : undefined},
{type: 'detailsBody', content: [paragraph()]}
]
});
await setDocument(page, {
type: 'doc',
content: [paragraph('Before'), details('Title'), paragraph('After')]
});
let blocks = await textblocks(page);
const body = blocks.findIndex(({path, text}) => path.includes('detailsBody') && !text);
expect(body).toBeGreaterThanOrEqual(0);
expect(await page.evaluate((index) => (
window.chatInputEditorHarness.setCaretInTextblock(index, 'start')
), body)).toBe(true);
await page.keyboard.press('Backspace');
await settleNativeSelection(page);
await expectTextSelection(page, 'Title', 'Title'.length);
await setDocument(page, {
type: 'doc',
content: [paragraph('Before'), details(''), paragraph('After')]
});
blocks = await textblocks(page);
const summary = blocks.findIndex(({path}) => path.includes('detailsSummary'));
expect(summary).toBeGreaterThanOrEqual(0);
expect(await page.evaluate((index) => (
window.chatInputEditorHarness.setCaretInTextblock(index, 'start')
), summary)).toBe(true);
await page.keyboard.press('Backspace');
await settleNativeSelection(page);
expect(await page.evaluate(() => (
window.chatInputEditorHarness.nodePositions('details')
))).toEqual([]);
expect((await page.evaluate(() => window.chatInputEditorHarness.selection())).type)
.toBe('text');
});