package threading

import (
	"reflect"
	"testing"
)

func TestNormalizeMessageID(t *testing.T) {
	tests := []struct {
		in, want string
	}{
		{"", ""},
		{"<a@b>", "<a@b>"},
		{"a@b", "<a@b>"},
	}
	for _, tc := range tests {
		if got := NormalizeMessageID(tc.in); got != tc.want {
			t.Fatalf("NormalizeMessageID(%q) = %q, want %q", tc.in, got, tc.want)
		}
	}
}

func TestParseMessageIDs(t *testing.T) {
	got := ParseMessageIDs("<a@x> <b@y>")
	want := []string{"<a@x>", "<b@y>"}
	if !reflect.DeepEqual(got, want) {
		t.Fatalf("ParseMessageIDs() = %v, want %v", got, want)
	}
}

func TestBuildReferences(t *testing.T) {
	got := BuildReferences("<c@z>", []string{"<a@x>", "<b@y>"})
	want := []string{"<a@x>", "<b@y>", "<c@z>"}
	if !reflect.DeepEqual(got, want) {
		t.Fatalf("BuildReferences() = %v, want %v", got, want)
	}
}

func TestNormalizeMessageID_imapEnvelopeWithoutBrackets(t *testing.T) {
	// go-imap Envelope.MessageID is documented without angle brackets.
	if got := NormalizeMessageID("abc@host.test"); got != "<abc@host.test>" {
		t.Fatalf("NormalizeMessageID() = %q, want %q", got, "<abc@host.test>")
	}
}

func TestBuildReferences_dedupesParent(t *testing.T) {
	got := BuildReferences("<b@y>", []string{"<a@x>", "<b@y>"})
	want := []string{"<a@x>", "<b@y>"}
	if !reflect.DeepEqual(got, want) {
		t.Fatalf("BuildReferences() = %v, want %v", got, want)
	}
}
