oficina / docs Public

Delete: answer a native caller {"ok":true} (spec 2026-11-22 §1)

POST /d/{sqid}/delete with Accept: application/json now answers 200
{"ok":true} once Store.Delete has committed, with nothing read again;
browsers keep their 303 to the list. Written directly rather than with
nativeJSON: in team mode its guarded write re-checks every document the
request read, and the deleted one answered 404 for a delete that had
committed (TestTeamNativeDelete caught it).

The Go matrix: the owner gets ok and the document is gone from its URL
and the list; a writer grantee, a reader grantee, a guest and a team
member who reads it by the team rung get 404 with nothing deleted; an
already-deleted document is 404; under Unknown the lookup fails with 503
(not a page, not ok) and nothing is deleted; the suite feed announces the
deletion; the browser's 303 is unchanged (delete_test.go).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 2ee258cf5325c7bdaecc665b8f45980dfc57808f parent 765e992
3 files changed, +207 −4
  • internal/docs/delete.go +23 −4
  • internal/docs/delete_native_test.go +97 −0
  • internal/docs/delete_team_test.go +87 −0
diff --git a/internal/docs/delete.go b/internal/docs/delete.go
index 7f746c1..ba4d4b9 100644
--- a/internal/docs/delete.go
+++ b/internal/docs/delete.go
@@ -86,10 +86,29 @@ func (h *handlers) deletePost(w http.ResponseWriter, r *http.Request) {
h.ownerLookupFailureOrStatus(w, r, err)
return
}
- // No native answer: no Apple client deletes yet, and the shared
- // lookup-failure path would re-read the deleted document and answer
- // a JSON caller 404 for a delete that committed. One should be added
- // with its client, and a test.
+ // The Mac and iPhone apps' Delete… (spec 2026-11-22-native-redesign
+ // §1): {"ok":true} once Store.Delete has committed, and nothing is
+ // read again (a re-read would find no document and answer 404 for a
+ // delete that happened). The client takes only this answer, or a 404
+ // from its own GET of the document afterwards, as deleted; an older
+ // server's 303 reaches it as "not JSON" and it checks with that GET,
+ // so a client released before this deploys is still safe.
+ //
+ // Written here, not with nativeJSON: in team mode its guarded write
+ // (team_emission.go) checks every document the request read again
+ // before sending, and this one is gone by design, so it would answer
+ // 404 for a delete that committed (TestTeamNativeDelete). The answer
+ // says nothing about the document, and the delete itself was
+ // authorized inside teammode.Guard before and after the write, so
+ // nothing is left to check; the browser's 303 is written the same way.
+ if wantsNative(r) {
+ w.Header().Set("Content-Type", "application/json")
+ w.Header().Set("Cache-Control", "no-store")
+ w.Header().Add("Vary", "Accept")
+ w.WriteHeader(http.StatusOK)
+ _, _ = w.Write([]byte(`{"ok":true}`))
+ return
+ }
http.Redirect(w, r, "/", http.StatusSeeOther)
}
diff --git a/internal/docs/delete_native_test.go b/internal/docs/delete_native_test.go
new file mode 100644
index 0000000..ce28435
--- /dev/null
+++ b/internal/docs/delete_native_test.go
@@ -0,0 +1,97 @@
+package docs_test
+
+import (
+ "encoding/json"
+ "net/http"
+ "strings"
+ "testing"
+
+ "github.com/carlosframework/docs/internal/docs"
+)
+
+// The Mac and iPhone apps' Delete… (spec 2026-11-22 §1, "Delete…"): the
+// owner's POST with Accept: application/json answers {"ok":true} once the
+// delete has committed, with no re-read and no redirect, and the document
+// is gone from its URL and the list. The browser's 303 is unchanged
+// (delete_test.go). The client reads only this JSON as "deleted"; anything
+// else it checks with a GET of /d/{sqid}.
+func TestNativeOwnerDeleteAnswersOK(t *testing.T) {
+ app, _ := docs.NewForTest(t)
+ alice := signInCookies(t, app, "alice@example.com")
+ sqid := createDoc(t, app, alice, "Old+minutes")
+
+ rec := nativeRequest(app, alice, "POST", "/d/"+sqid+"/delete", "")
+ if rec.Code != http.StatusOK {
+ t.Fatalf("native delete: %d %q, want 200", rec.Code, rec.Body.String())
+ }
+ if got := rec.Header().Get("Content-Type"); got != "application/json" {
+ t.Errorf("native delete Content-Type %q, want application/json", got)
+ }
+ if rec.Header().Get("Location") != "" {
+ t.Errorf("native delete redirected to %q", rec.Header().Get("Location"))
+ }
+ var answer map[string]any
+ if err := json.Unmarshal(rec.Body.Bytes(), &answer); err != nil || len(answer) != 1 || answer["ok"] != true {
+ t.Fatalf("native delete answered %q, want exactly {\"ok\":true}", rec.Body.String())
+ }
+ if got := nativeRequest(app, alice, "GET", "/d/"+sqid, ""); got.Code != http.StatusNotFound {
+ t.Errorf("after a native delete the document answers %d, want 404", got.Code)
+ }
+ if list := nativeRequest(app, alice, "GET", "/", ""); strings.Contains(list.Body.String(), sqid) {
+ t.Error("a deleted document is still in the native list")
+ }
+ // Already deleted: 404, the answer a retry after a lost {"ok":true}
+ // gets. The client's re-GET (also 404) then says it is deleted.
+ if again := nativeRequest(app, alice, "POST", "/d/"+sqid+"/delete", ""); again.Code != http.StatusNotFound {
+ t.Errorf("deleting it again: %d, want 404", again.Code)
+ }
+}
+
+// Everybody but the owner gets 404 from a native delete, exactly as a
+// browser does, and nothing is deleted: a writer grantee, a reader
+// grantee, a guest and a team member who can read the document by its
+// team rung. A 404 is ambiguous to the client for exactly this reason,
+// so it re-GETs before it drops anything.
+func TestNativeDeleteByAnyoneButTheOwnerIs404AndDeletesNothing(t *testing.T) {
+ app := members(t, "alice@example.com,bob@example.com,dora@example.com")
+ alice := signInCookies(t, app, "alice@example.com")
+ private := createPrivateDoc(t, app, alice, "Private+plan")
+ team := createDoc(t, app, alice, "Team+plan")
+ for _, grant := range []string{"address=bob@example.com&role=writer", "address=carol@example.com&role=reader"} {
+ if rec := post(t, app, alice, "/d/"+private+"/share", grant); rec.Code != http.StatusSeeOther {
+ t.Fatalf("share %s: %d", grant, rec.Code)
+ }
+ }
+ if rec := post(t, app, alice, "/d/"+private+"/share", "address=dora@example.com&role=reader"); rec.Code != http.StatusSeeOther {
+ t.Fatalf("share with dora: %d", rec.Code)
+ }
+ bob := signInCookies(t, app, "bob@example.com")
+ // Carol is on no allowlist: her reader grant admits her as a guest.
+ carol := signInCookies(t, app, "carol@example.com")
+ dora := signInCookies(t, app, "dora@example.com")
+
+ for _, c := range []struct {
+ who string
+ cookies []*http.Cookie
+ sqid string
+ }{
+ {"a writer grantee", bob, private},
+ {"a guest (reader grantee)", carol, private},
+ {"a member reader grantee", dora, private},
+ {"a team member, not the owner", bob, team},
+ } {
+ if got := nativeRequest(app, c.cookies, "GET", "/d/"+c.sqid, ""); got.Code != http.StatusOK {
+ t.Fatalf("precondition: %s cannot read the document (%d)", c.who, got.Code)
+ }
+ rec := nativeRequest(app, c.cookies, "POST", "/d/"+c.sqid+"/delete", "")
+ if rec.Code != http.StatusNotFound {
+ t.Errorf("%s: native delete %d, want 404", c.who, rec.Code)
+ }
+ if strings.Contains(rec.Body.String(), `"ok"`) {
+ t.Errorf("%s: a refused delete answered ok: %q", c.who, rec.Body.String())
+ }
+ if got := nativeRequest(app, alice, "GET", "/d/"+c.sqid, ""); got.Code != http.StatusOK {
+ t.Errorf("after %s's refused delete the owner gets %d", c.who, got.Code)
+ }
+ }
+}
diff --git a/internal/docs/delete_team_test.go b/internal/docs/delete_team_test.go
index f9bc3fe..9d5a71b 100644
--- a/internal/docs/delete_team_test.go
+++ b/internal/docs/delete_team_test.go
@@ -2,7 +2,10 @@ package docs
import (
"net/http"
+ "net/http/httptest"
+ "strings"
"testing"
+ "time"
)
// Delete in team mode, through the real router and Home sign-in: the
@@ -45,6 +48,90 @@ func TestTeamOwnerDeletesOnlyHerOwnDocument(t *testing.T) {
}
}
+// The native Delete… in team mode (spec 2026-11-22 §1): the owner's JSON
+// POST answers {"ok":true} and the deletion is on the suite feed; another
+// member's team document, which she can read by its rung, and a document
+// shared with her as a writer answer 404 and stay; and under Unknown the
+// lookup fails with its status (503), never an ok and never a page, so
+// the client keeps the document and says it could not delete it.
+func TestTeamNativeDelete(t *testing.T) {
+ f := newTeamRouterFixture(t)
+ signIn := func(address string, subject byte) *routerBrowser {
+ b := f.browser()
+ b.localSignin(t, address)
+ b.linkProof(t, teamSubject(subject), address)
+ routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/"))
+ return b
+ }
+ // A new document is on the team rung (Paul, 2026-11-01).
+ create := func(b *routerBrowser, title string) *Document {
+ sqid := strings.TrimPrefix(routerRedirect(t, b.do("POST", "/documents", "title="+title), ""), "/d/")
+ return &Document{Sqid: sqid, Title: title, ID: f.documentID(t, sqid)}
+ }
+ bob := signIn("bob@example.com", 'b')
+ bobsTeam := create(bob, "Bob+team")
+ alice := signIn("alice@example.com", 'a')
+ native := func(method, path string) *httptest.ResponseRecorder {
+ r := alice.request(method, path, "")
+ r.Header.Set("Accept", "application/json")
+ return alice.serve(r)
+ }
+
+ if r := native("GET", "/d/"+bobsTeam.Sqid); r.Code != http.StatusOK {
+ t.Fatalf("precondition: alice cannot read Bob's team document (%d)", r.Code)
+ }
+ for _, d := range []*Document{bobsTeam, f.shared} {
+ if r := native("POST", "/d/"+d.Sqid+"/delete"); r.Code != http.StatusNotFound || strings.Contains(r.Body.String(), `"ok"`) {
+ t.Errorf("native delete %q (not hers): %d %q, want 404", d.Title, r.Code, r.Body.String())
+ }
+ if deleted(t, f, d) {
+ t.Errorf("%q is deleted after someone else's delete was refused", d.Title)
+ }
+ }
+
+ r := native("POST", "/d/"+f.own.Sqid+"/delete")
+ if r.Code != http.StatusOK || strings.TrimSpace(r.Body.String()) != `{"ok":true}` {
+ t.Fatalf("owner's native delete: %d %q, want 200 {\"ok\":true}", r.Code, r.Body.String())
+ }
+ if !deleted(t, f, f.own) {
+ t.Error("the owner's document is still there after her native delete")
+ }
+ var gone int64
+ if err := f.app.db.Writer().QueryRowContext(t.Context(), "SELECT COUNT(*) FROM suite_feed WHERE document_id = ? AND gone = 1", f.own.ID).Scan(&gone); err != nil {
+ t.Fatalf("read the suite feed: %v", err)
+ }
+ if gone == 0 {
+ t.Error("the native delete was not announced on the suite feed")
+ }
+ if r := native("POST", "/d/"+f.own.Sqid+"/delete"); r.Code != http.StatusNotFound {
+ t.Errorf("deleting it again: %d, want 404", r.Code)
+ }
+
+ // Unknown: the roster cannot say who is on the team.
+ mine := create(alice, "Alice+second")
+ f.directory.change(nil, true)
+ f.clock.step(61 * time.Second)
+ r = native("POST", "/d/"+mine.Sqid+"/delete")
+ if r.Code != http.StatusServiceUnavailable {
+ t.Errorf("native delete under Unknown: %d %q, want 503", r.Code, r.Body.String())
+ }
+ if strings.Contains(r.Header().Get("Content-Type"), "text/html") || strings.Contains(r.Body.String(), `"ok"`) {
+ t.Errorf("native delete under Unknown answered a page or an ok: %q %q", r.Header().Get("Content-Type"), r.Body.String())
+ }
+ if deleted(t, f, mine) {
+ t.Error("a delete under Unknown deleted the document")
+ }
+}
+
+func (f *teamRouterFixture) documentID(t *testing.T, sqid string) int64 {
+ t.Helper()
+ var id int64
+ if err := f.app.db.Writer().QueryRowContext(t.Context(), "SELECT id FROM documents WHERE sqid = ?", sqid).Scan(&id); err != nil {
+ t.Fatalf("read %s: %v", sqid, err)
+ }
+ return id
+}
+
func deleted(t *testing.T, f *teamRouterFixture, d *Document) bool {
t.Helper()
var n int64