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
3// The shared chrome as Docs renders it (estilo chrome, spec
4// 2026-09-23-shared-chrome.md). A direct App.Handler() call installs no
5// locale middleware, so rastrillo.T returns catalog keys verbatim: every
6// label below is its key.
7
8import (
9 "html"
10 "net/http"
11 "net/http/httptest"
12 "net/url"
13 "regexp"
14 "strings"
15 "testing"
16
17 "github.com/carlosframework/docs/internal/docs"
18)
19
20func getAccept(t *testing.T, app *docs.App, cookies []*http.Cookie, path string, header ...string) *httptest.ResponseRecorder {
21 t.Helper()
22 req := httptest.NewRequest(http.MethodGet, path, nil)
23 req.Header.Set("Accept", "text/html,application/xhtml+xml")
24 for i := 0; i+1 < len(header); i += 2 {
25 req.Header.Set(header[i], header[i+1])
26 }
27 for _, c := range cookies {
28 req.AddCookie(c)
29 }
30 rec := httptest.NewRecorder()
31 app.Handler().ServeHTTP(rec, req)
32 return rec
33}
34
35func titleOf(body string) string {
36 m := regexp.MustCompile(`<title>([^<]*)</title>`).FindStringSubmatch(body)
37 if m == nil {
38 return ""
39 }
40 return m[1]
41}
42
43// Every page's <title> is "{part} · {part} · Docs", built by the layout
44// alone (spec §10): the root is just the app's name. The sign-in door is
45// the one exception: its title is rastrillo's for the state it shows
46// (estilo-signin-screen-title), which already names Docs, so that the tab
47// and the heading always say the same thing.
48func TestPageTitlesFollowTheSuitePattern(t *testing.T) {
49 app, _ := docs.NewForTest(t)
50 alice := signInCookies(t, app, "alice@example.com")
51 sqid := createDoc(t, app, alice, "Minutes")
52
53 cases := []struct {
54 name, path string
55 cookies []*http.Cookie
56 want string
57 }{
58 {"list", "/", alice, "docs.app.name"},
59 {"search", "/?q=min", alice, "“min” · docs.app.name"},
60 {"document", "/d/" + sqid, alice, "Minutes · docs.app.name"},
61 {"share", "/d/" + sqid + "/share", alice, "docs.share.title · Minutes · docs.app.name"},
62 {"sign in", "/signin", nil, "Sign in to Docs"},
63 {"other ways", "/signin?ways=1", nil, "Other ways to sign in to Docs"},
64 {"problem", "/signin?err=rate", nil, "Problem signing in to Docs"},
65 {"sent", "/signin?sent=1", nil, "Check your email to sign in to Docs"},
66 {"signed out", "/signed-out", nil, "Signed out · docs.app.name"},
67 }
68 for _, c := range cases {
69 rec := getAccept(t, app, c.cookies, c.path)
70 if got := titleOf(rec.Body.String()); got != c.want {
71 t.Errorf("%s: <title> = %q, want %q (status %d)", c.name, got, c.want, rec.Code)
72 }
73 }
74}
75
76// The bar is the same bar on every signed-in page: 52px is estilo's, and
77// this is the structure behind it. Exactly one Sign out, the account menu
78// last in the end group, and no rastrillo shell left anywhere.
79func TestEverySignedInPageHasOneSignOutInTheSameCorner(t *testing.T) {
80 app, _ := docs.NewForTest(t)
81 alice := signInCookies(t, app, "alice@example.com")
82 sqid := createDoc(t, app, alice, "Minutes")
83 for _, path := range []string{"/", "/?q=min", "/d/" + sqid, "/d/" + sqid + "/share", "/no/such/page", "/d/doesnotexist"} {
84 body := getAccept(t, app, alice, path).Body.String()
85 if n := strings.Count(body, "docs.signout.label"); n != 1 {
86 t.Errorf("%s: Sign out appears %d times, want exactly 1", path, n)
87 }
88 if strings.Contains(body, "rst-shell") {
89 t.Errorf("%s: the rastrillo shell is still rendered", path)
90 }
91 end := strings.Index(body, "<div estilo-bar-end>")
92 acct := strings.Index(body, "<details estilo-account")
93 closeEnd := strings.Index(body[acct:], "</details>\n </div>\n</header>")
94 if end < 0 || acct < end || closeEnd < 0 {
95 t.Errorf("%s: the account menu is not the last thing in the bar's end group", path)
96 }
97 if !strings.Contains(body, "alice@example.com") {
98 t.Errorf("%s: the account menu does not say who is signed in", path)
99 }
100 }
101}
102
103// Signed out, the corner is "Sign in", carrying the page to come back to;
104// on the sign-in pages themselves the corner is empty; on the public page
105// the brand is plain text and Sign in carries no capability path.
106func TestTheSignedOutCorner(t *testing.T) {
107 app, _ := docs.NewForTest(t)
108 alice := signInCookies(t, app, "alice@example.com")
109 sqid := createDoc(t, app, alice, "Minutes")
110 body := getAccept(t, app, nil, "/no/such/page?x=1").Body.String()
111 if !strings.Contains(body, `<a rst-btn href="/signin?return_to=`+url.QueryEscape("/no/such/page?x=1")+`" estilo-bar-signin>docs.chrome.signin</a>`) {
112 t.Error("the signed-out corner has no Sign in carrying this page", body)
113 }
114 for _, path := range []string{"/signin", "/signin?sent=1", "/signin?err=expired", "/signed-out"} {
115 body := getAccept(t, app, nil, path).Body.String()
116 if strings.Contains(body, "estilo-bar-signin") || strings.Contains(body, "estilo-account") {
117 t.Errorf("%s: a sign-in page shows the sign-in corner", path)
118 }
119 if !strings.Contains(body, "<header estilo-bar") {
120 t.Errorf("%s: a sign-in page has no bar (and so no name)", path)
121 }
122 }
123
124 post(t, app, alice, "/d/"+sqid+"/share/link", "on=1")
125 share := get(t, app, alice, "/d/"+sqid+"/share").Body.String()
126 m := regexp.MustCompile(`/p/[A-Za-z0-9_-]+`).FindString(share)
127 if m == "" {
128 t.Fatal("no public link to follow")
129 }
130 public := getAccept(t, app, nil, m).Body.String()
131 if strings.Contains(public, "<a estilo-bar-brand") || !strings.Contains(public, "<span estilo-bar-brand>") {
132 t.Error("the public page's brand is a link a stranger is bounced by")
133 }
134 if !strings.Contains(public, `<a rst-btn href="/signin" estilo-bar-signin>`) {
135 t.Error("the public page's Sign in carries the capability path, or is missing")
136 }
137 if !strings.Contains(public, `rst-tone="info"`) || strings.Contains(public, "rst-notice") {
138 t.Error("the public notice is not an info callout")
139 }
140}
141
142// The switcher appears only with a hub directory configured. It shows the
143// suite's six apps as tiles at their hosts under the directory's, with
144// Docs marked current, and "All apps" below a divider.
145func TestTheSwitcherShowsTheSuite(t *testing.T) {
146 plain, _ := docs.NewForTest(t)
147 if body := get(t, plain, signInCookies(t, plain, "a@example.com"), "/").Body.String(); strings.Contains(body, "estilo-switcher") {
148 t.Error("a switcher with no hub directory")
149 }
150 app, err := docs.NewForTestWith(t, docs.Config{Directory: "https://oficina.works"})
151 if err != nil {
152 t.Fatal(err)
153 }
154 body := get(t, app, signInCookies(t, app, "a@example.com"), "/").Body.String()
155 start := strings.Index(body, `<details estilo-switcher name="estilo-menus">`)
156 if start < 0 {
157 t.Fatal("no switcher", body)
158 }
159 panel := body[start:]
160 panel = panel[:strings.Index(panel, "</details>")]
161 grid, allApps, ok := strings.Cut(panel, "<hr>")
162 if !ok || !strings.Contains(allApps, `<a estilo-menu-item href="https://oficina.works/"><span estilo-menu-label>docs.chrome.allapps</span></a>`) {
163 t.Error("the switcher does not offer All apps below the tiles", panel)
164 }
165 var tiles []string
166 for _, tile := range strings.Split(grid, "<a estilo-switcher-app ")[1:] {
167 tiles = append(tiles, tile[:strings.Index(tile, "</a>")])
168 }
169 want := []string{"docs", "sheets", "slides", "calendar", "memoria", "calls"}
170 names := []string{"Docs", "Sheets", "Slides", "Calendar", "Memoria", "Calls"}
171 if len(tiles) != len(want) {
172 t.Fatalf("%d tiles, want %d: %s", len(tiles), len(want), grid)
173 }
174 for i, slug := range want {
175 head := `href="https://` + slug + `.oficina.works/"`
176 if slug == "docs" {
177 head += ` aria-current="true"`
178 }
179 head += `><img src="/static/estilo/brand/` + slug + `.svg" alt="" width="32" height="32"><span>` + names[i]
180 if !strings.HasPrefix(tiles[i], head) {
181 t.Errorf("tile %d = %s, want it to start %s", i, tiles[i], head)
182 }
183 }
184 if strings.Count(grid, "aria-current") != 1 || !strings.Contains(tiles[0], "<span estilo-visually-hidden> docs.chrome.thisapp</span>") {
185 t.Error("Docs is not the one current tile", grid)
186 }
187 if res := get(t, app, nil, "/static/estilo/brand/calls.svg"); res.Code != http.StatusOK {
188 t.Errorf("a tile's icon is %d", res.Code)
189 }
190 for _, bad := range []string{"http://oficina.works", "https://oficina.works/", "https://Oficina.works", "https://127.0.0.1", "https://localhost", "https://a.b:8443", "https://user@a.b"} {
191 if _, err := docs.NewForTestWith(t, docs.Config{Directory: bad}); err == nil {
192 t.Errorf("directory %q was accepted", bad)
193 }
194 }
195}
196
197// A writer renames in the bar: a real form, so Enter works with scripts
198// off, and it lands back on the document. A reader gets the title
199// read-only (spec §5.4).
200func TestTheBarRenamesTheDocument(t *testing.T) {
201 app, _ := docs.NewForTest(t)
202 alice := signInCookies(t, app, "alice@example.com")
203 sqid := createDoc(t, app, alice, "Untitled")
204 body := get(t, app, alice, "/d/"+sqid).Body.String()
205 // The editor's bar is stacked, so estilo draws the fitted title: its
206 // size attribute is estilo's first guess (TitleEdit.Size), whatever
207 // that guess is.
208 fitted := regexp.MustCompile(`name="title" value="Untitled" size="\d+" aria-label="docs.chrome.title.label"`)
209 if !strings.Contains(body, `<form estilo-bar-title method="post" action="/d/`+sqid+`/title">`) || !fitted.MatchString(body) {
210 t.Fatal("a writer has no editable title in the bar")
211 }
212 req := httptest.NewRequest(http.MethodPost, "/d/"+sqid+"/title", strings.NewReader("title=Board+minutes"))
213 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
214 req.Header.Set("Accept", "text/html")
215 req.Header.Set("Sec-Fetch-Site", "same-origin")
216 for _, c := range alice {
217 req.AddCookie(c)
218 }
219 rec := httptest.NewRecorder()
220 app.Handler().ServeHTTP(rec, req)
221 if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/d/"+sqid {
222 t.Fatalf("rename: %d %q, want 303 back to the document", rec.Code, rec.Header().Get("Location"))
223 }
224 if title := titleOf(get(t, app, alice, "/d/"+sqid).Body.String()); title != "Board minutes · docs.app.name" {
225 t.Fatalf("after rename the title is %q", title)
226 }
227
228 bob := signInCookies(t, app, "bob@example.com")
229 post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=reader")
230 reader := get(t, app, bob, "/d/"+sqid).Body.String()
231 if strings.Contains(reader, "estilo-bar-title") || !strings.Contains(reader, `<span estilo-bar-context dir="auto" title="Board minutes">Board minutes</span>`) {
232 t.Error("a reader is offered rename, or loses the title")
233 }
234}
235
236// Appearance is per app, in a host-only cookie, and the layout writes it
237// into <html data-theme> so the first paint is right.
238func TestAppearanceIsRememberedAndRendered(t *testing.T) {
239 app, _ := docs.NewForTest(t)
240 alice := signInCookies(t, app, "alice@example.com")
241 if body := get(t, app, alice, "/").Body.String(); strings.Contains(body, "data-theme") || !strings.Contains(body, `value="system" aria-pressed="true"`) {
242 t.Fatal("no preference should follow the system")
243 }
244 rec := post(t, app, alice, "/appearance", "mode=dark&return_to=%2F%3Fq%3Dx")
245 if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/?q=x" {
246 t.Fatalf("POST /appearance: %d %q", rec.Code, rec.Header().Get("Location"))
247 }
248 cookies := append(rec.Result().Cookies(), alice...)
249 body := get(t, app, cookies, "/").Body.String()
250 if !strings.Contains(body, `<html lang="en" dir="ltr" data-theme="dark">`) || !strings.Contains(body, `value="dark" aria-pressed="true"`) {
251 t.Fatal("the chosen appearance is not rendered")
252 }
253 // Outside the session guard, inside the CSRF check.
254 signedOut := post(t, app, nil, "/appearance", "mode=light&return_to=%2F")
255 if signedOut.Code != http.StatusSeeOther {
256 t.Errorf("an expired session turns Appearance into %d", signedOut.Code)
257 }
258 cross := httptest.NewRequest(http.MethodPost, "/appearance", strings.NewReader("mode=light"))
259 cross.Header.Set("Content-Type", "application/x-www-form-urlencoded")
260 cross.Header.Set("Sec-Fetch-Site", "cross-site")
261 crossRec := httptest.NewRecorder()
262 app.Handler().ServeHTTP(crossRec, cross)
263 if crossRec.Code == http.StatusSeeOther || len(crossRec.Result().Cookies()) != 0 {
264 t.Error("a cross-site form set the appearance")
265 }
266}
267
268// Every notice is a rastrillo callout with one of its four tones (spec
269// §4): rst-notice, a valued rst-callout attribute and "warn" are gone.
270func TestTemplatesUseOnlyCalloutTones(t *testing.T) {
271 for _, bad := range []string{"rst-notice", `rst-callout="`, `rst-tone="warn"`} {
272 for path, body := range docs.TestViewSources(t) {
273 if strings.Contains(body, bad) {
274 t.Errorf("%s writes %s", path, bad)
275 }
276 }
277 }
278}
279
280// Shortcut labels are the reader's platform's, decided on the server so
281// nothing flips after paint (spec §7.3).
282func TestShortcutLabelsFollowThePlatform(t *testing.T) {
283 app, _ := docs.NewForTest(t)
284 alice := signInCookies(t, app, "alice@example.com")
285 sqid := createDoc(t, app, alice, "Minutes")
286 // html/template writes "+" in an attribute as &#43;; a browser reads it
287 // back as "+", and so does this.
288 mac := html.UnescapeString(getAccept(t, app, alice, "/d/"+sqid, "User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)").Body.String())
289 win := html.UnescapeString(getAccept(t, app, alice, "/d/"+sqid, "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)").Body.String())
290 for _, want := range []string{`data-docs-shortcut="⌘Z"`, `data-docs-shortcut="⇧⌘Z"`, `data-docs-shortcut="⌘B"`, `data-docs-shortcut="⌘K"`, `data-keys="⇧⇥"`, `data-keys="⎋"`} {
291 if !strings.Contains(mac, want) {
292 t.Errorf("Mac page lacks %s", want)
293 }
294 }
295 for _, want := range []string{`data-docs-shortcut="Ctrl+Z"`, `data-docs-shortcut="Ctrl+Y"`, `data-docs-shortcut="Ctrl+B"`, `data-docs-shortcut="Ctrl+K"`, `data-keys="Shift+Tab"`, `data-keys="Esc"`} {
296 if !strings.Contains(win, want) {
297 t.Errorf("Windows page lacks %s", want)
298 }
299 }
300 if strings.Contains(mac, "Ctrl+") {
301 t.Error("the Mac page still says Ctrl")
302 }
303}
304
305// Unbuilt features are hidden, not disabled (spec §7.3): no Insert menu,
306// no Find and replace, no Show ruler, and Sign out is not in File.
307func TestUnbuiltMenuItemsAreHidden(t *testing.T) {
308 app, _ := docs.NewForTest(t)
309 alice := signInCookies(t, app, "alice@example.com")
310 sqid := createDoc(t, app, alice, "Minutes")
311 body := get(t, app, alice, "/d/"+sqid).Body.String()
312 menubar := body[strings.Index(body, "<nav estilo-menubar"):]
313 menubar = menubar[:strings.Index(menubar, "</nav>")]
314 for _, gone := range []string{"docs.editor.menu.insert", "insert-", "find_replace", "view_ruler", "data-docs-unavailable", "/signout"} {
315 if strings.Contains(menubar, gone) {
316 t.Errorf("the menu bar still offers %s", gone)
317 }
318 }
319 for _, want := range []string{"docs.editor.menu.file", "docs.editor.menu.edit", "docs.editor.menu.view", "docs.editor.menu.format", "docs.editor.menu.tools", "docs.editor.menu.help", `data-docs-jumpto`} {
320 if !strings.Contains(menubar, want) {
321 t.Errorf("the menu bar lost %s", want)
322 }
323 }
324 if n := strings.Count(menubar, `name="estilo-menus"`); n != 6 {
325 t.Errorf("%d menubar menus in the shared exclusivity group, want 6", n)
326 }
327}
328