test: reorganize core suite into explicit domain files

This commit is contained in:
2026-02-18 03:17:33 +01:00
parent bd2bd43e7f
commit cd99960f2f
19 changed files with 805 additions and 326 deletions

View File

@@ -0,0 +1,56 @@
from __future__ import annotations
from typing import TypeAlias
from auditui.app.bindings import BINDINGS
from textual.binding import Binding
BindingTuple: TypeAlias = tuple[str, str, str]
NormalizedBinding: TypeAlias = tuple[str, str, str, bool]
EXPECTED_BINDINGS: tuple[NormalizedBinding, ...] = (
("?", "show_help", "Help", False),
("s", "show_stats", "Stats", False),
("/", "filter", "Filter", False),
("escape", "clear_filter", "Clear filter", False),
("n", "sort", "Sort by name", False),
("p", "sort_by_progress", "Sort by progress", False),
("a", "show_all", "All/Unfinished", False),
("r", "refresh", "Refresh", False),
("enter", "play_selected", "Play", False),
("space", "toggle_playback", "Pause/Resume", True),
("left", "seek_backward", "-30s", False),
("right", "seek_forward", "+30s", False),
("ctrl+left", "previous_chapter", "Previous chapter", False),
("ctrl+right", "next_chapter", "Next chapter", False),
("up", "increase_speed", "Increase speed", False),
("down", "decrease_speed", "Decrease speed", False),
("f", "toggle_finished", "Mark finished", False),
("d", "toggle_download", "Download/Delete", False),
("q", "quit", "Quit", False),
)
def _normalize_binding(binding: Binding | BindingTuple) -> NormalizedBinding:
"""Return key, action, description, and priority from one binding item."""
if isinstance(binding, Binding):
return (binding.key, binding.action, binding.description, binding.priority)
key, action, description = binding
return (key, action, description, False)
def _all_bindings() -> list[NormalizedBinding]:
"""Normalize all app bindings into a stable comparable structure."""
return [_normalize_binding(binding) for binding in BINDINGS]
def test_bindings_match_expected_shortcuts() -> None:
"""Ensure the shipped shortcut list stays stable and explicit."""
assert _all_bindings() == list(EXPECTED_BINDINGS)
def test_binding_keys_are_unique() -> None:
"""Ensure each key is defined only once to avoid dispatch ambiguity."""
keys = [binding[0] for binding in _all_bindings()]
assert len(keys) == len(set(keys))

View File

@@ -0,0 +1,64 @@
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any, cast
from auditui.app import Auditui
from auditui.library import build_search_text, filter_items
class StubLibrary:
"""Minimal library facade used by search-related app helpers."""
def extract_title(self, item: dict) -> str:
"""Return title from a synthetic item."""
return item.get("title", "")
def extract_authors(self, item: dict) -> str:
"""Return authors from a synthetic item."""
return item.get("authors", "")
@dataclass(slots=True)
class DummyAuditui:
"""Narrow object compatible with Auditui search-cache helper calls."""
_search_text_cache: dict[int, str] = field(default_factory=dict)
library_client: StubLibrary = field(default_factory=StubLibrary)
def test_get_search_text_is_cached() -> None:
"""Ensure repeated text extraction for one item reuses cache entries."""
item = {"title": "Title", "authors": "Author"}
dummy = DummyAuditui()
first = Auditui._get_search_text(cast(Auditui, dummy), item)
second = Auditui._get_search_text(cast(Auditui, dummy), item)
assert first == "title author"
assert first == second
assert len(dummy._search_text_cache) == 1
def test_filter_items_uses_cached_callable() -> None:
"""Ensure filter_items cooperates with a memoized search text callback."""
library = StubLibrary()
cache: dict[int, str] = {}
items = [
{"title": "Alpha", "authors": "Author One"},
{"title": "Beta", "authors": "Author Two"},
]
def cached(item: dict) -> str:
"""Build and cache normalized search text per object identity."""
cache_key = id(item)
if cache_key not in cache:
cache[cache_key] = build_search_text(item, cast(Any, library))
return cache[cache_key]
result = filter_items(items, "beta", cached)
assert result == [items[1]]
def test_build_search_text_without_library_client() -> None:
"""Ensure fallback search text path handles inline author dicts."""
item = {"title": "Title", "authors": [{"name": "A"}, {"name": "B"}]}
assert build_search_text(item, None) == "title a, b"