feat: tag system

This commit is contained in:
2026-02-03 09:41:03 +01:00
parent 3f5cf0d673
commit cb11e34798
11 changed files with 397 additions and 10 deletions

View File

@ -3,6 +3,7 @@ package web
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
@ -15,7 +16,7 @@ import (
type testEnv struct {
handler *Handler
notes map[string]*note.Note
storage *storage.NoteStorage
}
func newTestEnv(t *testing.T) *testEnv {
@ -56,14 +57,9 @@ func newTestEnv(t *testing.T) *testEnv {
tm := render.NewTemplateManager("templates")
handler := NewHandler(svc, tm)
noteMap := map[string]*note.Note{}
for _, n := range notes {
noteMap[n.ID] = n
}
return &testEnv{
handler: handler,
notes: noteMap,
storage: ns,
}
}
@ -232,3 +228,52 @@ func TestExtractHash(t *testing.T) {
})
}
}
func TestHandlerTags_Add(t *testing.T) {
env := newTestEnv(t)
form := url.Values{}
form.Set("tag", " Go ")
req := httptest.NewRequest(http.MethodPost, "/notes/b2/tags", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rec := httptest.NewRecorder()
env.handler.ServeHTTP(rec, req)
if rec.Code != http.StatusSeeOther {
t.Fatalf("expected status 303, got %d", rec.Code)
}
n, err := env.storage.Get("b2")
if err != nil {
t.Fatalf("get note: %v", err)
}
if len(n.Tags) != 1 || n.Tags[0] != "go" {
t.Fatalf("expected normalized tag on note, got %+v", n.Tags)
}
}
func TestHandlerTags_Remove(t *testing.T) {
env := newTestEnv(t)
if err := env.storage.AddTag("b2", "go"); err != nil {
t.Fatalf("seed tag: %v", err)
}
req := httptest.NewRequest(http.MethodPost, "/notes/b2/tags/go", nil)
rec := httptest.NewRecorder()
env.handler.ServeHTTP(rec, req)
if rec.Code != http.StatusSeeOther {
t.Fatalf("expected status 303, got %d", rec.Code)
}
n, err := env.storage.Get("b2")
if err != nil {
t.Fatalf("get note: %v", err)
}
if len(n.Tags) != 0 {
t.Fatalf("expected tag to be removed, got %+v", n.Tags)
}
}