package handlers import ( "fmt" "net/http" "os" "testing" "git.gobha.me/xcaliber/chat-switchboard/database" ) // ═══════════════════════════════════════════ // Live Provider Integration Tests // ═══════════════════════════════════════════ // These tests require: // - TEST_DATABASE_URL or PGHOST+PGUSER // - VENICE_API_KEY secret // // They exercise the full flow: create provider → // fetch models → enable model → resolve → complete. // ═══════════════════════════════════════════ func requireVeniceKey(t *testing.T) string { t.Helper() key := os.Getenv("VENICE_API_KEY") if key == "" { t.Skip("VENICE_API_KEY not set — skipping live provider test") } return key } // TestLive_VeniceProviderFullFlow exercises the complete admin workflow: // create provider → fetch models → enable a model → user sees it → chat completion func TestLive_VeniceProviderFullFlow(t *testing.T) { h := setupHarness(t) veniceKey := requireVeniceKey(t) _, adminToken := h.createAdminUser("admin", "admin@test.com") // ── 1. Create Venice provider config ──── t.Log("Step 1: Creating Venice provider config") w := h.request("POST", "/api/v1/admin/configs", adminToken, map[string]interface{}{ "name": "Venice Live Test", "provider": "venice", "endpoint": "https://api.venice.ai/api/v1", "api_key": veniceKey, }) if w.Code != http.StatusCreated { t.Fatalf("create venice config: want 201, got %d: %s", w.Code, w.Body.String()) } var configResp map[string]interface{} decode(w, &configResp) configID := configResp["id"].(string) t.Logf(" Created config: %s", configID) // ── 2. Fetch models from Venice ───────── t.Log("Step 2: Fetching models from Venice API") w = h.request("POST", "/api/v1/admin/models/fetch", adminToken, map[string]interface{}{ "provider_config_id": configID, }) if w.Code != http.StatusOK { t.Fatalf("fetch models: want 200, got %d: %s", w.Code, w.Body.String()) } var fetchResp map[string]interface{} decode(w, &fetchResp) totalFetched := fetchResp["total"].(float64) if totalFetched < 1 { t.Fatalf("Venice should return at least 1 model, got %.0f", totalFetched) } t.Logf(" Fetched %.0f models from Venice", totalFetched) // ── 3. List catalog models (all disabled by default) ── t.Log("Step 3: Listing catalog models") w = h.request("GET", "/api/v1/admin/models", adminToken, nil) if w.Code != http.StatusOK { t.Fatalf("list models: want 200, got %d: %s", w.Code, w.Body.String()) } var modelsResp map[string]interface{} decode(w, &modelsResp) catalogModels := modelsResp["models"].([]interface{}) if len(catalogModels) < 1 { t.Fatal("catalog should have models after fetch") } // Find a text model to enable (prefer a small/fast one) var enableID string var enableModelID string for _, raw := range catalogModels { m := raw.(map[string]interface{}) modelID := m["model_id"].(string) vis := m["visibility"].(string) if vis == "disabled" { enableID = m["id"].(string) enableModelID = modelID break } } if enableID == "" { t.Fatal("no disabled model found to enable") } t.Logf(" Will enable: %s (catalog ID: %s)", enableModelID, enableID) // ── 4. Enable the model ───────────────── t.Log("Step 4: Enabling model") w = h.request("PUT", "/api/v1/admin/models/"+enableID, adminToken, map[string]interface{}{"visibility": "enabled"}) if w.Code != http.StatusOK { t.Fatalf("enable model: want 200, got %d: %s", w.Code, w.Body.String()) } // ── 5. Verify models/enabled returns it (admin) ── t.Log("Step 5: Verifying models/enabled (admin)") w = h.request("GET", "/api/v1/models/enabled", adminToken, nil) if w.Code != http.StatusOK { t.Fatalf("models/enabled: want 200, got %d: %s", w.Code, w.Body.String()) } var enabledResp map[string]interface{} decode(w, &enabledResp) enabledModels := enabledResp["models"].([]interface{}) if len(enabledModels) < 1 { t.Fatal("models/enabled should return at least 1 model after enabling") } // Verify our model is in the list found := false for _, raw := range enabledModels { m := raw.(map[string]interface{}) if m["model_id"] == enableModelID { found = true t.Logf(" ✓ Found %s in enabled models", enableModelID) // Verify it has the required fields for the frontend if m["config_id"] == nil || m["config_id"] == "" { t.Error("enabled model must have config_id for composite ID") } if m["provider_name"] == nil || m["provider_name"] == "" { t.Error("enabled model must have provider_name for display") } break } } if !found { t.Errorf("enabled model %s not found in models/enabled response", enableModelID) } // ── 6. Verify a REGULAR USER also sees the model ── t.Log("Step 6: Verifying models/enabled (regular user)") userID := database.SeedTestUser(t, "liveuser", "liveuser@test.com") database.TestDB.Exec("UPDATE users SET is_active = true WHERE id = $1", userID) userToken := makeToken(userID, "liveuser@test.com", "user") w = h.request("GET", "/api/v1/models/enabled", userToken, nil) if w.Code != http.StatusOK { t.Fatalf("user models/enabled: want 200, got %d: %s", w.Code, w.Body.String()) } var userResp map[string]interface{} decode(w, &userResp) userModels := userResp["models"].([]interface{}) if len(userModels) < 1 { t.Fatalf("regular user should see at least 1 enabled model, got %d — "+ "admin can see models but regular user cannot; check ListVisible query", len(userModels)) } userFound := false for _, raw := range userModels { m := raw.(map[string]interface{}) if m["model_id"] == enableModelID { userFound = true t.Logf(" ✓ Regular user can see %s", enableModelID) // Verify same fields available for regular user if m["config_id"] == nil || m["config_id"] == "" { t.Error("user: enabled model must have config_id") } if m["provider_name"] == nil || m["provider_name"] == "" { t.Error("user: enabled model must have provider_name") } break } } if !userFound { t.Errorf("regular user cannot see %s — admin→user visibility broken", enableModelID) } } // TestLive_VeniceFetchModelsCapabilities verifies that Venice model // capabilities are correctly parsed into the catalog. func TestLive_VeniceFetchModelsCapabilities(t *testing.T) { h := setupHarness(t) veniceKey := requireVeniceKey(t) _, adminToken := h.createAdminUser("admin", "admin@test.com") // Create provider + fetch w := h.request("POST", "/api/v1/admin/configs", adminToken, map[string]interface{}{ "name": "Venice Caps Test", "provider": "venice", "endpoint": "https://api.venice.ai/api/v1", "api_key": veniceKey, }) var cfg map[string]interface{} decode(w, &cfg) configID := cfg["id"].(string) h.request("POST", "/api/v1/admin/models/fetch", adminToken, map[string]interface{}{"provider_config_id": configID}) // Read catalog and check capabilities w = h.request("GET", "/api/v1/admin/models", adminToken, nil) var resp map[string]interface{} decode(w, &resp) for _, raw := range resp["models"].([]interface{}) { m := raw.(map[string]interface{}) caps, ok := m["capabilities"].(map[string]interface{}) if !ok { t.Errorf("model %s: capabilities must be an object", m["model_id"]) continue } // streaming should always be true for Venice if caps["streaming"] != true { t.Errorf("model %s: streaming should be true", m["model_id"]) } // Verify capabilities are actual booleans (not strings) for _, key := range []string{"streaming", "vision", "tool_calling", "reasoning"} { if v, exists := caps[key]; exists { if _, ok := v.(bool); !ok { t.Errorf("model %s: capability %s should be bool, got %T", m["model_id"], key, v) } } } } } // TestLive_VeniceChatCompletion sends an actual chat completion. func TestLive_VeniceChatCompletion(t *testing.T) { h := setupHarness(t) veniceKey := requireVeniceKey(t) adminID, adminToken := h.createAdminUser("admin", "admin@test.com") _ = adminID // Create provider w := h.request("POST", "/api/v1/admin/configs", adminToken, map[string]interface{}{ "name": "Venice Chat Test", "provider": "venice", "endpoint": "https://api.venice.ai/api/v1", "api_key": veniceKey, }) var cfg map[string]interface{} decode(w, &cfg) configID := cfg["id"].(string) // Fetch + enable a fast model h.request("POST", "/api/v1/admin/models/fetch", adminToken, map[string]interface{}{"provider_config_id": configID}) w = h.request("GET", "/api/v1/admin/models", adminToken, nil) var modelsResp map[string]interface{} decode(w, &modelsResp) // Find and enable a small model (prefer llama or qwen for speed) var targetModelID, targetCatalogID string for _, raw := range modelsResp["models"].([]interface{}) { m := raw.(map[string]interface{}) mid := m["model_id"].(string) // Pick any available model - first disabled one if m["visibility"].(string) == "disabled" { targetModelID = mid targetCatalogID = m["id"].(string) break } } if targetModelID == "" { t.Skip("no model available to test chat completion") } h.request("PUT", "/api/v1/admin/models/"+targetCatalogID, adminToken, map[string]interface{}{"visibility": "enabled"}) // Set as default model and send completion // First get enabled model's composite ID w = h.request("GET", "/api/v1/models/enabled", adminToken, nil) var enabled map[string]interface{} decode(w, &enabled) if len(enabled["models"].([]interface{})) == 0 { t.Fatal("no enabled models for completion test") } firstModel := enabled["models"].([]interface{})[0].(map[string]interface{}) modelForChat := firstModel["model_id"].(string) configForChat := firstModel["config_id"].(string) t.Logf("Sending completion to %s via config %s", modelForChat, configForChat) // Create a channel first w = h.request("POST", "/api/v1/channels", adminToken, map[string]interface{}{ "title": "Test Chat", "type": "direct", }) if w.Code != http.StatusCreated { // Some channel handlers may use database.DB directly t.Skipf("channel creation failed (may need database.DB global): %d %s", w.Code, w.Body.String()) } var ch map[string]interface{} decode(w, &ch) channelID := ch["id"].(string) // Send completion w = h.request("POST", "/api/v1/chat/completions", adminToken, map[string]interface{}{ "channel_id": channelID, "model": modelForChat, "config_id": configForChat, "stream": false, "messages": []map[string]string{ {"role": "user", "content": "Say hello in exactly 3 words."}, }, }) if w.Code != http.StatusOK { t.Logf("completion response: %s", w.Body.String()) t.Skipf("chat completion failed with %d (may need full router wiring)", w.Code) } t.Logf(" ✓ Completion succeeded: %s", w.Body.String()[:min(200, w.Body.Len())]) } // TestLive_VeniceModelDeletion tests cleanup: delete provider removes catalog entries func TestLive_VeniceModelDeletion(t *testing.T) { h := setupHarness(t) veniceKey := requireVeniceKey(t) _, adminToken := h.createAdminUser("admin", "admin@test.com") // Create + fetch w := h.request("POST", "/api/v1/admin/configs", adminToken, map[string]interface{}{ "name": "Venice Delete Test", "provider": "venice", "endpoint": "https://api.venice.ai/api/v1", "api_key": veniceKey, }) var cfg map[string]interface{} decode(w, &cfg) configID := cfg["id"].(string) h.request("POST", "/api/v1/admin/models/fetch", adminToken, map[string]interface{}{"provider_config_id": configID}) // Verify models exist var count int database.TestDB.QueryRow("SELECT COUNT(*) FROM model_catalog WHERE provider_config_id = $1", configID).Scan(&count) if count == 0 { t.Fatal("catalog should have models after fetch") } t.Logf(" %d models in catalog before delete", count) // Delete the provider w = h.request("DELETE", "/api/v1/admin/configs/"+configID, adminToken, nil) if w.Code != http.StatusOK { t.Fatalf("delete config: want 200, got %d: %s", w.Code, w.Body.String()) } // Verify cascade: catalog entries should be gone database.TestDB.QueryRow("SELECT COUNT(*) FROM model_catalog WHERE provider_config_id = $1", configID).Scan(&count) if count != 0 { t.Errorf("catalog should be empty after provider delete, got %d entries", count) } t.Log(" ✓ Cascade delete cleaned up catalog entries") } // TestLive_VeniceBYOK_AutoFetch exercises the ACTUAL user experience: // user creates a BYOK provider → auto-fetch triggers → models appear in /models/enabled // // This is the definitive test. No simulated data. Real Venice API. func TestLive_VeniceBYOK_AutoFetch(t *testing.T) { h := setupHarness(t) veniceKey := requireVeniceKey(t) _, adminToken := h.createAdminUser("admin", "admin@test.com") // Enable BYOK policy h.request("PUT", "/api/v1/admin/settings/allow_user_byok", adminToken, map[string]interface{}{"value": "true"}) // Create regular user userID := database.SeedTestUser(t, "byokuser", "byokuser@test.com") database.TestDB.Exec("UPDATE users SET is_active = true WHERE id = $1", userID) userToken := makeToken(userID, "byokuser@test.com", "user") // ── Step 1: User creates BYOK provider (the ONLY user action) ── t.Log("Step 1: User creates BYOK Venice provider") w := h.request("POST", "/api/v1/api-configs", userToken, map[string]interface{}{ "name": "My Venice", "provider": "venice", "endpoint": "https://api.venice.ai/api/v1", "api_key": veniceKey, }) if w.Code != http.StatusCreated { t.Fatalf("create BYOK provider: want 201, got %d: %s", w.Code, w.Body.String()) } var created map[string]interface{} decode(w, &created) cfgID := created["id"].(string) t.Logf(" Created provider: %s", cfgID) // ── Step 2: Verify auto-fetch happened ── if created["warning"] != nil { t.Fatalf("auto-fetch should succeed with real Venice key, got warning: %v", created["warning"]) } modelsFetched := created["models_fetched"] if modelsFetched == nil || modelsFetched.(float64) < 1 { t.Fatalf("auto-fetch should return models_fetched > 0, got: %v", modelsFetched) } t.Logf(" Auto-fetched %.0f models", modelsFetched.(float64)) // ── Step 3: User's models/enabled shows personal models ── t.Log("Step 3: Verify user sees BYOK models in /models/enabled") w = h.request("GET", "/api/v1/models/enabled", userToken, nil) if w.Code != http.StatusOK { t.Fatalf("models/enabled: %d: %s", w.Code, w.Body.String()) } var resp map[string]interface{} decode(w, &resp) userModels := resp["models"].([]interface{}) personalCount := 0 for _, raw := range userModels { m := raw.(map[string]interface{}) if m["scope"] == "personal" { personalCount++ } } if personalCount < 1 { t.Fatalf("user should see personal BYOK models, got %d personal out of %d total\n"+ " model IDs: %v", personalCount, len(userModels), func() []string { ids := make([]string, 0) for _, raw := range userModels { m := raw.(map[string]interface{}) ids = append(ids, fmt.Sprintf("%s(scope=%s)", m["model_id"], m["scope"])) } return ids }()) } t.Logf(" ✓ User sees %d personal BYOK models (out of %d total)", personalCount, len(userModels)) // ── Step 4: Verify model fields for frontend ── t.Log("Step 4: Verify frontend-required fields on BYOK models") for _, raw := range userModels { m := raw.(map[string]interface{}) if m["scope"] != "personal" { continue } if m["config_id"] == nil || m["config_id"] == "" { t.Errorf("personal model %s missing config_id", m["model_id"]) } if m["provider_name"] == nil || m["provider_name"] == "" { t.Errorf("personal model %s missing provider_name", m["model_id"]) } if m["model_id"] == nil || m["model_id"] == "" { t.Errorf("personal model missing model_id") } break // check first personal model only } // ── Cleanup ── h.request("DELETE", fmt.Sprintf("/api/v1/api-configs/%s", cfgID), userToken, nil) }