package cli

import (
	"bytes"
	"strings"
	"testing"

	"banger/internal/api"
)

func TestPrintVMSpecLineWithAllFields(t *testing.T) {
	vcpu, mem := 2, 2048
	params := api.VMCreateParams{
		VCPUCount:    &vcpu,
		MemoryMiB:    &mem,
		WorkDiskSize: "8G",
	}
	var buf bytes.Buffer
	printVMSpecLine(&buf, params)
	got := buf.String()
	for _, want := range []string{"spec:", "2 vcpu", "2048 MiB", "8G"} {
		if !strings.Contains(got, want) {
			t.Errorf("output missing %q:\n%s", want, got)
		}
	}
	if !strings.HasSuffix(got, "\n") {
		t.Error("spec line should terminate with newline")
	}
}

func TestPrintVMSpecLineFallsBackToBuiltinsOnNilFields(t *testing.T) {
	// Empty params — the printer reaches for DefaultVCPUCount /
	// DefaultMemoryMiB / DefaultWorkDiskSize so output is still sane.
	var buf bytes.Buffer
	printVMSpecLine(&buf, api.VMCreateParams{})
	got := buf.String()
	// Not asserting exact values — just that it produced a plausible
	// line with the three labels.
	for _, want := range []string{"spec:", "vcpu", "MiB", "disk"} {
		if !strings.Contains(got, want) {
			t.Errorf("output missing %q:\n%s", want, got)
		}
	}
}

func TestPrintVMSpecLineIgnoresUnparseableDiskSize(t *testing.T) {
	// Falls back to builtin default; must not panic or print garbage.
	var buf bytes.Buffer
	printVMSpecLine(&buf, api.VMCreateParams{WorkDiskSize: "not-a-size"})
	if !strings.Contains(buf.String(), "spec:") {
		t.Errorf("expected spec line even with bad input, got %q", buf.String())
	}
}
