options_test.go

206 lines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
package application

import (
	"net/http"
	"testing"
	"testing/fstest"
)

func TestWithValue_SetsTemplateValue(t *testing.T) {
	app := New(WithValue("siteName", "TestSite"))

	funcs := app.templateFuncs()
	fn, ok := funcs["siteName"]
	if !ok {
		t.Fatal("expected 'siteName' func to be registered")
	}

	// The func should return the value
	result := fn.(func() any)()
	if result != "TestSite" {
		t.Errorf("expected 'TestSite', got %v", result)
	}
}

func TestWithFunc_RegistersTemplateFunction(t *testing.T) {
	add := func(a, b int) int { return a + b }
	app := New(WithFunc("add", add))

	funcs := app.templateFuncs()
	_, ok := funcs["add"]
	if !ok {
		t.Fatal("expected 'add' func to be registered")
	}
}

func TestWithMiddleware_AddsMiddleware(t *testing.T) {
	called := false
	mw := func(next http.Handler) http.Handler {
		called = true
		return next
	}

	app := New(WithMiddleware(mw))

	if len(app.middlewares) != 1 {
		t.Fatalf("expected 1 middleware, got %d", len(app.middlewares))
	}

	// Execute the middleware to verify it was stored
	app.middlewares[0](http.DefaultServeMux)
	if !called {
		t.Error("expected middleware to be called")
	}
}

func TestWithMiddleware_MultipleMiddlewares(t *testing.T) {
	mw1 := func(next http.Handler) http.Handler { return next }
	mw2 := func(next http.Handler) http.Handler { return next }
	mw3 := func(next http.Handler) http.Handler { return next }

	app := New(
		WithMiddleware(mw1),
		WithMiddleware(mw2),
		WithMiddleware(mw3),
	)

	if len(app.middlewares) != 3 {
		t.Errorf("expected 3 middlewares, got %d", len(app.middlewares))
	}
}

func TestWithHealthPath_SetsHealthPath(t *testing.T) {
	app := New(WithHealthPath("/healthz"))

	if app.healthPath != "/healthz" {
		t.Errorf("expected '/healthz', got %q", app.healthPath)
	}
}

func TestWithHealthPath_DefaultEmpty(t *testing.T) {
	app := New()

	if app.healthPath != "" {
		t.Errorf("expected empty healthPath by default, got %q", app.healthPath)
	}
}

type testController struct {
	BaseController
	setupCalled bool
}

func (c *testController) Handle(r *http.Request) Controller {
	return c
}

func (c *testController) Setup(app *App) {
	c.BaseController.Setup(app)
	c.setupCalled = true
}

func TestWithController_RegistersController(t *testing.T) {
	ctrl := &testController{}
	app := New(WithController("test", ctrl))

	if _, ok := app.controllers["test"]; !ok {
		t.Fatal("expected controller to be registered")
	}

	if !ctrl.setupCalled {
		t.Error("expected Setup to be called on controller")
	}
}

func TestWithController_MultipleControllers(t *testing.T) {
	ctrl1 := &testController{}
	ctrl2 := &testController{}

	app := New(
		WithController("home", ctrl1),
		WithController("admin", ctrl2),
	)

	if len(app.controllers) != 2 {
		t.Errorf("expected 2 controllers, got %d", len(app.controllers))
	}
	if _, ok := app.controllers["home"]; !ok {
		t.Error("expected 'home' controller")
	}
	if _, ok := app.controllers["admin"]; !ok {
		t.Error("expected 'admin' controller")
	}
}

func TestNew_AppliesOptions(t *testing.T) {
	ctrl := &testController{}

	app := New(
		WithValue("brand", "Congo"),
		WithHealthPath("/up"),
		WithController("main", ctrl),
		WithMiddleware(func(next http.Handler) http.Handler { return next }),
	)

	if app == nil {
		t.Fatal("expected non-nil App")
	}
	if app.healthPath != "/up" {
		t.Errorf("expected healthPath '/up', got %q", app.healthPath)
	}
	if len(app.controllers) != 1 {
		t.Errorf("expected 1 controller, got %d", len(app.controllers))
	}
	if len(app.middlewares) != 1 {
		t.Errorf("expected 1 middleware, got %d", len(app.middlewares))
	}

	funcs := app.templateFuncs()
	if _, ok := funcs["brand"]; !ok {
		t.Error("expected 'brand' func to be registered")
	}
}

func TestNew_EmptyOptions(t *testing.T) {
	app := New()

	if app == nil {
		t.Fatal("expected non-nil App")
	}
	if app.funcs == nil {
		t.Error("expected non-nil funcs map")
	}
	if app.controllers == nil {
		t.Error("expected non-nil controllers map")
	}
	if len(app.funcs) != 0 {
		t.Errorf("expected empty funcs, got %d", len(app.funcs))
	}
	if len(app.controllers) != 0 {
		t.Errorf("expected empty controllers, got %d", len(app.controllers))
	}
}

func TestWithViews_SetsViewsFS(t *testing.T) {
	views := fstest.MapFS{
		"views/layouts/base.html": {Data: []byte(`{{define "base.html"}}base{{end}}`)},
	}

	app := New(WithViews(views))

	if app.viewsFS == nil {
		t.Error("expected viewsFS to be set")
	}
	if app.base == nil {
		t.Error("expected base templates to be parsed")
	}
}

func TestWithEmailer_SetsEmailer(t *testing.T) {
	// Emailer is an interface; use nil to verify the option function runs
	app := New(WithEmailer(nil))

	if app.Emailer != nil {
		t.Error("expected nil emailer")
	}
}