package service

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

func TestBuildStageHeartbeatProgresses(t *testing.T) {
	p1, m1 := buildStageHeartbeat("titan-13", "titan-04", 10*time.Second)
	p2, m2 := buildStageHeartbeat("titan-13", "titan-04", 3*time.Minute)
	p3, m3 := buildStageHeartbeat("titan-13", "titan-04", 11*time.Minute)

	if !(p1 > 8 && p1 < p2 && p2 < p3 && p3 <= 76) {
		t.Fatalf("unexpected build progress values: %v %v %v", p1, p2, p3)
	}
	if !strings.Contains(m1, "Scheduling") {
		t.Fatalf("expected scheduling message, got %q", m1)
	}
	if !strings.Contains(m2, "replacement image filesystem") {
		t.Fatalf("expected filesystem message, got %q", m2)
	}
	if !strings.Contains(m3, "Harbor") {
		t.Fatalf("expected Harbor message, got %q", m3)
	}
}

func TestFlashStageHeartbeatProgresses(t *testing.T) {
	p1, m1 := flashStageHeartbeat("titan-22", "registry.bstein.dev/metis/titan-13:latest", 5*time.Second)
	p2, m2 := flashStageHeartbeat("titan-22", "registry.bstein.dev/metis/titan-13:latest", 20*time.Second)
	p3, m3 := flashStageHeartbeat("titan-22", "registry.bstein.dev/metis/titan-13:latest", 90*time.Second)

	if !(p1 > 84 && p1 < p2 && p2 < p3 && p3 <= 98) {
		t.Fatalf("unexpected flash progress values: %v %v %v", p1, p2, p3)
	}
	if !strings.Contains(m1, "Pulling") {
		t.Fatalf("expected pulling message, got %q", m1)
	}
	if !strings.Contains(m2, "Writing") {
		t.Fatalf("expected writing message, got %q", m2)
	}
	if !strings.Contains(m3, "Flushing") {
		t.Fatalf("expected flushing message, got %q", m3)
	}
}
