Files
uconsole_asw_game/tests/test_commander_static_assets.py
T
db9979andClaude Opus 5.5 4dd23bc242 Web client: CIC redesign with ES modules and bundled fonts
- Remote Crew browser client rebuilt as a dark combat-information-centre layout
  for large desktop monitors: slim status bar (stations, mission, clocks, UTC,
  link state), pulsing alert band, the instrument as centrepiece and
  collapsible docks (contacts, station, contact detail, log drawer; keys , . L).
  Four columns from 2400 px, fluid type up to 4K; guide, lookout and contact
  library open as overlays (Esc closes).
- app.js split into native ES modules under data/commander/js; the transport
  (net/*) no longer touches the DOM and talks to the views through synchronous
  topics (core/events.js, app/bindings.js); redraws share one frame scheduler.
- style.css replaced by a token-based design system in data/commander/css.
- Inter 4.1 and JetBrains Mono 2.304 bundled unmodified (OFL-1.1) with
  provenance in THIRD_PARTY_NOTICES.md; CSP allows font-src 'self'.
- Server serves the css/js/fonts trees from one asset manifest
  (src/commander/assets.py); packaging, capture tool, help, manual and docs
  updated; tests share tests/commander_web.py and import modules directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 12:02:01 +02:00

71 lines
3.1 KiB
Python

"""The Remote Crew static asset manifest (src/commander/assets.py)."""
import hashlib
import re
import pytest
from commander_web import ASSET_DIR, ROOT, tree_files
from src.commander.assets import MIME_TYPES, static_assets, tree_assets
def _write(path, data=b"x"):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(data)
def _fixture(root):
for name in ("index.html", "sonar-audio-worklet.js", "manual.css",
"admin.html", "admin.js", "admin.css", "voice.js", "voice-worklet.js"):
_write(root / name, name.encode())
def test_trees_are_served_with_allowlisted_types_only(tmp_path):
_fixture(tmp_path)
_write(tmp_path / "js" / "net" / "request.js", b"export {}")
_write(tmp_path / "css" / "tokens.css", b":root{}")
_write(tmp_path / "fonts" / "a.woff2", b"wOF2")
_write(tmp_path / "fonts" / "ofl-a.txt", b"OFL")
_write(tmp_path / "js" / "notes.md", b"never served")
_write(tmp_path / "js" / "image.svg", b"<svg/>")
_write(tmp_path / "js" / "__pycache__" / "x.js", b"skip")
_write(tmp_path / "images" / "x.css", b"not a tree")
assets = static_assets(False, tmp_path)
assert assets["/js/net/request.js"] == (MIME_TYPES[".js"], b"export {}")
assert assets["/css/tokens.css"][0] == "text/css; charset=utf-8"
assert assets["/fonts/a.woff2"] == ("font/woff2", b"wOF2")
assert assets["/fonts/ofl-a.txt"][0].startswith("text/plain")
assert not any(route.endswith((".md", ".svg")) or "__pycache__" in route
or route.startswith("/images") for route in assets)
# The local crew listener has no admin/voice routes; the web host has both.
assert "/admin" not in assets and "/voice.js" not in assets
assert {"/admin", "/voice.js"} <= static_assets(True, tmp_path).keys()
@pytest.mark.parametrize("bad", ["Upper/x.js", "a b/x.js", ".hidden/x.js", "a/b/c/d/x.js"])
def test_unexpected_directories_are_rejected(tmp_path, bad):
_write(tmp_path / "js" / bad)
with pytest.raises(ValueError):
tree_assets(tmp_path)
def test_packaged_trees_match_the_manifest():
served = tree_assets()
assert {f"/{path.as_posix()}" for path in tree_files()
if path.suffix in MIME_TYPES} == set(served)
assert served["/fonts/inter-variable.woff2"][0] == "font/woff2"
def test_font_files_match_documented_provenance():
notices = (ROOT / "THIRD_PARTY_NOTICES.md").read_text(encoding="utf-8")
fonts = sorted((ASSET_DIR / "fonts").glob("*.woff2"))
assert [path.name for path in fonts] == [
"inter-variable.woff2", "jetbrains-mono-bold.woff2", "jetbrains-mono-regular.woff2"]
for path in fonts:
digest = hashlib.sha256(path.read_bytes()).hexdigest()
assert f"`{path.name}`" in notices and digest in notices, path.name
for license_name in ("ofl-inter.txt", "ofl-jetbrains-mono.txt"):
text = (ASSET_DIR / "fonts" / license_name).read_text(encoding="utf-8")
assert "SIL Open Font License, Version 1.1" in text
pyproject = (ROOT / "pyproject.toml").read_text(encoding="utf-8")
assert re.search(r'license-files = \[[^]]*"data/commander/fonts/ofl-\*\.txt"', pyproject)