//go:build e2elive && browser package e2elivetest import ( "context" "fmt" "net/http" "net/url" "testing" "time" "amadan.net/oficina/equipo/internal/browsertest" "amadan.net/rastrillo/rastrillo/db" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/network" "github.com/chromedp/chromedp" ) func liveChatFeel(t *testing.T, c *http.Client, origin string, d *db.DB, startRunner func()) { t.Helper() rig := harness.New(t, func(string) http.Handler { return http.NotFoundHandler() }, harness.WithScrollbars()) u, _ := url.Parse(origin) rig.Run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { cookies := []*network.CookieParam{} for _, cookie := range c.Jar.Cookies(u) { cookies = append(cookies, &network.CookieParam{Name: cookie.Name, Value: cookie.Value, URL: origin}) } _, err := cdp.Call(ctx, target, network.SetCookies, network.SetCookiesParams{Cookies: cookies}) return err }), chromedp.Navigate(origin+"/conversation"), browsertest.Wait(`!!document.querySelector('#composer')`)) check := func(js, why string) { t.Helper() if !harness.Evaluate[bool](rig, js) { t.Fatal(why) } } send := func(body string) { t.Helper() ms := harness.Evaluate[float64](rig, fmt.Sprintf(`(()=>{const f=document.querySelector('#composer');f.querySelector('textarea').value=%q;const at=performance.now();f.requestSubmit();const b=document.querySelector('ol [equipo-thinking]');return b && b.getBoundingClientRect().height>0 ? performance.now()-at : 9999})()`, body)) if ms > 300 { t.Fatalf("thinking bubble took %.1f ms", ms) } t.Logf("thinking bubble visible after %.1f ms", ms) rig.Run(browsertest.Wait(`!document.querySelector('#composer').hasAttribute('aria-busy')`)) check(`!!document.querySelector('ol [equipo-thinking]')`, "thinking disappeared after POST") } send("For our next reply, remember that the first topic is bicycles. Wait for the next message and answer the two topics together.") send("The second topic is rivers. Answer both pending messages in ONE post_message call, starting with **Bicycles and rivers**, followed by about 350 words explaining both, with an italic phrase, an inline code example and a short bullet list. Use post_message only, no other tools.") for _, width := range []int64{1280, 390} { rig.Run(chromedp.EmulateViewport(width, 900)) browsertest.Screenshot(t, rig, fmt.Sprintf("thinking-%d", width)) check(`document.documentElement.scrollWidth<=innerWidth`, "thinking overflow") } startRunner() liveWait(t, "stream before completion", 150*time.Second, func() bool { return harness.Evaluate[bool](rig, `!!document.querySelector('[data-stream-text]')`) }) var running int executionMust(t, d.Writer().QueryRow("SELECT count(*) FROM coordinator_turns WHERE state='running' AND partial_text<>''").Scan(&running)) if running != 1 { t.Fatal("first text arrived after turn completed") } t.Log("first streamed text visible while turn is running") for _, width := range []int64{1280, 390} { rig.Run(chromedp.EmulateViewport(width, 900)) browsertest.Screenshot(t, rig, fmt.Sprintf("streaming-%d", width)) } waitDelivered := func(n int) { t.Helper() liveWait(t, "delivered reply", 150*time.Second, func() bool { var count int executionMust(t, d.Writer().QueryRow("SELECT count(*) FROM coordinator_turns WHERE state='delivered'").Scan(&count)) return count == n }) } waitDelivered(1) rig.Run(browsertest.Wait(`!document.querySelector('ol [equipo-thinking]') && !!document.querySelector('[equipo-markdown] strong')`)) check(`Array.from(document.querySelectorAll('[equipo-markdown] strong')).some(n=>n.textContent==='Bicycles and rivers')`, "markdown heading not rendered") var replies, superseded int executionMust(t, d.Writer().QueryRow("SELECT count(*) FROM conversation_messages WHERE author_kind='coordinator'").Scan(&replies)) executionMust(t, d.Writer().QueryRow("SELECT count(*) FROM coordinator_turns WHERE state='superseded'").Scan(&superseded)) if replies != 1 || superseded != 1 { t.Fatalf("two messages produced %d replies and %d superseded turns", replies, superseded) } send("Please work on amadan.net/oficina/e2e-unknown-repo. First use propose_project with that repository and why: requested for the local test. Then use post_message with body exactly: Set up **amadan.net/oficina/e2e-unknown-repo** below. Do not create a task or run anything.") waitDelivered(2) rig.Run(browsertest.Wait(`!!document.querySelector('form[action^="/conversation/projects/"]') && !document.querySelector('ol [equipo-thinking]')`)) check(`(()=>{const card=document.querySelector('form[action^="/conversation/projects/"]').closest('[equipo-feed-card]');return card.previousElementSibling?.querySelector('[equipo-markdown]')?.textContent.includes('amadan.net/oficina/e2e-unknown-repo')})()`, "project card not immediately after proposing message") check(`document.querySelectorAll('[equipo-owner-label]').length===1 && !Array.from(document.querySelectorAll('[equipo-system-note]')).some(n=>n.textContent.trim()==='owner mode')`, "per-turn owner mode note") for _, width := range []int64{1280, 390} { rig.Run(chromedp.EmulateViewport(width, 900)) browsertest.Screenshot(t, rig, fmt.Sprintf("reply-card-%d", width)) check(`document.documentElement.scrollWidth<=innerWidth`, "reply overflow") } t.Log("two quick messages -> one reply; Markdown rendered; project card adjacent; owner label once") }