package main import ( "encoding/json" "testing" ) // The "published in the same report" box is capped at pubTogetherCap, and // the overflow links point at the article pages: one per article that holds // other documents, in publication order, so each link's number is the // number its page lists. func TestDocArticles(t *testing.T) { saved := corpus().articleByLink defer func() { corpus().articleByLink = saved }() small := &Article{Key: "aaaaaaaaaaaa", DocIDs: []string{"a", "b"}} big := &Article{Key: "bbbbbbbbbbbb", DocIDs: []string{"a", "b", "c", "d"}} alone := &Article{Key: "cccccccccccc", DocIDs: []string{"a"}} corpus().articleByLink = map[string]*Article{ "https://small.example/story": small, "https://big.example/appendix": big, "https://alone.example/x": alone, } var doc Document if err := json.Unmarshal([]byte(`{"publication":[ {"link": "https://small.example/story"}, {"link": "https://alone.example/x"}, {"link": "https://big.example/appendix"}, {"link": "https://small.example/story"}]}`), &doc); err != nil { t.Fatal(err) } got := corpus().docArticles(&doc) if len(got) != 2 || got[0] != small || got[1] != big { t.Fatalf("docArticles = %+v, want the two articles with other documents, once each, in order", got) } var orphan Document if err := json.Unmarshal([]byte(`{"publication":[{"link": "https://nowhere.example/x"}]}`), &orphan); err != nil { t.Fatal(err) } if a := corpus().docArticles(&orphan); a != nil { t.Error("a document whose links have no article must produce no overflow link") } }