from click.testing import CliRunner
from llm.cli import cli
import os

ROBOTS_CONTENT = (
    "# This robots.txt file is autogenerated by Read the Docs.\n"
    "# It controls the crawling and indexing of your documentation by search engines.\n"
    "#\n"
    "# You can learn more about robots.txt, including how to customize it, in our documentation:\n"
    "#\n"
    "# * Our documentation on Robots.txt: https://docs.readthedocs.com/platform/stable/reference/robots.html\n"
    "# * Our guide about SEO techniques: https://docs.readthedocs.com/platform/stable/guides/technical-docs-seo-guide.html\n"
    "\n"
    "User-agent: *\n"
    "\n"
    "Disallow: # Allow everything\n"
    "\n"
    "Sitemap: https://llm.datasette.io/sitemap.xml\n"
)


def test_rovodev_with_fragment_file(monkeypatch):
    runner = CliRunner()

    # Mock acli to emit a banner, model line and a Response box.
    # The plugin should only output the content inside the Response box by default.
    script = (
        "#!/usr/bin/env python3\n"
        "print('╭───────────────────────────────────────────────────╮')\n"
        "print('│ Announcing GPT-5 in Rovo Dev CLI                  │')\n"
        "print('│                                                   │')\n"
        "print('│ GPT-5 is now available for use in Rovo Dev CLI.   │')\n"
        "print('│ Try it today using the /models command!           │')\n"
        "print('╰───────────────────────────────────────────────────╯')\n"
        "print('Using model: gpt-5-2025-08-07')\n"
        "print('')\n"
        "print('╭─ Response ─────────────────────────────────────────────╮')\n"
        "print('│ Fragment + extra instruction handled correctly        │')\n"
        "print('│ Output should not include model/banners               │')\n"
        "print('╰────────────────────────────────────────────────────────╯')\n"
    )

    with runner.isolated_filesystem():
        # Create the fragment file resembling robots.txt content
        frag_path = os.path.abspath("test-fragment.txt")
        with open(frag_path, "w", encoding="utf-8") as f:
            f.write(ROBOTS_CONTENT)

        # Create mock acli and shim
        mock_path = os.path.abspath("mock_acli.py")
        with open(mock_path, "w", encoding="utf-8") as f:
            f.write(script)
        os.chmod(mock_path, 0o755)

        shim = os.path.abspath("mock_acli_shim.sh")
        with open(shim, "w", encoding="utf-8") as f:
            f.write("#!/usr/bin/env bash\n" "python3 \"%s\" \"$@\"\n" % mock_path)
        os.chmod(shim, 0o755)

        # Ensure our plugin finds the shim
        os.environ["ACLI_BIN"] = shim

        # Invoke llm with the rovodev model, passing the fragment file and extra instruction
        args = [
            "-m",
            "rovodev",
            "-f",
            os.path.basename(frag_path),
            "explain in 1 sentence",
        ]
        result = runner.invoke(cli, args)
        assert result.exit_code == 0, result.output

        output = result.output.strip()

        # Output should only be the parsed Response block, so it should NOT contain banners or model line
        assert "Using model:" not in output
        assert "Announcing GPT-5" not in output

        # It SHOULD contain our response box inner content
        assert "Fragment + extra instruction handled correctly" in output
        assert "Output should not include model/banners" in output
