File size: 1,837 Bytes
c794b6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Phase 2 — Priority 2: session reset vs login behavior."""
import os
import sys

os.environ.setdefault("CEPHEUS_CLOUD", "1")
os.environ.setdefault("CEPHEUS_API_KEY", "test-key")

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

import pytest
from fastapi.testclient import TestClient

import main  # noqa: E402

API_HEADERS = {"X-API-Key": "test-key"}


@pytest.fixture
def client():
    return TestClient(main.app)


def test_new_session_clears_tracking_not_login(client, monkeypatch):
    """Explicit session reset clears data; routine API calls do not."""
    monkeypatch.setattr(main, "detections_history", [{"name": "MK", "camId": "cam-01"}])
    main.vision_engine.face_results["cam-01"] = [{"name": "MK"}]
    if hasattr(main.vision_engine, "browser_feeds"):
        main.vision_engine.browser_feeds["cam-01"] = {"status": "ACTIVE"}

    r = client.post(
        "/tracking/session/reset",
        headers=API_HEADERS,
        json={"broadcast": False, "full_reset": True, "clear_agent_steps": True},
    )
    assert r.status_code == 200
    assert main.detections_history == []
    assert main.vision_engine.face_results.get("cam-01", []) == []


def test_global_reset_broadcast_requires_admin(client):
    r = client.post(
        "/tracking/session/reset",
        headers=API_HEADERS,
        json={"broadcast": True},
    )
    assert r.status_code == 403


def test_operator_can_reset_own_session_without_broadcast(client, monkeypatch):
    monkeypatch.setattr(main, "detections_history", [{"name": "X"}])
    r = client.post(
        "/tracking/session/reset",
        headers=API_HEADERS,
        json={"broadcast": False, "full_reset": True},
    )
    assert r.status_code == 200
    assert main.detections_history == []