package service

import (
	"strings"
	"testing"
	"time"

	"metis/pkg/inventory"
)

func TestRemoteHelperBranches(t *testing.T) {
	if got := prettyDeviceTarget(""); got != "the selected target" {
		t.Fatalf("prettyDeviceTarget empty = %q", got)
	}
	if got := prettyDeviceTarget("hosttmp:///tmp"); got != "/tmp" {
		t.Fatalf("prettyDeviceTarget hosttmp = %q", got)
	}
	if got := ramp(0, 10, 20, 1, 2); got != 1 {
		t.Fatalf("ramp before start = %v", got)
	}
	if got := mountedHostTmpDir("/tmp/metis-flash-test"); got != "/host-tmp/metis-flash-test" {
		t.Fatalf("mountedHostTmpDir = %q", got)
	}
	if got := shellQuote("a'b"); got != `'a'"'"'b'` {
		t.Fatalf("shellQuote = %q", got)
	}
	if got, msg := buildStageHeartbeat("n1", "b1", 5*time.Second); got < 8 || !strings.Contains(msg, "Scheduling") {
		t.Fatalf("buildStageHeartbeat early = %v %q", got, msg)
	}
	if got, msg := flashStageHeartbeat("h1", "artifact", 15*time.Second); got < 88 || !strings.Contains(msg, "Writing") {
		t.Fatalf("flashStageHeartbeat = %v %q", got, msg)
	}

	app := newTestApp(t)
	app.artifactStore["n1"] = ArtifactSummary{Node: "n1", Ref: "registry.example/metis/n1:latest"}
	if got := app.remoteArtifactNote("n1"); got != "registry.example/metis/n1:latest" {
		t.Fatalf("remoteArtifactNote = %q", got)
	}
	if got := inventoryNodeArch(&inventory.NodeSpec{}, &inventory.NodeClass{Arch: "amd64"}); got != "amd64" {
		t.Fatalf("inventoryNodeArch = %q", got)
	}
	worker := remoteWorkerEntrypoint(true, "--node", "n1")
	if !strings.Contains(worker, "metis-runtime-env.sh") || !strings.Contains(worker, "metis-ssh-env.sh") {
		t.Fatalf("remoteWorkerEntrypoint missing expected sources: %s", worker)
	}
}

func TestSelectBuilderHostPrefersWorkerAndArch(t *testing.T) {
	kube := fakeKubeServer(t)
	installKubeFactory(t, kube)
	app := newTestApp(t)
	node, err := app.selectBuilderHost("arm64", "titan-22")
	if err != nil {
		t.Fatalf("selectBuilderHost: %v", err)
	}
	if node.Name != "titan-22" {
		t.Fatalf("expected titan-22 builder, got %s", node.Name)
	}
}

func TestSelectBuilderHostErrorBranch(t *testing.T) {
	kube := fakeKubeServer(t)
	installKubeFactory(t, kube)
	app := newTestApp(t)
	if _, err := app.selectBuilderHost("s390x", "titan-22"); err == nil {
		t.Fatal("expected selectBuilderHost error")
	}
}
