from __future__ import annotations import json from pathlib import Path import pandas as pd import pytest from src.baselines import gold_prediction, run_rule_baseline, run_schema_template from src.domain import ( DEFAULT_SEED, DEV_FAMILIES, INTERACTIVE_SUFFIXES, N_DEVELOPMENT, N_INTERACTIVE, N_ITEMS, N_TEST, SCORE_WEIGHTS, TEST_FAMILIES, Family, Task, ) from src.evaluators import score_prediction from src.generator import INTERACTIVE_IDS, generate_catalog, load_catalog from src.metrics import summarize from src.model_registry import UNEVALUATED_MODELS, evaluated_ids from src.pipeline import evaluate_item from src.redaction import contains_secret from src.validation import parse_answer, validate_text ROOT = Path(__file__).resolve().parents[1] @pytest.fixture(scope="session") def catalog(): path = ROOT / "examples" / "items.jsonl" return load_catalog(path if path.exists() else None) def test_catalog_size_and_ids(catalog): assert len(catalog) == N_ITEMS ids = [item.item_id for item in catalog] assert len(ids) == len(set(ids)) assert sum(item.interactive for item in catalog) == N_INTERACTIVE assert set(INTERACTIVE_IDS).issubset(set(ids)) for task in Task: assert sum(item.task == task for item in catalog) == 40 def test_split_is_by_family(catalog): assert sum(item.split.value == "development" for item in catalog) == N_DEVELOPMENT assert sum(item.split.value == "test" for item in catalog) == N_TEST for task in Task: development = {item.family for item in catalog if item.task == task and item.split.value == "development"} test = {item.family for item in catalog if item.task == task and item.split.value == "test"} assert development == DEV_FAMILIES[task] assert test == TEST_FAMILIES[task] assert development.isdisjoint(test) templates = {(item.task.value, item.template_id) for item in catalog if item.split.value == "development"} test_templates = {(item.task.value, item.template_id) for item in catalog if item.split.value == "test"} assert templates.isdisjoint(test_templates) def test_gold_is_hidden_from_prompts(catalog): for item in catalog: blob = json.dumps(item.visible_input, ensure_ascii=False) assert "should_abstain" not in blob assert "must_cite" not in blob gold_json = json.dumps(item.gold, sort_keys=True) assert gold_json not in item.prompt_en assert gold_json not in item.prompt_fa def test_no_duplicate_payloads(catalog): keys = [item.structural_key for item in catalog] assert len(keys) == len(set(keys)) def test_gold_echo_is_near_perfect(catalog): sample = [item for item in catalog if item.item_id.endswith("-001") or item.family in TEST_FAMILIES[item.task]] for item in sample[:16]: score = score_prediction(item, gold_prediction(item)) assert score.json_validity == 1 assert score.quality is not None assert score.quality >= 0.99 assert not score.fabricated def test_template_beats_rule_on_travel_vpn(catalog): item = next(item for item in catalog if item.family == Family.TRAVEL_VPN) rule = score_prediction(item, run_rule_baseline(item)) template = score_prediction(item, run_schema_template(item)) assert template.abstention == 1 assert rule.abstention == 0 assert (template.quality or 0) > (rule.quality or 0) def test_fabricated_event_id_fails_groundedness(catalog): item = next(item for item in catalog if item.task == Task.TRIAGE and not item.gold["should_abstain"]) pred = gold_prediction(item) parsed = dict(pred.parsed) parsed["cited_event_ids"] = list(parsed["cited_event_ids"]) + ["E-FAKE"] pred.parsed = parsed pred.raw_text = json.dumps(parsed) score = score_prediction(item, pred) assert score.groundedness == 0 assert "E-FAKE" in score.fabricated def test_invented_ioc_fails_groundedness(catalog): item = next(item for item in catalog if item.task == Task.CTI and item.family == Family.LISTED_IOCS) pred = gold_prediction(item) parsed = dict(pred.parsed) parsed["ips"] = list(parsed["ips"]) + ["9.9.9.9"] pred.parsed = parsed score = score_prediction(item, pred) assert score.groundedness == 0 assert "9.9.9.9" in score.fabricated def test_sigma_unbounded_fails(catalog): item = next(item for item in catalog if item.family == Family.OVERBROAD_HYPOTHESIS) rule = score_prediction(item, run_rule_baseline(item)) template = score_prediction(item, run_schema_template(item)) assert "unbounded_query" in rule.violated or rule.task_score == 0 assert template.abstention == 1 assert template.quality == 1 def test_rag_forbidden_citation_fails(catalog): item = next(item for item in catalog if item.family == Family.CONFLICTING_VERSIONS) rule = score_prediction(item, run_rule_baseline(item)) template = score_prediction(item, run_schema_template(item)) assert "forbidden_citation" in rule.violated or rule.groundedness == 0 assert template.groundedness == 1 assert template.task_score == 1 def test_empty_and_secret_rejected(): empty = validate_text("") assert not empty.ok secret = validate_text("token sk-lab-not-real") assert not secret.ok assert contains_secret("BEGIN PRIVATE KEY") def test_unevaluated_models_are_not_zero(catalog): result = evaluate_item(catalog[0], lang="en") skipped = [row for row in result["rows"] if row.method_id in {item.method_id for item in UNEVALUATED_MODELS}] assert skipped assert all(row.evaluated is False for row in skipped) assert all(row.status == "not_evaluated" for row in skipped) assert all(row.quality is None for row in skipped) summary = summarize(result["rows"]) qwen = summary.loc[summary["method_id"] == "Qwen/Qwen3-4B-Instruct-2507"].iloc[0] assert qwen["evaluated"] == False # noqa: E712 assert qwen["quality"] == "not_evaluated" def test_evaluate_item_has_local_methods(catalog): item = next(item for item in catalog if item.interactive) result = evaluate_item(item, lang="en") ids = {row.method_id for row in result["rows"]} assert set(evaluated_ids()).issubset(ids) assert "Qwen/Qwen3.8-27B" in ids assert abs(sum(SCORE_WEIGHTS.values()) - 1.0) < 1e-9 def test_user_json_is_scored(catalog): item = next(item for item in catalog if item.task == Task.TRIAGE and item.gold["label"] == "investigate") payload = gold_prediction(item).raw_text result = evaluate_item(item, user_text=payload) user = next(row for row in result["rows"] if row.method_id == "user_json") assert user.evaluated is True assert user.quality >= 0.99 def test_parse_sigma_answer(): parsed, ok = parse_answer("title: x\nlogsource:\n product: windows\ndetection:\n selection:\n EventID: 1\n condition: selection\n") assert ok assert "sigma" in parsed def test_interactive_suffixes_stable(): assert INTERACTIVE_SUFFIXES == (1, 8, 13, 25, 33) assert generate_catalog(DEFAULT_SEED)[0].seed == DEFAULT_SEED