"""
Tests for #681 — Python scripts have shebangs.
"""

import os
import unittest
from pathlib import Path

REPO_ROOT = Path(__file__).parent.parent

# Files mentioned in issue #681
ISSUE_FILES = [
    "bin/glitch_patterns.py",
    "bin/nostr-agent-demo.py",
    "bin/soul_eval_gate.py",
    "scripts/captcha_bypass_handler.py",
    "scripts/diagram_meaning_extractor.py",
    "scripts/visual_pr_reviewer.py",
]


class TestShebangs(unittest.TestCase):
    def test_all_issue_files_have_shebangs(self):
        """All files listed in #681 have #!/usr/bin/env python3 shebang."""
        for relpath in ISSUE_FILES:
            filepath = REPO_ROOT / relpath
            with self.subTest(file=relpath):
                self.assertTrue(filepath.exists(), f"{relpath} not found")
                with open(filepath) as f:
                    first_line = f.readline().strip()
                self.assertEqual(
                    first_line, "#!/usr/bin/env python3",
                    f"{relpath} missing shebang, first line: {first_line}"
                )

    def test_bin_scripts_have_shebangs(self):
        """All .py files in bin/ have shebangs."""
        bin_dir = REPO_ROOT / "bin"
        if not bin_dir.exists():
            self.skipTest("bin/ not found")

        for filepath in bin_dir.glob("*.py"):
            with self.subTest(file=filepath.name):
                with open(filepath) as f:
                    first_line = f.readline().strip()
                self.assertTrue(
                    first_line.startswith("#!"),
                    f"bin/{filepath.name} missing shebang"
                )


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