Files
MindSift/internal/api/journal_test.go
T
sandrodachandClaude Opus 5.5 9008d7c915
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 2m6s
feat(workspace): journal with one note per day and a calendar
- journal_days (migration 8) maps a person's day to an ordinary note;
  GET /journal lists the days, POST /journal/{date} opens or creates one
- the journal sits on top of the notes sidebar: days below each other
  (today first, older ones load while scrolling), a calendar with month
  and year picker follows the day in view and jumps to any day
- the header shares the day in view; journal notes stay out of the folder
  tree; can be switched off per device under Darstellung

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 19:20:58 +01:00

79 lines
2.5 KiB
Go

package api
import (
"encoding/json"
"net/http"
"testing"
"time"
)
func TestJournalDayIsCreatedOnceAndListed(t *testing.T) {
srv, store, _ := setupTestServer(t)
router := srv.Router()
me, token := newTestUser(t, store, "journal@example.com")
rec := sessionCall(router, "POST", "/api/v1/journal/2026-10-30", token, "", "")
if rec.Code != http.StatusCreated {
t.Fatalf("create: %d %s", rec.Code, rec.Body.String())
}
var doc struct {
ID, Title, Icon string
}
_ = json.Unmarshal(rec.Body.Bytes(), &doc)
if doc.Title != "Freitag, 30. Oktober 2026" || doc.Icon != "📅" {
t.Fatalf("unexpected note: %+v", doc)
}
// Opening the same day again returns the same note
rec = sessionCall(router, "POST", "/api/v1/journal/2026-10-30", token, "", "")
var again struct{ ID string }
_ = json.Unmarshal(rec.Body.Bytes(), &again)
if rec.Code != http.StatusOK || again.ID != doc.ID {
t.Fatalf("second open: %d id %s want %s", rec.Code, again.ID, doc.ID)
}
rec = sessionCall(router, "GET", "/api/v1/journal", token, "", "")
var list struct {
Days []struct {
Date, DocID string
Empty bool
}
}
_ = json.Unmarshal(rec.Body.Bytes(), &list)
if len(list.Days) != 1 || list.Days[0].Date != "2026-10-30" || list.Days[0].DocID != doc.ID || !list.Days[0].Empty {
t.Fatalf("list: %s", rec.Body.String())
}
// Other people have their own journal
_, otherToken := newTestUser(t, store, "andere@example.com")
rec = sessionCall(router, "GET", "/api/v1/journal", otherToken, "", "")
if rec.Code != http.StatusOK || rec.Body.String() == "" {
t.Fatalf("other list: %d", rec.Code)
}
_ = json.Unmarshal(rec.Body.Bytes(), &list)
if len(list.Days) != 0 {
t.Fatalf("other user must not see my journal: %+v", list.Days)
}
// A day whose note went to the trash gets a fresh note
if err := store.DeleteDocument(me.ID, doc.ID); err != nil {
t.Fatalf("trash: %v", err)
}
rec = sessionCall(router, "POST", "/api/v1/journal/2026-10-30", token, "", "")
_ = json.Unmarshal(rec.Body.Bytes(), &again)
if rec.Code != http.StatusCreated || again.ID == doc.ID {
t.Fatalf("after trash: %d id %s", rec.Code, again.ID)
}
if rec := sessionCall(router, "POST", "/api/v1/journal/30.10.2026", token, "", ""); rec.Code != http.StatusBadRequest {
t.Fatalf("bad date: want 400, got %d", rec.Code)
}
}
func TestJournalTitle(t *testing.T) {
day := time.Date(2026, 3, 1, 0, 0, 0, 0, time.UTC)
if got := journalTitle(day); got != "Sonntag, 1. März 2026" {
t.Fatalf("got %q", got)
}
}