Files
docspell-cli/mount_test.go
T

122 lines
3.4 KiB
Go

package main
import (
"context"
"path/filepath"
"testing"
"bazil.org/fuse"
)
func TestBuildMountTreeGroupsByFolderAndDate(t *testing.T) {
items := []SearchItem{{
ID: "item-1",
Name: "Invoice/July",
Date: 1720396800000,
Folder: &DocspellEntity{
Name: "Bills/Private",
},
Attachments: []ItemAttachment{{ID: "att-1", Name: "invoice.pdf", Size: 123}},
}}
root := buildMountTree(context.Background(), &DocspellClient{}, items)
folder, ok := root.children["Bills_Private"].(*dirNode)
if !ok {
t.Fatalf("expected sanitized folder directory, got %#v", root.children)
}
year, ok := folder.children["2024"].(*dirNode)
if !ok {
t.Fatalf("expected year directory, got %#v", folder.children)
}
month, ok := year.children["07"].(*dirNode)
if !ok {
t.Fatalf("expected month directory, got %#v", year.children)
}
if _, ok := month.children["Invoice_July.pdf"].(*fileNode); !ok {
t.Fatalf("expected item-named attachment file, got %#v", month.children)
}
}
func TestItemIDFromURLOrID(t *testing.T) {
if got := itemIDFromURLOrID("https://docspell.example/app/item/item-123"); got != "item-123" {
t.Fatalf("expected item-123, got %q", got)
}
if got := itemIDFromURLOrID("item-456"); got != "item-456" {
t.Fatalf("expected raw id, got %q", got)
}
}
func TestItemIDFromLocalByIDPath(t *testing.T) {
mountPath := t.TempDir()
path := filepath.Join(mountPath, "by-id", "it", "item-1.pdf")
got, err := itemIDFromLocalPath(path, config{MountPath: mountPath})
if err != nil {
t.Fatal(err)
}
if got != "item-1" {
t.Fatalf("expected item-1, got %q", got)
}
}
func TestBuildMountTreeDeduplicatesAttachmentNames(t *testing.T) {
items := []SearchItem{{
ID: "item-1",
Name: "Invoice",
Attachments: []ItemAttachment{
{ID: "att-1", Name: "same.pdf"},
{ID: "att-2", Name: "same.pdf"},
},
}}
root := buildMountTree(context.Background(), &DocspellClient{}, items)
folder := root.children["No Folder"].(*dirNode)
date := folder.children["No Date"].(*dirNode)
if _, ok := date.children["Invoice - 01.pdf"]; !ok {
t.Fatalf("expected first unique name, got %#v", date.children)
}
if _, ok := date.children["Invoice - 02.pdf"]; !ok {
t.Fatalf("expected second unique name, got %#v", date.children)
}
}
func TestFileNodeUnknownSizeDoesNotAdvertiseZeroLength(t *testing.T) {
f := &fileNode{size: 0, knownSize: false}
var attr fuse.Attr
if err := f.Attr(context.Background(), &attr); err != nil {
t.Fatal(err)
}
if attr.Size != 0 {
t.Fatalf("expected absent size to remain zero, got %d", attr.Size)
}
if f.knownSize {
t.Fatal("expected unknown size before download")
}
}
func TestBuildMountTreeAddsByIDDirectory(t *testing.T) {
items := []SearchItem{{
ID: "item-1",
Name: "Invoice",
Attachments: []ItemAttachment{
{ID: "att-1", Name: "original.pdf"},
{ID: "att-2", Name: "extra.txt"},
{ID: "att-3", Name: "scan.pdf"},
},
}}
root := buildMountTree(context.Background(), &DocspellClient{}, items)
byID, ok := root.children["by-id"].(*dirNode)
if !ok {
t.Fatalf("expected by-id directory, got %#v", root.children)
}
shard, ok := byID.children["it"].(*dirNode)
if !ok {
t.Fatalf("expected by-id shard, got %#v", byID.children)
}
for _, name := range []string{"item-1.pdf", "item-1-2.txt", "item-1-3.pdf"} {
if _, ok := shard.children[name].(*fileNode); !ok {
t.Fatalf("expected %s in by-id shard, got %#v", name, shard.children)
}
}
}