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 "bytes"
5 "context"
6 "encoding/json"
7 "net/http"
8 "strings"
9 "sync/atomic"
10 "testing"
11
12 access "amadan.net/oficina/agent-access.git"
13 "amadan.net/oficina/memoria/internal/memoria/seal"
14 "gorm.io/gorm"
15)
16
17func agentNoteCoordinate(t *testing.T, a *app, p access.Principal) string {
18 t.Helper()
19 raw, _ := json.Marshal([]string{a.home.issuer, a.agentIssuer, p.Subject, p.Client, a.agentTeam, "a8d99147-249b-40df-a8a6-c311d425e03b"})
20 key, e := a.store.sealer.Fingerprint("agent.note.coordinate.v1", raw)
21 if e != nil {
22 t.Fatal(e)
23 }
24 return key
25}
26func assertAgentNoteEmpty(t *testing.T, a *app) {
27 t.Helper()
28 for _, table := range []string{"documents", "chunks", "memoria_agent_notes"} {
29 var n int
30 if e := a.db.Writer().QueryRow("SELECT COUNT(*) FROM " + table).Scan(&n); e != nil || n != 0 {
31 t.Fatal("partial note", table, n, e)
32 }
33 }
34}
35func TestAgentNoteAtomicReceiptAndAuthority(t *testing.T) {
36 for _, mode := range []string{"receipt-failure", "chunk-failure", "binding", "unlink", "revoke-reserved", "revoke-final"} {
37 t.Run(mode, func(t *testing.T) {
38 f, p := linkedAgentFixture(t)
39 var checks atomic.Int64
40 mux := http.NewServeMux()
41 if e := f.app.mountAgentAccess(mux, "https://connect.example.test", "acme", access.VerifyFunc(func(context.Context, string) (access.Principal, error) {
42 n := checks.Add(1)
43 if mode == "revoke-reserved" && n >= 2 || mode == "revoke-final" && n >= 3 {
44 return access.Principal{}, access.Fail("unauthenticated", "Revoked.")
45 }
46 return p, nil
47 })); e != nil {
48 t.Fatal(e)
49 }
50 trigger := ""
51 switch mode {
52 case "receipt-failure":
53 trigger = "CREATE TRIGGER fail_receipt BEFORE UPDATE OF document_id ON memoria_agent_notes BEGIN SELECT RAISE(ABORT,'injected'); END"
54 case "chunk-failure":
55 trigger = "CREATE TRIGGER fail_chunk AFTER INSERT ON chunks BEGIN SELECT RAISE(ABORT,'injected'); END"
56 case "binding":
57 trigger = "CREATE TRIGGER lose_binding AFTER INSERT ON chunks BEGIN UPDATE home_identity_links SET person_binding='broken'; END"
58 case "unlink":
59 trigger = "CREATE TRIGGER unlink_after_body AFTER INSERT ON chunks BEGIN DELETE FROM home_identity_links; END"
60 }
61 if trigger != "" {
62 if _, e := f.app.db.Writer().Exec(trigger); e != nil {
63 t.Fatal(e)
64 }
65 }
66 rec := agentCall(mux, "notes_create", agentNoteInput)
67 if rec.Code != 401 && rec.Code != 403 && rec.Code != 503 {
68 t.Fatal("authority/fault succeeded", mode, rec.Code, rec.Body.String())
69 }
70 assertAgentNoteEmpty(t, f.app)
71 })
72 }
73}
74func TestAgentNoteOrphanNeverUpsertsAndMarkerRefuses(t *testing.T) {
75 f, p := linkedAgentFixture(t)
76 active := true
77 h := mountAgentFixture(t, f, &p, &active)
78 key := agentNoteCoordinate(t, f.app, p)
79 id, e := f.app.store.Ingest(t.Context(), IngestSpec{Source: "paste", Key: "agent-" + key, Title: "Later title", Body: "Later body must survive"})
80 if e != nil {
81 t.Fatal(e)
82 }
83 rec := agentCall(h, "notes_create", agentNoteInput)
84 if rec.Code != 503 {
85 t.Fatal("orphan overwritten", rec.Code)
86 }
87 var d Document
88 if e = f.app.db.G.First(&d, id).Error; e != nil {
89 t.Fatal(e)
90 }
91 plain, e := f.app.store.sealer.Open(seal.PurposeBody, d.SealedBody)
92 if e != nil || string(plain) != "Later body must survive" || d.Revision != 1 {
93 t.Fatal("orphan content changed", e)
94 }
95 for _, kind := range []string{"issuer", "home", "team", "key"} {
96 t.Run(kind, func(t *testing.T) {
97 oldIssuer, oldHome, oldTeam, oldSeal := f.app.agentIssuer, f.app.home.issuer, f.app.agentTeam, f.app.store.sealer
98 defer func() {
99 f.app.agentIssuer = oldIssuer
100 f.app.home.issuer = oldHome
101 f.app.agentTeam = oldTeam
102 f.app.store.sealer = oldSeal
103 }()
104 switch kind {
105 case "issuer":
106 f.app.agentIssuer = "https://other-connect.test"
107 case "home":
108 f.app.home.issuer = "https://other-home.test"
109 case "team":
110 f.app.agentTeam = "green"
111 case "key":
112 f.app.store.sealer, _ = seal.New(bytes.Repeat([]byte{9}, 32))
113 }
114 if e := f.app.db.G.Transaction(func(tx *gorm.DB) error { return f.app.bindAgent(tx) }); e == nil {
115 t.Fatal("changed authority accepted")
116 }
117 })
118 }
119 // Even existing pending receipts cannot bootstrap a fresh marker.
120 if _, e = f.app.db.Writer().Exec("INSERT INTO memoria_agent_notes(coordinate,content) VALUES('old','old')"); e != nil {
121 t.Fatal(e)
122 }
123 if _, e = f.app.db.Writer().Exec("DELETE FROM memoria_agent_state"); e != nil {
124 t.Fatal(e)
125 }
126 if e = f.app.db.G.Transaction(func(tx *gorm.DB) error { return f.app.bindAgent(tx) }); e == nil {
127 t.Fatal("unbound receipts blessed")
128 }
129}
130func TestAgentNoteRejectsLossyUnicode(t *testing.T) {
131 f, p := linkedAgentFixture(t)
132 active := true
133 h := mountAgentFixture(t, f, &p, &active)
134 for _, invalid := range []string{`\ud800`, string([]byte{255})} {
135 raw := strings.Replace(agentNoteInput, "Nebula", invalid, 1)
136 if rec := agentCall(h, "notes_create", raw); rec.Code != 422 {
137 t.Fatal("malformed Unicode accepted", rec.Code)
138 }
139 }
140 assertAgentNoteEmpty(t, f.app)
141}
142