2023-12-01 23:47:00 +00:00
|
|
|
package files
|
2023-09-30 14:18:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2023-12-01 23:47:00 +00:00
|
|
|
|
|
|
|
"github.com/gtsteffaniak/filebrowser/settings"
|
2023-09-30 14:18:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkFillIndex(b *testing.B) {
|
2024-11-26 17:21:41 +00:00
|
|
|
InitializeIndex(false)
|
2023-12-01 23:47:00 +00:00
|
|
|
si := GetIndex(settings.Config.Server.Root)
|
2023-09-30 14:18:21 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2023-12-01 23:47:00 +00:00
|
|
|
si.createMockData(50, 3) // 1000 dirs, 3 files per dir
|
2023-09-30 14:18:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-01 23:47:00 +00:00
|
|
|
func (si *Index) createMockData(numDirs, numFilesPerDir int) {
|
2023-09-30 14:18:21 +00:00
|
|
|
for i := 0; i < numDirs; i++ {
|
2024-11-21 00:15:30 +00:00
|
|
|
dirPath := generateRandomPath(rand.Intn(3) + 1)
|
2024-11-26 17:21:41 +00:00
|
|
|
files := []ItemInfo{} // Slice of FileInfo
|
2024-10-07 22:44:53 +00:00
|
|
|
|
|
|
|
// Simulating files and directories with FileInfo
|
2023-09-30 14:18:21 +00:00
|
|
|
for j := 0; j < numFilesPerDir; j++ {
|
2024-11-26 17:21:41 +00:00
|
|
|
newFile := ItemInfo{
|
2024-10-07 22:44:53 +00:00
|
|
|
Name: "file-" + getRandomTerm() + getRandomExtension(),
|
|
|
|
Size: rand.Int63n(1000), // Random size
|
|
|
|
ModTime: time.Now().Add(-time.Duration(rand.Intn(100)) * time.Hour), // Random mod time
|
2024-11-21 00:15:30 +00:00
|
|
|
Type: "blob",
|
2023-12-01 23:47:00 +00:00
|
|
|
}
|
|
|
|
files = append(files, newFile)
|
2023-09-30 14:18:21 +00:00
|
|
|
}
|
2024-11-21 00:15:30 +00:00
|
|
|
dirInfo := &FileInfo{
|
|
|
|
Path: dirPath,
|
|
|
|
Files: files,
|
2024-10-07 22:44:53 +00:00
|
|
|
}
|
2024-11-21 00:15:30 +00:00
|
|
|
|
|
|
|
si.UpdateMetadata(dirInfo)
|
2023-09-30 14:18:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRandomPath(levels int) string {
|
|
|
|
rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
dirName := "srv"
|
|
|
|
for i := 0; i < levels; i++ {
|
|
|
|
dirName += "/" + getRandomTerm()
|
|
|
|
}
|
|
|
|
return dirName
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRandomTerm() string {
|
|
|
|
wordbank := []string{
|
|
|
|
"hi", "test", "other", "name",
|
|
|
|
"cool", "things", "more", "items",
|
|
|
|
}
|
|
|
|
rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
|
|
|
index := rand.Intn(len(wordbank))
|
|
|
|
return wordbank[index]
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRandomExtension() string {
|
|
|
|
wordbank := []string{
|
|
|
|
".txt", ".mp3", ".mov", ".doc",
|
|
|
|
".mp4", ".bak", ".zip", ".jpg",
|
|
|
|
}
|
|
|
|
rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
index := rand.Intn(len(wordbank))
|
|
|
|
return wordbank[index]
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRandomSearchTerms(numTerms int) []string {
|
|
|
|
// Generate random search terms
|
|
|
|
searchTerms := make([]string, numTerms)
|
|
|
|
for i := 0; i < numTerms; i++ {
|
|
|
|
searchTerms[i] = getRandomTerm()
|
|
|
|
}
|
|
|
|
return searchTerms
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSONBytesEqual compares the JSON in two byte slices.
|
|
|
|
func JSONBytesEqual(a, b []byte) (bool, error) {
|
|
|
|
var j, j2 interface{}
|
|
|
|
if err := json.Unmarshal(a, &j); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(b, &j2); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return reflect.DeepEqual(j2, j), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetIndex(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
want *map[string][]string
|
|
|
|
}{
|
|
|
|
// TODO: Add test cases.
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-12-01 23:47:00 +00:00
|
|
|
if got := GetIndex("root"); !reflect.DeepEqual(got, tt.want) {
|
2023-09-30 14:18:21 +00:00
|
|
|
t.Errorf("GetIndex() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-11-27 13:30:03 +00:00
|
|
|
|
|
|
|
func TestMakeIndexPath(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
subPath string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"Root path returns slash", "/", "/"},
|
|
|
|
{"Dot-prefixed returns slash", ".", "/"},
|
|
|
|
{"Double-dot prefix ignored", "./", "/"},
|
|
|
|
{"Dot prefix followed by text", "./test", "/test"},
|
|
|
|
{"Dot prefix followed by text", ".test", "/.test"},
|
|
|
|
{"Hidden file at root", "/.test", "/.test"},
|
|
|
|
{"Trailing slash removed", "/test/", "/test"},
|
|
|
|
{"Subpath without root prefix", "/other/test", "/other/test"},
|
|
|
|
{"Complex nested paths", "/nested/path", "/nested/path"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
si := &Index{Root: "/"}
|
|
|
|
result := si.makeIndexPath(tt.subPath)
|
|
|
|
if result != tt.expected {
|
|
|
|
t.Errorf("makeIndexPath(%q) = %q; want %q", tt.name, result, tt.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|