package myList_test

import "seekia/internal/myDatastores/myList"
import "seekia/internal/appUsers"
import "seekia/internal/helpers"
import "seekia/internal/localFilesystem"

import "testing"
import "slices"

//TODO: Add concurrency tests

func TestMyList(t *testing.T){

	err := localFilesystem.InitializeAppDatastores()
	if (err != nil){
		t.Fatal("Failed to initialize app datastores: " + err.Error())
	}
	err = appUsers.InitializeAppUserForTests(false, false)
	if (err != nil){
		t.Fatal("Failed to initalize app user for tests: " + err.Error())
	}

	newList := make([]string, 0, 100)

	for i:=1; i < 100; i++{

		newItem := helpers.ConvertIntToString(i)
	
		newList = append(newList, newItem)
	}

	newListObject, err := myList.CreateNewList("TestList")
	if (err != nil){
		t.Fatal("Failed to create new myList object: " + err.Error())
	}

	for _, item := range newList{

		err := newListObject.AddListItem(item)
		if (err != nil){
			t.Fatal("Failed to add list item: " + err.Error())
		}
	}

	retrievedList, err := newListObject.GetList()
	if (err != nil){
		t.Fatal("Failed to get list: " + err.Error())
	}

	areIdentical := slices.Equal(newList, retrievedList)
	if (areIdentical == false){
		t.Fatal("Stored list does not match retrieved list.")
	}

	err = newListObject.DeleteListItem("1")
	if (err != nil){
		t.Fatal("Failed to delete list item: " + err.Error())
	}

	retrievedList, err = newListObject.GetList()
	if (err != nil){
		t.Fatal("Failed to get list: " + err.Error())
	}

	for index, element := range retrievedList{

		expectedValue := newList[index+1]
		
		if (element != expectedValue){
			t.Fatal("Retrieved list does not match after deleting item.")
		}
	}

	err = newListObject.DeleteList()
	if (err != nil){
		t.Fatal("Failed to delete list: " + err.Error())
	}
}




