| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "sync/atomic" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | access "amadan.net/oficina/agent-access.git" |
| 13 | "amadan.net/rastrillo/rastrillo/db" |
| 14 | "github.com/google/uuid" |
| 15 | "gorm.io/gorm" |
| 16 | ) |
| 17 | |
| 18 | func TestAgentNoteUpdateIndexFailureAndRestart(t *testing.T) { |
| 19 | for _, mode := range []string{"index", "index-receipt", "noop-index"} { |
| 20 | t.Run(mode, func(t *testing.T) { |
| 21 | f, p := linkedAgentFixture(t) |
| 22 | active := true |
| 23 | h := mountAgentFixture(t, f, &p, &active) |
| 24 | id, err := f.app.store.Ingest(t.Context(), IngestSpec{Source: "paste", Key: "recovery", Title: "before", Body: "before body"}) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | if mode == "index-receipt" { |
| 29 | if err := f.app.db.G.Exec("CREATE TRIGGER fail_index BEFORE UPDATE OF indexed ON memoria_agent_note_updates BEGIN SELECT RAISE(ABORT,'injected'); END").Error; err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | } else { |
| 33 | f.app.store.ix.Close() |
| 34 | } |
| 35 | title, body := "after", "after body" |
| 36 | if mode == "noop-index" { |
| 37 | title, body = "before", "before body" |
| 38 | } |
| 39 | update := noteUpdateJSON(id, 1, uuid.NewString(), title, body) |
| 40 | first := agentCall(h, "notes_update", update) |
| 41 | if first.Code != 200 || !strings.Contains(first.Body.String(), `"indexed":false`) { |
| 42 | t.Fatal("false indexing success", first.Code, first.Body.String()) |
| 43 | } |
| 44 | if _, err := f.app.store.Ingest(t.Context(), IngestSpec{Source: "paste", Key: "recovery", Title: "latest", Body: "latest searchable quasar"}); err != nil && !errors.Is(err, ErrIndexLag) { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | reopened := reopenAgentNoteFixture(t, f, true) |
| 48 | h = mountAgentFixture(t, reopened, &p, &active) |
| 49 | if w := agentCall(h, "notes_update", update); w.Code != 200 || w.Body.String() != first.Body.String() { |
| 50 | t.Fatal("replay changed receipt", w.Code, w.Body.String()) |
| 51 | } |
| 52 | if w := agentCall(h, "notes_search", `{"team":"acme","query":"quasar"}`); w.Code != 200 || !strings.Contains(w.Body.String(), "latest searchable quasar") { |
| 53 | t.Fatal("retry restored old index", w.Code, w.Body.String()) |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestAgentNoteUpdateRetryFinalGuardWithoutResponse(t *testing.T) { |
| 60 | f, p := linkedAgentFixture(t) |
| 61 | active := true |
| 62 | h := mountAgentFixture(t, f, &p, &active) |
| 63 | id, err := f.app.store.Ingest(t.Context(), IngestSpec{Source: "paste", Key: "retry", Title: "before", Body: "before"}) |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | update := noteUpdateJSON(id, 1, uuid.NewString(), "after", "after") |
| 68 | if w := agentCall(h, "notes_update", update); w.Code != 200 { |
| 69 | t.Fatal(w.Code) |
| 70 | } |
| 71 | checks := 0 |
| 72 | state := &agentRequest{verify: func(context.Context, access.Principal, string) error { |
| 73 | checks++ |
| 74 | if checks == 2 { |
| 75 | return access.Fail("unauthenticated", "Revoked.") |
| 76 | } |
| 77 | return nil |
| 78 | }} |
| 79 | ctx := context.WithValue(t.Context(), agentBoundaryKey{}, state) |
| 80 | address, err := f.app.agentAddress(ctx, p, "acme") |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | if _, err := f.app.agentUpdateNote(ctx, p, []byte(update), address); err == nil || access.SafeFailure(err).Code != "unauthenticated" || checks != 2 { |
| 85 | t.Fatal("replay skipped commit guard", err, checks) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestAgentNoteUpdateRevocationWaitingForWriter(t *testing.T) { |
| 90 | f, p := linkedAgentFixture(t) |
| 91 | active := true |
| 92 | mountAgentFixture(t, f, &p, &active) |
| 93 | id, err := f.app.store.Ingest(t.Context(), IngestSpec{Source: "paste", Key: "waiting", Title: "before", Body: "before"}) |
| 94 | if err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | var revoked atomic.Bool |
| 98 | h := http.NewServeMux() |
| 99 | if err := f.app.mountAgentAccess(h, f.app.agentIssuer, "acme", access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| 100 | if revoked.Load() { |
| 101 | return access.Principal{}, access.Fail("unauthenticated", "Revoked.") |
| 102 | } |
| 103 | return p, nil |
| 104 | })); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | var path string |
| 108 | if err := f.app.db.Writer().QueryRow("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path); err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | other, err := db.Open(path, f.app.logger) |
| 112 | if err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | defer other.Close() |
| 116 | held, err := other.Writer().BeginTx(t.Context(), nil) |
| 117 | if err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | defer held.Rollback() |
| 121 | if _, err := held.Exec("UPDATE memoria_agent_state SET keycheck=keycheck"); err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | attempted := make(chan struct{}) |
| 125 | var once atomic.Bool |
| 126 | if err := f.app.db.G.Callback().Raw().Before("gorm:raw").Register("test:update-wait", func(tx *gorm.DB) { |
| 127 | if strings.HasPrefix(tx.Statement.SQL.String(), "INSERT INTO memoria_agent_state") && once.CompareAndSwap(false, true) { |
| 128 | close(attempted) |
| 129 | } |
| 130 | }); err != nil { |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | defer f.app.db.G.Callback().Raw().Remove("test:update-wait") |
| 134 | result := make(chan int, 1) |
| 135 | go func() { |
| 136 | result <- agentCall(h, "notes_update", noteUpdateJSON(id, 1, uuid.NewString(), "after", "after")).Code |
| 137 | }() |
| 138 | select { |
| 139 | case <-attempted: |
| 140 | case <-time.After(3 * time.Second): |
| 141 | t.Fatal("no writer attempt") |
| 142 | } |
| 143 | revoked.Store(true) |
| 144 | if err := held.Commit(); err != nil { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | select { |
| 148 | case code := <-result: |
| 149 | if code != 401 { |
| 150 | t.Fatal(code) |
| 151 | } |
| 152 | case <-time.After(3 * time.Second): |
| 153 | t.Fatal("writer remained blocked") |
| 154 | } |
| 155 | if title, body, err := f.app.store.Body(t.Context(), id); err != nil || title != "before" || body != "before" { |
| 156 | t.Fatal("revoked edit committed", err) |
| 157 | } |
| 158 | } |
| 159 | |