mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-08 07:18:47 +00:00
feat(stats): refresh triggers background sync
This commit is contained in:
18
src/main.py
18
src/main.py
@@ -275,12 +275,23 @@ def handle_display_npub(password_manager: PasswordManager):
|
||||
def _display_live_stats(
|
||||
password_manager: PasswordManager, interval: float = 1.0
|
||||
) -> None:
|
||||
"""Continuously refresh stats until the user presses Enter."""
|
||||
"""Continuously refresh stats until the user presses Enter.
|
||||
|
||||
Each refresh also triggers a background sync so the latest stats are
|
||||
displayed if newer data exists on Nostr.
|
||||
"""
|
||||
|
||||
display_fn = getattr(password_manager, "display_stats", None)
|
||||
sync_fn = getattr(password_manager, "start_background_sync", None)
|
||||
if not callable(display_fn):
|
||||
return
|
||||
|
||||
if callable(sync_fn):
|
||||
try:
|
||||
sync_fn()
|
||||
except Exception as exc: # pragma: no cover - sync best effort
|
||||
logging.debug("Background sync failed during stats display: %s", exc)
|
||||
|
||||
if not sys.stdin or not sys.stdin.isatty():
|
||||
clear_screen()
|
||||
display_fn()
|
||||
@@ -292,6 +303,11 @@ def _display_live_stats(
|
||||
return
|
||||
|
||||
while True:
|
||||
if callable(sync_fn):
|
||||
try:
|
||||
sync_fn()
|
||||
except Exception: # pragma: no cover - sync best effort
|
||||
logging.debug("Background sync failed during stats display")
|
||||
clear_screen()
|
||||
display_fn()
|
||||
note = get_notification_text(password_manager)
|
||||
|
@@ -9,7 +9,10 @@ import main
|
||||
|
||||
|
||||
def _make_pm():
|
||||
return SimpleNamespace(display_stats=lambda: print("stats"))
|
||||
return SimpleNamespace(
|
||||
display_stats=lambda: print("stats"),
|
||||
start_background_sync=lambda: None,
|
||||
)
|
||||
|
||||
|
||||
def test_live_stats_shows_message(monkeypatch, capsys):
|
||||
@@ -36,3 +39,21 @@ def test_live_stats_shows_notification(monkeypatch, capsys):
|
||||
main._display_live_stats(pm)
|
||||
out = capsys.readouterr().out
|
||||
assert "note" in out
|
||||
|
||||
|
||||
def test_live_stats_triggers_background_sync(monkeypatch):
|
||||
called = {"sync": 0}
|
||||
|
||||
pm = _make_pm()
|
||||
pm.start_background_sync = lambda: called.__setitem__("sync", called["sync"] + 1)
|
||||
|
||||
monkeypatch.setattr(main, "get_notification_text", lambda *_: "")
|
||||
monkeypatch.setattr(
|
||||
main,
|
||||
"timed_input",
|
||||
lambda *_: (_ for _ in ()).throw(KeyboardInterrupt()),
|
||||
)
|
||||
|
||||
main._display_live_stats(pm)
|
||||
|
||||
assert called["sync"] >= 1
|
||||
|
Reference in New Issue
Block a user