| 1 | //go:build browser |
| 2 | |
| 3 | package thintest |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | func TestBrowserAccessRequest(t *testing.T) { |
| 13 | for _, outcome := range []string{"approved", "declined", "expired", "limited"} { |
| 14 | for _, width := range []int64{1280, 390} { |
| 15 | for _, scheme := range []string{"light", "dark"} { |
| 16 | t.Run(fmt.Sprintf("%d-%s-%s", width, scheme, outcome), func(t *testing.T) { |
| 17 | r := newToolRigSaying(t, wire.ProtocolCurrent, "Fix the build in carlos/platform") |
| 18 | got := r.call("access-proposal", wire.ToolRequestAccess, map[string]any{"target": "carlos/platform", "reason": "I need access to fix the build."}) |
| 19 | if got.Code != "asked" { |
| 20 | t.Fatal(got) |
| 21 | } |
| 22 | var card wire.Card |
| 23 | r.wait("proposal", func(c *Conversation) bool { |
| 24 | for _, v := range c.Cards { |
| 25 | if v.Access != nil { |
| 26 | card = v |
| 27 | return true |
| 28 | } |
| 29 | } |
| 30 | return false |
| 31 | }) |
| 32 | 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 { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | r.wait("replied", func(c *Conversation) bool { return c.Turns[r.turn].State == wire.TurnReplied }) |
| 36 | p := openInner(t, r.inst.URL, r.inst.Cookie(), width, 844, width == 390, scheme, "reduce") |
| 37 | defer p.stop() |
| 38 | p.open("/conversation") |
| 39 | p.wait(`document.querySelector('[data-access-state="confirmation"]')`, "confirmation") |
| 40 | p.check(`document.querySelector('[data-access-state]').textContent.includes('Ask Amadan for access to carlos/platform?')`, "target confirmation") |
| 41 | p.check(`document.querySelector('[data-access-state]').textContent.includes('Equipo’s own words')`, "reason attribution") |
| 42 | p.check(`!document.querySelector('[data-access-approve]')`, "no approval before filing") |
| 43 | p.fits("access confirmation") |
| 44 | p.shot(fmt.Sprintf("access-confirm-%d-%s", width, scheme)) |
| 45 | p.click(`[data-access-aside]`) |
| 46 | p.wait(`document.querySelector('[equipo-aside-stub]')`, "aside") |
| 47 | p.click(`[equipo-aside-stub] [data-aside-undo]`) |
| 48 | p.wait(`document.querySelector('[data-access-confirm]')`, "restore") |
| 49 | p.click(`[data-access-confirm]`) |
| 50 | _, body, err := r.runner.Expect(r.ctx, wire.FrameRequestAccess) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | cmd := body.(*wire.AccessCommand) |
| 55 | if !cmd.Confirmed || cmd.CardID != card.ID || cmd.Target != "carlos/platform" || cmd.Role != "agent" { |
| 56 | t.Fatalf("command: %+v", cmd) |
| 57 | } |
| 58 | for _, state := range []string{"pending", outcome} { |
| 59 | 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 { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | p.wait(`document.querySelector('[data-access-state="`+state+`"]')`, state) |
| 63 | if state == "pending" { |
| 64 | p.check(`(()=>{window.open=()=>null; document.querySelector('[data-access-approve]').click(); return !!document.querySelector('[data-access-waiting]:not([hidden])')})()`, "immediate approval busy state") |
| 65 | p.wait(`!document.querySelector('[data-access-approve]')`, "approval saved") |
| 66 | p.open("/conversation") |
| 67 | p.wait(`document.querySelector('[data-access-waiting]')`, "busy state after reload") |
| 68 | 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") |
| 69 | p.check(`document.querySelector('[data-access-reopen]').href==='https://amadan.net/settings/agents/requests/ar_test'`, "reopen link") |
| 70 | p.shot(fmt.Sprintf("access-waiting-%d-%s", width, scheme)) |
| 71 | } |
| 72 | if state != "pending" { |
| 73 | 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") |
| 74 | } |
| 75 | p.fits("access " + state) |
| 76 | p.shot(fmt.Sprintf("access-%s-%d-%s", state, width, scheme)) |
| 77 | } |
| 78 | if outcome == "approved" { |
| 79 | hello := primaryAt(toolHello(r.runner.Welcome.Generation), wire.ProtocolCurrent) |
| 80 | hello.Catalog = append(hello.Catalog, wire.RepoInfo{Repo: "amadan.net/carlos/platform", Names: []string{"platform"}, Hints: []string{}}) |
| 81 | hello.Repos = append(hello.Repos, "amadan.net/carlos/platform") |
| 82 | for i := 0; i < 2; i++ { |
| 83 | if err := r.runner.Send(r.ctx, fmt.Sprintf("grant-catalog-%d", i), wire.FrameHello, hello); err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | } |
| 87 | p.wait(`document.querySelector('[equipo-card-done]')?.textContent.includes('Access granted to carlos/platform.')`, "compact granted note") |
| 88 | p.wait(`document.querySelectorAll('[equipo-thinking]').length===1`, "resumed thinking") |
| 89 | p.check(`!document.querySelector('[data-access-state]') && !document.querySelector('[data-access-aside]')`, "no stale card controls") |
| 90 | p.open("/conversation") |
| 91 | p.wait(`document.querySelectorAll('[equipo-thinking]').length===1`, "resumed thinking after reload") |
| 92 | var count int64 |
| 93 | r.inst.DB.G.Table("chat_messages").Where("idempotency_key = ?", "access-approved:"+card.ID).Count(&count) |
| 94 | if count != 1 { |
| 95 | t.Fatal("continuation count", count) |
| 96 | } |
| 97 | p.shot(fmt.Sprintf("access-resumed-%d-%s", width, scheme)) |
| 98 | var resumed string |
| 99 | r.inst.DB.G.Table("chat_messages").Select("turn_id").Where("idempotency_key = ?", "access-approved:"+card.ID).Scan(&resumed) |
| 100 | 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 { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | 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") |
| 104 | p.open("/conversation") |
| 105 | p.check(`document.querySelector('[data-chat-stream]').textContent.includes('I can now read carlos/platform.')`, "reply survives reload") |
| 106 | } |
| 107 | }) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |