| 1 | package slidestest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/url" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // shelfTile is one cover on the shelf: the tile's link and the picture |
| 12 | // inside it. A deck opens in the editor, as a document does in Docs, so |
| 13 | // the link is /edit and the captured path is the deck's own. |
| 14 | var shelfTile = regexp.MustCompile(`<a slides-tile slides-lift href="(/d/[a-z0-9]{10})/edit">\s*<span slides-cover[^>]*>(?:<img src="([^"]+)")?`) |
| 15 | |
| 16 | // rowLink is one row's title link, to the editor like a cover's. |
| 17 | var rowLink = regexp.MustCompile(`<span rst-row-main><a href="(/d/[a-z0-9]{10})/edit">([^<]*)</a>`) |
| 18 | |
| 19 | // TestListShowsTheNewestFiveAsCoversAndTheRestAsRows: the newest five |
| 20 | // decks are their first slides, newest first; everything older is a row |
| 21 | // with a small cover and its rung in words. |
| 22 | func TestListShowsTheNewestFiveAsCoversAndTheRestAsRows(t *testing.T) { |
| 23 | a := newApp(t) |
| 24 | cookies := signedIn(t, a, allowedAddress) |
| 25 | // The sample deck is first and so oldest; six more after it. |
| 26 | var made []string |
| 27 | for _, title := range []string{"One", "Two", "Three", "Four", "Five", "Six"} { |
| 28 | made = append(made, createDeck(t, a, cookies, title)) |
| 29 | } |
| 30 | |
| 31 | page := mustGet(t, a, "/", http.StatusOK, cookies).Body.String() |
| 32 | tiles := shelfTile.FindAllStringSubmatch(page, -1) |
| 33 | if len(tiles) != 5 { |
| 34 | t.Fatalf("the shelf holds %d covers, want 5:\n%s", len(tiles), page) |
| 35 | } |
| 36 | for i, tile := range tiles { |
| 37 | if want := made[len(made)-1-i]; tile[1] != want { |
| 38 | t.Errorf("cover %d links %s, want %s (newest first)", i, tile[1], want) |
| 39 | } |
| 40 | // The deck's first slide, as the picture thumbs.go serves. |
| 41 | if !strings.HasPrefix(tile[2], tile[1]+"/slides/") || !strings.Contains(tile[2], ".svg?w=") { |
| 42 | t.Errorf("cover %d is %q, want its first slide's SVG", i, tile[2]) |
| 43 | } |
| 44 | pic := mustGet(t, a, strings.SplitN(tile[2], "?", 2)[0], http.StatusOK, cookies) |
| 45 | if ct := pic.Header().Get("Content-Type"); !strings.HasPrefix(ct, "image/svg+xml") { |
| 46 | t.Errorf("cover %d answers %q, want an SVG", i, ct) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | rows := rowLink.FindAllStringSubmatch(page, -1) |
| 51 | if len(rows) != 2 || rows[0][1] != made[0] || rows[1][2] != sampleDeckTitle { |
| 52 | t.Fatalf("rows = %v, want One then the sample deck", rows) |
| 53 | } |
| 54 | // The rung is words in every row, never colour alone. |
| 55 | if n := strings.Count(page, `rst-status rst-tone="neutral">Only me<`); n != 2 { |
| 56 | t.Errorf("%d rows say Only me, want 2", n) |
| 57 | } |
| 58 | // A time is relative, with the date itself in its title. |
| 59 | if !regexp.MustCompile(`<time slides-row-when datetime="[^"]+" title="[^"]+">Just now</time>`).MatchString(page) { |
| 60 | t.Errorf("a row's time is not a <time> with its date in the title:\n%s", page) |
| 61 | } |
| 62 | if !strings.Contains(page, `<h2 slides-rows-label>Earlier</h2>`) { |
| 63 | t.Errorf("the rows have no heading") |
| 64 | } |
| 65 | // Covers are decorative, beside the title that names the deck. |
| 66 | if covers, empty := len(regexp.MustCompile(`<img src="/d/`).FindAllString(page, -1)), len(regexp.MustCompile(`<img src="/d/[^>]* alt=""`).FindAllString(page, -1)); covers != 7 || empty != covers { |
| 67 | t.Errorf("%d covers, %d with alt=\"\": want 7, all empty (the title beside each names the deck)", covers, empty) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // TestAnEmptyDeckGetsABlankCoverInItsTheme: no slide, no picture to |
| 72 | // fetch, and the sheet is the theme's own slide background. |
| 73 | func TestAnEmptyDeckGetsABlankCoverInItsTheme(t *testing.T) { |
| 74 | a := newApp(t) |
| 75 | cookies := signedIn(t, a, allowedAddress) |
| 76 | token := mintAgent(t, a, cookies, "claude") |
| 77 | out := apiJSON(t, a, http.MethodPost, "/api/v1/decks", token, `{"title":"Nothing yet","theme":"forest"}`, http.StatusCreated) |
| 78 | sqid := out["sqid"].(string) |
| 79 | got := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) |
| 80 | slide := got["slides"].([]any)[0].(map[string]any)["id"].(string) |
| 81 | apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, |
| 82 | `{"ops":[{"op":"remove_slide","id":`+quote(slide)+`}]}`, http.StatusOK) |
| 83 | |
| 84 | page := mustGet(t, a, "/", http.StatusOK, cookies).Body.String() |
| 85 | m := regexp.MustCompile(`<a slides-tile slides-lift href="/d/` + sqid + `/edit">\s*<span slides-cover[^>]*>(<img)?`).FindStringSubmatch(page) |
| 86 | if m == nil { |
| 87 | t.Fatalf("the empty deck is not on the shelf:\n%s", page) |
| 88 | } |
| 89 | if m[1] != "" { |
| 90 | t.Errorf("the empty deck's cover has a picture") |
| 91 | } |
| 92 | if !strings.Contains(m[0], `style="background-color: #0f2418"`) { |
| 93 | t.Errorf("the empty deck's cover is %q, want forest's background", m[0]) |
| 94 | } |
| 95 | if !strings.Contains(page, "No slides") { |
| 96 | t.Errorf("the empty deck does not say it has no slides") |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // TestListSearchIsAGetFormOnTheTitle: ?q= narrows the list to titles |
| 101 | // holding the words, as rows only; LIKE's wildcards are the words |
| 102 | // themselves; and a search that finds nothing says so and offers the way |
| 103 | // back. |
| 104 | func TestListSearchIsAGetFormOnTheTitle(t *testing.T) { |
| 105 | a := newApp(t) |
| 106 | cookies := signedIn(t, a, allowedAddress) |
| 107 | createDeck(t, a, cookies, "Hiring plan") |
| 108 | createDeck(t, a, cookies, "Roadmap") |
| 109 | createDeck(t, a, cookies, "Growth 100% plan") |
| 110 | createDeck(t, a, cookies, "Growth 1000 ideas") |
| 111 | |
| 112 | list := mustGet(t, a, "/", http.StatusOK, cookies).Body.String() |
| 113 | if !regexp.MustCompile(`<form slides-search method="get" action="/" role="search" data-busy="false">`).MatchString(list) { |
| 114 | t.Errorf("the search is not a GET form exempt from the busy rule:\n%s", list) |
| 115 | } |
| 116 | |
| 117 | found := mustGet(t, a, "/?q="+url.QueryEscape("PLAN"), http.StatusOK, cookies).Body.String() |
| 118 | titles := rowLink.FindAllStringSubmatch(found, -1) |
| 119 | if len(titles) != 2 || titles[0][2] != "Growth 100% plan" || titles[1][2] != "Hiring plan" { |
| 120 | t.Errorf("?q=PLAN rows = %v, want the two plans, newest first", titles) |
| 121 | } |
| 122 | if strings.Contains(found, "slides-shelf") || strings.Contains(found, "Roadmap") { |
| 123 | t.Errorf("a result page shows the shelf or a deck that does not match") |
| 124 | } |
| 125 | if !strings.Contains(found, `value="PLAN"`) { |
| 126 | t.Errorf("the field does not hold what was searched for") |
| 127 | } |
| 128 | if !strings.Contains(found, "<title>Search: PLAN · Slides</title>") { |
| 129 | t.Errorf("a result page's title does not name the search") |
| 130 | } |
| 131 | |
| 132 | pct := mustGet(t, a, "/?q="+url.QueryEscape("100%"), http.StatusOK, cookies).Body.String() |
| 133 | if got := rowLink.FindAllStringSubmatch(pct, -1); len(got) != 1 || got[0][2] != "Growth 100% plan" { |
| 134 | t.Errorf("?q=100%% rows = %v, want only the deck with a percent sign", got) |
| 135 | } |
| 136 | |
| 137 | none := mustGet(t, a, "/?q=zzz", http.StatusOK, cookies).Body.String() |
| 138 | if !strings.Contains(none, "No decks match") || !strings.Contains(none, `<p slides-search-clear rst-section><a href="/">Clear search</a></p>`) { |
| 139 | t.Errorf("a search that finds nothing does not say so with a way back:\n%s", none) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // TestFileNewDeckMakesTheDeck: File → New deck in the editor is the |
| 144 | // list's button again, a bare POST to /decks, so it lands in the new |
| 145 | // deck's editor with no page in between (Docs' File → New document is |
| 146 | // the same form). |
| 147 | func TestFileNewDeckMakesTheDeck(t *testing.T) { |
| 148 | a := newApp(t) |
| 149 | cookies := signedIn(t, a, allowedAddress) |
| 150 | path := sampleDeck(t, a, cookies) |
| 151 | |
| 152 | page := mustGet(t, a, path+"/edit", http.StatusOK, cookies).Body.String() |
| 153 | file := regexp.MustCompile(`(?s)<details estilo-menubar-menu slides-menu="file".*?</details>`).FindString(page) |
| 154 | if file == "" { |
| 155 | t.Fatal("the editor has no File menu") |
| 156 | } |
| 157 | form := regexp.MustCompile(`(?s)<form method="post" action="/decks">\s*<button estilo-menu-item type="submit">(.*?)</button>\s*</form>`).FindStringSubmatch(file) |
| 158 | if form == nil || !strings.Contains(form[1], "New deck") { |
| 159 | t.Errorf("File → New deck is not a one-button POST to /decks:\n%s", file) |
| 160 | } |
| 161 | if strings.Contains(page, `href="/decks/new"`) { |
| 162 | t.Error("the editor still links to the in-between page at /decks/new") |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // TestNewDeckNeedsNoTitleAndOpensTheEditor: the list's button is a form |
| 167 | // with nothing in it but itself; it makes "Untitled deck" and lands in |
| 168 | // the editor. GET /decks/new is the same button on its own page. |
| 169 | func TestNewDeckNeedsNoTitleAndOpensTheEditor(t *testing.T) { |
| 170 | a := newApp(t) |
| 171 | cookies := signedIn(t, a, allowedAddress) |
| 172 | |
| 173 | list := mustGet(t, a, "/", http.StatusOK, cookies).Body.String() |
| 174 | form := regexp.MustCompile(`(?s)<form slides-create method="post" action="/decks">(.*?)</form>`).FindStringSubmatch(list) |
| 175 | if form == nil || strings.Contains(form[1], "<input") { |
| 176 | t.Fatalf("the list's New deck is not a bare POST form:\n%s", list) |
| 177 | } |
| 178 | |
| 179 | rec := post(t, a, "/decks", url.Values{}, cookies...) |
| 180 | loc := rec.Header().Get("Location") |
| 181 | path := deckPath.FindString(loc) |
| 182 | if rec.Code != http.StatusSeeOther || path == "" || loc != path+"/edit" { |
| 183 | t.Fatalf("POST /decks with nothing = %d to %q, want 303 to the editor", rec.Code, loc) |
| 184 | } |
| 185 | mustGet(t, a, loc, http.StatusOK, cookies) |
| 186 | if page := mustGet(t, a, path+"?slide=1", http.StatusOK, cookies).Body.String(); !strings.Contains(page, "Untitled deck") { |
| 187 | t.Errorf("a deck made with no title is not called Untitled deck") |
| 188 | } |
| 189 | |
| 190 | if rec := post(t, a, "/decks", url.Values{"theme": {"neon"}}, cookies...); rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), "Pick one of the themes listed.") { |
| 191 | t.Errorf("an unknown theme = %d, want 422 with the sentence", rec.Code) |
| 192 | } |
| 193 | |
| 194 | page := mustGet(t, a, "/decks/new", http.StatusOK, cookies).Body.String() |
| 195 | if !strings.Contains(page, `action="/decks"`) || strings.Contains(page, `name="title"`) || !strings.Contains(page, "Create deck") { |
| 196 | t.Errorf("GET /decks/new is not one Create button:\n%s", page) |
| 197 | } |
| 198 | } |
| 199 | |