package busy import ( "crypto/sha256" "encoding/hex" "encoding/json" "net/http" "net/http/httptest" "os" "strings" "testing" ) // The three files are copies, and a copy that drifts is a fork nobody // chose: each must be the bytes provenance.json records. func TestFilesAreTheRecordedCopies(t *testing.T) { raw, err := os.ReadFile("provenance.json") if err != nil { t.Fatal(err) } var record struct { Files map[string]struct { SHA256 string `json:"sha256"` Bytes int `json:"bytes"` } `json:"files"` } if err := json.Unmarshal(raw, &record); err != nil { t.Fatal(err) } for name, body := range map[string][]byte{"rastrillo.js": ShimJS(), "busy.js": JS(), "busy.css": CSS()} { want, ok := record.Files[name] sum := sha256.Sum256(body) if !ok || want.SHA256 != hex.EncodeToString(sum[:]) || want.Bytes != len(body) { t.Errorf("%s is not the copy provenance.json records", name) } } } // The point of the package: the shim it hands out has no busy rule left in // it (or it would fight busy.js), and busy.js has the minimum. func TestTheShimLeftTheRuleToBusyJS(t *testing.T) { if strings.Contains(string(ShimJS()), "busySubmit") || strings.Contains(string(ShimJS()), `"submit"`) { t.Error("the shim still carries a busy rule; two on one page fight over every submit") } for _, want := range []string{"HOLD_MS = 650", "busySubmit", "pageshow", "pagehide"} { if !strings.Contains(string(JS()), want) { t.Errorf("busy.js lacks %q", want) } } if !strings.Contains(string(CSS()), `:not([data-idle-label]):has(> [rst-spin])`) { t.Error("busy.css lacks the spinner-in-place rule") } } func TestHandlerServesOnlyItsTwoFiles(t *testing.T) { h := Handler() for path, want := range map[string]int{ Prefix + "busy.js": 200, Prefix + "busy.css": 200, Prefix + "rastrillo.js": 404, Prefix + "../busy.go": 404, Prefix: 404, "/static/busy.js": 404, "/static/estilo/busy/v1/busy.js": 404, // the version is the digest, not a counter } { rec := httptest.NewRecorder() h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil)) if rec.Code != want { t.Errorf("GET %s = %d, want %d", path, rec.Code, want) } } rec := httptest.NewRecorder() h.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, Prefix+"busy.js", nil)) if rec.Code != http.StatusMethodNotAllowed { t.Errorf("POST = %d", rec.Code) } }