package alerts

import (
	"testing"
	"time"

	"crypto-miner-server/internal/models"
)

func TestFormatPct(t *testing.T) {
	if formatPct(50.55) != "50.5" {
		t.Fatalf("expected 50.5 got %s", formatPct(50.55))
	}
}

func TestEvaluatorOfflineAlert(t *testing.T) {
	var fired []AlertEvent
	e := &Evaluator{
		thresholds: func() Thresholds { return Thresholds{OfflineMinutes: 5} },
		notify:     func() NotifyConfig { return NotifyConfig{} },
		broadcast:  func(ev AlertEvent) { fired = append(fired, ev) },
		baseline:   make(map[string]float64),
		lastFired:  make(map[string]time.Time),
		cooldown:   0,
	}

	agent := &models.Agent{
		ID:       "a1",
		Name:     "worker-1",
		Status:   "offline",
		LastSeen: time.Now().Add(-10 * time.Minute),
	}

	e.checkOffline(agent, e.thresholds(), time.Now())
	if len(fired) != 1 {
		t.Fatalf("expected 1 alert, got %d", len(fired))
	}
	if fired[0].Type != "offline" {
		t.Fatalf("expected offline alert")
	}
}
