headroom/tests/test_providers_opencode_install.py
Parideboy 487aa71a3c
ci: restore green lint (reformat for ruff 0.15.17, fix mypy no-any-return, pin linters) (#1295)
## Description

The CI lint job (`ruff check .` → `ruff format --check .` → `mypy
headroom`) was red on `main`
and therefore on every open PR, for two unrelated reasons that the early
ruff failure was masking:

1. **ruff**: the lint job installs `ruff` unpinned, and ruff 0.15.17
began enforcing import-block
sorting (`I001`) and formatting that older ruff accepted → `ruff check
.` / `ruff format --check .`
   fail on files nobody touched.
2. **mypy**: `headroom/providers/opencode/config.py` had two `return
json.loads(...)` statements in
a function declared `-> dict[str, Any]`; `json.loads` is typed `Any`, so
`mypy headroom` fails
with `no-any-return` (reproduced on mypy 1.20.2 — not a version-specific
quirk).

This restores a green lint baseline and pins both linters so a future
release can't silently break
CI again.

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)

## Changes Made

- `.github/workflows/ci.yml`: pin `ruff==0.15.17` and `mypy==1.20.2` in
the lint job.
- Applied `ruff check --fix .` (6 `I001` import-sort fixes) and `ruff
format .` (10 files) across
  the repo — import ordering and whitespace only, no behavior change.
- `headroom/providers/opencode/config.py`: narrow both
`_parse_json_loose` return sites with an
`isinstance(parsed, dict)` guard, so the `dict[str, Any]` annotation is
true at runtime
(non-dict JSON falls back to `{}`) and mypy's `no-any-return` is
resolved.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`, `ruff format --check .`, `mypy
headroom --ignore-missing-imports`)

### Test Output

```text
$ python -m ruff check .
All checks passed!

$ python -m ruff format --check .
913 files already formatted

$ python -m mypy headroom/providers/opencode/config.py --ignore-missing-imports
Success: no issues found in 1 source file

$ python -m pytest tests/test_providers_opencode_config.py -q
37 passed
```

## Real Behavior Proof

- Environment: Windows 11, Python 3.13, ruff 0.15.17, mypy 1.20.2,
branch ci/fix-ruff-lint off
  headroomlabs-ai/main
- Exact command / steps: reproduced the red lint (latest ruff: 6 `I001`
+ 10 unformatted files;
the mypy failure was read from the #1295 CI lint log —
`config.py:125,133 no-any-return`, and
re-confirmed locally on mypy 1.20.2). Applied the ruff auto-fix/format,
added the dict guard,
  pinned both linters, and re-ran each lint step.
- Observed result: `ruff check .` → "All checks passed!"; `ruff format
--check .` → "913 files
already formatted"; `mypy` on the fixed file → "Success: no issues
found"; full `mypy headroom`
reports only Unix `fcntl` attributes that don't exist on this Windows
box (present on the Linux
CI runner, where the prior run showed exactly the two now-fixed errors).
37 opencode-config
  tests pass.
- Not tested: did not run the full OS/Python test matrix — the change is
formatting + two CI
dependency pins + a two-line type-narrowing guard, with no runtime
behavior change for dict JSON.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-22 15:14:40 -05:00

115 lines
3.5 KiB
Python

"""Tests for OpenCode install-time helpers."""
from __future__ import annotations
from pathlib import Path
import pytest
from headroom.install.models import ConfigScope, DeploymentManifest
from headroom.providers.opencode.install import (
apply_provider_scope,
build_install_env,
revert_provider_scope,
)
def _manifest(port: int = 8787) -> DeploymentManifest:
return DeploymentManifest(
profile="test",
preset="persistent-task",
runtime_kind="python",
supervisor_kind="none",
scope=ConfigScope.PROVIDER.value,
provider_mode="auto",
targets=[],
port=port,
host="127.0.0.1",
backend="anthropic",
proxy_args=[],
base_env={},
tool_envs={},
)
def test_build_install_env() -> None:
"""build_install_env leaves OpenCode provider env vars untouched."""
env = build_install_env(port=8787, backend="anthropic")
assert env == {}
def test_apply_provider_scope_creates_config(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""apply_provider_scope creates the opencode config with headroom provider."""
home = str(tmp_path)
monkeypatch.setenv("HOME", home)
monkeypatch.setenv("USERPROFILE", home)
monkeypatch.delenv("OPENCODE_HOME", raising=False)
monkeypatch.delenv("OPENCODE_CONFIG", raising=False)
manifest = _manifest(port=8787)
mutation = apply_provider_scope(manifest)
assert mutation is not None
assert mutation.target == "opencode"
assert mutation.kind == "json-block"
config_file = tmp_path / ".config" / "opencode" / "opencode.json"
assert config_file.exists()
import json
config = json.loads(config_file.read_text())
assert config["provider"]["headroom"]["options"]["baseURL"] == "http://127.0.0.1:8787/v1"
def test_apply_provider_scope_skips_when_scope_is_not_provider(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""apply_provider_scope returns None when scope is not PROVIDER."""
manifest = _manifest()
manifest.scope = ConfigScope.USER.value
result = apply_provider_scope(manifest)
assert result is None
def test_revert_provider_scope_restores_file(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""revert_provider_scope strips the Headroom block from the config."""
home = str(tmp_path)
monkeypatch.setenv("HOME", home)
monkeypatch.setenv("USERPROFILE", home)
monkeypatch.delenv("OPENCODE_HOME", raising=False)
monkeypatch.delenv("OPENCODE_CONFIG", raising=False)
config_file = tmp_path / ".config" / "opencode" / "opencode.json"
config_file.parent.mkdir(parents=True, exist_ok=True)
config_file.write_text('{"model": "openai/gpt-4o"}')
from headroom.install.models import ManagedMutation
mutation = ManagedMutation(
target="opencode",
kind="json-block",
path=str(config_file),
)
manifest = _manifest()
revert_provider_scope(mutation, manifest)
assert config_file.exists()
assert config_file.read_text().strip() == '{"model": "openai/gpt-4o"}'
def test_revert_provider_scope_noop_when_file_missing(
tmp_path: Path,
) -> None:
"""revert_provider_scope is a safe no-op when the config file is gone."""
from headroom.install.models import ManagedMutation
mutation = ManagedMutation(
target="opencode",
kind="json-block",
path=str(tmp_path / "nonexistent.json"),
)
manifest = _manifest()
revert_provider_scope(mutation, manifest)
# Should not raise