package trivy

import (
	"testing"
)

func TestParseRepoURL(t *testing.T) {
	testcases := []struct {
		URL          string
		ExpectedHost string
		ExpectedPath string
	}{
		{"https://github.com/pingcap/tidb.git#branch@master", "github.com", "/pingcap/tidb"},
		{"http://github.com/pingcap/tidb.git#branch@master", "github.com", "/pingcap/tidb"},
		{"https://github.com/pingcap/tidb#branch@master", "github.com", "/pingcap/tidb"},
		{"http://github.com/pingcap/tidb#branch@master", "github.com", "/pingcap/tidb"},
		{"github.com/pingcap/tidb.git#branch@master", "github.com", "/pingcap/tidb"},
		{"github.com/pingcap/tidb#branch@master", "github.com", "/pingcap/tidb"},
	}

	for _, tc := range testcases {
		t.Run(tc.URL, func(t *testing.T) {
			if repoURL, err := parseRepoURL(tc.URL); err != nil {
				t.Fatalf("Test parseRepoURL(\"%s\") failed, %+v", tc.URL, err)
			} else {
				if repoURL.Host != tc.ExpectedHost || repoURL.Path != tc.ExpectedPath {
					t.Fatalf("Test parseRepoURL(\"%s\") failed, expected: host(%s) path(%s), but host(%s) path(%s) got", tc.URL,
						tc.ExpectedHost, tc.ExpectedPath,
						repoURL.Host, repoURL.Path,
					)
				}
			}
		})
	}
}
