Files
odysseus/tests/test_document_rich_keyboard_shortcuts.py
T
Léo 9b2185f1cc test(harness): bind the static test server to an ephemeral port
The session-scoped autouse fixture bound 127.0.0.1:7011 and raised when the
port was taken. Because it is autouse, that raise errored every collected
test rather than the browser ones: a second worktree running its own suite
produced 10,612 errors, none of them about the code under test. 7011 is also
the application's own default port, so the suite could not run while a local
instance was up.

Bind port 0 instead and publish the resulting origin as
ODYSSEUS_TEST_STATIC_ORIGIN. The browser tests shell out to node, which
inherits the environment, so the snippets read process.env rather than
hardcoding a port. ODYSSEUS_TEST_STATIC_PORT still pins one when something
outside pytest has to reach the server; that is the only path that can now
fail to bind, and it fails with a message that says so.

Two concurrent full runs from one checkout now both pass. Only the docx export
snippet is an rf-string, so it is the only one whose JS braces needed doubling.
2026-09-25 10:45:58 +02:00

118 lines
5.7 KiB
Python

"""Browser coverage for standard Rich Text document shortcuts."""
import json
import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def test_rich_document_shortcuts_work_at_desktop_and_mobile_widths():
script = r"""
import { chromium } from 'playwright';
const browser = await chromium.launch({ headless: true });
async function exercise(viewport, suffix) {
const page = await browser.newPage({ viewport });
await page.goto(`${process.env.ODYSSEUS_TEST_STATIC_ORIGIN}/static/js/documentStats.js`);
await page.setContent('<link rel="stylesheet" href="/static/style.css?v=20260831richtexttools91"><div id="toast"></div><div id="chat-container"></div><div id="sidebar"></div>');
await page.evaluate(async suffix => {
const mod = await import(`/static/js/document.js?v=20260831richtexttools91&keyboard-shortcuts=${suffix}`);
mod.init('/api');
mod.injectFreshDoc({
id: `keyboard-shortcuts-${suffix}`,
title: 'Keyboard shortcuts',
language: 'richtext',
current_content: '<p>Heading target</p><p>Align target</p><p>Indent target</p><p>Strike target</p>',
version_count: 1,
});
await new Promise(resolve => setTimeout(resolve, 400));
}, suffix);
async function selectText(text, collapse = true) {
await page.evaluate(({ text, collapse }) => {
const rich = document.querySelector('#doc-email-richbody');
const walker = document.createTreeWalker(rich, NodeFilter.SHOW_ELEMENT);
let block = null;
while (walker.nextNode()) {
if (walker.currentNode.textContent === text) {
block = walker.currentNode;
break;
}
}
const range = document.createRange();
range.selectNodeContents(block);
if (collapse) range.collapse(true);
const selection = getSelection();
selection.removeAllRanges();
selection.addRange(range);
rich.focus();
}, { text, collapse });
}
await selectText('Heading target');
await page.keyboard.press('Control+Alt+6');
const headingTag = await page.locator('#doc-email-richbody').evaluate(root => root.firstElementChild.tagName);
await page.locator('[data-dd="heading"]').click();
const h6Current = await page.locator('#doc-md-dd-menu .doc-overflow-item').filter({ hasText: 'Heading 6' }).getAttribute('aria-checked');
await page.keyboard.press('Escape');
await selectText('Heading target');
await page.keyboard.press('Control+Alt+0');
const paragraphTag = await page.locator('#doc-email-richbody').evaluate(root => root.firstElementChild.tagName);
await selectText('Align target');
await page.keyboard.press('Control+Shift+e');
const center = await page.locator('#doc-email-richbody').evaluate(root => root.children[1].style.textAlign);
await page.locator('[data-dd="align"]').click();
await page.locator('#doc-md-dd-menu .doc-overflow-item').filter({ hasText: 'Align right' }).click();
const right = await page.locator('#doc-email-richbody').evaluate(root => root.children[1].style.textAlign);
await page.keyboard.press('Control+Shift+j');
const justify = await page.locator('#doc-email-richbody').evaluate(root => root.children[1].style.textAlign);
await page.keyboard.press('Control+Shift+l');
const left = await page.locator('#doc-email-richbody').evaluate(root => root.children[1].style.textAlign);
await selectText('Indent target');
await page.keyboard.press('Control+]');
const indented = await page.locator('#doc-email-richbody').evaluate(root => !!Array.from(root.children).find(child => child.tagName === 'BLOCKQUOTE' && child.textContent === 'Indent target'));
await page.keyboard.press('Control+[');
const outdented = await page.locator('#doc-email-richbody').evaluate(root => Array.from(root.children).some(child => child.tagName === 'P' && child.textContent === 'Indent target'));
await selectText('Strike target', false);
await page.keyboard.press('Control+Shift+5');
const struck = await page.locator('#doc-email-richbody').evaluate(root => !!root.querySelector('strike, s'));
const geometry = await page.evaluate(() => ({
viewport: document.documentElement.clientWidth,
document: document.documentElement.scrollWidth,
}));
await page.close();
return { headingTag, h6Current, paragraphTag, center, right, justify, left, indented, outdented, struck, geometry };
}
const desktop = await exercise({ width: 900, height: 700 }, 'desktop');
const mobile = await exercise({ width: 390, height: 844 }, 'mobile');
console.log(JSON.stringify({ desktop, mobile }));
await browser.close();
"""
result = subprocess.run(
["node", "--input-type=module", "-e", script],
cwd=ROOT,
check=False,
capture_output=True,
text=True,
)
assert result.returncode == 0, result.stderr
data = json.loads(result.stdout)
for layout in (data["desktop"], data["mobile"]):
assert layout["headingTag"] == "H6"
assert layout["h6Current"] == "true"
assert layout["paragraphTag"] == "P"
assert layout["center"] == "center"
assert layout["right"] == "right"
assert layout["justify"] == "justify"
assert layout["left"] == "left"
assert layout["indented"] is True
assert layout["outdented"] is True
assert layout["struck"] is True
assert layout["geometry"]["document"] <= layout["geometry"]["viewport"]