test_upload_skill.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env python3
  2. """
  3. Tests for cli/upload_skill.py functionality
  4. """
  5. import unittest
  6. import tempfile
  7. import zipfile
  8. import os
  9. from pathlib import Path
  10. import sys
  11. from skill_seekers.cli.upload_skill import upload_skill_api
  12. class TestUploadSkillAPI(unittest.TestCase):
  13. """Test upload_skill_api function"""
  14. def setUp(self):
  15. """Store original API key state"""
  16. self.original_api_key = os.environ.get('ANTHROPIC_API_KEY')
  17. def tearDown(self):
  18. """Restore original API key state"""
  19. if self.original_api_key:
  20. os.environ['ANTHROPIC_API_KEY'] = self.original_api_key
  21. elif 'ANTHROPIC_API_KEY' in os.environ:
  22. del os.environ['ANTHROPIC_API_KEY']
  23. def create_test_zip(self, tmpdir):
  24. """Helper to create a test .zip file"""
  25. zip_path = Path(tmpdir) / "test-skill.zip"
  26. with zipfile.ZipFile(zip_path, 'w') as zf:
  27. zf.writestr("SKILL.md", "---\nname: test\n---\n# Test Skill")
  28. zf.writestr("references/index.md", "# Index")
  29. return zip_path
  30. def test_upload_without_api_key(self):
  31. """Test that upload fails gracefully without API key"""
  32. # Remove API key
  33. if 'ANTHROPIC_API_KEY' in os.environ:
  34. del os.environ['ANTHROPIC_API_KEY']
  35. with tempfile.TemporaryDirectory() as tmpdir:
  36. zip_path = self.create_test_zip(tmpdir)
  37. success, message = upload_skill_api(zip_path)
  38. self.assertFalse(success)
  39. # Check for api_key (with underscore) in message
  40. self.assertTrue('api_key' in message.lower() or 'api key' in message.lower())
  41. def test_upload_with_nonexistent_file(self):
  42. """Test upload with nonexistent file"""
  43. os.environ['ANTHROPIC_API_KEY'] = 'sk-ant-test-key'
  44. success, message = upload_skill_api("/nonexistent/file.zip")
  45. self.assertFalse(success)
  46. self.assertIn('not found', message.lower())
  47. def test_upload_with_invalid_zip(self):
  48. """Test upload with invalid zip file (not a zip)"""
  49. os.environ['ANTHROPIC_API_KEY'] = 'sk-ant-test-key'
  50. with tempfile.NamedTemporaryFile(suffix='.zip', delete=False) as tmpfile:
  51. tmpfile.write(b"Not a valid zip file")
  52. tmpfile.flush()
  53. try:
  54. success, message = upload_skill_api(tmpfile.name)
  55. # Should either fail validation or detect invalid zip
  56. self.assertFalse(success)
  57. finally:
  58. os.unlink(tmpfile.name)
  59. def test_upload_accepts_path_object(self):
  60. """Test that upload_skill_api accepts Path objects"""
  61. os.environ['ANTHROPIC_API_KEY'] = 'sk-ant-test-key'
  62. with tempfile.TemporaryDirectory() as tmpdir:
  63. zip_path = self.create_test_zip(tmpdir)
  64. # This should not raise TypeError
  65. try:
  66. success, message = upload_skill_api(Path(zip_path))
  67. except TypeError:
  68. self.fail("upload_skill_api should accept Path objects")
  69. class TestUploadSkillCLI(unittest.TestCase):
  70. """Test upload_skill.py command-line interface"""
  71. def test_cli_help_output(self):
  72. """Test that skill-seekers upload --help works"""
  73. import subprocess
  74. try:
  75. result = subprocess.run(
  76. ['skill-seekers', 'upload', '--help'],
  77. capture_output=True,
  78. text=True,
  79. timeout=5
  80. )
  81. # argparse may return 0 or 2 for --help
  82. self.assertIn(result.returncode, [0, 2])
  83. output = result.stdout + result.stderr
  84. self.assertTrue('usage:' in output.lower() or 'upload' in output.lower())
  85. except FileNotFoundError:
  86. self.skipTest("skill-seekers command not installed")
  87. def test_cli_executes_without_errors(self):
  88. """Test that skill-seekers-upload entry point works"""
  89. import subprocess
  90. try:
  91. result = subprocess.run(
  92. ['skill-seekers-upload', '--help'],
  93. capture_output=True,
  94. text=True,
  95. timeout=5
  96. )
  97. # argparse may return 0 or 2 for --help
  98. self.assertIn(result.returncode, [0, 2])
  99. except FileNotFoundError:
  100. self.skipTest("skill-seekers-upload command not installed")
  101. def test_cli_requires_zip_argument(self):
  102. """Test that CLI requires zip file argument"""
  103. import subprocess
  104. result = subprocess.run(
  105. ['python3', 'cli/upload_skill.py'],
  106. capture_output=True,
  107. text=True
  108. )
  109. # Should fail or show usage
  110. self.assertTrue(
  111. result.returncode != 0 or 'usage' in result.stderr.lower() or 'usage' in result.stdout.lower()
  112. )
  113. if __name__ == '__main__':
  114. unittest.main()