package model import ( "reflect" "testing" ) func newTestData(names ...string) *SubscriptionsData { d := NewSubscriptionsData() for _, n := range names { d.AddSubscription(n, "http://example.com/"+n) } return d } func TestOrderedNames_FollowsAdditionOrder(t *testing.T) { d := newTestData("c", "a", "b") want := []string{"c", "a", "b"} if got := d.OrderedNames(); !reflect.DeepEqual(got, want) { t.Fatalf("got %#v, want %#v", got, want) } } func TestOrderedNames_MissingFromOrderAppendedAlphabetically(t *testing.T) { d := newTestData("b", "a") // Simulate a subscriptions.yaml predating the Order field, or a // subscription added by some other path that didn't touch Order. d.Order = nil d.Subscriptions["z"] = NewSubscription("z", "http://example.com/z") got := d.OrderedNames() want := []string{"a", "b", "z"} if !reflect.DeepEqual(got, want) { t.Fatalf("got %#v, want %#v", got, want) } } func TestOrderedNames_StaleOrderEntryIgnored(t *testing.T) { d := newTestData("a", "b") d.Order = []string{"a", "removed", "b"} got := d.OrderedNames() want := []string{"a", "b"} if !reflect.DeepEqual(got, want) { t.Fatalf("got %#v, want %#v", got, want) } } func TestMoveSubscription_Before(t *testing.T) { d := newTestData("a", "b", "c") if !d.MoveSubscription("c", "a", true) { t.Fatal("MoveSubscription() = false, want true") } want := []string{"c", "a", "b"} if got := d.OrderedNames(); !reflect.DeepEqual(got, want) { t.Fatalf("got %#v, want %#v", got, want) } } func TestMoveSubscription_After(t *testing.T) { d := newTestData("a", "b", "c") if !d.MoveSubscription("a", "b", false) { t.Fatal("MoveSubscription() = false, want true") } want := []string{"b", "a", "c"} if got := d.OrderedNames(); !reflect.DeepEqual(got, want) { t.Fatalf("got %#v, want %#v", got, want) } } func TestMoveSubscription_AfterLast(t *testing.T) { d := newTestData("a", "b", "c") if !d.MoveSubscription("a", "c", false) { t.Fatal("MoveSubscription() = false, want true") } want := []string{"b", "c", "a"} if got := d.OrderedNames(); !reflect.DeepEqual(got, want) { t.Fatalf("got %#v, want %#v", got, want) } } func TestMoveSubscription_BeforeFirst(t *testing.T) { d := newTestData("a", "b", "c") if !d.MoveSubscription("c", "a", true) { t.Fatal("MoveSubscription() = false, want true") } want := []string{"c", "a", "b"} if got := d.OrderedNames(); !reflect.DeepEqual(got, want) { t.Fatalf("got %#v, want %#v", got, want) } } func TestMoveSubscription_NoOpWhenSameName(t *testing.T) { d := newTestData("a", "b") if d.MoveSubscription("a", "a", true) { t.Fatal("MoveSubscription(a, a) = true, want false") } } func TestMoveSubscription_UnknownName(t *testing.T) { d := newTestData("a", "b") if d.MoveSubscription("nope", "a", true) { t.Fatal("MoveSubscription() = true, want false for unknown name") } } func TestMoveSubscription_UnknownTarget(t *testing.T) { d := newTestData("a", "b") if d.MoveSubscription("a", "nope", true) { t.Fatal("MoveSubscription() = true, want false for unknown target") } } func TestMoveSubscription_MaterializesFullOrderFromLegacyData(t *testing.T) { // A subscriptions.yaml predating the Order field: Subscriptions // populated directly, Order left nil, as if freshly loaded from an old // file. d := &SubscriptionsData{Subscriptions: map[string]*Subscription{ "a": NewSubscription("a", "http://example.com/a"), "b": NewSubscription("b", "http://example.com/b"), "c": NewSubscription("c", "http://example.com/c"), }} if !d.MoveSubscription("c", "a", true) { t.Fatal("MoveSubscription() = false, want true") } // Legacy fallback order is alphabetical (a, b, c); moving c before a // should yield c, a, b. want := []string{"c", "a", "b"} if got := d.OrderedNames(); !reflect.DeepEqual(got, want) { t.Fatalf("got %#v, want %#v", got, want) } } func TestRenameSubscription_PreservesPosition(t *testing.T) { d := newTestData("a", "b", "c") if !d.RenameSubscription("b", "bee") { t.Fatal("RenameSubscription() = false") } want := []string{"a", "bee", "c"} if got := d.OrderedNames(); !reflect.DeepEqual(got, want) { t.Fatalf("got %#v, want %#v", got, want) } } func TestRemoveSubscription_RemovesFromOrder(t *testing.T) { d := newTestData("a", "b", "c") if !d.RemoveSubscription("b") { t.Fatal("RemoveSubscription() = false") } want := []string{"a", "c"} if got := d.OrderedNames(); !reflect.DeepEqual(got, want) { t.Fatalf("got %#v, want %#v", got, want) } } func TestGetActiveSubscriptions_RespectsOrder(t *testing.T) { d := newTestData("a", "b", "c") d.SetInactive("a") // AddSubscription auto-activates the first one added d.SetActive("b") d.SetActive("c") d.MoveSubscription("c", "a", true) actives := d.GetActiveSubscriptions() if len(actives) != 2 || actives[0].Name != "c" || actives[1].Name != "b" { t.Fatalf("unexpected order: %#v", actives) } }