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 "net/http"
6 "strings"
7 "sync/atomic"
8 "testing"
9 "time"
10
11 access "amadan.net/oficina/agent-access.git"
12 "amadan.net/rastrillo/rastrillo/db"
13 "gorm.io/gorm"
14)
15
16func TestAgentNoteRevocationWhileWaitingForWriter(t *testing.T) {
17 f, p := linkedAgentFixture(t)
18 var revoked atomic.Bool
19 mux := http.NewServeMux()
20 if e := f.app.mountAgentAccess(mux, "https://connect.example.test", "acme", access.VerifyFunc(func(context.Context, string) (access.Principal, error) {
21 if revoked.Load() {
22 return access.Principal{}, access.Fail("unauthenticated", "Revoked.")
23 }
24 return p, nil
25 })); e != nil {
26 t.Fatal(e)
27 }
28 // If authorization is moved after body work, this fault wins and produces503.
29 if _, e := f.app.db.Writer().Exec("CREATE TRIGGER body_must_not_run BEFORE INSERT ON documents BEGIN SELECT RAISE(ABORT,'body attempted'); END"); e != nil {
30 t.Fatal(e)
31 }
32 var path string
33 if e := f.app.db.Writer().QueryRow("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path); e != nil {
34 t.Fatal(e)
35 }
36 other, e := db.Open(path, f.app.logger)
37 if e != nil {
38 t.Fatal(e)
39 }
40 defer other.Close()
41 held, e := other.Writer().BeginTx(t.Context(), nil)
42 if e != nil {
43 t.Fatal(e)
44 }
45 defer held.Rollback()
46 if _, e = held.Exec("UPDATE memoria_agent_state SET keycheck=keycheck"); e != nil {
47 t.Fatal(e)
48 }
49 // The separate SQLite connection holds the writer; WAL identity reads finish.
50 // Observe entry into the real reservation statement after BOTH Home lookups.
51 attempted := make(chan struct{})
52 var once atomic.Bool
53 if e = f.app.db.G.Callback().Raw().Before("gorm:raw").Register("test:agent_writer", func(tx *gorm.DB) {
54 if strings.HasPrefix(tx.Statement.SQL.String(), "INSERT INTO memoria_agent_state") && once.CompareAndSwap(false, true) {
55 close(attempted)
56 }
57 }); e != nil {
58 t.Fatal(e)
59 }
60 defer f.app.db.G.Callback().Raw().Remove("test:agent_writer")
61 result := make(chan int, 1)
62 go func() { result <- agentCall(mux, "notes_create", agentNoteInput).Code }()
63 select {
64 case <-attempted:
65 case <-time.After(3 * time.Second):
66 t.Fatal("request never reached reserved-writer statement")
67 }
68 select {
69 case code := <-result:
70 t.Fatal("writer did not block", code)
71 case <-time.After(20 * time.Millisecond):
72 }
73 revoked.Store(true)
74 if e = held.Commit(); e != nil {
75 t.Fatal(e)
76 }
77 select {
78 case code := <-result:
79 if code != 401 {
80 t.Fatal("body attempted before fresh delegation check", code)
81 }
82 case <-time.After(3 * time.Second):
83 t.Fatal("writer did not resume")
84 }
85 assertAgentNoteEmpty(t, f.app)
86}
87func TestAgentNoteCompletedRetryRechecksBeforeCommit(t *testing.T) {
88 f, p := linkedAgentFixture(t)
89 active := true
90 h := mountAgentFixture(t, f, &p, &active)
91 rec := agentCall(h, "notes_create", agentNoteInput)
92 made := agentNoteResultFrom(t, rec.Code, rec.Body.Bytes())
93 var checks atomic.Int64
94 mux := http.NewServeMux()
95 if e := f.app.mountAgentAccess(mux, f.app.agentIssuer, "acme", access.VerifyFunc(func(context.Context, string) (access.Principal, error) {
96 if checks.Add(1) >= 3 {
97 return access.Principal{}, access.Fail("unauthenticated", "Revoked.")
98 }
99 return p, nil
100 })); e != nil {
101 t.Fatal(e)
102 }
103 if rec = agentCall(mux, "notes_create", agentNoteInput); rec.Code != 401 {
104 t.Fatal("completed retry skipped final check", rec.Code)
105 }
106 var d Document
107 if e := f.app.db.G.First(&d, made.ID).Error; e != nil || d.Revision != 1 {
108 t.Fatal("retry changed content", d.Revision, e)
109 }
110}
111