-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
106 lines (86 loc) · 2.4 KB
/
Copy pathtest_cli.py
File metadata and controls
106 lines (86 loc) · 2.4 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
from __future__ import annotations
import json
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def run_cli(args: list[str], *, stdin: str | None = None) -> subprocess.CompletedProcess[str]:
return subprocess.run(
[sys.executable, "-m", "memory_quality_gate", *args],
cwd=ROOT,
env={"PYTHONPATH": str(ROOT / "src")},
input=stdin,
capture_output=True,
text=True,
check=False,
)
def test_cli_check_returns_zero_on_pass() -> None:
text = (
"Always run ./scripts/deploy.sh --dry-run before shipping v2.4.1 because "
"it prevents partial deploys."
)
proc = run_cli(
[
"check",
"--text",
text,
"--format",
"json",
]
)
assert proc.returncode == 0
payload = json.loads(proc.stdout)
assert payload["passed"] is True
def test_cli_check_returns_two_on_fail() -> None:
proc = run_cli(
[
"check",
"--text",
"noted",
"--format",
"json",
]
)
assert proc.returncode == 2
payload = json.loads(proc.stdout)
assert payload["passed"] is False
def test_cli_rejects_oversized_file(tmp_path: Path) -> None:
candidate = tmp_path / "candidate.txt"
candidate.write_text("x" * 11, encoding="utf-8")
proc = run_cli(
[
"score",
"--file",
str(candidate),
"--max-input-bytes",
"10",
]
)
assert proc.returncode == 2
assert "exceeds maximum size of 10 bytes" in proc.stderr
assert "Traceback" not in proc.stderr
def test_cli_rejects_oversized_stdin() -> None:
proc = run_cli(
[
"score",
"--max-input-bytes",
"10",
],
stdin="x" * 11,
)
assert proc.returncode == 2
assert "stdin exceeds maximum size of 10 bytes" in proc.stderr
def test_cli_redacts_candidate_text_in_json() -> None:
proc = run_cli(
[
"score",
"--text",
"Always check docs/scoring-model.md because retrieval workers depend on it.",
"--format",
"json",
"--redact-text",
]
)
assert proc.returncode == 0
payload = json.loads(proc.stdout)
assert payload["candidate"]["text"] == "[redacted]"