mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-25 20:18:54 +08:00
feat/https-compose-caddy
1
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
65a219877c |
test(telemetry): move the 530-line session.demo() self-check out of the shipped module (#4106)
## Description
`headroom/telemetry/session.py` ended with `def demo()`, about 530 lines
of `assert` statements, plus `if __name__ == "__main__": demo()`. It was
a self-check that only ran by hand, with `python -m
headroom.telemetry.session`.
It shipped in every wheel and made up about 20% of the module, yet no
test or CI job ran it. A regression it would catch went unnoticed.
This PR moves it, almost verbatim, into
`tests/test_telemetry_session_selfcheck.py` as one test. CI now runs it,
and the product module loses 535 lines.
Evidence that nothing else used it: `rg -n 'demo\(|python -m
headroom.telemetry.session' headroom docs wiki scripts .github` has no
hits for this module. The org's consumer repos do not reference `demo`
either.
Refs #4136
## 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
- `headroom/telemetry/session.py`: delete `demo()` and the `__main__`
guard. No other line changes.
- `tests/test_telemetry_session_selfcheck.py` (new):
`test_session_selfcheck(monkeypatch)` holds the demo body. Every
assertion is the same. Only three mechanical changes:
- The manual `os.environ` save and restore blocks become
`monkeypatch.context()` with `setenv` and `delenv`.
- `globals()["_aggregator"] = ...` becomes `mp.setattr(S, "_aggregator",
...)`, so the swap is undone automatically.
- An `isolated_session` fixture does all the setup and cleanup, so
cleanup runs even when an assertion fails:
- It points `HOME`, `HEADROOM_CONFIG_DIR` and `HEADROOM_WORKSPACE_DIR`
at `tmp_path`. `resource_attributes()` mints and persists an install id,
and the test must not write `~/.headroom/config/install_id` in the
developer's real home.
- It resets the memoised `_install_id` and `_aggregator` through
monkeypatch.
- It clears the four module staging dicts before and after the test.
- It switches the beacon on (`HEADROOM_BEACON=on`, with `DO_NOT_TRACK`
and `HEADROOM_OFFLINE` cleared). `tests/conftest.py` turns the beacon
off for every test, and the staging helpers are no-ops while it is off.
The demo had relied on the ambient environment.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [ ] Manual testing performed
### Test Output
```text
# before the move, on origin/main:
$ python -c "from headroom.telemetry import session; session.demo()"
ok
# after the move, the new test plus the beacon tests that share module state, in both orders:
$ pytest -q -p no:cacheprovider tests/test_telemetry_session_selfcheck.py tests/test_beacon_schema_v2.py tests/test_beacon_gzip.py tests/test_stateless_writers.py tests/test_outcome_dual_ruler_funnel.py tests/test_telemetry.py tests/test_telemetry_context.py
148 passed in 17.50s
# same files, reverse order:
148 passed in 10.91s
# hermeticity: run with an empty HOME, then look for an install id there
$ HOME=<empty dir> pytest -q -p no:cacheprovider tests/test_telemetry_session_selfcheck.py && find <empty dir> -name install_id
1 passed in 2.01s
(no install_id found)
# cleanup on failure: forced an AssertionError mid-test, then ran a probe test that checks the staging dicts are empty and _install_id/_aggregator are None
1 failed (forced), 1 passed (probe)
$ uvx ruff@0.16.8 check headroom/telemetry/session.py tests/test_telemetry_session_selfcheck.py
All checks passed!
$ python -m mypy headroom/telemetry/session.py --ignore-missing-imports
Success: no issues found in 1 source file
```
## Real Behavior Proof
- Environment: macOS, Python 3.12.13, this branch checked out in a
worktree, repo .venv; temp HOME and HEADROOM_WORKSPACE_DIR, offline.
- Exact command / steps: (1) The shared smoke harness `smoke.sh
<worktree>`: it starts a real proxy from this checkout against a local
fake upstream and sends one Anthropic and one OpenAI request with a
compressible tool result. (2) A direct command on this PR's own path:
`python -m headroom.telemetry.session; echo exit=$?; python -c "import
headroom.telemetry.session as s; print('demo' in vars(s))";
HOME=$TMP/home pytest -q tests/test_telemetry_session_selfcheck.py; find
$TMP/home -name install_id | wc -l`.
- Observed result: Both requests return 200 and `/stats` shows savings,
with only the expected offline-mode warnings in proxy.log. The direct
command's output is below.
- Not tested: Nothing at runtime changes; no product code path called
demo().
```text
$ bash smoke.sh <worktree>
branch: cleanup/core-misc-session-selfcheck @
|