@@ -553,6 +553,60 @@ def test_full_analyze():
553553 assert d ["provider" ] == "rule-based"
554554 assert d ["analysis_time_ms" ] is not None
555555
556+ def test_full_analyze_uses_cache_for_identical_inputs ():
557+ from app .main import _request_counts
558+ from app .services .cache import cache
559+
560+ _request_counts .clear ()
561+ cache .clear_memory ()
562+ payload = {"code" : PYTHON_BUGGY , "language" : "python" }
563+
564+ first = client .post ("/analyze/" , json = payload )
565+ second = client .post ("/analyze/" , json = payload )
566+
567+ assert first .status_code == 200
568+ assert second .status_code == 200
569+ assert first .headers ["X-Cache" ] == "MISS"
570+ assert second .headers ["X-Cache" ] == "HIT"
571+ assert second .json () == first .json ()
572+ _request_counts .clear ()
573+
574+ def test_analyze_cache_expires (monkeypatch ):
575+ from app .services import cache as cache_module
576+ from app .services .cache import cache
577+
578+ cache .clear_memory ()
579+ payload = {"code" : PYTHON_BUGGY , "language" : "python" }
580+ start = 1_700_000_000
581+
582+ monkeypatch .setattr (cache_module .time , "time" , lambda : start )
583+ first = client .post ("/analyze/" , json = payload )
584+
585+ monkeypatch .setattr (cache_module .time , "time" , lambda : start + 301 )
586+ second = client .post ("/analyze/" , json = payload )
587+
588+ assert first .status_code == 200
589+ assert second .status_code == 200
590+ assert first .headers ["X-Cache" ] == "MISS"
591+ assert second .headers ["X-Cache" ] == "MISS"
592+ cache .clear_memory ()
593+
594+ def test_memory_cache_evicts_least_recently_used_entries ():
595+ from app .services .cache import cache
596+
597+ cache .clear_memory ()
598+
599+ for index in range (100 ):
600+ cache .set ("test" , f"item-{ index } " , {"index" : index })
601+
602+ assert cache .get ("test" , "item-0" ) == {"index" : 0 }
603+ cache .set ("test" , "item-100" , {"index" : 100 })
604+
605+ assert cache .get ("test" , "item-1" ) is None
606+ assert cache .get ("test" , "item-0" ) == {"index" : 0 }
607+ assert cache .get ("test" , "item-100" ) == {"index" : 100 }
608+ cache .clear_memory ()
609+
556610def test_full_analyze_all_languages ():
557611 for code , lang in [
558612 (JS_CODE , "javascript" ),
0 commit comments