Files
OBLITERATUS/tests/test_concept_input_boundaries.py

91 lines
3.9 KiB
Python

"""Reject ambiguous concept inputs before extraction or model execution."""
from pathlib import Path
from unittest.mock import Mock
import pytest
import yaml
from obliteratus.concepts import ConceptTarget, ExtractionRecipe
from obliteratus.concepts.entry_points import EntryPoint
from obliteratus.concepts.evaluation_model import ModelEvaluator
from obliteratus.concepts.extraction import validate_recipe
from obliteratus.concepts.validation import PackError, load_manifest, validate_manifest
pytestmark = pytest.mark.cpu
@pytest.mark.parametrize("entry_point", ["final_token", [], 1, False])
def test_recipe_rejects_non_object_entry_point(entry_point):
with pytest.raises(ValueError, match="entry_point must be an EntryPoint or object"):
ExtractionRecipe(entry_point=entry_point)
@pytest.mark.parametrize("records", [
{"text": "first"},
[{"text": "first", "item": "one"}],
[{"text": "second", "item": "two"}, {"text": "first", "item": "one"}],
["first", "second"],
])
def test_grouped_target_refuses_records_that_cannot_identify_prompt_order(records):
target = ConceptTarget(
"grouped", ["first", "second"], {"neutral": ["control"]},
recipe=ExtractionRecipe(layer_select={"strategy": "cv_auc", "group_by": "item"}),
records={"positive": records},
)
with pytest.raises(ValueError, match="Concept records do not match prompt order: positive"):
target.sample_groups()
def test_grouped_target_preserves_record_order_and_identity():
records = [{"text": "first", "item": "one"}, {"text": "second", "item": "two"}]
target = ConceptTarget(
"grouped", ["first", "second"], {"neutral": ["control"]},
recipe=ExtractionRecipe(layer_select={"strategy": "cv_auc", "group_by": "item"}),
records={"positive": records, "control:neutral": [{"text": "control", "item": 0}]},
)
assert target.prompt_records("positive", target.positive) == records
assert target.sample_groups() == {"positive": ["one", "two"], "control:neutral": [0]}
@pytest.mark.parametrize("name", ["readout", "denoise", "layer_select", "intervention_layer"])
def test_extraction_rejects_unknown_recipe_options(name):
recipe = ExtractionRecipe(**{name: {"silently_ignored_option": True}})
with pytest.raises(ValueError, match=rf"Unknown recipe\.{name} keys"):
validate_recipe(recipe)
def test_structured_entry_point_cannot_also_apply_legacy_suffix():
recipe = ExtractionRecipe(
entry_point=EntryPoint(), readout={"position": "final_token", "suffix": "legacy"},
)
with pytest.raises(ValueError, match="entry_point replaces legacy readout"):
validate_recipe(recipe)
@pytest.mark.parametrize("value", [0, -1, True, 1.5, "32"])
def test_evaluator_rejects_invalid_sequence_limit_before_model_access(value):
model, tokenizer = Mock(), Mock()
with pytest.raises(ValueError, match="max_seq_length must be a positive integer"):
ModelEvaluator(model, tokenizer, None, max_seq_length=value)
assert model.mock_calls == tokenizer.mock_calls == []
def test_manifest_rejects_system_message_after_user_with_preserved_cause():
path = Path(__file__).parent / "fixtures/concept_packs/valid_minimal/manifest.yaml"
manifest = yaml.safe_load(path.read_text())
manifest["schema_version"] = "1.1"
manifest["recipe"]["entry_point"] = {"messages": [
{"role": "user", "content": "first"},
{"role": "system", "content": "too late"},
]}
with pytest.raises(PackError, match="manifest.recipe.entry_point:.*system must be first") as error:
validate_manifest(manifest)
assert isinstance(error.value.__cause__, ValueError)
@pytest.mark.parametrize("data", ["1: value", "? [first, second]\n: value", "true: value"])
def test_manifest_rejects_non_string_keys_before_schema_validation(data):
with pytest.raises(PackError, match="manifest.yaml: keys must be strings"):
load_manifest(data)