//go:build browser package thintest import ( "fmt" "testing" "amadan.net/oficina/equipo/internal/wire" ) func TestBrowserAccessRequest(t *testing.T) { for _, outcome := range []string{"approved", "declined", "expired", "limited"} { for _, width := range []int64{1280, 390} { for _, scheme := range []string{"light", "dark"} { t.Run(fmt.Sprintf("%d-%s-%s", width, scheme, outcome), func(t *testing.T) { r := newToolRigSaying(t, wire.ProtocolCurrent, "Fix the build in carlos/platform") got := r.call("access-proposal", wire.ToolRequestAccess, map[string]any{"target": "carlos/platform", "reason": "I need access to fix the build."}) if got.Code != "asked" { t.Fatal(got) } var card wire.Card r.wait("proposal", func(c *Conversation) bool { for _, v := range c.Cards { if v.Access != nil { card = v return true } } return false }) if err := r.runner.Send(r.ctx, "proposal-reply", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Please confirm the access request."}); err != nil { t.Fatal(err) } r.wait("replied", func(c *Conversation) bool { return c.Turns[r.turn].State == wire.TurnReplied }) p := openInner(t, r.inst.URL, r.inst.Cookie(), width, 844, width == 390, scheme, "reduce") defer p.stop() p.open("/conversation") p.wait(`document.querySelector('[data-access-state="confirmation"]')`, "confirmation") p.check(`document.querySelector('[data-access-state]').textContent.includes('Ask Amadan for access to carlos/platform?')`, "target confirmation") p.check(`document.querySelector('[data-access-state]').textContent.includes('Equipo’s own words')`, "reason attribution") p.check(`!document.querySelector('[data-access-approve]')`, "no approval before filing") p.fits("access confirmation") p.shot(fmt.Sprintf("access-confirm-%d-%s", width, scheme)) p.click(`[data-access-aside]`) p.wait(`document.querySelector('[equipo-aside-stub]')`, "aside") p.click(`[equipo-aside-stub] [data-aside-undo]`) p.wait(`document.querySelector('[data-access-confirm]')`, "restore") p.click(`[data-access-confirm]`) _, body, err := r.runner.Expect(r.ctx, wire.FrameRequestAccess) if err != nil { t.Fatal(err) } cmd := body.(*wire.AccessCommand) if !cmd.Confirmed || cmd.CardID != card.ID || cmd.Target != "carlos/platform" || cmd.Role != "agent" { t.Fatalf("command: %+v", cmd) } for _, state := range []string{"pending", outcome} { if err := r.runner.Send(r.ctx, "access-"+state, wire.FrameAccess, wire.AccessReport{CardID: card.ID, Person: cmd.Person, RequestID: "ar_test", State: state, ApproveURL: "https://amadan.net/settings/agents/requests/ar_test"}); err != nil { t.Fatal(err) } p.wait(`document.querySelector('[data-access-state="`+state+`"]')`, state) if state == "pending" { p.check(`(()=>{window.open=()=>null; document.querySelector('[data-access-approve]').click(); return !!document.querySelector('[data-access-waiting]:not([hidden])')})()`, "immediate approval busy state") p.wait(`!document.querySelector('[data-access-approve]')`, "approval saved") p.open("/conversation") p.wait(`document.querySelector('[data-access-waiting]')`, "busy state after reload") p.check(`document.querySelector('[data-access-waiting]').textContent.includes('Waiting for your approval on Amadan…') && document.querySelector('[equipo-access-spinner]') && getComputedStyle(document.querySelector('[equipo-access-spinner]')).borderTopStyle==='solid'`, "waiting copy and spinner") p.check(`document.querySelector('[data-access-reopen]').href==='https://amadan.net/settings/agents/requests/ar_test'`, "reopen link") p.shot(fmt.Sprintf("access-waiting-%d-%s", width, scheme)) } if state != "pending" { p.check(`!document.querySelector('[data-access-waiting]') && !document.querySelector('[data-access-approve]') && !document.querySelector('[data-access-reopen]') && !document.querySelector('[data-access-aside]') && !document.querySelector('[data-set-aside]')`, "final outcome replaces controls") } p.fits("access " + state) p.shot(fmt.Sprintf("access-%s-%d-%s", state, width, scheme)) } if outcome == "approved" { hello := primaryAt(toolHello(r.runner.Welcome.Generation), wire.ProtocolCurrent) hello.Catalog = append(hello.Catalog, wire.RepoInfo{Repo: "amadan.net/carlos/platform", Names: []string{"platform"}, Hints: []string{}}) hello.Repos = append(hello.Repos, "amadan.net/carlos/platform") for i := 0; i < 2; i++ { if err := r.runner.Send(r.ctx, fmt.Sprintf("grant-catalog-%d", i), wire.FrameHello, hello); err != nil { t.Fatal(err) } } p.wait(`document.querySelector('[equipo-card-done]')?.textContent.includes('Access granted to carlos/platform.')`, "compact granted note") p.wait(`document.querySelectorAll('[equipo-thinking]').length===1`, "resumed thinking") p.check(`!document.querySelector('[data-access-state]') && !document.querySelector('[data-access-aside]')`, "no stale card controls") p.open("/conversation") p.wait(`document.querySelectorAll('[equipo-thinking]').length===1`, "resumed thinking after reload") var count int64 r.inst.DB.G.Table("chat_messages").Where("idempotency_key = ?", "access-approved:"+card.ID).Count(&count) if count != 1 { t.Fatal("continuation count", count) } p.shot(fmt.Sprintf("access-resumed-%d-%s", width, scheme)) var resumed string r.inst.DB.G.Table("chat_messages").Select("turn_id").Where("idempotency_key = ?", "access-approved:"+card.ID).Scan(&resumed) if err := r.runner.Send(r.ctx, "resumed-reply", wire.FrameReply, wire.Reply{TurnID: resumed, Attempt: 1, Text: "I can now read carlos/platform."}); err != nil { t.Fatal(err) } p.wait(`document.querySelector('[equipo-message="incoming"] [equipo-message-body]') && document.querySelector('[data-chat-stream]').textContent.includes('I can now read carlos/platform.')`, "resumed reply visible") p.open("/conversation") p.check(`document.querySelector('[data-chat-stream]').textContent.includes('I can now read carlos/platform.')`, "reply survives reload") } }) } } } }