package sqlite import ( "context" "database/sql" "errors" "testing" ) func TestProvidersRepoCreateAndGet(t *testing.T) { store := openTestDB(t) packID := createTestPack(t, store) providerID, err := store.Providers().Create(context.Background(), Provider{ PackID: packID, ProviderID: "deepseek", DisplayName: "DeepSeek", BaseURL: "https://api.deepseek.com", Platform: "openai", AccountType: "apikey", SmokeTestModel: "deepseek-chat", ManifestJSON: `{"models":["deepseek-chat"]}`, }) if err != nil { t.Fatalf("Create() error = %v", err) } if providerID <= 0 { t.Fatalf("Create() id = %d, want positive", providerID) } got, err := store.Providers().GetByPackIDAndProviderID(context.Background(), packID, "deepseek") if err != nil { t.Fatalf("GetByPackIDAndProviderID() error = %v", err) } if got.ProviderID != "deepseek" || got.DisplayName != "DeepSeek" { t.Fatalf("GetByPackIDAndProviderID() = %+v, want deepseek", got) } } func TestProvidersRepoListByProviderID(t *testing.T) { store := openTestDB(t) packID1 := createTestPackWithSuffix(t, store, "a") packID2 := createTestPackWithSuffix(t, store, "b") store.Providers().Create(context.Background(), Provider{PackID: packID1, ProviderID: "deepseek", DisplayName: "DS1", BaseURL: "https://a.com", Platform: "openai"}) store.Providers().Create(context.Background(), Provider{PackID: packID2, ProviderID: "deepseek", DisplayName: "DS2", BaseURL: "https://b.com", Platform: "openai"}) providers, err := store.Providers().ListByProviderID(context.Background(), "deepseek") if err != nil { t.Fatalf("ListByProviderID() error = %v", err) } if len(providers) != 2 { t.Fatalf("ListByProviderID() count = %d, want 2", len(providers)) } } func createTestPackWithSuffix(t *testing.T, store *DB, suffix string) int64 { t.Helper() id, err := store.Packs().Create(context.Background(), Pack{ PackID: "pack-" + sanitizeTestName(t.Name()) + "-" + suffix, Version: "1.0.0", Checksum: "chk", }) if err != nil { t.Fatalf("createTestPackWithSuffix error = %v", err) } return id } func TestProvidersRepoListByProviderIDEmpty(t *testing.T) { store := openTestDB(t) providers, err := store.Providers().ListByProviderID(context.Background(), "nonexistent") if err != nil { t.Fatalf("ListByProviderID() error = %v", err) } if len(providers) != 0 { t.Fatalf("ListByProviderID() count = %d, want 0", len(providers)) } } func TestProvidersRepoUpsertCreatesNew(t *testing.T) { store := openTestDB(t) packID := createTestPack(t, store) id, err := store.Providers().Upsert(context.Background(), Provider{ PackID: packID, ProviderID: "upsert-p", DisplayName: "P", BaseURL: "https://u.com", Platform: "openai", }) if err != nil { t.Fatalf("Upsert() error = %v", err) } if id <= 0 { t.Fatalf("Upsert() id = %d, want positive", id) } } func TestProvidersRepoUpsertUpdatesExisting(t *testing.T) { store := openTestDB(t) packID := createTestPack(t, store) id1, _ := store.Providers().Upsert(context.Background(), Provider{ PackID: packID, ProviderID: "upsert-p", DisplayName: "P1", BaseURL: "https://u1.com", Platform: "openai", }) id2, _ := store.Providers().Upsert(context.Background(), Provider{ PackID: packID, ProviderID: "upsert-p", DisplayName: "P2", BaseURL: "https://u2.com", Platform: "openai", }) if id2 != id1 { t.Fatalf("Upsert update id = %d, want %d", id2, id1) } got, _ := store.Providers().GetByPackIDAndProviderID(context.Background(), packID, "upsert-p") if got.DisplayName != "P2" { t.Fatalf("DisplayName after upsert = %q, want P2", got.DisplayName) } } func TestProvidersRepoValidationErrors(t *testing.T) { store := openTestDB(t) packID := createTestPack(t, store) tests := []struct { name string provider Provider }{ {"pack_id zero", Provider{ProviderID: "p", DisplayName: "d", BaseURL: "b", Platform: "openai"}}, {"empty provider_id", Provider{PackID: packID, DisplayName: "d", BaseURL: "b", Platform: "openai"}}, {"empty display_name", Provider{PackID: packID, ProviderID: "p", BaseURL: "b", Platform: "openai"}}, {"empty base_url", Provider{PackID: packID, ProviderID: "p", DisplayName: "d", Platform: "openai"}}, {"empty platform", Provider{PackID: packID, ProviderID: "p", DisplayName: "d", BaseURL: "b"}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { _, err := store.Providers().Create(context.Background(), tt.provider) if err == nil { t.Fatal("Create() error = nil, want validation error") } }) } } func TestProvidersRepoGetByPackIDAndProviderIDNotFound(t *testing.T) { store := openTestDB(t) _, err := store.Providers().GetByPackIDAndProviderID(context.Background(), 999, "p") if !errors.Is(err, sql.ErrNoRows) { t.Fatalf("GetByPackIDAndProviderID() error = %v, want sql.ErrNoRows", err) } } func TestProvidersRepoGetByPackIDEmptyError(t *testing.T) { store := openTestDB(t) _, err := store.Providers().GetByPackIDAndProviderID(context.Background(), 0, "p") if err == nil { t.Fatal("GetByPackIDAndProviderID with packID=0 error = nil, want error") } } func TestDefaultJSONObject(t *testing.T) { if got := defaultJSONObject(""); got != "{}" { t.Fatalf("defaultJSONObject('') = %q, want {}", got) } if got := defaultJSONObject(`{"a":1}`); got != `{"a":1}` { t.Fatalf("defaultJSONObject() = %q, want input", got) } } func TestDefaultJSONArray(t *testing.T) { if got := defaultJSONArray(""); got != "[]" { t.Fatalf("defaultJSONArray('') = %q, want []", got) } if got := defaultJSONArray(`["a"]`); got != `["a"]` { t.Fatalf("defaultJSONArray() = %q, want input", got) } }