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 "bytes"
5 "encoding/json"
6 "slices"
7 "testing"
8
9 "amadan.net/oficina/equipo/internal/wire"
10)
11
12// glanceEvents are the glance events among events.
13func glanceEvents(events []wire.Event) []wire.Event {
14 var out []wire.Event
15 for _, e := range events {
16 if e.Type == wire.EventGlance {
17 out = append(out, e)
18 }
19 }
20 return out
21}
22
23// assistantTurn sends an Assistant message and returns its turn, the
24// runner's command acked.
25func (r *rig) assistantTurn(runner *FakeRunner, a *AssistantClient, text, key string) *wire.MessageCommand {
26 r.t.Helper()
27 if _, err := a.Send(r.ctx, text, key); err != nil {
28 r.t.Fatal(err)
29 }
30 f, frame, err := runner.Expect(r.ctx, wire.FrameMessage)
31 if err != nil {
32 r.t.Fatal(err)
33 }
34 if err = runner.Send(r.ctx, "ack-"+key, wire.FrameAck, wire.Ack{ID: f.ID}); err != nil {
35 r.t.Fatal(err)
36 }
37 return frame.(*wire.MessageCommand)
38}
39
40// TestContractAssistantGlance: a runner's glance for a live Assistant
41// person turn reaches the Assistant's stream as a glance event, data
42// {turn_id, text}, revision 0, before the turn's first delta; it is no
43// message, in no snapshot, and never on the Coordinator's stream.
44func TestContractAssistantGlance(t *testing.T) {
45 r := newRig(t)
46 runner := r.dialAt(wire.ProtocolGlance)
47 a := r.client.Assistant()
48 snap, err := a.Snapshot(r.ctx)
49 if err != nil {
50 t.Fatal(err)
51 }
52 s, err := a.Open(r.ctx, snap.Cursor)
53 if err != nil {
54 t.Fatal(err)
55 }
56 defer s.Close()
57 cmd := r.assistantTurn(runner, a, "fix my website", "g-1")
58 runner.Send(r.ctx, "g-glance", wire.FrameGlance, wire.GlanceReport{TurnID: cmd.TurnID, Attempt: 1, Text: "Starting on your website"})
59 runner.Send(r.ctx, "g-delta", wire.FrameDelta, wire.DeltaReport{TurnID: cmd.TurnID, Attempt: 1, Text: "On it."})
60 runner.Send(r.ctx, "g-reply", wire.FrameReply, wire.Reply{TurnID: cmd.TurnID, Attempt: 1, Text: "On it."})
61 var seen []wire.Event
62 assistEvents(t, s, "the reply", &seen, func(es []wire.Event) bool {
63 return slices.ContainsFunc(es, func(e wire.Event) bool {
64 return e.Type == wire.EventMessage && e.TurnID == cmd.TurnID && bytes.Contains(e.Data, []byte(`"role":"assistant"`))
65 })
66 })
67 glance := slices.IndexFunc(seen, func(e wire.Event) bool { return e.Type == wire.EventGlance })
68 delta := slices.IndexFunc(seen, func(e wire.Event) bool { return e.Type == wire.EventDelta })
69 if glance < 0 || delta < glance {
70 t.Fatalf("glance %d, delta %d: %+v", glance, delta, seen)
71 }
72 e := seen[glance]
73 var data map[string]any
74 if json.Unmarshal(e.Data, &data) != nil || len(data) != 2 || data["turn_id"] != cmd.TurnID || data["text"] != "Starting on your website" {
75 t.Fatalf("glance data %s", e.Data)
76 }
77 if e.Resource != "turn" || e.ID != cmd.TurnID || e.TurnID != cmd.TurnID || e.Revision != 0 || e.Attempt != 1 || e.MessageID != "" || e.Cursor != 0 {
78 t.Fatalf("glance event %+v", e)
79 }
80 // Live only: no event row holds it, and a stream that follows from
81 // before it (a reconnect) never sees it, though it sees the delta.
82 var rows int
83 if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM chat_events WHERE body LIKE ? OR type = ?", "%Starting on your website%", wire.EventGlance).Scan(&rows); err != nil || rows != 0 {
84 t.Fatalf("%d stored glance events %v", rows, err)
85 }
86 again, err := a.Open(r.ctx, snap.Cursor)
87 if err != nil {
88 t.Fatal(err)
89 }
90 defer again.Close()
91 var replay []wire.Event
92 assistEvents(t, again, "the replayed reply", &replay, func(es []wire.Event) bool {
93 return slices.ContainsFunc(es, func(e wire.Event) bool {
94 return e.Type == wire.EventMessage && e.TurnID == cmd.TurnID && bytes.Contains(e.Data, []byte(`"role":"assistant"`))
95 })
96 })
97 if g := glanceEvents(replay); len(g) != 0 || !slices.ContainsFunc(replay, func(e wire.Event) bool { return e.Type == wire.EventDelta }) {
98 t.Fatalf("replay %+v", replay)
99 }
100 snap, err = a.Snapshot(r.ctx)
101 if err != nil || len(snap.Messages) != 2 {
102 t.Fatalf("snapshot %+v %v", snap, err)
103 }
104 for _, m := range snap.Messages {
105 if m.Text == "Starting on your website" {
106 t.Fatal("a glance was stored as a message")
107 }
108 }
109 var stored int
110 if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM chat_messages WHERE text = ?", "Starting on your website").Scan(&stored); err != nil || stored != 0 {
111 t.Fatalf("%d glance messages %v", stored, err)
112 }
113 // The Coordinator's stream never carries it.
114 coord, err := r.client.Send(r.ctx, "to the coordinator", "c-1")
115 if err != nil {
116 t.Fatal(err)
117 }
118 cs, err := r.client.Open(r.ctx, 0)
119 if err != nil {
120 t.Fatal(err)
121 }
122 defer cs.Close()
123 var coordSeen []wire.Event
124 assistEvents(t, cs, "the coordinator message", &coordSeen, func(es []wire.Event) bool { return eventFor(es, wire.EventMessage, coord.Message.ID, nil) >= 0 })
125 if g := glanceEvents(coordSeen); len(g) != 0 {
126 t.Fatalf("a glance on /api/v1/events: %+v", g)
127 }
128}
129
130// TestContractAssistantGlanceDropped: the instance publishes a glance
131// only for a live Assistant person turn at its attempt, from the
132// person's primary runner speaking 23, with a clean line. Anything else
133// is dropped without closing the stream: a wrong attempt, unclean text,
134// a Coordinator turn, an ended turn, a worker runner and a runner at 22.
135func TestContractAssistantGlanceDropped(t *testing.T) {
136 r := newRig(t)
137 runner := r.dialAt(wire.ProtocolGlance)
138 a := r.client.Assistant()
139 snap, err := a.Snapshot(r.ctx)
140 if err != nil {
141 t.Fatal(err)
142 }
143 s, err := a.Open(r.ctx, snap.Cursor)
144 if err != nil {
145 t.Fatal(err)
146 }
147 defer s.Close()
148 csnap, err := r.client.Snapshot(r.ctx)
149 if err != nil {
150 t.Fatal(err)
151 }
152 cmd := r.assistantTurn(runner, a, "hello", "d-1")
153 bad := []wire.GlanceReport{
154 {TurnID: cmd.TurnID, Attempt: 2, Text: "Wrong attempt"},
155 {TurnID: cmd.TurnID, Attempt: 0, Text: "Wrong attempt"},
156 {TurnID: cmd.TurnID, Attempt: 1, Text: "Asking the team now"},
157 {TurnID: cmd.TurnID, Attempt: 1, Text: "Two\nlines"},
158 {TurnID: cmd.TurnID, Attempt: 1, Text: "**Bold** words"},
159 {TurnID: cmd.TurnID, Attempt: 1, Text: ""},
160 {TurnID: cmd.TurnID, Attempt: 1, Text: "one two three four five six seven eight nine"},
161 {TurnID: "t_nobody", Attempt: 1, Text: "Unknown turn"},
162 }
163 // A Coordinator turn has no glance.
164 coord, err := r.client.Send(r.ctx, "to the coordinator", "c-1")
165 if err != nil {
166 t.Fatal(err)
167 }
168 if _, _, err := runner.Expect(r.ctx, wire.FrameMessage); err != nil {
169 t.Fatal(err)
170 }
171 bad = append(bad, wire.GlanceReport{TurnID: coord.Turn.ID, Attempt: 1, Text: "Looking at that"})
172 for i, g := range bad {
173 if err := runner.Send(r.ctx, "bad-"+string(rune('a'+i)), wire.FrameGlance, g); err != nil {
174 t.Fatal(err)
175 }
176 }
177 // The turn ends; a glance after that is late.
178 runner.Send(r.ctx, "d-reply", wire.FrameReply, wire.Reply{TurnID: cmd.TurnID, Attempt: 1, Text: "Hi."})
179 runner.Send(r.ctx, "d-late", wire.FrameGlance, wire.GlanceReport{TurnID: cmd.TurnID, Attempt: 1, Text: "After the reply"})
180 // Its reply began: a glance after the turn's first delta is late.
181 streamed := r.assistantTurn(runner, a, "and the calendar", "d-3")
182 runner.Send(r.ctx, "s-delta", wire.FrameDelta, wire.DeltaReport{TurnID: streamed.TurnID, Attempt: 1, Text: "Sure"})
183 runner.Send(r.ctx, "s-glance", wire.FrameGlance, wire.GlanceReport{TurnID: streamed.TurnID, Attempt: 1, Text: "Checking your calendar"})
184 runner.Send(r.ctx, "s-reply", wire.FrameReply, wire.Reply{TurnID: streamed.TurnID, Attempt: 1, Text: "Sure."})
185 next := r.assistantTurn(runner, a, "and the invoices", "d-2")
186 // A worker of the same person, speaking 23, is not the primary: its
187 // glance for the live turn is dropped (the lease check after it is
188 // answered only once the glance was applied).
189 hello := WorkerHello(0)
190 hello.Protocol = wire.ProtocolGlance
191 worker, err := DialRunnerHello(r.ctx, r.inst.URL, r.inst.EnrolRunner(t).Token, hello)
192 if err != nil {
193 t.Fatal(err)
194 }
195 defer worker.Close()
196 if err := worker.Send(r.ctx, "w-glance", wire.FrameGlance, wire.GlanceReport{TurnID: next.TurnID, Attempt: 1, Text: "From a worker"}); err != nil {
197 t.Fatal(err)
198 }
199 if err := worker.Send(r.ctx, "w-lease", wire.FrameLease, wire.LeaseCheck{Generation: worker.Welcome.Generation}); err != nil {
200 t.Fatal(err)
201 }
202 if _, _, err := worker.Expect(r.ctx, wire.FrameAck); err != nil {
203 t.Fatalf("the worker's stream after its glance: %v", err)
204 }
205 // A live turn's clean glance still goes: the stream stayed open.
206 runner.Send(r.ctx, "d-good", wire.FrameGlance, wire.GlanceReport{TurnID: next.TurnID, Attempt: 1, Text: "Looking into your invoices"})
207 var seen []wire.Event
208 assistEvents(t, s, "the good glance", &seen, func(es []wire.Event) bool { return len(glanceEvents(es)) > 0 })
209 g := glanceEvents(seen)
210 if len(g) != 1 || g[0].TurnID != next.TurnID || !bytes.Contains(g[0].Data, []byte(`"Looking into your invoices"`)) {
211 t.Fatalf("glances %+v", g)
212 }
213 // Nor did the Coordinator turn's glance reach the Coordinator's stream.
214 cs, err := r.client.Open(r.ctx, csnap.Cursor)
215 if err != nil {
216 t.Fatal(err)
217 }
218 defer cs.Close()
219 mark, err := r.client.Send(r.ctx, "a mark", "c-2")
220 if err != nil {
221 t.Fatal(err)
222 }
223 var coordSeen []wire.Event
224 assistEvents(t, cs, "the mark", &coordSeen, func(es []wire.Event) bool { return eventFor(es, wire.EventMessage, mark.Message.ID, nil) >= 0 })
225 if g := glanceEvents(coordSeen); len(g) != 0 {
226 t.Fatalf("a Coordinator turn's glance: %+v", g)
227 }
228
229 // A runner at protocol 22 sends no glance an instance would publish.
230 old := newRig(t)
231 oldRunner := old.dialAt(wire.ProtocolJobs)
232 oa := old.client.Assistant()
233 osnap, err := oa.Snapshot(old.ctx)
234 if err != nil {
235 t.Fatal(err)
236 }
237 ostream, err := oa.Open(old.ctx, osnap.Cursor)
238 if err != nil {
239 t.Fatal(err)
240 }
241 defer ostream.Close()
242 ocmd := old.assistantTurn(oldRunner, oa, "hello", "o-1")
243 oldRunner.Send(old.ctx, "o-glance", wire.FrameGlance, wire.GlanceReport{TurnID: ocmd.TurnID, Attempt: 1, Text: "At protocol 22"})
244 oldRunner.Send(old.ctx, "o-delta", wire.FrameDelta, wire.DeltaReport{TurnID: ocmd.TurnID, Attempt: 1, Text: "Hi"})
245 var oseen []wire.Event
246 assistEvents(t, ostream, "the delta", &oseen, func(es []wire.Event) bool {
247 return slices.ContainsFunc(es, func(e wire.Event) bool { return e.Type == wire.EventDelta })
248 })
249 if g := glanceEvents(oseen); len(g) != 0 {
250 t.Fatalf("a glance at protocol 22: %+v", g)
251 }
252}
253