File size: 5,977 Bytes
151f388
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
Diagnostic Test Suite for Detector Registry
Validates:
- Lazy registration without model instantiation
- Registration and unregistration APIs
- Isolated retrieval (selecting one detector does NOT instantiate the other)
- Inspection without weight loading
- Dynamic multi-detector discovery
"""

import os
import sys
import unittest

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

from src.detector_schema import DetectorResult, PREDICTION_REAL


class DummyTestDetector:
    """Mock detector to test registration without heavy ML dependencies."""
    DETECTOR_NAME = "DummyTestDetector"
    MODEL_NAME = "Mock/Model-v1"
    DECISION_THRESHOLD = 0.50

    def __init__(self, lazy_load: bool = True):
        self.lazy_load = lazy_load
        self.weights_loaded = False

    def load_model(self):
        self.weights_loaded = True
        return True

    def predict(self, image_input):
        return DetectorResult(
            detector_name=self.DETECTOR_NAME,
            model_name=self.MODEL_NAME,
            prediction=PREDICTION_REAL,
            fake_probability=0.1,
            real_probability=0.9,
            raw_logit=-2.0,
            confidence=0.9,
            threshold=self.DECISION_THRESHOLD,
        )


def test_registry():
    print("=" * 75)
    print("DETECTOR REGISTRY VERIFICATION SUITE")
    print("=" * 75 + "\n")

    # 1. Test Import isolation
    print("1. Testing registry import isolation...")
    import src.detector_registry as registry
    assert len(registry._INSTANCES) == 0, "No detectors should be instantiated on import!"
    print("   [PASS] Registry imported cleanly with 0 active instances.")

    # 2. Test List detectors
    print("\n2. Testing list_detectors()...")
    detectors = registry.list_detectors()
    print(f"   Registered detectors: {detectors}")
    assert "univfd" in detectors, "univfd should be registered by default"
    assert "vit" in detectors, "vit should be registered by default"
    assert "chen2024_convnext" in detectors, "chen2024_convnext should be registered by default"
    assert len(detectors) == 3, f"Expected 3 default detectors, found {len(detectors)}"
    print("   [PASS] All 3 default detectors correctly registered.")

    # 3. Test Metadata retrieval without instantiation
    print("\n3. Testing get_detector_info() without instantiation...")
    u_info = registry.get_detector_info("univfd")
    v_info = registry.get_detector_info("vit")
    c_info = registry.get_detector_info("chen2024_convnext")
    assert u_info["name"] == "univfd"
    assert v_info["name"] == "vit"
    assert c_info["name"] == "chen2024_convnext"
    assert not u_info["instantiated"]
    assert not v_info["instantiated"]
    assert not c_info["instantiated"]
    assert len(registry._INSTANCES) == 0, "Info check must not instantiate models!"
    print("   [PASS] Metadata retrieved with 0 model instantiations.")

    # 4. Test Single Detector Retrieval (Isolation)
    print("\n4. Testing isolated detector instantiation...")
    univfd_adapter = registry.get_detector("univfd", lazy_load=True)
    assert univfd_adapter is not None
    assert "univfd" in registry._INSTANCES
    assert "vit" not in registry._INSTANCES, "Getting univfd must NOT instantiate vit!"
    assert "chen2024_convnext" not in registry._INSTANCES, "Getting univfd must NOT instantiate convnext!"
    assert univfd_adapter.model is None, "Lazy loading must not load model weights into RAM!"
    print("   [PASS] UnivFD instantiated in lazy mode without touching ViT or ConvNeXt.")

    # 5. Test ViT Detector Retrieval
    print("\n5. Testing ViT detector instantiation...")
    vit_adapter = registry.get_detector("vit", lazy_load=True)
    assert vit_adapter is not None
    assert "vit" in registry._INSTANCES
    assert "chen2024_convnext" not in registry._INSTANCES
    assert vit_adapter.model is None, "Lazy loading must not load model weights into RAM!"
    print("   [PASS] ViT instantiated in lazy mode without touching ConvNeXt.")

    # 5b. Test ConvNeXt Detector Retrieval
    print("\n5b. Testing ConvNeXt detector instantiation...")
    convnext_adapter = registry.get_detector("chen2024_convnext", lazy_load=True)
    assert convnext_adapter is not None
    assert "chen2024_convnext" in registry._INSTANCES
    assert convnext_adapter.model is None, "Lazy loading must not load model weights into RAM!"
    print("   [PASS] ConvNeXt instantiated in lazy mode.")

    # 6. Test Dynamic Registration of New / Third Detector
    print("\n6. Testing dynamic registration of mock detector...")
    registry.register_detector(
        name="dummy",
        adapter_cls_or_factory=DummyTestDetector,
        metadata={"description": "Mock detector for testing third-slot capability"},
    )
    assert "dummy" in registry.list_detectors()
    dummy_info = registry.get_detector_info("dummy")
    assert dummy_info["metadata"]["description"] == "Mock detector for testing third-slot capability"

    dummy_adapter = registry.get_detector("dummy")
    assert isinstance(dummy_adapter, DummyTestDetector)
    dummy_res = dummy_adapter.predict("test")
    assert dummy_res.prediction == PREDICTION_REAL
    print("   [PASS] Dynamic registration and execution of candidate detector verified.")

    # 7. Test Unregistration
    print("\n7. Testing unregistration...")
    assert registry.unregister_detector("dummy") is True
    assert "dummy" not in registry.list_detectors()
    assert registry.unregister_detector("dummy") is False
    print("   [PASS] Unregistration successfully cleaned up mock detector.")

    # 8. Reset instances
    registry.reset_instances()
    assert len(registry._INSTANCES) == 0
    print("\n   [PASS] Reset instances cleanly wiped active cache.")

    print("\n" + "=" * 75)
    print("ALL DETECTOR REGISTRY CONTRACT TESTS PASSED!")
    print("=" * 75)


if __name__ == "__main__":
    test_registry()