import json
from pathlib import Path
from typer.testing import CliRunner
from qrm import cli

runner = CliRunner()


def test_show_skill_prints_skill_md(tmp_path, monkeypatch):
    # Use the real SKILL.md content and skill name
    import qrm.llm_cmd as llm_cmd

    real_skill_md = (llm_cmd._skill_source_dir() / "SKILL.md").read_text(encoding="utf-8")
    result = runner.invoke(cli.app, ["--show-skill"])
    assert result.exit_code == 0
    assert real_skill_md.strip() in result.stdout


def test_install_skill_creates_files(tmp_path, monkeypatch):
    # Patch home to tmp_path for Claude
    import qrm.llm_cmd as llm_cmd

    monkeypatch.setattr(Path, "home", lambda: tmp_path)
    # Patch cwd to tmp_path for Copilot
    monkeypatch.setattr(Path, "cwd", lambda: tmp_path)
    result = runner.invoke(cli.app, ["--install-skill"])
    assert result.exit_code == 0
    skill_name = llm_cmd._SKILL_NAME
    # Check Claude install
    assert (tmp_path / ".claude" / "skills" / skill_name / "SKILL.md").exists()
    # Check Copilot instructions
    assert (tmp_path / ".github" / "instructions" / f"{skill_name}.instructions.md").exists()
    # Check VSCode settings
    vscode_settings = tmp_path / ".vscode" / "settings.json"
    assert vscode_settings.exists()
    data = json.loads(vscode_settings.read_text())
    assert data["github.copilot.chat.codeGeneration.useInstructionFiles"] is True
