package stats

import "testing"

func TestFormatCounter(t *testing.T) {
	cases := []struct {
		n          int64
		wantMain   string
		wantSuffix string
	}{
		{12_400_000, "12", ",4M+"},
		{8_200_000, "8", ",2M+"},
		{3_100_000, "3", ",1M+"},
		{18_000_000, "18", "M+"},
		{27, "27", ""},
	}
	for _, c := range cases {
		got := FormatCounter(c.n)
		if got.Main != c.wantMain || got.Suffix != c.wantSuffix {
			t.Fatalf("FormatCounter(%d) = %+v, want main=%q suffix=%q", c.n, got, c.wantMain, c.wantSuffix)
		}
	}
}

func TestFormatMillionsLabel(t *testing.T) {
	if got := FormatMillionsLabel(12_400_000); got != "12,4 milhões de empresas" {
		t.Fatalf("got %q", got)
	}
	if got := FormatMillionsLabel(18_000_000); got != "18 milhões de empresas" {
		t.Fatalf("got %q", got)
	}
}

func TestApplyHomeHTML(t *testing.T) {
	html := `<div><!-- LEADB2B_STAT:empresas_indice -->old<!-- /LEADB2B_STAT:empresas_indice --></div>`
	f := File{
		Metrics: map[string]Metric{
			"empresas_indice": {Value: 5_000_000, Status: "done"},
		},
	}
	got := ApplyHomeHTML(html, f, DefaultFallbacks())
	if got != `<div><span class="counter">5</span>M+</div>` {
		t.Fatalf("got %q", got)
	}
}
