package sborepo

import (
	"path/filepath"
	"strings"
	"testing"

	"gitlab.com/M0M097/pkg/internal/filecheck"
	"gitlab.com/M0M097/pkg/internal/must"
)

func TestCreate(t *testing.T) {
	td := t.TempDir()
	categoryPath := filepath.Join(td, "category")
	pkg := PackageCreator{
		Name:              DUMMY_PKG1,
		Path:              categoryPath,
		Version:           DUMMY_VERSION,
		Download:          DUMMY_DOWNLOAD_URL,
		MD5Sum:            DUMMY_MD5SUM,
		Download64:        DUMMY_DOWNLOAD_URL64,
		MD5Sum64:          DUMMY_MD5SUM64,
		Requires:          strings.Join([]string{DUMMY_PKG2, DUMMY_PKG3}, " "),
		SlackBuildContent: MOCK_PKG_SLACKBUILD_CONTENT,
		READMEContent:     MOCK_PKG_README_CONTENT,
		SlackDescContent:  MOCK_PKG_SLACK_DES_CONTENT,
	}.Create()
	mustBeEqual(t, filecheck.Exists(pkg.SlackBuild()), true)
	mustBeEqual(t, filecheck.Exists(pkg.Readme()), true)
	mustBeEqual(t, filecheck.Exists(pkg.InfoFile()), true)
	mustBeEqual(t, filecheck.Exists(pkg.SlackDesc()), true)

	mustBeEqual(t, pkg.Name, DUMMY_PKG1)
	mustBeEqual(t, pkg.ParentPath, categoryPath)
	mustBeEqual(t, pkg.Path(), filepath.Join(categoryPath, pkg.Name))

	// Check the contents of the info file
	mustBeEqual(t, pkg.Version(), DUMMY_VERSION)
	mustBeEqualStringSlices(t, pkg.MD5Sums(), []string{DUMMY_MD5SUM64})
	mustBeEqualStringSlices(t, pkg.DownloadURLs(), []string{DUMMY_DOWNLOAD_URL64})
	mustBeEqualStringSlices(t, pkg.Dependencies(), []string{DUMMY_PKG2, DUMMY_PKG3})

	// Check the contents of the SlackBuild, README, and slack-desc files
	mustBeEqual(t, string(must.ReadFile(pkg.SlackBuild())), MOCK_PKG_SLACKBUILD_CONTENT)
	mustBeEqual(t, string(must.ReadFile(pkg.Readme())), MOCK_PKG_README_CONTENT)
	mustBeEqual(t, string(must.ReadFile(pkg.SlackDesc())), MOCK_PKG_SLACK_DES_CONTENT)

}
