package zfs

import (
	"context"
	"testing"

	"github.com/example/storage-appliance/internal/infra/osexec"
)

func TestListPoolsParse(t *testing.T) {
	out := "tank\tONLINE\t1.00T\n"
	fr := &osexec.FakeRunner{Stdout: out, ExitCode: 0}
	a := NewAdapter(fr)
	ctx := context.Background()
	pools, err := a.ListPools(ctx)
	if err != nil {
		t.Fatalf("ListPools failed: %v", err)
	}
	if len(pools) != 1 || pools[0].Name != "tank" || pools[0].Health != "ONLINE" {
		t.Fatalf("unexpected pools result: %+v", pools)
	}
}

func TestListDatasetsParse(t *testing.T) {
	out := "tank\tdataset\ntank/ds\tdataset\n"
	fr := &osexec.FakeRunner{Stdout: out, ExitCode: 0}
	a := NewAdapter(fr)
	pool := "tank"
	ds, err := a.ListDatasets(context.Background(), pool)
	if err != nil {
		t.Fatalf("ListDatasets failed: %v", err)
	}
	if len(ds) != 2 {
		t.Fatalf("expected 2 datasets, got %d", len(ds))
	}
}

func TestScrubStatusParse(t *testing.T) {
	out := "  scan: scrub repaired 0 in 0h0m with 0 errors on Tue Jul  1 12:34:56 2025\n"
	fr := &osexec.FakeRunner{Stdout: out, ExitCode: 0}
	a := NewAdapter(fr)
	s, err := a.ScrubStatus(context.Background(), "tank")
	if err != nil {
		t.Fatalf("ScrubStatus failed: %v", err)
	}
	if s == "" {
		t.Fatalf("expected scan line, got empty")
	}
}

func TestCreatePoolSync(t *testing.T) {
	fr := &osexec.FakeRunner{Stdout: "", ExitCode: 0}
	a := NewAdapter(fr)
	if err := a.CreatePoolSync(context.Background(), "tank", []string{"/dev/sda"}); err != nil {
		t.Fatalf("CreatePoolSync failed: %v", err)
	}
}
