update
This commit is contained in:
@@ -226,3 +226,60 @@ async def test_remove_guide_format_clears_everything(testdb, monkeypatch):
|
||||
assert res["removed"] == 3
|
||||
assert await db.list_guides() == [] or all(g["topic"] != TOPIC for g in await db.list_guides())
|
||||
assert await db.list_guide_cards(TOPIC, "Guide") == []
|
||||
|
||||
|
||||
# ── Token-Logging pro Agent (OpenCode-Session → Event-Meta) ──────────────────────────
|
||||
|
||||
def test_session_tokens_reads_newest(tmp_path, monkeypatch):
|
||||
"""--title = Agent-Key: neueste Session gewinnt (Retry); fehlende DB/Zeile → None."""
|
||||
import sqlite3
|
||||
dbf = tmp_path / "oc.db"
|
||||
con = sqlite3.connect(dbf)
|
||||
con.execute("CREATE TABLE session (title TEXT, time_created INT, tokens_input INT,"
|
||||
" tokens_output INT, tokens_reasoning INT, tokens_cache_read INT, tokens_cache_write INT)")
|
||||
con.execute("INSERT INTO session VALUES ('k', 1, 1, 1, 0, 10, 0)")
|
||||
con.execute("INSERT INTO session VALUES ('k', 2, 7, 3, 0, 99, 5)")
|
||||
con.commit()
|
||||
con.close()
|
||||
monkeypatch.setattr(agents, "_OPENCODE_DB", dbf)
|
||||
assert agents._session_tokens("k") == {"input": 7, "output": 3, "reasoning": 0,
|
||||
"cache_read": 99, "cache_write": 5}
|
||||
assert agents._session_tokens("fehlt") is None
|
||||
monkeypatch.setattr(agents, "_OPENCODE_DB", tmp_path / "nope.db")
|
||||
assert agents._session_tokens("k") is None
|
||||
|
||||
|
||||
async def test_opencode_cmd_sets_title(monkeypatch):
|
||||
"""Der Agent-Key wird Session-Titel — der Join-Schlüssel fürs Token-Logging."""
|
||||
seen = {}
|
||||
|
||||
async def fake_comm(agent_key, cmd, stdin, timeout, stagger=False, on_line=None, label="", env=None):
|
||||
seen["cmd"] = cmd
|
||||
return 0, "", ""
|
||||
|
||||
monkeypatch.setattr(agents, "_communicate", fake_comm)
|
||||
await agents._run_opencode("blocks-t-z", "p", 5, "minimax", "m", "none")
|
||||
i = seen["cmd"].index("--title")
|
||||
assert seen["cmd"][i + 1] == "blocks-t-z"
|
||||
|
||||
|
||||
async def test_run_agent_logs_opencode_tokens(testdb, monkeypatch):
|
||||
"""OpenCode-Lauf: Token-Zähler der Session landen im Event-Meta."""
|
||||
recorded = []
|
||||
|
||||
async def sink(**kw):
|
||||
recorded.append(kw)
|
||||
|
||||
async def fake_oc(agent_key, prompt, timeout, provider, model, capabilities, on_line=None, label=""):
|
||||
return 0, "out", ""
|
||||
|
||||
monkeypatch.setattr(agents, "on_event", sink)
|
||||
monkeypatch.setattr(agents, "_run_opencode", fake_oc)
|
||||
monkeypatch.setattr(agents.shutil, "which", lambda c: "/bin/true")
|
||||
monkeypatch.setattr(agents, "resolve_role", lambda p, r: ("minimax", "test-model"))
|
||||
monkeypatch.setattr(agents, "_session_tokens",
|
||||
lambda k: {"input": 5, "output": 2, "reasoning": 0,
|
||||
"cache_read": 100, "cache_write": 0})
|
||||
rc, *_ = await agents.run_agent("blocks-t-tok", "p", 5, provider="minimax", scope=TOPIC)
|
||||
assert rc == 0
|
||||
assert recorded and recorded[0]["meta"]["tokens"]["cache_read"] == 100
|
||||
|
||||
Reference in New Issue
Block a user