| 1 | //go:build browser |
| 2 | |
| 3 | package headless |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/browsertest" |
| 15 | "amadan.net/oficina/equipo/internal/thintest" |
| 16 | "amadan.net/oficina/equipo/internal/wire" |
| 17 | "github.com/chromedp/chromedp" |
| 18 | ) |
| 19 | |
| 20 | // notebookEnv is a coordinator that moved in (repo and own notes) and |
| 21 | // noted something the person said. |
| 22 | func notebookEnv(t *testing.T) (*Env, *thintest.Instance) { |
| 23 | env, inst, r := localEnv(t) |
| 24 | r.opts.NoMoveIn = false |
| 25 | r.start(t) |
| 26 | s := open(t, env) |
| 27 | s.wait(t, "moved in", time.Minute, func(c *thintest.Conversation) bool { return c.Coordinator.State == wire.CoordinatorReady }) |
| 28 | sent := s.send(t, "I like short replies, and plain words please.\ntool:write_note:+person|I like short replies, and plain words please.") |
| 29 | s.waitTurn(t, sent.Turn.ID, wire.TurnReplied) |
| 30 | if n := notebookText(t, s); !strings.Contains(n, "person") || !strings.Contains(n, "repo") { |
| 31 | t.Fatalf("notebook for the page:\n%s", n) |
| 32 | } |
| 33 | return env, inst |
| 34 | } |
| 35 | |
| 36 | // theme sets the page's theme as the theme switcher does. |
| 37 | func (p *page) theme(name string) { |
| 38 | p.t.Helper() |
| 39 | p.check(`(()=>{document.documentElement.dataset.theme=`+jsonString(name)+`;return true})()`, "theme") |
| 40 | } |
| 41 | |
| 42 | // TestNotebookScreenshots writes notebook-<label>-<width>-<theme>.png to |
| 43 | // docs/mockups/built/2026-11-07 when EQUIPO_NOTEBOOK_SHOTS names the label. |
| 44 | func TestNotebookScreenshots(t *testing.T) { |
| 45 | label := os.Getenv("EQUIPO_NOTEBOOK_SHOTS") |
| 46 | if label == "" { |
| 47 | t.Skip("set EQUIPO_NOTEBOOK_SHOTS=before|after to write screenshots") |
| 48 | } |
| 49 | _, inst := notebookEnv(t) |
| 50 | c := inst.Cookie() |
| 51 | dir := filepath.Join("..", "..", "docs", "mockups", "built", "2026-11-07") |
| 52 | if err := os.MkdirAll(dir, 0755); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | for _, width := range []int64{390, 1280} { |
| 56 | p := openPage(t, inst.URL, c.Name, c.Value, width, 900, width > 600) |
| 57 | for _, theme := range []string{"light", "dark"} { |
| 58 | for _, view := range []string{"conversation", "notebook"} { |
| 59 | path := "/conversation" |
| 60 | if view == "notebook" { |
| 61 | path = "/conversation/settings/notebook" |
| 62 | } |
| 63 | p.run(chromedp.Navigate(inst.URL+path), browsertest.Wait(`document.readyState==='complete'`)) |
| 64 | p.theme(theme) |
| 65 | var shot []byte |
| 66 | p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 67 | var err error |
| 68 | shot, err = chromedp.FullScreenshot(90)(ctx, target) |
| 69 | return err |
| 70 | })) |
| 71 | name := "notebook-" + label + "-" + view + "-" + strconv.FormatInt(width, 10) + "-" + theme + ".png" |
| 72 | if err := os.WriteFile(filepath.Join(dir, name), shot, 0644); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | p.stop() |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Paul, 2b: Notebook is a quiet icon button in the conversation header |
| 82 | // (named, with a tooltip, reachable by keyboard, 44 px on touch), and the |
| 83 | // notes look like notes: the text first, a small caption with the source |
| 84 | // and day, a paper card with a hairline border, tint and soft shadow, no |
| 85 | // coloured stripe, the person's words as a quote, in columns on a desktop |
| 86 | // and one stack at 390 px, in both themes; Reset stays quiet below. |
| 87 | func TestBrowserNotebookLook(t *testing.T) { |
| 88 | _, inst := notebookEnv(t) |
| 89 | c := inst.Cookie() |
| 90 | backgrounds := map[string]bool{} |
| 91 | for _, width := range []int64{390, 1280} { |
| 92 | p := openPage(t, inst.URL, c.Name, c.Value, width, 900, width > 600) |
| 93 | for _, theme := range []string{"light", "dark"} { |
| 94 | p.run(chromedp.Navigate(inst.URL+"/conversation"), browsertest.Wait(`!!document.querySelector('[data-chat-stream]')`)) |
| 95 | p.theme(theme) |
| 96 | if width < 600 { |
| 97 | p.check(`getComputedStyle(document.querySelector('[equipo-chat-header] [equipo-header-button]')).display==='none' && !!document.querySelector('[equipo-needs-pill]')`, "phone header keeps the Needs you control") |
| 98 | p.run(chromedp.Navigate(inst.URL+"/conversation/settings"), browsertest.Wait(`!!document.querySelector('a[href="/conversation/settings/notebook"]')`)) |
| 99 | p.check(`(()=>{document.querySelector('a[href="/conversation/settings/notebook"]').click();return true})()`, "Notebook reachable through settings") |
| 100 | p.run(browsertest.Wait(`location.pathname==='/conversation/settings/notebook'`)) |
| 101 | } else { |
| 102 | p.check(`(()=>{const b=document.querySelector('[equipo-chat-header] a[equipo-header-button][href="/conversation/settings/notebook"]');if(!b)return false;const r=b.getBoundingClientRect();return b.getAttribute('aria-label')==='Notebook' && b.title==='Notebook' && !!b.querySelector('svg') && b.textContent.trim()==='' && getComputedStyle(b).cursor==='pointer' && (`+strconv.FormatBool(width < 600)+` ? r.width>=44 && r.height>=44 : r.width>=24)})()`, "header button") |
| 103 | // Keyboard: Tab reaches the button, and its focus shows. |
| 104 | for i := 0; i < 30; i++ { |
| 105 | var on bool |
| 106 | p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 107 | var err error |
| 108 | on, err = chromedp.Evaluate[bool](`document.activeElement && document.activeElement.hasAttribute('equipo-header-button')`)(ctx, target) |
| 109 | return err |
| 110 | })) |
| 111 | if on { |
| 112 | break |
| 113 | } |
| 114 | p.run(chromedp.KeyEvent("\t")) |
| 115 | } |
| 116 | p.check(`document.activeElement.hasAttribute('equipo-header-button') && getComputedStyle(document.activeElement).outlineStyle!=='none'`, "keyboard focus on the Notebook button") |
| 117 | p.run(chromedp.KeyEvent("\r"), browsertest.Wait(`location.pathname==='/conversation/settings/notebook'`)) |
| 118 | } |
| 119 | p.theme(theme) |
| 120 | p.check(`(()=>{const ns=[...document.querySelectorAll('[equipo-notes] > [equipo-note]')];if(ns.length!==5)return false;const page=getComputedStyle(document.body).backgroundColor;return ns.every(n=>{const s=getComputedStyle(n);return s.borderTopWidth==='1px'&&s.borderLeftWidth==='1px'&&s.borderLeftColor===s.borderTopColor&&s.boxShadow!=='none'&&s.backgroundColor!==page&&n.querySelector('[equipo-note-caption] time')})})()`, "notes are not paper cards") |
| 121 | p.check(`(()=>{const q=document.querySelector('[equipo-note][data-said] blockquote p');return !!q && q.textContent.startsWith('I like short replies') && getComputedStyle(q,'::before').content==='"“"' && q.closest('[equipo-note]').querySelector('[equipo-note-caption]').textContent.includes('You said')})()`, "the person's words are not a quote") |
| 122 | p.check(`[...document.querySelectorAll('[equipo-note-caption]')].some(e=>e.textContent.includes('From oficina/equipo/README.md')) && [...document.querySelectorAll('[equipo-note-caption]')].some(e=>e.textContent.includes("Coordinator's note"))`, "captions") |
| 123 | if width < 600 { |
| 124 | p.check(`new Set([...document.querySelectorAll('[equipo-note]')].map(n=>Math.round(n.getBoundingClientRect().x))).size===1 && document.documentElement.scrollWidth<=document.documentElement.clientWidth`, "390 px is not one stack") |
| 125 | } else { |
| 126 | p.check(`new Set([...document.querySelectorAll('[equipo-note]')].map(n=>Math.round(n.getBoundingClientRect().x))).size>1`, "desktop is not columns") |
| 127 | } |
| 128 | p.check(`(()=>{const r=document.querySelector('[data-notebook-reset]');const last=[...document.querySelectorAll('[equipo-note]')].pop();return !!r && !r.hasAttribute('rst-btn') && !!(last.compareDocumentPosition(r)&4)})()`, "Reset is not quiet or not below the notes") |
| 129 | var bg string |
| 130 | p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 131 | var err error |
| 132 | bg, err = chromedp.Evaluate[string](`getComputedStyle(document.querySelector('[equipo-note]')).backgroundColor`)(ctx, target) |
| 133 | return err |
| 134 | })) |
| 135 | backgrounds[theme+bg] = true |
| 136 | } |
| 137 | p.stop() |
| 138 | } |
| 139 | if len(backgrounds) < 2 { |
| 140 | t.Fatalf("notes look the same in both themes: %v", backgrounds) |
| 141 | } |
| 142 | } |
| 143 | |