//go:build browser
package thintest
import (
"testing"
"amadan.net/oficina/equipo/internal/wire"
)
// With script, a question asked about a Mac-made step while that Mac is
// offline says which Mac and to ask again when it is back, as text, beside
// the retry; nothing was asked.
func TestBrowserAskMacOffline(t *testing.T) {
r := newMacRig(t)
a := r.newMacEngineer("Tito Studio", wire.EngineeringReady)
task, d := r.macDispatch(a, "iOS offline page", sha(1))
r.askableOn(a, d, sha(0xc1), "mac-c1", "sess-1")
r.macOffline(a)
p := openInner(t, r.inst.URL, r.inst.Cookie(), 1280, 900, false, "light", "no-preference")
defer p.stop()
p.open("/tasks/" + task)
p.wait(`!!document.querySelector('[data-chat-stream][data-task] #composer')`, "the task's composer")
p.check(`(()=>{const i=document.querySelector('#chat-body');i.value='What did you change?';i.dispatchEvent(new Event('input'));document.querySelector('#composer').requestSubmit();return true})()`, "ask")
p.wait(`!!document.querySelector('[data-send-failed] [data-retry-send]')`, "the send failed")
p.check(`(()=>{const e=document.querySelector('[data-send-failed] [data-send-error]');return !!e && e.textContent==='Tito Studio is offline. Ask again when it is back.' && e.children.length===0})()`, "the offline Mac named, with when to ask again, as text")
// The line is shown as text, whatever it holds.
hostile := `Mac B
is offline. Ask again when it is back.`
p.check(`(()=>{const m=`+jsString(hostile)+`;window.fetch=()=>Promise.resolve(new Response(JSON.stringify({error:{code:'runner_offline',message:m}}),{status:409,headers:{'Content-Type':'application/json'}}));document.querySelector('[data-send-failed] [data-retry-send]').click();return true})()`, "retry, refused with markup in the line")
p.wait(`(()=>{const e=document.querySelector('[data-send-failed] [data-send-error]');return !!e && e.textContent.startsWith('Mac ')})()`, "the hostile line")
p.check(`(()=>{const es=document.querySelectorAll('[data-send-error]');return es.length===1 && es[0].children.length===0 && es[0].textContent===`+jsString(hostile)+` && !document.querySelector('[data-send-failed] img') && window.__pwned===undefined})()`, "the line as text only, once")
if k := r.task(task); len(k.Questions) != 0 {
t.Fatalf("a question was asked: %+v", k.Questions)
}
}