"""Tests for the `existing_test_count` field on TestTemplateSummary. Block 3 fix: the Test Catalog page had no way to know a technique already had one or more tests, so operators could unknowingly instantiate a duplicate from a template. list_templates now attaches a per-technique test count so the frontend can show a warning badge. """ from app.models.enums import TestState from app.models.technique import Technique from app.models.test import Test from app.models.test_template import TestTemplate from app.services.test_template_service import list_templates def test_existing_test_count_reflects_tests_for_that_technique(db, red_lead_user): tech = Technique(mitre_id="T1055", name="Process Injection", tactic="defense-evasion", platforms=["windows"]) db.add(tech) db.flush() db.add(TestTemplate(mitre_technique_id="T1055", name="Injection template", source="custom")) db.add(Test(technique_id=tech.id, name="Existing test 1", state=TestState.draft, created_by=red_lead_user.id)) db.add(Test(technique_id=tech.id, name="Existing test 2", state=TestState.validated, created_by=red_lead_user.id)) db.commit() templates = list_templates(db, mitre_technique_id="T1055") assert len(templates) == 1 assert templates[0].existing_test_count == 2 def test_existing_test_count_zero_when_no_tests(db): tech = Technique(mitre_id="T1056", name="Input Capture", tactic="collection", platforms=["windows"]) db.add(tech) db.flush() db.add(TestTemplate(mitre_technique_id="T1056", name="Capture template", source="custom")) db.commit() templates = list_templates(db, mitre_technique_id="T1056") assert len(templates) == 1 assert templates[0].existing_test_count == 0