#!/usr/bin/env python3
"""Audit MugiTests coverage against the 14 canonical user-facing surfaces.

Prints a matrix of surface → test files. Exit 1 if any surface has zero tests.
Used by the 1.0 hardening plan (WS2).
"""
from __future__ import annotations

import os
import sys

_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
_TESTS_DIR = os.path.join(_REPO, "MugiApp", "MugiTests")

# Canonical surfaces (plan-size-cap.mdc) → filename substring hints in MugiTests.
SURFACE_HINTS: dict[str, tuple[str, ...]] = {
    "Chat": ("Chat", "ContentView", "DashboardChat"),
    "Sidebar": ("WorkspaceReload", "Sidebar"),
    "Kanban": ("Kanban",),
    "Preferences": ("PreferencesPane", "Prefs"),
    "Vault": ("Vault",),
    "Live View": ("LiveView",),
    "Activity feed": ("Activity", "ProactiveInbox", "SubagentTracker"),
    "Mesh": ("Mesh",),
    "Skills/MCP": ("MCP",),
    "Plugins / Dashboard": ("Plugin", "Dashboard"),
    "Knowledge / Vault search": ("Knowledge",),
    "Voice / Audio": ("Audio", "MLX", "TTS"),
    "Diagnostics": ("Setup", "BackendInfo", "Diagnostics"),
    "Subagent / Agent loop": ("Subagent",),
}


def main() -> int:
    if not os.path.isdir(_TESTS_DIR):
        print(f"ERROR: {_TESTS_DIR} missing", file=sys.stderr)
        return 2
    files = [f for f in os.listdir(_TESTS_DIR) if f.endswith("Tests.swift") or f.endswith("Test.swift")]
    failures: list[str] = []
    print("Swift surface coverage audit\n")
    for surface, hints in SURFACE_HINTS.items():
        matches = [f for f in files if any(h.lower() in f.lower() for h in hints)]
        status = "OK" if matches else "MISSING"
        print(f"  [{status:7}] {surface}: {', '.join(matches) or '(none)'}")
        if not matches:
            failures.append(surface)
    print(f"\nTotal MugiTests files: {len(files)}")
    if failures:
        print(f"\nAUDIT: FAIL — {len(failures)} surface(s) without tests: {failures}")
        return 1
    print("\nAUDIT: PASS — all 14 surfaces have at least one test file")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
