screen-locker/screen_locker/tests/test_mcp.py
Krzysztof kuhy Rudnicki 275a0862fc
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (flutter, flutter-analyze, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,flutter-analyze) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / lock-popup-safety (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (0) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (1) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (2) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (3) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (4) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (5) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (6) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (7) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (8) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (9) (push) Waiting to run
Tests / flutter-coverage (push) Blocked by required conditions
Tests / web-coverage (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shutdown: minutes end to end (*_MINUTES schedule keys, HH:MM helper args, to_hours removed)
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-04 19:48:24 +02:00

149 lines
4.9 KiB
Python

"""Tests for the read-only MCP server tools in ``_mcp``.
Helpers are patched at the ``_mcp`` module namespace (where they are imported),
keeping each tool's own logic under test while isolating the already-tested
leaf functions. ``get_status`` / ``explain_lock`` are exercised against a real
:class:`StatusSnapshot` so the ``dataclasses.asdict`` conversion (and its
JSON-friendliness) is covered for real, not mocked away.
"""
from __future__ import annotations
from dataclasses import asdict
import json
from unittest.mock import patch
from screen_locker import _mcp
from screen_locker._compliance_state import (
AutoUpgradeOpportunity,
LockExplanation,
PredicateResult,
)
from screen_locker._status_data import (
DayStatus,
ManualWorkoutBudgetStatus,
ShutdownProjection,
ShutdownProjectionDay,
SickBudgetStatus,
StatusSnapshot,
WeeklySummary,
)
def _lock_explanation() -> LockExplanation:
return LockExplanation(
fired=True,
stage="would_lock",
reason="No skip condition applies.",
trace=(PredicateResult("scheduled_skip", fired=False, reason="not a skip"),),
auto_upgrade=AutoUpgradeOpportunity(
would_attempt=False, via="none", reason="No pending opportunity."
),
heat_skip_evaluated=False,
)
def _snapshot() -> StatusSnapshot:
day = DayStatus(
date="2026-07-10",
label="Fri Jul 10",
entry_types=(),
source="",
counted=False,
day_count=0,
is_sick_day=False,
)
return StatusSnapshot(
today=day,
week=WeeklySummary(
days=(day,), counted_count=2, minimum=4, remaining=2, extra=0
),
bonus_hours_this_week=0,
streak=1,
early_bird_extended=False,
shutdown=ShutdownProjection(
tonight=(1320, 1320, 300),
rest_of_week=(
ShutdownProjectionDay(label="Mon", minutes=1260, speculative=False),
),
next_week_preview=(
ShutdownProjectionDay(label="Mon", minutes=1260, speculative=True),
),
explanation="explanation text",
),
lock_explanation=_lock_explanation(),
sick_budget=SickBudgetStatus(
used_7d=0,
budget_7d=1,
used_30d=0,
budget_30d=3,
used_90d=0,
budget_90d=10,
debt=0,
exhausted=False,
),
manual_workout_budget=ManualWorkoutBudgetStatus(
used_7d=0, budget_7d=2, used_30d=0, budget_30d=5, exhausted=False
),
generated_at="2026-07-10T12:00:00+00:00",
)
class TestReadTools:
def test_get_status_returns_json_friendly_asdict(self) -> None:
snap = _snapshot()
with patch.object(_mcp, "gather_status", return_value=snap) as gs:
out = _mcp.get_status()
gs.assert_called_once_with()
assert out == asdict(snap)
# asdict output must be pure JSON (no datetime/dataclass leaks).
json.dumps(out)
def test_get_summary_combines_line_and_state(self) -> None:
with (
patch.object(_mcp, "gather_status") as gs,
patch.object(_mcp, "format_summary_line", return_value="LINE") as fsl,
patch.object(_mcp, "compliance_state_word", return_value="ok") as csw,
):
out = _mcp.get_summary()
assert out == {"summary_line": "LINE", "compliance_state": "ok"}
fsl.assert_called_once_with(gs.return_value)
csw.assert_called_once_with(gs.return_value)
def test_explain_lock_returns_lock_explanation_asdict(self) -> None:
snap = _snapshot()
with patch.object(_mcp, "gather_status", return_value=snap):
out = _mcp.explain_lock()
assert out == asdict(snap.lock_explanation)
assert out["fired"] is True
json.dumps(out)
class TestGetFlags:
def test_flags_dict_and_predicate_wiring(self) -> None:
with (
patch.object(_mcp, "load_history", return_value="HIST") as lh,
patch.object(_mcp, "has_logged_today", return_value=True) as hlt,
patch.object(_mcp, "is_scheduled_skip_today", return_value=False) as iss,
patch.object(_mcp, "is_early_bird_pending", return_value=True) as iep,
patch.object(_mcp, "is_sick_day_today", return_value=False) as isd,
):
out = _mcp.get_flags()
assert out == {
"has_logged_today": True,
"is_scheduled_skip_today": False,
"is_early_bird_pending": True,
"is_sick_day_today": False,
}
lh.assert_called_once_with()
hlt.assert_called_once_with(_mcp._LOG_FILE)
iss.assert_called_once_with()
iep.assert_called_once_with(_mcp.EARLY_BIRD_PENDING_FILE)
isd.assert_called_once_with("HIST")
def test_main_runs_stdio_server() -> None:
with patch.object(_mcp.mcp, "run") as run:
_mcp.main()
run.assert_called_once_with()