| 1 | package sheets |
| 2 | |
| 3 | // The shared chrome, as Sheets renders it (estilo spec/shared-chrome, |
| 4 | // §5, §6, §7, §8, §9, §10). Each test names the section it holds. |
| 5 | |
| 6 | import ( |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "regexp" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | var pageTitle = regexp.MustCompile(`<title>([^<]*)</title>`) |
| 16 | |
| 17 | // page GETs path as a browser does, with the session cookies. |
| 18 | func page(t *testing.T, app *App, cookies []*http.Cookie, path string) *httptest.ResponseRecorder { |
| 19 | t.Helper() |
| 20 | req := httptest.NewRequest(http.MethodGet, path, nil) |
| 21 | req.Header.Set("Accept", "text/html,application/xhtml+xml;q=0.9,*/*;q=0.8") |
| 22 | return do(t, app, as(req, cookies)) |
| 23 | } |
| 24 | |
| 25 | func titleOf(t *testing.T, body string) string { |
| 26 | t.Helper() |
| 27 | m := pageTitle.FindStringSubmatch(body) |
| 28 | if m == nil { |
| 29 | t.Fatal("no <title>") |
| 30 | } |
| 31 | return m[1] |
| 32 | } |
| 33 | |
| 34 | // homeSignedIn signs the fixture's browser in THROUGH Home: a linked |
| 35 | // account, then an ordinary sign-in by assertion, so the session's |
| 36 | // method is Home's. |
| 37 | func homeSignedIn(t *testing.T, f *homeFixture) Member { |
| 38 | t.Helper() |
| 39 | member := f.local(t, signedInAddress) |
| 40 | f.link(t, "opaque-home-subject") |
| 41 | f.jar = map[string]*http.Cookie{} |
| 42 | signin := f.request(t, "GET", "/signin/oficina?return_to=%2Fsheets", nil, false) |
| 43 | start := f.request(t, "GET", signin.Header().Get("Location"), nil, false) |
| 44 | if finish := f.exchange(t, start, "opaque-home-subject"); finish.Code != 303 { |
| 45 | t.Fatal("Home sign-in", finish.Code) |
| 46 | } |
| 47 | return member |
| 48 | } |
| 49 | |
| 50 | // §10: every <title> is "{part} · {part} · Sheets", built in the layout, |
| 51 | // and a page's {Page} is its h1. None says "Oficina Sheets", "—" or "|". |
| 52 | func TestPageTitlesFollowTheSuitePattern(t *testing.T) { |
| 53 | app, cookies := signIn(t) |
| 54 | s, _ := newSheet(t, app, cookies, "Commission Q2") |
| 55 | id := strconv.FormatUint(s.ID, 10) |
| 56 | for path, want := range map[string]string{ |
| 57 | "/sheets": "Sheets", |
| 58 | "/sheets/" + id: "Commission Q2 · Sheets", |
| 59 | "/sheets/" + id + "/table": "Table view · Commission Q2 · Sheets", |
| 60 | "/sheets/" + id + "/history": "Version history · Commission Q2 · Sheets", |
| 61 | "/sheets/" + id + "/comments": "Comments · Commission Q2 · Sheets", |
| 62 | "/sheets/" + id + "/share": "Share · Commission Q2 · Sheets", |
| 63 | "/sheets/import": "Import a spreadsheet · Sheets", |
| 64 | "/sheets/99999": "Sheet not available · Sheets", |
| 65 | "/no/such/page": "Page not found · Sheets", |
| 66 | } { |
| 67 | if got := titleOf(t, page(t, app, cookies, path).Body.String()); got != want { |
| 68 | t.Errorf("%s: <title> %q, want %q", path, got, want) |
| 69 | } |
| 70 | } |
| 71 | // A sub-page's h1 is its {Page}; the sheet's title is in the bar. |
| 72 | for path, h1 := range map[string]string{ |
| 73 | "/sheets/" + id + "/table": "Table view", |
| 74 | "/sheets/" + id + "/history": "Version history", |
| 75 | "/sheets/" + id + "/comments": "Comments", |
| 76 | "/sheets/" + id + "/share": "Share", |
| 77 | "/sheets": "All sheets", |
| 78 | } { |
| 79 | body := page(t, app, cookies, path).Body.String() |
| 80 | if !strings.Contains(body, "<h1>"+h1+"</h1>") { |
| 81 | t.Errorf("%s: the h1 is not %q", path, h1) |
| 82 | } |
| 83 | if path != "/sheets" && !strings.Contains(body, `<span estilo-bar-context dir="auto" title="Commission Q2">Commission Q2</span>`) { |
| 84 | t.Errorf("%s: the sheet's title is not the bar's context", path) |
| 85 | } |
| 86 | } |
| 87 | // The sign-in door's title is the exception the smart door made: it |
| 88 | // is rastrillo's for the door's state, so the tab and the heading say |
| 89 | // the same thing, and it names Sheets already ("Sign in to Sheets"). |
| 90 | for path, want := range map[string]string{ |
| 91 | "/": "Sheets", |
| 92 | "/signin": "Sign in to Sheets", |
| 93 | "/signin?sent=1": "Check your email to sign in to Sheets", |
| 94 | "/signed-out": "Signed out · Sheets", |
| 95 | "/no/such/page": "Page not found · Sheets", |
| 96 | "/signin?err=bad": "Problem signing in to Sheets", |
| 97 | } { |
| 98 | if got := titleOf(t, page(t, app, nil, path).Body.String()); got != want { |
| 99 | t.Errorf("%s signed out: <title> %q, want %q", path, got, want) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // §5.1 / §8.2: on every signed-in page the account menu is the last |
| 105 | // thing in the bar, and "Sign out" appears exactly once in the page — |
| 106 | // it used to be the account list's fourth flat row. |
| 107 | func TestEverySignedInPageHasOneSignOutInTheSameCorner(t *testing.T) { |
| 108 | app, cookies := signIn(t) |
| 109 | s, _ := newSheet(t, app, cookies, "Budget") |
| 110 | id := strconv.FormatUint(s.ID, 10) |
| 111 | for _, path := range []string{"/sheets", "/sheets/" + id, "/sheets/" + id + "/table", "/sheets/" + id + "/share", "/sheets/" + id + "/history", "/sheets/" + id + "/comments", "/sheets/import", "/no/such/page", "/sheets/99999"} { |
| 112 | body := page(t, app, cookies, path).Body.String() |
| 113 | if n := strings.Count(body, ">Sign out<"); n != 1 { |
| 114 | t.Errorf("%s: %d Sign out, want exactly one", path, n) |
| 115 | } |
| 116 | end := body[strings.Index(body, "<div estilo-bar-end>"):strings.Index(body, "</header>")] |
| 117 | tail := end[strings.LastIndex(end, "</details>"):] |
| 118 | if !strings.Contains(end, "<details estilo-account") || strings.Count(tail, "<") != 2 { // </details> </div> |
| 119 | t.Errorf("%s: the account is not the last thing in the bar", path) |
| 120 | } |
| 121 | if strings.Contains(body, "rst-shell-") { |
| 122 | t.Errorf("%s: the rastrillo topbar is still rendered", path) |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // §5.1 / §8.4: signed out, the corner is "Sign in" carrying the page to |
| 128 | // come back to; on the sign-in pages themselves it is empty; on the |
| 129 | // public link page the brand is plain text and Sign in carries no path |
| 130 | // (the path is a capability). |
| 131 | func TestTheSignedOutCorner(t *testing.T) { |
| 132 | app, cookies := signIn(t) |
| 133 | s, _ := newSheet(t, app, cookies, "Budget") |
| 134 | token, err := app.createLink(nil, s.ID, SheetViewer, signedInMember(t, app)) |
| 135 | if err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | body := page(t, app, nil, "/no/such/page?x=1").Body.String() |
| 139 | if !strings.Contains(body, `<a rst-btn href="/signin?return_to=%2Fno%2Fsuch%2Fpage%3Fx%3D1" estilo-bar-signin>Sign in</a>`) { |
| 140 | t.Error("a signed-out page's corner is not Sign in with its return_to") |
| 141 | } |
| 142 | for _, path := range []string{"/signin", "/signin?sent=1", "/signed-out"} { |
| 143 | body := page(t, app, nil, path).Body.String() |
| 144 | if strings.Contains(body, "estilo-bar-signin") || strings.Contains(body, "estilo-account") { |
| 145 | t.Errorf("%s: the corner is not empty on a sign-in page", path) |
| 146 | } |
| 147 | } |
| 148 | link := page(t, app, nil, "/l/"+token).Body.String() |
| 149 | if !strings.Contains(link, "<span estilo-bar-brand>") || strings.Contains(link, "<a estilo-bar-brand") { |
| 150 | t.Error("the public link page's brand is a link a stranger would be bounced through") |
| 151 | } |
| 152 | if !strings.Contains(link, `<a rst-btn href="/signin" estilo-bar-signin>Sign in</a>`) || strings.Contains(link, "return_to=%2Fl%2F") { |
| 153 | t.Error("the link page's Sign in is missing, or copies the capability path into a URL") |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // §6: the switcher shows only when Home names a hub directory. It holds |
| 158 | // the suite's six apps as tiles at their hosts under the directory's, |
| 159 | // with Sheets marked current, and "All apps" below a divider. |
| 160 | func TestTheSwitcherShowsTheSuite(t *testing.T) { |
| 161 | app, cookies := signIn(t) |
| 162 | if body := page(t, app, cookies, "/sheets").Body.String(); strings.Contains(body, "estilo-switcher") { |
| 163 | t.Error("a standalone instance shows a switcher; there is no directory to switch through") |
| 164 | } |
| 165 | f := newHomeFixtureWith(t, "https://oficina.example.com") |
| 166 | f.local(t, signedInAddress) |
| 167 | body := f.request(t, "GET", "/sheets", nil, false).Body.String() |
| 168 | if !strings.Contains(body, `aria-label="Oficina apps"`) { |
| 169 | t.Error("the switcher is not labelled Oficina apps") |
| 170 | } |
| 171 | start := strings.Index(body, `<details estilo-switcher name="estilo-menus">`) |
| 172 | if start < 0 { |
| 173 | t.Fatal("no switcher", body) |
| 174 | } |
| 175 | panel := body[start:] |
| 176 | panel = panel[:strings.Index(panel, "</details>")] |
| 177 | grid, allApps, ok := strings.Cut(panel, "<hr>") |
| 178 | if !ok || !strings.Contains(allApps, `<a estilo-menu-item href="https://oficina.example.com/"><span estilo-menu-label>All apps</span></a>`) { |
| 179 | t.Error("the switcher does not offer All apps below the tiles", panel) |
| 180 | } |
| 181 | var tiles []string |
| 182 | for _, tile := range strings.Split(grid, "<a estilo-switcher-app ")[1:] { |
| 183 | tiles = append(tiles, tile[:strings.Index(tile, "</a>")]) |
| 184 | } |
| 185 | want := []string{"docs", "sheets", "slides", "calendar", "memoria", "calls"} |
| 186 | names := []string{"Docs", "Sheets", "Slides", "Calendar", "Memoria", "Calls"} |
| 187 | if len(tiles) != len(want) { |
| 188 | t.Fatalf("%d tiles, want %d: %s", len(tiles), len(want), grid) |
| 189 | } |
| 190 | for i, slug := range want { |
| 191 | head := `href="https://` + slug + `.oficina.example.com/"` |
| 192 | if slug == "sheets" { |
| 193 | head += ` aria-current="true"` |
| 194 | } |
| 195 | head += `><img src="/static/estilo/brand/` + slug + `.svg" alt="" width="32" height="32"><span>` + names[i] |
| 196 | if !strings.HasPrefix(tiles[i], head) { |
| 197 | t.Errorf("tile %d = %s, want it to start %s", i, tiles[i], head) |
| 198 | } |
| 199 | } |
| 200 | if strings.Count(grid, "aria-current") != 1 || !strings.Contains(tiles[1], "<span estilo-visually-hidden> (this app)</span>") { |
| 201 | t.Error("Sheets is not the one current tile", grid) |
| 202 | } |
| 203 | // Every tile's icon is served here, publicly: no cookies at all. |
| 204 | if rec := do(t, app, httptest.NewRequest(http.MethodGet, "/static/estilo/brand/calls.svg", nil)); rec.Code != http.StatusOK { |
| 205 | t.Errorf("a tile's icon is %d", rec.Code) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // §5.1 / §5.3: the grid is the one page whose shell fills the viewport; |
| 210 | // every other page's shell is plain, and every page has the bar. |
| 211 | func TestTheGridFillsTheViewport(t *testing.T) { |
| 212 | app, cookies := signIn(t) |
| 213 | s, _ := newSheet(t, app, cookies, "Budget") |
| 214 | grid := page(t, app, cookies, "/sheets/"+strconv.FormatUint(s.ID, 10)).Body.String() |
| 215 | if !strings.Contains(grid, `<div estilo-shell="fill">`) || !strings.Contains(grid, `<header estilo-bar>`) { |
| 216 | t.Error("the grid's shell does not fill the viewport, or its bar is sticky in a page that never scrolls") |
| 217 | } |
| 218 | list := page(t, app, cookies, "/sheets").Body.String() |
| 219 | if !strings.Contains(list, "<div estilo-shell>") || !strings.Contains(list, `<header estilo-bar="sticky">`) { |
| 220 | t.Error("an ordinary page's shell or bar is wrong") |
| 221 | } |
| 222 | } |
| 223 | |