Viewing files at commit ebd567552629Commit detailsView latest

Browse files
tests/test_public_readiness.py 110 lines · 3.8 KB · python
1
from __future__ import annotations
2
3
import contextlib
4
import io
5
import json
6
import sys
7
import tempfile
8
import unittest
9
from pathlib import Path
10
11
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
12
13
import public_readiness
14
15
16
def write_jsonl(path: Path, rows: list[dict]) -> None:
17
    path.parent.mkdir(parents=True, exist_ok=True)
18
    path.write_text("\n".join(json.dumps(row) for row in rows) + "\n")
19
20
21
def row(subject: str, run: int, *, operation: str = "status.clean") -> dict:
22
    return {
23
        "bench_id": "test",
24
        "profile": "standard",
25
        "subject": subject,
26
        "subject_kind": "oak" if subject.startswith("oak") else "git",
27
        "subject_label": subject,
28
        "scenario": "tiny_text_100",
29
        "operation": operation,
30
        "run": run,
31
        "returncode": 0,
32
        "elapsed_ms": 10.0 + run,
33
        "command": [subject, "status"],
34
        "tool_call_count": 1,
35
        "env_isolation_version": "1",
36
        "randomized_subject_order": True,
37
        "subject_order": ["git", "git_untracked_cache", "git_fsmonitor", "oak_main"],
38
        "source": {"oak_hash": "abc123", "oak_status": ""},
39
    }
40
41
42
class PublicReadinessTest(unittest.TestCase):
43
    def test_writes_readiness_bundle_for_same_inputs(self) -> None:
44
        with tempfile.TemporaryDirectory() as tmp:
45
            root = Path(tmp)
46
            results = root / "results"
47
            out_dir = root / "bundle"
48
            rows = []
49
            for run in range(30):
50
                rows.extend(
51
                    [
52
                        row("git", run),
53
                        row("git_untracked_cache", run),
54
                        row("git_fsmonitor", run),
55
                        row("oak_main", run),
56
                    ]
57
                )
58
            write_jsonl(results / "latest.jsonl", rows)
59
60
            stdout = io.StringIO()
61
            with contextlib.redirect_stdout(stdout):
62
                code = public_readiness.main(
63
                    [
64
                        "--results",
65
                        str(results),
66
                        "--out-dir",
67
                        str(out_dir),
68
                        "--bootstrap-samples",
69
                        "10",
70
                    ]
71
                )
72
73
            self.assertEqual(code, 0, stdout.getvalue())
74
            manifest = json.loads((out_dir / "manifest.json").read_text())
75
            self.assertTrue(manifest["ready"])
76
            self.assertEqual(manifest["inputs"], [str((results / "latest.jsonl").resolve())])
77
            self.assertTrue((out_dir / "publish_gate.txt").exists())
78
            self.assertTrue((out_dir / "benchmark_stats.md").exists())
79
            self.assertTrue((out_dir / "opportunities.md").exists())
80
            self.assertTrue((out_dir / "opportunities.json").exists())
81
82
    def test_publish_gate_failure_marks_bundle_not_ready(self) -> None:
83
        with tempfile.TemporaryDirectory() as tmp:
84
            root = Path(tmp)
85
            results = root / "results"
86
            out_dir = root / "bundle"
87
            write_jsonl(results / "latest.jsonl", [row("git", 0)])
88
89
            stdout = io.StringIO()
90
            with contextlib.redirect_stdout(stdout):
91
                code = public_readiness.main(
92
                    [
93
                        "--results",
94
                        str(results),
95
                        "--out-dir",
96
                        str(out_dir),
97
                        "--skip-stats",
98
                        "--skip-opportunities",
99
                    ]
100
                )
101
102
            self.assertEqual(code, 1)
103
            manifest = json.loads((out_dir / "manifest.json").read_text())
104
            self.assertFalse(manifest["ready"])
105
            gate = next(check for check in manifest["checks"] if check["name"] == "publish_gate")
106
            self.assertEqual(gate["exit_code"], 1)
107
108
109
if __name__ == "__main__":
110
    unittest.main()