package main

import (
	"strings"
	"testing"

	"go.uber.org/mock/gomock"

	"src.opensuse.org/autogits/common"
	mock_common "src.opensuse.org/autogits/common/mock"
)

func TestLinkParsing(t *testing.T) {
	tests := []struct {
		name         string
		data         []byte
		links        []*PackageRebaseLink
		expected_err string
	}{
		{
			name: "No links file",
		},
		{
			name: "Empty links file",
			data: []byte("[]"),
		},
		{
			name: "Single package linked",
			data: []byte(`[{"Pkg": "foo"}]`),
			links: []*PackageRebaseLink{
				&PackageRebaseLink{
					Pkg: "foo",
				},
			},
		},
		{
			name: "Multiple packages linked, resorted",
			data: []byte(`[{"Pkg": "foo"}, {"Pkg": "abc"}, {"Pkg": "aaa", "SourceBranch": "q"}]`),
			links: []*PackageRebaseLink{
				&PackageRebaseLink{
					Pkg:          "aaa",
					SourceBranch: "q",
				},
				&PackageRebaseLink{
					Pkg:          "abc",
				},
				&PackageRebaseLink{
					Pkg: "foo",
				},
			},
		},
		{
			name:         "Broken source file",
			data:         []byte(`[{"Pkg": "pool"}, SourceBranch": "pool", "TargetBranch": "abc"}, {"Pkg": "abc"}]`),
			expected_err: "invalid character 'S' looking for beginning of value",
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			ctl := gomock.NewController(t)
			gitea_mock := mock_common.NewMockGitea(ctl)

			gitea_mock.EXPECT().GetRepositoryFileContent("org", "repo", "branch", common.PrjLinksFile).Return(test.data, "", nil)

			res, _, err := ProcessProjectLinks(gitea_mock, "org", "repo", "branch")
			if len(test.expected_err) > 0 && err == nil {
				t.Error("Expected an error:", test.expected_err, "; but got nothing")
			} else if err != nil && len(test.expected_err) == 0 {
				t.Fatal("Unexpected error:", err)
			} else if err != nil && !strings.Contains(err.Error(), test.expected_err) {
				t.Fatal("Expected an error:", test.expected_err, "; but got:", err)
			}

			if len(res) != len(test.links) {
				t.Fatal("len of result", len(res), "vs. expected", len(test.links))
			}

			// verify that returned data is present and in expected order
			for i := range test.links {
				if *test.links[i] != *res[i] {
					t.Error("index", i, "expected", *test.links[i], "received", *res[i])
				}
			}
		})
	}
}
/*
func TestLinkUpdater(t *testing.T) {
	tests := []struct {
		name       string
		mock_setup func(*mock_common.MockGit, *mock_common.MockGitea)
		link       *OrgLinks
	}{
		{
			name: "no-op update",
			mock_setup: func(git *mock_common.MockGit, gitea *mock_common.MockGitea) {

				git.EXPECT().GitExecWithOutputOrPanic(gomock.Any(), "rev-list", "upstream/main", "^pkg_hash").Return("")
			},
			link: &OrgLinks{
				Pkg: "pkg",
//				parentOrg: "rorg",
//				parentRepo: "rpkg",
			},
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			ctl := gomock.NewController(t)
			git := mock_common.NewMockGit(ctl)
			gitea := mock_common.NewMockGitea(ctl)

			test.mock_setup(git, gitea)
			test.link.StateUpdater(git, gitea)
		})
	}
}
*/
