package classes import ( "encoding/json" "fmt" "strings" "testing" "git.golem15.com/golem15/sm-sitemap-plugin/models" ) // TestHTMLGroupOrder pins UI-SPEC L3's order of the named groups. func TestHTMLGroupOrder(t *testing.T) { if got := strings.Join(htmlGroupOrder, " "); got != "pages offers services categories posts tags authors" { t.Fatalf("htmlGroupOrder = %s", got) } } // TestToEntries pins the links of rows: absolute on the site, the path as // the title when there is none, the first parent kept, and a row that // cannot be made absolute skipped. func TestToEntries(t *testing.T) { refs, _ := json.Marshal([]models.Ref{{Type: "cat", ID: "1"}, {Type: "cat", ID: "9"}}) got := toEntries([]htmlRow{ {TypeKey: "cat", RecordID: "2", URL: "/kategoria/b", Title: " Bee ", ParentRefs: string(refs)}, {TypeKey: "cat", RecordID: "3", URL: "/kategoria/c"}, {TypeKey: "cat", RecordID: "4", URL: "javascript:alert(1)", Title: "x"}, {TypeKey: "cat", RecordID: "5", URL: "https://example.test/abs", Title: "Abs", ParentRefs: "[]"}, }, "https://example.test") if len(got) != 3 { t.Fatalf("entries = %+v, want 3 (the javascript: row skipped)", got) } if got[0].Title != "Bee" || got[0].URL != "https://example.test/kategoria/b" || got[0].parent == nil || *got[0].parent != (models.Ref{Type: "cat", ID: "1"}) || got[0].node != (models.Ref{Type: "cat", ID: "2"}) { t.Fatalf("entry 0 = %+v", got[0]) } if got[1].Title != "/kategoria/c" || got[1].parent != nil || got[2].parent != nil || got[2].URL != "https://example.test/abs" { t.Fatalf("entries 1 and 2 = %+v %+v", got[1], got[2]) } } func treeEntry(id, title string, parent string) entry { e := entry{HTMLEntry: HTMLEntry{Title: title, URL: "/" + id}, node: models.Ref{Type: "cat", ID: id}} if parent != "" { e.parent = &models.Ref{Type: "cat", ID: parent} } return e } // TestTreeOrder pins the tree order: depth first with siblings in the // input order (alphabetical from the query), the parent indices into the // flat result, an entry whose parent is not in the group a root, and a // cycle broken into a root. func TestTreeOrder(t *testing.T) { in := []entry{ treeEntry("1", "A", ""), treeEntry("2", "A1", "1"), treeEntry("3", "B", ""), treeEntry("4", "A2", "1"), treeEntry("5", "A1a", "2"), treeEntry("6", "Orphan", "99"), treeEntry("7", "Self", "7"), treeEntry("8", "Cy1", "9"), treeEntry("9", "Cy2", "8"), } flat, parents := treeOrder(in) var titles []string for _, e := range flat { titles = append(titles, e.Title) } if got := strings.Join(titles, " "); got != "A A1 A1a A2 B Orphan Self Cy1 Cy2" { t.Fatalf("order = %s", got) } if fmt.Sprint(parents) != "[-1 0 1 0 -1 -1 -1 -1 7]" { t.Fatalf("parents = %v", parents) } if len(flat) != len(in) { t.Fatalf("%d entries out of %d", len(flat), len(in)) } } // TestNestPaging pins the nesting of one page of the tree: a child whose // parent is on the same page nests under it (children in order), a child // whose parent is on an earlier page is a top-level link of its page, and // pages cover every entry once. func TestNestPaging(t *testing.T) { flat, parents := treeOrder([]entry{ treeEntry("1", "A", ""), treeEntry("2", "A1", "1"), treeEntry("3", "A2", "1"), treeEntry("4", "A2x", "3"), treeEntry("5", "B", ""), }) whole := nest(flat, parents, 0, len(flat)) if len(whole) != 2 || whole[0].Title != "A" || len(whole[0].Children) != 2 || whole[0].Children[1].Title != "A2" || len(whole[0].Children[1].Children) != 1 || whole[0].Children[1].Children[0].Title != "A2x" || whole[1].Title != "B" || whole[1].Children != nil { t.Fatalf("whole tree = %+v", whole) } first := nest(flat, parents, 0, 2) second := nest(flat, parents, 2, 4) third := nest(flat, parents, 4, 5) if len(first) != 1 || first[0].Title != "A" || len(first[0].Children) != 1 { t.Fatalf("page 1 = %+v", first) } if len(second) != 1 || second[0].Title != "A2" || len(second[0].Children) != 1 || second[0].Children[0].Title != "A2x" { t.Fatalf("page 2 = %+v, want A2 as a top-level link with its child", second) } if len(third) != 1 || third[0].Title != "B" { t.Fatalf("page 3 = %+v", third) } if got := nest(flat, parents, 5, 5); got == nil || len(got) != 0 { t.Fatalf("an empty page = %v, want []", got) } } // TestPickTitle pins the heading per locale: the locale's, else the // default locale's, else "". func TestPickTitle(t *testing.T) { titles := map[string]string{"en": "Posts", "pl": " ", "de": "Beiträge"} for _, tc := range []struct{ locale, def, want string }{ {"de", "en", "Beiträge"}, {"pl", "en", "Posts"}, {"fr", "en", "Posts"}, {"fr", "it", ""}, {"en", "", "Posts"}, } { if got := pickTitle(titles, tc.locale, tc.def); got != tc.want { t.Errorf("pickTitle(%s, %s) = %q, want %q", tc.locale, tc.def, got, tc.want) } } if pickTitle(nil, "en", "en") != "" { t.Fatal("no titles must give \"\"") } }