File size: 1,775 Bytes
633f159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys

from fastapi.testclient import TestClient

PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if PROJECT_ROOT not in sys.path:
    sys.path.insert(0, PROJECT_ROOT)

import main  # noqa: E402


def _make_test_client():
    # use test db
    main.db_path = os.path.join(PROJECT_ROOT, "test_focus_guard.db")
    if os.path.exists(main.db_path):
        os.remove(main.db_path)
    return TestClient(main.app)


def test_get_settings_default_fields():
    client = _make_test_client()
    with client:
        resp = client.get("/api/settings")
        assert resp.status_code == 200
        data = resp.json()
        assert "sensitivity" in data
        assert "notification_enabled" in data
        assert "notification_threshold" in data
        assert "frame_rate" in data
        assert "model_name" in data


def test_update_settings_clamped_ranges():
    client = _make_test_client()
    with client:
        # get setting
        r0 = client.get("/api/settings")
        assert r0.status_code == 200

        # set unlogic params
        payload = {
            "sensitivity": 100,              
            "notification_enabled": False,
            "notification_threshold": 1,     
            "frame_rate": 1000,             
        }
        r_put = client.put("/api/settings", json=payload)
        assert r_put.status_code == 200
        body = r_put.json()
        assert body["status"] == "success"
        assert body["updated"] is True

        r1 = client.get("/api/settings")
        data = r1.json()
        assert 1 <= data["sensitivity"] <= 10
        assert bool(data["notification_enabled"]) is False
        assert 5 <= data["notification_threshold"] <= 300
        assert 5 <= data["frame_rate"] <= 60