package e2e import ( "os/exec" "strings" "testing" ) // The sr-only summary describes the page to a screen reader, and reads as a spec anywhere else. func TestALinkPreviewSaysWhatTheThingIsNotWhatThePageIs(t *testing.T) { if _, err := exec.LookPath("git"); err != nil { t.Skip("git is not installed") } in := newInstance(t) john := in.account("john") work := seed(t, in, john, "john", "johnbot") write(t, work, ".barerepo/config", "[repo]\nvisibility = \"public\"\n"+ "description = \"a bot that answers\"\n") run(t, work, "git", "add", "-A") run(t, work, "git", "commit", "-qm", "describe the repository") run(t, work, "git", "push", "-q", in.url(john, "/john/johnbot"), "master") for _, c := range []struct { what, path string want []string }{ {"the repository log", "/john/johnbot", []string{"john/johnbot", "a bot that answers"}}, {"the profile", "/john", []string{"john", "repositor"}}, } { code, _, body := get(t, in.http.URL+c.path) if code != 200 { t.Fatalf("%s answered %d", c.what, code) } og := metaContent(body, "og:description") if og == "" { t.Errorf("%s has no og:description", c.what) continue } if strings.Contains(og, "This is the landing page") || strings.Contains(og, "listing repositories sorted by last push") { t.Errorf("%s shares the screen reader summary: %q", c.what, og) } for _, w := range c.want { if !strings.Contains(og, w) { t.Errorf("%s og:description %q does not say %q", c.what, og, w) } } // The screen reader sentence must still be on the page, which chapter 25 requires. if !strings.Contains(body, "sr-only") { t.Errorf("%s lost its screen reader summary", c.what) } } } // metaContent pulls one meta tag's content out of a page. func metaContent(body, prop string) string { i := strings.Index(body, `property="`+prop+`"`) if i < 0 { return "" } rest := body[i:] j := strings.Index(rest, `content="`) if j < 0 { return "" } rest = rest[j+len(`content="`):] k := strings.Index(rest, `"`) if k < 0 { return "" } return rest[:k] }