| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/estilo.git/savestatus" |
| 12 | ) |
| 13 | |
| 14 | func TestShowLoadsSharedSaveStatusAssets(t *testing.T) { |
| 15 | app, err := NewForTest(t) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | owner := app.TestPerson(t, "status@example.test") |
| 20 | doc, err := app.Store().Create(context.Background(), owner, "Status fixture") |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | rec := serve(app, app.TestRequest(t, "status@example.test", http.MethodGet, "/d/"+doc.Sqid, nil)) |
| 25 | if rec.Code != http.StatusOK { |
| 26 | t.Fatalf("show: %d", rec.Code) |
| 27 | } |
| 28 | body := rec.Body.String() |
| 29 | if !strings.Contains(body, `role="status" aria-live="polite" aria-atomic="true"`) { |
| 30 | t.Fatal("persistent live region missing") |
| 31 | } |
| 32 | if strings.Contains(body, "docs-save-status") { |
| 33 | t.Fatal("app-local save status remains") |
| 34 | } |
| 35 | if !strings.Contains(body, `data-text-retrying="docs.editor.status.retrying"`) || !strings.Contains(body, `data-text-failed="docs.editor.status.failed"`) { |
| 36 | t.Fatal("existing status translations missing") |
| 37 | } |
| 38 | for _, item := range []struct { |
| 39 | pattern string |
| 40 | want []byte |
| 41 | mime string |
| 42 | }{ |
| 43 | {`data-save-status-module="([^"]+)"`, savestatus.JS(), "javascript"}, |
| 44 | {`href="([^"]*estilo-save-status[^\"]*\.css)"`, savestatus.CSS(), "text/css"}, |
| 45 | } { |
| 46 | match := regexp.MustCompile(item.pattern).FindStringSubmatch(body) |
| 47 | if len(match) != 2 { |
| 48 | t.Fatalf("asset missing: %s", item.pattern) |
| 49 | } |
| 50 | if !strings.HasPrefix(match[1], "/static/") { |
| 51 | t.Fatalf("asset is not same-origin: %s", match[1]) |
| 52 | } |
| 53 | asset := serve(app, httptest.NewRequest(http.MethodGet, match[1], nil)) |
| 54 | if asset.Code != http.StatusOK || asset.Body.String() != string(item.want) { |
| 55 | t.Fatalf("shared asset not served: %s status%d", match[1], asset.Code) |
| 56 | } |
| 57 | if !strings.Contains(asset.Header().Get("Content-Type"), item.mime) { |
| 58 | t.Fatalf("wrong MIME: %s", asset.Header().Get("Content-Type")) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |