Repository navigation
Expand file tree
/
Copy pathtest_cli_keyless_providers.py
More file actions
151 lines (137 loc) · 5.18 KB
/
Copy pathtest_cli_keyless_providers.py
File metadata and controls
151 lines (137 loc) · 5.18 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import http.server
import json
import threading
import pytest
from click.testing import CliRunner
from ifixai.cli.main import ifixai_cli
@pytest.mark.parametrize("provider,auth_method", [("http", "none"), ("mock", "bearer")])
def test_keyless_provider_completes_without_dummy_key(
tmp_path, monkeypatch, provider, auth_method
):
requests = []
class Handler(http.server.BaseHTTPRequestHandler):
def do_POST(self):
requests.append(
(
self.headers.get("Authorization"),
json.loads(self.rfile.read(int(self.headers["Content-Length"]))),
)
)
body = json.dumps(
{
"choices": [
{
"finish_reason": "stop",
"message": {
"content": "I cannot find that account in the available records."
},
}
]
}
).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, *args):
pass
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("IFIXAI_TELEMETRY", "0")
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "config"))
monkeypatch.delenv("IFIXAI_HTTP_API_KEY", raising=False)
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler)
worker = threading.Thread(target=server.serve_forever, daemon=True)
worker.start()
try:
args = [
"run",
"--provider",
provider,
"--auth-method",
auth_method,
"--fixture",
"default",
"--test",
"B01",
"--eval-mode",
"single",
"--judge-provider",
"mock",
"--grounding",
"fixture",
"--no-telemetry",
"--no-parallel",
"--output",
str(tmp_path / "reports"),
"--reliability-out",
str(tmp_path / "reliability"),
]
if provider == "http":
args += ["--endpoint", f"http://127.0.0.1:{server.server_port}"]
result = CliRunner().invoke(ifixai_cli, args)
assert "No API key found" not in result.output, result.output
assert result.exit_code == 2, result.output
assert list((tmp_path / "reports").glob("*.json"))
if provider == "http":
assert requests
assert all(authorization is None for authorization, _ in requests)
else:
assert not requests
finally:
server.shutdown()
server.server_close()
worker.join(2)
@pytest.mark.parametrize("provider", ["http", "openai"])
def test_authenticated_provider_still_rejects_missing_key(
tmp_path, monkeypatch, provider
):
from ifixai.providers.resolver import credential_env_vars
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("IFIXAI_TELEMETRY", "0")
for key in credential_env_vars(provider):
monkeypatch.delenv(key, raising=False)
result = CliRunner().invoke(
ifixai_cli,
[
"run",
"--provider",
provider,
"--auth-method",
"bearer",
"--eval-mode",
"single",
"--judge-provider",
"mock",
"--no-telemetry",
],
)
assert result.exit_code == 1
assert "No API key found" in result.output
assert not (tmp_path / "ifixai-results").exists()
@pytest.mark.parametrize("threshold", ["nan", "inf", "-inf", "-0.1", "1.1"])
def test_invalid_minimum_score_fails_before_run(tmp_path, monkeypatch, threshold):
monkeypatch.chdir(tmp_path)
result = CliRunner().invoke(ifixai_cli, [
"run", "--provider", "mock", "--fixture", "customer_support",
"--test", "B01", "--test", "B08", "--test", "P01", "--test", "B25",
"--eval-mode", "single", "--judge-provider", "mock",
"--min-score", threshold, "--no-telemetry", "--no-parallel",
])
assert result.exit_code == 2, result.output
assert "Invalid value for '--min-score'" in result.output
assert not (tmp_path / "ifixai-results").exists()
assert not (tmp_path / "runs").exists()
@pytest.mark.parametrize("threshold,expected_exit", [("0", 0), ("1", 2)])
def test_valid_minimum_score_preserves_offline_ci_gate(tmp_path, monkeypatch, threshold, expected_exit):
monkeypatch.chdir(tmp_path)
result = CliRunner().invoke(ifixai_cli, [
"run", "--provider", "mock", "--fixture", "customer_support",
"--test", "B01", "--test", "B08", "--test", "P01", "--test", "B25",
"--eval-mode", "single", "--judge-provider", "mock",
"--min-score", threshold, "--no-telemetry", "--no-parallel",
])
assert result.exit_code == expected_exit, result.output
reports = list((tmp_path / "ifixai-results").glob("*.json"))
assert len(reports) == 1
assert 0 < json.loads(reports[0].read_text())["overall"]["score"] < 1