|
| 1 | +import pytest |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +from sam_faces.cli import main |
| 4 | +import sys |
| 5 | + |
| 6 | + |
| 7 | +def test_cli_help(capsys): |
| 8 | + """Test that CLI prints help when no args given.""" |
| 9 | + with pytest.raises(SystemExit) as exc: |
| 10 | + with patch.object(sys, 'argv', ['sam-faces']): |
| 11 | + main() |
| 12 | + assert exc.value.code == 1 |
| 13 | + captured = capsys.readouterr() |
| 14 | + assert "usage" in captured.out.lower() |
| 15 | + |
| 16 | + |
| 17 | +def test_cli_identify(): |
| 18 | + """Test identify command.""" |
| 19 | + mock_result = { |
| 20 | + "face_count": 1, |
| 21 | + "faces": [{"name": "Jane", "confidence": 0.9}], |
| 22 | + "llm_context": "1 face detected: Jane" |
| 23 | + } |
| 24 | + |
| 25 | + with patch("sam_faces.cli.identify", return_value=mock_result) as mock_identify, \ |
| 26 | + patch.object(sys, 'argv', ['sam-faces', 'identify', 'photo.jpg']): |
| 27 | + main() |
| 28 | + mock_identify.assert_called_once_with("photo.jpg", threshold=0.55, save_unknowns=True, save_crops=True) |
| 29 | + |
| 30 | + |
| 31 | +def test_cli_enroll(): |
| 32 | + """Test enroll command.""" |
| 33 | + mock_result = { |
| 34 | + "encoding_id": "abc123", |
| 35 | + "person_id": "person456", |
| 36 | + "person_name": "Jane Smith", |
| 37 | + "crop_path": "/tmp/crop.jpg", |
| 38 | + "note": "test" |
| 39 | + } |
| 40 | + |
| 41 | + with patch("sam_faces.cli.enroll", return_value=mock_result) as mock_enroll, \ |
| 42 | + patch.object(sys, 'argv', ['sam-faces', 'enroll', '--name', 'Jane Smith', '--photo', 'photo.jpg']): |
| 43 | + main() |
| 44 | + mock_enroll.assert_called_once_with("Jane Smith", "photo.jpg", "", None) |
| 45 | + |
| 46 | + |
| 47 | +def test_cli_enroll_with_note_and_index(): |
| 48 | + """Test enroll command with optional args.""" |
| 49 | + with patch("sam_faces.cli.enroll", return_value={}) as mock_enroll, \ |
| 50 | + patch.object(sys, 'argv', ['sam-faces', 'enroll', '--name', 'Jane', '--photo', 'p.jpg', '--note', 'birthday', '--face-index', '1']): |
| 51 | + main() |
| 52 | + mock_enroll.assert_called_once_with("Jane", "p.jpg", "birthday", 1) |
| 53 | + |
| 54 | + |
| 55 | +def test_cli_legacy_photo_flag(): |
| 56 | + """Test backward compatibility with --photo flag.""" |
| 57 | + mock_result = { |
| 58 | + "face_count": 2, |
| 59 | + "faces": [], |
| 60 | + "llm_context": "test" |
| 61 | + } |
| 62 | + |
| 63 | + with patch("sam_faces.cli.identify", return_value=mock_result) as mock_identify, \ |
| 64 | + patch.object(sys, 'argv', ['sam-faces', '--photo', 'photo.jpg']): |
| 65 | + main() |
| 66 | + mock_identify.assert_called_once_with("photo.jpg", threshold=0.55, save_unknowns=True, save_crops=True) |
| 67 | + |
| 68 | + |
| 69 | +def test_cli_list(capsys): |
| 70 | + """Test list command.""" |
| 71 | + with patch("sam_faces.cli.list_people", return_value=[{"name": "Alice", "encoding_count": 2, "id": "abc"}]), \ |
| 72 | + patch("sam_faces.cli.init_db"), \ |
| 73 | + patch.object(sys, 'argv', ['sam-faces', 'list']): |
| 74 | + main() |
| 75 | + captured = capsys.readouterr() |
| 76 | + assert "Alice" in captured.out |
| 77 | + |
| 78 | + |
| 79 | +def test_cli_unknowns(capsys): |
| 80 | + """Test unknowns command.""" |
| 81 | + with patch("sam_faces.cli.list_unknowns", return_value=[{"id": "xyz", "detected_at": "2026-04-26", "image_path": "/tmp/p.jpg"}]), \ |
| 82 | + patch("sam_faces.cli.init_db"), \ |
| 83 | + patch.object(sys, 'argv', ['sam-faces', 'unknowns']): |
| 84 | + main() |
| 85 | + captured = capsys.readouterr() |
| 86 | + assert "xyz" in captured.out |
0 commit comments