testsAndMisc/python_pkg/vscode_optimizer/tests/test_render.py
Krzysztof kuhy Rudnicki 9d12c03ccc vscode_optimizer: move under python_pkg and cover 0% -> 100%
meta/scripts/optimize_vscode.py was 498 lines against a 250-line cap and
completely untested, and at that path the repo-wide gate (--cov=python_pkg,
fail_under = 100) could not see it. Moved verbatim to
python_pkg/vscode_optimizer/_optimize.py - git records a pure rename, and
the content is byte-identical to HEAD.

The move and the tests are one commit by necessity: any intermediate state
with the module under python_pkg/ but uncovered turns every pytest run in
the repo red, including ci-mirror on pre-push for unrelated work.

Running it by path no longer works after the move (sys.path[0] would be the
module's own directory), so __main__.py replaces it, following the existing
focus_policy convention rather than a sys.path insert. Nothing invoked the
old path: grep over .sh/.desktop/.service/.yml/.py found zero callers.

98 tests reach 100% line and branch coverage. Patching is confined to real
boundaries - subprocess.run, the module-level Path, builtins.input, sys.argv
- so the forthcoming split has few monkeypatch targets to re-point.

Writing the tests surfaced a pre-existing bug, left unfixed here and pinned
by a strict xfail: _VENDOR_KW's "ati" key is a substring of "VGA compatible
controller", which appears in every lspci display line, so it matches before
"intel" is ever tried and Intel iGPUs are misreported as AMD.

Verified: python3 -m python_pkg.vscode_optimizer --dry-run reproduces the
pre-move 18-change plan and writes nothing; the real settings.json is
untouched; repo-wide pytest is 1846 passed at 100%; over-cap files 37 -> 36.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-17 13:10:10 +02:00

119 lines
3.4 KiB
Python

"""Tests for the terminal output: the hardware summary and the plan listing.
These assert on the informational content of each line rather than its exact
decoration, so the ANSI escapes stay free to change.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from python_pkg.vscode_optimizer import _optimize as opt
if TYPE_CHECKING:
import pytest
def test_show_hw_reports_every_detected_component(
capsys: pytest.CaptureFixture[str],
) -> None:
"""CPU, RAM, GPU and disk each appear, with RAM converted to GB."""
hw = opt._Hw(
cpu_model="AMD Ryzen 9 7900X3D",
cpu_physical_cores=12,
cpu_logical_cores=24,
cpu_max_mhz=5662.6,
ram_total_mb=30000,
gpu_vendor="NVIDIA",
gpu_model="GeForce RTX 3090",
gpu_vram_mb=24576,
disk_type="nvme",
)
opt._show_hw(hw)
out = capsys.readouterr().out
assert "AMD Ryzen 9 7900X3D" in out
assert "12 cores / 24 threads" in out
assert "5663 MHz" in out
assert "29 GB" in out
assert "NVIDIA - GeForce RTX 3090" in out
assert "24576 MB VRAM" in out
assert "NVME" in out
def test_show_hw_omits_vram_when_it_is_unknown(
capsys: pytest.CaptureFixture[str],
) -> None:
"""A card whose VRAM could not be read shows no empty parentheses."""
opt._show_hw(opt._Hw(gpu_vendor="AMD", gpu_model="Radeon"))
assert "VRAM" not in capsys.readouterr().out
def test_show_plan_lists_new_flags_and_settings(
capsys: pytest.CaptureFixture[str],
) -> None:
"""Each proposed change is numbered and shows before -> after."""
opts = [
opt._Opt(
key="editor.minimap.enabled",
value=False,
reason="Minimap costs GPU",
current=True,
)
]
opt._show_plan(opts, ["--enable-zero-copy"], [])
out = capsys.readouterr().out
assert "--enable-zero-copy" in out
assert "1. editor.minimap.enabled" in out
assert "Minimap costs GPU" in out
assert "true" in out
assert "false" in out
assert "2 change(s) proposed" in out
def test_show_plan_says_flags_are_already_present(
capsys: pytest.CaptureFixture[str],
) -> None:
"""Flags that are all already set are reported, not listed as additions."""
opt._show_plan([], ["--enable-zero-copy"], ["--enable-zero-copy"])
out = capsys.readouterr().out
assert "already present" in out
assert "+ --enable-zero-copy" not in out
def test_show_plan_reports_a_fully_optimized_configuration(
capsys: pytest.CaptureFixture[str],
) -> None:
"""With nothing to change, the plan says so and proposes no count."""
opt._show_plan([], [], [])
out = capsys.readouterr().out
assert "All settings already optimized" in out
assert "change(s) proposed" not in out
def test_show_plan_renders_an_addition_with_no_previous_value(
capsys: pytest.CaptureFixture[str],
) -> None:
"""A setting that was never set shows a dash as its 'before'."""
opt._show_plan([opt._Opt("search.maxThreads", 12, "cores")], [], [])
assert "-" in capsys.readouterr().out
def test_show_plan_truncates_very_long_values(
capsys: pytest.CaptureFixture[str],
) -> None:
"""A large exclude dict is clipped so one setting cannot flood the screen."""
big = {f"**/dir{i}": True for i in range(50)}
opt._show_plan([opt._Opt("files.watcherExclude", big, "exclude", big)], [], [])
for line in capsys.readouterr().out.splitlines():
assert len(line) < 200