Files
pages/internal/server/task_daily_notes_test.go
T
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

297 lines
10 KiB
Go

package server
import (
"context"
"database/sql"
"encoding/json"
"errors"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/google/uuid"
"pageswiki/internal/store"
)
func TestTaskDailyHierarchyPaging(t *testing.T) {
s, token, domain, actor := taskFixture(t)
ctx := context.Background()
save := func(i store.TaskRecord) store.TaskRecord {
t.Helper()
v, e := s.store.SaveTask(ctx, domain, actor, i, i.Version)
if e != nil {
t.Fatal(e)
}
return v
}
parent := save(newTaskRecord("task", "Parent"))
other := save(newTaskRecord("task", "Other"))
child := newTaskRecord("task", "Child")
child.ParentUUID = parent.UUID
child.PlannedOn = "2026-09-26"
child = save(child)
// A newer child must not split the family from its parent across pages.
for id, stamp := range map[string]int{parent.UUID: 1, other.UUID: 2, child.UUID: 3} {
if _, e := s.store.DB.Exec(`UPDATE organizer_items SET created_at_ms=? WHERE uuid=?`, stamp, id); e != nil {
t.Fatal(e)
}
}
q := store.TaskQuery{Kind: "task", View: "all", Today: "2026-09-25", Limit: 1, Hierarchy: true}
ids := []string{}
for {
items, next, e := s.store.ListTasks(ctx, domain, actor, q)
if e != nil {
t.Fatal(e)
}
for _, i := range items {
ids = append(ids, i.UUID)
}
if next == "" {
break
}
q.Before = next
if len(ids) > 4 {
t.Fatal("cursor loop")
}
}
if strings.Join(ids, ",") != strings.Join([]string{other.UUID, parent.UUID, child.UUID}, ",") {
t.Fatal("family ordering", ids)
}
w := taskRequest(s, token, "GET", "items?view=day&date=2026-09-26&hierarchy=1", nil, "")
var body struct{ Items, Parents []store.TaskRecord }
if w.Code != 200 {
t.Fatal(w.Code, w.Body)
}
json.Unmarshal(w.Body.Bytes(), &body)
if len(body.Items) != 1 || body.Items[0].UUID != child.UUID || len(body.Parents) != 1 || body.Parents[0].UUID != parent.UUID || body.Parents[0].Notes != "" {
t.Fatal(w.Body)
}
parent.PlannedOn = "2026-09-24"
parent = save(parent)
q = store.TaskQuery{Kind: "task", View: "carryover", Today: "2026-09-25", Hierarchy: true}
items, _, e := s.store.ListTasks(ctx, domain, actor, q)
if e != nil || len(items) != 1 || items[0].UUID != parent.UUID {
t.Fatal(items, e)
}
parent.PlannedOn = "2026-09-26"
parent = save(parent)
items, _, e = s.store.ListTasks(ctx, domain, actor, q)
if e != nil || len(items) != 0 {
t.Fatal("carryover rescheduling", items, e)
}
child.DueOn = "2026-09-24"
save(child)
items, _, e = s.store.ListTasks(ctx, domain, actor, q)
if e != nil || len(items) != 1 || items[0].DueOn != "2026-09-24" {
t.Fatal("overdue independent of plan", items, e)
}
for _, bad := range []string{"", "2026-02-30", "2026/09/26"} {
if w = taskRequest(s, token, "GET", "items?view=day&date="+bad, nil, ""); w.Code != 422 {
t.Fatal("bad date", bad, w.Code)
}
}
for _, p := range []string{"/v/plan/tasks/carryover", "/v/plan/tasks/tomorrow", "/v/plan/tasks/day/2026/09/26"} {
r := httptest.NewRequest("GET", "http://localhost"+p, nil)
r.Header.Set("Authorization", "Bearer "+token)
w = httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
if w.Code != 200 {
t.Fatal(p, w.Code, w.Body)
}
if strings.HasSuffix(p, "tomorrow") {
date := time.Now().In(s.getLocation(domain)).AddDate(0, 0, 1).Format("2006-01-02")
if !strings.Contains(w.Body.String(), `data-selected-date="`+date+`"`) {
t.Fatal("domain tomorrow date missing")
}
}
}
}
func TestTaskWikiNotesAtomicCreation(t *testing.T) {
s, token, domain, actor := taskFixture(t)
ctx := context.Background()
save := func(i store.TaskRecord) store.TaskRecord {
t.Helper()
v, e := s.store.SaveTask(ctx, domain, actor, i, i.Version)
if e != nil {
t.Fatal(e)
}
return v
}
root, e := s.store.CreatePageAtPathInDomain(domain, "/projects/wiki")
if e != nil {
t.Fatal(e)
}
project := newTaskRecord("project", "Wiki")
project.PageUUID = root
project.NotesSubpath = "work/notes"
project = save(project)
task := newTaskRecord("task", "実装 <draft>")
task.ProjectUUID = project.UUID
task = save(task)
endpoint := "items/" + task.UUID + "/note"
w := taskRequest(s, token, "GET", endpoint, nil, "")
var result struct {
Item store.TaskRecord
Path, URL string
EditURL string `json:"edit_url"`
Linked, Exists, Writable bool
}
if w.Code != 200 {
t.Fatal(w.Code, w.Body)
}
json.Unmarshal(w.Body.Bytes(), &result)
target := "/projects/wiki/work/notes/実装 <draft>"
if result.Path != target || result.Linked || result.Exists || !result.Writable {
t.Fatal(w.Body)
}
if _, e = s.store.ResolvePathInDomain(domain, target); !errors.Is(e, sql.ErrNoRows) {
t.Fatal("GET created a page", e)
}
if w = taskRequest(s, token, "POST", endpoint, map[string]string{"path": target}, ""); w.Code != 428 {
t.Fatal(w.Code)
}
if w = taskRequest(s, token, "POST", endpoint, map[string]string{"path": "/unconfirmed"}, `"1"`); w.Code != 412 {
t.Fatal("unconfirmed target", w.Code, w.Body)
}
// Rejecting the task journal must also roll back the object, path and revision.
if _, e = s.store.DB.Exec(`CREATE TRIGGER reject_task_note BEFORE INSERT ON organizer_changes BEGIN SELECT RAISE(ABORT,'fixture'); END`); e != nil {
t.Fatal(e)
}
w = taskRequest(s, token, "POST", endpoint, map[string]string{"path": target}, `"1"`)
if w.Code != 500 {
t.Fatal("rollback fixture", w.Code, w.Body)
}
if _, e = s.store.ResolvePathInDomain(domain, target); !errors.Is(e, sql.ErrNoRows) {
t.Fatal("orphan note", e)
}
if _, e = s.store.DB.Exec(`DROP TRIGGER reject_task_note`); e != nil {
t.Fatal(e)
}
w = taskRequest(s, token, "POST", endpoint, map[string]string{"path": target}, `"1"`)
if w.Code != 200 {
t.Fatal(w.Code, w.Body)
}
json.Unmarshal(w.Body.Bytes(), &result)
if !result.Linked || result.Item.PageUUID == "" || result.Item.Version != 2 || !strings.HasPrefix(result.EditURL, "/e/-/projects/wiki/") {
t.Fatal(w.Body)
}
rev, e := s.store.GetLatestRevision(result.Item.PageUUID)
if e != nil || rev.Body != "<h1>実装 &lt;draft&gt;</h1>\n" {
t.Fatal("note title escaping", rev, e)
}
if w = taskRequest(s, token, "POST", endpoint, map[string]string{"path": target}, `"1"`); w.Code != 412 {
t.Fatal("stale creation retry", w.Code)
}
if _, e = s.store.DB.Exec(`UPDATE paths SET path='/projects/wiki/moved' WHERE domain_uuid=? AND object_uuid=?`, domain, result.Item.PageUUID); e != nil {
t.Fatal(e)
}
w = taskRequest(s, token, "GET", endpoint, nil, "")
json.Unmarshal(w.Body.Bytes(), &result)
if result.Path != "/projects/wiki/moved" {
t.Fatal("UUID did not follow rename", w.Body)
}
if _, e = s.store.DB.Exec(`UPDATE principals SET state='readonly' WHERE uuid=?`, actor); e != nil {
t.Fatal(e)
}
w = taskRequest(s, token, "GET", endpoint, nil, "")
json.Unmarshal(w.Body.Bytes(), &result)
if w.Code != 200 || result.Writable {
t.Fatal("readonly", w.Code, w.Body)
}
if w = taskRequest(s, token, "POST", endpoint, nil, `"2"`); w.Code != 403 {
t.Fatal("readonly write", w.Code)
}
}
func TestTaskNoteCollisionsAndProjectValidation(t *testing.T) {
s, token, domain, actor := taskFixture(t)
ctx := context.Background()
root, e := s.store.CreatePageAtPathInDomain(domain, "/projects/app")
if e != nil {
t.Fatal(e)
}
project := newTaskRecord("project", "App")
project.PageUUID = root
for _, sub := range []string{"../outside", "/absolute", "a/../b", "a//b", "a/_/edit", "a\nb"} {
bad := project
bad.NotesSubpath = sub
if _, e = s.store.SaveTask(ctx, domain, actor, bad, 0); !errors.Is(e, store.ErrTaskInvalid) {
t.Fatal(sub, e)
}
}
project, e = s.store.SaveTask(ctx, domain, actor, project, 0)
if e != nil {
t.Fatal(e)
}
task := newTaskRecord("task", "API / UI")
task.ProjectUUID = project.UUID
task, e = s.store.SaveTask(ctx, domain, actor, task, 0)
if e != nil {
t.Fatal(e)
}
target := store.TaskNotePath("/projects/app", project, task)
if target != "/projects/app/tasks/API - UI" {
t.Fatal(target)
}
existing, e := s.store.CreatePageAtPathInDomain(domain, target)
if e != nil {
t.Fatal(e)
}
w := taskRequest(s, token, "POST", "items/"+task.UUID+"/note", map[string]string{"path": target}, `"1"`)
if w.Code != 409 {
t.Fatal("collision", w.Code, w.Body)
}
current, e := s.store.GetTask(ctx, domain, actor, task.UUID)
if e != nil || current.PageUUID != "" || current.Version != 1 {
t.Fatal("collision mutated task", current, e)
}
bound, _ := s.store.ResolvePathInDomain(domain, target)
if bound != existing {
t.Fatal("existing page overwritten")
}
missing := newTaskRecord("task", "Unassigned")
missing, e = s.store.SaveTask(ctx, domain, actor, missing, 0)
if e != nil {
t.Fatal(e)
}
if w = taskRequest(s, token, "GET", "items/"+missing.UUID+"/note", nil, ""); w.Code != 422 || !strings.Contains(w.Body.String(), "note_setup") {
t.Fatal(w.Code, w.Body)
}
if w = taskRequest(s, token, "GET", "items/"+uuid.NewString()+"/note", nil, ""); w.Code != 404 {
t.Fatal(w.Code)
}
other, e := s.store.CreatePasswordAccount(domain, "noteother", "a long fixture password", "Other", "")
if e != nil {
t.Fatal(e)
}
otherToken, e := s.store.CreateAuthSession(domain, other.UUID)
if e != nil {
t.Fatal(e)
}
if w = taskRequest(s, otherToken, "GET", "items/"+task.UUID+"/note", nil, ""); w.Code != 404 {
t.Fatal("note ownership", w.Code, w.Body)
}
// Own private tasks do not grant write permission to a Wiki path.
ownProject := newTaskRecord("project", "Other project")
ownProject.PageUUID = root
ownProject, e = s.store.SaveTask(ctx, domain, other.UUID, ownProject, 0)
if e != nil {
t.Fatal(e)
}
ownTask := newTaskRecord("task", "No Wiki permission")
ownTask.ProjectUUID = ownProject.UUID
ownTask, e = s.store.SaveTask(ctx, domain, other.UUID, ownTask, 0)
if e != nil {
t.Fatal(e)
}
w = taskRequest(s, otherToken, "POST", "items/"+ownTask.UUID+"/note", map[string]string{"path": store.TaskNotePath("/projects/app", ownProject, ownTask)}, `"1"`)
if w.Code == 200 {
t.Fatal("task ownership granted Wiki access")
}
if _, e = s.store.ResolvePathInDomain(domain, store.TaskNotePath("/projects/app", ownProject, ownTask)); !errors.Is(e, sql.ErrNoRows) {
t.Fatal("unauthorized note created", e)
}
}