package runtime

import (
	"errors"
	"testing"
)

func TestEngineVersionRegisterValidate(t *testing.T) {
	cases := []struct {
		name    string
		input   RegisterInput
		wantErr error
	}{
		{
			name:    "empty version",
			input:   RegisterInput{Version: "", ImageRef: "img"},
			wantErr: ErrInvalidInput,
		},
		{
			name:    "non-semver",
			input:   RegisterInput{Version: "abc", ImageRef: "img"},
			wantErr: ErrInvalidInput,
		},
		{
			name:    "empty image",
			input:   RegisterInput{Version: "0.1.0", ImageRef: ""},
			wantErr: ErrInvalidInput,
		},
		{
			name:    "valid",
			input:   RegisterInput{Version: "0.1.0", ImageRef: "img"},
			wantErr: nil,
		},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			err := (&c.input).Validate()
			if c.wantErr == nil {
				if err != nil {
					t.Fatalf("unexpected error: %v", err)
				}
				return
			}
			if !errors.Is(err, c.wantErr) {
				t.Fatalf("got %v, want %v", err, c.wantErr)
			}
		})
	}
}

func TestCheckPatchCompatible(t *testing.T) {
	cases := []struct {
		name    string
		current string
		target  string
		wantErr error
	}{
		{"same patch", "0.1.0", "0.1.0", nil},
		{"compatible patch", "0.1.0", "0.1.4", nil},
		{"different minor", "0.1.0", "0.2.0", ErrPatchSemverIncompatible},
		{"different major", "1.0.0", "2.0.0", ErrPatchSemverIncompatible},
		{"invalid current", "abc", "0.1.0", ErrInvalidInput},
		{"invalid target", "0.1.0", "abc", ErrInvalidInput},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			err := CheckPatchCompatible(c.current, c.target)
			if c.wantErr == nil && err != nil {
				t.Fatalf("unexpected error: %v", err)
			}
			if c.wantErr != nil && !errors.Is(err, c.wantErr) {
				t.Fatalf("got %v, want %v", err, c.wantErr)
			}
		})
	}
}
