mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 15:10:00 +08:00
## Description The dashboard has loaded htmx on every page view since the UI's first commit (`1599821c`, 2026-01-30), and vendored it locally since #2734, but nothing in the repository has ever used it. Its interaction code is Alpine plus native `fetch`. A repository-wide search across all file types — `hx-*`, `data-hx-*`, `hx_`, `htmx.on/ajax/process/trigger/config/find/swap`, and `HX-Request`/`HX-Trigger`/`HX-Target`/`HX-Swap` — returns no hit outside the asset itself. `settings.html` never loaded it, and no packaging file names it. This removes the 47,755-byte asset and its references. No replacement library. ## Type of Change - [ ] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [x] Code refactoring (no functional changes) ## Changes Made - Delete `headroom/dashboard/static/htmx.min.js` (47,755 bytes) and its `<script>` tag in `dashboard.html`. - Remove the htmx stanza from `NOTICE` and the `htmx.min.js` entry from the `ASSETS` list in `tests/test_dashboard_static_assets.py` and `tests/test_dashboard_static_mime_types.py`. - Update three `tailwind/htmx/alpine` prose comments to `tailwind/alpine` (`headroom/dashboard/__init__.py`, `headroom/proxy/server.py`, and the test module docstring). - Strengthen `test_dashboard_only_references_served_assets`: it checked one direction only (each `ASSETS` entry appears in the served HTML). It now asserts set equality both ways — every `/dashboard/static/...` reference is in `ASSETS`, and every file in `STATIC_DIR` is in `ASSETS` — so a re-vendored library nobody loads, or a loaded file nobody vendored, fails the suite. No new test file; the existing test already owned this contract. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [x] Manual testing performed The strengthened assertion fails against the tree without this change and passes with it: ### Test Output ```text $ pytest tests/test_dashboard_static_assets.py -q -k only_references # asset restored, template tag restored FAILED tests/test_dashboard_static_assets.py::test_dashboard_only_references_served_assets 1 failed, 8 deselected in 1.82s $ pytest tests/test_dashboard_static_assets.py tests/test_dashboard_static_mime_types.py \ tests/test_dashboard_accessibility.py tests/test_dashboard_agent_usage.py \ tests/test_dashboard_token_savings.py tests/test_cli_dashboard.py \ tests/test_proxy_dashboard_stats_cache.py \ tests/test_dashboard_cache_lifetime_playwright.py \ tests/test_dashboard_cache_net_playwright.py \ tests/test_dashboard_cache_ttl_playwright.py -q 58 passed, 1 skipped in 13.23s $ pre-commit run --files NOTICE headroom/dashboard/__init__.py \ headroom/dashboard/templates/dashboard.html headroom/proxy/server.py \ tests/test_dashboard_static_assets.py tests/test_dashboard_static_mime_types.py ruff (legacy alias)......................................................Passed ruff format..............................................................Passed mypy.....................................................................Passed ``` `tests/test_dashboard/test_live_feed.py` fails 4 tests here, identically with and without this change: the harness waits for `#feed-toggle`, which is not in the template. Pre-existing and unrelated. ## Real Behavior Proof - Environment: macOS 15.6, Python 3.13.11, Playwright 1.52.0 with Chromium Headless Shell 136.0.7103.25, branch based on `main` @ `e67b3c8a`. - Exact command / steps: drove a real browser through the repository's existing accessibility harness (`tests/test_dashboard_accessibility._open`, which serves the real vendored assets from disk) — load `/dashboard`, switch Lifetime then Session, toggle the theme twice, expand a request row, open the live-feed drawer and close it with Escape, then wait one 5 s poll cycle — recording every request, every console warning/error, every page error and the rendered `<main>` text. Ran identically against the tree with htmx restored and with it removed, then diffed. - Observed result: output is identical except the two intended differences — `GET /dashboard/static/htmx.min.js` is gone (14 requests to 13, 47,755 fewer bytes per uncached load) and two JavaScript stack-trace line numbers shift by one, from the deleted `<script>` line. Same console warnings, same errors, same 2,192 characters of rendered `<main>`. Every dashboard section still renders, which is the symptom to watch for here: each lives inside a `<template x-if>` that renders nothing at all if Alpine fails to load. - Not tested: no browser latency was measured and none is claimed — the honest result is one fewer request and 47,755 fewer bytes. Static analysis cannot prove the absence of runtime htmx use; the evidence is the repository-wide search plus the byte-identical browser run, not a proof. Not exercised against a real upstream provider, and not tested on Windows or in Edge, the environment that motivated vendoring these assets in #2734. ## Runtime Rollout Safety - Rollout-managed feature(s): None. The dashboard's static assets are not rollout-gated. - Minimum rollout channel: N/A — no rollout-managed feature is touched. - Stable/default behavior changed: No. The dashboard renders and behaves identically; one script that was parsed and never used is no longer sent. - Kill switch / disable path: N/A — there is no flag to flip. Reverting the commit restores the asset and the script tag. - Unsafe override required: No. - Qualification impact: None. No provider, proxy or optimization path is touched. - Rollback path: `git revert` of this commit restores `htmx.min.js`, the `<script>` tag, the NOTICE stanza and the previous test lists in one step. No data, schema or config migration is involved. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] I did **not** edit `CHANGELOG.md` — it is generated by release-please from my Conventional Commit PR title (a CI guard enforces this) ## Screenshots (if applicable) None. The rendered dashboard is unchanged — verified by comparing the rendered `<main>` text before and after, not by eye. ## Additional Notes - Documentation change is the `NOTICE` entry: the vendored-asset attribution list is the documentation that had to move with this. - One environment note for anyone reproducing the browser proof: the dashboard Playwright tests need `python -m playwright install chromium`. Without it, seven dashboard tests fail before reaching any assertion. - A separate pre-existing issue found while running the suites: `tests/test_dashboard_cache_ttl_playwright.py` writes `dashboard-cache-ttl-main.png` into the repository root on every run, so it shows as a modified tracked file. Left alone here; it deserves its own change.
105 lines
3.1 KiB
Python
105 lines
3.1 KiB
Python
"""The dashboard must not depend on third-party CDNs.
|
|
|
|
Edge's Tracking Prevention (and corporate proxies) block unpkg.com and
|
|
cdn.tailwindcss.com, which left the dashboard unstyled and dataless on some
|
|
Windows machines. Tailwind/alpine are vendored and served locally instead.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import pytest
|
|
|
|
pytest.importorskip("fastapi")
|
|
|
|
from fastapi.responses import Response # noqa: E402
|
|
from fastapi.testclient import TestClient # noqa: E402
|
|
|
|
from headroom.dashboard import ( # noqa: E402
|
|
STATIC_DIR,
|
|
get_dashboard_html,
|
|
get_settings_html,
|
|
)
|
|
from headroom.proxy.server import ProxyConfig, create_app # noqa: E402
|
|
|
|
ASSETS = ["tailwind.min.js", "alpine.min.js"]
|
|
|
|
|
|
@pytest.fixture
|
|
def client(monkeypatch):
|
|
monkeypatch.setenv("HEADROOM_SKIP_UPSTREAM_CHECK", "1")
|
|
app = create_app(
|
|
ProxyConfig(
|
|
optimize=False,
|
|
cache_enabled=False,
|
|
rate_limit_enabled=False,
|
|
cost_tracking_enabled=False,
|
|
log_requests=False,
|
|
http2=False,
|
|
)
|
|
)
|
|
with TestClient(app) as c:
|
|
yield c
|
|
|
|
|
|
@pytest.fixture
|
|
def passthrough(client):
|
|
with patch.object(
|
|
client.app.state.proxy,
|
|
"handle_passthrough",
|
|
new=AsyncMock(return_value=Response("upstream", status_code=418)),
|
|
) as handler:
|
|
yield handler
|
|
|
|
|
|
@pytest.mark.parametrize("html", [get_dashboard_html(), get_settings_html()])
|
|
def test_templates_reference_no_cdn(html: str):
|
|
assert "unpkg.com" not in html
|
|
assert "cdn.tailwindcss.com" not in html
|
|
|
|
|
|
@pytest.mark.parametrize("asset", ASSETS)
|
|
def test_asset_is_served(client, passthrough, asset: str):
|
|
resp = client.get(f"/dashboard/static/{asset}")
|
|
assert resp.status_code == 200, resp.text
|
|
assert len(resp.content) > 10_000
|
|
passthrough.assert_not_called()
|
|
|
|
|
|
def test_dashboard_trailing_slash_is_served_locally(client, passthrough):
|
|
resp = client.get("/dashboard/")
|
|
|
|
assert resp.status_code == 200
|
|
assert resp.text == get_dashboard_html()
|
|
passthrough.assert_not_called()
|
|
|
|
|
|
@pytest.mark.parametrize("path", ["/dashboard/static", "/dashboard/static/"])
|
|
def test_static_directory_boundaries_are_not_forwarded(client, passthrough, path: str):
|
|
resp = client.get(path)
|
|
|
|
assert resp.status_code == 404
|
|
assert resp.text == "Not Found"
|
|
passthrough.assert_not_called()
|
|
|
|
|
|
def test_unrelated_unknown_path_still_reaches_passthrough(client, passthrough):
|
|
resp = client.get("/unrelated-unknown-path")
|
|
|
|
assert resp.status_code == 418
|
|
passthrough.assert_awaited_once()
|
|
|
|
|
|
def test_dashboard_only_references_served_assets(client):
|
|
"""Both directions: nothing vendored goes unloaded, nothing loaded is missing.
|
|
|
|
htmx shipped 47 KB to every dashboard visitor for months without a single
|
|
``hx-*`` attribute or API call anywhere in the tree.
|
|
"""
|
|
html = client.get("/dashboard").text
|
|
|
|
assert set(re.findall(r"/dashboard/static/([\w.-]+)", html)) == set(ASSETS)
|
|
assert {path.name for path in STATIC_DIR.iterdir() if path.is_file()} == set(ASSETS)
|