Files
Llamagochi/tests/test_log_parser.py
Vlad Doloman 28400b3cae feat: ServerState data model with serialization
Add dataclasses for ServerState, LoadingInfo, and Metrics to represent
the server's runtime state. ServerState includes a to_dict() method for
serialization with timestamp.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-22 18:42:45 +03:00

96 lines
3.6 KiB
Python

import pytest
from log_parser import strip_ts, classify_line
# ── strip_ts ────────────────────────────────────────────────────────────────
def test_strip_ts_with_prefix():
line = "[2026-06-22 14:29:49] 0.00.044.403 I srv init: running without SSL"
assert strip_ts(line) == "0.00.044.403 I srv init: running without SSL"
def test_strip_ts_without_prefix():
line = "0.00.044.403 I srv init: running without SSL"
assert strip_ts(line) == "0.00.044.403 I srv init: running without SSL"
def test_strip_ts_child_line():
line = "[2026-06-22 14:31:59] [41803] 0.00.041.673 I log_info: verbosity = 3"
assert strip_ts(line) == "[41803] 0.00.041.673 I log_info: verbosity = 3"
def test_strip_ts_cmd_line():
line = '[2026-06-22 14:31:59] [41803] cmd_child_to_router:state:{"state":"loading"}'
assert strip_ts(line) == '[41803] cmd_child_to_router:state:{"state":"loading"}'
# ── classify_line ────────────────────────────────────────────────────────────
def test_classify_router_line():
kind, port, content = classify_line("0.00.044.403 I srv init: running without SSL")
assert kind == "router"
assert port is None
assert content == "0.00.044.403 I srv init: running without SSL"
def test_classify_child_line():
kind, port, content = classify_line("[41803] 0.00.041.673 I log_info: verbosity = 3")
assert kind == "child"
assert port == 41803
assert content == "0.00.041.673 I log_info: verbosity = 3"
def test_classify_child_cmd_line():
payload = '{"state":"loading","payload":null}'
kind, port, content = classify_line(f"[41803] cmd_child_to_router:state:{payload}")
assert kind == "child"
assert port == 41803
assert content == f"cmd_child_to_router:state:{payload}"
def test_classify_router_proxy_line():
line = "2.10.286.889 I srv proxy_reques: proxying request to model Qwen3 on port 41803"
kind, port, content = classify_line(line)
assert kind == "router"
assert port is None
# ── ServerState data model ───────────────────────────────────────────────────
from log_parser import ServerState, LoadingInfo, Metrics
def test_server_state_defaults():
s = ServerState()
assert s.state == "offline"
assert s.model is None
assert s.loading is None
assert s.request_count == 0
def test_server_state_to_dict_offline():
s = ServerState()
d = s.to_dict()
assert d["state"] == "offline"
assert d["model"] is None
assert d["loading"] is None
assert d["metrics"]["gen_speed"] is None
assert "timestamp" in d
def test_server_state_to_dict_loading():
s = ServerState(
state="loading",
model="Qwen3",
loading=LoadingInfo(stages=["text_model", "mmproj_model"], current="text_model", progress=0.5),
)
d = s.to_dict()
assert d["state"] == "loading"
assert d["loading"]["current"] == "text_model"
assert d["loading"]["progress"] == 0.5
def test_server_state_to_dict_generating():
s = ServerState(
state="generating",
model="Qwen3",
metrics=Metrics(gen_speed=72.2, n_decoded=218),
request_count=5,
)
d = s.to_dict()
assert d["metrics"]["gen_speed"] == 72.2
assert d["metrics"]["n_decoded"] == 218
assert d["request_count"] == 5