mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-11 20:53:48 +01:00
Tests are now linted by the 10.00 gate (ignore-paths dropped), with no per-test disables. CtlServer/DaemonControl members the tests drive are public (renamed, behaviour unchanged); SteamAPIClient._get takes its params explicitly, as every caller already did. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
95 lines
2.4 KiB
Python
95 lines
2.4 KiB
Python
"""Shared builders for the LeetCode ledger tests.
|
|
|
|
Split out of ``test_leetcode_ledger.py`` to keep every file inside the 250-line
|
|
cap. The ``key_file`` fixture (``fixture_key_file``) is imported by name into
|
|
each test module, which is what registers it there.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import TYPE_CHECKING, Any
|
|
from unittest.mock import patch
|
|
|
|
import earned_time
|
|
import pytest
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Iterator
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
LEDGER_PKG = "steam_backlog_enforcer._leetcode_ledger"
|
|
KEY = b"0123456789abcdef0123456789abcdef"
|
|
|
|
|
|
def sign(entry: dict[str, Any], key: bytes = KEY) -> dict[str, Any]:
|
|
"""Attach a genuine signature to an entry, signed as every locker signs.
|
|
|
|
Args:
|
|
entry: The entry to sign.
|
|
key: The signing key.
|
|
|
|
Returns:
|
|
The same entry with an ``hmac`` field.
|
|
"""
|
|
entry["hmac"] = earned_time.entry_signature(entry, key)
|
|
return entry
|
|
|
|
|
|
def credit(*, when: datetime, entry_id: str = "ac:1") -> dict[str, Any]:
|
|
"""Build a signed credit entry submitted at *when*.
|
|
|
|
Args:
|
|
when: The submission time.
|
|
entry_id: The entry id, unique per credit.
|
|
|
|
Returns:
|
|
A signed credit entry.
|
|
"""
|
|
return sign(
|
|
{
|
|
"entry_id": entry_id,
|
|
"kind": "credit",
|
|
"day": when.date().isoformat(),
|
|
"created_at": when.isoformat(),
|
|
"amount": 1,
|
|
"device": "pc",
|
|
"detail": {
|
|
"source": "leetcode",
|
|
"submitted_at": str(int(when.timestamp())),
|
|
},
|
|
}
|
|
)
|
|
|
|
|
|
def write_ledger(path: Path, entries: list[dict[str, Any]]) -> Path:
|
|
"""Write a ledger file.
|
|
|
|
Args:
|
|
path: Directory to write into.
|
|
entries: The entries to store.
|
|
|
|
Returns:
|
|
The ledger path.
|
|
"""
|
|
ledger = path / "ledger.json"
|
|
ledger.write_text(json.dumps({"version": 1, "entries": entries}), encoding="utf-8")
|
|
return ledger
|
|
|
|
|
|
@pytest.fixture(name="key_file")
|
|
def fixture_key_file(tmp_path: Path) -> Iterator[Path]:
|
|
"""Point the module at a throwaway signing key.
|
|
|
|
Args:
|
|
tmp_path: pytest's temporary directory.
|
|
|
|
Yields:
|
|
The key path, with the module patched to use it.
|
|
"""
|
|
path = tmp_path / "hmac.key"
|
|
path.write_bytes(KEY)
|
|
with patch(f"{LEDGER_PKG}.HMAC_KEY_FILE", path):
|
|
yield path
|