| 1 | //go:build e2elive && browser |
| 2 | |
| 3 | package e2elivetest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "net/http" |
| 9 | "net/url" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/browsertest" |
| 14 | "amadan.net/rastrillo/rastrillo/db" |
| 15 | "amadan.net/rastrillo/rastrillo/harness" |
| 16 | "github.com/chromedp/cdproto/cdp" |
| 17 | "github.com/chromedp/cdproto/network" |
| 18 | "github.com/chromedp/chromedp" |
| 19 | ) |
| 20 | |
| 21 | func liveChatFeel(t *testing.T, c *http.Client, origin string, d *db.DB, startRunner func()) { |
| 22 | t.Helper() |
| 23 | rig := harness.New(t, func(string) http.Handler { return http.NotFoundHandler() }, harness.WithScrollbars()) |
| 24 | u, _ := url.Parse(origin) |
| 25 | rig.Run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 26 | cookies := []*network.CookieParam{} |
| 27 | for _, cookie := range c.Jar.Cookies(u) { |
| 28 | cookies = append(cookies, &network.CookieParam{Name: cookie.Name, Value: cookie.Value, URL: origin}) |
| 29 | } |
| 30 | _, err := cdp.Call(ctx, target, network.SetCookies, network.SetCookiesParams{Cookies: cookies}) |
| 31 | return err |
| 32 | }), chromedp.Navigate(origin+"/conversation"), browsertest.Wait(`!!document.querySelector('#composer')`)) |
| 33 | check := func(js, why string) { |
| 34 | t.Helper() |
| 35 | if !harness.Evaluate[bool](rig, js) { |
| 36 | t.Fatal(why) |
| 37 | } |
| 38 | } |
| 39 | send := func(body string) { |
| 40 | t.Helper() |
| 41 | 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)) |
| 42 | if ms > 300 { |
| 43 | t.Fatalf("thinking bubble took %.1f ms", ms) |
| 44 | } |
| 45 | t.Logf("thinking bubble visible after %.1f ms", ms) |
| 46 | rig.Run(browsertest.Wait(`!document.querySelector('#composer').hasAttribute('aria-busy')`)) |
| 47 | check(`!!document.querySelector('ol [equipo-thinking]')`, "thinking disappeared after POST") |
| 48 | } |
| 49 | send("For our next reply, remember that the first topic is bicycles. Wait for the next message and answer the two topics together.") |
| 50 | 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.") |
| 51 | for _, width := range []int64{1280, 390} { |
| 52 | rig.Run(chromedp.EmulateViewport(width, 900)) |
| 53 | browsertest.Screenshot(t, rig, fmt.Sprintf("thinking-%d", width)) |
| 54 | check(`document.documentElement.scrollWidth<=innerWidth`, "thinking overflow") |
| 55 | } |
| 56 | startRunner() |
| 57 | liveWait(t, "stream before completion", 150*time.Second, func() bool { |
| 58 | return harness.Evaluate[bool](rig, `!!document.querySelector('[data-stream-text]')`) |
| 59 | }) |
| 60 | var running int |
| 61 | executionMust(t, d.Writer().QueryRow("SELECT count(*) FROM coordinator_turns WHERE state='running' AND partial_text<>''").Scan(&running)) |
| 62 | if running != 1 { |
| 63 | t.Fatal("first text arrived after turn completed") |
| 64 | } |
| 65 | t.Log("first streamed text visible while turn is running") |
| 66 | for _, width := range []int64{1280, 390} { |
| 67 | rig.Run(chromedp.EmulateViewport(width, 900)) |
| 68 | browsertest.Screenshot(t, rig, fmt.Sprintf("streaming-%d", width)) |
| 69 | } |
| 70 | waitDelivered := func(n int) { |
| 71 | t.Helper() |
| 72 | liveWait(t, "delivered reply", 150*time.Second, func() bool { |
| 73 | var count int |
| 74 | executionMust(t, d.Writer().QueryRow("SELECT count(*) FROM coordinator_turns WHERE state='delivered'").Scan(&count)) |
| 75 | return count == n |
| 76 | }) |
| 77 | } |
| 78 | waitDelivered(1) |
| 79 | rig.Run(browsertest.Wait(`!document.querySelector('ol [equipo-thinking]') && !!document.querySelector('[equipo-markdown] strong')`)) |
| 80 | check(`Array.from(document.querySelectorAll('[equipo-markdown] strong')).some(n=>n.textContent==='Bicycles and rivers')`, "markdown heading not rendered") |
| 81 | var replies, superseded int |
| 82 | executionMust(t, d.Writer().QueryRow("SELECT count(*) FROM conversation_messages WHERE author_kind='coordinator'").Scan(&replies)) |
| 83 | executionMust(t, d.Writer().QueryRow("SELECT count(*) FROM coordinator_turns WHERE state='superseded'").Scan(&superseded)) |
| 84 | if replies != 1 || superseded != 1 { |
| 85 | t.Fatalf("two messages produced %d replies and %d superseded turns", replies, superseded) |
| 86 | } |
| 87 | 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.") |
| 88 | waitDelivered(2) |
| 89 | rig.Run(browsertest.Wait(`!!document.querySelector('form[action^="/conversation/projects/"]') && !document.querySelector('ol [equipo-thinking]')`)) |
| 90 | 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") |
| 91 | 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") |
| 92 | for _, width := range []int64{1280, 390} { |
| 93 | rig.Run(chromedp.EmulateViewport(width, 900)) |
| 94 | browsertest.Screenshot(t, rig, fmt.Sprintf("reply-card-%d", width)) |
| 95 | check(`document.documentElement.scrollWidth<=innerWidth`, "reply overflow") |
| 96 | } |
| 97 | t.Log("two quick messages -> one reply; Markdown rendered; project card adjacent; owner label once") |
| 98 | } |
| 99 | |