package platform

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

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

	dir := t.TempDir()
	path := filepath.Join(dir, "lsblk.json")
	if err := os.WriteFile(path, []byte(`{"blockdevices":[{"name":"sda","type":"disk","tran":"usb"},{"name":"sda1","type":"part"},{"name":"nvme0n1","type":"disk","tran":"nvme"},{"name":"sdb","type":"disk","tran":"sata"}]}`), 0644); err != nil {
		t.Fatalf("write lsblk fixture: %v", err)
	}

	got := lsblkDumpDevices(path)
	want := []string{"nvme0n1", "sdb"}
	if !reflect.DeepEqual(got, want) {
		t.Fatalf("lsblkDumpDevices=%v want %v", got, want)
	}
}

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

	dir := t.TempDir()
	path := filepath.Join(dir, "nvme-list.json")
	if err := os.WriteFile(path, []byte(`{"Devices":[{"DevicePath":"/dev/nvme1n1"},{"DevicePath":"/dev/nvme0n1"},{"DevicePath":"/dev/nvme1n1"}]}`), 0644); err != nil {
		t.Fatalf("write nvme fixture: %v", err)
	}

	got := nvmeDumpDevices(path)
	want := []string{"/dev/nvme0n1", "/dev/nvme1n1"}
	if !reflect.DeepEqual(got, want) {
		t.Fatalf("nvmeDumpDevices=%v want %v", got, want)
	}
}

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

	if got := sanitizeDumpName("/dev/nvme0n1"); got != "nvme0n1" {
		t.Fatalf("sanitizeDumpName=%q want nvme0n1", got)
	}
}
