Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

refactor(chrome-ext): speed up tests (#4603)

Elijah Potterc9398866e813
diff --git a/packages/chrome-plugin/playwright.config.ts b/packages/chrome-plugin/playwright.config.ts
index 633adcd46..c1dbe7498 100644
--- a/packages/chrome-plugin/playwright.config.ts
+++ b/packages/chrome-plugin/playwright.config.ts
@@ -38,7 +38,7 @@ export default defineConfig({
projects: [
{
name: 'chromium',
- workers: process.env.CI ? 1 : '50%',
+ workers: process.env.CI ? 2 : '50%',
use: {
...devices['Desktop Chrome'],
launchOptions: {
@@ -56,7 +56,7 @@ export default defineConfig({
},
{
name: 'firefox',
- workers: process.env.CI ? 1 : '50%',
+ workers: process.env.CI ? 2 : '50%',
use: { ...devices['Desktop Firefox'] },
},
],
diff --git a/packages/chrome-plugin/tests/lexical.spec.ts b/packages/chrome-plugin/tests/lexical.spec.ts
index b0f55a43e..7e04c0608 100644
--- a/packages/chrome-plugin/tests/lexical.spec.ts
+++ b/packages/chrome-plugin/tests/lexical.spec.ts
@@ -1,3 +1,5 @@
+import type { Page } from '@playwright/test';
+import { expect } from './fixtures';
import {
getLexicalEditor,
testBasicSuggestion,
@@ -6,9 +8,15 @@ import {
testMultipleSuggestionsAndUndo,
} from './testUtils';
-const TEST_PAGE_URL = 'https://playground.lexical.dev/';
+const TEST_PAGE_URL = 'http://localhost:8081/lexical.html';
-testBasicSuggestion(TEST_PAGE_URL, getLexicalEditor);
-testCanIgnoreSuggestion(TEST_PAGE_URL, getLexicalEditor);
-testCanBlockRuleSuggestion(TEST_PAGE_URL, getLexicalEditor);
-testMultipleSuggestionsAndUndo(TEST_PAGE_URL, getLexicalEditor);
+/** Wait for the CDN modules, editor, and undo history before sending keyboard input. */
+async function waitForLexicalReady(page: Page) {
+ await expect(page.locator('#status')).toHaveText('Ready', { timeout: 30000 });
+ await expect(getLexicalEditor(page)).toBeEditable();
+}
+
+testBasicSuggestion(TEST_PAGE_URL, getLexicalEditor, waitForLexicalReady);
+testCanIgnoreSuggestion(TEST_PAGE_URL, getLexicalEditor, waitForLexicalReady);
+testCanBlockRuleSuggestion(TEST_PAGE_URL, getLexicalEditor, waitForLexicalReady);
+testMultipleSuggestionsAndUndo(TEST_PAGE_URL, getLexicalEditor, waitForLexicalReady);
diff --git a/packages/chrome-plugin/tests/pages/lexical.html b/packages/chrome-plugin/tests/pages/lexical.html
new file mode 100644
index 000000000..11b754019
--- /dev/null
+++ b/packages/chrome-plugin/tests/pages/lexical.html
@@ -0,0 +1,59 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="UTF-8" />
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
+ <title>Lexical Test</title>
+ <style>
+ body {
+ font-family: sans-serif;
+ padding: 2rem;
+ }
+ #editor {
+ border: 1px solid #ccc;
+ padding: 1rem;
+ min-height: 150px;
+ max-width: 600px;
+ white-space: pre-wrap;
+ }
+ </style>
+ </head>
+ <body>
+ <h1>Lexical Editor</h1>
+ <p id="status" role="status">Loading Lexical…</p>
+ <div id="editor" role="textbox" aria-label="Lexical editor" aria-multiline="true"></div>
+
+ <!-- Real Lexical fixture. Keep all CDN versions in sync so plugins share one Lexical instance. -->
+ <script type="module">
+ const status = document.getElementById('status');
+
+ try {
+ const [lexical, richText, history] = await Promise.all([
+ import('https://cdn.jsdelivr.net/npm/[email protected]/+esm'),
+ import('https://cdn.jsdelivr.net/npm/@lexical/[email protected]/+esm'),
+ import('https://cdn.jsdelivr.net/npm/@lexical/[email protected]/+esm'),
+ ]);
+ const editor = lexical.createEditor({
+ namespace: 'HarperLexicalTest',
+ onError(error) {
+ throw error;
+ },
+ });
+
+ richText.registerRichText(editor);
+ history.registerHistory(editor, history.createEmptyHistoryState(), 300);
+ const root = document.getElementById('editor');
+ editor.setRootElement(root);
+ editor.update(() => {
+ lexical.$getRoot().append(lexical.$createParagraphNode());
+ }, { discrete: true });
+ // Expose the editable only after its initial paragraph exists, as on the playground.
+ root.contentEditable = 'true';
+ status.textContent = 'Ready';
+ } catch (error) {
+ status.textContent = `Lexical initialization failed: ${error.message}`;
+ throw error;
+ }
+ </script>
+ </body>
+</html>
diff --git a/packages/chrome-plugin/tests/testUtils.ts b/packages/chrome-plugin/tests/testUtils.ts
index 5493ad188..a98f8619b 100644
--- a/packages/chrome-plugin/tests/testUtils.ts
+++ b/packages/chrome-plugin/tests/testUtils.ts
@@ -78,7 +78,9 @@ export async function replaceEditorContent(editorEl: Locator, text: string, soft
return;
}
- await editorEl.selectText();
+ // Let the editor update its own selection state through its keyboard handler.
+ await editorEl.focus();
+ await editorEl.press('ControlOrMeta+a');
await editorEl.press('Backspace');
const lines = text.split('\n');
@@ -226,8 +228,7 @@ async function assertEditorText(editor: Locator, text: string) {
/** Substring assertion: `inputValue` for form elements, `toContainText` for contenteditable. */
async function assertEditorContains(editor: Locator, text: string) {
if (await isFormElement(editor)) {
- const value = await editor.inputValue();
- expect(value).toContain(text);
+ await expect.poll(() => editor.inputValue()).toContain(text);
} else {
await expect(editor).toContainText(text);
}
@@ -258,19 +259,17 @@ export async function testBasicSuggestion(
expect(opened).toBe(true);
await page.getByTitle('Click to replace "an" with "a"').click();
- await page.waitForTimeout(3000);
-
await assertEditorText(editor, 'This is a test');
-
- await page.waitForTimeout(3000);
+ await expect(editor).toBeFocused();
// Cursor should be right after "a" (pos 9). ArrowRight×3 + Backspace deletes 'e'.
await page.keyboard.press('ArrowRight');
- await page.waitForTimeout(200);
await page.keyboard.press('ArrowRight');
- await page.waitForTimeout(200);
await page.keyboard.press('ArrowRight');
- await page.waitForTimeout(200);
+ // Let rich editors process the queued selectionchange before deleting.
+ await page.evaluate(
+ () => new Promise<void>((resolve) => requestAnimationFrame(() => resolve())),
+ );
await page.keyboard.press('Backspace');
await assertEditorText(editor, 'This is a tst');
@@ -340,17 +339,13 @@ export async function testCanBlockRuleSuggestion(
}
await replaceEditorContent(editor, 'I could of gone.');
- await page.waitForTimeout(1000);
-
const opened = await clickHarperHighlight(page);
expect(opened).toBe(true);
await page.getByTitle('Disable the ModalOf rule').click();
- await page.waitForTimeout(1000);
-
await assertHarperHighlightBoxes(page, []);
- await assertLocatorIsFocused(page, editor);
+ await expect(editor).toBeFocused();
});
}
@@ -390,16 +385,13 @@ export async function testMultipleSuggestionsAndUndo(
// Soft breaks: no false positives from concatenation + correct span alignment.
await replaceEditorContent(editor, 'Valid words\ntset here.', true);
- await page.waitForTimeout(4000);
- await expect(getHarperHighlights(page)).toHaveCount(1);
+ await expect(getHarperHighlights(page)).toHaveCount(1, { timeout: 30000 });
expect(await clickHarperHighlight(page)).toBe(true);
await page.getByTitle('Click to replace "tset" with "test"').click();
- await page.waitForTimeout(5000);
await assertEditorContains(editor, 'test here');
await replaceEditorContent(editor, 'The first tset.\nThe second tset.\nThe third tset.');
- await page.waitForTimeout(12000);
- await expect(getHarperHighlights(page)).toHaveCount(3);
+ await expect(getHarperHighlights(page)).toHaveCount(3, { timeout: 30000 });
// Get highlights sorted by visual position and click on the middle one
const sortedBoxes = await getSortedHighlightBoxes(page);
@@ -413,16 +405,17 @@ export async function testMultipleSuggestionsAndUndo(
}
await page.getByTitle('Click to replace "tset" with "test"').click();
- await page.waitForTimeout(5000);
+
+ // Wait for the edit before checking the unchanged lines.
+ await assertEditorContains(editor, 'second test');
// Verify only second "tset" was corrected
await assertEditorContains(editor, 'first tset');
- await assertEditorContains(editor, 'second test');
await assertEditorContains(editor, 'third tset');
// Undo
+ await expect(editor).toBeFocused();
await editor.press('Control+z');
- await page.waitForTimeout(3000);
await assertEditorContains(editor, 'The second tset');
});
}
@@ -483,7 +476,11 @@ export async function testPageHasNHighlights(testPageUrl: TestPageUrlProvider, n
const url = await resolveTestPage(testPageUrl, page);
await page.goto(url);
- await page.waitForTimeout(6000);
+ // Zero highlights can be observed before linting starts, so retain the
+ // observation window for negative checks.
+ if (n === 0) {
+ await page.waitForTimeout(6000);
+ }
await assertPageHasNHighlights(page, n);
});
diff --git a/packages/chrome-plugin/vglrunWrapper.js b/packages/chrome-plugin/vglrunWrapper.js
index c4667d62b..0028c57e9 100755
--- a/packages/chrome-plugin/vglrunWrapper.js
+++ b/packages/chrome-plugin/vglrunWrapper.js
@@ -13,7 +13,7 @@ function hasProgram(program) {
if (hasProgram('vglrun')) {
console.log('Has vglrun');
- spawnSync(`vglrun -d :0 -- ${chromiumExecutablePath} ${args}`, {
+ spawnSync(`vglrun -d "$DISPLAY" -- ${chromiumExecutablePath} ${args}`, {
stdio: [0, 1, 2, 3, 4],
shell: true,
});