# -*- coding: utf-8 -*-
"""Seam test for scripts/repo_gates.py.

Run:  python3 scripts/test_repo_gates.py

WHY THIS EXISTS. An entry point is a seam by definition: a runner that LISTS a gate but never
executes it is inert and fully green, and this project has shipped an inert seam four times. So
the assertion is on each member gate's OWN distinctive stdout — never on the runner's summary
line, which the runner can print without ever calling anything — plus the exit code, which is a
runner's actual effect.

Red-proofed 2026-08-02: replacing run_gate's body with `return 0` (the inert runner) turns
test_every_member_gate_actually_ran red while the summary still prints "all felhom.eu gates OK".
"""
import os
import subprocess
import sys
import unittest

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ENTRY = os.path.join(ROOT, "scripts", "repo_gates.py")

# (label, a substring only THAT gate can print)
FINGERPRINTS = [
    ("site",            "site gates OK"),
    ("hostinstall",     "hostinstall gates: ALL PASS"),
    ("hub-confirm",     "hub confirm gate"),
    ("manifest-bearer", "manifest bearer gate"),
    ("reuse-refs",      "cited paths — exact"),
]


class RepoGatesTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        p = subprocess.run([sys.executable, ENTRY, "--fast"], cwd=ROOT,
                           stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        cls.rc = p.returncode
        cls.out = p.stdout.decode("utf-8", "replace")

    def test_exit_code_is_zero(self):
        self.assertEqual(self.rc, 0, self.out)

    def test_every_member_gate_actually_ran(self):
        for label, fingerprint in FINGERPRINTS:
            self.assertIn(fingerprint, self.out,
                          "gate %r is listed but its own output never appeared — an inert runner "
                          "prints the summary without calling anything:\n%s" % (label, self.out))

    def test_unknown_argument_is_rejected(self):
        p = subprocess.run([sys.executable, ENTRY, "--nope"], cwd=ROOT,
                           stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        self.assertEqual(p.returncode, 2, p.stdout.decode("utf-8", "replace"))


if __name__ == "__main__":
    unittest.main(verbosity=2)
