package inspur

import (
	"testing"

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

func TestInferBoardSerialFromFallbackLogs_PrefersFRU(t *testing.T) {
	files := []parser.ExtractedFile{
		{
			Path: "component/fru.txt",
			Content: []byte(`FRU Device Description : Builtin FRU Device (ID 0)
 Product Serial        : 23DB01639
`),
		},
		{
			Path:    "runningdata/RTOSDump/hostname",
			Content: []byte("HOSTNAME-FALLBACK\n"),
		},
		{
			Path:    "log/bmc/struct-log/maintenance_json.log",
			Content: []byte(`{ "_HOSTNAME": "JSON-FALLBACK" }`),
		},
	}

	got := inferBoardSerialFromFallbackLogs(files)
	if got != "23DB01639" {
		t.Fatalf("expected FRU serial 23DB01639, got %q", got)
	}
}

func TestInferBoardSerialFromFallbackLogs_UsesHostnameFile(t *testing.T) {
	files := []parser.ExtractedFile{
		{
			Path:    "runningdata/RTOSDump/hostname",
			Content: []byte("23DB01639\n"),
		},
	}

	got := inferBoardSerialFromFallbackLogs(files)
	if got != "23DB01639" {
		t.Fatalf("expected hostname serial 23DB01639, got %q", got)
	}
}

func TestInferBoardSerialFromFallbackLogs_UsesMaintenanceJSON(t *testing.T) {
	files := []parser.ExtractedFile{
		{
			Path:    "log/bmc/struct-log/maintenance_json.log",
			Content: []byte(`{ "_HOSTNAME": "23DB01639", "MESSAGE": "ok" }`),
		},
	}

	got := inferBoardSerialFromFallbackLogs(files)
	if got != "23DB01639" {
		t.Fatalf("expected JSON hostname serial 23DB01639, got %q", got)
	}
}

func TestInferBoardModelFromFallbackLogs_PrefersFRU(t *testing.T) {
	files := []parser.ExtractedFile{
		{
			Path: "component/fru.txt",
			Content: []byte(`FRU Device Description : Builtin FRU Device (ID 0)
 Board Product         : KR9288-X3-A0-F0-00
 Product Name          : KR9288-X3-A0-F0-00
`),
		},
	}

	got := inferBoardModelFromFallbackLogs(files)
	if got != "KR9288-X3-A0-F0-00" {
		t.Fatalf("expected board model KR9288-X3-A0-F0-00, got %q", got)
	}
}
