package provider

import "testing"

func TestConfigNormalizedTrimsIdentityFields(t *testing.T) {
	config := Config{
		ID:            ProviderID(" GitHub "),
		Endpoint:      " https://api.github.com ",
		CredentialRef: " secret://gito/providers/github ",
		Capabilities:  CapabilityChangeRequest | CapabilityWebhook,
	}

	got := config.Normalized()

	if got.ID != ProviderGitHub {
		t.Fatalf("ID: got %q want %q", got.ID, ProviderGitHub)
	}
	if got.Endpoint != "https://api.github.com" {
		t.Fatalf("Endpoint: got %q", got.Endpoint)
	}
	if got.CredentialRef != "secret://gito/providers/github" {
		t.Fatalf("CredentialRef: got %q", got.CredentialRef)
	}
}

func TestCapabilityFlags(t *testing.T) {
	capabilities := CapabilityChangeRequest | CapabilityWebhook | CapabilityComment

	if !capabilities.Has(CapabilityChangeRequest) {
		t.Fatal("expected change request capability")
	}
	if !capabilities.Has(CapabilityWebhook) {
		t.Fatal("expected webhook capability")
	}
	if !capabilities.Has(CapabilityComment) {
		t.Fatal("expected comment capability")
	}
	if capabilities.Has(CapabilityChecks) {
		t.Fatal("did not expect checks capability")
	}
	if capabilities.Has(0) {
		t.Fatal("zero capability should not be supported")
	}
}

func TestConfigSupportsCapability(t *testing.T) {
	config := Config{
		ID:           ProviderForgejo,
		Capabilities: CapabilityWebhook | CapabilityChecks,
	}

	if !config.Supports(CapabilityWebhook) {
		t.Fatal("expected webhook support")
	}
	if !config.Supports(CapabilityChecks) {
		t.Fatal("expected checks support")
	}
	if config.Supports(CapabilityChangeRequest) {
		t.Fatal("did not expect change request support")
	}
}
