#!/usr/bin/env python3 """Verify package-smoke wrappers report the expected package readiness matrix.""" from __future__ import annotations import json import re import sys from pathlib import Path EXPECTED_PACKAGE_KINDS = [ "windows-appx", "android-standard-apk", "android-quest-apk", "android-focus-apk", "apple-bundle", "webgl", ] def repo_root() -> Path: return Path(__file__).resolve().parents[2] def powershell_package_kinds(root: Path) -> list[str]: script = (root / "scripts" / "automation" / "package-smoke.ps1").read_text(encoding="utf-8") match = re.search(r"\[string\[\]\]\$PackageKinds\s*=\s*@\((.*?)\n\s*\)", script, re.S) if not match: raise RuntimeError("Could not find PackageKinds default in package-smoke.ps1") return sorted(re.findall(r'"([^"]+)"', match.group(1))) def shell_package_kinds(root: Path) -> list[str]: script = (root / "scripts" / "automation" / "package-smoke.sh").read_text(encoding="utf-8") return sorted(set(re.findall(r'"kind":"([^"]+)"', script))) def count_regex(root: Path, patterns: dict[str, str]) -> dict[str, int]: counts: dict[str, int] = {} for script_name, pattern in patterns.items(): text = (root / "scripts" / "automation" / script_name).read_text(encoding="utf-8") counts[script_name] = len(re.findall(pattern, text)) return counts def main() -> int: root = repo_root() expected = sorted(EXPECTED_PACKAGE_KINDS) ps_kinds = powershell_package_kinds(root) sh_kinds = shell_package_kinds(root) debt_counts = count_regex(root, { "package-smoke.ps1": r'debt\s*=\s*"DEBT-0011"', "package-smoke.sh": r'"debt":"DEBT-0011"', }) blocked_counts = count_regex(root, { "package-smoke.ps1": r'-Status\s+"blocked"', "package-smoke.sh": r'"status":"blocked"', }) readiness_mode_counts = { "package-smoke.ps1": (root / "scripts" / "automation" / "package-smoke.ps1").read_text(encoding="utf-8").count("ReadinessOnly"), "package-smoke.sh": (root / "scripts" / "automation" / "package-smoke.sh").read_text(encoding="utf-8").count("readiness_only"), } missing = { "package-smoke.ps1": [kind for kind in expected if kind not in ps_kinds], "package-smoke.sh": [kind for kind in expected if kind not in sh_kinds], } unexpected = { "package-smoke.ps1": [kind for kind in ps_kinds if kind not in expected], "package-smoke.sh": [kind for kind in sh_kinds if kind not in expected], } debt_thresholds = { "package-smoke.ps1": 1, "package-smoke.sh": len(expected), } debt_complete = {name: count >= debt_thresholds[name] for name, count in debt_counts.items()} blocked_complete = {name: count >= len(expected) for name, count in blocked_counts.items()} readiness_mode_present = {name: count > 0 for name, count in readiness_mode_counts.items()} ok = ( all(not values for values in missing.values()) and all(not values for values in unexpected.values()) and all(debt_complete.values()) and all(blocked_complete.values()) and all(readiness_mode_present.values()) ) print(json.dumps({ "ok": ok, "expectedPackageKinds": expected, "packageKinds": { "package-smoke.ps1": ps_kinds, "package-smoke.sh": sh_kinds, }, "missing": missing, "unexpected": unexpected, "debtComplete": debt_complete, "blockedComplete": blocked_complete, "readinessModePresent": readiness_mode_present, }, separators=(",", ":"))) return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())