| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "html/template" |
| 8 | "reflect" |
| 9 | "strings" |
| 10 | "sync" |
| 11 | "testing" |
| 12 | "unicode/utf8" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/equipo" |
| 15 | ) |
| 16 | |
| 17 | func TestConversationPrivateAndThreadShared(t *testing.T) { |
| 18 | f := newFixture(t) |
| 19 | if w := f.get(t, owner, "/conversation"); w.Code != 200 { |
| 20 | t.Fatalf("fixture session: %d", w.Code) |
| 21 | } |
| 22 | secret := send(t, f, owner, "", "private history", "secret") |
| 23 | send(t, f, owner, "", "second secret", "secret2") |
| 24 | page, e := f.Store.Conversation(ctx, owner, "", 1) |
| 25 | must(t, e) |
| 26 | if len(page.Messages) != 1 || page.NextCursor == "" { |
| 27 | t.Fatal("missing private page") |
| 28 | } |
| 29 | other, e := f.Store.Conversation(ctx, peer, "", 50) |
| 30 | must(t, e) |
| 31 | if len(other.Messages) != 0 { |
| 32 | t.Fatal("private conversation leaked") |
| 33 | } |
| 34 | if _, e = f.Store.Conversation(ctx, peer, page.NextCursor, 1); !errors.Is(e, equipo.ErrNotFound) { |
| 35 | t.Fatal("private cursor accepted", e) |
| 36 | } |
| 37 | if _, e = f.Store.SendMessage(ctx, peer, secret.ConversationID, "intrude", equipo.Mutation{RequestID: "intrude"}); !errors.Is(e, equipo.ErrNotFound) { |
| 38 | t.Fatal("private conversation ID accepted as thread", e) |
| 39 | } |
| 40 | task := f.task(t, owner, "own-task") |
| 41 | if _, e = f.Store.RouteMessage(ctx, peer, secret.ID, task.ID, equipo.Mutation{RequestID: "steal"}); !errors.Is(e, equipo.ErrNotFound) { |
| 42 | t.Fatal("private source leaked", e) |
| 43 | } |
| 44 | msg := send(t, f, owner, task.ID, "shared discussion", "discussion") |
| 45 | thread, e := f.Store.TaskThread(ctx, peer, task.ID, "", 50) |
| 46 | must(t, e) |
| 47 | if len(thread.Messages) != 1 || thread.Messages[0].ID != msg.ID || thread.Messages[0].ConversationID != "" { |
| 48 | t.Fatal("thread visibility or private provenance", thread) |
| 49 | } |
| 50 | send(t, f, peer, msg.ThreadID, "ordinary discussion", "peer-discussion") |
| 51 | if f.count(t, "SELECT count(*) FROM conversation_outboxes WHERE purpose='coordinator'") != 2 { |
| 52 | t.Fatal("thread discussion queued execution instructions") |
| 53 | } |
| 54 | f.exec(t, "UPDATE members SET active=0 WHERE address=?", peer.Address) |
| 55 | if _, e = f.Store.TaskThread(ctx, peer, task.ID, "", 50); !errors.Is(e, equipo.ErrForbidden) { |
| 56 | t.Fatal("removed member read thread", e) |
| 57 | } |
| 58 | if _, e = f.Store.SendMessage(ctx, peer, msg.ThreadID, "ordinary discussion", equipo.Mutation{RequestID: "peer-discussion"}); !errors.Is(e, equipo.ErrForbidden) { |
| 59 | t.Fatal("removed member replayed", e) |
| 60 | } |
| 61 | if w := f.get(t, peer, "/conversation"); w.Code == 200 { |
| 62 | t.Fatal("removed member fixture session still works") |
| 63 | } |
| 64 | f.exec(t, "ALTER TABLE members RENAME TO unavailable_members") |
| 65 | if _, e = f.Store.Conversation(ctx, owner, "", 50); e == nil { |
| 66 | t.Fatal("roster lookup failed open") |
| 67 | } |
| 68 | } |
| 69 | func TestConversationRouteOnce(t *testing.T) { |
| 70 | f := newFixture(t) |
| 71 | task := f.createTask(t) |
| 72 | if f.count(t, "SELECT count(*) FROM conversation_threads WHERE task_id=?", task.ID) != 1 { |
| 73 | t.Fatal("task creation omitted thread") |
| 74 | } |
| 75 | secret := send(t, f, owner, "", "selected reply", "source") |
| 76 | send(t, f, owner, "", "keep private", "history") |
| 77 | m, e := f.Store.RouteMessage(ctx, owner, secret.ID, task.ID, equipo.Mutation{RequestID: "route", Revision: 1}) |
| 78 | must(t, e) |
| 79 | again, e := f.Store.RouteMessage(ctx, owner, secret.ID, task.ID, equipo.Mutation{RequestID: "route", Revision: 1}) |
| 80 | must(t, e) |
| 81 | third, e := f.Store.RouteMessage(ctx, owner, secret.ID, task.ID, equipo.Mutation{RequestID: "route-again"}) |
| 82 | must(t, e) |
| 83 | if !reflect.DeepEqual(m, again) || !reflect.DeepEqual(m, third) { |
| 84 | t.Fatal("route replay changed reply") |
| 85 | } |
| 86 | if f.count(t, "SELECT count(*) FROM conversation_threads WHERE task_id=?", task.ID) != 1 || f.count(t, "SELECT count(*) FROM conversation_routes") != 1 { |
| 87 | t.Fatal("duplicate route or thread") |
| 88 | } |
| 89 | main, e := f.Store.Conversation(ctx, owner, "", 50) |
| 90 | must(t, e) |
| 91 | shared, e := f.Store.TaskThread(ctx, peer, task.ID, "", 50) |
| 92 | must(t, e) |
| 93 | if len(main.Messages) != 4 || main.Messages[3].AuthorKind != "system" || len(main.Cards) != 1 || main.Cards[0].ResourceID != task.ID || len(shared.Messages) != 1 || shared.Messages[0].Body != "selected reply" { |
| 94 | t.Fatal("route did not leave exactly one reply and notice", main, shared) |
| 95 | } |
| 96 | if shared.Messages[0].ConversationID != "" || shared.Messages[0].ID == secret.ID || len(shared.Cards) != 0 { |
| 97 | t.Fatal("route exposed private source") |
| 98 | } |
| 99 | other := f.task(t, owner, "other-task") |
| 100 | if _, e = f.Store.RouteMessage(ctx, owner, secret.ID, other.ID, equipo.Mutation{RequestID: "different"}); !errors.Is(e, equipo.ErrConflict) { |
| 101 | t.Fatal("route changed target", e) |
| 102 | } |
| 103 | their := f.task(t, peer, "their-task") |
| 104 | fresh := send(t, f, owner, "", "fresh", "fresh") |
| 105 | if _, e = f.Store.RouteMessage(ctx, owner, fresh.ID, their.ID, equipo.Mutation{RequestID: "other-person"}); !errors.Is(e, equipo.ErrConflict) { |
| 106 | t.Fatal("routed into another person's task", e) |
| 107 | } |
| 108 | f.exec(t, "UPDATE tasks SET team_id='foreign-team' WHERE id=?", other.ID) |
| 109 | if _, e = f.Store.RouteMessage(ctx, owner, fresh.ID, other.ID, equipo.Mutation{RequestID: "foreign"}); !errors.Is(e, equipo.ErrNotFound) { |
| 110 | t.Fatal("foreign task accepted", e) |
| 111 | } |
| 112 | legacy := f.task(t, owner, "legacy") |
| 113 | for range 2 { |
| 114 | _, e = f.Store.TaskThread(ctx, peer, legacy.ID, "", 50) |
| 115 | must(t, e) |
| 116 | } |
| 117 | if f.count(t, "SELECT count(*) FROM conversation_threads WHERE task_id=?", legacy.ID) != 1 { |
| 118 | t.Fatal("backfill duplicated thread") |
| 119 | } |
| 120 | f.exec(t, "CREATE TRIGGER fail_route_audit BEFORE INSERT ON audit_events WHEN NEW.kind='conversation.route' BEGIN SELECT RAISE(ABORT,'audit failed'); END") |
| 121 | before := f.count(t, "SELECT count(*) FROM conversation_messages") |
| 122 | if _, e = f.Store.RouteMessage(ctx, owner, fresh.ID, legacy.ID, equipo.Mutation{RequestID: "rollback-route"}); e == nil { |
| 123 | t.Fatal("route audit failure ignored") |
| 124 | } |
| 125 | if f.count(t, "SELECT count(*) FROM conversation_messages") != before || f.count(t, "SELECT count(*) FROM conversation_routes") != 1 { |
| 126 | t.Fatal("partial route survived audit failure") |
| 127 | } |
| 128 | } |
| 129 | func TestConversationPageBounds(t *testing.T) { |
| 130 | f := newFixture(t) |
| 131 | body := "<script>alert('x')</script> & café" |
| 132 | first := send(t, f, owner, "", body, "html") |
| 133 | var out bytes.Buffer |
| 134 | must(t, template.Must(template.New("message").Parse("{{.Body}}")).Execute(&out, first)) |
| 135 | if strings.Contains(out.String(), "<script>") || !strings.Contains(out.String(), "<script>") { |
| 136 | t.Fatal("message HTML not escaped") |
| 137 | } |
| 138 | for _, body := range []string{"", strings.Repeat("a", 16385), string([]byte{0xff})} { |
| 139 | if _, e := f.Store.SendMessage(ctx, owner, "", body, equipo.Mutation{RequestID: "invalid"}); !errors.Is(e, equipo.ErrInvalid) { |
| 140 | t.Fatal("invalid body accepted", e) |
| 141 | } |
| 142 | } |
| 143 | maxBody := strings.Repeat("é", 8192) |
| 144 | if m := send(t, f, owner, "", maxBody, "max"); !utf8.ValidString(m.Body) || len(m.Body) != 16384 { |
| 145 | t.Fatal("UTF-8 or boundary changed") |
| 146 | } |
| 147 | for _, n := range []int{-1, 101} { |
| 148 | if _, e := f.Store.Conversation(ctx, owner, "", n); !errors.Is(e, equipo.ErrInvalid) { |
| 149 | t.Fatal("bad page bound accepted", n, e) |
| 150 | } |
| 151 | } |
| 152 | for i := 2; i < 105; i++ { |
| 153 | send(t, f, owner, "", fmt.Sprint(i), fmt.Sprint(i)) |
| 154 | } |
| 155 | for _, n := range []int{0, 1, 50, 100} { |
| 156 | p, e := f.Store.Conversation(ctx, owner, "", n) |
| 157 | must(t, e) |
| 158 | want := n |
| 159 | if want == 0 { |
| 160 | want = 50 |
| 161 | } |
| 162 | if len(p.Messages) != want { |
| 163 | t.Fatal("page size", n, len(p.Messages)) |
| 164 | } |
| 165 | } |
| 166 | p, e := f.Store.Conversation(ctx, owner, "", 1) |
| 167 | must(t, e) |
| 168 | snapshotRevision := p.Revision |
| 169 | var wg sync.WaitGroup |
| 170 | errs := make(chan error, 10) |
| 171 | for i := 0; i < 10; i++ { |
| 172 | wg.Go(func() { |
| 173 | _, e := f.Store.SendMessage(ctx, owner, "", "concurrent", equipo.Mutation{RequestID: fmt.Sprint("concurrent", i)}) |
| 174 | errs <- e |
| 175 | }) |
| 176 | } |
| 177 | seen := map[string]bool{p.Messages[0].ID: true} |
| 178 | seq := int64(1) |
| 179 | for p.NextCursor != "" { |
| 180 | p, e = f.Store.Conversation(ctx, owner, p.NextCursor, 17) |
| 181 | must(t, e) |
| 182 | if p.Revision != snapshotRevision { |
| 183 | t.Fatal("snapshot revision changed") |
| 184 | } |
| 185 | for _, m := range p.Messages { |
| 186 | seq++ |
| 187 | if seen[m.ID] || m.Sequence != seq { |
| 188 | t.Fatal("unstable cursor order") |
| 189 | } |
| 190 | seen[m.ID] = true |
| 191 | } |
| 192 | } |
| 193 | wg.Wait() |
| 194 | close(errs) |
| 195 | for e := range errs { |
| 196 | must(t, e) |
| 197 | } |
| 198 | if len(seen) != 106 { |
| 199 | t.Fatal("inserts changed snapshot", len(seen)) |
| 200 | } |
| 201 | if _, e = f.Store.Conversation(ctx, owner, "guessed", 50); !errors.Is(e, equipo.ErrNotFound) { |
| 202 | t.Fatal("unknown cursor accepted", e) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | func TestConversationReplayScopeAndPreconditions(t *testing.T) { |
| 207 | f := newFixture(t) |
| 208 | p, e := f.Store.Conversation(ctx, owner, "", 50) |
| 209 | must(t, e) |
| 210 | mutation := equipo.Mutation{RequestID: "revision-bound", Revision: p.Revision} |
| 211 | m, e := f.Store.SendMessage(ctx, owner, "", "first", mutation) |
| 212 | must(t, e) |
| 213 | send(t, f, owner, "", "second", "second") |
| 214 | replay, e := f.Store.SendMessage(ctx, owner, "", "first", mutation) |
| 215 | must(t, e) |
| 216 | if !reflect.DeepEqual(replay, m) { |
| 217 | t.Fatal("replay rejected original revision") |
| 218 | } |
| 219 | mutation.Revision++ |
| 220 | if _, e = f.Store.SendMessage(ctx, owner, "", "first", mutation); !errors.Is(e, equipo.ErrConflict) { |
| 221 | t.Fatal("changed canonical revision accepted", e) |
| 222 | } |
| 223 | for _, bad := range []equipo.Mutation{{}, {RequestID: strings.Repeat("a", 257)}, {RequestID: string([]byte{255})}, {RequestID: "negative", Revision: -1}} { |
| 224 | if _, e = f.Store.SendMessage(ctx, owner, "", "bad", bad); !errors.Is(e, equipo.ErrInvalid) { |
| 225 | t.Fatal("bad mutation accepted", e) |
| 226 | } |
| 227 | } |
| 228 | task := f.task(t, owner, "scope-task") |
| 229 | send(t, f, owner, task.ID, "other resource", "revision-bound") |
| 230 | if _, e = f.Store.RouteMessage(ctx, owner, m.ID, task.ID, equipo.Mutation{RequestID: "revision-bound", Revision: 7}); !errors.Is(e, equipo.ErrConflict) { |
| 231 | t.Fatal("stale source routed", e) |
| 232 | } |
| 233 | routed, e := f.Store.RouteMessage(ctx, owner, m.ID, task.ID, equipo.Mutation{RequestID: "revision-bound", Revision: 1}) |
| 234 | must(t, e) |
| 235 | if routed.ID == m.ID { |
| 236 | t.Fatal("operation scope reused send result") |
| 237 | } |
| 238 | f.exec(t, "UPDATE tasks SET requester=? WHERE id=?", peer.Address, task.ID) |
| 239 | if _, e = f.Store.RouteMessage(ctx, owner, m.ID, task.ID, equipo.Mutation{RequestID: "revision-bound", Revision: 1}); !errors.Is(e, equipo.ErrConflict) { |
| 240 | t.Fatal("route replay skipped current requester", e) |
| 241 | } |
| 242 | f.exec(t, "UPDATE members SET role='unknown' WHERE address=?", owner.Address) |
| 243 | if _, e = f.Store.SendMessage(ctx, owner, "", "first", equipo.Mutation{RequestID: "revision-bound", Revision: p.Revision}); !errors.Is(e, equipo.ErrForbidden) { |
| 244 | t.Fatal("replay skipped current role", e) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | func TestConversationCursorScopeAndExpiry(t *testing.T) { |
| 249 | f := newFixture(t) |
| 250 | for i := range 3 { |
| 251 | send(t, f, owner, "", fmt.Sprint(i), fmt.Sprint(i)) |
| 252 | } |
| 253 | page, e := f.Store.Conversation(ctx, owner, "", 1) |
| 254 | must(t, e) |
| 255 | task := f.task(t, owner, "cursor-task") |
| 256 | if _, e = f.Store.TaskThread(ctx, owner, task.ID, page.NextCursor, 1); !errors.Is(e, equipo.ErrNotFound) { |
| 257 | t.Fatal("cursor crossed resource", e) |
| 258 | } |
| 259 | f.exec(t, "UPDATE conversation_cursors SET expires_at='2000-01-01 00:00:00' WHERE id=?", page.NextCursor) |
| 260 | if _, e = f.Store.Conversation(ctx, owner, page.NextCursor, 1); !errors.Is(e, equipo.ErrCursorExpired) { |
| 261 | t.Fatal("expired cursor accepted", e) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | func TestConversationConcurrentReplay(t *testing.T) { |
| 266 | f := newFixture(t) |
| 267 | task := f.task(t, owner, "concurrent-task") |
| 268 | var wg sync.WaitGroup |
| 269 | errs := make(chan error, 16) |
| 270 | for range 8 { |
| 271 | wg.Go(func() { |
| 272 | _, e := f.Store.SendMessage(ctx, owner, "", "once", equipo.Mutation{RequestID: "parallel"}) |
| 273 | errs <- e |
| 274 | }) |
| 275 | } |
| 276 | wg.Wait() |
| 277 | page, e := f.Store.Conversation(ctx, owner, "", 50) |
| 278 | must(t, e) |
| 279 | if len(page.Messages) != 2 { |
| 280 | t.Fatal("concurrent send duplicated") |
| 281 | } |
| 282 | for range 8 { |
| 283 | wg.Go(func() { |
| 284 | _, e := f.Store.RouteMessage(ctx, owner, page.Messages[0].ID, task.ID, equipo.Mutation{RequestID: "parallel-route"}) |
| 285 | errs <- e |
| 286 | }) |
| 287 | } |
| 288 | wg.Wait() |
| 289 | close(errs) |
| 290 | for e := range errs { |
| 291 | must(t, e) |
| 292 | } |
| 293 | if f.count(t, "SELECT count(*) FROM conversation_messages") != 4 || f.count(t, "SELECT count(*) FROM conversation_outboxes") != 4 || f.count(t, "SELECT count(*) FROM conversation_threads") != 1 || f.count(t, "SELECT count(*) FROM conversation_routes") != 1 { |
| 294 | t.Fatal("concurrent retry duplicated effects") |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | func TestConversationRouteRevisionAndCardResource(t *testing.T) { |
| 299 | f := newFixture(t) |
| 300 | task := f.task(t, owner, "card-task") |
| 301 | source := send(t, f, owner, "", "route", "source") |
| 302 | _, e := f.Store.RouteMessage(ctx, owner, source.ID, task.ID, equipo.Mutation{RequestID: "route", Revision: 1}) |
| 303 | must(t, e) |
| 304 | if _, e = f.Store.RouteMessage(ctx, owner, source.ID, task.ID, equipo.Mutation{RequestID: "new-stale", Revision: 2}); !errors.Is(e, equipo.ErrConflict) { |
| 305 | t.Fatal("new stale request reused existing route", e) |
| 306 | } |
| 307 | f.exec(t, "UPDATE tasks SET revision=7 WHERE id=?", task.ID) |
| 308 | page, e := f.Store.Conversation(ctx, owner, "", 50) |
| 309 | must(t, e) |
| 310 | if len(page.Cards) != 1 || page.Cards[0].Revision != 7 { |
| 311 | t.Fatal("card did not project authoritative resource revision", page.Cards) |
| 312 | } |
| 313 | } |
| 314 | |