package filter

import "testing"

func TestMatchSender(t *testing.T) {
	tests := []struct {
		name    string
		senders []string
		sender  string
		want    bool
	}{
		{
			name:    "empty allowlist allows all",
			senders: nil,
			sender:  "anyone@example.com",
			want:    true,
		},
		{
			name:    "exact match",
			senders: []string{"allowed@example.com"},
			sender:  "allowed@example.com",
			want:    true,
		},
		{
			name:    "case insensitive match",
			senders: []string{"Allowed@Example.COM"},
			sender:  "allowed@example.com",
			want:    true,
		},
		{
			name:    "sender case insensitive",
			senders: []string{"allowed@example.com"},
			sender:  "ALLOWED@EXAMPLE.COM",
			want:    true,
		},
		{
			name:    "whitespace trimmed from sender",
			senders: []string{"allowed@example.com"},
			sender:  "  allowed@example.com  ",
			want:    true,
		},
		{
			name:    "whitespace trimmed from allowlist",
			senders: []string{"  allowed@example.com  "},
			sender:  "allowed@example.com",
			want:    true,
		},
		{
			name:    "not in allowlist",
			senders: []string{"allowed@example.com"},
			sender:  "blocked@example.com",
			want:    false,
		},
		{
			name:    "multiple allowed senders",
			senders: []string{"one@example.com", "two@example.com", "three@example.com"},
			sender:  "two@example.com",
			want:    true,
		},
		{
			name:    "partial match not allowed",
			senders: []string{"allowed@example.com"},
			sender:  "allowed@example",
			want:    false,
		},
		{
			name:    "empty sender with allowlist",
			senders: []string{"allowed@example.com"},
			sender:  "",
			want:    false,
		},
		{
			name:    "empty sender with empty allowlist",
			senders: nil,
			sender:  "",
			want:    true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			f := &Filters{Senders: tt.senders}
			if got := f.MatchSender(tt.sender); got != tt.want {
				t.Errorf(
					"MatchSender(%q) = %v, want %v",
					tt.sender, got, tt.want,
				)
			}
		})
	}
}
