| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "log/slog" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo" |
| 11 | "amadan.net/rastrillo/rastrillo/mail" |
| 12 | ) |
| 13 | |
| 14 | func liveFixture(t *testing.T, f *fixture) { |
| 15 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 16 | router, err := equipo.RouterWithStore(f.DB, equipo.Config{Origin: origin, InstanceKey: "plan3-fixture", AllowLoggedMail: true, Owners: []string{owner.Address}}, mail.Logged(logger), logger, f.Store) |
| 17 | must(t, err) |
| 18 | t.Cleanup(func() { equipo.CloseLive(router) }) |
| 19 | f.Handler = equipo.Mux(router) |
| 20 | } |
| 21 | |
| 22 | type conversationNotifier struct { |
| 23 | deadlines []string |
| 24 | topics []string |
| 25 | check func() |
| 26 | } |
| 27 | |
| 28 | func (n *conversationNotifier) Notify(topics ...string) { |
| 29 | if n.check != nil { |
| 30 | n.check() |
| 31 | } |
| 32 | n.topics = append(n.topics, topics...) |
| 33 | } |
| 34 | func (n *conversationNotifier) NotifyAt(at time.Time, topics ...string) { |
| 35 | for _, topic := range topics { |
| 36 | if strings.HasPrefix(topic, "conversation/") { |
| 37 | n.deadlines = append(n.deadlines, topic) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | func TestConversationQuestionRace(t *testing.T) { |
| 42 | for _, state := range []string{"current", "expired", "cancelled", "superseded"} { |
| 43 | t.Run(state, func(t *testing.T) { |
| 44 | f, x, principal, c := claimTools(t) |
| 45 | q := toolCall(t, x, principal, c, "q", "ask_person", map[string]any{"message_id": c.Turn.MessageID, "question": "Choose", "options": []map[string]string{{"id": "one", "label": "One"}}}) |
| 46 | path := "/conversation/questions/" + q.ResourceID + "/answer" |
| 47 | v := actionForm("stale", 2) |
| 48 | v.Set("option_id", "one") |
| 49 | expectHTTP(t, formPost(t, f, owner, path, v), 409) |
| 50 | v = actionForm("answer", 1) |
| 51 | v.Set("option_id", "one") |
| 52 | expectHTTP(t, formPost(t, f, peer, path, v), 404) |
| 53 | if state == "expired" { |
| 54 | f.exec(t, `UPDATE coordinator_questions SET payload=json_set(payload,'$.ExpiresAt','2000-01-01T00:00:00Z') WHERE id=?`, q.ResourceID) |
| 55 | } |
| 56 | if state == "cancelled" { |
| 57 | f.exec(t, "UPDATE coordinator_turns SET state='cancelled' WHERE id=?", c.Turn.ID) |
| 58 | } |
| 59 | if state == "superseded" { |
| 60 | f.exec(t, "UPDATE coordinator_questions SET state='superseded' WHERE id=?", q.ResourceID) |
| 61 | } |
| 62 | want := 409 |
| 63 | if state == "current" { |
| 64 | want = 303 |
| 65 | } |
| 66 | expectHTTP(t, formPost(t, f, owner, path, v), want) |
| 67 | expectHTTP(t, formPost(t, f, owner, path, v), want) |
| 68 | if state == "current" { |
| 69 | v.Set("request_id", "second") |
| 70 | expectHTTP(t, formPost(t, f, owner, path, v), 409) |
| 71 | if f.count(t, "SELECT count(*) FROM coordinator_turns WHERE request_id=?", "clarification:"+q.ResourceID) != 1 { |
| 72 | t.Fatal("answer reached more than one continuation") |
| 73 | } |
| 74 | } else if f.count(t, "SELECT count(*) FROM coordinator_turns WHERE request_id=?", "clarification:"+q.ResourceID) != 0 { |
| 75 | t.Fatal("inactive question reached provider continuation") |
| 76 | } |
| 77 | }) |
| 78 | } |
| 79 | } |
| 80 | |