Files
odysseus/tests/test_active_document_visibility_contract.py
T
Léo ab5a08a6f9 refactor(email): move emailLibrary.js into static/js/emailLibrary/
The email library was 11,375 lines in one file, the second-largest JS
module in the repo. `static/js/emailLibrary/` already held four extracted
helpers, so the package existed; the bulk of the code just was not in it.

The implementation moves to `emailLibrary/index.js` and the old path
becomes a re-export wrapper. Five call sites import that path, four of
them dynamically with a `?v=` string, and `sw.js` caches URLs verbatim,
so a wrapper is what makes the move need no coordinated edit to any of
them.

The test side is the part worth reviewing. 58 tests read
`static/js/emailLibrary.js` as text. Pointing them at
`emailLibrary/index.js` would buy one move and break again on the next
one, which is exactly what happened to the stylesheet tests (they now go
through `tests/helpers/stylesheets.py`). So the same shape:
`tests/helpers/js_modules.py` reads the whole package, and assertions
stop caring which module a function sits in.

`tests/test_email_library_module_graph_js.py` is new. This frontend has
no module-graph validation, and a package fails in ways a single file
cannot: a wrapper that drops an export is `undefined` at call time rather
than an error at load time, and a module that reads a `const` across an
import cycle throws only when that module is entered first. It pins the
wrapper's surface against the entry module's, evaluates every module on
its own in a browser, and requires both import paths to hand out one
instance.

It also caught a live gap while being written:
`emailLibrary/replyRecipients.js` is imported by `emailInbox.js`, an app
shell module, and was never in the `sw.js` precache.
2026-09-30 09:08:21 +02:00

74 lines
3.1 KiB
Python

from pathlib import Path
import re
from tests.helpers.js_modules import email_library_paths
ROOT = Path(__file__).resolve().parents[1]
DOCUMENT_JS = (ROOT / "static/js/document.js").read_text(encoding="utf-8")
CHAT_JS = (ROOT / "static/js/chat.js").read_text(encoding="utf-8")
APP_JS = (ROOT / "static/app.js").read_text(encoding="utf-8")
SETTINGS_JS = (ROOT / "static/js/settings.js").read_text(encoding="utf-8")
INDEX_HTML = (ROOT / "static/index.html").read_text(encoding="utf-8")
CHAT_ROUTE = (ROOT / "routes/chat_routes.py").read_text(encoding="utf-8")
def test_visible_or_minimized_linked_document_is_sent_as_chat_context():
function = DOCUMENT_JS.split("export function getChatDocumentId()", 1)[1].split(
"export function getActiveEmailComposerContext()", 1
)[0]
assert "pane?.isConnected" in function
assert "document.body.classList.contains('doc-view')" not in function
assert "style?.display !== 'none'" in function
assert "style?.visibility !== 'hidden'" in function
assert "const id = visiblyOpen ? activeDocId : minimizedId" in function
assert "_minimizedDocId" in function
def test_browser_explicitly_reports_absent_document_context():
assert "? (documentModule?.isPanelOpen?.() ? 'visible' : 'minimized')" in CHAT_JS
assert ": 'none'" in CHAT_JS
def test_chat_and_app_share_one_document_module_instance():
chat_version = re.search(r"from './document\.js\?v=([^']+)'", CHAT_JS).group(1)
app_version = re.search(r"from './js/document\.js\?v=([^']+)'", APP_JS).group(1)
assert chat_version == app_version
def test_all_runtime_document_imports_share_one_module_url():
runtime_files = [
ROOT / "static/app.js",
ROOT / "static/index.html",
ROOT / "static/sw.js",
ROOT / "static/js/chat.js",
ROOT / "static/js/chatStream.js",
ROOT / "static/js/chatRenderer.js",
ROOT / "static/js/slashCommands.js",
*email_library_paths(include_wrapper=True),
]
versions = {
match
for path in runtime_files
for match in re.findall(r"document\.js\?v=([A-Za-z0-9_-]+)", path.read_text(encoding="utf-8"))
}
assert versions == {"20260916docctx2"}
def test_server_fallback_is_legacy_only_when_ui_state_is_absent():
assert "legacy_active_doc_fallback = not active_doc_state" in CHAT_ROUTE
assert CHAT_ROUTE.count("if not active_doc and legacy_active_doc_fallback:") == 3
def test_document_writing_action_uses_document_style_not_email_style():
assert "const generalStyle = String(generalData.document_writing_style || '').trim()" in DOCUMENT_JS
assert "emailResponse = await fetch(`/api/email/style${suffix}`" in DOCUMENT_JS
assert "GENERAL WRITING STYLE:" in DOCUMENT_JS
assert "EMAIL CONVENTIONS:" in DOCUMENT_JS
assert 'id="set-document-style"' in INDEX_HTML
assert 'id="set-document-style-extract"' in INDEX_HTML
assert "'/api/auth/settings/document-style/extract'" in SETTINGS_JS
assert "spinner.createWhirlpool(14)" in SETTINGS_JS
assert 'id="set-document-style-save"' in INDEX_HTML
assert 'M17 21v-8H7v8' in INDEX_HTML
assert "document_writing_style: styleEl.value" in SETTINGS_JS