Files
photog/default_view_test.go
puttaalu 8fa1aa4cd4 Add -default-view to choose timeline or grid as the site root
The view named by -default-view (timeline, the default, or grid) is published
at the site root; the other is served under /timeline/ or /grid/. viewBase and
pageURL derive every page path, pager link and the Timeline<->Grid cross-link
from the single setting, and Build removes both timeline/ and grid/ on each run
so flipping the default never leaves a stale root behind.

Reimplemented on trunk (which has albums and the /manage feature) rather than
cherry-picked, since the original was written against an earlier base. Credit
for the edit/delete and default-view ideas goes to Santhosh Raju, now noted in
a README Contributors section.

- main.go: -default-view flag, validated (fatal on anything but timeline/grid)
- module.nix: services.photogallery.defaultView enum option
- templates: timeline/grid/day/album/albums cross-links use TimelineURL/GridURL
- tests: timeline-root, grid-root, and switch-cleanup
2026-10-18 22:06:46 +02:00

109 lines
3.3 KiB
Go

package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
// buildWithView builds a two-photo site with the given default view and returns
// the output dir.
func buildWithView(t *testing.T, view string) string {
t.Helper()
content := t.TempDir()
output := t.TempDir()
writePNG(t, filepath.Join(content, "a.png"))
writePNG(t, filepath.Join(content, "b.png"))
g := NewGenerator(content, output, "http://x", "A", "@a", "bio", "", "f", "s", "m", 60, view)
if err := g.Build(); err != nil {
t.Fatalf("build (%s): %v", view, err)
}
return output
}
func exists(p string) bool { _, err := os.Stat(p); return err == nil }
func TestDefaultViewTimeline(t *testing.T) {
out := buildWithView(t, viewTimeline)
// Timeline owns the root; grid is the secondary under /grid/.
if !exists(filepath.Join(out, "index.html")) {
t.Error("root index.html missing")
}
if !exists(filepath.Join(out, "grid", "index.html")) {
t.Error("/grid/ should exist as the secondary view")
}
// No /timeline/ dir — timeline is at the root, not under its own name.
if exists(filepath.Join(out, "timeline")) {
t.Error("/timeline/ should not exist when timeline is the default")
}
// The root (timeline) links to grid at /grid/.
root := readFile(t, filepath.Join(out, "index.html"))
if !strings.Contains(root, `href="/grid/"`) {
t.Error("timeline root should link to /grid/")
}
}
func TestDefaultViewGrid(t *testing.T) {
out := buildWithView(t, viewGrid)
// Grid owns the root; timeline is the secondary under /timeline/.
if !exists(filepath.Join(out, "index.html")) {
t.Error("root index.html missing")
}
if !exists(filepath.Join(out, "timeline", "index.html")) {
t.Error("/timeline/ should exist as the secondary view")
}
// No /grid/ dir — grid is at the root now.
if exists(filepath.Join(out, "grid")) {
t.Error("/grid/ should not exist when grid is the default")
}
// The root (grid) links back to the timeline at /timeline/.
root := readFile(t, filepath.Join(out, "index.html"))
if !strings.Contains(root, `href="/timeline/"`) {
t.Error("grid root should link to /timeline/")
}
}
// TestDefaultViewSwitchCleansUp confirms that flipping the default view does not
// leave the previous root's view directory behind.
func TestDefaultViewSwitchCleansUp(t *testing.T) {
content := t.TempDir()
output := t.TempDir()
writePNG(t, filepath.Join(content, "a.png"))
// First build with grid as root → creates /timeline/.
g1 := NewGenerator(content, output, "http://x", "A", "@a", "b", "", "f", "s", "m", 60, viewGrid)
if err := g1.Build(); err != nil {
t.Fatal(err)
}
if !exists(filepath.Join(output, "timeline")) {
t.Fatal("expected /timeline/ after grid-default build")
}
// Rebuild with timeline as root → /timeline/ must be gone, /grid/ present.
g2 := NewGenerator(content, output, "http://x", "A", "@a", "b", "", "f", "s", "m", 60, viewTimeline)
if err := g2.Build(); err != nil {
t.Fatal(err)
}
if exists(filepath.Join(output, "timeline")) {
t.Error("/timeline/ left over after switching default to timeline")
}
if !exists(filepath.Join(output, "grid", "index.html")) {
t.Error("/grid/ should exist after switching default to timeline")
}
}
func readFile(t *testing.T, p string) string {
t.Helper()
b, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
return string(b)
}