package discovery

import (
	"testing"

	"github.com/ultisuite/ulti-backend/internal/websearch"
)

func TestMergeSearchProviderSecretsPreservesExistingKey(t *testing.T) {
	existing := websearch.Settings{
		Providers: []websearch.Provider{{
			ID: "brave-1", Type: websearch.ProviderBrave, APIKey: "stored-key",
		}},
	}
	patch := websearch.Settings{
		Providers: []websearch.Provider{{
			ID: "brave-1", Type: websearch.ProviderBrave, APIKey: "",
		}},
	}
	merged := mergeSearchProviderSecrets(existing, patch)
	if merged.Providers[0].APIKey != "stored-key" {
		t.Fatalf("api_key = %q, want stored-key", merged.Providers[0].APIKey)
	}
}

func TestMergeSearchProviderSecretsKeepsNewKey(t *testing.T) {
	existing := websearch.Settings{
		Providers: []websearch.Provider{{
			ID: "brave-1", Type: websearch.ProviderBrave, APIKey: "old-key",
		}},
	}
	patch := websearch.Settings{
		Providers: []websearch.Provider{{
			ID: "brave-1", Type: websearch.ProviderBrave, APIKey: "new-key",
		}},
	}
	merged := mergeSearchProviderSecrets(existing, patch)
	if merged.Providers[0].APIKey != "new-key" {
		t.Fatalf("api_key = %q, want new-key", merged.Providers[0].APIKey)
	}
}
