diora-web/tui/tests/test_remote.py

60 lines
2.1 KiB
Python
Raw Normal View History

"""flush_pending / sync_library against a stubbed API (the server side is covered by the real
local server in tools/smoke_server.py)."""
import pytest
import requests
from diora_tui import api, progress, remote
from diora_tui.config import RemoteConfig
T = 1_760_000_000_000
CFG = RemoteConfig("http://x", "tok", "key")
@pytest.fixture(autouse=True)
def fresh_store(tmp_path, monkeypatch):
store = progress.ProgressStore(tmp_path / "p.json")
monkeypatch.setattr(progress, "_default", store)
return store
def test_flush_sends_read_at_and_device_and_clears_the_queue(fresh_store, monkeypatch):
fresh_store.write("s7", "3:0.5", 0.25, T, now=T)
sent = []
def fake_post(server, token, book_id, **kw):
sent.append((book_id, kw))
return {"ok": True, "stored": True, "read_at": T, "position_anchor": "3:0.5", "scroll_fraction": 0.25, "server_now": T}
monkeypatch.setattr(api, "post_progress", fake_post)
assert remote.flush_pending(CFG) == 1
assert sent == [(7, {"anchor": "3:0.5", "fraction": 0.25, "read_at": T, "device": "diora TUI", "force": False})]
assert fresh_store.get("s7").pending is None
def test_flush_keeps_the_queue_when_offline(fresh_store, monkeypatch):
fresh_store.write("s7", "3:0.5", 0.25, T, now=T)
def offline(*a, **kw):
raise requests.ConnectionError("no network")
monkeypatch.setattr(api, "post_progress", offline)
assert remote.flush_pending(CFG) == 0
assert fresh_store.get("s7").pending is not None
def test_flush_drops_the_queue_of_a_deleted_book(fresh_store, monkeypatch):
fresh_store.write("s7", "3:0.5", 0.25, T, now=T)
def gone(*a, **kw):
raise api.ApiError("Buch 7 nicht gefunden (gelöscht oder anderes Konto?)")
monkeypatch.setattr(api, "post_progress", gone)
remote.flush_pending(CFG)
assert fresh_store.get("s7").pending is None
def test_flush_ignores_local_books(fresh_store, monkeypatch):
fresh_store.write("abcdef0123456789", "3:0.5", 0.25, T, now=T) # queue=True by default, but not a server key
monkeypatch.setattr(api, "post_progress", lambda *a, **k: pytest.fail("must not be sent"))
assert remote.flush_pending(CFG) == 0