#!/bin/bash # Test script for Gemini File API endpoints # This script tests all four file operations: upload, get, list, and delete set -e # Configuration BASE_URL="${BASE_URL:-http://localhost:3000}" API_TOKEN="${API_TOKEN:-your-api-token-here}" # Colors for output GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Helper functions print_success() { echo -e "${GREEN}✓ $1${NC}" } print_error() { echo -e "${RED}✗ $1${NC}" } print_info() { echo -e "${YELLOW}ℹ $1${NC}" } # Check if API token is set if [ "$API_TOKEN" = "your-api-token-here" ]; then print_error "Please set API_TOKEN environment variable" echo "Usage: API_TOKEN=your-token BASE_URL=http://localhost:3000 ./test_gemini_file_api.sh" exit 1 fi echo "==========================================" echo "Testing Gemini File API Endpoints" echo "==========================================" echo "" # Create a test file TEST_FILE="/tmp/test_gemini_upload.txt" echo "This is a test file for Gemini File API upload" > "$TEST_FILE" print_info "Created test file: $TEST_FILE" echo "" # Test 1: Upload File echo "Test 1: Upload File" echo "--------------------" print_info "Uploading file to $BASE_URL/upload/v1beta/files" UPLOAD_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/upload/v1beta/files" \ -H "Authorization: Bearer $API_TOKEN" \ -F "file=@$TEST_FILE" \ -F "display_name=Test Document") HTTP_CODE=$(echo "$UPLOAD_RESPONSE" | tail -n1) RESPONSE_BODY=$(echo "$UPLOAD_RESPONSE" | sed '$d') if [ "$HTTP_CODE" = "200" ]; then print_success "File uploaded successfully (HTTP $HTTP_CODE)" Response: $RESPONSE_BODY" # Extract file name from response FILE_NAME=$(echo "$RESPONSE_BODY" | grep -o '"name":"[^"]*"' | cut -d'"' -f4) if [ -n "$FILE_NAME" ]; then print_info "File name: $FILE_NAME" fi else print_error "File upload failed (HTTP $HTTP_CODE)" echo "Response: $RESPONSE_BODY" exit 1 fi echo "" # Test 2: List Files echo "Test 2: List Files" echo "------------------" print_info "Listing files from $BASE_URL/v1beta/files" LIST_RESPONSE=$(curl -s -w "\n%{http_code}" -X GET "$BASE_URL/v1beta/files" \ -H "Authorization: Bearer $API_TOKEN") HTTP_CODE=$(echo "$LIST_RESPONSE" | tail -n1) RESPONSE_BODY=$(echo "$LIST_RESPONSE" | sed '$d') if [ "$HTTP_CODE" = "200" ]; then print_success "Files listed successfully (HTTP $HTTP_CODE)" echo "Response: $RESPONSE_BODY" else print_error "File listing failed (HTTP $HTTP_CODE)" echo "Response: $RESPONSE_BODY" fi echo "" # Test 3: Get File Metadata (if we have a file name) if [ -n "$FILE_NAME" ]; then echo "Test 3: Get File Metadata" echo "-------------------------" print_info "Getting metadata for $FILE_NAME" GET_RESPONSE=$(curl -s -w "\n%{http_code}" -X GET "$BASE_URL/v1beta/$FILE_NAME" \ -H "Authorization: Bearer $API_TOKEN") HTTP_CODE=$(echo "$GET_RESPONSE" | tail -n1) RESPONSE_BODY=$(echo "$GET_RESPONSE" | sed '$d') if [ "$HTTP_CODE" = "200" ]; then print_success "File metadata retrieved successfully (HTTP $HTTP_CODE)" echo "Response: $RESPONSE_BODY" else print_error "File metadata retrieval failed (HTTP $HTTP_CODE)" echo "Response: $RESPONSE_BODY" fi echo "" # Test 4: Delete File echo "Test 4: Delete File" echo "-------------------" print_info "Deleting file $FILE_NAME" DELETE_RESPONSE=$(curl -s -w "\n%{http_code}" -X DELETE "$BASE_URL/v1beta/$FILE_NAME" \ -H "Authorization: Bearer $API_TOKEN") HTTP_CODE=$(echo "$DELETE_RESPONSE" | tail -n1) RESPONSE_BODY=$(echo "$DELETE_RESPONSE" | sed '$d') if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "204" ]; then print_success "File deleted successfully (HTTP $HTTP_CODE)" echo "Response: $RESPONSE_BODY" else print_error "File deletion failed (HTTP $HTTP_CODE)" echo "Response: $RESPONSE_BODY" fi echo "" fi # Cleanup rm -f "$TEST_FILE" print_info "Cleaned up test file" echo "" echo "==========================================" echo "Test Summary" echo "==========================================" print_success "All tests completed!" echo "" echo "Note: If you see authentication errors, make sure:" echo " 1. The server is running" echo " 2. Your API token is valid" echo " 3. The token has access to a Gemini channel" echo " 4. The Gemini API key is properly configured in the channel"