package mcp import ( "context" "testing" "time" ) // TestLightRAGClient_NewClient tests client creation func TestLightRAGClient_NewClient(t *testing.T) { config := LightRAGConfig{ BaseURL: "http://127.0.0.1:9621", Timeout: 10 * time.Second, APIKey: "", } client := NewLightRAGClient(config) if client == nil { t.Fatal("expected non-nil client") } if client.baseURL != config.BaseURL { t.Errorf("expected baseURL %s, got %s", config.BaseURL, client.baseURL) } } // TestLightRAGClient_Health tests health check // NOTE: This test requires a running LightRAG server at 127.0.0.1:9621 func TestLightRAGClient_Health(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } config := LightRAGConfig{ BaseURL: "http://127.0.0.1:9621", Timeout: 5 * time.Second, } client := NewLightRAGClient(config) ctx := context.Background() health, err := client.Health(ctx) if err != nil { t.Logf("Health check failed (server may not be running): %v", err) t.Skip("skipping test - lightrag server not available") return } if health.Status != "healthy" { t.Errorf("expected status 'healthy', got '%s'", health.Status) } t.Logf("LightRAG Health: %s", health.Status) t.Logf("Core Version: %s", health.CoreVersion) t.Logf("API Version: %s", health.APIVersion) } // TestLightRAGClient_IsHealthy tests the convenience health check func TestLightRAGClient_IsHealthy(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } config := LightRAGConfig{ BaseURL: "http://127.0.0.1:9621", Timeout: 5 * time.Second, } client := NewLightRAGClient(config) ctx := context.Background() healthy := client.IsHealthy(ctx) if !healthy { t.Log("Server not healthy (may not be running)") t.Skip("skipping test - lightrag server not available") } } // TestLightRAGClient_Query tests querying with different modes func TestLightRAGClient_Query(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } config := LightRAGConfig{ BaseURL: "http://127.0.0.1:9621", Timeout: 30 * time.Second, } client := NewLightRAGClient(config) ctx := context.Background() // First check if server is available if !client.IsHealthy(ctx) { t.Skip("skipping test - lightrag server not available") } testCases := []struct { name string query string mode QueryMode }{ { name: "naive mode", query: "What is CHORUS?", mode: QueryModeNaive, }, { name: "local mode", query: "How does P2P networking work?", mode: QueryModeLocal, }, { name: "global mode", query: "What are the main components?", mode: QueryModeGlobal, }, { name: "hybrid mode", query: "Explain the architecture", mode: QueryModeHybrid, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { response, err := client.Query(ctx, tc.query, tc.mode) if err != nil { t.Logf("Query failed: %v", err) return // Non-fatal - may just have empty knowledge base } if response == nil { t.Error("expected non-nil response") return } t.Logf("Query: %s", tc.query) t.Logf("Mode: %s", tc.mode) t.Logf("Response length: %d chars", len(response.Response)) }) } } // TestLightRAGClient_GetContext tests context retrieval func TestLightRAGClient_GetContext(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } config := LightRAGConfig{ BaseURL: "http://127.0.0.1:9621", Timeout: 30 * time.Second, } client := NewLightRAGClient(config) ctx := context.Background() if !client.IsHealthy(ctx) { t.Skip("skipping test - lightrag server not available") } context, err := client.GetContext(ctx, "distributed systems", QueryModeHybrid) if err != nil { t.Logf("GetContext failed: %v", err) return // Non-fatal } t.Logf("Context length: %d chars", len(context)) } // TestLightRAGClient_Insert tests document insertion func TestLightRAGClient_Insert(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } config := LightRAGConfig{ BaseURL: "http://127.0.0.1:9621", Timeout: 30 * time.Second, } client := NewLightRAGClient(config) ctx := context.Background() if !client.IsHealthy(ctx) { t.Skip("skipping test - lightrag server not available") } text := `CHORUS is a distributed task coordination system built on P2P networking. It uses libp2p for peer-to-peer communication and implements democratic leader election. Tasks are executed in Docker sandboxes for security and isolation.` description := "CHORUS system overview" err := client.Insert(ctx, text, description) if err != nil { t.Errorf("Insert failed: %v", err) return } t.Log("Document inserted successfully") // Give time for indexing time.Sleep(2 * time.Second) // Try to query the inserted document response, err := client.Query(ctx, "What is CHORUS?", QueryModeHybrid) if err != nil { t.Logf("Query after insert failed: %v", err) return } t.Logf("Query response after insert: %s", response.Response) } // TestLightRAGClient_QueryWithContext tests retrieving both response and context func TestLightRAGClient_QueryWithContext(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } config := LightRAGConfig{ BaseURL: "http://127.0.0.1:9621", Timeout: 30 * time.Second, } client := NewLightRAGClient(config) ctx := context.Background() if !client.IsHealthy(ctx) { t.Skip("skipping test - lightrag server not available") } response, err := client.QueryWithContext(ctx, "distributed coordination", QueryModeHybrid) if err != nil { t.Logf("QueryWithContext failed: %v", err) return } t.Logf("Response: %s", response.Response) t.Logf("Context: %s", response.Context) }