oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "context"
5 "encoding/json"
6 "strings"
7 "testing"
8
9 "amadan.net/oficina/equipo/internal/websearch"
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// replyTo is the Assistant's reply in call's turn, as a reader gets it.
14func (r *lookupRig) replyTo(call toolCallRecord) wire.Message {
15 r.t.Helper()
16 m := messageRecord{ID: "reply" + call.TurnID, Person: abPerson, IdempotencyKey: "rk" + call.TurnID, Role: wire.RoleAssistant, Text: "Here you go.", TurnID: call.TurnID, Lane: wire.LaneAssistant, CreatedAt: r.h.now()}
17 if err := r.h.s.db.G.Create(&m).Error; err != nil {
18 r.t.Fatal(err)
19 }
20 views := []wire.Message{messageView(m)}
21 if err := withFiles(r.h.s.db.G, views); err != nil {
22 r.t.Fatal(err)
23 }
24 return views[0]
25}
26
27// A reply shows its turn's two lookups in call order; one from an earlier
28// attempt isn't shown.
29func TestTwoLookupsOneReply(t *testing.T) {
30 r := newLookupRig(t)
31 r.search.Results["dublin weather"] = []websearch.Result{{Title: "Met", URL: "https://www.met.ie/dublin"}}
32 call := r.ask("Weather in Dublin and Cork?", wire.TurnPerson)
33 for i, answer := range []string{"Dublin: rain.", "Cork: sun."} {
34 r.answer = answer
35 call.CallID = "two" + itoa64(int64(i))
36 if _, err := r.lookup(call, `{}`); err != nil {
37 t.Fatal(err)
38 }
39 }
40 got := r.replyTo(call).Lookups
41 if len(got) != 2 || got[0].Answer != "Dublin: rain." || got[1].Answer != "Cork: sun." || got[0].Sources[0].URL != "https://www.met.ie/dublin" {
42 t.Errorf("lookups %+v", got)
43 }
44 call = r.ask("Weather?", wire.TurnPerson)
45 if err := r.h.s.db.G.Create(&lookupRecord{ID: "early", Person: abPerson, TurnID: call.TurnID, Attempt: 0, Seq: 1, CallID: "early", State: lookupShown, Answer: "Old.", Sources: `[{"url":"https://www.met.ie/","title":"Met"}]`, CreatedAt: r.h.now()}).Error; err != nil {
46 t.Fatal(err)
47 }
48 r.answer = "New."
49 if _, err := r.lookup(call, `{}`); err != nil {
50 t.Fatal(err)
51 }
52 if got := r.replyTo(call).Lookups; len(got) != 1 || got[0].Answer != "New." {
53 t.Errorf("an earlier attempt's was shown: %+v", got)
54 }
55}
56
57func TestAnswerLinksStripped(t *testing.T) {
58 for in, want := range map[string]string{
59 "See [the forecast](https://evil.example.org/x) for **rain**.": "See the forecast for **rain**.",
60 "Open <https://evil.example.org/> now": "Open now",
61 "Rain. https://evil.example.org/track?id=1 Sun later.": "Rain. Sun later.",
62 "![pixel](https://evil.example.org/p.png)Rain.": "pixelRain.",
63 } {
64 if got := stripLinks(in); got != want {
65 t.Errorf("%q: %q", in, got)
66 }
67 }
68}
69
70// While a lookup runs, the reply's activity line is the lookup's.
71func TestLookupActivityLabel(t *testing.T) {
72 r := newLookupRig(t)
73 r.search.Results["dublin weather"] = []websearch.Result{{Title: "Met", URL: "https://www.met.ie/dublin"}}
74 call := r.ask("Weather?", wire.TurnPerson)
75 if _, err := r.lookup(call, `{}`); err != nil {
76 t.Fatal(err)
77 }
78 var events []eventRecord
79 if err := r.h.s.db.G.Where("person = ? AND type = ?", abPerson, wire.EventActivity).Find(&events).Error; err != nil {
80 t.Fatal(err)
81 }
82 found := false
83 for _, e := range events {
84 var ev struct {
85 Data wire.Activity `json:"data"`
86 }
87 _ = json.Unmarshal([]byte(e.Body), &ev)
88 a := ev.Data
89 found = found || a.TurnID == call.TurnID && a.Label == lookupActivityKey && e.Lane == wire.LaneAssistant
90 }
91 if !found {
92 t.Errorf("no lookup activity in %d events: %+v", len(events), events)
93 }
94}
95
96// A read without lookup_answers gets each answer folded into the text
97// with its sources; the stored message, and what the Assistant reads of
98// it, are unchanged.
99func TestLookupFallbackForOldClients(t *testing.T) {
100 r := newLookupRig(t)
101 r.search.Results["dublin weather"] = []websearch.Result{{Title: "Met", URL: "https://www.met.ie/dublin"}}
102 call := r.ask("Weather?", wire.TurnPerson)
103 if _, err := r.lookup(call, `{}`); err != nil {
104 t.Fatal(err)
105 }
106 reply := r.replyTo(call)
107 plain := plainLookups(reply, "Sources:")
108 if plain.Lookups != nil || plain.Text != "Here you go.\n\nRain at first, 11 degrees.\n\nSources: https://www.met.ie/dublin" {
109 t.Errorf("plain %q %v", plain.Text, plain.Lookups)
110 }
111 h := &HTTP{Store: r.h.s}
112 raw, _ := json.Marshal(reply)
113 read := h.plainEvents(func(context.Context, string, int64, int) ([]wire.Event, error) {
114 return []wire.Event{{Type: wire.EventMessage, Data: raw}}, nil
115 })
116 events, err := read(context.Background(), abPerson, 0, 10)
117 if err != nil || !strings.Contains(string(events[0].Data), "https://www.met.ie/dublin") || strings.Contains(string(events[0].Data), `"lookups"`) {
118 t.Errorf("event %s %v", events[0].Data, err)
119 }
120 var stored messageRecord
121 if err := r.h.s.db.G.Where("id = ?", reply.ID).First(&stored).Error; err != nil || stored.Text != "Here you go." {
122 t.Errorf("stored %q %v", stored.Text, err)
123 }
124}
125