headroom/tests/test_image_compression_policy.py
JD Davis 2b09ecea76
refactor(proxy): isolate image compression policy (#1958)
## Description

Extracts image-compression gating and tag stamping into a pure policy
module while preserving the public `ImageCompressionDecision.decide` API
used by handlers. This keeps the frozen decision value type separate
from the canonical precedence rules it wraps.

Closes #

## 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

- Added `headroom.proxy.image_compression_policy` for pure
image-compression precedence and tag stamping helpers.
- Updated `ImageCompressionDecision.decide` and
`ImageCompressionDecision.apply_to_tags` to delegate to the extracted
policy.
- Added direct tests for the extracted policy boundary.
- Kept the LiteLLM callback compatibility shim required for repo-wide
type checking on fresh branches.

## 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
python -m pytest tests/test_image_compression_policy.py tests/test_image_compression_decision.py tests/test_handler_outcome_tag_invariant.py tests/test_litellm_callback.py tests/test_compress_api.py::TestLiteLLMCallback -q
34 passed in 6.77s

python -m ruff check .
All checks passed!

python -m ruff format --check .
1095 files already formatted

python -m mypy headroom --ignore-missing-imports
Success: no issues found in 409 source files

gitleaks protect --staged --no-banner --redact
no leaks found
```

## Real Behavior Proof

- Environment: Windows, Python 3.13.13, local worktree based on
`headroomlabs/main`.
- Exact command / steps: ran focused image compression policy/decision
tests, handler outcome tag invariant tests, LiteLLM callback tests, Ruff
lint/format checks, mypy over `headroom`, and staged gitleaks scan.
- Observed result: all local checks passed; staged secret scan found no
leaks.
- Not tested: full CI matrix and deployment flows; those are covered by
GitHub Actions.

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

## Screenshots (if applicable)

N/A

## Additional Notes

Documentation and changelog updates are not applicable for this internal
refactor. GitHub reported existing Dependabot alerts on the default
branch during push; this PR does not change dependencies, and the staged
secret scan is clean.
2026-07-11 10:25:15 -05:00

58 lines
1.7 KiB
Python

"""Tests for pure image-compression decision policy helpers."""
from __future__ import annotations
from headroom.proxy.image_compression_policy import (
apply_image_skip_reason,
decide_image_compression,
)
def test_decide_image_compression_allows_happy_path() -> None:
decision = decide_image_compression(
headers={},
image_optimize_enabled=True,
has_messages=True,
)
assert decision.should_compress is True
assert decision.passthrough_reason is None
def test_decide_image_compression_uses_canonical_precedence() -> None:
decision = decide_image_compression(
headers={"x-headroom-bypass": "true"},
image_optimize_enabled=False,
has_messages=False,
)
assert decision.should_compress is False
assert decision.passthrough_reason == "bypass_header"
assert decision.bypass_header_set is True
assert decision.image_optimize_enabled is False
assert decision.has_messages is False
def test_decide_image_compression_reports_config_and_message_reasons() -> None:
disabled = decide_image_compression(
headers={},
image_optimize_enabled=False,
has_messages=True,
)
empty = decide_image_compression(
headers={},
image_optimize_enabled=True,
has_messages=False,
)
assert disabled.passthrough_reason == "image_optimize_disabled"
assert empty.passthrough_reason == "no_messages"
def test_apply_image_skip_reason_stamps_only_when_skipping() -> None:
tags: dict[str, str] = {}
apply_image_skip_reason(tags, None)
assert tags == {}
apply_image_skip_reason(tags, "no_messages")
assert tags == {"image_skip_reason": "no_messages"}