| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | "sync/atomic" |
| 8 | "testing" |
| 9 | |
| 10 | access "amadan.net/oficina/agent-access.git" |
| 11 | ) |
| 12 | |
| 13 | func TestAgentNoteLiveMarkerAndVerifierContinuity(t *testing.T) { |
| 14 | for _, field := range []string{"subject", "client", "grant", "team", "scope"} { |
| 15 | t.Run(field, func(t *testing.T) { |
| 16 | f, p := linkedAgentFixture(t) |
| 17 | var checks atomic.Int64 |
| 18 | h := http.NewServeMux() |
| 19 | if e := f.app.mountAgentAccess(h, "https://connect.example.test", "acme", access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| 20 | current := p |
| 21 | if checks.Add(1) > 1 { |
| 22 | switch field { |
| 23 | case "subject": |
| 24 | current.Subject = "different" |
| 25 | case "client": |
| 26 | current.Client = "different" |
| 27 | case "grant": |
| 28 | current.Grant = "different" |
| 29 | case "team": |
| 30 | current.Teams = []string{"green"} |
| 31 | case "scope": |
| 32 | current.Scopes = []string{"notes:read"} |
| 33 | } |
| 34 | } |
| 35 | return current, nil |
| 36 | })); e != nil { |
| 37 | t.Fatal(e) |
| 38 | } |
| 39 | if rec := agentCall(h, "notes_create", agentNoteInput); rec.Code != 403 { |
| 40 | t.Fatal("changed principal accepted", field, rec.Code) |
| 41 | } |
| 42 | assertAgentNoteEmpty(t, f.app) |
| 43 | }) |
| 44 | } |
| 45 | f, p := linkedAgentFixture(t) |
| 46 | active := true |
| 47 | h := mountAgentFixture(t, f, &p, &active) |
| 48 | if rec := agentCall(h, "notes_create", agentNoteInput); rec.Code != 200 { |
| 49 | t.Fatal(rec.Code) |
| 50 | } |
| 51 | if _, e := f.app.db.Writer().Exec("UPDATE memoria_agent_state SET keycheck='damaged'"); e != nil { |
| 52 | t.Fatal(e) |
| 53 | } |
| 54 | for _, input := range []string{agentNoteInput, strings.Replace(agentNoteInput, "a8d99147-249b-40df-a8a6-c311d425e03b", "15bb7913-1316-4b96-bc3a-4b4cded1ded4", 1)} { |
| 55 | if rec := agentCall(h, "notes_create", input); rec.Code != 503 { |
| 56 | t.Fatal("live marker ignored", rec.Code) |
| 57 | } |
| 58 | } |
| 59 | var n int |
| 60 | if e := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM documents").Scan(&n); e != nil || n != 1 { |
| 61 | t.Fatal("bad marker wrote extra note", n, e) |
| 62 | } |
| 63 | } |
| 64 | |