//go:build browser package browsertest import ( "net/url" "strconv" "strings" "testing" "time" "github.com/chromedp/chromedp" "amadan.net/rastrillo/rastrillo/harness" "amadan.net/oficina/base/internal/equipo/equipotest" ) // personSends counts the person's messages Base sent to Equipo. func personSends(f *equipotest.Fake) int { n := 0 for _, c := range f.Calls("/api/v1/assistant/messages") { if c.Method == "POST" { n++ } } return n } // threadJS is the conversation, one line per item: its kind and its words. const threadJS = `[...document.querySelectorAll("#ask-panel .thread > li")].map((li) => (li.matches(".ask-notice") ? "note" : li.matches(".thinking") ? "thinking" : li.matches(".person") ? "person" : li.matches(".connect-offer") ? "offer" : "assistant") + ": " + li.textContent.trim()).join("\n")` // Back from connecting Oficina in the chat, Base sends nothing; the chat // says "Oficina connected." once, after the reply that asked. Equipo goes on // by itself: a turn the person did not send shows the Assistant thinking // (its spark bubble and dots) and its answer streaming in, whether the // turn is already running when Home loads or arrives on the live stream. // Turned down, the note says so in the same place, and what comes later // goes below it. func TestAskOficinaResumesAfterConnect(t *testing.T) { rig, f := askBase(t) th := thinking{t, rig, f} done := rig.Origin + "/ask/oficina/done?" + url.Values{"oficina_state": {strings.Repeat("s", 12)}, "oficina_code": {strings.Repeat("c", 12)}}.Encode() // asked: the person asks, and the Assistant asks them to connect. asked := func(text string, replies int) { t.Helper() f.Reply = func(string) string { return "Connect Oficina so I can add it to your calendar." } drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), text+"\r")) // The reply has landed and its turn ended (Stop gone), so Equipo has it. th.wait(`[...document.querySelectorAll("#ask-panel .thread > li.msg.coordinator:not(.thinking):not(.draft)")].length === ` + strconv.Itoa(replies) + ` && document.querySelector("#ask-panel form.composer button.stop").hidden`) f.RaiseConnect("", rig.Origin+"/ask/oficina/connect") th.wait(`!!document.querySelector("#ask-panel .thread a.connect-btn")`) } back := func(target string) { t.Helper() drive(t, rig, chromedp.Navigate(target), chromedp.WaitVisible(chromedp.CSS(`#ask-panel textarea`)), chromedp.Poll[chromedp.Void](readJS, chromedp.WithPollingTimeout(5*time.Second)), chromedp.Sleep(300*time.Millisecond)) } // answer streams reply into turn id and lands it. answer := func(id, reply string) { t.Helper() f.Stream(id, reply) th.wait(`(document.querySelector(` + strconv.Quote(thinkingSel+", #ask-panel .thread > li[data-draft]") + `)?.textContent || "").includes(` + strconv.Quote(strings.Fields(reply)[0]) + `)`) f.Answer(id, reply, false) th.wait(`!document.querySelector("#ask-panel .thinking, #ask-panel [data-draft]") && document.querySelector("#ask-panel .thread > li:last-child").textContent === ` + strconv.Quote(reply)) } openPanel(t, rig) asked("Add getting coffee at 9:30 today", 1) sent := personSends(f) // Connected, with Equipo's turn already running when Home loads. back(done) if n := personSends(f); n != sent { t.Errorf("connecting sent %d messages", n-sent) } id := f.EmitTurn("", "running") back(rig.Origin + "/") th.wait(`!!document.querySelector(` + strconv.Quote(thinkingSel+" .dots") + `)`) if got := th.eval(threadJS); !strings.HasSuffix(got, "assistant: Connect Oficina so I can add it to your calendar.\nthinking: ") { t.Errorf("the turn running at load, the conversation is:\n%s", got) } answer(id, "Added getting coffee to your calendar today.") // Connected again, the turn arriving on the live stream: the note, // then the Assistant thinking below it, then its answer in place. asked("Add lunch at 1", 3) sent = personSends(f) back(done) if got := th.eval(threadJS); !strings.HasSuffix(got, "assistant: Connect Oficina so I can add it to your calendar.\nnote: Oficina connected.") { t.Errorf("after connecting, the conversation is:\n%s", got) } id = f.EmitTurn("", "queued") th.wait(`!!document.querySelector(` + strconv.Quote(thinkingSel+" .dots") + `)`) if got := th.eval(threadJS); !strings.HasSuffix(got, "note: Oficina connected.\nthinking: ") { t.Errorf("the turn arriving live, the conversation is:\n%s", got) } viewportShot(t, rig, "oficina-resume-thinking") f.EmitTurn("team_update", "running") // quiet: no second bubble drive(t, rig, chromedp.Sleep(300*time.Millisecond)) if n := harness.Evaluate[int](rig, `document.querySelectorAll("#ask-panel .thinking").length`); n != 1 { t.Errorf("%d thinking bubbles", n) } f.RunTurn(id) answer(id, "Added lunch to your calendar today, 1 to 2 pm.") if got := th.eval(threadJS); !strings.HasSuffix(got, "note: Oficina connected.\nassistant: Added lunch to your calendar today, 1 to 2 pm.") { t.Errorf("the answer is not where the bubble was:\n%s", got) } if n := personSends(f); n != sent { t.Errorf("Base sent %d messages for the person", n-sent) } // Turned down: nothing is sent; the note follows the reply that // asked, and the next exchange goes below it. asked("Add dinner at 7", 5) sent = personSends(f) back(rig.Origin + "/ask/oficina/done?oficina=denied") if n := personSends(f); n != sent { t.Errorf("turning it down sent %d messages", n-sent) } // (The card is still open, so its button ends the reply that asked.) if got := th.eval(threadJS); !strings.Contains(got, "person: Add dinner at 7\nassistant: Connect Oficina so I can add it to your calendar.") || !strings.HasSuffix(got, "Calendar, Docs, Sheets and more\nnote: Oficina wasn't connected.") || strings.Contains(got, "thinking:") { t.Errorf("after turning it down, the conversation is:\n%s", got) } f.Reply = func(string) string { return "Nothing else today." } drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), "What else is on?\r")) th.wait(`document.querySelector("#ask-panel .thread > li:last-child").textContent.startsWith("Nothing else today.")`) if got := th.eval(threadJS); !strings.Contains(got, "note: Oficina wasn't connected.\nperson: What else is on?\nassistant: Nothing else today.") { t.Errorf("later lines do not follow the note:\n%s", got) } viewportShot(t, rig, "oficina-note-in-conversation") }