Files
odysseus/tests/test_document_rich_selection_toolbar.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

127 lines
6.1 KiB
Python

"""Browser coverage for contextual Rich Text selection formatting."""
import json
import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
DOC_JS = (ROOT / "static/js/document.js").read_text(encoding="utf-8")
STYLE = (ROOT / "static/style.css").read_text(encoding="utf-8")
def test_selection_toolbar_uses_shared_formatting_path_and_preserves_range():
assert 'id = \'doc-rich-selection-toolbar\'' in DOC_JS
assert "_restoreRichSelectionToolbarRange(rich)" in DOC_JS
assert "applyMdFormat(button.dataset.richSelectionAction)" in DOC_JS
assert "toolbar.addEventListener('pointerdown', preserve)" in DOC_JS
assert "liveSelection.collapseToEnd()" in DOC_JS
assert "const isMobileViewport = viewportWidth <= 768;" in DOC_JS
assert ".doc-rich-selection-toolbar" in STYLE
def test_selection_toolbar_formats_and_stays_inside_desktop_and_mobile_viewports():
script = r"""
import { chromium } from 'playwright';
const browser = await chromium.launch({ headless: true });
const page = await browser.newPage({ viewport: { width: 900, height: 700 } });
await page.goto(`${process.env.ODYSSEUS_TEST_STATIC_ORIGIN}/static/js/documentOutline.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 () => {
const mod = await import('/static/js/document.js?v=20260831richtexttools91&selection-toolbar-test=1');
mod.init('/api');
mod.injectFreshDoc({
id: 'selection-toolbar-doc',
title: 'Selection toolbar',
language: 'richtext',
current_content: '<p><strong>Bold words</strong> and plain text for formatting.</p><p>Second paragraph.</p>',
version_count: 1,
});
await new Promise(resolve => setTimeout(resolve, 100));
});
await page.evaluate(() => {
const text = document.querySelector('#doc-email-richbody strong').firstChild;
const range = document.createRange();
range.selectNodeContents(text);
const selection = getSelection();
selection.removeAllRanges();
selection.addRange(range);
document.dispatchEvent(new Event('selectionchange'));
});
await page.waitForSelector('#doc-rich-selection-toolbar');
const activeBold = await page.locator('[data-rich-selection-action="bold"]').evaluate(el => el.classList.contains('is-active'));
const desktopGeometry = await page.evaluate(() => {
const selected = getSelection().getRangeAt(0).getBoundingClientRect();
const toolbar = document.querySelector('#doc-rich-selection-toolbar').getBoundingClientRect();
return {
within: toolbar.left >= 0 && toolbar.right <= innerWidth && toolbar.top >= 0 && toolbar.bottom <= innerHeight,
separate: toolbar.bottom <= selected.top || toolbar.top >= selected.bottom,
};
});
await page.locator('[data-rich-selection-action="italic"]').click();
await page.waitForTimeout(40);
const formattedHtml = await page.locator('#doc-email-richbody').innerHTML();
const selectedAfterFormat = await page.evaluate(() => getSelection().toString());
await page.locator('#doc-email-richbody').press('Escape');
await page.waitForTimeout(30);
const dismissed = await page.locator('#doc-rich-selection-toolbar').count() === 0;
await page.setViewportSize({ width: 390, height: 844 });
await page.waitForTimeout(60);
await page.evaluate(() => {
const rich = document.querySelector('#doc-email-richbody');
rich.focus({ preventScroll: true });
const range = document.createRange();
range.selectNodeContents(rich.querySelector('p'));
const selection = getSelection();
selection.removeAllRanges();
selection.addRange(range);
document.dispatchEvent(new Event('selectionchange'));
});
await page.waitForSelector('#doc-rich-selection-toolbar');
await page.waitForTimeout(380);
const mobileGeometry = await page.evaluate(() => {
const selected = getSelection().getRangeAt(0).getBoundingClientRect();
const toolbar = document.querySelector('#doc-rich-selection-toolbar').getBoundingClientRect();
const editor = document.querySelector('#doc-email-richbody').getBoundingClientRect();
const mainToolbar = document.querySelector('#doc-md-toolbar').getBoundingClientRect();
return {
left: toolbar.left,
right: toolbar.right,
top: toolbar.top,
bottom: toolbar.bottom,
width: toolbar.width,
separate: toolbar.bottom <= selected.top || toolbar.top >= selected.bottom,
dockedToEditorBottom: Math.abs(toolbar.bottom - editor.bottom + 4) < 1,
insideEditorTop: toolbar.top >= editor.top,
clearOfMainToolbar: toolbar.top >= mainToolbar.bottom || toolbar.bottom <= mainToolbar.top,
viewport: [innerWidth, innerHeight],
};
});
console.log(JSON.stringify({ activeBold, desktopGeometry, formattedHtml, selectedAfterFormat, dismissed, mobileGeometry }));
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)
assert data["activeBold"] is True
assert data["desktopGeometry"] == {"within": True, "separate": True}
assert "<i>" in data["formattedHtml"] or "<em>" in data["formattedHtml"]
assert data["selectedAfterFormat"] == "Bold words"
assert data["dismissed"] is True
mobile = data["mobileGeometry"]
assert mobile["left"] >= 0 and mobile["right"] <= mobile["viewport"][0]
assert mobile["top"] >= 0 and mobile["bottom"] <= mobile["viewport"][1]
assert mobile["separate"] is True
assert mobile["dockedToEditorBottom"] is True
assert mobile["insideEditorTop"] is True
assert mobile["clearOfMainToolbar"] is True
assert mobile["width"] <= mobile["viewport"][0] - 16