package platform

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestWriteGPUMetricsCSVIncludesStageColumn(t *testing.T) {
	t.Parallel()

	dir := t.TempDir()
	path := filepath.Join(dir, "gpu-metrics.csv")
	rows := []GPUMetricRow{
		{Stage: "warmup", ElapsedSec: 1, GPUIndex: 0, TempC: 71, UsagePct: 99, MemUsagePct: 80, PowerW: 420, ClockMHz: 1800, MemClockMHz: 1200},
	}
	if err := WriteGPUMetricsCSV(path, rows); err != nil {
		t.Fatalf("WriteGPUMetricsCSV: %v", err)
	}
	raw, err := os.ReadFile(path)
	if err != nil {
		t.Fatalf("ReadFile: %v", err)
	}
	text := string(raw)
	for _, needle := range []string{
		"stage,elapsed_sec,gpu_index",
		`"warmup",1.0,0,71.0,99.0,80.0,420.0,1800,1200`,
	} {
		if !strings.Contains(text, needle) {
			t.Fatalf("csv missing %q\n%s", needle, text)
		}
	}
}

func TestWriteGPUMetricsHTMLShowsStageLegendAndLabels(t *testing.T) {
	t.Parallel()

	dir := t.TempDir()
	path := filepath.Join(dir, "gpu-metrics.html")
	rows := []GPUMetricRow{
		{Stage: "baseline", ElapsedSec: 1, GPUIndex: 0, TempC: 50, UsagePct: 10, MemUsagePct: 5, PowerW: 100, ClockMHz: 500, MemClockMHz: 400},
		{Stage: "baseline", ElapsedSec: 2, GPUIndex: 0, TempC: 51, UsagePct: 11, MemUsagePct: 5, PowerW: 101, ClockMHz: 510, MemClockMHz: 400},
		{Stage: "steady-fp16", ElapsedSec: 3, GPUIndex: 0, TempC: 70, UsagePct: 98, MemUsagePct: 75, PowerW: 390, ClockMHz: 1700, MemClockMHz: 1100},
		{Stage: "steady-fp16", ElapsedSec: 4, GPUIndex: 0, TempC: 71, UsagePct: 99, MemUsagePct: 76, PowerW: 395, ClockMHz: 1710, MemClockMHz: 1110},
	}
	if err := WriteGPUMetricsHTML(path, rows); err != nil {
		t.Fatalf("WriteGPUMetricsHTML: %v", err)
	}
	raw, err := os.ReadFile(path)
	if err != nil {
		t.Fatalf("ReadFile: %v", err)
	}
	text := string(raw)
	for _, needle := range []string{
		"stage-legend",
		"baseline",
		"steady-fp16",
		"GPU Stress Test Metrics",
	} {
		if !strings.Contains(text, needle) {
			t.Fatalf("html missing %q\n%s", needle, text)
		}
	}
}
