| 1 | //go:build browser |
| 2 | |
| 3 | package thintest |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/chromedp/chromedp" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // click activates the element selector finds, as a tap or a click does. |
| 16 | func (p *innerPage) click(selector string) { |
| 17 | p.t.Helper() |
| 18 | p.check(`(()=>{const e=document.querySelector(`+jsString(selector)+`);if(!e)return false;e.click();return true})()`, "click "+selector) |
| 19 | p.do(chromedp.Sleep(50 * time.Millisecond)) |
| 20 | } |
| 21 | |
| 22 | // TestBrowserHousekeeping: the Housekeeping view and the digest line at |
| 23 | // 1280 with a mouse and at 390 with touch. The view lists what Equipo |
| 24 | // combined with its evidence and Undo in reach; today's three budgets are |
| 25 | // quiet meters; the conversation's header leads to the view and the |
| 26 | // digest line links there; Undo from the view restores the task; nothing |
| 27 | // scrolls sideways. Screens are saved when EQUIPO_BROWSER_ARTIFACTS names |
| 28 | // a directory (hk-<screen>-<width>.png). |
| 29 | func TestBrowserHousekeeping(t *testing.T) { |
| 30 | r := newToolRig(t) |
| 31 | for i, title := range []string{"Swipe back from the edge doesn't work", "Timeline jumps when images load"} { |
| 32 | r.pair(fmt.Sprintf("b%d", i), title) |
| 33 | } |
| 34 | // The second pair's record_task combines the first; the sweep the |
| 35 | // second. |
| 36 | r.tidy() |
| 37 | if n := len(r.housekeeping().Actions); n != 2 { |
| 38 | t.Fatalf("combined %d", n) |
| 39 | } |
| 40 | // Tomorrow at 08:30 the digest line is written. |
| 41 | r.clock(time.Now().UTC().Add(24 * time.Hour).Truncate(24 * time.Hour).Add(8*time.Hour + 30*time.Minute)) |
| 42 | r.tick() |
| 43 | r.inst.Store.Chat().SetHousekeepingClock(daytime()) |
| 44 | |
| 45 | for _, size := range []struct { |
| 46 | w, h int64 |
| 47 | touch bool |
| 48 | }{{1280, 900, false}, {390, 844, true}} { |
| 49 | p := openInner(t, r.inst.URL, r.inst.Cookie(), size.w, size.h, size.touch, "light", "no-preference") |
| 50 | |
| 51 | // The conversation: the header's way to the view, the digest line. |
| 52 | p.open("/conversation") |
| 53 | p.wait(`document.querySelector('[data-digest]')`, "digest line") |
| 54 | p.check(`document.querySelector('[data-digest]').textContent.includes('Tidied 2 things.')`, "digest words") |
| 55 | p.check(`document.querySelector('[data-digest-link]').getAttribute('href')==='/housekeeping'`, "digest link") |
| 56 | if !size.touch { |
| 57 | p.check(`(()=>{const a=document.querySelector('[data-housekeeping-link]');const b=a.getBoundingClientRect();return b.width>0&&b.height>0&&b.top>=0})()`, "header link visible") |
| 58 | } |
| 59 | p.check(`document.querySelectorAll('[equipo-card="combined"]').length===2`, "two quiet receipts") |
| 60 | p.fits("conversation") |
| 61 | p.shot(fmt.Sprintf("hk-conversation-%d", size.w)) |
| 62 | |
| 63 | // The view. |
| 64 | p.click(`[data-digest-link]`) |
| 65 | p.wait(`document.querySelector('[data-housekeeping]')`, "the view") |
| 66 | // Settled, as opened directly (the phone slides the page in). |
| 67 | p.open("/housekeeping") |
| 68 | p.check(`document.querySelectorAll('[data-hk-meter]').length===3`, "three meters") |
| 69 | p.check(`document.querySelectorAll('[equipo-hk-action]').length===2`, "two actions") |
| 70 | p.check(`[...document.querySelectorAll('[equipo-hk-action] [equipo-hk-line]')].every(e=>e.textContent.includes('Combined'))`, "action lines") |
| 71 | p.check(`[...document.querySelectorAll('[equipo-hk-action] [equipo-hk-meta]')].every(e=>/\d+% alike, \d+ words in common/.test(e.textContent))`, "evidence") |
| 72 | p.check(`document.querySelectorAll('[data-detector]').length===2 && !document.querySelector('[data-detector][data-off]')`, "detectors on") |
| 73 | p.check(`document.querySelector('[data-hk-settings-link]').getAttribute('href')==='/conversation/settings/housekeeping'`, "the way to Settings") |
| 74 | if size.touch { |
| 75 | p.check(`[...document.querySelectorAll('[data-hk-undo]')].every(b=>{const r=b.getBoundingClientRect();return r.height>=44&&r.width>0})`, "Undo within reach on touch") |
| 76 | } else { |
| 77 | p.check(`[...document.querySelectorAll('[data-hk-undo]')].every(b=>b.getBoundingClientRect().width>0)`, "Undo visible") |
| 78 | } |
| 79 | p.fits("housekeeping") |
| 80 | p.shot(fmt.Sprintf("hk-view-%d", size.w)) |
| 81 | p.stop() |
| 82 | } |
| 83 | |
| 84 | // Live: the view follows the stream. A new combine arrives, Undo |
| 85 | // applies and a detector switches, all without a reload. |
| 86 | p := openInner(t, r.inst.URL, r.inst.Cookie(), 1280, 900, false, "light", "no-preference") |
| 87 | defer p.stop() |
| 88 | p.open("/housekeeping") |
| 89 | r.pair("b2", "Compose sheet keyboard covers the send button") |
| 90 | r.tidy() |
| 91 | p.wait(`document.querySelectorAll('[equipo-hk-action]').length===3`, "the new action, live") |
| 92 | p.stayed("a new action") |
| 93 | id := eval[string](p, `document.querySelector('[equipo-hk-action]').dataset.action`) |
| 94 | p.click(`[data-action="` + id + `"] [data-hk-undo]`) |
| 95 | p.wait(`document.querySelector('[data-action="`+id+`"][data-undone]')`, "the action undone") |
| 96 | p.stayed("Undo") |
| 97 | p.check(`!document.querySelector('[data-action="`+id+`"] [data-hk-undo]')`, "no Undo after Undo") |
| 98 | p.check(`document.querySelector('[data-action="`+id+`"] [data-hk-state]').textContent==='Undone'`, "state words") |
| 99 | a := r.housekeeping() |
| 100 | for _, x := range a.Actions { |
| 101 | if x.ID == id { |
| 102 | if k, _ := r.client.Task(r.ctx, x.Subject.ID); k.Folded != nil { |
| 103 | t.Fatal("Undo from the view did not restore the task") |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | p.click(`[data-detector="duplicate_possible"] [data-hk-detector-off]`) |
| 108 | p.wait(`document.querySelector('[data-detector="duplicate_possible"][data-off]')`, "detector off") |
| 109 | p.stayed("a detector switch") |
| 110 | // A change made elsewhere (another device) carries only a housekeeping |
| 111 | // event: the view follows it too. |
| 112 | h := r.housekeeping() |
| 113 | if _, err := r.client.SetDetector(r.ctx, "duplicate_possible", true, h.Revision); err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | p.wait(`document.querySelector('[data-detector="duplicate_possible"]:not([data-off])')`, "switched on elsewhere, live") |
| 117 | p.stayed("a change from elsewhere") |
| 118 | p.shot("hk-view-undone-1280") |
| 119 | |
| 120 | // Settings' Housekeeping section, at both sizes: Pause, quiet hours |
| 121 | // saved and shown again, and the way to the activity view. |
| 122 | for _, size := range []struct { |
| 123 | w, h int64 |
| 124 | touch bool |
| 125 | }{{1280, 900, false}, {390, 844, true}} { |
| 126 | q := openInner(t, r.inst.URL, r.inst.Cookie(), size.w, size.h, size.touch, "light", "no-preference") |
| 127 | q.open("/conversation/settings/housekeeping") |
| 128 | q.check(`!!document.querySelector('[data-hk-settings] [data-hk-pause]') && document.querySelector('[data-hk-activity]').getAttribute('href')==='/housekeeping'`, "the section") |
| 129 | q.check(`document.querySelector('[data-hk-quiet-on]').checked && document.querySelector('[data-hk-quiet-from]').value==='22' && document.querySelector('[data-hk-quiet-until]').value==='8'`, "default quiet hours") |
| 130 | q.fits("settings") |
| 131 | q.shot(fmt.Sprintf("hk-settings-%d", size.w)) |
| 132 | q.stop() |
| 133 | } |
| 134 | q := openInner(t, r.inst.URL, r.inst.Cookie(), 1280, 900, false, "light", "no-preference") |
| 135 | defer q.stop() |
| 136 | q.open("/conversation/settings/housekeeping") |
| 137 | q.check(`(()=>{window.__hkmark=1;for(const [s,v] of [['[data-hk-quiet-from]','23'],['[data-hk-quiet-until]','7']]){const e=document.querySelector(s);e.value=v;e.dispatchEvent(new Event('change',{bubbles:true}))}return true})()`, "choose hours") |
| 138 | q.click(`[data-savebar-save]`) |
| 139 | // Saved in the background (Paul: a page post replayed the sliding nav). |
| 140 | q.wait(`window.__hkmark===1 && document.querySelector('#housekeeping [equipo-saved-tick][data-shown]') && document.querySelector('[data-hk-quiet-from]').value==='23' && document.querySelector('[data-hk-quiet-until]').value==='7'`, "quiet hours saved") |
| 141 | if h := r.housekeeping(); h.Quiet != (wire.QuietHours{From: 23, Until: 7}) { |
| 142 | t.Fatalf("quiet hours from Settings: %+v", h.Quiet) |
| 143 | } |
| 144 | q.check(`(()=>{window.__hkmark=1;return true})()`, "mark") |
| 145 | q.click(`[data-hk-pause]`) |
| 146 | q.wait(`window.__hkmark===1 && document.querySelector('[data-hk-status][data-paused] [data-hk-resume]')`, "paused") |
| 147 | q.stayed("Pause from Settings") |
| 148 | if h := r.housekeeping(); !h.Paused { |
| 149 | t.Fatal("Pause from Settings did not take") |
| 150 | } |
| 151 | q.shot("hk-settings-paused-1280") |
| 152 | // The view says so and leads back to Settings. |
| 153 | q.open("/housekeeping") |
| 154 | q.check(`!!document.querySelector('[data-housekeeping][data-paused] [data-hk-settings-link]')`, "paused on the view") |
| 155 | } |
| 156 | |