//go:build browser package headless import ( "strconv" "testing" "time" "github.com/chromedp/chromedp" "amadan.net/oficina/equipo/internal/browsertest" "amadan.net/oficina/equipo/internal/thintest" "amadan.net/oficina/equipo/internal/wire" ) // Connectors spec rev 3.2 S4 in a browser: the Usage page at 1280 with a // mouse and at 390 with touch. The sidebar leads to it; advice is the one // card at the top, its action within reach; what is available shows each // reading's age; spend this month says where the prices come from; the // token tabs switch without a script; nothing scrolls sideways. Raise // today's budget acts through the ordinary call: the runner gets the // budget frame at twice the figure, and the card is gone. func TestBrowserUsageAndAdvice(t *testing.T) { for _, width := range []int64{1280, 390} { t.Run(strconv.FormatInt(width, 10), func(t *testing.T) { inst := thintest.StartInstance(t) chats := inst.Store.Chat() midday := time.Now().UTC().Truncate(24 * time.Hour).Add(12 * time.Hour) chats.SetHousekeepingClock(func() time.Time { return midday }) runner := dialFakeAt(t, inst, wire.ProtocolCurrent) read := time.Now().UTC().Add(-2 * time.Hour) reset := midday.Add(70 * time.Hour) runner.hello(t, wire.HeldConnection{LocalID: "codex-1", Person: thintest.Owner, Provider: wire.ProviderCodex, Kind: wire.KindSubscription, Label: thintest.Owner, State: wire.ConnHealthy, Plan: "pro", Headroom: &wire.Headroom{Windows: []wire.Window{{Name: "primary", UsedPercent: 42, Minutes: 300, ResetsAt: &reset}, {Name: "secondary", UsedPercent: 18, Minutes: 10080, ResetsAt: &reset}}, CheckedAt: &read}}, wire.HeldConnection{LocalID: "tensorx-1", Person: thintest.Owner, Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, State: wire.ConnHealthy, Harness: wire.HarnessPi, Upstream: "https://api.tensorx.ai", Billing: wire.BillingMetered, Model: "z-ai/glm-5.3", Budget: &wire.Budget{Day: 1_000_000, Month: 10_000_000, Currency: "USD", Revision: 1}, Spend: &wire.Spend{Day: 840_000, Month: 6_200_000, Currency: "USD"}}, ) client := thintest.NewClient(inst.URL, inst.DeviceToken(t)) deadline := time.Now().Add(10 * time.Second) for { if list, err := client.Connections(t.Context()); err == nil && len(list.Items) == 2 { break } if time.Now().After(deadline) { t.Fatal("the connections did not arrive") } time.Sleep(50 * time.Millisecond) } if err := chats.RefreshAdvice(t.Context(), thintest.Owner); err != nil { t.Fatal(err) } c := inst.Cookie() p := openPage(t, inst.URL, c.Name, c.Value, width, 900, width > 600) t.Cleanup(p.stop) p.run(chromedp.Navigate(inst.URL+"/conversation"), browsertest.Wait(`!!document.querySelector('#nav-usage')`)) p.check(`document.querySelector('#nav-usage').getAttribute('href')==='/usage'`, "the sidebar does not lead to Usage") p.check(`!!document.querySelector('[data-advice-card]') && document.querySelectorAll('[equipo-card="advice"]').length===1`, "no one quiet advice card in the conversation") p.run(chromedp.Navigate(inst.URL+"/usage"), browsertest.Wait(`!!document.querySelector('[data-usage]')`)) p.check(`(()=>{const a=document.querySelector('[data-advice]');const now=document.querySelector('#usage-now');return a&&now&&(a.compareDocumentPosition(now)&4)&&a.querySelector('[data-advice-text]').textContent==='TensorX: $0.84 of today’s $1 budget used.'})()`, "the advice is not the card at the top") p.check(`(()=>{const b=document.querySelector('[data-advice-act]').getBoundingClientRect();return b.width>0&&b.height>=`+map[bool]string{true: "24", false: "44"}[width > 600]+`&&b.top>=0})()`, "the advice's action is out of reach") p.check(`document.querySelector('[data-available] [data-reading-age]').textContent==='as of 2 h ago'`, "no reading age on Codex's row") p.check(`document.querySelector('[data-spend] [data-spend-line]').textContent.includes('$0.84 of $1 today')`, "no spend line") p.check(`document.documentElement.scrollWidth<=document.documentElement.clientWidth`, "the page scrolls sideways") // The tabs are links: by role this week. p.run(chromedp.Navigate(inst.URL+"/usage?period=week&by=role"), browsertest.Wait(`!!document.querySelector('[data-by="role"][aria-current]')`)) p.check(`!!document.querySelector('[data-period="week"][aria-current]') && document.querySelector('[data-usage-table] thead th').textContent==='Role'`, "the tabs did not switch") // Act: the budget call, at twice today's figure. p.run(chromedp.Navigate(inst.URL+"/usage"), browsertest.Wait(`!!document.querySelector('[data-advice-act]')`)) p.check(`(()=>{document.querySelector('[data-advice-act]').click();return true})()`, "act") b := runner.expect(t, wire.FrameBudget).(*wire.BudgetCommand) if b.Budget.Day != 2_000_000 || b.Budget.Month != 10_000_000 || b.Budget.Revision != 2 { t.Fatalf("budget %+v", b.Budget) } p.run(waitLong(`location.pathname==='/usage' && !document.querySelector('[data-advice]')`)) connShotOn(t, p, "2026-11-12", "usage", width) }) } }