test_llms_txt_detector.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import pytest
  2. from unittest.mock import patch, Mock
  3. from skill_seekers.cli.llms_txt_detector import LlmsTxtDetector
  4. def test_detect_llms_txt_variants():
  5. """Test detection of llms.txt file variants"""
  6. detector = LlmsTxtDetector("https://hono.dev/docs")
  7. with patch('skill_seekers.cli.llms_txt_detector.requests.head') as mock_head:
  8. mock_response = Mock()
  9. mock_response.status_code = 200
  10. mock_head.return_value = mock_response
  11. variants = detector.detect()
  12. assert variants is not None
  13. assert variants['url'] == 'https://hono.dev/llms-full.txt'
  14. assert variants['variant'] == 'full'
  15. mock_head.assert_called()
  16. def test_detect_no_llms_txt():
  17. """Test detection when no llms.txt file exists"""
  18. detector = LlmsTxtDetector("https://example.com/docs")
  19. with patch('skill_seekers.cli.llms_txt_detector.requests.head') as mock_head:
  20. mock_response = Mock()
  21. mock_response.status_code = 404
  22. mock_head.return_value = mock_response
  23. variants = detector.detect()
  24. assert variants is None
  25. assert mock_head.call_count == 3 # Should try all three variants
  26. def test_url_parsing_with_complex_paths():
  27. """Test URL parsing handles non-standard paths correctly"""
  28. detector = LlmsTxtDetector("https://example.com/docs/v2/guide")
  29. with patch('skill_seekers.cli.llms_txt_detector.requests.head') as mock_head:
  30. mock_response = Mock()
  31. mock_response.status_code = 200
  32. mock_head.return_value = mock_response
  33. variants = detector.detect()
  34. assert variants is not None
  35. assert variants['url'] == 'https://example.com/llms-full.txt'
  36. mock_head.assert_called_with(
  37. 'https://example.com/llms-full.txt',
  38. timeout=5,
  39. allow_redirects=True
  40. )
  41. def test_detect_all_variants():
  42. """Test detecting all llms.txt variants"""
  43. detector = LlmsTxtDetector("https://hono.dev/docs")
  44. with patch('skill_seekers.cli.llms_txt_detector.requests.head') as mock_head:
  45. # Mock responses for different variants
  46. def mock_response(url, **kwargs):
  47. response = Mock()
  48. # All 3 variants exist for Hono
  49. if 'llms-full.txt' in url or 'llms.txt' in url or 'llms-small.txt' in url:
  50. response.status_code = 200
  51. else:
  52. response.status_code = 404
  53. return response
  54. mock_head.side_effect = mock_response
  55. variants = detector.detect_all()
  56. assert len(variants) == 3
  57. assert any(v['variant'] == 'full' for v in variants)
  58. assert any(v['variant'] == 'standard' for v in variants)
  59. assert any(v['variant'] == 'small' for v in variants)
  60. assert all('url' in v for v in variants)