package common

import (
	"net/url"
	"testing"
)

func TestListenDefinitionsTopicUpdate(t *testing.T) {

	tests := []struct {
		name         string
		handlers     []string
		orgs1, orgs2 []string

		topicDelta []string
	}{
		{
			name: "no handlers, no orgs",
		},
		{
			name:       "adding one org",
			handlers:   []string{"foo"},
			orgs2:      []string{"newOrg"},
			topicDelta: []string{"+suse"},
		},
	}

	u, _ := url.Parse("amqps://rabbit.example.com")
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			l := ListenDefinitions{
				Orgs:            test.orgs1,
				Handlers:        make(map[string]RequestProcessor),
				topicSubChanges: make(chan string, len(test.topicDelta)*10),
				RabbitURL:       u,
			}

			for _, r := range test.handlers {
				l.Handlers[r] = nil
			}

			l.UpdateTopics()
			if len(l.topicSubChanges) != len(test.topicDelta) {
				t.Fatal("topicSubChanges != topicDelta")
			}
		})
	}
}
