fix(editor): ci stability (#14704)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Chores**
* Improved Electron shutdown, diagnostics and tab teardown for more
reliable exits and forced cleanup on stubborn processes.
* **Tests**
* Added polling-based test helpers, stronger scroll/page readiness,
timeout-tolerant page selection, and async cleanup/worker teardown;
updated many tests to wait for UI/model updates and rendering frames.
* **Bug Fixes**
* Reduced flakiness by awaiting paragraph visibility, nested counts,
selection/navigation stability, tab counts, post-action renders, and
safer element interactions.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
DarkSky
2026-03-23 16:25:15 +08:00
committed by GitHub
parent 8ba02ed6fb
commit dcf041a3f2
17 changed files with 619 additions and 256 deletions
+35 -6
View File
@@ -38,9 +38,9 @@ import {
} from './utils/actions/index.js';
import {
assertAlmostEqual,
assertBlockChildrenIds,
assertLocatorVisible,
assertRichImage,
assertRichTextInlineDeltas,
assertRichTextInlineRange,
assertRichTexts,
} from './utils/asserts.js';
@@ -251,6 +251,19 @@ test('should format quick bar be able to change background color', async ({
// );
await highlight.redForegroundBtn.click();
await waitNextFrame(page, 200);
await assertRichTextInlineDeltas(
page,
[
{
attributes: {
color: 'var(--affine-text-highlight-foreground-red)',
},
insert: '456',
},
],
1
);
// TODO(@fundon): these recent settings should be added to the dropdown menu.
// await expect(highlight.highlightBtn).toHaveAttribute(
@@ -265,11 +278,26 @@ test('should format quick bar be able to change background color', async ({
// select `123` paragraph by ctrl + a
await focusRichText(page);
await selectAllByKeyboard(page);
await waitNextFrame(page, 200);
await assertRichTextInlineRange(page, 0, 0, 3);
// // use last used color
// await highlight.highlightBtn.click();
await highlight.highlightBtn.click();
await highlight.redForegroundBtn.click();
await waitNextFrame(page, 200);
await assertRichTextInlineDeltas(
page,
[
{
attributes: {
color: 'var(--affine-text-highlight-foreground-red)',
},
insert: '123',
},
],
0
);
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
`${testInfo.title}_select_all.json`
@@ -278,6 +306,8 @@ test('should format quick bar be able to change background color', async ({
await highlight.highlightBtn.click();
await expect(highlight.defaultColorBtn).toBeVisible();
await highlight.defaultColorBtn.click();
await waitNextFrame(page, 200);
await assertRichTextInlineDeltas(page, [{ insert: '123' }], 0);
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
`${testInfo.title}_default_color.json`
@@ -929,8 +959,7 @@ test('create linked doc from block selection with format bar', async ({
await focusRichText(page, 1);
await pressTab(page);
await assertRichTexts(page, ['123', '456', '789']);
await assertBlockChildrenIds(page, '1', ['2', '4']);
await assertBlockChildrenIds(page, '2', ['3']);
await waitNextFrame(page, 200);
await selectAllBlocksByKeyboard(page);
await waitNextFrame(page, 200);
@@ -980,9 +1009,9 @@ test.describe('more menu button', () => {
await copyBtn.click();
await assertRichTextInlineRange(page, 1, 0, 3);
await focusRichText(page, 1);
await focusRichTextEnd(page, 1);
await pasteByKeyboard(page);
await waitNextFrame(page);
await waitNextFrame(page, 200);
await assertRichTexts(page, ['123', '456456', '789']);
});
@@ -1000,7 +1029,7 @@ test.describe('more menu button', () => {
await expect(duplicateBtn).toBeVisible();
await duplicateBtn.click();
await waitNextFrame(page);
await waitNextFrame(page, 200);
await assertRichTexts(page, ['123', '456', '456', '789']);
});
+14 -7
View File
@@ -33,6 +33,7 @@ import {
} from '../utils/actions/index.js';
import {
assertBlockChildrenIds,
assertRichTextInlineDeltas,
assertRichTextInlineRange,
assertRichTextModelType,
assertRichTexts,
@@ -162,20 +163,23 @@ test('use formatted cursor with hotkey', async ({ page }, testInfo) => {
);
});
test('use formatted cursor with hotkey at empty line', async ({
page,
}, testInfo) => {
test('use formatted cursor with hotkey at empty line', async ({ page }) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);
await focusRichText(page);
// format bold
await page.keyboard.press(`${SHORT_KEY}+b`, { delay: 50 });
await waitNextFrame(page, 200);
await type(page, 'aaa');
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
`${testInfo.title}_bold.json`
);
await assertRichTextInlineDeltas(page, [
{
attributes: {
bold: true,
},
insert: 'aaa',
},
]);
});
test('should single line format hotkey work', async ({ page }, testInfo) => {
@@ -264,6 +268,7 @@ test('format list to h1', async ({ page }) => {
await type(page, 'aa');
await focusRichText(page, 0);
await updateBlockType(page, 'affine:paragraph', 'h1');
await waitNextFrame(page, 200);
await assertRichTextModelType(page, 'h1');
await undoByClick(page);
await assertRichTextModelType(page, 'bulleted');
@@ -280,6 +285,8 @@ test('should cut work single line', async ({ page }, testInfo) => {
await dragBetweenIndices(page, [0, 1], [0, 4]);
// cut
await page.keyboard.press(`${SHORT_KEY}+x`);
await waitNextFrame(page, 200);
await assertRichTexts(page, ['ho']);
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
`${testInfo.title}_init.json`
);
@@ -107,12 +107,15 @@ test('should cut work multiple line', async ({ page }, testInfo) => {
await dragBetweenIndices(page, [0, 1], [2, 2]);
// cut
await page.keyboard.press(`${SHORT_KEY}+x`);
await waitNextFrame(page, 200);
await assertRichTexts(page, ['19']);
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
`${testInfo.title}_init.json`
);
await undoByKeyboard(page);
const text = await readClipboardText(page);
expect(text).toBe(`23 456 78`);
await assertRichTexts(page, ['123', '456', '789']);
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
`${testInfo.title}_undo.json`
);
+26 -6
View File
@@ -188,9 +188,11 @@ test('unindent list block', async ({ page }) => {
await assertBlockChildrenIds(page, '2', ['3']);
await pressShiftTab(page); // 0(1(2,3,4))
await waitNextFrame(page, 200);
await assertBlockChildrenIds(page, '1', ['2', '3', '4']);
await pressShiftTab(page);
await waitNextFrame(page, 200);
await assertBlockChildrenIds(page, '1', ['2', '3', '4']);
});
@@ -204,6 +206,7 @@ test('remove all indent for a list block', async ({ page }) => {
await page.keyboard.press('Tab', { delay: 50 }); // 0(1(2(3(4))))
await assertBlockChildrenIds(page, '3', ['4']);
await pressBackspaceWithShortKey(page); // 0(1(2(3)4))
await waitNextFrame(page, 200);
await assertBlockChildrenIds(page, '1', ['2', '4']);
await assertBlockChildrenIds(page, '2', ['3']);
});
@@ -231,6 +234,7 @@ test('delete at start of list block', async ({ page }) => {
await enterPlaygroundWithList(page);
await focusRichText(page, 1);
await page.keyboard.press('Backspace');
await waitNextFrame(page, 200);
await assertBlockChildrenFlavours(page, '1', [
'affine:list',
'affine:paragraph',
@@ -252,25 +256,41 @@ test('delete at start of list block', async ({ page }) => {
test('nested list blocks', async ({ page }, testInfo) => {
await enterPlaygroundWithList(page);
const focusListItem = async (blockId: string) => {
await page
.locator(`[data-block-id="${blockId}"]`)
.locator('rich-text')
.first()
.click({
force: true,
});
};
await focusRichText(page, 0);
await focusListItem('2');
await type(page, '123');
await focusRichText(page, 1);
await focusListItem('3');
await pressTab(page);
await type(page, '456');
await focusRichText(page, 2);
await focusListItem('4');
await pressTab(page);
await waitNextFrame(page, 200);
await focusListItem('4');
await pressTab(page);
await waitNextFrame(page, 200);
await type(page, '789');
await assertRichTexts(page, ['123', '456', '789']);
await waitNextFrame(page, 200);
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
`${testInfo.title}_init.json`
);
await focusRichText(page, 1);
await focusListItem('3');
await pressShiftTab(page);
await waitNextFrame(page, 200);
await assertRichTexts(page, ['123', '456', '789']);
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
`${testInfo.title}_finial.json`
@@ -595,14 +615,14 @@ test('delete list item with nested children items', async ({ page }) => {
// 4
await pressBackspace(page);
await waitNextFrame(page);
await waitNextFrame(page, 200);
// 1
// |2
// 3
// 4
await pressBackspace(page);
await waitNextFrame(page);
await waitNextFrame(page, 200);
// 1|2
// 3
// 4
+3 -3
View File
@@ -31,6 +31,7 @@ import {
redoByClick,
redoByKeyboard,
resetHistory,
setInlineRangeInInlineEditor,
setSelection,
SHORT_KEY,
switchReadonly,
@@ -87,10 +88,9 @@ test('init paragraph by page title enter in middle', async ({ page }) => {
await waitDefaultPageLoaded(page);
await focusTitle(page);
await type(page, 'hello');
await page.keyboard.press('ArrowLeft');
await page.keyboard.press('ArrowLeft');
await page.keyboard.press('ArrowLeft');
await setInlineRangeInInlineEditor(page, { index: 2, length: 0 });
await pressEnter(page);
await waitNextFrame(page, 200);
await assertTitle(page, 'he');
await assertRichTexts(page, ['llo', '']);
@@ -143,6 +143,7 @@ test('select all should work for multiple notes in doc mode', async ({
async function clickListIcon(page: Page, i = 0) {
const locator = page.locator('.affine-list-block__prefix').nth(i);
await expect(locator).toBeVisible();
await locator.click({
force: true,
position: {
+29 -11
View File
@@ -51,6 +51,7 @@ import {
waitNextFrame,
} from '../utils/actions/index.js';
import {
assertBlockChildrenIds,
assertBlockCount,
assertBlockSelections,
assertClipItems,
@@ -114,6 +115,13 @@ test('native range delete with indent', async ({ page }, testInfo) => {
// abc
// def
// ghi
await assertRichTexts(page, ['123', '456', '789', 'abc', 'def', 'ghi']);
await assertBlockChildrenIds(page, '1', ['2', '5']);
await assertBlockChildrenIds(page, '2', ['3']);
await assertBlockChildrenIds(page, '3', ['4']);
await assertBlockChildrenIds(page, '5', ['6']);
await assertBlockChildrenIds(page, '6', ['7']);
await waitNextFrame(page);
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
`${testInfo.title}_init.json`
@@ -129,18 +137,21 @@ test('native range delete with indent', async ({ page }, testInfo) => {
// ghi
await pressBackspace(page);
await waitNextFrame(page);
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
`${testInfo.title}_after_backspace.json`
);
await waitNextFrame(page);
await undoByKeyboard(page);
await waitNextFrame(page);
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
`${testInfo.title}_after_undo.json`
);
await redoByKeyboard(page);
await waitNextFrame(page);
expect(await getPageSnapshot(page, true)).toMatchSnapshot(
`${testInfo.title}_after_redo.json`
);
@@ -280,22 +291,29 @@ test('cursor move to up and down with children block', async ({ page }) => {
await page.keyboard.press('ArrowRight');
}
await page.keyboard.press('ArrowUp');
const indexOne = await getInlineSelectionIndex(page);
const textOne = await getInlineSelectionText(page);
expect(textOne).toBe('arrow down test 2');
expect(indexOne).toBe(13);
await expect
.poll(async () => {
return {
index: await getInlineSelectionIndex(page),
text: await getInlineSelectionText(page),
};
})
.toEqual({ index: 13, text: 'arrow down test 2' });
for (let i = 0; i < 3; i++) {
await page.keyboard.press('ArrowLeft');
}
await page.keyboard.press('ArrowUp');
const indexTwo = await getInlineSelectionIndex(page);
const textTwo = await getInlineSelectionText(page);
expect(textTwo).toBe('arrow down test 1');
expect(indexTwo).toBeGreaterThanOrEqual(12);
expect(indexTwo).toBeLessThanOrEqual(17);
await expect
.poll(async () => {
const index = await getInlineSelectionIndex(page);
const text = await getInlineSelectionText(page);
return text === 'arrow down test 1' && index >= 12 && index <= 17;
})
.toBe(true);
await page.keyboard.press('ArrowDown');
const textThree = await getInlineSelectionText(page);
expect(textThree).toBe('arrow down test 2');
await expect
.poll(() => getInlineSelectionText(page))
.toBe('arrow down test 2');
});
test('cursor move left and right', async ({ page }) => {
+20 -5
View File
@@ -376,13 +376,28 @@ export async function setEdgelessTool(
'shape',
false
);
// Avoid clicking on the shape-element (will trigger dragging mode)
await shapeToolButton.click({ position: { x: 5, y: 5 } });
const shapeToolBox = await shapeToolButton.boundingBox();
if (!shapeToolBox) {
throw new Error('shapeToolBox is not found');
}
const squareShapeButton = page
.locator('edgeless-slide-menu edgeless-tool-icon-button')
await page.mouse.click(shapeToolBox.x + 2, shapeToolBox.y + 2);
const shapeMenu = page.locator('edgeless-shape-menu');
await expect(shapeMenu).toBeVisible();
const squareShapeButton = shapeMenu
.locator('edgeless-tool-icon-button')
.filter({ hasText: shape });
await squareShapeButton.click();
await expect(squareShapeButton).toBeVisible();
const squareShapeBox = await squareShapeButton.boundingBox();
if (!squareShapeBox) {
throw new Error('squareShapeBox is not found');
}
await page.mouse.click(
squareShapeBox.x + squareShapeBox.width / 2,
squareShapeBox.y + squareShapeBox.height / 2
);
break;
}
}
+27 -32
View File
@@ -549,6 +549,7 @@ export async function focusRichText(
await page.mouse.move(0, 0);
const editor = getEditorHostLocator(page);
const locator = editor.locator(RICH_TEXT_SELECTOR).nth(i);
await expect(locator).toBeVisible();
// need to set `force` to true when clicking on `affine-selected-blocks`
await locator.click({ force: true, position: options?.clickPosition });
}
@@ -1229,43 +1230,37 @@ export async function getCurrentThemeCSSPropertyValue(
}
export async function scrollToTop(page: Page) {
await page.mouse.wheel(0, -1000);
await page.waitForFunction(() => {
const scrollContainer = document.querySelector('.affine-page-viewport');
if (!scrollContainer) {
throw new Error("Can't find scroll container");
}
return scrollContainer.scrollTop < 10;
const scrollContainer = page.locator('.affine-page-viewport');
await expect(scrollContainer).toBeVisible();
await scrollContainer.evaluate(node => {
(node as HTMLElement).scrollTop = 0;
});
await expect
.poll(async () => {
return await scrollContainer.evaluate(node => {
return (node as HTMLElement).scrollTop;
});
})
.toBeLessThan(10);
}
export async function scrollToBottom(page: Page) {
// await page.mouse.wheel(0, 1000);
await page
.locator('.affine-page-viewport')
.evaluate(node =>
node.scrollTo({ left: 0, top: 1000, behavior: 'smooth' })
);
// TODO switch to `scrollend`
// See https://developer.chrome.com/en/blog/scrollend-a-new-javascript-event/
await page.waitForFunction(() => {
const scrollContainer = document.querySelector('.affine-page-viewport');
if (!scrollContainer) {
throw new Error("Can't find scroll container");
}
return (
// Wait for scrolled to the bottom
// Refer to https://stackoverflow.com/questions/3898130/check-if-a-user-has-scrolled-to-the-bottom-not-just-the-window-but-any-element
Math.abs(
scrollContainer.scrollHeight -
scrollContainer.scrollTop -
scrollContainer.clientHeight
) < 10
);
const scrollContainer = page.locator('.affine-page-viewport');
await expect(scrollContainer).toBeVisible();
await scrollContainer.evaluate(node => {
const viewport = node as HTMLElement;
viewport.scrollTop = viewport.scrollHeight;
});
await expect
.poll(async () => {
return await scrollContainer.evaluate(node => {
const viewport = node as HTMLElement;
return Math.abs(
viewport.scrollHeight - viewport.scrollTop - viewport.clientHeight
);
});
})
.toBeLessThan(10);
}
export async function mockParseDocUrlService(
+72 -58
View File
@@ -110,10 +110,13 @@ export async function assertEmpty(page: Page) {
}
export async function assertTitle(page: Page, text: string) {
const editor = getEditorLocator(page);
const inlineEditor = editor.locator('.doc-title-container').first();
const vText = inlineEditorInnerTextToString(await inlineEditor.innerText());
expect(vText).toBe(text);
await expect
.poll(async () => {
const editor = getEditorLocator(page);
const inlineEditor = editor.locator('.doc-title-container').first();
return inlineEditorInnerTextToString(await inlineEditor.innerText());
})
.toBe(text);
}
export async function assertInlineEditorDeltas(
@@ -121,13 +124,16 @@ export async function assertInlineEditorDeltas(
deltas: unknown[],
i = 0
) {
const actual = await page.evaluate(i => {
const inlineRoot = document.querySelectorAll<InlineRootElement>(
'[data-v-root="true"]'
)[i];
return inlineRoot.inlineEditor.yTextDeltas;
}, i);
expect(actual).toEqual(deltas);
await expect
.poll(async () => {
return page.evaluate(i => {
const inlineRoot = document.querySelectorAll<InlineRootElement>(
'[data-v-root="true"]'
)[i];
return inlineRoot?.inlineEditor.yTextDeltas;
}, i);
})
.toEqual(deltas);
}
export async function assertRichTextInlineDeltas(
@@ -135,17 +141,20 @@ export async function assertRichTextInlineDeltas(
deltas: unknown[],
i = 0
) {
const actual = await page.evaluate(
([i]) => {
const editorHost = document.querySelector('editor-host');
const inlineRoot = editorHost?.querySelectorAll<InlineRootElement>(
'rich-text [data-v-root="true"]'
)[i];
return inlineRoot?.inlineEditor.yTextDeltas;
},
[i]
);
expect(actual).toEqual(deltas);
await expect
.poll(async () => {
return page.evaluate(
([i]) => {
const editorHost = document.querySelector('editor-host');
const inlineRoot = editorHost?.querySelectorAll<InlineRootElement>(
'rich-text [data-v-root="true"]'
)[i];
return inlineRoot?.inlineEditor.yTextDeltas;
},
[i]
);
})
.toEqual(deltas);
}
export async function assertText(page: Page, text: string, i = 0) {
@@ -168,7 +177,8 @@ export async function assertRichTexts(page: Page, texts: string[]) {
);
return richTexts.map(richText => {
const editor = richText.inlineEditor as AffineInlineEditor;
return editor.yText.toString();
const text = editor.yText.toString();
return /^\n\s*$/u.test(text) ? '' : text;
});
});
})
@@ -352,20 +362,20 @@ export async function assertRichTextModelType(
type: string,
index = 0
) {
const actual = await page.evaluate(
({ index, BLOCK_ID_ATTR }) => {
const editorHost = document.querySelector('editor-host');
const richText = editorHost?.querySelectorAll('rich-text')[index];
const block = richText?.closest<BlockComponent>(`[${BLOCK_ID_ATTR}]`);
if (!block) {
throw new Error('block component is undefined');
}
return (block.model as BlockModel<{ type: string }>).props.type;
},
{ index, BLOCK_ID_ATTR }
);
expect(actual).toEqual(type);
await expect
.poll(async () => {
return page.evaluate(
({ index, BLOCK_ID_ATTR }) => {
const editorHost = document.querySelector('editor-host');
const richText = editorHost?.querySelectorAll('rich-text')[index];
const block = richText?.closest<BlockComponent>(`[${BLOCK_ID_ATTR}]`);
return (block?.model as BlockModel<{ type: string }> | undefined)
?.props.type;
},
{ index, BLOCK_ID_ATTR }
);
})
.toEqual(type);
}
export async function assertTextFormats(page: Page, resultObj: unknown[]) {
@@ -405,16 +415,19 @@ export async function assertBlockChildrenIds(
blockId: string,
ids: string[]
) {
const actual = await page.evaluate(
({ blockId }) => {
const element = document.querySelector(`[data-block-id="${blockId}"]`);
// @ts-ignore
const model = element.model as BlockModel;
return model.children.map(child => child.id);
},
{ blockId }
);
expect(actual).toEqual(ids);
await expect
.poll(async () => {
return page.evaluate(
({ blockId }) => {
const element = document.querySelector<BlockComponent>(
`[data-block-id="${blockId}"]`
);
return element?.model.children.map(child => child.id);
},
{ blockId }
);
})
.toEqual(ids);
}
export async function assertBlockChildrenFlavours(
@@ -422,18 +435,19 @@ export async function assertBlockChildrenFlavours(
blockId: string,
flavours: string[]
) {
const actual = await page.evaluate(
({ blockId }) => {
const element = document.querySelector<BlockComponent>(
`[data-block-id="${blockId}"]`
await expect
.poll(async () => {
return page.evaluate(
({ blockId }) => {
const element = document.querySelector<BlockComponent>(
`[data-block-id="${blockId}"]`
);
return element?.model.children.map(child => child.flavour);
},
{ blockId }
);
// @ts-ignore
const model = element.model as BlockModel;
return model.children.map(child => child.flavour);
},
{ blockId }
);
expect(actual).toEqual(flavours);
})
.toEqual(flavours);
}
export async function assertParentBlockId(