oficina / estilo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package busy
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "encoding/json"
7 "net/http"
8 "net/http/httptest"
9 "os"
10 "regexp"
11 "strings"
12 "testing"
13)
14
15// The three files are copies, and a copy that drifts is a fork nobody
16// chose: each must be the bytes provenance.json records.
17func TestFilesAreTheRecordedCopies(t *testing.T) {
18 raw, err := os.ReadFile("provenance.json")
19 if err != nil {
20 t.Fatal(err)
21 }
22 var record struct {
23 Files map[string]struct {
24 SHA256 string `json:"sha256"`
25 Bytes int `json:"bytes"`
26 } `json:"files"`
27 }
28 if err := json.Unmarshal(raw, &record); err != nil {
29 t.Fatal(err)
30 }
31 for name, body := range map[string][]byte{"rastrillo.js": ShimJS(), "busy.js": JS(), "busy.css": CSS()} {
32 want, ok := record.Files[name]
33 sum := sha256.Sum256(body)
34 if !ok || want.SHA256 != hex.EncodeToString(sum[:]) || want.Bytes != len(body) {
35 t.Errorf("%s is not the copy provenance.json records", name)
36 }
37 }
38}
39
40// The point of the package: the shim it hands out has no busy rule left in
41// it (or it would fight busy.js), and busy.js has the minimum.
42func TestTheShimLeftTheRuleToBusyJS(t *testing.T) {
43 shim := string(ShimJS())
44 for _, rule := range []string{"busySubmit", "aria-busy", "rst-spin", "HOLD_MS", "data-busy"} {
45 if strings.Contains(shim, rule) {
46 t.Errorf("the shim still carries a busy rule (%q); two on one page fight over every submit", rule)
47 }
48 }
49 // It hears submit exactly once, for the colour-scheme menu's forms
50 // ([rst-scheme]), which it applies in place; any other form it leaves
51 // to the browser and busy.js. (The shim at rastrillo 0547879, which
52 // Docs pins, added this; the switch drag came with it.)
53 if n := submitHooks(shim); n != 1 {
54 t.Fatalf("the shim listens for submit %d times; only the scheme switch should", n)
55 }
56 scheme := shim[strings.Index(shim, `addEventListener("submit"`):]
57 if !strings.Contains(scheme[:strings.Index(scheme, "e.preventDefault()")], `f.closest("[rst-scheme],.rst-scheme")`) {
58 t.Error("the shim's submit listener prevents a submit outside the scheme menu")
59 }
60 if !strings.Contains(shim, "[rst-switch-track]") {
61 t.Error("the shim lacks the rst-switch drag Docs' share switches need")
62 }
63 // Both timings: a POST's 650ms hold and a GET's 500ms wait before it
64 // shows anything. A copy with only the first holds every search.
65 for _, want := range []string{"HOLD_MS = 650", "SHOW_AFTER_MS = 500", "busySubmit", "pageshow", "pagehide"} {
66 if !strings.Contains(string(JS()), want) {
67 t.Errorf("busy.js lacks %q", want)
68 }
69 }
70 if !strings.Contains(string(CSS()), `:not([data-idle-label]):has(> [rst-spin])`) {
71 t.Error("busy.css lacks the spinner-in-place rule")
72 }
73}
74
75// submitHooks counts the ways a script can hear a submit:
76// addEventListener with the type in any of JavaScript's three quotes, and
77// an onsubmit handler, assigned or as an attribute.
78func submitHooks(js string) int {
79 return len(regexp.MustCompile("addEventListener\\(\\s*[\"'`]submit[\"'`]|\\bonsubmit\\b").FindAllString(js, -1))
80}
81
82// The guard above must see a second submit listener however it is
83// spelled, or a re-vendored shim with a busy rule written another way
84// would pass it (Fable, estilo #34: it caught only "submit" in double
85// quotes).
86func TestTheSubmitGuardSeesEverySpelling(t *testing.T) {
87 shim := string(ShimJS())
88 for _, extra := range []string{
89 `document.addEventListener("submit", busy, true)`,
90 `document.addEventListener('submit', busy, true)`,
91 "document.addEventListener(`submit`, busy, true)",
92 `document.addEventListener( "submit", busy)`,
93 `form.onsubmit = busy`,
94 `document.body.setAttribute("onsubmit", "busy()")`,
95 } {
96 if n := submitHooks(shim + "\n" + extra + "\n"); n != 2 {
97 t.Errorf("the guard counts %d submit listeners with %s added; want 2", n, extra)
98 }
99 }
100}
101
102func TestHandlerServesOnlyItsTwoFiles(t *testing.T) {
103 h := Handler()
104 for path, want := range map[string]int{
105 Prefix + "busy.js": 200,
106 Prefix + "busy.css": 200,
107 Prefix + "rastrillo.js": 404,
108 Prefix + "../busy.go": 404,
109 Prefix: 404,
110 "/static/busy.js": 404,
111 "/static/estilo/busy/v1/busy.js": 404, // the version is the digest, not a counter
112 } {
113 rec := httptest.NewRecorder()
114 h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil))
115 if rec.Code != want {
116 t.Errorf("GET %s = %d, want %d", path, rec.Code, want)
117 }
118 }
119 rec := httptest.NewRecorder()
120 h.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, Prefix+"busy.js", nil))
121 if rec.Code != http.StatusMethodNotAllowed {
122 t.Errorf("POST = %d", rec.Code)
123 }
124}
125