kink-discovery / tests /test_starter_deck_saves_api.py
Perplexed7675's picture
Sync from kink_cli (Docker Space)
3bd91cd verified
Raw
History Blame Contribute Delete
2.49 kB
"""Sequential play saves on a fresh user (guards starter deck API + play keys)."""
from __future__ import annotations
import shutil
import sys
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
_SEED = Path(__file__).resolve().parent.parent / "deploy/hf/seed/hf_bundled_store.db"
@pytest.fixture()
def api_client(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
assert _SEED.is_file(), f"Missing seed DB: {_SEED}"
dest = tmp_path / "starter_saves_store.db"
shutil.copyfile(_SEED, dest)
monkeypatch.setenv("KINK_STORE_PATH", str(dest))
monkeypatch.setenv("KINK_SKIP_HEAVY_WARM", "1")
monkeypatch.setenv("KINK_HF_REQUIRE_FULL_CATALOG", "0")
sys.modules.pop("api", None)
import api as api_mod
api_mod._backend_impl = None
api_mod._get_backend()
return api_mod
def test_two_distinct_likes_increment_play_count(api_client) -> None:
client = TestClient(api_client.app)
u = client.post("/users", json={}).json()
uid, tok = u["id"], u["private_token"]
h = {"x-private-token": tok}
rec = client.get(f"/users/{uid}/recommendations?limit=8", headers=h).json()
items = rec.get("items") or []
assert len(items) >= 2, items
a = items[0]["kink"]["id"]
b = items[1]["kink"]["id"]
assert a != b
body = {"kink_id": a, "interest_state": "like", "directions": ["together"]}
r1 = client.post(f"/users/{uid}/plays", json=body, headers=h)
assert r1.status_code == 200, r1.text
r2 = client.post(
f"/users/{uid}/plays",
json={"kink_id": b, "interest_state": "like", "directions": ["together"]},
headers=h,
)
assert r2.status_code == 200, r2.text
plays = r2.json().get("plays") or {}
assert len(plays) == 2, plays
def test_recommendations_after_first_save_exclude_that_kink(api_client) -> None:
client = TestClient(api_client.app)
u = client.post("/users", json={}).json()
uid, tok = u["id"], u["private_token"]
h = {"x-private-token": tok}
rec0 = client.get(f"/users/{uid}/recommendations?limit=8", headers=h).json()
top = (rec0.get("items") or [])[0]["kink"]["id"]
client.post(
f"/users/{uid}/plays",
json={"kink_id": top, "interest_state": "like", "directions": ["together"]},
headers=h,
)
rec1 = client.get(f"/users/{uid}/recommendations?limit=8", headers=h).json()
ids = [row["kink"]["id"] for row in (rec1.get("items") or [])]
assert top not in ids, (top, ids[:5])