Use mount metadata for path lookups
This commit is contained in:
+62
-35
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -59,6 +60,28 @@ func TestItemIDFromLocalByIDPath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestItemIDFromLocalFriendlyPathUsesMetadata(t *testing.T) {
|
||||
mountPath := t.TempDir()
|
||||
metadata := mountMetadata{Items: map[string]mountedItemMetadata{
|
||||
"item-1": {FriendlyPath: "Bills/2024/07/Invoice.pdf"},
|
||||
}}
|
||||
data, err := json.Marshal(metadata)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(mountPath, mountMetadataFileName), data, 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got, err := itemIDFromLocalPath(filepath.Join(mountPath, "Bills", "2024", "07", "Invoice.pdf"), 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",
|
||||
@@ -121,50 +144,54 @@ func TestBuildMountTreeAddsByIDDirectory(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFriendlyLocalPathPrefersNonByIDPath(t *testing.T) {
|
||||
mountPath := t.TempDir()
|
||||
friendlyDir := filepath.Join(mountPath, "Bills", "2024", "07")
|
||||
byIDDir := filepath.Join(mountPath, "by-id", "it")
|
||||
if err := os.MkdirAll(friendlyDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(byIDDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
friendlyPath := filepath.Join(friendlyDir, "Invoice.pdf")
|
||||
byIDPath := filepath.Join(byIDDir, "item-1.pdf")
|
||||
if err := os.WriteFile(friendlyPath, []byte("pdf"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Link(friendlyPath, byIDPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
func TestBuildMountTreeAddsMetadataLookup(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}},
|
||||
}}
|
||||
|
||||
got, err := friendlyLocalPath(mountPath, byIDPath)
|
||||
if err != nil {
|
||||
root := buildMountTree(context.Background(), &DocspellClient{}, items)
|
||||
metadataNode, ok := root.children[mountMetadataFileName].(*staticFileNode)
|
||||
if !ok {
|
||||
t.Fatalf("expected metadata file, got %#v", root.children[mountMetadataFileName])
|
||||
}
|
||||
var metadata mountMetadata
|
||||
if err := json.Unmarshal(metadataNode.data, &metadata); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != friendlyPath {
|
||||
t.Fatalf("expected friendly path %q, got %q", friendlyPath, got)
|
||||
got := metadata.Items["item-1"]
|
||||
if got.FriendlyPath != "Bills_Private/2024/07/Invoice_July.pdf" {
|
||||
t.Fatalf("expected friendly metadata path, got %q", got.FriendlyPath)
|
||||
}
|
||||
if got.ByIDPath != "by-id/it/item-1.pdf" {
|
||||
t.Fatalf("expected by-id metadata path, got %q", got.ByIDPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFriendlyLocalPathReturnsEmptyWhenNoFriendlyPathExists(t *testing.T) {
|
||||
func TestFriendlyLocalPathForItemIDUsesMetadata(t *testing.T) {
|
||||
mountPath := t.TempDir()
|
||||
byIDDir := filepath.Join(mountPath, "by-id", "it")
|
||||
if err := os.MkdirAll(byIDDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
byIDPath := filepath.Join(byIDDir, "item-1.pdf")
|
||||
if err := os.WriteFile(byIDPath, []byte("pdf"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got, err := friendlyLocalPath(mountPath, byIDPath)
|
||||
metadata := mountMetadata{Items: map[string]mountedItemMetadata{
|
||||
"item-1": {FriendlyPath: "Bills/2024/07/Invoice.pdf"},
|
||||
}}
|
||||
data, err := json.Marshal(metadata)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != "" {
|
||||
t.Fatalf("expected no friendly path, got %q", got)
|
||||
if err := os.WriteFile(filepath.Join(mountPath, mountMetadataFileName), data, 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got, err := friendlyLocalPathForItemID(mountPath, "item-1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := filepath.Join(mountPath, "Bills", "2024", "07", "Invoice.pdf")
|
||||
if got != want {
|
||||
t.Fatalf("expected %q, got %q", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user