mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-11-10 20:27:13 +01:00
`requires-python` and the CI pin already said 3.14, but ruff, mypy and pylint were still grading against 3.10 -- three tools judging an interpreter nothing runs any more, and ruff withholding the pyupgrade fixes that only become safe at the newer target. `pin_ci_python.py` rewrites exactly these three knobs. What the stale target was hiding: 118 sites on `datetime.timezone.utc` instead of `datetime.UTC` (UP017), two classes still written with `Generic[...]` rather than PEP 695 syntax, and ruff's stable formatter applying PEP 758 -- `except (A, B):` becomes `except A, B:`. That last one makes the source 3.14-only at the syntax level, which is what `requires-python = ">=3.14"` already promised; kuhy chose it deliberately over holding ruff a release back (2026-08-29). The PEP 695 fix needed a hand: ruff rewrote `class WidgetGroup(Generic[_W])` to `class WidgetGroup[W: tk.Misc]` but left the body annotated with the module-level `_W`, a *different* type variable -- mypy caught it as "a function returning TypeVar should receive at least one argument containing the same TypeVar". Renamed the body to the new parameter, gave `FrameGroup.__init__` the concrete `list[tk.Frame]` it always wrapped, and deleted the two now-dead module-level TypeVars. ruff, ruff-format, mypy --strict and both pylint passes clean at the new target. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SfnJmxxsZ534AKk16rC2Gr
134 lines
4.2 KiB
Python
134 lines
4.2 KiB
Python
"""Tests for screen_locker initialization, logging, and basic operations."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
import json
|
|
from typing import TYPE_CHECKING, Any
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from screen_locker.tests.conftest import create_locker
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
|
|
class TestSaveWorkoutLog:
|
|
"""Tests for save_workout_log method."""
|
|
|
|
def test_save_to_new_file(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test saving to a new log file includes HMAC."""
|
|
log_file = tmp_path / "log.json"
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
locker.log_file = log_file
|
|
locker.workout_data = {"type": "running"}
|
|
with patch(
|
|
"screen_locker._log_mixin.compute_entry_hmac",
|
|
return_value="abc123",
|
|
):
|
|
locker.save_workout_log()
|
|
|
|
assert log_file.exists()
|
|
with log_file.open() as f:
|
|
data: dict[str, Any] = json.load(f)
|
|
today = datetime.now(tz=UTC).strftime("%Y-%m-%d")
|
|
assert today in data
|
|
# The log now holds a list of entries per day (multiple workouts/day).
|
|
assert data[today][0]["workout_data"]["type"] == "running"
|
|
assert data[today][0]["hmac"] == "abc123"
|
|
|
|
def test_save_to_new_file_no_hmac_key(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test saving without HMAC key produces unsigned entry."""
|
|
log_file = tmp_path / "log.json"
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
locker.log_file = log_file
|
|
locker.workout_data = {"type": "running"}
|
|
with patch(
|
|
"screen_locker._log_mixin.compute_entry_hmac",
|
|
return_value=None,
|
|
):
|
|
locker.save_workout_log()
|
|
|
|
with log_file.open() as f:
|
|
data: dict[str, Any] = json.load(f)
|
|
today = datetime.now(tz=UTC).strftime("%Y-%m-%d")
|
|
assert "hmac" not in data[today][0]
|
|
|
|
def test_save_to_existing_file(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test saving appends to existing log file."""
|
|
log_file = tmp_path / "log.json"
|
|
log_file.write_text(json.dumps({"2020-01-01": {"old": "data"}}))
|
|
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
locker.log_file = log_file
|
|
locker.workout_data = {"type": "strength"}
|
|
with patch(
|
|
"screen_locker._log_mixin.compute_entry_hmac",
|
|
return_value="sig",
|
|
):
|
|
locker.save_workout_log()
|
|
|
|
with log_file.open() as f:
|
|
data: dict[str, Any] = json.load(f)
|
|
assert "2020-01-01" in data
|
|
today = datetime.now(tz=UTC).strftime("%Y-%m-%d")
|
|
assert today in data
|
|
|
|
def test_save_with_corrupted_existing_file(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test saving when existing file is corrupted."""
|
|
log_file = tmp_path / "log.json"
|
|
log_file.write_text("not valid json")
|
|
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
locker.log_file = log_file
|
|
locker.workout_data = {"type": "running"}
|
|
with patch(
|
|
"screen_locker._log_mixin.compute_entry_hmac",
|
|
return_value="sig",
|
|
):
|
|
locker.save_workout_log()
|
|
|
|
with log_file.open() as f:
|
|
data: dict[str, Any] = json.load(f)
|
|
today = datetime.now(tz=UTC).strftime("%Y-%m-%d")
|
|
assert today in data
|
|
|
|
def test_save_with_write_error(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test saving handles write errors gracefully."""
|
|
log_file = tmp_path / "nonexistent_dir" / "log.json"
|
|
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
locker.log_file = log_file
|
|
locker.workout_data = {"type": "running"}
|
|
with patch(
|
|
"screen_locker._log_mixin.compute_entry_hmac",
|
|
return_value="sig",
|
|
):
|
|
# Should not raise, just log warning
|
|
locker.save_workout_log()
|