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 "encoding/base64"
6 "encoding/json"
7 "errors"
8 "reflect"
9 "sort"
10 "strconv"
11 "strings"
12 "testing"
13
14 "amadan.net/oficina/suite"
15
16 "amadan.net/oficina/memoria/internal/memoria/provider"
17 "amadan.net/oficina/memoria/internal/memoria/seal"
18)
19
20// GATE 43 (feedback) β€” Β§8.11's πŸ‘/πŸ‘Ž, kept as Β§8.10 keeps a correction:
21// coordinates and the person's own words, never passage text.
22//
23// MUTATION-TESTED 2026-10-27, each reverted: see the case comments.
24func TestGate43Feedback(t *testing.T) {
25 paul := suite.Subject{Kind: suite.SubjectAddress, Value: "paul@tito.io"}
26 doc := suite.Subject{Kind: suite.SubjectAddress, Value: "doc@tito.io"}
27 ctx := context.Background()
28 const answerText = "Paul Campbell is the chief executive."
29 setup := func(t *testing.T) (*Store, *AIAnswer) {
30 s := factCorpus(t, &perItemCustody{apps: docsRoster()}, [][3]string{
31 {"p", "Paul Campbell", "## Roles\n\nPaul Campbell is Chief Executive Officer."},
32 {"l", "Leadership Team", "## Leadership\n\nPaul Campbell, Co-founder. Doc Parsons, Co-founder."},
33 })
34 s.answerer = &fakeAnswerer{reply: []provider.Sentence{{Text: answerText, Cite: []int{1}}}}
35 a, err := s.Answer(ctx, paul, "who is Paul Campbell")
36 if err != nil || a == nil || a.Feedback == "" {
37 t.Fatalf("answer = %+v, %v; want a handle", a, err)
38 }
39 return s, a
40 }
41
42 // PRESENCE. Mutation: Cited built from every passage sent rather than
43 // the cited ones β†’ "cited". Others: the asker check in the handle,
44 // the reporter print in reads and deletes, and the verdict check,
45 // each removed β†’ red in "only your own".
46 t.Run("a verdict is kept, with the cited passages as links", func(t *testing.T) {
47 s, a := setup(t)
48 id, q, err := s.SaveFeedback(ctx, paul, a.Feedback, "up")
49 if err != nil || id == 0 || q != "who is Paul Campbell" {
50 t.Fatalf("save = %d, %q, %v", id, q, err)
51 }
52 list, err := s.Feedback(ctx, paul)
53 if err != nil || len(list) != 1 || !list[0].Up || list[0].Query != q {
54 t.Fatalf("list = %+v, %v", list, err)
55 }
56 want := a.Blocks()[0].Cites[0].Href
57 if len(list[0].Cited) != 1 || list[0].Cited[0].Href != want {
58 t.Fatalf("cited = %+v, want the one passage cited, %s", list[0].Cited, want)
59 }
60 // An unchanged source says nothing. Mutation: revisions recorded
61 // as zero β†’ "changed since" on a source nobody touched.
62 if st := list[0].Cited[0].Status; st != "" {
63 t.Errorf("an unchanged source says %q", st)
64 }
65 // The handle is sealed under its own purpose. Mutation: sealed as
66 // feedback.body β†’ it opens as a stored row.
67 ct, _ := base64.RawURLEncoding.DecodeString(a.Feedback)
68 if _, err := s.sealer.Open(seal.PurposeFeedback, ct); err == nil {
69 t.Error("a handle opens under the stored row's purpose")
70 }
71 if _, err := s.SetFeedbackNote(ctx, paul, id, " it missed the CTO role "); err != nil {
72 t.Fatal(err)
73 }
74 if v, _ := s.FeedbackFor(ctx, paul, id); v.Note != "it missed the CTO role" {
75 t.Errorf("note = %q", v.Note)
76 }
77 })
78
79 // What is stored, decrypted: coordinates and the person's words, and
80 // no passage or answer text β€” the exact key set, so a field added to
81 // carry text fails here before any text is looked for.
82 t.Run("no passage text is stored", func(t *testing.T) {
83 s, a := setup(t)
84 id, _, err := s.SaveFeedback(ctx, paul, a.Feedback, "down")
85 if err != nil {
86 t.Fatal(err)
87 }
88 var row AnswerFeedback
89 if err := s.db.G.Where("id = ?", id).First(&row).Error; err != nil {
90 t.Fatal(err)
91 }
92 raw, err := s.sealer.Open(seal.PurposeFeedback, row.SealedFeedback)
93 if err != nil {
94 t.Fatal(err)
95 }
96 var m map[string]any
97 if err := json.Unmarshal(raw, &m); err != nil {
98 t.Fatal(err)
99 }
100 var keys []string
101 for k := range m {
102 keys = append(keys, k)
103 }
104 sort.Strings(keys)
105 if want := []string{"cites", "model", "note", "provider", "query", "sent"}; !reflect.DeepEqual(keys, want) {
106 t.Errorf("stored keys = %v, want %v", keys, want)
107 }
108 for _, text := range []string{answerText, "Chief Executive Officer", "Co-founder", "Leadership Team"} {
109 if strings.Contains(string(raw), text) {
110 t.Errorf("stored text: the payload holds %q", text)
111 }
112 }
113 if row.Verdict != "down" {
114 t.Errorf("verdict = %q", row.Verdict)
115 }
116 })
117
118 // Refusals, each a 404 to the handler. Mutations: the asker check in
119 // openFeedbackHandle removed β†’ "another person's handle"; the
120 // reporter print dropped from ownFeedback's query β†’ "another
121 // person's id"; the verdict check removed β†’ "a third verdict".
122 t.Run("only your own, only what was shown", func(t *testing.T) {
123 s, a := setup(t)
124 if _, _, err := s.SaveFeedback(ctx, doc, a.Feedback, "up"); !errors.Is(err, ErrFeedbackNotFound) {
125 t.Errorf("another person's handle: err = %v", err)
126 }
127 ct, _ := base64.RawURLEncoding.DecodeString(a.Feedback)
128 ct[len(ct)-1] ^= 1
129 if _, _, err := s.SaveFeedback(ctx, paul, base64.RawURLEncoding.EncodeToString(ct), "up"); !errors.Is(err, ErrFeedbackNotFound) {
130 t.Errorf("a tampered handle: err = %v", err)
131 }
132 if _, _, err := s.SaveFeedback(ctx, paul, "not a handle", "up"); !errors.Is(err, ErrFeedbackNotFound) {
133 t.Errorf("not a handle: err = %v", err)
134 }
135 if _, _, err := s.SaveFeedback(ctx, paul, a.Feedback, "sideways"); !errors.Is(err, ErrFeedbackNotFound) {
136 t.Errorf("a third verdict: err = %v", err)
137 }
138 id, _, err := s.SaveFeedback(ctx, paul, a.Feedback, "down")
139 if err != nil {
140 t.Fatal(err)
141 }
142 if _, err := s.FeedbackFor(ctx, doc, id); !errors.Is(err, ErrFeedbackNotFound) {
143 t.Errorf("another person's id (read): err = %v", err)
144 }
145 if _, err := s.SetFeedbackNote(ctx, doc, id, "x"); !errors.Is(err, ErrFeedbackNotFound) {
146 t.Errorf("another person's id (note): err = %v", err)
147 }
148 if err := s.DeleteFeedback(ctx, doc, id); !errors.Is(err, ErrFeedbackNotFound) {
149 t.Errorf("another person's id (delete): err = %v", err)
150 }
151 if list, _ := s.Feedback(ctx, doc); len(list) != 0 {
152 t.Errorf("another person lists %d of paul's ratings", len(list))
153 }
154 if err := s.DeleteFeedback(ctx, paul, id); err != nil {
155 t.Errorf("own delete: %v", err)
156 }
157 if list, _ := s.Feedback(ctx, paul); len(list) != 0 {
158 t.Errorf("after delete, %d remain", len(list))
159 }
160 })
161
162 // A deleted document's coordinate is a link that says so; nothing of
163 // it survives in the rating.
164 t.Run("a deleted source", func(t *testing.T) {
165 s, a := setup(t)
166 id, _, err := s.SaveFeedback(ctx, paul, a.Feedback, "down")
167 if err != nil {
168 t.Fatal(err)
169 }
170 cited := a.Blocks()[0].Cites[0]
171 docID, err := strconv.ParseInt(strings.SplitN(strings.TrimPrefix(cited.Href, "/documents/"), "#", 2)[0], 10, 64)
172 if err != nil {
173 t.Fatalf("href %q: %v", cited.Href, err)
174 }
175 if err := s.Delete(ctx, docID); err != nil {
176 t.Fatal(err)
177 }
178 v, err := s.FeedbackFor(ctx, paul, id)
179 if err != nil || len(v.Cited) != 1 || v.Cited[0].Status != "no longer available" {
180 t.Errorf("after the source was deleted: %+v, %v", v, err)
181 }
182 })
183}
184