|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Verify Python tests are faithful 1:1 translations of TypeScript tests. |
| 3 | +
|
| 4 | +For each TS test file, extracts every it("...") test name, converts to |
| 5 | +snake_case, and checks that a corresponding def test_...() exists in the |
| 6 | +Python translation. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python scripts/verify_test_fidelity.py [--fix] |
| 10 | +
|
| 11 | +With --fix: appends stub test functions for any missing translations. |
| 12 | +""" |
| 13 | + |
| 14 | +import re |
| 15 | +import sys |
| 16 | +import os |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +TS_ROOT = os.environ.get("TS_ROOT", "/tmp/vercel-chat") |
| 20 | +PY_ROOT = os.environ.get("PY_ROOT", str(Path(__file__).parent.parent)) |
| 21 | + |
| 22 | +# Mapping: TS test file -> Python test file |
| 23 | +MAPPING = { |
| 24 | + "packages/chat/src/chat.test.ts": "tests/test_chat_faithful.py", |
| 25 | + "packages/chat/src/thread.test.ts": "tests/test_thread_faithful.py", |
| 26 | + "packages/chat/src/channel.test.ts": "tests/test_channel_faithful.py", |
| 27 | + "packages/chat/src/markdown.test.ts": "tests/test_markdown_faithful.py", |
| 28 | + "packages/chat/src/streaming-markdown.test.ts": "tests/test_streaming_markdown.py", |
| 29 | + "packages/chat/src/serialization.test.ts": "tests/test_serialization.py", |
| 30 | + "packages/chat/src/ai.test.ts": "tests/test_ai.py", |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +def ts_name_to_python(ts_name: str) -> str: |
| 35 | + """Convert a TS it("should do X") name to test_should_do_x.""" |
| 36 | + name = ts_name.lower() |
| 37 | + name = re.sub(r"[^a-z0-9\s]", "", name) |
| 38 | + name = re.sub(r"\s+", "_", name.strip()) |
| 39 | + name = re.sub(r"_+", "_", name) |
| 40 | + return f"test_{name}" |
| 41 | + |
| 42 | + |
| 43 | +def extract_ts_tests(ts_path: str) -> list[tuple[str, str, str]]: |
| 44 | + """Extract (describe, it_name, python_name) from a TS test file.""" |
| 45 | + with open(ts_path) as f: |
| 46 | + content = f.read() |
| 47 | + |
| 48 | + tests = [] |
| 49 | + current_describe = "" |
| 50 | + |
| 51 | + for line in content.split("\n"): |
| 52 | + desc_match = re.search(r'describe\("([^"]+)"', line) |
| 53 | + if desc_match: |
| 54 | + current_describe = desc_match.group(1) |
| 55 | + |
| 56 | + it_match = re.search(r'it\("([^"]+)"', line) |
| 57 | + if it_match: |
| 58 | + ts_name = it_match.group(1) |
| 59 | + py_name = ts_name_to_python(ts_name) |
| 60 | + tests.append((current_describe, ts_name, py_name)) |
| 61 | + |
| 62 | + return tests |
| 63 | + |
| 64 | + |
| 65 | +def extract_py_tests(py_path: str) -> set[str]: |
| 66 | + """Extract all test function names from a Python file.""" |
| 67 | + if not os.path.exists(py_path): |
| 68 | + return set() |
| 69 | + with open(py_path) as f: |
| 70 | + content = f.read() |
| 71 | + return set(re.findall(r"def (test_\w+)", content)) |
| 72 | + |
| 73 | + |
| 74 | +def fuzzy_match(py_name, py_tests): |
| 75 | + """Try to match a derived Python test name against existing tests.""" |
| 76 | + if py_name in py_tests: |
| 77 | + return py_name |
| 78 | + |
| 79 | + words = [w for w in py_name.replace("test_", "").split("_") if len(w) > 2][:4] |
| 80 | + for existing in py_tests: |
| 81 | + if all(w in existing for w in words): |
| 82 | + return existing |
| 83 | + return None |
| 84 | + |
| 85 | + |
| 86 | +def check_fidelity(ts_rel: str, py_rel: str) -> tuple[list, list, int]: |
| 87 | + """Returns (missing, extra, matched).""" |
| 88 | + ts_path = os.path.join(TS_ROOT, ts_rel) |
| 89 | + py_path = os.path.join(PY_ROOT, py_rel) |
| 90 | + |
| 91 | + if not os.path.exists(ts_path): |
| 92 | + return [], [], 0 |
| 93 | + |
| 94 | + ts_tests = extract_ts_tests(ts_path) |
| 95 | + py_tests = extract_py_tests(py_path) |
| 96 | + remaining_py = set(py_tests) |
| 97 | + |
| 98 | + missing = [] |
| 99 | + matched = 0 |
| 100 | + |
| 101 | + for describe, ts_name, py_name in ts_tests: |
| 102 | + m = fuzzy_match(py_name, remaining_py) |
| 103 | + if m: |
| 104 | + matched += 1 |
| 105 | + remaining_py.discard(m) |
| 106 | + else: |
| 107 | + missing.append((describe, ts_name, py_name)) |
| 108 | + |
| 109 | + extra = sorted(remaining_py) |
| 110 | + return missing, extra, matched |
| 111 | + |
| 112 | + |
| 113 | +def generate_stubs(ts_rel, missing): |
| 114 | + """Generate Python test stubs for missing translations.""" |
| 115 | + lines = [ |
| 116 | + "", |
| 117 | + "", |
| 118 | + f"# ===== STUBS: {len(missing)} tests need faithful translation =====", |
| 119 | + f"# Source: {ts_rel}", |
| 120 | + "# Each stub must be translated line-by-line from the TS it() block.", |
| 121 | + "# Do NOT write new tests — translate the EXISTING TS test.", |
| 122 | + ] |
| 123 | + current_class = "" |
| 124 | + |
| 125 | + for describe, ts_name, py_name in missing: |
| 126 | + class_name = "Test" + re.sub(r"[^a-zA-Z0-9]", "", describe.title().replace(" ", "")) |
| 127 | + if class_name != current_class: |
| 128 | + current_class = class_name |
| 129 | + lines.append(f"\n\nclass {class_name}Stubs:") |
| 130 | + lines.append(f' """Stubs for: {describe}"""') |
| 131 | + |
| 132 | + lines.append(f"") |
| 133 | + lines.append(f" async def {py_name}(self):") |
| 134 | + lines.append(f' # TS: it("{ts_name}")') |
| 135 | + lines.append(f" raise NotImplementedError(\"Translate from {ts_rel}\")") |
| 136 | + |
| 137 | + return "\n".join(lines) |
| 138 | + |
| 139 | + |
| 140 | +def main() -> int: |
| 141 | + fix_mode = "--fix" in sys.argv |
| 142 | + total_missing = 0 |
| 143 | + total_matched = 0 |
| 144 | + total_ts = 0 |
| 145 | + |
| 146 | + print("=" * 70) |
| 147 | + print("TEST FIDELITY REPORT") |
| 148 | + print("=" * 70) |
| 149 | + |
| 150 | + for ts_rel, py_rel in MAPPING.items(): |
| 151 | + ts_path = os.path.join(TS_ROOT, ts_rel) |
| 152 | + if not os.path.exists(ts_path): |
| 153 | + print(f"\n{ts_rel} — SKIPPED (file not found)") |
| 154 | + continue |
| 155 | + |
| 156 | + ts_tests = extract_ts_tests(ts_path) |
| 157 | + missing, extra, matched = check_fidelity(ts_rel, py_rel) |
| 158 | + |
| 159 | + total_ts += len(ts_tests) |
| 160 | + total_matched += matched |
| 161 | + total_missing += len(missing) |
| 162 | + |
| 163 | + status = "OK" if not missing else f"GAPS ({len(missing)})" |
| 164 | + print(f"\n{ts_rel}") |
| 165 | + print(f" -> {py_rel}") |
| 166 | + print( |
| 167 | + f" TS: {len(ts_tests)} | Matched: {matched} | Missing: {len(missing)} | Extra: {len(extra)} | {status}" |
| 168 | + ) |
| 169 | + |
| 170 | + if missing: |
| 171 | + for describe, ts_name, py_name in missing[:5]: |
| 172 | + print(f" MISSING: [{describe}] {ts_name}") |
| 173 | + if len(missing) > 5: |
| 174 | + print(f" ... and {len(missing) - 5} more") |
| 175 | + |
| 176 | + if fix_mode and missing: |
| 177 | + py_path = os.path.join(PY_ROOT, py_rel) |
| 178 | + stubs = generate_stubs(ts_rel, missing) |
| 179 | + |
| 180 | + if os.path.exists(py_path): |
| 181 | + with open(py_path, "a") as f: |
| 182 | + f.write(stubs) |
| 183 | + print(f" -> Appended {len(missing)} stubs to {py_rel}") |
| 184 | + else: |
| 185 | + with open(py_path, "w") as f: |
| 186 | + f.write(f'"""Faithful translation of {ts_rel}"""\n\nimport pytest\n') |
| 187 | + f.write(stubs) |
| 188 | + print(f" -> Created {py_rel} with {len(missing)} stubs") |
| 189 | + |
| 190 | + pct = total_matched * 100 // max(total_ts, 1) |
| 191 | + print(f"\n{'=' * 70}") |
| 192 | + print(f"TOTAL: {total_matched}/{total_ts} matched ({pct}%), {total_missing} missing") |
| 193 | + |
| 194 | + if total_missing > 0: |
| 195 | + print("\nRun with --fix to generate stubs for missing tests.") |
| 196 | + return 1 |
| 197 | + print("\nAll TS tests have Python equivalents.") |
| 198 | + return 0 |
| 199 | + |
| 200 | + |
| 201 | +if __name__ == "__main__": |
| 202 | + sys.exit(main()) |
0 commit comments