package storage

import (
	"github.com/stretchr/testify/assert"
	"os"
	"testing"
	"time"
)

func TestLocaleFileSystemReadFromFolder(t *testing.T) {
	dname, err := os.MkdirTemp("", "sampledir")
	assert.Nil(t, err, "should not throw error")

	d1 := []byte("hello\ngo\n")

	err = os.WriteFile(dname+"/file1", d1, 0644)
	assert.Nil(t, err, "should not throw error")
	err = os.WriteFile(dname+"/file", d1, 0644)
	assert.Nil(t, err, "should not throw error")

	config := make(map[string]string)
	config["path"] = dname

	lfs := LocaleFileSystem{Config: config}
	files, err := lfs.ListFiles()
	assert.Nil(t, err, "should get files without throwing error")

	filelistFromLFS := []string{}
	for _, f := range files {
		name, err := f.GetName()
		assert.Nil(t, err, "should not get error")
		filelistFromLFS = append(filelistFromLFS, name)
	}

	fileListWanted := []string{dname + "/file", dname + "/file1"}
	assert.Equal(t, fileListWanted, filelistFromLFS, "should get currect files")

	os.RemoveAll(dname)
}

func TestLocaleFileSystemDeleteFile(t *testing.T) {
	dname, err := os.MkdirTemp("", "sampledir")
	assert.Nil(t, err, "should not throw error")

	d1 := []byte("hello\ngo\n")

	err = os.WriteFile(dname+"/file1", d1, 0644)
	assert.Nil(t, err, "should not throw error")

	f := GeneralFile{
		name: dname + "/file1",
		time: time.Time{},
	}
	FileToDelete := []File{f}

	config := make(map[string]string)
	config["path"] = dname

	lfs := LocaleFileSystem{Config: config}

	files, err := lfs.ListFiles()
	assert.Nil(t, err, "should not throw error")
	assert.Equal(t, 1, len(files), "should found one file")

	err = lfs.Delete(FileToDelete)
	assert.Nil(t, err, "should not throw error")

	files, err = lfs.ListFiles()
	assert.Nil(t, err, "should not throw error")
	assert.Equal(t, 0, len(files), "should found zero file")

	os.RemoveAll(dname)
}
