feat(админка): очередь внимания
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
"""P2.4: устойчивые ключи, done/snooze, идемпотентность, не врём про done."""
|
||||
import importlib
|
||||
import os
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
if ROOT not in sys.path:
|
||||
sys.path.insert(0, ROOT)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def env(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("DB_PATH", str(tmp_path / "t.db"))
|
||||
monkeypatch.setenv("SESSION_SECRET", "test-secret")
|
||||
monkeypatch.setenv("AI_BACKEND", "claude")
|
||||
monkeypatch.chdir(ROOT)
|
||||
import auth
|
||||
import db
|
||||
import main
|
||||
import admin_api
|
||||
import attention
|
||||
for m in (db, auth, main, admin_api, attention):
|
||||
importlib.reload(m)
|
||||
db.init_db()
|
||||
db.create_user("boss@x", auth.hash_password("Passw0rd!ok"), role="admin")
|
||||
db.admin_verify_user("boss@x")
|
||||
from fastapi.testclient import TestClient
|
||||
client = TestClient(main.app)
|
||||
h = {"Authorization": "Bearer " + auth._make_token("boss@x", "admin")}
|
||||
return db, attention, client, h, monkeypatch
|
||||
|
||||
|
||||
def test_collect_stable_keys_and_state(env):
|
||||
db, attention, client, h, monkeypatch = env
|
||||
work = {"failures": 2, "limit_hits": 1}
|
||||
cash = {"unpriced": 3, "accounts_total_rub": 0}
|
||||
snap = attention.collect(work, cash)
|
||||
keys = {i["key"] for i in snap["items"]}
|
||||
assert {"failures:24h", "limits:24h", "unpriced"} <= keys
|
||||
|
||||
db.set_attention_state("failures:24h", "done")
|
||||
db.set_attention_state("limits:24h", "snoozed", 9_999_999_999)
|
||||
snap2 = attention.collect(work, cash)
|
||||
assert "failures:24h" not in {i["key"] for i in snap2["items"]}
|
||||
assert "limits:24h" not in {i["key"] for i in snap2["items"]}
|
||||
assert "unpriced" in {i["key"] for i in snap2["items"]}
|
||||
# Сигнал исчез — done снимается, чтобы завтрашний возврат снова был виден.
|
||||
snap3 = attention.collect({}, {"unpriced": 0, "accounts_total_rub": 0})
|
||||
assert "failures:24h" not in {i["key"] for i in snap3["items"]}
|
||||
assert not attention.state_of("failures:24h")
|
||||
|
||||
|
||||
def test_endpoint_done_snooze_and_unknown(env):
|
||||
db, attention, client, h, monkeypatch = env
|
||||
import admin_api
|
||||
monkeypatch.setattr(admin_api, "_attention_snapshot",
|
||||
lambda: {"items": [{"key": "failures:24h", "title": "x"},
|
||||
{"key": "unpriced", "title": "y"}], "count": 2})
|
||||
assert client.get("/api/admin/v2/attention", headers=h).status_code == 200
|
||||
assert client.post("/api/admin/v2/attention/failures:24h/done", headers=h).status_code == 200
|
||||
# повтор done — идемпотентен, хотя сигнала уже нет
|
||||
assert client.post("/api/admin/v2/attention/failures:24h/done", headers=h).status_code == 200
|
||||
assert client.post("/api/admin/v2/attention/nope:1/done", headers=h).status_code == 404
|
||||
assert client.post("/api/admin/v2/attention/failures:24h/snooze",
|
||||
json={"minutes": 0}, headers=h).status_code == 400
|
||||
assert client.post("/api/admin/v2/attention/failures:24h/snooze",
|
||||
headers=h).status_code == 400
|
||||
assert client.post("/api/admin/v2/attention/failures:24h/snooze",
|
||||
json={"minutes": "abc"}, headers=h).status_code == 400
|
||||
assert client.post("/api/admin/v2/attention/failures:24h/snooze",
|
||||
json={"minutes": 10}, headers=h).status_code == 200
|
||||
assert client.post("/api/admin/v2/attention/unpriced/done", headers=h).status_code == 200
|
||||
assert client.post("/api/admin/v2/attention/unpriced/snooze",
|
||||
json={"minutes": 5}, headers=h).status_code == 200
|
||||
|
||||
|
||||
def test_real_snapshot_without_monkeypatch(env):
|
||||
db, attention, client, h, monkeypatch = env
|
||||
db.add_account_entry("joe@x", 1000.0, "взнос", "boss@x", ts=0)
|
||||
with db._conn() as c:
|
||||
c.execute("INSERT INTO llm_usage (ts, username, tab_id, path, provider, model, "
|
||||
"input_tokens, cache_tokens, output_tokens, cost_usd, cost_rub, price_rub) "
|
||||
"VALUES (1,'joe@x','t','agent','deepseek','m',1,0,1,0,0,NULL)")
|
||||
r = client.get("/api/admin/v2/attention", headers=h)
|
||||
assert r.status_code == 200
|
||||
keys = {i["key"] for i in r.json()["items"]}
|
||||
assert "unpriced" in keys
|
||||
Reference in New Issue
Block a user