feat(админка): копейки и money API

This commit is contained in:
anor
2026-11-06 22:29:09 +10:00
parent 99b62fb5fb
commit 7ad2368719
6 changed files with 151 additions and 17 deletions
+29
View File
@@ -0,0 +1,29 @@
"""P3.1a: копейки и ROUND_HALF_UP — golden из плана."""
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
import balance # noqa: E402
def test_golden_1_ruble_minus_71_kopecks():
st = balance.compose(100, 0.71, 0.0, 0)
assert st["balance_kop"] == 29
assert st["balance_rub"] == 0.29
def test_golden_half_kopeck_rounds_up_not_even():
st = balance.compose(100, 0.125 + 0.25, 0.0, 0)
assert st["balance_kop"] == 63
assert st["balance_rub"] == 0.63
def test_zero_entries_still_none():
assert balance.compose(0, 0.0, 0.0, 0) is None
def test_dust_charge_does_not_create_account_and_low_is_not_truncated():
assert balance.compose(0, 0.0008, 0.0, 0) is None
st = balance.compose(29, 0.27, 0.0, 0)
assert st["balance_kop"] == 2 and st["low"] is True
+65
View File
@@ -0,0 +1,65 @@
"""P3.1b: один период — один итог в копейках; /api/admin/v2/money."""
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
for m in (db, auth, main):
importlib.reload(m)
db.init_db()
db.create_user("boss@x", auth.hash_password("Passw0rd!ok"), role="admin")
db.admin_verify_user("boss@x")
db.create_user("joe@x", auth.hash_password("Passw0rd!ok"))
db.admin_verify_user("joe@x")
db.add_account_entry("joe@x", 10.0, "взнос", "boss@x")
import time as _time
now = _time.time()
with db._conn() as c:
c.execute("INSERT INTO payments (ts, username, product, amount_rub, yk_payment_id, "
"status, updated_at) VALUES (?,?,?,?,?,?,?)",
(now, "joe@x", "p", "10.00", "m1", "succeeded", now))
db.record_llm_usage("joe@x", "t", "agent", "deepseek", "m", 1, 0, 1, 0,
cost_rub=2.5, price_rub=0, ts=now)
from fastapi.testclient import TestClient
client = TestClient(main.app)
h = {"Authorization": "Bearer " + auth._make_token("boss@x", "admin")}
return db, client, h
def test_money_kopecks_and_period(env):
db, client, h = env
r = client.get("/api/admin/v2/money?days=30", headers=h)
assert r.status_code == 200
d = r.json()
assert d["period_days"] == 30
assert d["accounts_total_kop"] == 1000
assert d["revenue_kop"] == 1000
assert d["cost_kop"] == 250
assert d["flow_kop"] == 750
assert isinstance(d["revenue_kop"], int)
assert isinstance(d["flow_kop"], int) or d["flow_kop"] is None
assert d["flow_kop"] == d["revenue_kop"] - d["cost_kop"]
assert client.get("/api/admin/v2/money?days=0", headers=h).json()["period_days"] == 1
assert client.get("/api/admin/v2/money?days=9999", headers=h).json()["period_days"] == 366
def test_money_is_admin_only(env):
db, client, h = env
import auth
user = {"Authorization": "Bearer " + auth._make_token("joe@x", "user")}
assert client.get("/api/admin/v2/money", headers=user).status_code == 403