| 1 | //go:build browser |
| 2 | |
| 3 | package headless |
| 4 | |
| 5 | import ( |
| 6 | "strconv" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/chromedp/chromedp" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/browsertest" |
| 13 | "amadan.net/oficina/equipo/internal/thintest" |
| 14 | "amadan.net/oficina/equipo/internal/wire" |
| 15 | ) |
| 16 | |
| 17 | // Connectors spec rev 3.2 S4 in a browser: the Usage page at 1280 with a |
| 18 | // mouse and at 390 with touch. The sidebar leads to it; advice is the one |
| 19 | // card at the top, its action within reach; what is available shows each |
| 20 | // reading's age; spend this month says where the prices come from; the |
| 21 | // token tabs switch without a script; nothing scrolls sideways. Raise |
| 22 | // today's budget acts through the ordinary call: the runner gets the |
| 23 | // budget frame at twice the figure, and the card is gone. |
| 24 | func TestBrowserUsageAndAdvice(t *testing.T) { |
| 25 | for _, width := range []int64{1280, 390} { |
| 26 | t.Run(strconv.FormatInt(width, 10), func(t *testing.T) { |
| 27 | inst := thintest.StartInstance(t) |
| 28 | chats := inst.Store.Chat() |
| 29 | midday := time.Now().UTC().Truncate(24 * time.Hour).Add(12 * time.Hour) |
| 30 | chats.SetHousekeepingClock(func() time.Time { return midday }) |
| 31 | runner := dialFakeAt(t, inst, wire.ProtocolCurrent) |
| 32 | read := time.Now().UTC().Add(-2 * time.Hour) |
| 33 | reset := midday.Add(70 * time.Hour) |
| 34 | runner.hello(t, |
| 35 | 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}}, |
| 36 | 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"}}, |
| 37 | ) |
| 38 | client := thintest.NewClient(inst.URL, inst.DeviceToken(t)) |
| 39 | deadline := time.Now().Add(10 * time.Second) |
| 40 | for { |
| 41 | if list, err := client.Connections(t.Context()); err == nil && len(list.Items) == 2 { |
| 42 | break |
| 43 | } |
| 44 | if time.Now().After(deadline) { |
| 45 | t.Fatal("the connections did not arrive") |
| 46 | } |
| 47 | time.Sleep(50 * time.Millisecond) |
| 48 | } |
| 49 | if err := chats.RefreshAdvice(t.Context(), thintest.Owner); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | c := inst.Cookie() |
| 53 | p := openPage(t, inst.URL, c.Name, c.Value, width, 900, width > 600) |
| 54 | t.Cleanup(p.stop) |
| 55 | |
| 56 | p.run(chromedp.Navigate(inst.URL+"/conversation"), browsertest.Wait(`!!document.querySelector('#nav-usage')`)) |
| 57 | p.check(`document.querySelector('#nav-usage').getAttribute('href')==='/usage'`, "the sidebar does not lead to Usage") |
| 58 | p.check(`!!document.querySelector('[data-advice-card]') && document.querySelectorAll('[equipo-card="advice"]').length===1`, "no one quiet advice card in the conversation") |
| 59 | p.run(chromedp.Navigate(inst.URL+"/usage"), browsertest.Wait(`!!document.querySelector('[data-usage]')`)) |
| 60 | 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") |
| 61 | 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") |
| 62 | p.check(`document.querySelector('[data-available] [data-reading-age]').textContent==='as of 2 h ago'`, "no reading age on Codex's row") |
| 63 | p.check(`document.querySelector('[data-spend] [data-spend-line]').textContent.includes('$0.84 of $1 today')`, "no spend line") |
| 64 | p.check(`document.documentElement.scrollWidth<=document.documentElement.clientWidth`, "the page scrolls sideways") |
| 65 | |
| 66 | // The tabs are links: by role this week. |
| 67 | p.run(chromedp.Navigate(inst.URL+"/usage?period=week&by=role"), browsertest.Wait(`!!document.querySelector('[data-by="role"][aria-current]')`)) |
| 68 | p.check(`!!document.querySelector('[data-period="week"][aria-current]') && document.querySelector('[data-usage-table] thead th').textContent==='Role'`, "the tabs did not switch") |
| 69 | |
| 70 | // Act: the budget call, at twice today's figure. |
| 71 | p.run(chromedp.Navigate(inst.URL+"/usage"), browsertest.Wait(`!!document.querySelector('[data-advice-act]')`)) |
| 72 | p.check(`(()=>{document.querySelector('[data-advice-act]').click();return true})()`, "act") |
| 73 | b := runner.expect(t, wire.FrameBudget).(*wire.BudgetCommand) |
| 74 | if b.Budget.Day != 2_000_000 || b.Budget.Month != 10_000_000 || b.Budget.Revision != 2 { |
| 75 | t.Fatalf("budget %+v", b.Budget) |
| 76 | } |
| 77 | p.run(waitLong(`location.pathname==='/usage' && !document.querySelector('[data-advice]')`)) |
| 78 | connShotOn(t, p, "2026-11-12", "usage", width) |
| 79 | }) |
| 80 | } |
| 81 | } |
| 82 | |