package service

import (
	"errors"
	"strings"
	"testing"
)

func TestReserveJobRejectsDuplicateActiveNodeJobs(t *testing.T) {
	app := newTestApp(t)
	active := app.newJob("replace", "titan-15", "titan-22", "/dev/sdk")

	_, err := app.reserveJob("build", "titan-15", "", "")
	if err == nil {
		t.Fatal("expected duplicate job reservation to fail")
	}
	var activeErr *activeNodeJobError
	if !errors.As(err, &activeErr) {
		t.Fatalf("expected activeNodeJobError, got %T", err)
	}
	if activeErr.JobID != active.ID || activeErr.Kind != "replace" {
		t.Fatalf("unexpected active job conflict: %#v", activeErr)
	}
	if !strings.Contains(err.Error(), active.ID) {
		t.Fatalf("expected error to mention active job id %s, got %q", active.ID, err.Error())
	}
}
