diff --git a/internal/operations/account_test.go b/internal/operations/account_test.go index 622dc2c0..adb3e526 100644 --- a/internal/operations/account_test.go +++ b/internal/operations/account_test.go @@ -7,6 +7,7 @@ import ( "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/operations" "github.com/alist-org/alist/v3/internal/store" + "github.com/alist-org/alist/v3/pkg/utils" "gorm.io/driver/sqlite" "gorm.io/gorm" ) @@ -39,3 +40,34 @@ func TestCreateAccount(t *testing.T) { } } } + +func TestGetAccountVirtualFilesByPath(t *testing.T) { + Setup(t) + virtualFiles := operations.GetAccountVirtualFilesByPath("/a") + var names []string + for _, virtualFile := range virtualFiles { + names = append(names, virtualFile.GetName()) + } + var expectedNames = []string{"b", "c", "d"} + if utils.SliceEqual(names, expectedNames) { + t.Logf("passed") + } else { + t.Errorf("expected: %+v, got: %+v", expectedNames, names) + } +} + +func Setup(t *testing.T) { + var accounts = []model.Account{ + {Driver: "Local", VirtualPath: "/a/b", Index: 0, Addition: "{}"}, + {Driver: "Local", VirtualPath: "/a/c", Index: 1, Addition: "{}"}, + {Driver: "Local", VirtualPath: "/a/d", Index: 2, Addition: "{}"}, + {Driver: "Local", VirtualPath: "/a/d/e", Index: 3, Addition: "{}"}, + {Driver: "Local", VirtualPath: "/a/d/e.balance", Index: 4, Addition: "{}"}, + } + for _, account := range accounts { + err := operations.CreateAccount(context.Background(), account) + if err != nil { + t.Fatalf("failed to create account: %+v", err) + } + } +} diff --git a/pkg/utils/slice.go b/pkg/utils/slice.go new file mode 100644 index 00000000..9d9e0d52 --- /dev/null +++ b/pkg/utils/slice.go @@ -0,0 +1,13 @@ +package utils + +func SliceEqual[T comparable](a, b []T) bool { + if len(a) != len(b) { + return false + } + for i, v := range a { + if v != b[i] { + return false + } + } + return true +}