|
| 1 | +from unittest.mock import patch, mock_open |
| 2 | +import pytest |
| 3 | + |
| 4 | +from ai_feedback.__main__ import _load_content_with_fallback |
| 5 | + |
| 6 | + |
| 7 | +class TestLoadContentWithFallback: |
| 8 | + """Test cases for _load_content_with_fallback function - the core logic we rely on.""" |
| 9 | + |
| 10 | + def test_load_content_predefined_name_success(self): |
| 11 | + """Test loading content using a predefined name.""" |
| 12 | + content = "Test prompt content" |
| 13 | + predefined_values = ["test_prompt", "another_prompt"] |
| 14 | + |
| 15 | + with patch("builtins.open", mock_open(read_data=content)): |
| 16 | + with patch("os.path.join") as mock_join: |
| 17 | + mock_join.return_value = "/fake/path/user/test_prompt.md" |
| 18 | + |
| 19 | + result = _load_content_with_fallback( |
| 20 | + "test_prompt", |
| 21 | + predefined_values, |
| 22 | + "user", |
| 23 | + "prompt" |
| 24 | + ) |
| 25 | + |
| 26 | + assert result == content |
| 27 | + mock_join.assert_called_once() |
| 28 | + |
| 29 | + def test_load_content_custom_file_path_success(self): |
| 30 | + """Test loading content using a custom file path.""" |
| 31 | + content = "Custom file content" |
| 32 | + predefined_values = ["predefined_prompt"] |
| 33 | + |
| 34 | + with patch("builtins.open", mock_open(read_data=content)): |
| 35 | + result = _load_content_with_fallback( |
| 36 | + "/custom/path/file.md", |
| 37 | + predefined_values, |
| 38 | + "user", |
| 39 | + "prompt" |
| 40 | + ) |
| 41 | + |
| 42 | + assert result == content |
| 43 | + |
| 44 | + def test_load_content_predefined_name_not_found(self, capsys): |
| 45 | + """Test error when predefined file is not found.""" |
| 46 | + predefined_values = ["test_prompt"] |
| 47 | + |
| 48 | + with patch("builtins.open", side_effect=FileNotFoundError): |
| 49 | + with pytest.raises(SystemExit) as exc_info: |
| 50 | + _load_content_with_fallback( |
| 51 | + "test_prompt", |
| 52 | + predefined_values, |
| 53 | + "user", |
| 54 | + "prompt" |
| 55 | + ) |
| 56 | + |
| 57 | + assert exc_info.value.code == 1 |
| 58 | + captured = capsys.readouterr() |
| 59 | + assert "Pre-defined prompt file 'test_prompt.md' not found in user subfolder." in captured.out |
| 60 | + |
| 61 | + def test_load_content_custom_file_path_not_found(self, capsys): |
| 62 | + """Test error when custom file is not found.""" |
| 63 | + predefined_values = ["predefined_prompt"] |
| 64 | + |
| 65 | + with patch("builtins.open", side_effect=FileNotFoundError): |
| 66 | + with pytest.raises(SystemExit) as exc_info: |
| 67 | + _load_content_with_fallback( |
| 68 | + "/nonexistent/file.md", |
| 69 | + predefined_values, |
| 70 | + "user", |
| 71 | + "prompt" |
| 72 | + ) |
| 73 | + |
| 74 | + assert exc_info.value.code == 1 |
| 75 | + captured = capsys.readouterr() |
| 76 | + assert "Prompt file '/nonexistent/file.md' not found." in captured.out |
0 commit comments