| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire" |
| 5 | "net/url" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // While one turn is running, answers to different cards and the main |
| 10 | // composer must queue independently. Otherwise their replies mix threads. |
| 11 | func TestRosterRepliesDoNotBatch(t *testing.T) { |
| 12 | r := newToolRig(t) |
| 13 | if res := r.call("one", wire.ToolAskPerson, map[string]any{"question": "First question?", "options": []string{}}); res.Code != "asked" { |
| 14 | t.Fatal(res) |
| 15 | } |
| 16 | // A second stored question exercises the mounted Reply route for |
| 17 | // both drawers while the earlier turn remains busy. |
| 18 | if _, err := r.inst.DB.Writer().Exec("INSERT INTO chat_cards(id,person,kind,state,turn_id,question,options,revision,created_at,updated_at,lane) SELECT 'c_second_reply',person,kind,state,turn_id,'Second question?',options,revision,created_at,updated_at,lane FROM chat_cards WHERE kind='question' LIMIT 1"); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | snap, err := r.client.Snapshot(r.ctx) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | before, err := r.client.Send(r.ctx, "An earlier main message", "before") |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | var turns []string |
| 30 | for _, c := range snap.Cards { |
| 31 | if c.Kind == wire.CardQuestion { |
| 32 | answer, err := r.client.Answer(r.ctx, c.ID, "My answer", c.Revision) |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | turns = append(turns, answer.Turn.ID) |
| 37 | status, _ := r.post(r.inst.Cookie(), "/conversation/cards/"+c.ID+"/reply", url.Values{"request_id": {"followup-" + c.ID}, "body": {"Another detail for " + c.ID}}) |
| 38 | if status != 303 { |
| 39 | t.Fatalf("reply route: %d", status) |
| 40 | } |
| 41 | var turn string |
| 42 | if err := r.inst.DB.Writer().QueryRow("SELECT turn_id FROM chat_card_threads WHERE card_id=?", c.ID).Scan(&turn); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | turns = append(turns, turn) |
| 46 | } |
| 47 | } |
| 48 | main, err := r.client.Send(r.ctx, "An unrelated request", "unrelated") |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | if len(turns) != 4 { |
| 53 | t.Fatalf("want two answers and two replies: %v", turns) |
| 54 | } |
| 55 | seen := map[string]bool{main.Turn.ID: true, before.Turn.ID: true} |
| 56 | for _, id := range turns { |
| 57 | if n := r.rows("SELECT count(*) FROM chat_messages WHERE turn_id = ? AND origin = 'composer'", id); n != 1 { |
| 58 | t.Fatalf("typed card answer or reply lost composer provenance: %s", id) |
| 59 | } |
| 60 | if seen[id] { |
| 61 | t.Fatalf("card replies share a turn: %v, main %s", turns, main.Turn.ID) |
| 62 | } |
| 63 | seen[id] = true |
| 64 | } |
| 65 | } |
| 66 | |