"""Tests for the source-text assertion guard.

Each flagged case reproduces a shape from the 2026-08-19 batch whose report
proved a comment containing the expected literal was enough to pass.
"""

import sys
import unittest
from pathlib import Path

SCRIPTS = Path(__file__).parents[1] / "scripts"
sys.path.insert(0, str(SCRIPTS))

import source_text_guard

PATCH = (
    "diff --git a/internal/biz/app/doubao.go b/internal/biz/app/doubao.go\n"
    "--- a/internal/biz/app/doubao.go\n"
    "+++ b/internal/biz/app/doubao.go\n"
    "@@ -1,3 +1,4 @@\n"
    "+\tpicked := false\n"
    "diff --git a/scripts/validate/e2e.sh b/scripts/validate/e2e.sh\n"
    "--- a/scripts/validate/e2e.sh\n"
    "+++ b/scripts/validate/e2e.sh\n"
    "@@ -1,2 +1,3 @@\n"
    "+echo stage\n"
)


class SourceTextGuardTest(unittest.TestCase):
    def test_read_text_plus_assert_in_is_flagged(self):
        content = (
            "def test_control_flow():\n"
            "    src = Path('/testbed/internal/biz/app/doubao.go').read_text()\n"
            "    assert 'picked := false' in src\n"
        )
        self.assertTrue(source_text_guard.analyze(content, PATCH))

    def test_count_on_patch_path_is_flagged(self):
        """A repo-relative path the patch touches counts as source under test."""
        content = (
            "def test_error_returns():\n"
            "    src = open('scripts/validate/e2e.sh').read()\n"
            "    assert src.count('阶段十之二') == 2\n"
        )
        self.assertTrue(source_text_guard.analyze(content, PATCH))

    def test_docstring_assertion_is_flagged(self):
        content = "def test_contract():\n    assert 'column projection' in analyze_sql.__doc__\n"
        self.assertTrue(source_text_guard.analyze(content, PATCH))

    def test_getsource_assertion_is_flagged(self):
        content = (
            "import inspect\n"
            "def test_shape():\n"
            "    assert 'for row in rows' in inspect.getsource(analyze_sql)\n"
        )
        self.assertTrue(source_text_guard.analyze(content, PATCH))

    def test_script_grep_on_source_under_test_is_flagged(self):
        content = (
            "#!/bin/sh\n"
            "set -e\n"
            "grep -q 'picked := false' /testbed/internal/biz/app/doubao.go\n"
        )
        self.assertTrue(source_text_guard.analyze(content, PATCH, is_script=True))

    def test_script_compiling_source_is_not_flagged(self):
        """javac/go build name source files but compile them, which is legitimate."""
        content = "#!/bin/sh\njavac -d /tmp/out /testbed/src/main/java/Ssrf.java\n"
        self.assertEqual([], source_text_guard.analyze(content, PATCH, is_script=True))

    def test_script_grep_on_program_output_is_not_flagged(self):
        content = "#!/bin/sh\n./run > /tmp/out.log\ngrep -q OK /tmp/out.log\n"
        self.assertEqual([], source_text_guard.analyze(content, PATCH, is_script=True))

    def test_reading_own_fixture_is_not_flagged(self):
        """A test may write and read back its own fixture; that is not the subject."""
        content = (
            "def test_linter(tmp_path):\n"
            "    sample = tmp_path / 'sample.py'\n"
            "    sample.write_text('x = 1\\n')\n"
            "    assert lint(sample.read_text()) == []\n"
        )
        self.assertEqual([], source_text_guard.analyze(content, PATCH))

    def test_behavior_test_without_reads_is_not_flagged(self):
        content = "def test_ok():\n    assert normalize('a') == 'A'\n"
        self.assertEqual([], source_text_guard.analyze(content, PATCH))

    def test_read_without_any_assertion_is_not_flagged(self):
        """Loading source as test input, with no assertion, is left to other gates."""
        content = "src = Path('/testbed/internal/biz/app/doubao.go').read_text()\nprint(src)\n"
        self.assertEqual([], source_text_guard.analyze(content, PATCH))


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