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.
This commit is contained in:
1 parent
288e6e94ac
commit
c1175d389f
699 files changed
+110197
-4117
No files matched your search
@@ -0,0 +1,198 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestTaskFocusLifecycle(t *testing.T) {
|
||||
s, token, domain, actor := taskFixture(t)
|
||||
ctx := context.Background()
|
||||
now := time.Date(2026, 9, 25, 1, 0, 0, 0, time.UTC)
|
||||
task, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("task", "Secret focus title"), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f := store.TaskFocus{UUID: uuid.NewString(), TaskUUID: task.UUID, Phase: "focus", DurationMS: 25 * 60000}
|
||||
initial := f
|
||||
f, err = s.store.ChangeTaskFocus(ctx, domain, actor, "start", f, 0, now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if f.Version != 1 || f.DeadlineMS != now.Add(25*time.Minute).UnixMilli() {
|
||||
t.Fatal(f)
|
||||
}
|
||||
retry, err := s.store.ChangeTaskFocus(ctx, domain, actor, "start", initial, 0, now.Add(time.Minute))
|
||||
if err != nil || retry.DeadlineMS != f.DeadlineMS {
|
||||
t.Fatal("retry reset", retry, err)
|
||||
}
|
||||
f, err = s.store.ChangeTaskFocus(ctx, domain, actor, "pause", f, 1, now.Add(10*time.Minute))
|
||||
if err != nil || f.RemainingMS != 15*60000 || f.DeadlineMS != 0 {
|
||||
t.Fatal(f, err)
|
||||
}
|
||||
if _, err = s.store.ChangeTaskFocus(ctx, domain, actor, "resume", f, 1, now); !errors.Is(err, store.ErrTaskConflict) {
|
||||
t.Fatal("stale transition", err)
|
||||
}
|
||||
f, err = s.store.ChangeTaskFocus(ctx, domain, actor, "resume", f, 2, now.Add(time.Hour))
|
||||
if err != nil || f.DeadlineMS != now.Add(75*time.Minute).UnixMilli() {
|
||||
t.Fatal(f, err)
|
||||
}
|
||||
if n, err := s.store.CompleteTaskFocus(ctx, now.Add(74*time.Minute), 100); err != nil || n != 0 {
|
||||
t.Fatal(n, err)
|
||||
}
|
||||
if n, err := s.store.CompleteTaskFocus(ctx, now.Add(76*time.Minute), 100); err != nil || n != 1 {
|
||||
t.Fatal(n, err)
|
||||
}
|
||||
if n, err := s.store.CompleteTaskFocus(ctx, now.Add(77*time.Minute), 100); err != nil || n != 0 {
|
||||
t.Fatal(n, err)
|
||||
}
|
||||
latest, err := s.store.LatestTaskFocus(ctx, domain, actor)
|
||||
if err != nil || latest.State != "completed" || latest.Version != 4 {
|
||||
t.Fatal(latest, err)
|
||||
}
|
||||
var notifications, changes int
|
||||
s.store.DB.QueryRow(`SELECT count(*) FROM notification_inbox WHERE kind=?`, store.TaskFocusEventType).Scan(¬ifications)
|
||||
s.store.DB.QueryRow(`SELECT count(*) FROM organizer_focus_changes WHERE session_uuid=?`, f.UUID).Scan(&changes)
|
||||
if notifications != 1 || changes != 4 {
|
||||
t.Fatal(notifications, changes)
|
||||
}
|
||||
fresh, _ := s.store.GetTask(ctx, domain, actor, task.UUID)
|
||||
if fresh.Status != "todo" || fresh.Version != task.Version {
|
||||
t.Fatal("timer completed task", fresh)
|
||||
}
|
||||
w := taskRequest(s, token, "GET", "focus", nil, "")
|
||||
if w.Code != 200 || strings.Contains(w.Body.String(), task.Title) || !strings.Contains(w.Body.String(), "server_now_ms") {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
if _, err = s.store.LatestTaskFocus(ctx, domain, uuid.NewString()); err == nil {
|
||||
t.Fatal("unauthorized")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskFocusConcurrencyAndRollback(t *testing.T) {
|
||||
s, _, domain, actor := taskFixture(t)
|
||||
ctx := context.Background()
|
||||
now := time.Now()
|
||||
var wg sync.WaitGroup
|
||||
errs := make(chan error, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
_, err := s.store.ChangeTaskFocus(ctx, domain, actor, "start", store.TaskFocus{UUID: uuid.NewString(), Phase: "focus", DurationMS: 60000}, 0, now)
|
||||
errs <- err
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
close(errs)
|
||||
success, conflict := 0, 0
|
||||
for err := range errs {
|
||||
if err == nil {
|
||||
success++
|
||||
} else if errors.Is(err, store.ErrTaskConflict) {
|
||||
conflict++
|
||||
} else {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if success != 1 || conflict != 1 {
|
||||
t.Fatal(success, conflict)
|
||||
}
|
||||
if _, err := s.store.DB.Exec(`CREATE TRIGGER fail_focus_notice BEFORE INSERT ON notification_inbox BEGIN SELECT RAISE(ABORT,'synthetic notice failure'); END`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.store.CompleteTaskFocus(ctx, now.Add(2*time.Minute), 100); err == nil {
|
||||
t.Fatal("failure missing")
|
||||
}
|
||||
f, _ := s.store.LatestTaskFocus(ctx, domain, actor)
|
||||
if f.State != "running" || f.Version != 1 {
|
||||
t.Fatal("partial completion", f)
|
||||
}
|
||||
if _, err := s.store.DB.Exec(`DROP TRIGGER fail_focus_notice`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.store.CompleteTaskFocus(ctx, now.Add(2*time.Minute), 100); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.store.ChangeTaskFocus(ctx, domain, actor, "cancel", *f, 1, now); !errors.Is(err, store.ErrTaskConflict) {
|
||||
t.Fatal("late cancel", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskFocusRevokedAndMissedNotifications(t *testing.T) {
|
||||
for _, mode := range []string{"cancel", "revoked", "disabled", "missed"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
s, _, domain, actor := taskFixture(t)
|
||||
ctx := context.Background()
|
||||
now := time.Now()
|
||||
f, err := s.store.ChangeTaskFocus(ctx, domain, actor, "start", store.TaskFocus{UUID: uuid.NewString(), Phase: "break", DurationMS: 60000}, 0, now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
switch mode {
|
||||
case "cancel":
|
||||
_, err = s.store.ChangeTaskFocus(ctx, domain, actor, "cancel", f, 1, now)
|
||||
case "revoked":
|
||||
_, err = s.store.DB.Exec(`UPDATE domain_memberships SET state='suspended' WHERE domain_uuid=? AND principal_uuid=?`, domain, actor)
|
||||
case "disabled":
|
||||
_, err = s.store.DB.Exec(`INSERT INTO config(domain_uuid,key,value,updated_at) VALUES(?,'system_extension_states','{"organizer.tasks":false}',0) ON CONFLICT(domain_uuid,key) DO UPDATE SET value=excluded.value`, domain)
|
||||
case "missed":
|
||||
now = now.Add(48 * time.Hour)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.CompleteTaskFocus(ctx, now.Add(2*time.Minute), 100); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var count int
|
||||
s.store.DB.QueryRow(`SELECT count(*) FROM notification_inbox WHERE kind=?`, store.TaskFocusEventType).Scan(&count)
|
||||
if count != 0 {
|
||||
t.Fatal("unwanted notice", count)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskFocusAPI(t *testing.T) {
|
||||
s, token, domain, actor := taskFixture(t)
|
||||
input := map[string]any{"uuid": uuid.NewString(), "action": "start", "phase": "focus", "minutes": 25, "version": 0}
|
||||
w := taskRequest(s, token, "POST", "focus", input, "")
|
||||
if w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
if w = taskRequest(s, token, "POST", "focus", input, ""); w.Code != 200 {
|
||||
t.Fatal("retry", w.Code, w.Body)
|
||||
}
|
||||
input["action"] = "pause"
|
||||
input["version"] = 1
|
||||
if w = taskRequest(s, token, "POST", "focus", input, ""); w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
if w = taskRequest(s, token, "POST", "focus", input, ""); w.Code != 412 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
if _, err := s.store.DB.Exec(`UPDATE principals SET state='readonly' WHERE uuid=?`, actor); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
input["version"] = 2
|
||||
input["action"] = "resume"
|
||||
if w = taskRequest(s, token, "POST", "focus", input, ""); w.Code != 403 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
if w = taskRequest(s, token, "GET", "focus", nil, ""); w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
var n int
|
||||
s.store.DB.QueryRow(`SELECT count(*) FROM organizer_focus WHERE domain_uuid=?`, domain).Scan(&n)
|
||||
if n != 1 {
|
||||
t.Fatal(n)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user