mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 06:17:25 +00:00
design.js.j2 evaluated the /auth route check once, when the script loaded. KIX changes routes on the client without a document load, so after a route change brand() kept the old answer: routed from /auth to /home it wrote the bare configured title over the page label, routed from /home to /auth it appended the suffix to the translated sign-in title (Welcome to KIX followed by the configured title). The check now runs inside brand(), so each MutationObserver callback reads the current location.pathname. Found by CodeRabbit on PR 2488 and reproduced: the old template returns the bare title and the suffixed sign-in title for the two navigations. test_design_title.py gained a navigation harness that loads the script on one route, moves location.pathname and fires the observer; both cases fail on the old template. Tests: KIX and Semaphore unit tests plus the once-flag, comment and import lints, 35 passed, 20 subtests. 🎯 Fix: 92 % 💥 Break: 2 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: 100 % 🏆 Score: 90 🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 % Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
3.7 KiB
Python
114 lines
3.7 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import shutil
|
|
import subprocess
|
|
import unittest
|
|
|
|
from jinja2 import Environment, select_autoescape
|
|
|
|
from utils.cache.files import read_text
|
|
|
|
from . import PROJECT_ROOT
|
|
|
|
TEMPLATE = PROJECT_ROOT / "roles" / "web-app-kix" / "templates" / "design.js.j2"
|
|
TITLE = "Acme Desk"
|
|
HARNESS = """
|
|
const vm = require("node:vm");
|
|
const script = process.argv[1];
|
|
const cases = JSON.parse(process.argv[2]);
|
|
const titles = cases.map(([pathname, title]) => {
|
|
const document = { title, head: {}, addEventListener() {} };
|
|
const sandbox = { document, location: { pathname }, MutationObserver: class { observe() {} } };
|
|
vm.runInNewContext(script, sandbox);
|
|
return document.title;
|
|
});
|
|
process.stdout.write(JSON.stringify(titles));
|
|
"""
|
|
NAVIGATION_HARNESS = """
|
|
const vm = require("node:vm");
|
|
const script = process.argv[1];
|
|
const cases = JSON.parse(process.argv[2]);
|
|
const titles = cases.map(([fromPath, fromTitle, toPath, toTitle]) => {
|
|
const document = { title: fromTitle, head: {}, addEventListener() {} };
|
|
let onChange = null;
|
|
const location = { pathname: fromPath };
|
|
const MutationObserver = class { constructor(callback) { onChange = callback; } observe() {} };
|
|
vm.runInNewContext(script, { document, location, MutationObserver });
|
|
location.pathname = toPath;
|
|
document.title = toTitle;
|
|
onChange();
|
|
return document.title;
|
|
});
|
|
process.stdout.write(JSON.stringify(titles));
|
|
"""
|
|
NAVIGATIONS = (
|
|
("/auth", "Welcome to KIX", "/home", "Home", f"Home · {TITLE}"),
|
|
("/home", "Home", "/auth", "Welcome to KIX", TITLE),
|
|
)
|
|
CASES = (
|
|
("/auth", "Welcome to KIX", TITLE),
|
|
("/auth", "Willkommen bei KIX", TITLE),
|
|
("/kix/auth", "Welcome to KIX", TITLE),
|
|
("/auth", TITLE, TITLE),
|
|
("/home", "KIX 18", TITLE),
|
|
("/home", "", TITLE),
|
|
("/home", "Home", f"Home · {TITLE}"),
|
|
("/home", f"Home · {TITLE}", f"Home · {TITLE}"),
|
|
("/authors", "Authors", f"Authors · {TITLE}"),
|
|
)
|
|
|
|
|
|
def _render() -> str:
|
|
environment = Environment(autoescape=select_autoescape(default_for_string=False))
|
|
environment.globals["lookup"] = lambda *_: {"title": TITLE}
|
|
environment.filters["to_json"] = json.dumps
|
|
return environment.from_string(read_text(str(TEMPLATE))).render(
|
|
application_id="web-app-kix"
|
|
)
|
|
|
|
|
|
@unittest.skipUnless(shutil.which("node"), "node is not available in PATH")
|
|
class TestDesignTitle(unittest.TestCase):
|
|
def test_every_page_carries_the_configured_title(self) -> None:
|
|
proc = subprocess.run(
|
|
[
|
|
"node",
|
|
"-e",
|
|
HARNESS,
|
|
_render(),
|
|
json.dumps([[path, title] for path, title, _ in CASES]),
|
|
],
|
|
capture_output=True,
|
|
text=True,
|
|
check=False,
|
|
)
|
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
|
for (path, title, expected), actual in zip(
|
|
CASES, json.loads(proc.stdout), strict=True
|
|
):
|
|
with self.subTest(path=path, title=title):
|
|
self.assertEqual(actual, expected)
|
|
|
|
def test_a_client_side_route_change_reads_the_new_route(self) -> None:
|
|
proc = subprocess.run(
|
|
[
|
|
"node",
|
|
"-e",
|
|
NAVIGATION_HARNESS,
|
|
_render(),
|
|
json.dumps([list(case[:4]) for case in NAVIGATIONS]),
|
|
],
|
|
capture_output=True,
|
|
text=True,
|
|
check=False,
|
|
)
|
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
|
for case, actual in zip(NAVIGATIONS, json.loads(proc.stdout), strict=True):
|
|
with self.subTest(navigation=f"{case[0]} -> {case[2]}"):
|
|
self.assertEqual(actual, case[4])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|