| 1 | //go:build browser |
| 2 | |
| 3 | package headless |
| 4 | |
| 5 | import ( |
| 6 | "strconv" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/browsertest" |
| 10 | "amadan.net/oficina/equipo/internal/thintest" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | "github.com/chromedp/chromedp" |
| 13 | ) |
| 14 | |
| 15 | // addDay is where the Add page's screenshots go. |
| 16 | const addDay = "2026-11-10" |
| 17 | |
| 18 | // Paul, 2026-11-10: "Can 'Add a connection' go to a new screen?" From |
| 19 | // Connections, Add a connection opens its own page in Settings (on a |
| 20 | // phone, Connections in its bar). The chooser is there; Codex's device |
| 21 | // code card arrives in place from the stream, with no reload; once the |
| 22 | // runner reports the connection healthy, the flourish plays and the page |
| 23 | // goes back to Connections, the new row marked for a moment. |
| 24 | func TestBrowserConnectionsAddPage(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 | runner := dialFake(t, inst) |
| 29 | c := inst.Cookie() |
| 30 | p := openPage(t, inst.URL, c.Name, c.Value, width, 900, width > 600) |
| 31 | t.Cleanup(p.stop) |
| 32 | shot := func(name string) { t.Helper(); connShotOn(t, p, addDay, name, width) } |
| 33 | p.run(chromedp.Navigate(inst.URL+"/conversation/settings/connections"), browsertest.Wait(`!!document.querySelector('[data-connections]')`)) |
| 34 | p.check(`(()=>{const a=document.querySelector('[data-add-link]');return !!a && a.checkVisibility() && a.textContent.trim()==='Add a connection' && !document.querySelector('[equipo-conn-chooser]')})()`, "Connections has no Add a connection link, or still holds the chooser") |
| 35 | p.check(`(()=>{document.querySelector('[data-add-link]').click();return true})()`, "Add a connection") |
| 36 | p.run(waitLong(`location.pathname==='/conversation/settings/connections/add' && !!document.querySelector('[equipo-conn-chooser]')`)) |
| 37 | p.check(`(()=>{window.__equipoStayed=true;sessionStorage.removeItem('success');new MutationObserver(()=>{if(document.querySelector('[data-success]'))sessionStorage.setItem('success','1')}).observe(document.body,{childList:true,subtree:true});return true})()`, "mark") |
| 38 | |
| 39 | // The page: its heading, the chooser with Claude and Codex |
| 40 | // live, TensorX, and two greyed, in Settings with Connections current. |
| 41 | p.check(`(()=>{const c=document.querySelector('[equipo-conn-chooser]');return document.querySelector('[equipo-settings-body] h1').textContent==='Add a connection' && c.checkVisibility() && !document.querySelector('[data-add="codex"]').disabled && !document.querySelector('[data-add="claude"]').disabled && c.querySelectorAll('[equipo-conn-opt][disabled]').length===2 && !!c.querySelector('[data-add-metered="tensorx"]') && document.querySelector('[equipo-settings-link][aria-current="page"]').dataset.settingsLink==='connections' && document.documentElement.scrollWidth<=innerWidth+1})()`, "the Add page is not the chooser in Settings") |
| 42 | if width < 600 { |
| 43 | p.check(`(()=>{const back=document.querySelector('[rst-shell-back] a');return back.checkVisibility() && back.getAttribute('href')==='/conversation/settings/connections' && back.textContent.trim()==='Connections' && !document.querySelector('[equipo-settings-nav]').checkVisibility()})()`, "390: no way back to Connections in the bar") |
| 44 | } else { |
| 45 | p.check(`document.querySelector('[equipo-settings-rail]').checkVisibility() && document.querySelector('[equipo-settings-rail] [aria-current="page"]').dataset.railSettings==='connections-add'`, "1280: the rail is not the Settings nav with Add a connection current") |
| 46 | } |
| 47 | p.run(waitLong(`document.getAnimations().every(a=>a.playState!=='running'||a.effect&&a.effect.getTiming().iterations===Infinity)`)) |
| 48 | shot("add-chooser") |
| 49 | |
| 50 | // Codex: the card at once, then the device code from the stream. |
| 51 | p.check(`(()=>{document.querySelector('[data-add="codex"]').click();return !!document.querySelector('[data-connecting][data-provider="codex"]')})()`, "no card the instant Codex is chosen") |
| 52 | cmd := runner.expect(t, wire.FrameConnect).(*wire.ConnectCommand) |
| 53 | if cmd.Provider != "codex" { |
| 54 | t.Fatalf("connect frame %+v", cmd) |
| 55 | } |
| 56 | runner.send(t, wire.FrameConnection, wire.ConnectionReport{Connection: cmd.Connection, Person: thintest.Owner, Provider: "codex", State: wire.ConnConnecting, Prompt: &wire.ConnectPrompt{Kind: wire.PromptDeviceCode, URL: "https://auth.openai.com/codex/device", Code: "FAKE-2468"}}) |
| 57 | p.run(waitLong(`!!document.querySelector('[data-device-code]')`)) |
| 58 | p.check(`(()=>{const card=document.querySelector('[data-connecting][data-provider="codex"]');return card.querySelector('[data-code]').textContent==='FAKE-2468' && card.querySelectorAll('ol > li').length===3 && !!card.querySelector('[data-copy-link]') && !!card.querySelector('[equipo-conn-waiting]') && !document.querySelector('[equipo-conn-chooser]')})()`, "the device code card is not the three steps") |
| 59 | p.check(stayed, "the page reloaded or navigated while the code arrived") |
| 60 | shot("add-codex-code") |
| 61 | |
| 62 | // Approved: the flourish here, then Connections with the row. |
| 63 | runner.send(t, wire.FrameConnection, wire.ConnectionReport{Connection: cmd.Connection, Person: thintest.Owner, LocalID: "codex-1", Provider: "codex", State: wire.ConnHealthy, Label: second, Plan: "pro"}) |
| 64 | runner.hello(t, wire.HeldConnection{LocalID: "codex-1", Person: thintest.Owner, Provider: "codex", Kind: wire.KindSubscription, Label: second, State: wire.ConnHealthy, Plan: "pro"}) |
| 65 | p.run(waitLong(`location.pathname==='/conversation/settings/connections' && !!document.querySelector('[equipo-conn-row][data-connection="` + cmd.Connection + `"][data-new]')`)) |
| 66 | p.check(`(()=>{const row=document.querySelector('[equipo-conn-row][data-connection="`+cmd.Connection+`"]');const nav=performance.getEntriesByType('navigation')[0];return sessionStorage.getItem('success')==='1' && nav.type==='navigate' && location.search==='' && row.checkVisibility() && row.querySelector('[data-connection-state]').textContent==='Ready' && row.textContent.includes('`+second+`') && row.getAnimations().length>0})()`, "no flourish before Connections, or the new row is not marked") |
| 67 | shot("add-connected") |
| 68 | // The mark fades and does not come back with the stream. |
| 69 | p.run(waitLong(`!document.querySelector('[equipo-conn-row][data-new]')`)) |
| 70 | }) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Paul, 2026-11-10: after an expired Claude sign-in, "The sign-in was |
| 75 | // not finished in time." stayed under the list. It is a quiet notice |
| 76 | // with Dismiss, and it goes by itself, live, once a newer attempt starts |
| 77 | // or connects. |
| 78 | func TestBrowserConnectionsEndedNote(t *testing.T) { |
| 79 | const note = `[...document.querySelectorAll('[data-connection-note]')].filter(n=>n.checkVisibility() && n.textContent==='The sign-in was not finished in time.')` |
| 80 | for _, width := range []int64{1280, 390} { |
| 81 | t.Run(strconv.FormatInt(width, 10), func(t *testing.T) { |
| 82 | inst := thintest.StartInstance(t) |
| 83 | runner := dialFake(t, inst) |
| 84 | client := thintest.NewClient(inst.URL, inst.DeviceToken(t)) |
| 85 | c := inst.Cookie() |
| 86 | p := openPage(t, inst.URL, c.Name, c.Value, width, 900, width > 600) |
| 87 | t.Cleanup(p.stop) |
| 88 | ended := func(provider string) { |
| 89 | t.Helper() |
| 90 | a, err := client.Connect(t.Context(), wire.Connect{Provider: provider}) |
| 91 | if err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | runner.expect(t, wire.FrameConnect) |
| 95 | runner.send(t, wire.FrameConnection, wire.ConnectionReport{Connection: a.ID, Person: thintest.Owner, Provider: provider, State: wire.ConnError, Reason: "timed_out"}) |
| 96 | } |
| 97 | ended("claude") |
| 98 | p.run(chromedp.Navigate(inst.URL+"/conversation/settings/connections"), browsertest.Wait(note+`.length===1`)) |
| 99 | p.check(`(()=>{window.__equipoStayed=true;const b=document.querySelector('[data-connection-note] [data-dismiss]');return b.checkVisibility() && b.getAttribute('aria-label')==='Dismiss'})()`, "the notice has no Dismiss") |
| 100 | connShotOn(t, p, addDay, "ended-note", width) |
| 101 | // Dismissed: gone at once, and for good. |
| 102 | p.check(`(()=>{document.querySelector('[data-connection-note] [data-dismiss]').click();return `+note+`.length===0})()`, "Dismiss did not hide the notice") |
| 103 | p.run(waitLong(`!document.querySelector('[data-connection-note]')`)) |
| 104 | p.check(stayed, "the page reloaded or navigated on Dismiss") |
| 105 | |
| 106 | // A newer attempt that connects clears it, live. |
| 107 | ended("claude") |
| 108 | p.run(waitLong(note + `.length===1`)) |
| 109 | a, err := client.Connect(t.Context(), wire.Connect{Provider: "codex"}) |
| 110 | if err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | runner.expect(t, wire.FrameConnect) |
| 114 | runner.send(t, wire.FrameConnection, wire.ConnectionReport{Connection: a.ID, Person: thintest.Owner, LocalID: "codex-1", Provider: "codex", State: wire.ConnHealthy, Label: second, Plan: "pro"}) |
| 115 | p.run(waitLong(`!!document.querySelector('[equipo-conn-row][data-provider="codex"]') && ` + note + `.length===0`)) |
| 116 | p.check(stayed, "the page reloaded or navigated") |
| 117 | |
| 118 | // On the Add page, choosing a provider starts a newer attempt: |
| 119 | // the notice goes at once and stays gone. |
| 120 | ended("claude") |
| 121 | p.run(chromedp.Navigate(inst.URL+"/conversation/settings/connections/add"), browsertest.Wait(note+`.length===1`)) |
| 122 | p.check(`(()=>{window.__equipoStayed=true;document.querySelector('[data-add="claude"]').click();return `+note+`.length===0})()`, "a new attempt left the notice") |
| 123 | runner.expect(t, wire.FrameConnect) |
| 124 | p.run(waitLong(`!!document.querySelector('[data-connecting][data-provider="claude"]') && ` + note + `.length===0`)) |
| 125 | p.check(stayed, "the page reloaded or navigated") |
| 126 | }) |
| 127 | } |
| 128 | } |
| 129 | |