package common_test

import (
	"testing"

	"go.uber.org/mock/gomock"
	mock_common "src.opensuse.org/autogits/common/mock"
	"src.opensuse.org/autogits/common"
	"src.opensuse.org/autogits/common/gitea-generated/models"
)

func TestConfigWorkflowParser(t *testing.T) {
	tests := []struct {
		name        string
		config_json string
		repo        models.Repository
	}{
		{
			name: "Regular workflow file",
			config_json: `{
    "Workflows": ["direct", "pr"],
    "Organization": "testing",
    "ReviewGroups": [
        {
            "Name": "gnuman1",
            "Reviewers": ["adamm"]
        }
    ]
}`,
			repo: models.Repository{
				DefaultBranch: "master",
			},
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			ctl := gomock.NewController(t)
			gitea := mock_common.NewMockGiteaFileContentAndRepoFetcher(ctl)
			gitea.EXPECT().GetRepositoryFileContent("foo", "bar", "", "workflow.config").Return([]byte(test.config_json), "abc", nil)
			gitea.EXPECT().GetRepository("foo", "bar").Return(&test.repo, nil)

			_, err := common.ReadWorkflowConfig(gitea, "foo/bar")
			if err != nil {
				t.Fatal(err)
			}
		})
	}
}
