Skip to content

Commit a22f4c6

Browse files
committed
test(markdown-viewer): EXPERIMENT trace selection API calls
1 parent ec1d1a4 commit a22f4c6

1 file changed

Lines changed: 48 additions & 2 deletions

File tree

‎apps/sim/scripts/test-markdown-viewer-e2e.ts‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ async function editLastCell(page: Page, marker: string) {
410410
} catch (error) {
411411
failures.push(`first: ${getErrorMessage(error)}`)
412412
}
413-
const iterations = browserName === 'webkit' ? 20 : 6
413+
const iterations = browserName === 'webkit' ? 30 : 2
414414
for (let attempt = 0; attempt < iterations; attempt++) {
415415
await page.reload({ waitUntil: 'domcontentloaded' })
416416
await expect(page.locator('.rich-markdown-prose[contenteditable="true"]')).toBeVisible({
@@ -464,6 +464,52 @@ async function proveKeyboardSelection(page: Page, label: string) {
464464
`${now()} tr doc=${transaction.docChanged} selSet=${transaction.selectionSet} sel=${transaction.selection.from}-${transaction.selection.to} meta=[${metaKeys}] sync=${sync ? JSON.stringify({ origin: sync.isChangeOrigin, undo: sync.isUndoRedoOperation }) : ''} cursor=${cursor ? JSON.stringify(cursor) : ''} domAfter=${dom()} carets=${carets()} toolbar=${document.querySelectorAll('[aria-label="Text formatting"]').length}`
465465
)
466466
})
467+
const stack = () =>
468+
(new Error().stack ?? '')
469+
.split('\n')
470+
.slice(2, 9)
471+
.map((line) => line.replace(/https?:\/\/[^/]+/, '').trim())
472+
.join(' < ')
473+
const selectionMethods = [
474+
'collapse',
475+
'setBaseAndExtent',
476+
'addRange',
477+
'removeAllRanges',
478+
'extend',
479+
'modify',
480+
'collapseToEnd',
481+
'collapseToStart',
482+
'setPosition',
483+
'empty',
484+
'removeRange',
485+
] as const
486+
for (const method of selectionMethods) {
487+
const original = Reflect.get(Selection.prototype, method) as (...args: unknown[]) => unknown
488+
if (typeof original !== 'function') continue
489+
Reflect.set(Selection.prototype, method, function (this: Selection, ...args: unknown[]) {
490+
const before = dom()
491+
const result = original.apply(this, args)
492+
log.push(
493+
`${now()} Selection.${method}(${args.map((arg) => (arg instanceof Node ? (arg.textContent ?? '').slice(0, 8) : String(arg))).join(',')}) ${before} -> ${dom()} pm=${pm()} stack=${stack()}`
494+
)
495+
return result
496+
})
497+
}
498+
const originalFocus = HTMLElement.prototype.focus
499+
HTMLElement.prototype.focus = function (this: HTMLElement, options?: FocusOptions) {
500+
log.push(`${now()} focus() ${this.className.slice(0, 30)} dom=${dom()} stack=${stack()}`)
501+
return originalFocus.call(this, options)
502+
}
503+
document.addEventListener(
504+
'keydown',
505+
(event) => {
506+
if (event.key === 'ArrowRight')
507+
setTimeout(() => log.push(`${now()} after-default ArrowRight dom=${dom()} pm=${pm()}`))
508+
},
509+
true
510+
)
511+
for (const type of ['focus', 'blur', 'mousedown', 'mouseup', 'click'] as const)
512+
document.addEventListener(type, () => log.push(`${now()} ${type} dom=${dom()}`), true)
467513
const observer = new MutationObserver((records) => {
468514
const changed = records.filter((record) => record.type === 'childList').length
469515
if (changed) log.push(`${now()} mutations=${changed} dom=${dom()} carets=${carets()}`)
@@ -474,7 +520,7 @@ async function proveKeyboardSelection(page: Page, label: string) {
474520
const readLog = () =>
475521
page.evaluate(() => {
476522
const log = Reflect.get(window, '__selectionLog') as string[] | undefined
477-
return (log ?? []).slice(0, 120).join('\n')
523+
return (log ?? []).slice(0, 300).join('\n')
478524
})
479525
await page
480526
.locator('.rich-markdown-prose strong')

0 commit comments

Comments
 (0)