package opscenter

import "testing"

func TestDedupeAppsKeepsRegistryAndAddsFixedApps(t *testing.T) {
	items := dedupeApps([]appDTO{
		{AppCode: "YUMI", AppName: "Yumi From Registry", Source: "registry", Status: "active", PackageName: "com.yumi"},
		{AppCode: "yumi", AppName: "Yumi", Source: "fixed", Status: "active"},
		{AppCode: "aslan", AppName: "Aslan", Source: "fixed", Status: "active"},
	})

	if len(items) != 2 {
		t.Fatalf("items len = %d, want 2: %+v", len(items), items)
	}
	if items[0].AppCode != "yumi" || items[0].Source != "registry" || items[0].PackageName != "com.yumi" {
		t.Fatalf("registry app should win duplicate app_code, got %+v", items[0])
	}
	if items[1].AppCode != "aslan" || items[1].Source != "fixed" {
		t.Fatalf("fixed app not appended correctly, got %+v", items[1])
	}
}

func TestFilterAppsByAppCodeAndStatus(t *testing.T) {
	items := filterApps([]appDTO{
		{AppCode: "aslan", Status: "active"},
		{AppCode: "yumi", Status: "disabled"},
	}, "yu", "disabled")

	if len(items) != 1 || items[0].AppCode != "yumi" {
		t.Fatalf("filter result = %+v, want only yumi", items)
	}
}
