import subprocess from kazeia_central.core.adb import parse_content_query from kazeia_central.core.adb.client import Adb, AdbError, parse_call_bundle from kazeia_central.core.provider import models as m def test_parse_rows(): out = ( "Row: 0 pid=1234, uptime_seconds=42, pss_mb=512, ion_mb=300, " "anon_pages_mb=100, mem_available_mb=2048\n" ) rows = parse_content_query(out) assert len(rows) == 1 st = m.StateRow(**rows[0]) assert st.pid == 1234 assert st.mem_available_mb == 2048 def test_parse_null_and_empty(): assert parse_content_query("No result found.") == [] rows = parse_content_query("Row: 0 component=stt, message=NULL") assert rows[0]["message"] is None def test_bool_coercion(): rows = parse_content_query("Row: 0 enabled=1, ready=0, model=e5-small, dim=384") rs = m.RagStatus(**rows[0]) assert rs.enabled is True and rs.ready is False and rs.dim == 384 def test_call_bundle(): b = parse_call_bundle("Result: Bundle[{ok=true, count=3}]") assert b["ok"] == "true" and b["count"] == "3" def test_devices_skips_daemon_noise(): """adb émet des lignes `* daemon ... *` AVANT l'en-tête — ne pas les prendre pour des tablettes (régression : `splitlines()[1:]` en fabriquait).""" out = ( b"* daemon not running; starting now at tcp:5037 *\n" b"* daemon started successfully *\n" b"List of devices attached\n" b"ABC123 device usb:1-1 product:OnePlus model:Pad3 transport_id:5\n\n" ) a = Adb() a._run = lambda *x, **k: subprocess.CompletedProcess(x, 0, stdout=out, stderr=b"") devs = a.devices() assert [d.serial for d in devs] == ["ABC123"] assert devs[0].model == "Pad3" and devs[0].ready def test_query_wakes_provider_on_empty(monkeypatch=None): """Endpoint mono-ligne vide = provider endormi (§3.5) → 1 retry de réveil.""" calls = {"n": 0} def shell(cmd, serial=None, timeout=None): calls["n"] += 1 return "" if calls["n"] == 1 else "Row: 0 pid=1, uptime_seconds=2, pss_mb=3, ion_mb=4" a = Adb() a.shell = shell rows = a.content_query("state", expect_rows=True) assert calls["n"] == 2 and rows # réveil puis succès def test_query_no_retry_on_empty_list(): """Endpoint-liste vide = réponse valide → pas de retry inutile.""" calls = {"n": 0} def shell(cmd, serial=None, timeout=None): calls["n"] += 1 return "No result found." a = Adb() a.shell = shell assert a.content_query("crashes") == [] and calls["n"] == 1 def test_query_retries_on_adb_error(): calls = {"n": 0} def shell(cmd, serial=None, timeout=None): calls["n"] += 1 if calls["n"] == 1: raise AdbError("provider down") return "Row: 0 pid=9, uptime_seconds=1, pss_mb=1, ion_mb=1" a = Adb() a.shell = shell assert a.content_query("state", expect_rows=True) and calls["n"] == 2