commit 78d6c27c8940da32a6de8e64327c86f74fdaa2eb Author: adminoo <git@kadath.corp> Date: Wed Feb 4 12:59:22 2026 +0100 feat: freebsd log rotation config thingie commit 55af4e6c70122e679272ed247c26e04b1247f694 Author: adminoo <git@kadath.corp> Date: Wed Feb 4 12:58:43 2026 +0100 feat: embed templates, static resolution commit 29c917f929a7378ec29c54315ee2e9f420747787 Author: adminoo <git@kadath.corp> Date: Wed Feb 4 10:44:34 2026 +0100 feat: set log file path commit 294fd3d1549979eab63587ceec6ff5d0978e9afc Author: adminoo <git@kadath.corp> Date: Wed Feb 4 10:23:53 2026 +0100 feat: logging HTTP request commit c9ae80b240d58e1abed7ae3b7b2c3b283a31f1a1 Author: adminoo <git@kadath.corp> Date: Wed Feb 4 09:54:05 2026 +0100 feat: freebsd-specific compile target and scripts commit 86ca154dedd19aa1fe5f571c445dcf17a8396bfa Author: adminoo <git@kadath.corp> Date: Wed Feb 4 09:25:16 2026 +0100 feat: mobile friendly CSS commit 199f4319e0b08a4b6d595d7eb3effb6db6c7beec Author: adminoo <git@kadath.corp> Date: Wed Feb 4 09:25:03 2026 +0100 feat: persisting rendered note commit 865e258237e45d7c542685a4653bcad3c5af259d Author: adminoo <git@kadath.corp> Date: Wed Feb 4 08:06:38 2026 +0100 fix: grouping notes by folder commit 242d1d074c92461f38212b033c7a9e383f9dc550 Author: adminoo <git@kadath.corp> Date: Tue Feb 3 16:52:50 2026 +0100 feat: storage layer logic - Prune notes from db not matching current folder structure at start - Detect file system deletion on start by comparing in-db notes - Prevent updating of in-db notes at start if modification time is not newer - Delete by path commit d75d46bc1ab22bd990d0fdc307e571fe52f0dd99 Author: adminoo <git@kadath.corp> Date: Tue Feb 3 15:27:07 2026 +0100 feat: group notes by root folders commit e1e25a938e717599332f7b40a449d9bb854b673a Author: adminoo <git@kadath.corp> Date: Tue Feb 3 14:24:37 2026 +0100 feat: size in kilobytes commit 61220272a2df2b66c2b8e356ba359ed01de3bd12 Author: adminoo <git@kadath.corp> Date: Tue Feb 3 14:19:40 2026 +0100 feat: styling inputs
150 lines
4.3 KiB
Go
150 lines
4.3 KiB
Go
package render
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRenderMarkdown(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
markdown string
|
|
want string
|
|
}{
|
|
{
|
|
name: "Markdown, no link",
|
|
markdown: `# Test
|
|
|
|
## 01/24/26 09:14:20 - some entry
|
|
check this out`,
|
|
want: `<h1>Test</h1><h2>01/24/26 09:14:20 - some entry</h2><p>check this out</p>`,
|
|
},
|
|
{
|
|
name: "Markdown, some link",
|
|
markdown: `# Test 2
|
|
## 01/24/26 09:14:20 - some entry (bare link)
|
|
Check this out http://tatata.toto here
|
|
`,
|
|
want: `<h1>Test 2</h1><h2>01/24/26 09:14:20 - some entry (bare link)</h2><p>Check this out <a href="http://tatata.toto" target="_blank">http://tatata.toto</a> here</p>`,
|
|
},
|
|
{
|
|
name: "Markdown, some link with description",
|
|
markdown: `# Test 2
|
|
## 01/24/26 09:14:20 - some entry (bare link)
|
|
Check this out [here](http://tatata.toto)
|
|
`,
|
|
want: `<h1>Test 2</h1><h2>01/24/26 09:14:20 - some entry (bare link)</h2><p>Check this out <a href="http://tatata.toto" target="_blank">here</a></p>`,
|
|
},
|
|
}
|
|
|
|
for _, test := range cases {
|
|
got, err := RenderMarkdown([]byte(test.markdown))
|
|
if err != nil {
|
|
t.Errorf("Error rendering markdown: '%s'\n", err)
|
|
}
|
|
strip := strings.ReplaceAll(string(got), "\n", "")
|
|
strip = strings.Trim(strip, " ")
|
|
|
|
if strip != test.want {
|
|
t.Errorf("Rendering markdown: Wanted '%s', got '%s'.\n", test.want, strip)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTemplateManagerGetTemplate_Caches(t *testing.T) {
|
|
baseDir := t.TempDir()
|
|
writeTemplate(t, baseDir, "base.tmpl", `{{ define "base" }}base{{ end }}`)
|
|
writeTemplate(t, baseDir, "index.tmpl", `{{ define "content" }}index{{ end }}`)
|
|
|
|
tm := NewTemplateManagerFS(os.DirFS(baseDir), "")
|
|
td := &TemplateData{
|
|
Name: "index",
|
|
FileNameSet: []string{"base", "index"},
|
|
}
|
|
|
|
first, err := tm.GetTemplate(td)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
second, err := tm.GetTemplate(td)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
if first != second {
|
|
t.Fatalf("expected cached template instance")
|
|
}
|
|
}
|
|
|
|
func TestTemplateManagerGetTemplate_Missing(t *testing.T) {
|
|
baseDir := t.TempDir()
|
|
tm := NewTemplateManagerFS(os.DirFS(baseDir), "")
|
|
|
|
td := &TemplateData{
|
|
Name: "missing",
|
|
FileNameSet: []string{"missing"},
|
|
}
|
|
|
|
if _, err := tm.GetTemplate(td); err == nil {
|
|
t.Fatalf("expected error for missing template")
|
|
}
|
|
}
|
|
|
|
func TestTemplateManagerRender(t *testing.T) {
|
|
baseDir := t.TempDir()
|
|
writeTemplate(t, baseDir, "base.tmpl", `{{ define "base" }}<html>{{ template "content" . }}</html>{{ end }}`)
|
|
writeTemplate(t, baseDir, "noteList.tmpl", `{{ define "noteList" }}notes{{ end }}`)
|
|
writeTemplate(t, baseDir, "metadata.tmpl", `{{ define "metadata" }}meta{{ end }}`)
|
|
writeTemplate(t, baseDir, "index.tmpl", `{{ define "content" }}hello{{ end }}`)
|
|
writeTemplate(t, baseDir, "metadata.tmpl", `{{ define "noteList" }}notes{{ end }}`)
|
|
|
|
tm := NewTemplateManagerFS(os.DirFS(baseDir), "")
|
|
rec := httptest.NewRecorder()
|
|
|
|
err := tm.Render(rec, "index", map[string]string{"msg": "hi"})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
if ct := rec.Header().Get("Content-Type"); ct != "text/html; charset=utf-8" {
|
|
t.Fatalf("expected content-type text/html; charset=utf-8, got %q", ct)
|
|
}
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("expected status 200, got %d", rec.Code)
|
|
}
|
|
|
|
if !strings.Contains(rec.Body.String(), "hello") {
|
|
t.Fatalf("expected rendered template content")
|
|
}
|
|
}
|
|
|
|
func TestTemplateManagerRender_MissingTemplate(t *testing.T) {
|
|
baseDir := t.TempDir()
|
|
writeTemplate(t, baseDir, "base.tmpl", `{{ define "base" }}<html>{{ template "content" . }}</html>{{ end }}`)
|
|
writeTemplate(t, baseDir, "noteList.tmpl", `{{ define "noteList" }}notes{{ end }}`)
|
|
writeTemplate(t, baseDir, "metadata.tmpl", `{{ define "metadata" }}meta{{ end }}`)
|
|
writeTemplate(t, baseDir, "metadata.tmpl", `{{ define "noteList" }}notes{{ end }}`)
|
|
|
|
tm := NewTemplateManagerFS(os.DirFS(baseDir), "")
|
|
rec := httptest.NewRecorder()
|
|
|
|
if err := tm.Render(rec, "index", nil); err == nil {
|
|
t.Fatalf("expected error for missing template")
|
|
}
|
|
}
|
|
|
|
func writeTemplate(t *testing.T, dir, name, contents string) {
|
|
t.Helper()
|
|
|
|
path := filepath.Join(dir, name)
|
|
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
|
|
t.Fatalf("write template %s: %v", name, err)
|
|
}
|
|
}
|