File size: 8,881 Bytes
979853c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/bin/bash

# LightRAG aquery_data endpoint test script
# Use curl command to test the new /query/data endpoint and validate the new data format

echo "🚀 LightRAG aquery_data Endpoint Test (New Data Format Validation)"
echo "=================================================="

# Base URL (adjust according to actual deployment)
BASE_URL="http://localhost:9621"

# Color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Test result statistics
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0

# Function to validate success response format
validate_success_response() {
    local response="$1"
    local test_name="$2"
    local expected_mode="$3"

    echo -e "${BLUE}Validating $test_name response format...${NC}"

    # Check if valid JSON
    if ! echo "$response" | jq . >/dev/null 2>&1; then
        echo -e "${RED}❌ Response is not valid JSON format${NC}"
        return 1
    fi

    # Validate required fields
    local status=$(echo "$response" | jq -r '.status // "missing"')
    local message=$(echo "$response" | jq -r '.message // "missing"')
    local data_exists=$(echo "$response" | jq 'has("data")')
    local metadata_exists=$(echo "$response" | jq 'has("metadata")')

    echo "  Status: $status"
    echo "  Message: $message"

    # Validate data structure
    if [[ "$data_exists" == "true" ]]; then
        local entities_count=$(echo "$response" | jq '.data.entities | length // 0')
        local relationships_count=$(echo "$response" | jq '.data.relationships | length // 0')
        local chunks_count=$(echo "$response" | jq '.data.chunks | length // 0')
        local references_count=$(echo "$response" | jq '.data.references | length // 0')

        echo "  Data.entities: $entities_count"
        echo "  Data.relationships: $relationships_count"
        echo "  Data.chunks: $chunks_count"
        echo "  Data.references: $references_count"
    else
        echo -e "${RED}  ❌ Missing 'data' field${NC}"
        return 1
    fi

    # Validate metadata
    if [[ "$metadata_exists" == "true" ]]; then
        local query_mode=$(echo "$response" | jq -r '.metadata.query_mode // "missing"')
        local keywords_exists=$(echo "$response" | jq 'has("metadata") and (.metadata | has("keywords"))')
        local processing_info_exists=$(echo "$response" | jq 'has("metadata") and (.metadata | has("processing_info"))')

        echo "  Metadata.query_mode: $query_mode"
        echo "  Metadata.keywords: $keywords_exists"
        echo "  Metadata.processing_info: $processing_info_exists"

        # Validate if query mode matches
        if [[ "$expected_mode" != "" && "$query_mode" != "$expected_mode" ]]; then
            echo -e "${YELLOW}  ⚠️  Query mode mismatch: expected '$expected_mode', actual '$query_mode'${NC}"
        fi
    else
        echo -e "${RED}  ❌ Missing 'metadata' field${NC}"
        return 1
    fi

    # Validate status
    if [[ "$status" == "success" ]]; then
        echo -e "${GREEN}  ✅ Response format validation passed${NC}"
        return 0
    else
        echo -e "${RED}  ❌ Status is not 'success': $status${NC}"
        return 1
    fi
}

# Function to validate error response format
validate_error_response() {
    local response="$1"
    local test_name="$2"

    echo -e "${BLUE}Validating $test_name response format...${NC}"

    # Check if valid JSON
    if ! echo "$response" | jq . >/dev/null 2>&1; then
        echo -e "${RED}❌ Response is not valid JSON format${NC}"
        return 1
    fi

    # Validate required fields
    local status=$(echo "$response" | jq -r '.status // "missing"')
    local message=$(echo "$response" | jq -r '.message // "missing"')
    local data_exists=$(echo "$response" | jq 'has("data")')
    local metadata_exists=$(echo "$response" | jq 'has("metadata")')

    echo "  Status: $status"
    echo "  Message: $message"

    # Validate basic structure exists
    if [[ "$data_exists" != "true" ]]; then
        echo -e "${RED}  ❌ Missing 'data' field${NC}"
        return 1
    fi

    if [[ "$metadata_exists" != "true" ]]; then
        echo -e "${RED}  ❌ Missing 'metadata' field${NC}"
        return 1
    fi

    echo "  Data: {}"
    echo "  Metadata: {}"

    # Validate status should be failure
    if [[ "$status" == "failure" ]]; then
        echo -e "${GREEN}  ✅ Error response format validation passed${NC}"
        return 0
    else
        echo -e "${RED}  ❌ Status is not 'failure': $status${NC}"
        return 1
    fi
}

# Function to run success test
run_success_test() {
    local test_name="$1"
    local query_data="$2"
    local expected_mode="$3"
    local print_json="${4:-false}"  # Optional parameter: whether to print JSON response (default: false)

    echo ""
    echo "=================================="
    echo -e "${BLUE}$test_name${NC}"
    echo "=================================="

    TOTAL_TESTS=$((TOTAL_TESTS + 1))

    # Send request
    echo "Sending request..."
    local response=$(curl -s -X POST "${BASE_URL}/query/data" \
      -H "Content-Type: application/json" \
      -H "X-API-Key: your-secure-api-key-here-123" \
      -d "$query_data")

    # Check if curl succeeded
    if [[ $? -ne 0 ]]; then
        echo -e "${RED}❌ Request failed - cannot connect to server${NC}"
        FAILED_TESTS=$((FAILED_TESTS + 1))
        return 1
    fi

    # Print JSON response if requested
    if [[ "$print_json" == "true" ]]; then
        echo ""
        echo "Response JSON:"
        echo "$response" | jq '.' 2>/dev/null || echo "$response"
        echo ""
    fi

    # Validate response
    if validate_success_response "$response" "$test_name" "$expected_mode"; then
        PASSED_TESTS=$((PASSED_TESTS + 1))
        echo -e "${GREEN}$test_name test passed${NC}"
    else
        FAILED_TESTS=$((FAILED_TESTS + 1))
        echo -e "${RED}$test_name test failed${NC}"
        echo "Raw response:"
        echo "$response" | jq '.' 2>/dev/null || echo "$response"
    fi
}

# Function to run error test
run_error_test() {
    local test_name="$1"
    local query_data="$2"

    echo ""
    echo "=================================="
    echo -e "${BLUE}$test_name${NC}"
    echo "=================================="

    TOTAL_TESTS=$((TOTAL_TESTS + 1))

    # Send request
    echo "Sending request..."
    local response=$(curl -s -X POST "${BASE_URL}/query/data" \
      -H "Content-Type: application/json" \
      -H "X-API-Key: your-secure-api-key-here-123" \
      -d "$query_data")

    # Check if curl succeeded
    if [[ $? -ne 0 ]]; then
        echo -e "${RED}❌ Request failed - cannot connect to server${NC}"
        FAILED_TESTS=$((FAILED_TESTS + 1))
        return 1
    fi

    # Validate response
    if validate_error_response "$response" "$test_name"; then
        PASSED_TESTS=$((PASSED_TESTS + 1))
        echo -e "${GREEN}$test_name test passed${NC}"
    else
        FAILED_TESTS=$((FAILED_TESTS + 1))
        echo -e "${RED}$test_name test failed${NC}"
        echo "Raw response:"
        echo "$response" | jq '.' 2>/dev/null || echo "$response"
    fi
}

# Start tests
echo "Starting tests for new /query/data endpoint data format..."
echo ""

# Test 1: Basic query test (mix mode)
run_success_test "1. Basic Query Test (mix mode)" '{
    "query": "What is GraphRAG",
    "mode": "mix",
    "top_k": 5
}' "mix" "true" # Output full JSON

# Test 2: Detailed parameter query test (hybrid mode)
run_success_test "2. Detailed Parameter Query Test (hybrid mode)" '{
    "query": "What is GraphRAG",
    "mode": "hybrid",
    "top_k": 5,
    "chunk_top_k": 8,
    "max_entity_tokens": 4000,
    "max_relation_tokens": 4000,
    "max_total_tokens": 16000,
    "enable_rerank": true,
    "response_type": "Multiple Paragraphs"
}' "hybrid"

# Output test result statistics
echo ""
echo "=================================================="
echo -e "${BLUE}Test Result Statistics${NC}"
echo "=================================================="
echo -e "Total tests: ${BLUE}$TOTAL_TESTS${NC}"
echo -e "Passed tests: ${GREEN}$PASSED_TESTS${NC}"
echo -e "Failed tests: ${RED}$FAILED_TESTS${NC}"

if [[ $FAILED_TESTS -eq 0 ]]; then
    echo -e "${GREEN}🎉 All tests passed! New data format adaptation successful!${NC}"
    exit 0
else
    echo -e "${RED}⚠️  $FAILED_TESTS test(s) failed, please check the issues${NC}"
    exit 1
fi

echo ""
echo "💡 Usage Instructions:"
echo "1. Ensure LightRAG API service is running (python -m lightrag.api.lightrag_server)"
echo "2. Adjust BASE_URL as needed"
echo "3. If authentication is required, add -H \"Authorization: Bearer your-token\""
echo "4. Install jq for better JSON formatting output: brew install jq (macOS) or apt install jq (Ubuntu)"
echo "5. Script will automatically validate new data format structure: status, message, data, metadata"