package inspur

import (
	"testing"

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

func TestAppendHGXFirmwareFromHWInfo_AppendsInventoryEntries(t *testing.T) {
	hw := &models.HardwareConfig{
		Firmware: []models.FirmwareInfo{
			{DeviceName: "BIOS", Version: "1.0.0"},
		},
	}

	content := []byte(`
{
  "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory/HGX_FW_BMC_0",
  "Id": "HGX_FW_BMC_0",
  "Oem": {
    "Nvidia": {
      "ActiveFirmwareSlot": {"Version": "25.05-A"},
      "InactiveFirmwareSlot": {"Version": "25.04-B"}
    }
  },
  "Version": "25.05-A",
  "WriteProtected": false
}
{
  "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory/HGX_FW_GPU_SXM_1",
  "Id": "HGX_FW_GPU_SXM_1",
  "Version": "97.00.C5.00.0E",
  "WriteProtected": false
}
{
  "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory/HGX_Driver_GPU_SXM_1",
  "Id": "HGX_Driver_GPU_SXM_1",
  "Version": "",
  "WriteProtected": false
}
`)

	appendHGXFirmwareFromHWInfo(content, hw)

	if len(hw.Firmware) != 5 {
		t.Fatalf("expected 5 firmware entries after append, got %d", len(hw.Firmware))
	}

	seen := make(map[string]string)
	for _, fw := range hw.Firmware {
		seen[fw.DeviceName] = fw.Version
	}

	if seen["HGX_FW_BMC_0"] != "25.05-A" {
		t.Fatalf("expected HGX_FW_BMC_0 version 25.05-A, got %q", seen["HGX_FW_BMC_0"])
	}
	if seen["HGX_FW_BMC_0 Active Slot"] != "25.05-A" {
		t.Fatalf("expected active slot version, got %q", seen["HGX_FW_BMC_0 Active Slot"])
	}
	if seen["HGX_FW_BMC_0 Inactive Slot"] != "25.04-B" {
		t.Fatalf("expected inactive slot version, got %q", seen["HGX_FW_BMC_0 Inactive Slot"])
	}
	if seen["HGX_FW_GPU_SXM_1"] != "97.00.C5.00.0E" {
		t.Fatalf("expected GPU FW entry, got %q", seen["HGX_FW_GPU_SXM_1"])
	}
	if _, ok := seen["HGX_Driver_GPU_SXM_1"]; ok {
		t.Fatalf("did not expect empty version driver entry")
	}
}
