Files
photog/default_view_test.go
T

108 lines
3.3 KiB
Go
Raw Normal View History

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)
}