Files
Codewhale/scripts/test_measure_runtime_contract.py
CodeWhale Bot e34f207d21 refactor(engine): extract the shared runtime from the TUI [skip ci]
Move the Engine band, extension host, fixtures and shared test support into
codewhale-engine; wire TUI/CLI to that owner. Preserve human-decision
construction boundaries and lower reusable test helpers. Remove unused
copied dependencies and repair path-based guards, scripts and crate maps.

Correct the blocking scanner external-module scope leak; raised counts equal
the corrected pre-move source. Refresh embedded built-in hashes after path-only
bundle regeneration. Restore the provider-free prompt budget to the exact
pre-b984e31290 macOS bytes and representative identities; tool catalogs are
unchanged. Portable prompt-fixture pinning remains follow-up work.

Local validation: Engine/TUI/CLI clippy --tests --no-deps -D warnings passed
with the existing allow-list; production CLI check passed; fmt passed.
Focused native run: 705 passed / 83 failed initially; after hash/path repairs,
81 affected cases plus 2 existing build-relocation cases passed (83/83).
Thus 786/788 original cases pass; 2 compiled-release-image cases lack input
and are not qualified. Frozen input inventories unchanged. macOS test-link
unwind-table warnings remain. Four metric tests pass; all 55 budgets pass.
Host Node: 413 pass, 7 skip; Bun: 414 pass, 6 skip. Existing scanner checks:
34 + 33 pass. Compiled-delivery synthetic fixtures: 7 pass. Web: 42 pass.
Import, blocking, dead-code, coarse-map, version and facts checks pass.

CW-7701. No hosted CI, provider call, release build or publication.
Stage A part 2 and the old App Server/ratatui dependencies remain separate.
2026-10-31 18:55:14 -07:00

116 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""Hermetic tests for scripts/measure-runtime-contract.py."""
from __future__ import annotations
import importlib.util
import io
import json
import subprocess
import sys
import unittest
from contextlib import redirect_stdout
from pathlib import Path
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
SCRIPT = ROOT / "scripts" / "measure-runtime-contract.py"
SPEC = importlib.util.spec_from_file_location("measure_runtime_contract", SCRIPT)
assert SPEC and SPEC.loader
mod = importlib.util.module_from_spec(SPEC)
sys.modules[SPEC.name] = mod
SPEC.loader.exec_module(mod)
class RuntimeContractMeasurementTests(unittest.TestCase):
def test_metric_runner_targets_exact_library_test(self) -> None:
short_name = "print_mode_tool_catalog_metrics"
exact_name = f"core::engine::tests::{short_name}"
marker = "TOOL_CATALOG_METRICS "
payload = {"modes": {}, "surface_profile": "fixture"}
completed = subprocess.CompletedProcess(
args=[],
returncode=0,
stdout=(
"running 1 test\n"
f"test {exact_name} ... {marker}{json.dumps(payload)}\n"
"ok\n\n"
"test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; "
"0 filtered out\n"
),
stderr="",
)
with mock.patch.object(mod.subprocess, "run", return_value=completed) as run:
measured = mod.run_metric(short_name, marker)
self.assertEqual(measured, payload)
self.assertEqual(
run.call_args.args[0],
[
"cargo",
"test",
"--locked",
"-p",
"codewhale-engine",
"--lib",
exact_name,
"--",
"--ignored",
"--exact",
"--nocapture",
"--test-threads=1",
],
)
def test_successful_zero_test_run_is_rejected(self) -> None:
completed = subprocess.CompletedProcess(
args=[],
returncode=0,
stdout=(
"running 0 tests\n\n"
"test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; "
"0 filtered out\n"
),
stderr="",
)
with (
mock.patch.object(mod.subprocess, "run", return_value=completed),
redirect_stdout(io.StringIO()),
self.assertRaisesRegex(RuntimeError, "ran zero tests"),
):
mod.run_metric(
"print_mode_tool_catalog_metrics", "TOOL_CATALOG_METRICS "
)
def test_successful_test_without_expected_marker_is_rejected(self) -> None:
completed = subprocess.CompletedProcess(
args=[],
returncode=0,
stdout=(
"running 1 test\n"
"test core::engine::tests::print_mode_tool_catalog_metrics ... ok\n\n"
"test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; "
"0 filtered out\n"
),
stderr="",
)
with (
mock.patch.object(mod.subprocess, "run", return_value=completed),
redirect_stdout(io.StringIO()),
self.assertRaisesRegex(
RuntimeError,
"missing TOOL_CATALOG_METRICS marker from exact library metric test",
),
):
mod.run_metric(
"print_mode_tool_catalog_metrics", "TOOL_CATALOG_METRICS "
)
if __name__ == "__main__":
raise SystemExit(unittest.main())