|
| 1 | +"""Tests for kalibr init context file writing (CLAUDE.md and .cursorrules).""" |
| 2 | + |
| 3 | +import os |
| 4 | +import tempfile |
| 5 | + |
| 6 | +import pytest |
| 7 | +from typer.testing import CliRunner |
| 8 | + |
| 9 | +from kalibr.cli.main import app |
| 10 | +from kalibr.cli.init_cmd import _write_context_files |
| 11 | + |
| 12 | + |
| 13 | +class TestWriteContextFiles: |
| 14 | + """Unit tests for _write_context_files helper.""" |
| 15 | + |
| 16 | + def test_creates_claude_md_when_missing(self, tmp_path): |
| 17 | + """Creates CLAUDE.md in target dir when it doesn't exist.""" |
| 18 | + _write_context_files(str(tmp_path)) |
| 19 | + claude_file = tmp_path / "CLAUDE.md" |
| 20 | + assert claude_file.exists(), "CLAUDE.md should be created" |
| 21 | + content = claude_file.read_text() |
| 22 | + assert "Kalibr" in content |
| 23 | + assert "Router" in content |
| 24 | + |
| 25 | + def test_creates_cursorrules_when_missing(self, tmp_path): |
| 26 | + """Creates .cursorrules in target dir when it doesn't exist.""" |
| 27 | + _write_context_files(str(tmp_path)) |
| 28 | + cursorrules_file = tmp_path / ".cursorrules" |
| 29 | + assert cursorrules_file.exists(), ".cursorrules should be created" |
| 30 | + content = cursorrules_file.read_text() |
| 31 | + assert "Kalibr" in content |
| 32 | + |
| 33 | + def test_does_not_overwrite_existing_claude_md(self, tmp_path): |
| 34 | + """Does NOT overwrite CLAUDE.md if it already exists.""" |
| 35 | + claude_file = tmp_path / "CLAUDE.md" |
| 36 | + original_content = "# My custom CLAUDE.md\nDo not overwrite me." |
| 37 | + claude_file.write_text(original_content) |
| 38 | + |
| 39 | + _write_context_files(str(tmp_path)) |
| 40 | + |
| 41 | + assert claude_file.read_text() == original_content, "CLAUDE.md should not be overwritten" |
| 42 | + |
| 43 | + def test_does_not_overwrite_existing_cursorrules(self, tmp_path): |
| 44 | + """Does NOT overwrite .cursorrules if it already exists.""" |
| 45 | + cursorrules_file = tmp_path / ".cursorrules" |
| 46 | + original_content = "# My custom rules\nDo not overwrite me." |
| 47 | + cursorrules_file.write_text(original_content) |
| 48 | + |
| 49 | + _write_context_files(str(tmp_path)) |
| 50 | + |
| 51 | + assert cursorrules_file.read_text() == original_content, ".cursorrules should not be overwritten" |
| 52 | + |
| 53 | + def test_creates_both_files(self, tmp_path): |
| 54 | + """Creates both CLAUDE.md and .cursorrules when neither exists.""" |
| 55 | + _write_context_files(str(tmp_path)) |
| 56 | + assert (tmp_path / "CLAUDE.md").exists() |
| 57 | + assert (tmp_path / ".cursorrules").exists() |
| 58 | + |
| 59 | + def test_skips_gracefully_when_both_exist(self, tmp_path): |
| 60 | + """No error when both files already exist.""" |
| 61 | + (tmp_path / "CLAUDE.md").write_text("existing claude") |
| 62 | + (tmp_path / ".cursorrules").write_text("existing cursor") |
| 63 | + |
| 64 | + # Should not raise |
| 65 | + _write_context_files(str(tmp_path)) |
| 66 | + |
| 67 | + assert (tmp_path / "CLAUDE.md").read_text() == "existing claude" |
| 68 | + assert (tmp_path / ".cursorrules").read_text() == "existing cursor" |
| 69 | + |
| 70 | + |
| 71 | +class TestInitCommandContextFiles: |
| 72 | + """Integration tests: kalibr init CLI writes context files.""" |
| 73 | + |
| 74 | + def test_init_creates_context_files_in_empty_dir(self, tmp_path): |
| 75 | + """kalibr init on an empty dir creates CLAUDE.md and .cursorrules.""" |
| 76 | + runner = CliRunner() |
| 77 | + result = runner.invoke(app, ["init", str(tmp_path)]) |
| 78 | + |
| 79 | + assert (tmp_path / "CLAUDE.md").exists(), "CLAUDE.md should be created by kalibr init" |
| 80 | + assert (tmp_path / ".cursorrules").exists(), ".cursorrules should be created by kalibr init" |
| 81 | + |
| 82 | + def test_init_reports_created_files(self, tmp_path): |
| 83 | + """kalibr init prints confirmation messages for created files.""" |
| 84 | + runner = CliRunner() |
| 85 | + result = runner.invoke(app, ["init", str(tmp_path)]) |
| 86 | + |
| 87 | + assert "Created CLAUDE.md" in result.output |
| 88 | + assert "Created .cursorrules" in result.output |
| 89 | + |
| 90 | + def test_init_reports_skipped_when_files_exist(self, tmp_path): |
| 91 | + """kalibr init prints skip messages when context files already exist.""" |
| 92 | + (tmp_path / "CLAUDE.md").write_text("existing") |
| 93 | + (tmp_path / ".cursorrules").write_text("existing") |
| 94 | + |
| 95 | + runner = CliRunner() |
| 96 | + result = runner.invoke(app, ["init", str(tmp_path)]) |
| 97 | + |
| 98 | + assert "already exists, skipping" in result.output |
| 99 | + |
| 100 | + def test_init_does_not_overwrite_existing_context_files(self, tmp_path): |
| 101 | + """kalibr init never overwrites existing CLAUDE.md or .cursorrules.""" |
| 102 | + (tmp_path / "CLAUDE.md").write_text("my custom claude") |
| 103 | + (tmp_path / ".cursorrules").write_text("my custom rules") |
| 104 | + |
| 105 | + runner = CliRunner() |
| 106 | + runner.invoke(app, ["init", str(tmp_path)]) |
| 107 | + |
| 108 | + assert (tmp_path / "CLAUDE.md").read_text() == "my custom claude" |
| 109 | + assert (tmp_path / ".cursorrules").read_text() == "my custom rules" |
0 commit comments