2024-09-16 21:01:16 +00:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2025-01-05 19:05:33 +00:00
|
|
|
"github.com/gtsteffaniak/filebrowser/backend/settings"
|
2024-10-07 22:44:53 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2024-09-16 21:01:16 +00:00
|
|
|
|
|
|
|
var testIndex Index
|
|
|
|
|
2024-10-07 22:44:53 +00:00
|
|
|
// Test for GetFileMetadata// Test for GetFileMetadata
|
|
|
|
func TestGetFileMetadataSize(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
adjustedPath string
|
|
|
|
expectedName string
|
|
|
|
expectedSize int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "testpath exists",
|
|
|
|
adjustedPath: "/testpath",
|
|
|
|
expectedName: "testfile.txt",
|
|
|
|
expectedSize: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "testpath exists",
|
|
|
|
adjustedPath: "/testpath",
|
|
|
|
expectedName: "directory",
|
|
|
|
expectedSize: 100,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-11-21 00:15:30 +00:00
|
|
|
fileInfo, _ := testIndex.GetReducedMetadata(tt.adjustedPath, true)
|
2024-10-07 22:44:53 +00:00
|
|
|
// Iterate over fileInfo.Items to look for expectedName
|
2024-11-26 17:21:41 +00:00
|
|
|
for _, item := range fileInfo.Files {
|
2024-10-07 22:44:53 +00:00
|
|
|
// Assert the existence and the name
|
|
|
|
if item.Name == tt.expectedName {
|
|
|
|
assert.Equal(t, tt.expectedSize, item.Size)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for GetFileMetadata// Test for GetFileMetadata
|
|
|
|
func TestGetFileMetadata(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
adjustedPath string
|
|
|
|
expectedName string
|
|
|
|
expectedExists bool
|
2024-11-21 00:15:30 +00:00
|
|
|
isDir bool
|
2024-10-07 22:44:53 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "testpath exists",
|
2024-11-21 00:15:30 +00:00
|
|
|
adjustedPath: "/testpath/testfile.txt",
|
2024-10-07 22:44:53 +00:00
|
|
|
expectedName: "testfile.txt",
|
|
|
|
expectedExists: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "testpath not exists",
|
2024-11-21 00:15:30 +00:00
|
|
|
adjustedPath: "/testpath/nonexistent.txt",
|
2024-10-07 22:44:53 +00:00
|
|
|
expectedName: "nonexistent.txt",
|
|
|
|
expectedExists: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "File exists in /anotherpath",
|
2024-11-21 00:15:30 +00:00
|
|
|
adjustedPath: "/anotherpath/afile.txt",
|
2024-10-07 22:44:53 +00:00
|
|
|
expectedName: "afile.txt",
|
|
|
|
expectedExists: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "File does not exist in /anotherpath",
|
2024-11-21 00:15:30 +00:00
|
|
|
adjustedPath: "/anotherpath/nonexistentfile.txt",
|
2024-10-07 22:44:53 +00:00
|
|
|
expectedName: "nonexistentfile.txt",
|
|
|
|
expectedExists: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Directory does not exist",
|
|
|
|
adjustedPath: "/nonexistentpath",
|
|
|
|
expectedName: "",
|
|
|
|
expectedExists: false,
|
2024-11-21 00:15:30 +00:00
|
|
|
isDir: true,
|
2024-10-07 22:44:53 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-11-26 17:21:41 +00:00
|
|
|
fileInfo, exists := testIndex.GetReducedMetadata(tt.adjustedPath, tt.isDir)
|
|
|
|
if !exists {
|
2024-11-21 00:15:30 +00:00
|
|
|
found := false
|
|
|
|
assert.Equal(t, tt.expectedExists, found)
|
|
|
|
return
|
|
|
|
}
|
2024-10-07 22:44:53 +00:00
|
|
|
found := false
|
2024-11-21 00:15:30 +00:00
|
|
|
if tt.isDir {
|
|
|
|
// Iterate over fileInfo.Items to look for expectedName
|
2024-11-26 17:21:41 +00:00
|
|
|
for _, item := range fileInfo.Files {
|
2024-11-21 00:15:30 +00:00
|
|
|
// Assert the existence and the name
|
|
|
|
if item.Name == tt.expectedName {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if fileInfo.Name == tt.expectedName {
|
2024-10-07 22:44:53 +00:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
2024-11-21 00:15:30 +00:00
|
|
|
|
2024-10-07 22:44:53 +00:00
|
|
|
assert.Equal(t, tt.expectedExists, found)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-09-16 21:01:16 +00:00
|
|
|
|
|
|
|
// Test for UpdateFileMetadata
|
|
|
|
func TestUpdateFileMetadata(t *testing.T) {
|
2024-11-21 00:15:30 +00:00
|
|
|
info := &FileInfo{
|
|
|
|
Path: "/testpath",
|
2024-11-26 17:21:41 +00:00
|
|
|
Files: []ItemInfo{
|
2024-11-21 00:15:30 +00:00
|
|
|
{Name: "testfile.txt"},
|
|
|
|
{Name: "anotherfile.txt"},
|
2024-09-16 21:01:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-11-21 00:15:30 +00:00
|
|
|
index := &Index{
|
|
|
|
Directories: map[string]*FileInfo{
|
|
|
|
"/testpath": info,
|
|
|
|
},
|
|
|
|
}
|
2024-09-16 21:01:16 +00:00
|
|
|
|
2024-11-21 00:15:30 +00:00
|
|
|
success := index.UpdateMetadata(info)
|
2024-09-16 21:01:16 +00:00
|
|
|
if !success {
|
|
|
|
t.Fatalf("expected UpdateFileMetadata to succeed")
|
|
|
|
}
|
|
|
|
|
2024-11-21 00:15:30 +00:00
|
|
|
fileInfo, exists := index.GetReducedMetadata("/testpath/testfile.txt", false)
|
|
|
|
if !exists || fileInfo.Name != "testfile.txt" {
|
|
|
|
t.Fatalf("expected testfile.txt to be updated in the directory metadata:%v %v", exists, info.Name)
|
2024-09-16 21:01:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for GetDirMetadata
|
|
|
|
func TestGetDirMetadata(t *testing.T) {
|
|
|
|
t.Parallel()
|
2024-11-21 00:15:30 +00:00
|
|
|
_, exists := testIndex.GetReducedMetadata("/testpath", true)
|
2024-09-16 21:01:16 +00:00
|
|
|
if !exists {
|
|
|
|
t.Fatalf("expected GetDirMetadata to return initialized metadata map")
|
|
|
|
}
|
|
|
|
|
2024-11-21 00:15:30 +00:00
|
|
|
_, exists = testIndex.GetReducedMetadata("/nonexistent", true)
|
2024-09-16 21:01:16 +00:00
|
|
|
if exists {
|
|
|
|
t.Fatalf("expected GetDirMetadata to return false for nonexistent directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for SetDirectoryInfo
|
|
|
|
func TestSetDirectoryInfo(t *testing.T) {
|
|
|
|
index := &Index{
|
2024-11-21 00:15:30 +00:00
|
|
|
Directories: map[string]*FileInfo{
|
2024-09-16 21:01:16 +00:00
|
|
|
"/testpath": {
|
2024-11-21 00:15:30 +00:00
|
|
|
Path: "/testpath",
|
2024-11-26 17:21:41 +00:00
|
|
|
ItemInfo: ItemInfo{
|
|
|
|
Name: "testpath",
|
|
|
|
Type: "directory",
|
|
|
|
},
|
|
|
|
Files: []ItemInfo{
|
2024-10-07 22:44:53 +00:00
|
|
|
{Name: "testfile.txt"},
|
|
|
|
{Name: "anotherfile.txt"},
|
2024-09-16 21:01:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-11-21 00:15:30 +00:00
|
|
|
dir := &FileInfo{
|
|
|
|
Path: "/newPath",
|
2024-11-26 17:21:41 +00:00
|
|
|
ItemInfo: ItemInfo{
|
|
|
|
Name: "newPath",
|
|
|
|
Type: "directory",
|
|
|
|
},
|
|
|
|
Files: []ItemInfo{
|
2024-10-07 22:44:53 +00:00
|
|
|
{Name: "testfile.txt"},
|
|
|
|
},
|
|
|
|
}
|
2024-11-21 00:15:30 +00:00
|
|
|
index.UpdateMetadata(dir)
|
2024-09-16 21:01:16 +00:00
|
|
|
storedDir, exists := index.Directories["/newPath"]
|
2024-11-26 17:21:41 +00:00
|
|
|
if !exists || storedDir.Files[0].Name != "testfile.txt" {
|
2024-09-16 21:01:16 +00:00
|
|
|
t.Fatalf("expected SetDirectoryInfo to store directory info correctly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for RemoveDirectory
|
|
|
|
func TestRemoveDirectory(t *testing.T) {
|
|
|
|
index := &Index{
|
2024-11-21 00:15:30 +00:00
|
|
|
Directories: map[string]*FileInfo{
|
2024-09-16 21:01:16 +00:00
|
|
|
"/testpath": {},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
index.RemoveDirectory("/testpath")
|
|
|
|
_, exists := index.Directories["/testpath"]
|
|
|
|
if exists {
|
|
|
|
t.Fatalf("expected directory to be removed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
testIndex = Index{
|
2025-01-05 19:05:33 +00:00
|
|
|
Source: settings.Source{
|
|
|
|
Path: "/",
|
|
|
|
Name: "test",
|
|
|
|
},
|
2024-11-26 17:21:41 +00:00
|
|
|
NumFiles: 10,
|
|
|
|
NumDirs: 5,
|
2024-11-21 00:15:30 +00:00
|
|
|
Directories: map[string]*FileInfo{
|
2024-09-16 21:01:16 +00:00
|
|
|
"/testpath": {
|
2024-11-21 00:15:30 +00:00
|
|
|
Path: "/testpath",
|
2024-11-26 17:21:41 +00:00
|
|
|
ItemInfo: ItemInfo{
|
|
|
|
Name: "testpath",
|
|
|
|
Type: "directory",
|
|
|
|
},
|
|
|
|
Files: []ItemInfo{
|
2024-10-07 22:44:53 +00:00
|
|
|
{Name: "testfile.txt", Size: 100},
|
|
|
|
{Name: "anotherfile.txt", Size: 100},
|
2024-09-16 21:01:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"/anotherpath": {
|
2024-11-21 00:15:30 +00:00
|
|
|
Path: "/anotherpath",
|
2024-11-26 17:21:41 +00:00
|
|
|
ItemInfo: ItemInfo{
|
|
|
|
Name: "anotherpath",
|
|
|
|
Type: "directory",
|
|
|
|
},
|
|
|
|
Files: []ItemInfo{
|
2024-10-07 22:44:53 +00:00
|
|
|
{Name: "afile.txt", Size: 100},
|
2024-09-16 21:01:16 +00:00
|
|
|
},
|
2024-11-26 17:21:41 +00:00
|
|
|
Folders: []ItemInfo{
|
|
|
|
{Name: "directory", Type: "directory", Size: 100},
|
2024-11-21 00:15:30 +00:00
|
|
|
},
|
2024-09-16 21:01:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|