from fastapi.testclient import TestClient import db from main import app client = TestClient(app) class TestCardManagerBackend: def test_reset_review_endpoint(self): """Test resetting card reviews back to initial FSRS state.""" # Pre-seed a review with advanced state (e.g. state 2) and stats revs = db.get_reviews_by_problem_id("prob-two-sum", "sandbox-user-id") if revs: db.update_review( revs[0]["id"], { "fsrs_state": 2, "fsrs_stability": 14.0, "times_correct": 10, "total_attempts": 10, }, "sandbox-user-id", ) # 1. Reset review for prob-two-sum res = client.post( "/api/reviews/reset", headers={"Authorization": "Bearer sandbox"}, json={"problem_id": "prob-two-sum"}, ) assert res.status_code == 200 data = res.json() assert data["success"] is True assert data["reset_count"] >= 1 # Verify review in DB revs = db.get_reviews_by_problem_id("prob-two-sum", "sandbox-user-id") assert len(revs) > 0 rev = revs[0] assert rev["fsrs_state"] == 0 assert rev["fsrs_stability"] == 0.0 assert rev["times_correct"] == 0 assert rev["total_attempts"] == 0 def test_reset_batch_reviews_endpoint(self): """Test resetting multiple cards in batch.""" res = client.post( "/api/reviews/reset", headers={"Authorization": "Bearer sandbox"}, json={"problem_ids": ["prob-two-sum", "prob-contains-duplicate"]}, ) assert res.status_code == 200 data = res.json() assert data["success"] is True assert data["reset_count"] == 2 def test_reset_reviews_empty_request(self): """Test that resetting with empty payload fails with 400.""" res = client.post( "/api/reviews/reset", headers={"Authorization": "Bearer sandbox"}, json={"problem_ids": []}, ) assert res.status_code == 400 def test_delete_custom_problem_endpoint(self): """Test deleting a problem via explorer DELETE endpoint.""" # 1. Create a custom problem create_res = client.post( "/api/explorer/problems/create", headers={"Authorization": "Bearer sandbox"}, json={ "name": "Unique Test Card For Deletion", "pattern": "Testing", "difficulty": "Easy", "reference_code": "def solve(): return 42", }, ) assert create_res.status_code == 200 prob_id = create_res.json()["problem_id"] # 2. Delete the created problem del_res = client.delete( f"/api/explorer/problems/{prob_id}", headers={"Authorization": "Bearer sandbox"}, ) assert del_res.status_code == 200 assert del_res.json()["success"] is True assert del_res.json()["problem_id"] == prob_id # 3. Verify it is gone assert db.get_problem(prob_id, "sandbox-user-id") is None def test_delete_nonexistent_problem(self): """Test deleting a nonexistent problem returns 404.""" del_res = client.delete( "/api/explorer/problems/nonexistent-card-id-12345", headers={"Authorization": "Bearer sandbox"}, ) assert del_res.status_code == 404