fix(dashboard): derive per-project setup URL from live origin (#1511)

## Description

The Per-Project Savings empty state currently shows a hardcoded
`ANTHROPIC_BASE_URL: http://127.0.0.1:8787/p/<project-name>`. When the
proxy listens on a fallback or custom port, users can copy a broken
setup URL from the dashboard. This change derives the hint from the
browser's live origin and keeps the existing `/p/<project-name>` suffix
used by per-project savings. Closes #1508.

Related context: #1406 made non-default proxy ports a normal path, which
makes the hardcoded dashboard hint user-visible more often.

## Type of Change

- [x] 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
- [ ] Code refactoring (no functional changes)

## Changes Made

- Replace the static Per-Project Savings setup hint with an Alpine
`x-text` binding that uses `window.location.origin`.
- Preserve the `/p/<project-name>` suffix so the displayed path shape
stays aligned with the existing per-project routing contract.
- Add a Playwright regression that loads the dashboard from non-default
origins and asserts the empty state follows the active page origin
instead of `8787`.

## Testing

- [x] Unit tests pass (`uv run pytest
tests/test_dashboard_cache_ttl_playwright.py -k
"per_project_setup_url_uses_current_origin" -v`)
- [x] Unit tests pass (`uv run pytest
tests/test_owned_asset_encoding.py::test_get_dashboard_html_reads_as_utf8
tests/test_proxy_dashboard_stats_cache.py::test_dashboard_uses_cached_stats_and_lazy_history_feed_polling
-v`)
- [x] Linting passes (`uv run ruff check
tests/test_dashboard_cache_ttl_playwright.py`)
- [ ] Type checking passes (`uv run mypy headroom`)
- [x] New tests added for new functionality when applicable
- [x] Manual testing performed

### Test Output

```text
uv run pytest tests/test_dashboard_cache_ttl_playwright.py -k "per_project_setup_url_uses_current_origin" -v
tests/test_dashboard_cache_ttl_playwright.py::test_dashboard_per_project_setup_url_uses_current_origin PASSED [100%]
================= 1 passed, 1 deselected, 1 warning in 0.82s ==================

uv run pytest tests/test_owned_asset_encoding.py::test_get_dashboard_html_reads_as_utf8 tests/test_proxy_dashboard_stats_cache.py::test_dashboard_uses_cached_stats_and_lazy_history_feed_polling -v
tests/test_owned_asset_encoding.py::test_get_dashboard_html_reads_as_utf8 PASSED [ 50%]
tests/test_proxy_dashboard_stats_cache.py::test_dashboard_uses_cached_stats_and_lazy_history_feed_polling PASSED [100%]
======================== 2 passed, 1 warning in 0.16s =========================

uv run ruff check tests/test_dashboard_cache_ttl_playwright.py
All checks passed!
```

## Real Behavior Proof

- Environment: Playwright Chromium dashboard harness, dashboard template
served through the existing route interception used by the dashboard
tests, no live provider required.
- Exact command / steps: `uv run pytest
tests/test_dashboard_cache_ttl_playwright.py -k
"per_project_setup_url_uses_current_origin" -v`
- Observed result: `http://127.0.0.1:8788/dashboard` passed with the new
current-origin assertion, and `origin/main` failed the same assertion
because the page still rendered `ANTHROPIC_BASE_URL:
http://127.0.0.1:8787/p/<project-name>`. A separate browser check
against `http://headroom.local:9393/dashboard` also passed on the
patched branch.
- Not tested: full live `headroom proxy --port 8788` browser validation,
unless it is run during implementation.

## 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
- [ ] 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 have updated the CHANGELOG.md if applicable

## Additional Notes

`CHANGELOG.md` is intentionally unchanged because this repo generates
changelog entries from conventional commits. The documentation checkbox
is satisfied by correcting the in-dashboard setup instruction.
This commit is contained in:
Rod Boev 2026-06-30 15:24:00 -04:00 committed by GitHub
parent db0356b0e7
commit e035aefce2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View file

@ -1180,7 +1180,7 @@
<div class="text-xs text-gray-500 mb-3">No per-project data yet.</div>
<div class="text-xs text-gray-600 font-mono leading-relaxed">
Add to each project's <span class="text-gray-400">.claude/settings.local.json</span>:<br>
<span class="text-accent">ANTHROPIC_BASE_URL: http://127.0.0.1:8787/p/&lt;project-name&gt;</span>
<span class="text-accent" x-text="'ANTHROPIC_BASE_URL: ' + window.location.origin + '/p/<project-name>'"></span>
</div>
</div>
</template>

View file

@ -187,6 +187,39 @@ def _install_dashboard_routes(page: Page) -> None:
page.route("**/*", handler)
def test_dashboard_per_project_setup_url_uses_current_origin() -> None:
with sync_playwright() as pw:
browser = pw.chromium.launch()
page = browser.new_page(viewport={"width": 1720, "height": 1400}, color_scheme="dark")
_install_dashboard_routes(page)
page.goto("http://127.0.0.1:8788/dashboard", wait_until="load")
expect(
page.get_by_text(
"ANTHROPIC_BASE_URL: http://127.0.0.1:8788/p/<project-name>", exact=True
)
).to_be_visible()
expect(
page.get_by_text(
"ANTHROPIC_BASE_URL: http://127.0.0.1:8787/p/<project-name>", exact=True
)
).to_have_count(0)
page.goto("http://headroom.local:9393/dashboard", wait_until="load")
expect(
page.get_by_text(
"ANTHROPIC_BASE_URL: http://headroom.local:9393/p/<project-name>", exact=True
)
).to_be_visible()
expect(
page.get_by_text(
"ANTHROPIC_BASE_URL: http://127.0.0.1:8787/p/<project-name>", exact=True
)
).to_have_count(0)
browser.close()
def test_dashboard_renders_observed_ttl_metrics_and_can_capture_screenshot() -> None:
artifact_dir = os.environ.get("HEADROOM_PLAYWRIGHT_ARTIFACT_DIR")