package buildinfo

import "testing"

func TestResolve(t *testing.T) {
	tests := []struct {
		name         string
		override     string
		buildVersion string
		want         string
		wantErr      bool
	}{
		{name: "stable main module version", buildVersion: "v1.2.3", want: "v1.2.3"},
		{name: "zero version", buildVersion: "v0.0.0", want: "v0.0.0"},
		{name: "override takes precedence", override: "v2.3.4", buildVersion: "v1.2.3", want: "v2.3.4"},
		{name: "invalid override", override: "version", buildVersion: "v1.2.3", wantErr: true},
		{name: "override with whitespace", override: " v1.2.3", wantErr: true},
		{name: "leading zero major", buildVersion: "v01.2.3", want: "development"},
		{name: "leading zero minor", buildVersion: "v1.02.3", want: "development"},
		{name: "leading zero patch", buildVersion: "v1.2.03", want: "development"},
		{name: "build version with whitespace", buildVersion: "v1.2.3 ", want: "development"},
		{name: "prerelease", buildVersion: "v1.2.3-rc.1", want: "development"},
		{name: "build suffix", buildVersion: "v1.2.3+build.1", want: "development"},
		{name: "pseudo version", buildVersion: "v0.0.0-20260102030405-abcdef123456", want: "development"},
		{name: "development build", buildVersion: "(devel)", want: "development"},
		{name: "missing build information", want: "development"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := resolve(tt.override, tt.buildVersion)
			if tt.wantErr {
				if err == nil {
					t.Fatal("resolve() error = nil, want error")
				}
				return
			}
			if err != nil {
				t.Fatalf("resolve() error = %v", err)
			}
			if got != tt.want {
				t.Fatalf("resolve() = %q, want %q", got, tt.want)
			}
		})
	}
}
