Files
pages/internal/server/task_planning_test.go
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

211 lines
7.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package server
import (
"context"
"encoding/json"
"strings"
"testing"
"github.com/google/uuid"
cal "pageswiki/internal/calendar"
"pageswiki/internal/store"
)
func TestTaskPlanningLifecycle(t *testing.T) {
s, token, domain, actor := taskFixture(t)
ctx := context.Background()
repo, err := s.calendars()
if err != nil {
t.Fatal(err)
}
c, err := repo.Create(ctx, domain, actor, "Private focus", "focus")
if err != nil {
t.Fatal(err)
}
task, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("task", "PRIVATE TITLE"), 0)
if err != nil {
t.Fatal(err)
}
input := map[string]any{"uuid": uuid.NewString(), "task_uuid": task.UUID, "task_version": task.Version, "calendar_uuid": c.ID, "start": "2026-09-25T10:00", "end": "2026-09-25T11:00", "timezone": "Asia/Tokyo"}
w := taskRequest(s, token, "POST", "blocks", input, "")
if w.Code != 201 {
t.Fatalf("create: %d %s", w.Code, w.Body)
}
var got struct {
Item store.TaskBlock `json:"item"`
}
if err = json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
b := got.Item
if b.Start.Hour() != 1 || b.State != "active" {
t.Fatalf("timezone/state: %+v", b)
}
event, err := repo.Event(ctx, domain, c.ID, actor, b.EventUID)
if err != nil {
t.Fatal(err)
}
if event.Summary != "Focus time("+task.Title+")" {
t.Fatalf("task title missing: %q", event.Summary)
}
if strings.Contains(event.Data, task.Notes) || strings.Contains(event.Data, task.UUID) {
t.Fatal("private task data leaked", event.Data)
}
// Normal calendar editing moves the stable UID across canonical month files.
old := event.ETag
event.Start = event.Start.AddDate(0, 1, 0)
event.End = event.End.AddDate(0, 1, 0)
event, err = cal.Build(event.Data, event)
if err != nil {
t.Fatal(err)
}
if _, err = repo.Change(ctx, domain, c.ID, actor, b.EventUID, old, &event); err != nil {
t.Fatal(err)
}
w = taskRequest(s, token, "POST", "blocks", input, "")
if w.Code != 201 {
t.Fatalf("retry: %d %s", w.Code, w.Body)
}
w = taskRequest(s, token, "GET", "blocks/"+b.UUID, nil, "")
if w.Code != 200 || !strings.Contains(w.Body.String(), `"start":"2026-10-25T01:00:00Z"`) || !strings.Contains(w.Body.String(), "?calendar=focus") {
t.Fatalf("current time: %d %s", w.Code, w.Body)
}
// A deleted active event is NOT resurrected by retries or GET.
fresh, err := repo.Event(ctx, domain, c.ID, actor, b.EventUID)
if err != nil {
t.Fatal(err)
}
if _, err = repo.Change(ctx, domain, c.ID, actor, b.EventUID, fresh.ETag, nil); err != nil {
t.Fatal(err)
}
w = taskRequest(s, token, "POST", "blocks", input, "")
if w.Code != 404 {
t.Fatalf("resurrected: %d %s", w.Code, w.Body)
}
w = taskRequest(s, token, "DELETE", "blocks/"+b.UUID, nil, "")
if w.Code != 204 {
t.Fatal(w.Code, w.Body)
}
w = taskRequest(s, token, "POST", "blocks", input, "")
if w.Code != 412 {
t.Fatal("tombstone replay", w.Code, w.Body)
}
var n int
s.store.DB.QueryRow(`SELECT count(*) FROM organizer_time_blocks`).Scan(&n)
if n != 1 {
t.Fatal("duplicate intents", n)
}
}
func TestTaskPlanningPartialCommitAndPolicy(t *testing.T) {
s, token, domain, actor := taskFixture(t)
ctx := context.Background()
repo, _ := s.calendars()
c, err := repo.Create(ctx, domain, actor, "Focus")
if err != nil {
t.Fatal(err)
}
task, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("task", "Secret"), 0)
if err != nil {
t.Fatal(err)
}
input := map[string]any{"uuid": uuid.NewString(), "task_uuid": task.UUID, "task_version": task.Version, "calendar_uuid": c.ID, "start": "2026-09-25T10:00:00Z", "end": "2026-09-25T11:00:00Z"}
// Inject a failure AFTER canonical calendar commit, before link activation.
if _, err = s.store.DB.Exec(`CREATE TRIGGER fail_block_activation BEFORE UPDATE ON organizer_time_blocks WHEN NEW.state='active' BEGIN SELECT RAISE(ABORT,'synthetic activation failure'); END`); err != nil {
t.Fatal(err)
}
w := taskRequest(s, token, "POST", "blocks", input, "")
if w.Code != 500 {
t.Fatal(w.Code, w.Body)
}
blocks, err := s.store.TaskBlocks(ctx, domain, actor, task.UUID)
if err != nil || len(blocks) != 1 || blocks[0].State != "pending" {
t.Fatal(blocks, err)
}
if _, err = s.store.DB.Exec(`DROP TRIGGER fail_block_activation`); err != nil {
t.Fatal(err)
}
w = taskRequest(s, token, "POST", "blocks", input, "")
if w.Code != 201 {
t.Fatal(w.Code, w.Body)
}
var count int
s.store.DB.QueryRow(`SELECT count(*) FROM calendar_event_cache WHERE domain=? AND calendar=?`, domain, c.ID).Scan(&count)
if count != 1 {
t.Fatal("duplicate calendar event", count)
}
// Linking does not change task dates/version/status.
fresh, _ := s.store.GetTask(ctx, domain, actor, task.UUID)
if fresh.Version != task.Version || fresh.PlannedOn != "" {
t.Fatal("task mutated", fresh)
}
// Sharing later blocks event reads, but allows unlink without deleting event.
c, _ = repo.Get(domain, c.ID, actor)
c, err = repo.Settings(ctx, domain, c.ID, actor, c.Version, c.Name, false, map[string]string{uuid.NewString(): "read"})
if err != nil {
t.Fatal(err)
}
w = taskRequest(s, token, "GET", "blocks/"+blocks[0].UUID, nil, "")
if !strings.Contains(w.Body.String(), `"available":false`) {
t.Fatal(w.Body)
}
w = taskRequest(s, token, "POST", "blocks", input, "")
if w.Code != 403 {
t.Fatal(w.Code, w.Body)
}
w = taskRequest(s, token, "DELETE", "blocks/"+blocks[0].UUID, nil, "")
if w.Code != 204 {
t.Fatal(w.Code, w.Body)
}
if _, err = repo.Event(ctx, domain, c.ID, actor, blocks[0].EventUID); err != nil {
t.Fatal("unlink deleted event", err)
}
}
func TestTaskPlanningValidationAndIsolation(t *testing.T) {
s, token, domain, actor := taskFixture(t)
ctx := context.Background()
repo, _ := s.calendars()
c, _ := repo.Create(ctx, domain, actor, "Focus")
task, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("task", "Secret"), 0)
if err != nil {
t.Fatal(err)
}
input := map[string]any{"uuid": uuid.NewString(), "task_uuid": task.UUID, "task_version": task.Version, "calendar_uuid": c.ID, "start": "2026-03-08T02:30", "end": "2026-03-08T04:30", "timezone": "America/New_York"}
if w := taskRequest(s, token, "POST", "blocks", input, ""); w.Code != 422 {
t.Fatal("DST gap", w.Code, w.Body)
}
input["start"] = "2026-09-25T10:00:00Z"
input["end"] = "2026-09-25T11:00:00Z"
input["task_version"] = task.Version + 1
if w := taskRequest(s, token, "POST", "blocks", input, ""); w.Code != 412 {
t.Fatal("stale version", w.Code, w.Body)
}
input["task_version"] = task.Version
if w := taskRequest(s, token, "POST", "blocks", input, ""); w.Code != 201 {
t.Fatal(w.Code, w.Body)
}
if _, err = s.store.GetTaskBlock(ctx, domain, uuid.NewString(), input["uuid"].(string)); err == nil {
t.Fatal("other owner access")
}
if _, err = s.store.DB.Exec(`UPDATE principals SET state='readonly' WHERE uuid=?`, actor); err != nil {
t.Fatal(err)
}
if w := taskRequest(s, token, "DELETE", "blocks/"+input["uuid"].(string), nil, ""); w.Code != 403 {
t.Fatal("readonly write", w.Code, w.Body)
}
if w := taskRequest(s, token, "GET", "blocks/"+input["uuid"].(string), nil, ""); w.Code != 200 {
t.Fatal("readonly read", w.Code, w.Body)
}
// A source calendar with no remote request or contents is only metadata.
external, _ := repo.Create(ctx, domain, actor, "Synthetic source")
_, err = repo.SetSource(ctx, domain, external.ID, actor, external.Version, cal.Source{Kind: "ics", URL: "https://example.invalid/calendar.ics", IntervalSeconds: 3600})
if err != nil {
t.Fatal(err)
}
w := taskRequest(s, token, "GET", "calendars", nil, "")
if w.Code != 200 || strings.Contains(w.Body.String(), external.ID) || strings.Contains(w.Body.String(), "example.invalid") {
t.Fatal("source in picker", w.Code, w.Body)
}
}