oficina / memoria Public

Clone
git clone https://amadan.net/oficina/memoria

Plain git — no account needed to clone.

Download

Download this file

1package memoria
2
3import (
4 "context"
5 "io"
6 "log/slog"
7 "net/http"
8 "net/http/httptest"
9 "path/filepath"
10 "strconv"
11 "strings"
12 "testing"
13
14 "amadan.net/oficina/suite"
15 "amadan.net/rastrillo/rastrillo/db"
16
17 "amadan.net/oficina/memoria/internal/memoria/seal"
18)
19
20// Every surface that names a document asks its app of record first (suite
21// revision 10, Paul, 2026-11-01: memoria holds siblings' private documents
22// too, ruled per person). Search always did; the document page, the
23// documents list, "Recently updated" and the delete question did not.
24//
25// MUTATION-TESTED 2026-11-01, each reverted: showDocument's mayOpen call
26// removed → "the document page"; the documents list left unfiltered →
27// "the documents list"; Recently updated left unfiltered → "recently
28// updated"; ownDocument removed from the delete question → "delete";
29// the UNKNOWN branch answering 404 → "an app that did not answer".
30// 2026-11-02: Summary.Sibling forced false → "Delete offered".
31func TestEverySurfaceAsksTheAppOfRecord(t *testing.T) {
32 logger := slog.New(slog.NewTextHandler(io.Discard, nil))
33 d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger)
34 if err != nil {
35 t.Fatal(err)
36 }
37 t.Cleanup(func() { d.Close() })
38 dek := make([]byte, 32)
39 for i := range dek {
40 dek[i] = byte(i * 11)
41 }
42 sealer, err := seal.New(dek)
43 if err != nil {
44 t.Fatal(err)
45 }
46 mux, a, err := AppForTest(d, "http://127.0.0.1:8899", sealer, logger)
47 if err != nil {
48 t.Fatal(err)
49 }
50 cus := &perItemCustody{apps: docsRoster(), give: map[string]suite.Authorization{
51 "theirs": suite.NOT_VISIBLE,
52 "unknown": suite.UNKNOWN,
53 }}
54 a.store.custody = cus
55 ctx := context.Background()
56 ingest := func(spec IngestSpec) int64 {
57 id, err := a.store.Ingest(ctx, spec)
58 if err != nil {
59 t.Fatal(err)
60 }
61 return id
62 }
63 own := ingest(IngestSpec{Source: "upload", Key: "own", Title: "My upload", Body: "Mine."})
64 mine := ingest(IngestSpec{Source: "Docs", Key: "mine", Title: "Shared with me", Body: "Visible.", ItemID: "mine", SiblingRevision: 1})
65 theirs := ingest(IngestSpec{Source: "Docs", Key: "theirs", Title: "Somebody else's private plan", Body: "Secret.", ItemID: "theirs", SiblingRevision: 1})
66 unknown := ingest(IngestSpec{Source: "Docs", Key: "unknown", Title: "Docs could not say", Body: "Maybe.", ItemID: "unknown", SiblingRevision: 1})
67
68 get := func(path string) *httptest.ResponseRecorder {
69 req := httptest.NewRequest("GET", path, nil)
70 req.Header.Set("Accept", "text/html")
71 rec := httptest.NewRecorder()
72 mux.ServeHTTP(rec, req)
73 return rec
74 }
75 doc := func(id int64) string { return "/documents/" + strconv.FormatInt(id, 10) }
76
77 // The document page.
78 if rec := get(doc(mine)); rec.Code != 200 || !strings.Contains(rec.Body.String(), "Shared with me") {
79 t.Errorf("the document page: one shared with me = %d", rec.Code)
80 }
81 if rec := get(doc(own)); rec.Code != 200 {
82 t.Errorf("the document page: memoria's own = %d", rec.Code)
83 }
84 if rec := get(doc(theirs)); rec.Code != 404 || strings.Contains(rec.Body.String(), "Secret") || strings.Contains(rec.Body.String(), "private plan") {
85 t.Errorf("the document page: somebody else's private document = %d", rec.Code)
86 }
87 if rec := get(doc(unknown)); rec.Code != 503 {
88 t.Errorf("an app that did not answer: the document page = %d, want 503", rec.Code)
89 }
90
91 // The documents list.
92 list := get("/documents").Body.String()
93 if !strings.Contains(list, "Shared with me") || !strings.Contains(list, "My upload") {
94 t.Errorf("the documents list: what I may see is missing")
95 }
96 if strings.Contains(list, "private plan") || strings.Contains(list, "could not say") {
97 t.Errorf("the documents list: names a document this person may not see")
98 }
99 if !strings.Contains(list, "rst-withheld") {
100 t.Errorf("the documents list: Docs did not answer for one, and the page does not say so")
101 }
102 // Delete is offered on memoria's own rows only (Fable, 2026-11-02:
103 // Summary.Sibling forced false survived; the link then 404s).
104 if !strings.Contains(list, `href="`+doc(own)+`/delete"`) || strings.Contains(list, `href="`+doc(mine)+`/delete"`) {
105 t.Errorf("the documents list: Delete offered on another app's row, or not on memoria's own")
106 }
107
108 // Recently updated, on the front page.
109 home := get("/").Body.String()
110 if strings.Contains(home, "private plan") || strings.Contains(home, "could not say") {
111 t.Errorf("recently updated: names a document this person may not see")
112 }
113 if !strings.Contains(home, "Shared with me") {
114 t.Errorf("recently updated: what I may see is missing")
115 }
116
117 // Delete: memoria deletes only what it owns.
118 if rec := get(doc(mine) + "/delete"); rec.Code != 404 {
119 t.Errorf("delete: the question for another app's document = %d, want 404", rec.Code)
120 }
121 if rec := get(doc(own) + "/delete"); rec.Code != 200 {
122 t.Errorf("delete: the question for memoria's own = %d", rec.Code)
123 }
124 post := httptest.NewRequest(http.MethodPost, doc(mine)+"/delete", nil)
125 post.Header.Set("Origin", "http://127.0.0.1:8899")
126 post.Header.Set("Accept", "text/html")
127 rec := httptest.NewRecorder()
128 mux.ServeHTTP(rec, post)
129 if rec.Code != 404 {
130 t.Errorf("delete: the POST for another app's document = %d, want 404", rec.Code)
131 }
132 if vis, _, _ := a.store.VisibleDocuments(ctx, suite.Subject{Kind: suite.SubjectAddress, Value: "x@example.com"}, []int64{mine}); !vis[mine] {
133 t.Errorf("delete: another app's document was deleted by the POST")
134 }
135}
136
137// VisibleDocuments refuses to rule with no asker: there is nobody to ask
138// about. Every caller checks first, so this is called directly — a guard
139// nothing can reach is a guard written to pass (AGENTS.md; Fable,
140// 2026-11-02). MUTATION-TESTED 2026-11-02: the check removed → red.
141func TestVisibleDocumentsRefusesWithNoAsker(t *testing.T) {
142 s := storeForTest(t)
143 s.custody = &fixedCustody{apps: docsRoster(), give: suite.VISIBLE}
144 id, err := s.Ingest(context.Background(), IngestSpec{Source: "Docs", Key: "k", Title: "T", Body: "B", ItemID: "k", SiblingRevision: 1})
145 if err != nil {
146 t.Fatal(err)
147 }
148 vis, _, err := s.VisibleDocuments(context.Background(), suite.Subject{}, []int64{id})
149 if err == nil || vis[id] {
150 t.Fatalf("no asker: visible=%v err=%v, want a refusal", vis, err)
151 }
152}
153