from flask import render_template_string from l4d2web.app import create_app def _make_app(tmp_path, monkeypatch, db_name: str): db_url = f"sqlite:///{tmp_path/db_name}" monkeypatch.setenv("DATABASE_URL", db_url) return create_app({"TESTING": True, "DATABASE_URL": db_url, "SECRET_KEY": "test"}) def test_base_layout_is_modal_partial_when_hx_modal_header_set(tmp_path, monkeypatch): app = _make_app(tmp_path, monkeypatch, "layout-modal.db") with app.test_request_context("/", headers={"HX-Modal": "1"}): assert render_template_string("{{ base_layout }}") == "_modal_partial.html" def test_base_layout_is_base_html_for_normal_request(tmp_path, monkeypatch): app = _make_app(tmp_path, monkeypatch, "layout-default.db") with app.test_request_context("/"): assert render_template_string("{{ base_layout }}") == "base.html" def test_base_layout_does_not_react_to_plain_hx_request_header(tmp_path, monkeypatch): # HTMX sets HX-Request on every request including the build-status poll; # only HX-Modal should switch the layout. app = _make_app(tmp_path, monkeypatch, "layout-hxreq.db") with app.test_request_context("/", headers={"HX-Request": "true"}): assert render_template_string("{{ base_layout }}") == "base.html"