Spaces:
Running
Running
Download tests/unit/test_challenger2_m2_backend.py from iamfebin/algospaced-dsa: direct link, hf CLI and curl.
- Browser
- Download file 4.9 kB
-
https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/tests/unit/test_challenger2_m2_backend.py
- Command line
-
hf download hf://spaces/iamfebin/algospaced-dsa/tests/unit/test_challenger2_m2_backend.py
-
curl -L -o test_challenger2_m2_backend.py https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/tests/unit/test_challenger2_m2_backend.py
4.9 kB
| """Empirical Challenge Test Suite: Backend Earning Hooks & Monotonic Lifetime Currency. | |
| Challenger 2 — Milestone 2: Frontend Economy Engine, Contracts, & Earning Hooks. | |
| Validates: | |
| 1. Contract symmetry: Rating payouts match frontend (1 -> +5g, 2 -> +15g, 3 -> +25g, 4 -> +35g) | |
| 2. Monotonicity: SQLite `lifetime_earned` strictly increases with earning and never drops on purchases | |
| 3. Concurrency / stress: Sequential and rapid review submissions accurately credit balance and lifetime | |
| 4. Audit integrity: Store transactions record correct balance_after without corrupting lifetime_earned | |
| """ | |
| import sqlite3 | |
| from datetime import UTC, datetime | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| import sandbox_db | |
| from main import app | |
| from routers.reviews import REVIEW_RATING_GOLD_MAP | |
| AUTH_HEADERS = {"Authorization": "Bearer sandbox"} | |
| def client(): | |
| return TestClient(app) | |
| def clean_sandbox_user(): | |
| """Reset sandbox user currency and review state before each test.""" | |
| sandbox_db.init_db() | |
| conn = sqlite3.connect(sandbox_db.DB_PATH) | |
| try: | |
| with conn: | |
| conn.execute("DELETE FROM user_currency WHERE user_id = 'sandbox-user-id'") | |
| conn.execute("DELETE FROM store_transactions WHERE user_id = 'sandbox-user-id'") | |
| conn.execute("DELETE FROM user_inventory WHERE user_id = 'sandbox-user-id'") | |
| conn.execute( | |
| "INSERT INTO user_currency (user_id, gold, lifetime_earned, updated_at) VALUES ('sandbox-user-id', 0, 0, ?)", | |
| (datetime.now(UTC).isoformat(),), | |
| ) | |
| finally: | |
| conn.close() | |
| yield | |
| def test_contract_symmetry_rating_scale(): | |
| """Verify backend rating scale strictly matches frontend scale (1:5, 2:15, 3:25, 4:35).""" | |
| assert REVIEW_RATING_GOLD_MAP[1] == 5 | |
| assert REVIEW_RATING_GOLD_MAP[2] == 15 | |
| assert REVIEW_RATING_GOLD_MAP[3] == 25 | |
| assert REVIEW_RATING_GOLD_MAP[4] == 35 | |
| def test_sqlite_lifetime_earned_monotonicity(): | |
| """Verify SQLite UPSERT strictly increases lifetime_earned and never drops on purchase.""" | |
| # Step 1: Initial state | |
| curr = sandbox_db.get_user_currency("sandbox-user-id") | |
| assert curr["gold"] == 0 | |
| assert curr["lifetime_earned"] == 0 | |
| # Step 2: Earn 50g | |
| c1 = sandbox_db.increment_user_gold(50, "sandbox-user-id") | |
| assert c1["gold"] == 50 | |
| assert c1["lifetime_earned"] == 50 | |
| # Step 3: Earn 35g | |
| c2 = sandbox_db.increment_user_gold(35, "sandbox-user-id") | |
| assert c2["gold"] == 85 | |
| assert c2["lifetime_earned"] == 85 | |
| # Step 4: Purchase an item (costs 50g) | |
| catalog_item = { | |
| "id": "consumable_campfire_stew", | |
| "name": "Hearty Wayfarer's Stew", | |
| "category": "consumable", | |
| "price": 50, | |
| "lore": "Herbal stew", | |
| "icon": "Flame", | |
| "effect_type": "focus_boost", | |
| "effect_payload": {}, | |
| } | |
| tx_res = sandbox_db.purchase_store_item(catalog_item, quantity=1, user_id="sandbox-user-id") | |
| assert tx_res["success"] is True | |
| assert tx_res["new_balance"] == 35 | |
| # Step 5: Check currency table: gold drops to 35, lifetime_earned MUST remain 85 | |
| c3 = sandbox_db.get_user_currency("sandbox-user-id") | |
| assert c3["gold"] == 35 | |
| assert c3["lifetime_earned"] == 85 # Strictly monotonic! | |
| def test_rapid_consecutive_gold_increments(): | |
| """Stress test 50 rapid sequential gold awards via SQLite layer.""" | |
| iterations = 50 | |
| total_expected = 0 | |
| for i in range(iterations): | |
| award = REVIEW_RATING_GOLD_MAP[(i % 4) + 1] | |
| total_expected += award | |
| sandbox_db.increment_user_gold(award, "sandbox-user-id") | |
| final_curr = sandbox_db.get_user_currency("sandbox-user-id") | |
| assert final_curr["gold"] == total_expected | |
| assert final_curr["lifetime_earned"] == total_expected | |
| def test_api_review_complete_earning_flow(client): | |
| """End-to-end API test: complete review increments gold and returns new balance.""" | |
| conn = sqlite3.connect(sandbox_db.DB_PATH) | |
| try: | |
| cur = conn.cursor() | |
| cur.execute("SELECT id FROM user_reviews WHERE user_id = 'sandbox-user-id' LIMIT 1") | |
| row = cur.fetchone() | |
| assert row is not None, "Seeded user reviews must exist" | |
| rev_id = row[0] | |
| finally: | |
| conn.close() | |
| res = client.post( | |
| "/api/reviews/complete", | |
| headers=AUTH_HEADERS, | |
| json={ | |
| "review_id": rev_id, | |
| "fsrs_state": 1, | |
| "fsrs_stability": 2.0, | |
| "fsrs_difficulty": 4.0, | |
| "rating": 4, # Easy -> +35g | |
| }, | |
| ) | |
| assert res.status_code == 200 | |
| data = res.json() | |
| assert data["success"] is True | |
| assert data["gold_awarded"] == 35 | |
| assert data["new_balance"] == 35 | |
| curr = sandbox_db.get_user_currency("sandbox-user-id") | |
| assert curr["gold"] == 35 | |
| assert curr["lifetime_earned"] == 35 | |