package nvidia_bug_report

import (
	"testing"

	"git.mchus.pro/mchus/logpile/internal/models"
)

func TestApplyGPUSerialNumbers_FromNvidiaSMI(t *testing.T) {
	content := `
/usr/bin/nvidia-smi --query
GPU 00000000:18:00.0
    Serial Number                         : 1653925025827
GPU 00000000:2A:00.0
    Serial Number                         : 1653925050608
`

	gpus := []models.GPU{
		{BDF: "0000:18:00.0"},
		{BDF: "0000:2a:00.0"},
	}

	applyGPUSerialNumbers(content, gpus)

	if gpus[0].SerialNumber != "1653925025827" {
		t.Fatalf("unexpected serial for gpu0: %q", gpus[0].SerialNumber)
	}
	if gpus[1].SerialNumber != "1653925050608" {
		t.Fatalf("unexpected serial for gpu1: %q", gpus[1].SerialNumber)
	}
}

func TestApplyGPUSerialNumbers_FromSummaryFallback(t *testing.T) {
	content := `
NVIDIA GPU Details                  | NVIDIA H200, 570.172.08, 143771 MiB, 96.00.D0.00.03, 00000000:18:00.0, 1653925025827
                                    | NVIDIA H200, 570.172.08, 143771 MiB, 96.00.D0.00.03, 00000000:2A:00.0, 1653925050608
NVIDIA Switch Details               | No devices matching query 'Quantum'
`

	gpus := []models.GPU{
		{BDF: "0000:18:00.0"},
		{BDF: "0000:2a:00.0"},
	}

	applyGPUSerialNumbers(content, gpus)

	if gpus[0].SerialNumber != "1653925025827" {
		t.Fatalf("unexpected serial for gpu0: %q", gpus[0].SerialNumber)
	}
	if gpus[1].SerialNumber != "1653925050608" {
		t.Fatalf("unexpected serial for gpu1: %q", gpus[1].SerialNumber)
	}
}

