"""Corpus invariants. These run on the frozen dataset and catch authoring slips.""" from __future__ import annotations import json from collections import Counter from tooluse1.cases_build import ALL_TOOLS, build, manifest from tooluse1.schema import ExpectCall, ExpectNoCall from tooluse1.tools import REGISTRY, canonical_name def test_ids_are_unique(): ids = [case.id for case in build()] assert len(ids) == len(set(ids)) _ID_PREFIX = { "calculator": "calc", "get_weather": "weather", "web_search": "search", "translate": "translate", "convert_currency": "currency", "convert_units": "units", "set_timer": "timer", "save_note": "note", "get_time": "time", "negative": "negative", "clarification": "clarify", "multi_step": "chain", } def test_ids_are_readable(): for case in build(): prefix = _ID_PREFIX[case.category] assert case.id.startswith(prefix + "-"), case.id assert case.id.startswith(f"{prefix}-{case.lang}-") or f"-{case.lang}-" in case.id, case.id def test_tools_exist_in_registry(): for case in build(): assert case.tools, case.id for tool in case.tools: assert tool in REGISTRY, f"{case.id}: unknown tool {tool}" def test_call_expectations_match_tool_schemas(): for case in build(): if not isinstance(case.expect, ExpectCall): continue spec = REGISTRY[case.expect.name] keys = set(case.expect.args) assert set(spec.required) <= keys, f"{case.id}: missing required args" assert keys <= set(spec.parameters), f"{case.id}: unknown args {keys - set(spec.parameters)}" assert case.expect.name in case.tools, f"{case.id}: tool not in the available list" def test_negatives_never_reference_unavailable_tools(): for case in build(): if isinstance(case.expect, ExpectCall): continue assert isinstance(case.expect, ExpectNoCall) if (case.expect.answer_contains_any or case.expect.answer_contains_all) and len(case.tools) == len(ALL_TOOLS): assert case.category in {"negative", "clarification", "multi_step"}, case.id def test_answer_constraints_are_lowercase(): for case in build(): if isinstance(case.expect, ExpectCall): continue for needle in case.expect.answer_contains_all: assert needle == needle.lower(), case.id for group in case.expect.answer_contains_any: for needle in group: assert needle == needle.lower(), case.id def test_balanced_languages_and_categories(): cases = build() languages = Counter(case.lang for case in cases) assert set(languages) == {"en"} assert languages["en"] >= 100, languages categories = Counter(case.category for case in cases) for required in ("calculator", "negative", "clarification", "multi_step"): assert categories[required] >= 4, categories def test_multi_step_cases_carry_history(): for case in build(): if case.category != "multi_step": continue assert case.history, case.id assert any(message.role == "tool" for message in case.history), case.id def test_multi_step_history_is_wellformed(): for case in build(): for message in case.history: if message.role == "assistant" and message.tool_name: assert message.tool_name in case.tools, case.id assert message.tool_args is not None, case.id spec = REGISTRY[message.tool_name] assert set(spec.required) <= set(message.tool_args), case.id if message.role == "tool": assert message.content, case.id def test_manifest_is_stable(): first = manifest(build()) second = manifest(build()) assert first["sha256"] == second["sha256"] assert first["cases"] == second["cases"] assert first["call_cases"] + first["no_call_cases"] == first["cases"] def test_dataset_roundtrips_through_json(): for case in build(): assert json.loads(case.to_json())["id"] == case.id def test_negatives_are_not_trivial(): negatives = [case for case in build() if isinstance(case.expect, ExpectNoCall)] constrained = [case for case in negatives if case.expect.answer_contains_any or case.expect.answer_contains_all] assert len(constrained) >= len(negatives) // 2 def test_canonical_names_are_registry_names(): for name in ALL_TOOLS: assert canonical_name(name) in REGISTRY