package llm

import (
	"errors"
	"strings"
	"testing"
)

func TestRedactSecrets(t *testing.T) {
	got := RedactSecrets("api key secret-token and Bearer secret-token failed", []string{"", "secret-token", "secret-token"})

	if strings.Contains(got, "secret-token") {
		t.Fatalf("expected secret to be redacted, got %q", got)
	}
	if strings.Count(got, secretReplacement) != 2 {
		t.Fatalf("expected two redactions, got %q", got)
	}
}

func TestRedactSecretsPrefersLongerSecrets(t *testing.T) {
	got := RedactSecrets("token token-extra", []string{"token", "token-extra"})

	if strings.Contains(got, "token") {
		t.Fatalf("expected overlapping secrets to be redacted, got %q", got)
	}
}

func TestErrorWithSecretsRedacted(t *testing.T) {
	err := ErrorWithSecretsRedacted(errors.New("secret-value failed"), []string{"secret-value"})

	if err == nil {
		t.Fatalf("expected redacted error")
	}
	if strings.Contains(err.Error(), "secret-value") {
		t.Fatalf("expected secret to be redacted, got %q", err.Error())
	}
}

func TestErrorWithSecretsRedactedNil(t *testing.T) {
	if err := ErrorWithSecretsRedacted(nil, []string{"secret"}); err != nil {
		t.Fatalf("expected nil error, got %v", err)
	}
}
