package ui

import (
	"strings"
	"testing"
	"time"
)

func TestFormatRelTimeWrapsInTickableTimeElement(t *testing.T) {
	// A long-open tab needs a stable anchor so the JS ticker can
	// refresh the label — see base.html.
	when := time.Now().Add(-3 * time.Hour)
	got := string(formatRelTime(when))
	if !strings.Contains(got, `<time data-rel-ts="`) {
		t.Errorf("missing data-rel-ts anchor in %q", got)
	}
	if !strings.Contains(got, "3h ago</time>") {
		t.Errorf("expected '3h ago' label, got %q", got)
	}
}

func TestFormatRelTimeNilReturnsDash(t *testing.T) {
	var p *time.Time
	if string(formatRelTime(p)) != "—" {
		t.Errorf("nil should render as em-dash, got %q", formatRelTime(p))
	}
	if string(formatRelTime(time.Time{})) != "—" {
		t.Errorf("zero should render as em-dash")
	}
}

func TestRelTimeLabelBuckets(t *testing.T) {
	cases := []struct {
		d    time.Duration
		want string
	}{
		{30 * time.Second, "30s ago"},
		{5 * time.Minute, "5m ago"},
		{2 * time.Hour, "2h ago"},
		{3 * 24 * time.Hour, "3d ago"},
		{2 * 7 * 24 * time.Hour, "2w ago"},
		{-5 * time.Minute, "5m from now"},
	}
	for _, c := range cases {
		if got := relTimeLabel(c.d); got != c.want {
			t.Errorf("relTimeLabel(%v) = %q, want %q", c.d, got, c.want)
		}
	}
}
