Repository navigation
Expand file tree
/
Copy pathchatInputEditorTableCursor.spec.ts
More file actions
315 lines (285 loc) · 11.8 KB
/
Copy pathchatInputEditorTableCursor.spec.ts
File metadata and controls
315 lines (285 loc) · 11.8 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
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
});
const cell = (
value: string,
type: 'tableCell' | 'tableHeader' = 'tableCell',
attrs?: Record<string, unknown>
): JSONContent => ({
type,
attrs,
content: [paragraph(value)]
});
const tableDocument = (
rows: JSONContent[][],
title = 'Title'
): JSONContent => ({
type: 'doc',
content: [{
type: 'chatTableWrapper',
content: [
{type: 'chatTableTitle', content: title ? [text(title)] : undefined},
{
type: 'table',
content: rows.map((content) => ({type: 'tableRow', content}))
}
]
}]
});
async function blocks(page: Page) {
return page.evaluate(() => window.chatInputEditorHarness.textblocks());
}
function blockIndex(
descriptors: TextblockDescriptor[],
value: string,
occurrence = 0
) {
let current = 0;
const index = descriptors.findIndex(({text}) => (
text === value && current++ === occurrence
));
expect(index, `Missing table textblock ${JSON.stringify(value)}`).toBeGreaterThanOrEqual(0);
return index;
}
async function setCaret(
page: Page,
descriptors: TextblockDescriptor[],
value: string,
edge: 'end' | 'start' | number,
occurrence = 0
) {
expect(await page.evaluate(({edge, index}) => (
window.chatInputEditorHarness.setCaretInTextblock(index, edge)
), {edge, index: blockIndex(descriptors, value, occurrence)})).toBe(true);
}
async function selectedTextblock(page: Page, value: string) {
const selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.type).toBe('text');
expect(selection.text).toBe(value);
return selection;
}
test.beforeEach(async({page}) => {
await openEditor(page);
});
test('navigates a merged table by its visual grid', async({page}) => {
await setDocument(page, tableDocument([
[
cell('Wide header', 'tableHeader', {colspan: 2, rowspan: 1}),
cell('Right header', 'tableHeader')
],
[cell('Left'), cell('Middle'), cell('Right')]
]));
const descriptors = await blocks(page);
for(const {edge, expected, key, source} of [
{edge: 'end', expected: 'Left', key: 'ArrowDown', source: 'Wide header'},
{edge: 'end', expected: 'Right', key: 'ArrowDown', source: 'Right header'},
{edge: 'start', expected: 'Wide header', key: 'ArrowUp', source: 'Left'},
{edge: 'start', expected: 'Wide header', key: 'ArrowUp', source: 'Middle'},
{edge: 'start', expected: 'Right header', key: 'ArrowUp', source: 'Right'}
] as const) {
await setCaret(page, descriptors, source, edge);
await page.keyboard.press(key);
await settleNativeSelection(page);
await selectedTextblock(page, expected);
}
});
test('navigates a table with combined rowspans and colspans by visual columns', async({page}) => {
await setDocument(page, tableDocument([
[
cell('Spanning', 'tableHeader', {colspan: 2, rowspan: 2}),
cell('Top right', 'tableHeader')
],
[cell('Middle right')],
[cell('Bottom left'), cell('Bottom middle'), cell('Bottom right')]
]));
const descriptors = await blocks(page);
for(const {edge, expected, key, source} of [
{edge: 'end', expected: 'Bottom left', key: 'ArrowDown', source: 'Spanning'},
{edge: 'end', expected: 'Middle right', key: 'ArrowDown', source: 'Top right'},
{edge: 'end', expected: 'Bottom right', key: 'ArrowDown', source: 'Middle right'},
{edge: 'start', expected: 'Spanning', key: 'ArrowUp', source: 'Bottom left'},
{edge: 'start', expected: 'Spanning', key: 'ArrowUp', source: 'Bottom middle'},
{edge: 'start', expected: 'Middle right', key: 'ArrowUp', source: 'Bottom right'}
] as const) {
await setCaret(page, descriptors, source, edge);
await page.keyboard.press(key);
await settleNativeSelection(page);
await selectedTextblock(page, expected);
}
});
test('keeps ArrowDown inside a multiline cell before moving to the next row', async({page}) => {
const document = tableDocument([
[cell('Placeholder'), cell('B')],
[cell('C'), cell('D')]
]);
document.content![0].content![1].content![0].content![0].content = [{
type: 'paragraph',
content: [text('A1'), {type: 'hardBreak'}, text('A2')]
}];
await setDocument(page, document);
const descriptors = await blocks(page);
const multiline = descriptors.findIndex(({text}) => text === 'A1A2');
expect(multiline).toBeGreaterThanOrEqual(0);
expect(await page.evaluate((index) => (
window.chatInputEditorHarness.setCaretInTextblock(index, 2)
), multiline)).toBe(true);
await page.keyboard.press('ArrowDown');
await settleNativeSelection(page);
let selection = await selectedTextblock(page, 'A1A2');
expect(selection.parentOffset).toBeGreaterThan(2);
expect(await page.evaluate((index) => (
window.chatInputEditorHarness.setCaretInTextblock(index, 'end')
), multiline)).toBe(true);
await page.keyboard.press('ArrowDown');
await settleNativeSelection(page);
selection = await selectedTextblock(page, 'C');
expect(selection.parentOffset).toBe(0);
});
test('moves through empty cells and deletes an empty table in two explicit steps', async({page}) => {
await setDocument(page, tableDocument([[cell('A'), cell('')]]));
let descriptors = await blocks(page);
const emptyCell = descriptors.findIndex(({path, text}) => (
path.includes('tableCell') && !text
));
expect(emptyCell).toBeGreaterThanOrEqual(0);
expect(await page.evaluate((index) => (
window.chatInputEditorHarness.setCaretInTextblock(index, 'start')
), emptyCell)).toBe(true);
await page.keyboard.press('Backspace');
await settleNativeSelection(page);
const previous = await selectedTextblock(page, 'A');
expect(previous.parentOffset).toBe(1);
await setDocument(page, tableDocument([[cell('')]], 'Title'));
descriptors = await blocks(page);
const firstCell = descriptors.findIndex(({path}) => path.includes('tableCell'));
expect(await page.evaluate((index) => (
window.chatInputEditorHarness.setCaretInTextblock(index, 'start')
), firstCell)).toBe(true);
await page.keyboard.press('Backspace');
await settleNativeSelection(page);
const title = await selectedTextblock(page, 'Title');
expect(title.parentOffset).toBe('Title'.length);
await setDocument(page, tableDocument([[cell('')]], ''));
await page.locator('.chat-input-table-title').click();
await settleNativeSelection(page);
expect((await page.evaluate(() => window.chatInputEditorHarness.selection())).path)
.toContain('chatTableTitle');
await page.keyboard.press('Backspace');
await settleNativeSelection(page);
expect((await page.evaluate(() => window.chatInputEditorHarness.selection())).type)
.toBe('cell');
expect(await page.evaluate(() => window.chatInputEditorHarness.nodePositions('table')))
.toHaveLength(1);
const frame = page.locator('.chat-input-table-active-cell-frame');
await expect(frame).toBeVisible();
expect(await frame.evaluate((element) => ({
hasSize: Number.parseFloat((element as HTMLElement).style.height) > 0 &&
Number.parseFloat((element as HTMLElement).style.width) > 0,
hidden: (element as HTMLElement).hidden
}))).toEqual({
hasSize: true,
hidden: false
});
await page.keyboard.press('Backspace');
await settleNativeSelection(page);
expect(await page.evaluate(() => window.chatInputEditorHarness.nodePositions('table')))
.toEqual([]);
expect((await page.evaluate(() => window.chatInputEditorHarness.selection())).type)
.toBe('text');
});
test('triple click selects the textblock instead of its table cell', async({page}) => {
const value = 'Whole cell line';
await setDocument(page, tableDocument([[cell(value), cell('Other')]]));
await page.locator('.chat-input-table td [data-chat-input-paragraph]')
.first()
.click({clickCount: 3});
await settleNativeSelection(page);
const selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.type).toBe('text');
expect(selection.text).toBe(value);
expect(selection.to - selection.from).toBe(value.length);
expect(await page.locator('.chat-input-table td.selectedCell').count()).toBe(0);
});
test('cycles through cells with Tab and creates a row only after the final cell', async({page}) => {
await setDocument(page, tableDocument([
[cell('A', 'tableHeader'), cell('B', 'tableHeader')],
[cell('C'), cell('D')]
]));
const descriptors = await blocks(page);
await setCaret(page, descriptors, 'Title', 'end');
for(const value of ['A', 'B', 'C', 'D']) {
await page.keyboard.press('Tab');
await settleNativeSelection(page);
await selectedTextblock(page, value);
}
expect(await page.evaluate(() => window.chatInputEditorHarness.nodePositions('tableRow')))
.toHaveLength(2);
await page.keyboard.press('Tab');
await settleNativeSelection(page);
expect(await page.evaluate(() => window.chatInputEditorHarness.nodePositions('tableRow')))
.toHaveLength(3);
const selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.type).toBe('text');
expect(selection.text).toBe('');
expect(selection.path).toContain('tableCell');
await page.keyboard.press('Shift+Tab');
await settleNativeSelection(page);
await selectedTextblock(page, 'D');
});
test('uses the visual exit arrow in the final RTL table cell', async({page}) => {
await setDocument(page, tableDocument([[cell('A'), cell('B')]]));
await page.evaluate(() => {
const table = document.querySelector<HTMLTableElement>('table.chat-input-table');
if(table) table.style.direction = 'rtl';
});
const descriptors = await blocks(page);
await setCaret(page, descriptors, 'B', 'end');
await page.keyboard.press('ArrowRight');
await settleNativeSelection(page);
const insideSelection = await page.evaluate(() => (
window.chatInputEditorHarness.selection()
));
expect(insideSelection.type).toBe('text');
expect(insideSelection.path).toContain('tableCell');
await setCaret(page, descriptors, 'B', 'end');
await page.keyboard.press('ArrowLeft');
await settleNativeSelection(page);
const selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.type).toBe('text');
expect(selection.text).toBe('');
expect(selection.path).not.toContain('tableCell');
});
test('progresses Select All from cell text to table cells to the whole document', async({page}) => {
await setDocument(page, tableDocument([[cell('Alpha'), cell('Bravo')]]));
const descriptors = await blocks(page);
await setCaret(page, descriptors, 'Alpha', 2);
const selectAll = await page.evaluate(() => (
/Mac|iPhone|iPad|iPod/.test(navigator.platform) ? 'Meta+A' : 'Control+A'
));
await page.keyboard.press(selectAll);
await settleNativeSelection(page);
let selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.type).toBe('text');
expect(selection.from).toBeLessThan(selection.to);
expect(selection.text).toBe('Alpha');
await page.keyboard.press(selectAll);
await settleNativeSelection(page);
selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.type).toBe('cell');
await page.keyboard.press(selectAll);
await settleNativeSelection(page);
selection = await page.evaluate(() => window.chatInputEditorHarness.selection());
expect(selection.type).toBe('all');
});