package installpackage_test

import (
	"testing"

	"github.com/aquaproj/aqua/v2/pkg/installpackage"
	"github.com/spf13/afero"
)

func TestMockLinker_Lstat(t *testing.T) {
	t.Parallel()
	data := []struct {
		name  string
		files map[string]string
		src   string
		isErr bool
	}{
		{
			name: "file found",
			files: map[string]string{
				pathHomeFooFoo: pkgFoo,
			},
			src: pkgFoo,
		},
		{
			name:  "file isn't found",
			files: map[string]string{},
			src:   pkgFoo,
			isErr: true,
		},
	}
	for _, d := range data {
		t.Run(d.name, func(t *testing.T) {
			t.Parallel()
			linker := installpackage.NewMockLinker(afero.NewMemMapFs())
			for dest, src := range d.files {
				if err := linker.Symlink(dest, src); err != nil {
					t.Fatal(err)
				}
			}

			_, err := linker.Lstat(d.src)
			if err != nil {
				if d.isErr {
					return
				}
				t.Fatal(err)
			}
			if d.isErr {
				t.Fatal("error must be returned")
			}
		})
	}
}

func TestMockLinker_Readlink(t *testing.T) {
	t.Parallel()
	data := []struct {
		name  string
		files map[string]string
		src   string
		exp   string
		isErr bool
	}{
		{
			name: "file found",
			files: map[string]string{
				pathHomeFooFoo: pkgFoo,
			},
			src: pkgFoo,
			exp: pathHomeFooFoo,
		},
		{
			name:  "file isn't found",
			src:   pkgFoo,
			isErr: true,
		},
	}
	for _, d := range data {
		t.Run(d.name, func(t *testing.T) {
			t.Parallel()
			linker := installpackage.NewMockLinker(afero.NewMemMapFs())
			for dest, src := range d.files {
				if err := linker.Symlink(dest, src); err != nil {
					t.Fatal(err)
				}
			}

			dest, err := linker.Readlink(d.src)
			if err != nil {
				if d.isErr {
					return
				}
				t.Fatal(err)
			}
			if d.isErr {
				t.Fatal("error must be returned")
			}
			if dest != d.exp {
				t.Fatalf("wanted %s, got %s", d.exp, dest)
			}
		})
	}
}
