oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package browsertest
4
5import (
6 "net/url"
7 "strconv"
8 "strings"
9 "testing"
10 "time"
11
12 "github.com/chromedp/chromedp"
13
14 "amadan.net/rastrillo/rastrillo/harness"
15
16 "amadan.net/oficina/base/internal/equipo/equipotest"
17)
18
19// personSends counts the person's messages Base sent to Equipo.
20func personSends(f *equipotest.Fake) int {
21 n := 0
22 for _, c := range f.Calls("/api/v1/assistant/messages") {
23 if c.Method == "POST" {
24 n++
25 }
26 }
27 return n
28}
29
30// threadJS is the conversation, one line per item: its kind and its words.
31const threadJS = `[...document.querySelectorAll("#ask-panel .thread > li")].map((li) =>
32 (li.matches(".ask-notice") ? "note" : li.matches(".thinking") ? "thinking" : li.matches(".person") ? "person" : li.matches(".connect-offer") ? "offer" : "assistant")
33 + ": " + li.textContent.trim()).join("\n")`
34
35// Back from connecting Oficina in the chat, Base sends nothing; the chat
36// says "Oficina connected." once, after the reply that asked. Equipo goes on
37// by itself: a turn the person did not send shows the Assistant thinking
38// (its spark bubble and dots) and its answer streaming in, whether the
39// turn is already running when Home loads or arrives on the live stream.
40// Turned down, the note says so in the same place, and what comes later
41// goes below it.
42func TestAskOficinaResumesAfterConnect(t *testing.T) {
43 rig, f := askBase(t)
44 th := thinking{t, rig, f}
45 done := rig.Origin + "/ask/oficina/done?" + url.Values{"oficina_state": {strings.Repeat("s", 12)}, "oficina_code": {strings.Repeat("c", 12)}}.Encode()
46 // asked: the person asks, and the Assistant asks them to connect.
47 asked := func(text string, replies int) {
48 t.Helper()
49 f.Reply = func(string) string { return "Connect Oficina so I can add it to your calendar." }
50 drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), text+"\r"))
51 // The reply has landed and its turn ended (Stop gone), so Equipo has it.
52 th.wait(`[...document.querySelectorAll("#ask-panel .thread > li.msg.coordinator:not(.thinking):not(.draft)")].length === ` + strconv.Itoa(replies) + ` &&
53 document.querySelector("#ask-panel form.composer button.stop").hidden`)
54 f.RaiseConnect("", rig.Origin+"/ask/oficina/connect")
55 th.wait(`!!document.querySelector("#ask-panel .thread a.connect-btn")`)
56 }
57 back := func(target string) {
58 t.Helper()
59 drive(t, rig, chromedp.Navigate(target), chromedp.WaitVisible(chromedp.CSS(`#ask-panel textarea`)),
60 chromedp.Poll[chromedp.Void](readJS, chromedp.WithPollingTimeout(5*time.Second)), chromedp.Sleep(300*time.Millisecond))
61 }
62 // answer streams reply into turn id and lands it.
63 answer := func(id, reply string) {
64 t.Helper()
65 f.Stream(id, reply)
66 th.wait(`(document.querySelector(` + strconv.Quote(thinkingSel+", #ask-panel .thread > li[data-draft]") + `)?.textContent || "").includes(` + strconv.Quote(strings.Fields(reply)[0]) + `)`)
67 f.Answer(id, reply, false)
68 th.wait(`!document.querySelector("#ask-panel .thinking, #ask-panel [data-draft]") && document.querySelector("#ask-panel .thread > li:last-child").textContent === ` + strconv.Quote(reply))
69 }
70
71 openPanel(t, rig)
72 asked("Add getting coffee at 9:30 today", 1)
73 sent := personSends(f)
74
75 // Connected, with Equipo's turn already running when Home loads.
76 back(done)
77 if n := personSends(f); n != sent {
78 t.Errorf("connecting sent %d messages", n-sent)
79 }
80 id := f.EmitTurn("", "running")
81 back(rig.Origin + "/")
82 th.wait(`!!document.querySelector(` + strconv.Quote(thinkingSel+" .dots") + `)`)
83 if got := th.eval(threadJS); !strings.HasSuffix(got, "assistant: Connect Oficina so I can add it to your calendar.\nthinking: ") {
84 t.Errorf("the turn running at load, the conversation is:\n%s", got)
85 }
86 answer(id, "Added getting coffee to your calendar today.")
87
88 // Connected again, the turn arriving on the live stream: the note,
89 // then the Assistant thinking below it, then its answer in place.
90 asked("Add lunch at 1", 3)
91 sent = personSends(f)
92 back(done)
93 if got := th.eval(threadJS); !strings.HasSuffix(got, "assistant: Connect Oficina so I can add it to your calendar.\nnote: Oficina connected.") {
94 t.Errorf("after connecting, the conversation is:\n%s", got)
95 }
96 id = f.EmitTurn("", "queued")
97 th.wait(`!!document.querySelector(` + strconv.Quote(thinkingSel+" .dots") + `)`)
98 if got := th.eval(threadJS); !strings.HasSuffix(got, "note: Oficina connected.\nthinking: ") {
99 t.Errorf("the turn arriving live, the conversation is:\n%s", got)
100 }
101 viewportShot(t, rig, "oficina-resume-thinking")
102 f.EmitTurn("team_update", "running") // quiet: no second bubble
103 drive(t, rig, chromedp.Sleep(300*time.Millisecond))
104 if n := harness.Evaluate[int](rig, `document.querySelectorAll("#ask-panel .thinking").length`); n != 1 {
105 t.Errorf("%d thinking bubbles", n)
106 }
107 f.RunTurn(id)
108 answer(id, "Added lunch to your calendar today, 1 to 2 pm.")
109 if got := th.eval(threadJS); !strings.HasSuffix(got, "note: Oficina connected.\nassistant: Added lunch to your calendar today, 1 to 2 pm.") {
110 t.Errorf("the answer is not where the bubble was:\n%s", got)
111 }
112 if n := personSends(f); n != sent {
113 t.Errorf("Base sent %d messages for the person", n-sent)
114 }
115
116 // Turned down: nothing is sent; the note follows the reply that
117 // asked, and the next exchange goes below it.
118 asked("Add dinner at 7", 5)
119 sent = personSends(f)
120 back(rig.Origin + "/ask/oficina/done?oficina=denied")
121 if n := personSends(f); n != sent {
122 t.Errorf("turning it down sent %d messages", n-sent)
123 }
124 // (The card is still open, so its button ends the reply that asked.)
125 if got := th.eval(threadJS); !strings.Contains(got, "person: Add dinner at 7\nassistant: Connect Oficina so I can add it to your calendar.") ||
126 !strings.HasSuffix(got, "Calendar, Docs, Sheets and more\nnote: Oficina wasn't connected.") || strings.Contains(got, "thinking:") {
127 t.Errorf("after turning it down, the conversation is:\n%s", got)
128 }
129 f.Reply = func(string) string { return "Nothing else today." }
130 drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), "What else is on?\r"))
131 th.wait(`document.querySelector("#ask-panel .thread > li:last-child").textContent.startsWith("Nothing else today.")`)
132 if got := th.eval(threadJS); !strings.Contains(got, "note: Oficina wasn't connected.\nperson: What else is on?\nassistant: Nothing else today.") {
133 t.Errorf("later lines do not follow the note:\n%s", got)
134 }
135 viewportShot(t, rig, "oficina-note-in-conversation")
136}
137