66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/tim/cairnquire/apps/server/internal/docs"
|
|
)
|
|
|
|
func TestBuildBrowserUsesFolderIndex(t *testing.T) {
|
|
browser := buildBrowser([]docs.DocumentRecord{
|
|
{
|
|
Path: "guide/index.md",
|
|
Title: "Guide",
|
|
UpdatedAt: time.Now(),
|
|
},
|
|
{
|
|
Path: "guide/setup.md",
|
|
Title: "Setup",
|
|
UpdatedAt: time.Now(),
|
|
},
|
|
}, "guide/index.md")
|
|
|
|
if len(browser.Columns) != 2 {
|
|
t.Fatalf("len(browser.Columns) = %d, want 2", len(browser.Columns))
|
|
}
|
|
|
|
root := browser.Columns[0].Items
|
|
if len(root) != 1 {
|
|
t.Fatalf("len(root items) = %d, want 1", len(root))
|
|
}
|
|
if !root[0].IsFolder || root[0].Name != "Guide" || root[0].URL != "/docs/guide" {
|
|
t.Fatalf("root folder = %#v, want Guide folder linking to /docs/guide", root[0])
|
|
}
|
|
|
|
child := browser.Columns[1].Items
|
|
if len(child) != 1 {
|
|
t.Fatalf("len(child items) = %d, want 1", len(child))
|
|
}
|
|
if child[0].Path == "guide/index.md" {
|
|
t.Fatalf("folder index file should be represented by the folder, not repeated as a child file")
|
|
}
|
|
}
|
|
|
|
func TestTrimEditSuffix(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
wantPath string
|
|
wantOK bool
|
|
}{
|
|
{name: "nested path", input: "guide/setup/edit", wantPath: "guide/setup", wantOK: true},
|
|
{name: "root edit sentinel", input: "edit", wantPath: "", wantOK: true},
|
|
{name: "document route", input: "guide/setup", wantPath: "", wantOK: false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
gotPath, gotOK := trimEditSuffix(tt.input)
|
|
if gotPath != tt.wantPath || gotOK != tt.wantOK {
|
|
t.Fatalf("trimEditSuffix(%q) = (%q, %t), want (%q, %t)", tt.input, gotPath, gotOK, tt.wantPath, tt.wantOK)
|
|
}
|
|
})
|
|
}
|
|
}
|