oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thintest
2
3import (
4 "slices"
5 "strings"
6 "testing"
7 "time"
8
9 "amadan.net/oficina/equipo/internal/wire"
10)
11
12// Words a failed turn carries into the next (dispatch's unanswered):
13// only a plain message the coordinator never saw, and in the turn that
14// carries them they are the person's words for team_request too.
15
16// failTurn reports id failed at attempt 1 and waits for it to end.
17func (r *rig) failTurn(runner *FakeRunner, id string) {
18 r.t.Helper()
19 if err := runner.Send(r.ctx, "e-"+id, wire.FrameTurn, wire.TurnReport{TurnID: id, Attempt: 1, State: wire.TurnFailed, Reason: "policy_rejected"}); err != nil {
20 r.t.Fatal(err)
21 }
22 for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); time.Sleep(20 * time.Millisecond) {
23 var state string
24 if err := r.inst.DB.Writer().QueryRow("SELECT state FROM chat_turns WHERE id = ?", id).Scan(&state); err == nil && state == wire.TurnFailed {
25 return
26 }
27 }
28 r.t.Fatalf("turn %s did not fail", id)
29}
30
31// A turn that reached the model (a delta or a thinking or tool activity
32// was recorded for it) is not carried: its words may already be in a
33// resumed session's transcript. One that failed before is.
34func TestContractCarriedOnlyBeforeTheModel(t *testing.T) {
35 r := newRig(t)
36 runner := r.dial(0)
37 turn := func(text, key string) (wire.Sent, *wire.MessageCommand) {
38 t.Helper()
39 sent, err := r.client.Send(r.ctx, text, key)
40 if err != nil {
41 t.Fatal(err)
42 }
43 f, body, err := runner.Expect(r.ctx, wire.FrameMessage)
44 if err != nil {
45 t.Fatal(err)
46 }
47 runner.Send(r.ctx, "a-"+key, wire.FrameAck, wire.Ack{ID: f.ID})
48 r.wait("running", func(c *Conversation) bool { return c.Turns[sent.Turn.ID].State == wire.TurnRunning })
49 return sent, body.(*wire.MessageCommand)
50 }
51 // Frames apply in order: each report lands before the failure after it.
52 streamed, _ := turn("streamed words", "k1")
53 runner.Send(r.ctx, "d1", wire.FrameDelta, wire.DeltaReport{TurnID: streamed.Turn.ID, Attempt: 1, Text: "Looking"})
54 r.failTurn(runner, streamed.Turn.ID)
55 thought, cmd := turn("thought words", "k2")
56 if slices.Contains(cmd.PersonText, "streamed words") {
57 t.Fatalf("a turn that streamed was carried %q", cmd.PersonText)
58 }
59 runner.Send(r.ctx, "t2", wire.FrameActivity, wire.ActivityReport{TurnID: thought.Turn.ID, Attempt: 1, Kind: wire.ActivityThink})
60 r.failTurn(runner, thought.Turn.ID)
61 early, cmd := turn("early words", "k3")
62 if len(cmd.PersonText) != 1 || strings.Contains(cmd.Text, "Not yet answered") {
63 t.Fatalf("a turn that reached the model was carried %q", cmd.PersonText)
64 }
65 r.failTurn(runner, early.Turn.ID)
66 last, cmd := turn("last", "k4")
67 if strings.Join(cmd.PersonText, "|") != "early words|last" {
68 t.Fatalf("a turn that failed before the model was not carried %q", cmd.PersonText)
69 }
70 // The turn that carried them reached the model: they are in its
71 // transcript now, and ride no further.
72 runner.Send(r.ctx, "d4", wire.FrameDelta, wire.DeltaReport{TurnID: last.Turn.ID, Attempt: 1, Text: "On it"})
73 r.failTurn(runner, last.Turn.ID)
74 if _, cmd = turn("final", "k5"); len(cmd.PersonText) != 1 {
75 t.Fatalf("carried past a turn that reached the model %q", cmd.PersonText)
76 }
77}
78
79// A typed answer to a card is not carried as a plain message: bare,
80// "the second one" means nothing without the question it answers.
81func TestContractCarriedSkipsCardAnswers(t *testing.T) {
82 r := newToolRig(t)
83 if res := r.call("q1", wire.ToolAskPerson, map[string]any{"question": "Which one do you mean?"}); res.Code != "asked" {
84 t.Fatalf("ask_person %+v", res)
85 }
86 r.runner.Send(r.ctx, "rep1", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Which day suits?"})
87 var card wire.Card
88 r.wait("question card", func(c *Conversation) bool {
89 for _, k := range c.Cards {
90 if k.Kind == wire.CardQuestion && k.State == wire.CardOpen {
91 card = k
92 return true
93 }
94 }
95 return false
96 })
97 answered, err := r.client.Answer(r.ctx, card.ID, "the second one", card.Revision)
98 if err != nil {
99 t.Fatal(err)
100 }
101 f, _, err := r.runner.Expect(r.ctx, wire.FrameMessage)
102 if err != nil {
103 t.Fatal(err)
104 }
105 r.runner.Send(r.ctx, "a-ans", wire.FrameAck, wire.Ack{ID: f.ID})
106 r.failTurn(r.runner, answered.Turn.ID)
107 if _, err = r.client.Send(r.ctx, "next", "k-next"); err != nil {
108 t.Fatal(err)
109 }
110 _, body, err := r.runner.Expect(r.ctx, wire.FrameMessage)
111 if err != nil {
112 t.Fatal(err)
113 }
114 if cmd := body.(*wire.MessageCommand); len(cmd.PersonText) != 1 || strings.Contains(cmd.Text, "Not yet answered") {
115 t.Fatalf("a card answer was carried bare %q %q", cmd.PersonText, cmd.Text)
116 }
117}
118
119// Words a failed Assistant turn left unanswered ride in the next turn,
120// and there team_request takes them as the person's: the hand-off is
121// their message, as written. Words from no message of theirs still are
122// not theirs.
123func TestContractCarriedWordsHandOver(t *testing.T) {
124 o := newTeamRig(t)
125 lost := o.ask("Can we fix a bug in Tito? Login is broken")
126 o.failTurn(o.runner, lost)
127 o.ask("Please continue")
128 if res := o.use("tr-not", "team_request", map[string]any{"words": "The checkout page is broken"}); !res.IsError || res.Code != "not_their_words" {
129 t.Fatalf("words from no message %+v", res)
130 }
131 request, cmd := o.handoff("tr1", "Login is broken", "")
132 if !strings.HasPrefix(cmd.Text, "Login is broken") {
133 t.Fatalf("thread frame %q", cmd.Text)
134 }
135 if thread := o.thread(request); len(thread) != 1 || thread[0].Text != "Login is broken" || thread[0].Role != wire.RolePerson {
136 t.Fatalf("thread %+v", thread)
137 }
138}
139