File size: 3,566 Bytes
f0175ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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