//go:build browser package headless import ( "strconv" "testing" "amadan.net/oficina/equipo/internal/browsertest" "amadan.net/oficina/equipo/internal/thintest" "amadan.net/oficina/equipo/internal/wire" "github.com/chromedp/chromedp" ) // signInEmailDay is where the sign-in email's screenshots go. const signInEmailDay = "2026-11-24" // Paul, 2026-11-23: a sign-in by a link makes a token connection whose // account Claude will not name, so the code's step asks for the account // email once, optionally: Claude account email under the code field, // empty, with its hint, and Connect after both. An email the server // refuses says so beside the field, the code waiting in its own; one it // takes is posted alone, before the sealed code, and is on the row as // the person's own once the connection is made. func TestBrowserConnectionsSignInEmail(t *testing.T) { for _, width := range []int64{1280, 390} { t.Run(strconv.FormatInt(width, 10), func(t *testing.T) { inst := thintest.StartInstance(t) runner := dialFake(t, inst) c := inst.Cookie() p := openPage(t, inst.URL, c.Name, c.Value, width, 900, width > 600) t.Cleanup(p.stop) shot := func(name string) { t.Helper(); connShotOn(t, p, signInEmailDay, name, width) } p.run(chromedp.Navigate(inst.URL+"/conversation/settings/connections/add"), browsertest.Wait(`!!document.querySelector('[data-connections]') && !!window.EquipoSeal && !!window.nacl`)) p.check(`(()=>{window.__equipoStayed=true;return true})()`, "mark") p.check(recordPosts, "record posts") p.check(`(()=>{document.querySelector('[data-add="claude"]').click();return true})()`, "Claude") cmd := runner.expect(t, wire.FrameConnect).(*wire.ConnectCommand) runner.send(t, wire.FrameConnection, wire.ConnectionReport{Connection: cmd.Connection, Person: thintest.Owner, Provider: "claude", State: wire.ConnConnecting, Prompt: &wire.ConnectPrompt{Kind: wire.PromptAuthorize, URL: signInLinks[0], Attempt: 1}}) p.run(waitLong(`!!document.querySelector('[data-code-steps]')`)) p.check(`(()=>{const form=document.querySelector('[data-code-form]');const code=form.querySelector('[data-code-field]');const mail=form.querySelector('[data-email-field]');const label=form.querySelector('label[for="'+mail.id+'"]');const hint=mail.nextElementSibling;const go=form.querySelector('[data-code-connect]');const all=[...form.querySelectorAll('*')];const follows=(a,b)=>all.indexOf(a)=mail.getBoundingClientRect().bottom && getComputedStyle(label).fontSize!==getComputedStyle(form.querySelector('label[for="'+code.id+'"]')).fontSize && document.documentElement.scrollWidth<=innerWidth+1})()`, "the sign-in step does not ask for the account email, empty and optional, before Connect") p.run(waitLong(`document.getAnimations().every(a=>a.playState!=='running'||a.effect&&a.effect.getTiming().iterations===Infinity)`)) shot("signin-email") // Refused by the server: said beside the field; the code waits. p.check(`(()=>{window.__posts=[];const f=document.querySelector('[data-code-form]');f.querySelector('[data-code-field]').value=`+jsonString(canarySignInCode)+`;f.querySelector('[data-email-field]').value='me@example';f.requestSubmit();return true})()`, "a bad email") p.run(waitLong(`(()=>{const n=document.querySelector('[data-code-form] [data-email-note]');return !n.hidden && n.textContent==='That does not look like an email address.' && n.checkVisibility()})()`)) p.check(`(()=>{const f=document.querySelector('[data-code-form]');return window.__posts.length===1 && window.__posts[0].url.endsWith('/email') && !window.__posts[0].body.includes('CANARY') && f.querySelector('[data-code-field]').value===`+jsonString(canarySignInCode)+` && document.activeElement===f.querySelector('[data-email-field]') && !f.closest('[data-code-steps]').hidden})()`, "a refused email sent the code, or lost it") shot("signin-email-bad") // Taken: the email alone, then the sealed code. p.check(`(()=>{window.__posts=[];const f=document.querySelector('[data-code-form]');f.querySelector('[data-email-field]').value=' me@example.com ';f.requestSubmit();return true})()`, "a good email") p.run(waitLong(`window.__posts.length===2`)) p.check(`(()=>{const [a,b]=window.__posts;return a.url.endsWith('/email') && a.body==='from=add&email=me%40example.com' && !a.body.includes('CANARY') && b.url.endsWith('/secret') && !b.body.includes('example.com') && !b.body.includes('CANARY')})()`, "the email and the code did not go apart, email first") secret := runner.expect(t, wire.FrameSecret).(*wire.SecretCommand) if opened := runner.open(t, secret.Sealed); opened.Secret != canarySignInCode { t.Fatal("the code") } // Connected: the row has the email as the person's own. runner.send(t, wire.FrameConnection, wire.ConnectionReport{Connection: cmd.Connection, Person: thintest.Owner, LocalID: "claude-1", Provider: "claude", State: wire.ConnHealthy}) runner.hello(t, wire.HeldConnection{LocalID: "claude-1", Person: thintest.Owner, Provider: "claude", Kind: wire.KindToken, State: wire.ConnHealthy}) p.run(waitLong(`location.pathname==='/conversation/settings/connections' && [...document.querySelectorAll('[equipo-conn-row][data-provider="claude"]')].some(e=>e.querySelector('[data-stated-email]')?.textContent==='me@example.com' && !e.querySelector('details[data-account-email]').open)`)) p.run(waitLong(`document.getAnimations().every(a=>a.playState!=='running'||a.effect&&a.effect.getTiming().iterations===Infinity)`)) shot("signin-email-row") }) } }