"""Protect the two-product boundary and consistency of the shipped recall policy.""" from copy import deepcopy import json from pathlib import Path import pytest from backend.calibration import active_evaluation, calibrated_card, MACARONI_FILENAME from scripts.audit_recall import metrics, quantile ROOT=Path(__file__).resolve().parents[1] @pytest.fixture def manifests(tmp_path,monkeypatch): monkeypatch.delenv('DEFECTRAG_CALIBRATION',raising=False) (tmp_path/'results').mkdir() names=['highres_deployment_evaluation.json','sensitive_deployment_evaluation.json',MACARONI_FILENAME] values=[] for name in names: value=json.loads((ROOT/'results'/name).read_text());values.append(value) (tmp_path/'results'/name).write_text(json.dumps(value)) return tmp_path,*values def test_default_changes_only_macaroni_cards_and_preserves_previous_manifest(manifests): root,baseline,previous,candidate=manifests before=deepcopy(previous) result=active_evaluation(root,baseline) assert result==candidate for category,old in previous['categories'].items(): card=calibrated_card(root,baseline['categories'][category]) assert card==result['categories'][category] if category in ('macaroni1','macaroni2'): assert card['threshold']=(.85 if category=='macaroni1' else .8) def test_public_evaluation_and_model_cards_agree_on_the_targeted_policy(manifests): from backend.config import Settings from backend.main import create_app from fastapi.testclient import TestClient root,baseline,_,candidate=manifests class Metadata: profile_name='dinov2_highres_v1' def cards(self):return [calibrated_card(root,c) for c in baseline['categories'].values()] with TestClient(create_app(Settings(root=root,data=root/'runtime'),detector=Metadata(),workflow=object())) as client: assert client.get('/api/evaluation').json()['data']==candidate cards={c['category']:c for c in client.get('/api/models').json()['data']} assert cards==candidate['categories']