oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package docs_test
2
3import (
4 "net/http"
5 "net/url"
6 "regexp"
7 "strings"
8 "testing"
9
10 "github.com/carlosframework/docs/internal/docs"
11)
12
13// The phone document screen (design/phone-web, approved by Paul
14// 2026-11-28; slice P1). Below 704px the estilo bar is compact
15// (chrome.Bar.Compact): Back, the undo/redo pair, Aa and •••; the page
16// carries a large title with the save state under it.
17
18// section is the markup from the first open of marker up to the first
19// close after it.
20func section(t *testing.T, body, open, close string) string {
21 t.Helper()
22 i := strings.Index(body, open)
23 if i < 0 {
24 t.Fatalf("no %q in the page", open)
25 }
26 s := body[i:]
27 j := strings.Index(s, close)
28 if j < 0 {
29 t.Fatalf("%q never closes with %q", open, close)
30 }
31 return s[:j+len(close)]
32}
33
34// people makes a document owned by alice, with bob a reader and carol a
35// writer, and returns each one's page.
36func people(t *testing.T, title string) (app *docs.App, sqid string, pages map[string]string) {
37 t.Helper()
38 app, _ = docs.NewForTest(t)
39 alice := signInCookies(t, app, "alice@example.com")
40 sqid = createDoc(t, app, alice, title)
41 bob := signInCookies(t, app, "bob@example.com")
42 post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=reader")
43 carol := signInCookies(t, app, "carol@example.com")
44 post(t, app, alice, "/d/"+sqid+"/share", "address=carol@example.com&role=writer")
45 pages = map[string]string{}
46 for who, c := range map[string][]*http.Cookie{"owner": alice, "reader": bob, "writer": carol} {
47 pages[who] = get(t, app, c, "/d/"+sqid).Body.String()
48 }
49 return app, sqid, pages
50}
51
52// The bar is compact on the document: the trail holds Aa (writers) and
53// ••• (everyone), and the compact title is the inline title the bar shows
54// once the large title has scrolled away.
55func TestTheDocumentBarIsCompact(t *testing.T) {
56 _, _, pages := people(t, "Minutes")
57 for who, body := range pages {
58 header := section(t, body, "<header estilo-bar", "</header>")
59 if !strings.HasPrefix(header, `<header estilo-bar="sticky stacked compact">`) {
60 t.Errorf("%s: the document's bar is not compact: %.60s", who, header)
61 }
62 if !strings.Contains(header, "<svg estilo-bar-back ") {
63 t.Errorf("%s: the bar has no Back", who)
64 }
65 trail := section(t, header, "<div estilo-bar-trail>", "</details></div>")
66 if !strings.Contains(trail, `<details docs-more name="estilo-menus">`) {
67 t.Errorf("%s: the trail has no ••• menu:\n%s", who, trail)
68 }
69 hasAa := strings.Contains(trail, "docs-aa")
70 if hasAa != (who != "reader") {
71 t.Errorf("%s: Aa in the trail is %v", who, hasAa)
72 }
73 inline := section(t, header, "<div estilo-bar-compact-title>", "</div></div>")
74 // The state first in its line, in its tone, so an ellipsis never
75 // cuts a failure (Fable on #179, S2).
76 tone := map[string]string{"owner": "saved", "writer": "saved", "reader": "viewonly"}[who]
77 if !strings.Contains(inline, `<small docs-inline-sub><span docs-inline-state data-state="`+tone+`">`) {
78 t.Errorf("%s: the inline line does not open with the state, toned %s:\n%s", who, tone, inline)
79 }
80 if !strings.Contains(inline, "Minutes") || !strings.Contains(inline, `aria-hidden="true"`) {
81 t.Errorf("%s: the inline title is not the title, hidden from assistive technology (the large title is the heading):\n%s", who, inline)
82 }
83 if strings.Contains(header, "estilo-bar-more") {
84 t.Errorf("%s: the compact bar drew estilo's Menu sheet", who)
85 }
86 }
87}
88
89// ••• holds what each person can do, and nothing they cannot (absent, not
90// disabled): the owner's Share and Delete, a writer's Rename and the
91// commands the editor runs. Its rows have no shortcut hints.
92func TestTheMoreMenuIsFilteredByCapability(t *testing.T) {
93 _, sqid, pages := people(t, "Minutes")
94 labels := func(panel string) []string {
95 var out []string
96 for _, m := range regexp.MustCompile(`<span estilo-menu-label>([^<]*)</span>`).FindAllStringSubmatch(panel, -1) {
97 out = append(out, m[1])
98 }
99 return out
100 }
101 want := map[string][]string{
102 "owner": {"docs.share.action", "docs.chrome.title.rename", "docs.export.pdf.label", "docs.export.markdown.label", "docs.editor.menu.file.print", "docs.editor.menu.tools.wordcount", "docs.editor.menu.file.open", "docs.editor.menu.view.paged", "docs.editor.menu.view.infinite", "docs.editor.menu.help", "docs.editor.menu.tools.shortcuts", "docs.help.fonts", "docs.help.colophon", "docs.editor.menu.file.delete"},
103 "writer": {"docs.chrome.title.rename", "docs.export.pdf.label", "docs.export.markdown.label", "docs.editor.menu.file.print", "docs.editor.menu.tools.wordcount", "docs.editor.menu.file.open", "docs.editor.menu.view.paged", "docs.editor.menu.view.infinite", "docs.editor.menu.help", "docs.editor.menu.tools.shortcuts", "docs.help.fonts", "docs.help.colophon"},
104 "reader": {"docs.export.pdf.label", "docs.export.markdown.label", "docs.editor.menu.file.open", "docs.editor.menu.help", "docs.help.fonts", "docs.help.colophon"},
105 }
106 for who, body := range pages {
107 panel := section(t, body, "<details docs-more", "</div></details></div>")
108 got := labels(panel)
109 if strings.Join(got, "|") != strings.Join(want[who], "|") {
110 t.Errorf("%s's •••:\n got %q\nwant %q", who, got, want[who])
111 }
112 if strings.Contains(panel, "<kbd") {
113 t.Errorf("%s: ••• shows a shortcut hint", who)
114 }
115 if strings.Contains(panel, " disabled") && who == "reader" {
116 t.Errorf("a reader's ••• has a disabled row; a row they cannot use must be absent")
117 }
118 }
119 owner := section(t, pages["owner"], "<details docs-more", "</div></details></div>")
120 for _, want := range []string{
121 `href="/d/` + sqid + `/share"`,
122 `<a estilo-menu-item="danger" href="/d/` + sqid + `/delete">`,
123 `href="#docs-title" data-docs-rename`,
124 `data-docs-proxy="print"`,
125 `data-docs-proxy="view_paged" aria-pressed="false"`,
126 `data-docs-proxy="view_infinite" aria-pressed="true"`,
127 `<details docs-more-help>`,
128 } {
129 if !strings.Contains(owner, want) {
130 t.Errorf("the owner's ••• lacks %s", want)
131 }
132 }
133 // Help is a disclosure inside the panel, never a member of the bar's
134 // exclusive group: a nested <details name="estilo-menus"> would close
135 // ••• when it opened.
136 if strings.Count(owner, `name="estilo-menus"`) != 1 {
137 t.Error("a <details> inside ••• joined the bar's exclusive group")
138 }
139 // A proxied row ships disabled, as the menu bar's command it runs
140 // does, until the editor mounts.
141 for _, b := range regexp.MustCompile(`<button [^>]*data-docs-proxy="[^"]*"[^>]*>`).FindAllString(owner, -1) {
142 if !strings.Contains(b, " disabled") {
143 t.Errorf("a proxied row is not disabled until the editor mounts: %s", b)
144 }
145 }
146}
147
148// A reader's menu bar holds only what they can use: no Edit or Format,
149// and no command the editor runs (they get no editor). A writer's is
150// whole.
151func TestAReadersMenuBarHasOnlyWhatTheyCanUse(t *testing.T) {
152 _, _, pages := people(t, "Minutes")
153 reader := section(t, pages["reader"], "<nav estilo-menubar", "</nav>")
154 if strings.Contains(reader, "data-docs-command") {
155 t.Error("a reader's menu bar has an editor command, disabled for good")
156 }
157 for _, id := range []string{"edit", "format", "view", "tools"} {
158 if strings.Contains(reader, `id="docs-menu-`+id+`"`) {
159 t.Errorf("a reader's menu bar has the %s menu, with nothing in it they can use", id)
160 }
161 }
162 for _, id := range []string{"file", "help"} {
163 if !strings.Contains(reader, `id="docs-menu-`+id+`"`) {
164 t.Errorf("a reader lost the %s menu", id)
165 }
166 }
167 if strings.Contains(reader, "<hr>\n </div>") || regexp.MustCompile(`<div estilo-menu>\s*<hr>`).MatchString(reader) {
168 t.Error("a reader's menu starts or ends with a divider")
169 }
170 writer := section(t, pages["writer"], "<nav estilo-menubar", "</nav>")
171 for _, id := range []string{"file", "edit", "view", "format", "tools", "help"} {
172 if !strings.Contains(writer, `id="docs-menu-`+id+`"`) {
173 t.Errorf("a writer lost the %s menu", id)
174 }
175 }
176}
177
178// (The test app resolves no catalog, so labels read as their keys.)
179//
180// The large title: the document's heading on a phone, renamed in place by
181// a writer through the same POST as the bar's title, with the save state
182// under it; a reader's says "View only". Hidden from 704px, where the bar
183// carries the title.
184func TestTheLargeTitle(t *testing.T) {
185 _, sqid, pages := people(t, "Minutes <draft>")
186 for who, body := range pages {
187 block := section(t, body, "<div docs-title-block", "<div docs-chrome>")
188 if !strings.Contains(block, `<h1 docs-title dir="auto"`) || !strings.Contains(block, "Minutes &lt;draft&gt;") {
189 t.Errorf("%s: no large title heading, escaped:\n%s", who, block)
190 }
191 form := strings.Contains(block, `<form docs-title-form method="post" action="/d/`+sqid+`/title">`)
192 if form != (who != "reader") {
193 t.Errorf("%s: the rename form is %v", who, form)
194 }
195 if who != "reader" {
196 for _, want := range []string{`<input docs-title-input id="docs-title-input" name="title" value="Minutes &lt;draft&gt;"`, `aria-label="docs.chrome.title.label"`, `<button rst-btn type="submit" docs-title-submit>docs.chrome.title.rename</button>`} {
197 if !strings.Contains(block, want) {
198 t.Errorf("%s: the title form lacks %s", who, want)
199 }
200 }
201 status := section(t, block, "<span estilo-save-status", "</span>")
202 for _, want := range []string{`estilo-save-status="inline subtitle"`, `data-state="saved"`, `data-text-saved="docs.editor.status.saved"`, `data-text-saving="docs.editor.status.saving"`, ">docs.editor.status.saved</span>"} {
203 if !strings.Contains(status, want) {
204 t.Errorf("%s: the subtitle's state lacks %s:\n%s", who, want, status)
205 }
206 }
207 } else {
208 if !strings.Contains(block, "docs.show.viewonly") || strings.Contains(block, "estilo-save-status") {
209 t.Errorf("a reader's subtitle is not View only:\n%s", block)
210 }
211 }
212 shared := strings.Contains(block, "docs.list.shared") || strings.Contains(block, "docs.list.team") // a new document is the team's by default
213 if shared != (who != "owner") {
214 t.Errorf("%s: the subtitle names the share: %v", who, shared)
215 }
216 }
217 // The corner toast stays for wide screens, unchanged.
218 if !strings.Contains(pages["owner"], `estilo-save-status="floating"`) {
219 t.Error("the floating save status is gone")
220 }
221}
222
223// A title is one line of text: a rename or a new document's title loses
224// its newlines, tabs and other control characters to spaces, server-side,
225// whatever sent it (Fable on #179). The large title's field holds at most
226// what the handler takes: 256 characters are at most 1024 bytes.
227func TestATitleIsOneLineOfText(t *testing.T) {
228 app, _ := docs.NewForTest(t)
229 alice := signInCookies(t, app, "alice@example.com")
230 sqid := createDoc(t, app, alice, "Minutes")
231 if rec := post(t, app, alice, "/d/"+sqid+"/title", "title="+url.QueryEscape("Board\nminutes\r\n\tdraft\x07 two")); rec.Code != http.StatusSeeOther {
232 t.Fatalf("rename: %d", rec.Code)
233 }
234 body := get(t, app, alice, "/d/"+sqid).Body.String()
235 if !strings.Contains(body, `value="Board minutes draft two"`) {
236 t.Errorf("the renamed title kept a control character or a run of spaces:\n%s", section(t, body, "<input docs-title-input", ">"))
237 }
238 made := createDoc(t, app, alice, "New\nline")
239 if !strings.Contains(get(t, app, alice, "/d/"+made).Body.String(), `value="New line"`) {
240 t.Error("a new document's title kept its newline")
241 }
242 if !strings.Contains(section(t, body, "<input docs-title-input", ">"), ` maxlength="256"`) {
243 t.Error("the large title's field has no maxlength")
244 }
245}
246
247// Aa opens the Format sheet on a phone, which is the inspector restyled
248// (design/phone-web, P2), so it controls the inspector; so does the quick
249// bar's Aa.
250func TestAaControlsTheFormatSheet(t *testing.T) {
251 _, _, pages := people(t, "Minutes")
252 body := pages["writer"]
253 aa := section(t, body, "<button estilo-bar-tool type=\"button\" docs-aa", ">")
254 if !strings.Contains(aa, `aria-controls="docs-inspector"`) {
255 t.Errorf("Aa does not control the Format sheet: %s", aa)
256 }
257 quick := section(t, body, "<button docs-qtool docs-quickbar-aa", ">")
258 if !strings.Contains(quick, `aria-controls="docs-inspector"`) {
259 t.Errorf("the quick bar's Aa does not control the Format sheet: %s", quick)
260 }
261 if strings.Contains(pages["reader"], "docs-quickbar") && strings.Contains(pages["reader"], "<div docs-quickbar") {
262 t.Error("a reader has a quick format bar")
263 }
264}
265
266// The large title is a button that renames, and says so: its name is the
267// title, its description the verb (Fable on #179).
268func TestTheTitleButtonSaysItRenames(t *testing.T) {
269 _, _, pages := people(t, "Minutes")
270 block := section(t, pages["writer"], "<div docs-title-block", "<div docs-chrome>")
271 if !strings.Contains(block, `<button type="button" docs-title-edit aria-describedby="docs-title-edit-says">`) || !strings.Contains(block, `<span id="docs-title-edit-says" hidden>docs.chrome.title.rename</span>`) {
272 t.Errorf("the title button has no description saying it renames:\n%s", block)
273 }
274}
275