first commit
This commit is contained in:
commit
8c4dcae475
89 changed files with 24623 additions and 0 deletions
277
tests/invariants_test.go
Normal file
277
tests/invariants_test.go
Normal file
|
|
@ -0,0 +1,277 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"html"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The archive's central promise: a number on a link is the number the click
|
||||
// returns. Facet counts, refine boxes and histogram bars all assert it, and
|
||||
// every one of them is computed by different code from the page it opens.
|
||||
// These tests walk the real pages and follow their own links.
|
||||
|
||||
var (
|
||||
reFacetLink = regexp.MustCompile(`<a class="facet-link" href="([^"]+)">.*?<span class="facet-count">(\d+)</span></a>`)
|
||||
reBarLink = regexp.MustCompile(`<a class="hist-col" href="([^"]+)"[^>]*>\s*<span class="hist-count">(\d+)</span>`)
|
||||
reTotal = regexp.MustCompile(`([\d,]+)\s+results?`)
|
||||
reListTotal = regexp.MustCompile(`([\d,]+)\s+documents?`)
|
||||
)
|
||||
|
||||
// resultTotal reads the count a /search or a list page reports.
|
||||
func resultTotal(t *testing.T, path string, cookies ...*http.Cookie) int {
|
||||
t.Helper()
|
||||
body := get(t, path, cookies...).Body.String()
|
||||
re := reTotal
|
||||
if !strings.HasPrefix(path, "/search") {
|
||||
re = reListTotal
|
||||
}
|
||||
m := re.FindStringSubmatch(body)
|
||||
if m == nil {
|
||||
t.Fatalf("%s: no result count in the page", path)
|
||||
}
|
||||
n, err := strconv.Atoi(strings.ReplaceAll(m[1], ",", ""))
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", path, err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// followLinks checks every (link, count) pair a page prints, as the reader
|
||||
// with these cookies sees it.
|
||||
func followLinks(t *testing.T, page string, re *regexp.Regexp, what string, cookies ...*http.Cookie) int {
|
||||
t.Helper()
|
||||
body := get(t, page, cookies...).Body.String()
|
||||
pairs := re.FindAllStringSubmatch(body, -1)
|
||||
for _, p := range pairs {
|
||||
link := html.UnescapeString(p[1])
|
||||
shown, _ := strconv.Atoi(p[2])
|
||||
if got := resultTotal(t, link, cookies...); got != shown {
|
||||
t.Errorf("%s: %s says %d, its link returns %d\n %s", page, what, shown, got, link)
|
||||
}
|
||||
}
|
||||
return len(pairs)
|
||||
}
|
||||
|
||||
// A category page lists exactly as many documents as its facet counts.
|
||||
func TestInvariantFacetCountsMatchPages(t *testing.T) {
|
||||
buildFixture(t)
|
||||
checked := 0
|
||||
for ci := range categories {
|
||||
c := &categories[ci]
|
||||
for val, want := range c.Facet(corpus()) {
|
||||
if got := len(corpus().categoryMatches(c.Slug, val)); got != want {
|
||||
t.Errorf("%s=%q: facet says %d, the page lists %d", c.Slug, val, want, got)
|
||||
}
|
||||
checked++
|
||||
}
|
||||
}
|
||||
if checked == 0 {
|
||||
t.Fatal("no facet values in the fixture")
|
||||
}
|
||||
}
|
||||
|
||||
// Refine links and histogram bars open searches that must return what they
|
||||
// promise - the two places where a count and its click are computed by
|
||||
// different code paths.
|
||||
func TestInvariantRefineAndBarCounts(t *testing.T) {
|
||||
pages := []string{
|
||||
"/browse/agency/NSA",
|
||||
"/browse/program/PRISM",
|
||||
"/browse/classification/TOP%20SECRET",
|
||||
"/search?q=prism&do_search=1",
|
||||
"/search?do_search=1&fqv=NSA&fqf=CR&fqc=and",
|
||||
}
|
||||
facets, bars := 0, 0
|
||||
for _, p := range pages {
|
||||
facets += followLinks(t, p, reFacetLink, "refine link")
|
||||
bars += followLinks(t, p, reBarLink, "histogram bar")
|
||||
}
|
||||
if facets == 0 || bars == 0 {
|
||||
t.Fatalf("nothing exercised: %d refine links, %d bars", facets, bars)
|
||||
}
|
||||
t.Logf("checked %d refine links and %d histogram bars", facets, bars)
|
||||
}
|
||||
|
||||
// The home page is scoped to the reported collection while the category
|
||||
// pages follow the reader's scope, so its bars and lists link to those
|
||||
// pages with the collection named. Every one of them has to be the number
|
||||
// its click returns.
|
||||
func TestInvariantHomeCounts(t *testing.T) {
|
||||
body := get(t, "/").Body.String()
|
||||
pairs := regexp.MustCompile(`<a class="pill" href="([^"]+)">[^<]*<span class="pill-count">(\d+)</span>`).FindAllStringSubmatch(body, -1)
|
||||
lists := regexp.MustCompile(`<li><a href="(/browse/[^"]+)">[^<]*</a> <span class="muted">(\d+)</span>`).FindAllStringSubmatch(body, -1)
|
||||
if len(lists) < 5 {
|
||||
t.Fatalf("only %d counted list links on the home page - the stat lists should link to their browse pages", len(lists))
|
||||
}
|
||||
pairs = append(pairs, lists...)
|
||||
pairs = append(pairs, regexp.MustCompile(`<a class="hist-col" href="([^"]+)" title="(\d+) documents`).FindAllStringSubmatch(body, -1)...)
|
||||
if len(pairs) < 5 {
|
||||
t.Fatalf("only %d counted links on the home page", len(pairs))
|
||||
}
|
||||
for _, m := range pairs {
|
||||
link, shown := html.UnescapeString(m[1]), m[2]
|
||||
if got := resultTotal(t, link); got != mustAtoi(t, shown) {
|
||||
t.Errorf("home says %s, its link returns %d: %s", shown, got, link)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func mustAtoi(t *testing.T, s string) int {
|
||||
t.Helper()
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
t.Fatalf("%q: %v", s, err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// The type pills follow the same rule: the count is over the full match set,
|
||||
// and clicking one narrows to exactly that many.
|
||||
func TestInvariantTypePillCounts(t *testing.T) {
|
||||
body := get(t, "/browse").Body.String()
|
||||
re := regexp.MustCompile(`href="([^"]*type=[^"]*)"[^>]*>\s*([A-Z]+)\s*<span[^>]*>\(?(\d+)\)?</span>`)
|
||||
for _, m := range re.FindAllStringSubmatch(body, -1) {
|
||||
link := html.UnescapeString(m[1])
|
||||
shown, _ := strconv.Atoi(m[3])
|
||||
if got := resultTotal(t, link); got != shown {
|
||||
t.Errorf("type pill %s says %d, its link returns %d (%s)", m[2], shown, got, link)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Everything the sitemap advertises must exist: those URLs are built in Go,
|
||||
// away from the templates that normally escape ids.
|
||||
func TestInvariantAdvertisedURLsResolve(t *testing.T) {
|
||||
for _, src := range []string{"/sitemap.xml"} {
|
||||
body := get(t, src).Body.String()
|
||||
locs := regexp.MustCompile(`<loc>([^<]+)</loc>|<link[^>]+href="([^"]+)"`).FindAllStringSubmatch(body, -1)
|
||||
if len(locs) == 0 {
|
||||
t.Fatalf("%s advertises nothing", src)
|
||||
}
|
||||
seen := 0
|
||||
for _, m := range locs {
|
||||
raw := m[1]
|
||||
if raw == "" {
|
||||
raw = m[2]
|
||||
}
|
||||
u, err := url.Parse(html.UnescapeString(raw))
|
||||
if err != nil || u.Path == "" {
|
||||
continue
|
||||
}
|
||||
p := u.Path
|
||||
if u.RawQuery != "" {
|
||||
p += "?" + u.RawQuery
|
||||
}
|
||||
if w := get(t, p); w.Code != 200 {
|
||||
t.Errorf("%s advertises %s, which answers %d", src, p, w.Code)
|
||||
}
|
||||
seen++
|
||||
}
|
||||
t.Logf("%s: %d URLs resolve", src, seen)
|
||||
}
|
||||
}
|
||||
|
||||
// The API's pagination envelope has to describe the page it returned, at
|
||||
// every page size, and per_page=all is the export mode that skips snippets.
|
||||
func TestInvariantAPIPagination(t *testing.T) {
|
||||
type envelope struct {
|
||||
Total int `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PerPage int `json:"per_page"`
|
||||
TotalPages int `json:"total_pages"`
|
||||
Omitted bool `json:"snippets_omitted"`
|
||||
Docs []struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"docs"`
|
||||
Results []struct {
|
||||
ID string `json:"id"`
|
||||
Snippet string `json:"snippet"`
|
||||
} `json:"results"`
|
||||
}
|
||||
read := func(path string) envelope {
|
||||
var e envelope
|
||||
w := get(t, path)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("%s: status %d", path, w.Code)
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &e); err != nil {
|
||||
t.Fatalf("%s: %v", path, err)
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
for _, path := range []string{"/api/docs", "/api/docs?per_page=1", "/api/docs?per_page=all",
|
||||
"/api/search?q=prism", "/api/search?q=prism&per_page=1", "/api/search?q=prism&per_page=all"} {
|
||||
e := read(path)
|
||||
n := len(e.Docs) + len(e.Results)
|
||||
switch {
|
||||
case e.Page < 1:
|
||||
t.Errorf("%s: page %d", path, e.Page)
|
||||
case e.PerPage < 1:
|
||||
t.Errorf("%s: per_page %d", path, e.PerPage)
|
||||
case n > e.PerPage:
|
||||
t.Errorf("%s: returned %d rows for a page of %d", path, n, e.PerPage)
|
||||
case e.TotalPages < 1:
|
||||
t.Errorf("%s: total_pages %d", path, e.TotalPages)
|
||||
}
|
||||
if want := (e.Total + e.PerPage - 1) / e.PerPage; e.Total > 0 && e.TotalPages != want {
|
||||
t.Errorf("%s: total_pages %d, want %d for %d/%d", path, e.TotalPages, want, e.Total, e.PerPage)
|
||||
}
|
||||
}
|
||||
|
||||
// An out-of-range page clamps rather than 500s or returns junk.
|
||||
if e := read("/api/docs?p=9999"); e.Page != e.TotalPages {
|
||||
t.Errorf("p=9999 gave page %d of %d", e.Page, e.TotalPages)
|
||||
}
|
||||
// Export mode returns everything and says why there are no snippets.
|
||||
all := read("/api/search?q=prism&per_page=all")
|
||||
if !all.Omitted || len(all.Results) != all.Total {
|
||||
t.Errorf("per_page=all: omitted=%v, %d of %d results", all.Omitted, len(all.Results), all.Total)
|
||||
}
|
||||
for _, r := range all.Results {
|
||||
if r.Snippet != "" {
|
||||
t.Errorf("per_page=all returned a snippet for %s", r.ID)
|
||||
}
|
||||
}
|
||||
// A normal page does carry them.
|
||||
paged := read("/api/search?q=prism")
|
||||
if paged.Omitted {
|
||||
t.Error("a paged search should not omit snippets")
|
||||
}
|
||||
}
|
||||
|
||||
// Snippet work is bounded by maxSnippetDocs, not by the page size, and the
|
||||
// page says so when the two differ.
|
||||
func TestInvariantSnippetCap(t *testing.T) {
|
||||
buildFixture(t)
|
||||
doc := corpus().docIndex["prism-slides"]
|
||||
if doc == nil {
|
||||
t.Fatal("fixture document missing")
|
||||
}
|
||||
docs := make([]*Document, maxSnippetDocs+5)
|
||||
for i := range docs {
|
||||
docs[i] = doc
|
||||
}
|
||||
page, capped := decorateResults(docs, []string{"collection"}, []string{"TE"}, []string{"and"})
|
||||
if !capped {
|
||||
t.Error("a page past the budget should report itself capped")
|
||||
}
|
||||
withSnippets := 0
|
||||
for i := range page {
|
||||
if len(page[i].Snippets) > 0 {
|
||||
withSnippets++
|
||||
}
|
||||
if page[i].Title == "" {
|
||||
t.Fatalf("row %d lost its title highlighting", i)
|
||||
}
|
||||
}
|
||||
if withSnippets != maxSnippetDocs {
|
||||
t.Errorf("%d rows got snippets, want %d", withSnippets, maxSnippetDocs)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue