File size: 4,526 Bytes
2b42456
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""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