oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "net/http/httptest"
5 "os"
6 "strings"
7 "testing"
8)
9
10// The menu bar is a second surface onto the toolbar's command table,
11// and these tests exist because "second surface" is exactly the shape
12// that drifts: a command renamed in one place and not the other fails
13// silently in front of a person, as a menu item that does nothing when
14// clicked.
15
16func newMenuViewForTest() menuView {
17 return newMenuViewFor(true)
18}
19
20// newMenuViewFor builds either variant. The guest's File menu is one item
21// shorter, and the structure rules below (no leading divider, every form
22// carries an action) have to hold for it too — a shorter list is exactly
23// where a leading divider would appear.
24//
25// A member is built as the document's owner, so the structure rules also
26// cover File's last group, the divider and Delete…, which only an owner
27// gets; a guest owns nothing and gets neither.
28func newMenuViewFor(canCreate bool) menuView {
29 return newMenuView(httptest.NewRequest("GET", "/d/abc123", nil), "abc123", canCreate, canCreate)
30}
31
32// Every command the menu names is implemented by one of the two modules
33// that implement commands — unless it is flagged as not built yet.
34//
35// This is the Go-model half of the same property
36// TestToolbarOptionValuesMatchTheBundle checks against the template.
37// Both are worth having: that one catches a command in the markup with
38// no implementation, this one catches a command in menu.go that never
39// reaches the markup at all, which is the case a template-only test
40// cannot see.
41func TestMenuCommandsExistInTheBundle(t *testing.T) {
42 toolbar, err := os.ReadFile("../../fixtures/toolbar.mjs")
43 if err != nil {
44 t.Fatal(err)
45 }
46 menu, err := os.ReadFile("../../fixtures/menu.mjs")
47 if err != nil {
48 t.Fatal(err)
49 }
50 // A third place a menu command can be implemented, and the reason
51 // is worth stating rather than looking like a widened net.
52 //
53 // toolbar.mjs holds commands that act on the editor's state; menu.mjs
54 // holds the ones that do not need it at all (Print). The View menu's
55 // paged/infinite pair is neither: it changes how the document is
56 // DRAWN, which is page-level state that editor-entry.js owns — it
57 // holds the mode, writes the cookie and configures the pagination
58 // plugin. Putting the pair in toolbar.mjs would mean the formatting
59 // bar's command table reaching up into the page's own view state.
60 entry, err := os.ReadFile("../../fixtures/editor-entry.js")
61 if err != nil {
62 t.Fatal(err)
63 }
64
65 v := newMenuViewForTest()
66 seen := 0
67 for _, name := range menuCommandNames(v) {
68 seen++
69 if strings.Contains(string(toolbar), name) ||
70 strings.Contains(string(menu), name) ||
71 strings.Contains(string(entry), name) {
72 continue
73 }
74 t.Errorf("menu.go offers %q, which none of fixtures/toolbar.mjs, fixtures/menu.mjs or fixtures/editor-entry.js implements", name)
75 }
76
77 // A guard on the guard. If the menu offered no commands at all the
78 // loop above would check nothing and report success. This repo has found
79 // several assertions unable to fail against the thing they name;
80 // the shape is always a check that cannot distinguish "no" from "I
81 // looked at nothing".
82 if seen == 0 {
83 t.Fatal("no menu commands to check, so this test verified nothing")
84 }
85}
86
87// The bar's shape. Each rule here is one way a menu item can be
88// malformed in a manner the Go compiler is happy with and a person is
89// not.
90func TestMenuStructureIsWellFormed(t *testing.T) {
91 // Both variants. The guest's File menu is one item shorter, and a
92 // shorter list is exactly where a leading divider appears — so the
93 // rules below have to hold for it, not only for a member's.
94 //
95 // The previous commit added newMenuViewFor and its comment saying so,
96 // and then never called it with false. The claim sat in a commit
97 // message for one round. (Re-review 4, finding 3.)
98 for _, canCreate := range []bool{true, false} {
99 t.Run(map[bool]string{true: "member", false: "guest"}[canCreate], func(t *testing.T) {
100 assertMenuWellFormed(t, newMenuViewFor(canCreate))
101 })
102 }
103}
104
105func assertMenuWellFormed(t *testing.T, v menuView) {
106 t.Helper()
107
108 if len(v.Menus) == 0 {
109 t.Fatal("the menu bar has no menus")
110 }
111 if v.Label == "" {
112 t.Error("the menu bar has no accessible name")
113 }
114
115 for _, m := range v.Menus {
116 if m.ID == "" || m.Title == "" {
117 t.Errorf("menu %q has no id or no title", m.ID)
118 }
119 if len(m.Items) == 0 {
120 t.Errorf("menu %q is empty — an empty menu is a title that opens onto nothing", m.ID)
121 }
122 // A menu that opens on a divider, or ends on one, has a rule
123 // against the panel's own edge where it separates nothing.
124 if m.Items[0].Kind == menuDivider {
125 t.Errorf("menu %q opens with a divider", m.ID)
126 }
127 if m.Items[len(m.Items)-1].Kind == menuDivider {
128 t.Errorf("menu %q ends with a divider", m.ID)
129 }
130
131 prevDivider := false
132 for _, it := range m.Items {
133 if it.Kind == menuDivider {
134 if prevDivider {
135 t.Errorf("menu %q has two dividers in a row", m.ID)
136 }
137 prevDivider = true
138 continue
139 }
140 prevDivider = false
141
142 if it.Label == "" {
143 t.Errorf("menu %q has an item with no label", m.ID)
144 }
145 switch it.Kind {
146 case menuCommand:
147 if it.Command == "" {
148 t.Errorf("menu %q: item %q is a command with no command name", m.ID, it.Label)
149 }
150 case menuLink:
151 if it.Href == "" {
152 t.Errorf("menu %q: item %q is a link with no href", m.ID, it.Label)
153 }
154 case menuForm:
155 if it.Action == "" {
156 t.Errorf("menu %q: item %q is a form with no action", m.ID, it.Label)
157 }
158 default:
159 t.Errorf("menu %q: item %q has unknown kind %q", m.ID, it.Label, it.Kind)
160 }
161 }
162 }
163}
164
165// The exports moved from two buttons pinned to the formatting bar into
166// the File menu, and the whole argument for <details> rather than a
167// scripted menubar was that they keep working with no JavaScript. This
168// checks the half of that claim the server owns: the links are still
169// rendered, still point at the same routes, and are still plain links
170// with a download attribute rather than anything a script has to
171// activate.
172func TestExportsAreScriptlessLinksInTheFileMenu(t *testing.T) {
173 v := newMenuViewForTest()
174
175 var file *menu
176 for i := range v.Menus {
177 if v.Menus[i].ID == "file" {
178 file = &v.Menus[i]
179 }
180 }
181 if file == nil {
182 t.Fatal("there is no File menu")
183 }
184
185 want := map[string]bool{
186 "/d/abc123/export.pdf": false,
187 "/d/abc123/export.md": false,
188 }
189 for _, it := range file.Items {
190 if _, ok := want[it.Href]; !ok {
191 continue
192 }
193 want[it.Href] = true
194 if it.Kind != menuLink {
195 t.Errorf("%s is a %q item; it must be a plain link or it stops working without JavaScript", it.Href, it.Kind)
196 }
197 if !it.Download {
198 t.Errorf("%s is missing the download attribute", it.Href)
199 }
200 }
201 for href, found := range want {
202 if !found {
203 t.Errorf("the File menu does not offer %s", href)
204 }
205 }
206}
207
208// Every label the bar renders came out of the catalog, not out of
209// menu.go as a literal. AGENTS.md: twelve base locales, and a
210// hardcoded English string is a bug.
211//
212// It works by rendering the view through a request with no locale
213// middleware, where rastrillo.T returns its key verbatim (documented
214// behaviour, and what a direct App.Handler() call in a test already
215// sees). So every label should come back looking like a key. A literal
216// would come back looking like English.
217func TestEveryMenuLabelIsACatalogKey(t *testing.T) {
218 v := newMenuViewForTest()
219
220 check := func(where, s string) {
221 if s == "" {
222 return
223 }
224 if !strings.HasPrefix(s, "docs.") {
225 t.Errorf("%s is the literal %q rather than a catalog key", where, s)
226 }
227 }
228
229 check("the menu bar's label", v.Label)
230 for _, m := range v.Menus {
231 check("menu "+m.ID+"'s title", m.Title)
232 for _, it := range m.Items {
233 if it.Kind == menuDivider {
234 continue
235 }
236 check("menu "+m.ID+"'s item", it.Label)
237 }
238 }
239}
240
241// Every catalog key the menu resolves is actually in the catalog.
242//
243// The test above proves the labels are keys; it cannot prove they are
244// keys that resolve, because an unresolved key returns itself and looks
245// exactly like a resolved one in that setting. A typo in a key is
246// therefore invisible to it — and visible to a person as raw
247// "docs.editor.menu.file.new" in their menu bar.
248func TestEveryMenuKeyIsInTheCatalog(t *testing.T) {
249 v := newMenuViewForTest()
250
251 check := func(key string) {
252 if key == "" {
253 return
254 }
255 if _, ok := Catalog[key]; !ok {
256 t.Errorf("the menu resolves %q, which is not in Catalog", key)
257 }
258 }
259
260 check(v.Label)
261 for _, m := range v.Menus {
262 check(m.Title)
263 for _, it := range m.Items {
264 if it.Kind == menuDivider {
265 continue
266 }
267 check(it.Label)
268 }
269 }
270}
271
272// File → Delete… is offered to the owner alone, as File's last item, set
273// apart by a divider, and as a plain link to the confirmation page. The
274// route refuses anyone else (TestTeamOwnerDeletesOnlyHerOwnDocument);
275// this is the half that keeps the menu from offering it to them.
276func TestFileMenuOffersDeleteToTheOwnerOnly(t *testing.T) {
277 for _, owner := range []bool{true, false} {
278 v := newMenuView(httptest.NewRequest("GET", "/d/abc123", nil), "abc123", true, owner)
279 var file []menuItem
280 for _, m := range v.Menus {
281 if m.ID == "file" {
282 file = m.Items
283 }
284 }
285 if len(file) < 2 {
286 t.Fatalf("owner=%v: File menu has %d items", owner, len(file))
287 }
288 last, before := file[len(file)-1], file[len(file)-2]
289 gotDelete := last.Kind == menuLink && last.Href == "/d/abc123/delete"
290 if gotDelete != owner {
291 t.Errorf("owner=%v: File ends with %+v; Delete… offered = %v", owner, last, gotDelete)
292 }
293 if owner && before.Kind != menuDivider {
294 t.Errorf("Delete… is not set apart: the item before it is %+v", before)
295 }
296 }
297}
298