Files
pages/internal/server/task_capture_recreate_test.go
T
Anya Tokugawa 4049acf76e
Build and publish container images / images (push) Successful in 12m0s
Release container images / release (push) Successful in 19s
fix(tasks): recover deleted captures without reviving tombstones
2026-11-08 19:26:50 +09:00

227 lines
8.7 KiB
Go

package server
import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
"testing"
"github.com/google/uuid"
"pageswiki/internal/store"
)
func TestTaskCaptureDeletedRecreation(t *testing.T) {
for _, wiki := range []bool{false, true} {
t.Run(fmt.Sprint(wiki), func(t *testing.T) {
s, token, domain, actor := taskFixture(t)
ctx := context.Background()
id := uuid.NewString()
input := map[string]any{"request_id": id, "title": "Original"}
if wiki {
input["wiki_body"], input["wiki_prefix"] = "Original private body", "/mail"
}
w := taskRequest(s, token, "POST", "capture", input, "")
if w.Code != 201 {
t.Fatal(w.Code, w.Body)
}
var result struct {
Item store.TaskRecord `json:"item"`
Path string `json:"page_path"`
}
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Fatal(err)
}
page, path := result.Item.PageUUID, result.Path
var revision string
if wiki {
r, err := s.store.GetLatestRevision(page)
if err != nil {
t.Fatal(err)
}
revision = r.UUID
}
if w = taskRequest(s, token, "DELETE", "items/"+id, nil, `"1"`); w.Code != 204 {
t.Fatal(w.Code, w.Body)
}
if w = taskRequest(s, token, "GET", "items/"+id, nil, ""); w.Code != 404 {
t.Fatal(w.Code, w.Body)
}
for _, title := range []string{"Original", "Changed"} {
input["title"] = title
if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 410 || !strings.Contains(w.Body.String(), `"deleted"`) {
t.Fatal(w.Code, w.Body)
}
}
input["replaces_deleted"], input["request_id"] = id, store.TaskRecreationID(id)
if wiki {
input["wiki_body"] = "Must not overwrite the retained page"
for _, invalidBody := range []string{"bad\x00body", strings.Repeat("x", 256<<10+1)} {
input["wiki_body"] = invalidBody
if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 422 {
t.Fatal("reused page bypassed body validation", w.Code, w.Body)
}
}
input["wiki_body"] = "Must not overwrite the retained page"
}
// A lost successful response, including concurrent retries, creates one
// successor. It neither revives the original nor revises the Wiki.
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
defer wg.Done()
v := taskRequest(s, token, "POST", "capture", input, "")
if v.Code != 201 {
t.Errorf("concurrent recreation: %d %s", v.Code, v.Body)
}
}()
}
wg.Wait()
w = taskRequest(s, token, "POST", "capture", input, "")
if w.Code != 201 {
t.Fatal(w.Code, w.Body)
}
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Fatal(err)
}
if result.Item.UUID != store.TaskRecreationID(id) || result.Item.UUID == id || result.Item.Title != "Changed" || result.Item.Version != 1 || result.Item.PageUUID != page || result.Path != path {
t.Fatal(result)
}
if wiki {
r, err := s.store.GetLatestRevision(page)
if err != nil || r.UUID != revision || !strings.Contains(r.Body, "Original private body") {
t.Fatal("Wiki changed", r, err)
}
}
for table, expected := range map[string]int{"organizer_items": 1, "organizer_capture_receipts": 2, "organizer_task_deletions": 1, "organizer_revisions": 1} {
var count int
if err := s.store.DB.QueryRow("SELECT count(*) FROM " + table).Scan(&count); err != nil || count != expected {
t.Fatal(table, count, err)
}
}
if _, err := s.store.SaveTask(ctx, domain, actor, store.TaskRecord{UUID: id, Kind: "task", Title: "Offline resurrection", Status: "todo", Bucket: "inbox", Priority: 1}, 0); err != store.ErrTaskDeleted {
t.Fatal("old UUID revived", err)
}
input["title"] = "Different committed recovery"
if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 412 {
t.Fatal(w.Code, w.Body)
}
// Deleting the successor cannot be undone by replaying the recovery.
if w = taskRequest(s, token, "DELETE", "items/"+result.Item.UUID, nil, `"1"`); w.Code != 204 {
t.Fatal(w.Code, w.Body)
}
if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 410 {
t.Fatal(w.Code, w.Body)
}
})
}
}
func TestTaskCaptureRecreationGuards(t *testing.T) {
s, token, domain, actor := taskFixture(t)
id := uuid.NewString()
input := map[string]any{"request_id": id, "title": "Original"}
if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 {
t.Fatal(w.Code, w.Body)
}
recreate := map[string]any{"request_id": store.TaskRecreationID(id), "replaces_deleted": id, "title": "New"}
if w := taskRequest(s, token, "POST", "capture", recreate, ""); w.Code != 412 {
t.Fatal("live capture replaced", w.Code, w.Body)
}
if w := taskRequest(s, token, "DELETE", "items/"+id, nil, `"1"`); w.Code != 204 {
t.Fatal(w.Code, w.Body)
}
other, err := s.store.CreatePasswordAccount(domain, "recreateother", "long fixture password", "Other", "")
if err != nil {
t.Fatal(err)
}
otherToken, err := s.store.CreateAuthSession(domain, other.UUID)
if err != nil {
t.Fatal(err)
}
if w := taskRequest(s, otherToken, "POST", "capture", recreate, ""); w.Code != 404 {
t.Fatal("other owner", w.Code, w.Body)
}
if _, err := s.store.RecreateCapturedTask(context.Background(), "other-domain", actor, store.TaskRecreationID(id), "hash", newTaskRecord("task", "Wrong domain"), "", "", id); err != store.ErrAccessDenied {
t.Fatal("wrong domain", err)
}
if err := s.store.SetPrincipalState(domain, actor, "readonly"); err != nil {
t.Fatal(err)
}
if _, err := s.store.RecreateCapturedTask(context.Background(), domain, actor, store.TaskRecreationID(id), "hash", newTaskRecord("task", "Readonly"), "", "", id); err != store.ErrAccessDenied {
t.Fatal("readonly", err)
}
if err := s.store.SetPrincipalState(domain, actor, "active"); err != nil {
t.Fatal(err)
}
token, err = s.store.CreateAuthSession(domain, actor)
if err != nil {
t.Fatal(err)
}
if err := s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.tasks":false}`); err != nil {
t.Fatal(err)
}
if _, err := s.store.RecreateCapturedTask(context.Background(), domain, actor, store.TaskRecreationID(id), "hash", newTaskRecord("task", "Disabled"), "", "", id); err != store.ErrAccessDenied {
t.Fatal("extension disabled", err)
}
if err := s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.tasks":true}`); err != nil {
t.Fatal(err)
}
for _, invalid := range []string{uuid.NewString(), id, "not-an-id"} {
recreate["request_id"] = invalid
if w := taskRequest(s, token, "POST", "capture", recreate, ""); w.Code != 422 {
t.Fatal("arbitrary recovery ID", w.Code, w.Body)
}
}
recreate["request_id"] = store.TaskRecreationID(id)
// Failure during receipt insertion rolls back the task, page and journals.
if _, err := s.store.DB.Exec(`CREATE TRIGGER reject_recreation BEFORE INSERT ON organizer_capture_receipts BEGIN SELECT RAISE(ABORT,'fixture rejection'); END`); err != nil {
t.Fatal(err)
}
recreate["wiki_body"], recreate["wiki_prefix"] = "New body", "/recreated"
if w := taskRequest(s, token, "POST", "capture", recreate, ""); w.Code != 500 {
t.Fatal(w.Code, w.Body)
}
var count int
if err := s.store.DB.QueryRow(`SELECT count(*) FROM organizer_items`).Scan(&count); err != nil || count != 0 {
t.Fatal("orphan task", count, err)
}
if err := s.store.DB.QueryRow(`SELECT count(*) FROM paths WHERE path LIKE '/recreated/%'`).Scan(&count); err != nil || count != 0 {
t.Fatal("orphan page", count, err)
}
if _, err := s.store.DB.Exec(`DROP TRIGGER reject_recreation`); err != nil {
t.Fatal(err)
}
if w := taskRequest(s, token, "POST", "capture", recreate, ""); w.Code != 201 {
t.Fatal(w.Code, w.Body)
}
if _, err := s.store.GetTask(context.Background(), domain, actor, store.TaskRecreationID(id)); err != nil {
t.Fatal(err)
}
}
func TestTaskCaptureOrphanReceipt(t *testing.T) {
s, token, domain, actor := taskFixture(t)
id := uuid.NewString()
input := map[string]any{"request_id": id, "title": "Original"}
if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 {
t.Fatal(w.Code, w.Body)
}
// Simulate preexisting corruption, not a supported user deletion operation.
if _, err := s.store.DB.Exec(`DELETE FROM organizer_items WHERE domain_uuid=? AND owner_uuid=? AND uuid=?`, domain, actor, id); err != nil {
t.Fatal(err)
}
for _, title := range []string{"Original", "Changed"} {
input["title"] = title
if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 409 || !strings.Contains(w.Body.String(), "capture_missing") {
t.Fatal(w.Code, w.Body)
}
}
input["request_id"], input["replaces_deleted"] = store.TaskRecreationID(id), id
if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 409 || !strings.Contains(w.Body.String(), "capture_missing") {
t.Fatal(w.Code, w.Body)
}
}