test_gemini_file_api.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/bin/bash
  2. # Test script for Gemini File API endpoints
  3. # This script tests all four file operations: upload, get, list, and delete
  4. set -e
  5. # Configuration
  6. BASE_URL="${BASE_URL:-http://localhost:3000}"
  7. API_TOKEN="${API_TOKEN:-your-api-token-here}"
  8. # Colors for output
  9. GREEN='\033[0;32m'
  10. RED='\033[0;31m'
  11. YELLOW='\033[1;33m'
  12. NC='\033[0m' # No Color
  13. # Helper functions
  14. print_success() {
  15. echo -e "${GREEN}✓ $1${NC}"
  16. }
  17. print_error() {
  18. echo -e "${RED}✗ $1${NC}"
  19. }
  20. print_info() {
  21. echo -e "${YELLOW}ℹ $1${NC}"
  22. }
  23. # Check if API token is set
  24. if [ "$API_TOKEN" = "your-api-token-here" ]; then
  25. print_error "Please set API_TOKEN environment variable"
  26. echo "Usage: API_TOKEN=your-token BASE_URL=http://localhost:3000 ./test_gemini_file_api.sh"
  27. exit 1
  28. fi
  29. echo "=========================================="
  30. echo "Testing Gemini File API Endpoints"
  31. echo "=========================================="
  32. echo ""
  33. # Create a test file
  34. TEST_FILE="/tmp/test_gemini_upload.txt"
  35. echo "This is a test file for Gemini File API upload" > "$TEST_FILE"
  36. print_info "Created test file: $TEST_FILE"
  37. echo ""
  38. # Test 1: Upload File
  39. echo "Test 1: Upload File"
  40. echo "--------------------"
  41. print_info "Uploading file to $BASE_URL/upload/v1beta/files"
  42. UPLOAD_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/upload/v1beta/files" \
  43. -H "Authorization: Bearer $API_TOKEN" \
  44. -F "file=@$TEST_FILE" \
  45. -F "display_name=Test Document")
  46. HTTP_CODE=$(echo "$UPLOAD_RESPONSE" | tail -n1)
  47. RESPONSE_BODY=$(echo "$UPLOAD_RESPONSE" | sed '$d')
  48. if [ "$HTTP_CODE" = "200" ]; then
  49. print_success "File uploaded successfully (HTTP $HTTP_CODE)"
  50. Response: $RESPONSE_BODY"
  51. # Extract file name from response
  52. FILE_NAME=$(echo "$RESPONSE_BODY" | grep -o '"name":"[^"]*"' | cut -d'"' -f4)
  53. if [ -n "$FILE_NAME" ]; then
  54. print_info "File name: $FILE_NAME"
  55. fi
  56. else
  57. print_error "File upload failed (HTTP $HTTP_CODE)"
  58. echo "Response: $RESPONSE_BODY"
  59. exit 1
  60. fi
  61. echo ""
  62. # Test 2: List Files
  63. echo "Test 2: List Files"
  64. echo "------------------"
  65. print_info "Listing files from $BASE_URL/v1beta/files"
  66. LIST_RESPONSE=$(curl -s -w "\n%{http_code}" -X GET "$BASE_URL/v1beta/files" \
  67. -H "Authorization: Bearer $API_TOKEN")
  68. HTTP_CODE=$(echo "$LIST_RESPONSE" | tail -n1)
  69. RESPONSE_BODY=$(echo "$LIST_RESPONSE" | sed '$d')
  70. if [ "$HTTP_CODE" = "200" ]; then
  71. print_success "Files listed successfully (HTTP $HTTP_CODE)"
  72. echo "Response: $RESPONSE_BODY"
  73. else
  74. print_error "File listing failed (HTTP $HTTP_CODE)"
  75. echo "Response: $RESPONSE_BODY"
  76. fi
  77. echo ""
  78. # Test 3: Get File Metadata (if we have a file name)
  79. if [ -n "$FILE_NAME" ]; then
  80. echo "Test 3: Get File Metadata"
  81. echo "-------------------------"
  82. print_info "Getting metadata for $FILE_NAME"
  83. GET_RESPONSE=$(curl -s -w "\n%{http_code}" -X GET "$BASE_URL/v1beta/$FILE_NAME" \
  84. -H "Authorization: Bearer $API_TOKEN")
  85. HTTP_CODE=$(echo "$GET_RESPONSE" | tail -n1)
  86. RESPONSE_BODY=$(echo "$GET_RESPONSE" | sed '$d')
  87. if [ "$HTTP_CODE" = "200" ]; then
  88. print_success "File metadata retrieved successfully (HTTP $HTTP_CODE)"
  89. echo "Response: $RESPONSE_BODY"
  90. else
  91. print_error "File metadata retrieval failed (HTTP $HTTP_CODE)"
  92. echo "Response: $RESPONSE_BODY"
  93. fi
  94. echo ""
  95. # Test 4: Delete File
  96. echo "Test 4: Delete File"
  97. echo "-------------------"
  98. print_info "Deleting file $FILE_NAME"
  99. DELETE_RESPONSE=$(curl -s -w "\n%{http_code}" -X DELETE "$BASE_URL/v1beta/$FILE_NAME" \
  100. -H "Authorization: Bearer $API_TOKEN")
  101. HTTP_CODE=$(echo "$DELETE_RESPONSE" | tail -n1)
  102. RESPONSE_BODY=$(echo "$DELETE_RESPONSE" | sed '$d')
  103. if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "204" ]; then
  104. print_success "File deleted successfully (HTTP $HTTP_CODE)"
  105. echo "Response: $RESPONSE_BODY"
  106. else
  107. print_error "File deletion failed (HTTP $HTTP_CODE)"
  108. echo "Response: $RESPONSE_BODY"
  109. fi
  110. echo ""
  111. fi
  112. # Cleanup
  113. rm -f "$TEST_FILE"
  114. print_info "Cleaned up test file"
  115. echo ""
  116. echo "=========================================="
  117. echo "Test Summary"
  118. echo "=========================================="
  119. print_success "All tests completed!"
  120. echo ""
  121. echo "Note: If you see authentication errors, make sure:"
  122. echo " 1. The server is running"
  123. echo " 2. Your API token is valid"
  124. echo " 3. The token has access to a Gemini channel"
  125. echo " 4. The Gemini API key is properly configured in the channel"