| 1 | package docs_test |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/carlosframework/docs/internal/docs" |
| 10 | ) |
| 11 | |
| 12 | // The I2 header (spec 2026-11-11): on the editor the estilo bar is |
| 13 | // stacked, with the document's menu bar in its Menubar slot and the |
| 14 | // undo/redo pair in its Lead slot; every other page keeps the one-row bar |
| 15 | // with neither. |
| 16 | func TestTheEditorBarIsStackedWithItsSlots(t *testing.T) { |
| 17 | app, _ := docs.NewForTest(t) |
| 18 | alice := signInCookies(t, app, "alice@example.com") |
| 19 | sqid := createDoc(t, app, alice, "Minutes") |
| 20 | |
| 21 | body := get(t, app, alice, "/d/"+sqid).Body.String() |
| 22 | header := body[strings.Index(body, "<header estilo-bar"):] |
| 23 | header = header[:strings.Index(header, "</header>")] |
| 24 | // Compact too (design/phone-web): the phone's one row below 704px. |
| 25 | if !strings.HasPrefix(header, `<header estilo-bar="sticky stacked compact">`) { |
| 26 | t.Errorf("the editor's bar is not stacked: %.60s", header) |
| 27 | } |
| 28 | if !strings.Contains(header, "<div estilo-bar-menubar><nav estilo-menubar") { |
| 29 | t.Error("the editor's menu bar is not in the bar's Menubar slot") |
| 30 | } |
| 31 | if !strings.Contains(header, `<div estilo-bar-lead><div docs-history="lead" role="group"`) { |
| 32 | t.Error("the undo/redo pair is not in the bar's Lead slot") |
| 33 | } |
| 34 | // One menu bar in the page: its menus have ids, and two would be two |
| 35 | // sets of the same ids (estilo draws it as the second row below 800px, |
| 36 | // so the strip no longer draws its own). |
| 37 | if n := strings.Count(body, "<nav estilo-menubar"); n != 1 { |
| 38 | t.Errorf("%d menu bars in the page, want 1", n) |
| 39 | } |
| 40 | if n := strings.Count(body, `id="docs-menu-file"`); n != 1 { |
| 41 | t.Errorf("%d File menus in the page, want 1", n) |
| 42 | } |
| 43 | // Two homes for the pair, the lead's and the strip's, and neither in |
| 44 | // the toolbar or its roving tab stop. |
| 45 | if n := strings.Count(body, `data-docs-command="undo" aria-label=`); n != 2 { |
| 46 | t.Errorf("%d undo buttons outside the menu, want 2 (the lead's and the strip's)", n) |
| 47 | } |
| 48 | if !strings.Contains(body, `<div docs-history role="group"`) { |
| 49 | t.Error("the strip has no undo/redo pair for below 800px") |
| 50 | } |
| 51 | toolbar := body[strings.Index(body, "<div docs-toolbar "):] |
| 52 | toolbar = toolbar[:strings.Index(toolbar, "</aside>")] |
| 53 | if strings.Contains(toolbar, `"undo"`) || strings.Contains(toolbar, `"redo"`) { |
| 54 | t.Error("undo or redo is still inside the role=toolbar") |
| 55 | } |
| 56 | history := regexp.MustCompile(`<button docs-tool[^>]*data-docs-command="(?:undo|redo)"[^>]*>`) |
| 57 | for _, b := range history.FindAllString(body, -1) { |
| 58 | if strings.Contains(b, "tabindex") { |
| 59 | t.Errorf("an undo/redo button carries a tabindex, as a roving control would: %s", b) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // The index's bar is the one-row bar, unchanged. |
| 64 | index := get(t, app, alice, "/").Body.String() |
| 65 | if !strings.Contains(index, `<header estilo-bar="sticky">`) { |
| 66 | t.Error("the index's bar is not the one-row bar") |
| 67 | } |
| 68 | for _, slot := range []string{"estilo-bar-stack", "estilo-bar-menubar", "estilo-bar-lead", "data-estilo-toggle"} { |
| 69 | if strings.Contains(index, slot) { |
| 70 | t.Errorf("the index's bar has %s", slot) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // The inspector's state is the docs-inspector cookie's, rendered onto the |
| 76 | // inspector and into Format's aria-expanded, so the first paint is right: |
| 77 | // open unless the cookie says exactly "closed". Format is rendered |
| 78 | // disabled (brand/inspector.js enables it) and wide only. |
| 79 | func TestFormatIsRenderedFromTheInspectorCookie(t *testing.T) { |
| 80 | app, _ := docs.NewForTest(t) |
| 81 | alice := signInCookies(t, app, "alice@example.com") |
| 82 | sqid := createDoc(t, app, alice, "Minutes") |
| 83 | |
| 84 | for _, c := range []struct { |
| 85 | cookie string |
| 86 | open bool |
| 87 | }{ |
| 88 | {"", true}, |
| 89 | {"open", true}, |
| 90 | {"closed", false}, |
| 91 | {"Closed", true}, |
| 92 | {"closed ", true}, |
| 93 | {`"><script>`, true}, |
| 94 | } { |
| 95 | cookies := alice |
| 96 | if c.cookie != "" { |
| 97 | cookies = append(append([]*http.Cookie(nil), alice...), &http.Cookie{Name: docs.InspectorCookie, Value: c.cookie}) |
| 98 | } |
| 99 | body := get(t, app, cookies, "/d/"+sqid).Body.String() |
| 100 | expanded, state := "true", "open" |
| 101 | if !c.open { |
| 102 | expanded, state = "false", "closed" |
| 103 | } |
| 104 | toggle := `<button rst-btn type="button" aria-expanded="` + expanded + `" aria-controls="docs-inspector" disabled data-estilo-toggle estilo-bar-wide>` |
| 105 | if !strings.Contains(body, toggle) { |
| 106 | t.Errorf("cookie %q: Format is not rendered %s", c.cookie, toggle) |
| 107 | } |
| 108 | aside := `<aside docs-inspector id="docs-inspector" rst-box data-state="` + state + `" aria-label="docs.editor.inspector.label">` |
| 109 | if !strings.Contains(body, aside) { |
| 110 | t.Errorf("cookie %q: the inspector is not rendered %s", c.cookie, aside) |
| 111 | } |
| 112 | if strings.Contains(body, "<script>\"") || strings.Contains(body, `"><script>`) { |
| 113 | t.Errorf("cookie %q reached the page", c.cookie) |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Each of the inspector's sections is a role="group" named by its visible |
| 119 | // label, and the labels are catalog keys. |
| 120 | func TestTheInspectorSectionsAreNamedGroups(t *testing.T) { |
| 121 | app, _ := docs.NewForTest(t) |
| 122 | alice := signInCookies(t, app, "alice@example.com") |
| 123 | sqid := createDoc(t, app, alice, "Minutes") |
| 124 | body := get(t, app, alice, "/d/"+sqid).Body.String() |
| 125 | for _, g := range []string{"style", "text", "align", "lists"} { |
| 126 | group := `<div docs-toolbar-group="` + g + `" role="group" aria-labelledby="docs-group-` + g + `">` |
| 127 | label := `<span docs-toolbar-label id="docs-group-` + g + `">docs.editor.toolbar.group.` + g + `</span>` |
| 128 | at := strings.Index(body, group) |
| 129 | if at < 0 { |
| 130 | t.Errorf("no %s group: %s", g, group) |
| 131 | continue |
| 132 | } |
| 133 | if !strings.HasPrefix(strings.TrimSpace(body[at+len(group):]), label) { |
| 134 | t.Errorf("the %s group does not open with its label %s", g, label) |
| 135 | } |
| 136 | } |
| 137 | if n := strings.Count(body, `role="toolbar"`); n != 1 { |
| 138 | t.Errorf("%d toolbars, want one at every width", n) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // A reader cannot format (review on #106): no inspector, no strip, no |
| 143 | // Format toggle and no undo/redo, in the bar or the strip. A dead panel |
| 144 | // pushed their sheet left; with no inspector the sheet is plainly |
| 145 | // centred. The writer and the owner keep all of it. |
| 146 | func TestAReaderGetsNoFormattingChrome(t *testing.T) { |
| 147 | app, _ := docs.NewForTest(t) |
| 148 | alice := signInCookies(t, app, "alice@example.com") |
| 149 | sqid := createDoc(t, app, alice, "Minutes") |
| 150 | bob := signInCookies(t, app, "bob@example.com") |
| 151 | post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=reader") |
| 152 | carol := signInCookies(t, app, "carol@example.com") |
| 153 | post(t, app, alice, "/d/"+sqid+"/share", "address=carol@example.com&role=writer") |
| 154 | |
| 155 | formatting := []string{"<aside docs-inspector", "<div docs-toolbar ", "<div docs-bar", "data-estilo-toggle", "<div docs-history", "<div estilo-bar-lead>", "/static/brand/inspector"} |
| 156 | reader := get(t, app, bob, "/d/"+sqid).Body.String() |
| 157 | for _, banned := range formatting { |
| 158 | if strings.Contains(reader, banned) { |
| 159 | t.Errorf("a reader's page has %q", banned) |
| 160 | } |
| 161 | } |
| 162 | // Still the stacked bar with its menus, and the document. |
| 163 | if !strings.Contains(reader, `<header estilo-bar="sticky stacked compact">`) || !strings.Contains(reader, "<nav estilo-menubar") || !strings.Contains(reader, "Minutes") { |
| 164 | t.Error("a reader lost the bar's menus or the document") |
| 165 | } |
| 166 | for who, cookies := range map[string][]*http.Cookie{"writer": carol, "owner": alice} { |
| 167 | body := get(t, app, cookies, "/d/"+sqid).Body.String() |
| 168 | for _, needed := range formatting { |
| 169 | if !strings.Contains(body, needed) { |
| 170 | t.Errorf("the %s's page lacks %q", who, needed) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |