package colophon import ( "bytes" "html/template" "net/url" "regexp" "strings" "testing" "amadan.net/oficina/estilo.git/credits" "amadan.net/oficina/estilo.git/internal/tokencheck" ) // The catalog is the one place every app's Colophon reads a project's // description from, so every entry in it must be whole: a blurb, who made // it, the licence it is under, and links to its home and its source. func TestCatalogEntriesAreComplete(t *testing.T) { projects := Catalog() if len(projects) < 50 { t.Fatalf("the catalog has %d projects; the checks below would pass nearly vacuously", len(projects)) } if err := Validate(projects); err != nil { t.Fatal(err) } } func TestValidateRefusesAnIncompleteEntry(t *testing.T) { good := Project{ID: "yjs", Name: "Yjs", Kind: Library, Blurb: "A CRDT for shared editing. It merges edits without a server deciding the order.", Author: "Kevin Jahns", Licence: Licence{SPDX: "MIT", Name: "MIT License"}, Home: "https://yjs.dev", Source: "https://github.com/yjs/yjs", Packages: []Ref{{Ecosystem: NPM, Name: "yjs"}}, Uses: map[string]string{"docs": "The editor keeps the document in it."}} if err := Validate([]Project{good}); err != nil { t.Fatalf("a complete entry was refused: %v", err) } for name, spoil := range map[string]func(*Project){ "no blurb": func(p *Project) { p.Blurb = "" }, "blurb in lower case": func(p *Project) { p.Blurb = "a CRDT for shared editing." }, "three sentences": func(p *Project) { p.Blurb = "One thing. Another thing. A third thing." }, "no author": func(p *Project) { p.Author = " " }, "no licence name": func(p *Project) { p.Licence.Name = "" }, "no licence identifier": func(p *Project) { p.Licence.SPDX = "" }, "no home": func(p *Project) { p.Home = "" }, "plain-http source": func(p *Project) { p.Source = "http://github.com/yjs/yjs" }, "script url": func(p *Project) { p.Home = "javascript:alert(1)" }, "no packages": func(p *Project) { p.Packages = nil }, "unknown kind": func(p *Project) { p.Kind = "gadget" }, "unknown ecosystem": func(p *Project) { p.Packages = []Ref{{Ecosystem: "maven", Name: "x"}} }, "bad id": func(p *Project) { p.ID = "Yjs Project" }, "empty usage note": func(p *Project) { p.Uses = map[string]string{"docs": ""} }, "usage not a sentence": func(p *Project) { p.Uses = map[string]string{"docs": "the editor"} }, } { p := good p.Packages = append([]Ref(nil), good.Packages...) p.Uses = map[string]string{"docs": good.Uses["docs"]} spoil(&p) if err := Validate([]Project{p}); err == nil { t.Errorf("%s: accepted", name) } } twin := good twin.ID = "yjs-again" if err := Validate([]Project{good, twin}); err == nil || !strings.Contains(err.Error(), "npm:yjs") { t.Errorf("one package claimed by two projects was accepted: %v", err) } same := good if err := Validate([]Project{good, same}); err == nil { t.Error("a duplicate id was accepted") } } func notice(text string) []credits.Notice { return []credits.Notice{{Name: "LICENSE", Text: text}} } func testCatalog() []Project { return []Project{ {ID: "yjs", Name: "Yjs", Kind: Library, Blurb: "A CRDT.", Author: "Kevin Jahns", Licence: Licence{"MIT", "MIT License"}, Home: "https://yjs.dev", Source: "https://github.com/yjs/yjs", Packages: []Ref{{NPM, "yjs"}}, Uses: map[string]string{"docs": "Holds the document."}}, {ID: "prosemirror", Name: "ProseMirror", Kind: Library, Blurb: "An editor toolkit.", Author: "Marijn Haverbeke", Licence: Licence{"MIT", "MIT License"}, Home: "https://prosemirror.net", Source: "https://github.com/ProseMirror", Packages: []Ref{{NPM, "prosemirror-model"}, {NPM, "prosemirror-view"}}, Uses: map[string]string{"docs": "Draws the editor."}}, {ID: "chi", Name: "chi", Kind: Library, Blurb: "A router.", Author: "Peter Kieltyka", Licence: Licence{"MIT", "MIT License"}, Home: "https://go-chi.io", Source: "https://github.com/go-chi/chi", Packages: []Ref{{Go, "github.com/go-chi/chi/v5"}}, Uses: map[string]string{"docs": "Routes requests."}}, {ID: "carlito", Name: "Carlito", Kind: Typeface, Blurb: "A sans serif.", Author: "tyPoland", Licence: Licence{"OFL-1.1", "SIL Open Font License 1.1"}, Home: "https://github.com/googlefonts/carlito", Source: "https://github.com/googlefonts/carlito", Packages: []Ref{{Font, "Carlito"}}, Uses: map[string]string{"docs": "The default font."}}, {ID: "esbuild", Name: "esbuild", Kind: Tool, Blurb: "A bundler.", Author: "Evan Wallace", Licence: Licence{"MIT", "MIT License"}, Home: "https://esbuild.github.io", Source: "https://github.com/evanw/esbuild", Packages: []Ref{{NPM, "esbuild"}}, Uses: map[string]string{"docs": "Builds the editor."}}, {ID: "wasmi", Name: "wasmi", Kind: Library, Blurb: "A wasm interpreter.", Author: "Parity", Licence: Licence{"MIT OR Apache-2.0", "MIT or Apache 2.0"}, Home: "https://github.com/wasmi-labs/wasmi", Source: "https://github.com/wasmi-labs/wasmi", Packages: []Ref{{Cargo, "wasmi"}, {Cargo, "wasmi_core"}}, Uses: map[string]string{"docs": "Tests the core.", "docs-native": "Runs the core."}}, {ID: "signin", Name: "signin", Kind: Library, Blurb: "Sign-in.", Author: "Keymail", Licence: Licence{NoAssertion, "No licence published"}, Home: "https://pkg.go.dev/github.com/keymaildev/signin", Source: "https://github.com/keymaildev/signin", Packages: []Ref{{Go, "github.com/keymaildev/signin"}}, Uses: map[string]string{"docs": "Signs people in."}}, } } func shippedSet() []Package { return []Package{ {Ecosystem: NPM, Name: "yjs", Version: "13.6.27", Notices: notice("yjs")}, {Ecosystem: NPM, Name: "prosemirror-view", Version: "1.42.3", Notices: notice("pmv")}, {Ecosystem: NPM, Name: "prosemirror-model", Version: "1.25.11", Notices: notice("pmm")}, {Ecosystem: Go, Name: "github.com/go-chi/chi/v5", Version: "v5.3.2", Notices: notice("chi")}, {Ecosystem: Font, Name: "Carlito", Notices: notice("OFL")}, {Ecosystem: NPM, Name: "esbuild", Version: "0.28.2", Tool: true, Notices: notice("esbuild")}, {Ecosystem: Cargo, Name: "wasmi", Version: "0.32.3", Tool: true, Notices: notice("wasmi")}, {Ecosystem: Go, Name: "github.com/keymaildev/signin", Version: "v0.1.1"}, } } func TestBuildGroupsEveryShippedPackageUnderItsProject(t *testing.T) { c, err := build(testCatalog(), "docs", shippedSet()) if err != nil { t.Fatal(err) } var order []string for _, s := range c.Sections { var names []string for _, e := range s.Entries { names = append(names, e.ID) } order = append(order, s.ID+"="+strings.Join(names, ",")) } // Typefaces first, then libraries by runtime, then tools. A library // that this app only builds or tests with (wasmi, a dev-dependency of // the web core) is a tool here, whatever it is elsewhere. if got, want := strings.Join(order, " "), "typefaces=carlito go=chi,signin javascript=prosemirror,yjs tools=esbuild,wasmi"; got != want { t.Fatalf("sections = %s, want %s", got, want) } pm, ok := c.Entry("prosemirror") if !ok || len(pm.Packages) != 2 || pm.Packages[0].Name != "prosemirror-model" || pm.Usage != "Draws the editor." || pm.Blurb != "An editor toolkit." { t.Fatalf("ProseMirror's two packages or its words were lost: %+v", pm) } if got := pm.VersionText(); got != "prosemirror-model 1.25.11, prosemirror-view 1.42.3" { t.Errorf("VersionText = %q", got) } if y, _ := c.Entry("yjs"); y.VersionText() != "13.6.27" { t.Errorf("a one-package project's version is %q", y.VersionText()) } if n := c.Count(); n["typefaces"] != 1 || n["javascript"] != 2 || n["total"] != 7 { t.Errorf("Count = %v", n) } } // The test every app's Colophon leans on: the shipped set and the Colophon // may not differ. Each way they can is refused, by name. func TestBuildRefusesMissingStaleAndIncompleteEntries(t *testing.T) { cases := map[string]struct { app string shipped func([]Package) []Package want string }{ "a shipped package with no catalog entry": {"docs", func(p []Package) []Package { return append(p, Package{Ecosystem: Cargo, Name: "yrs", Version: "0.23.5", Notices: notice("yrs")}) }, "cargo:yrs has no catalog entry"}, "a shipped project with no usage note for the app": {"docs-native", func(p []Package) []Package { return []Package{p[0]} }, "yjs has no usage note for docs-native"}, "a usage note for a project the app no longer ships": {"docs", func(p []Package) []Package { return p[1:] }, "yjs has a usage note for docs but docs does not ship it"}, "a package without its licence text": {"docs", func(p []Package) []Package { p[0].Notices = nil return p }, "npm:yjs 13.6.27 carries no licence text"}, "an unnamed package": {"docs", func(p []Package) []Package { return append(p, Package{Ecosystem: NPM}) }, "a package without an ecosystem or name"}, } for name, tc := range cases { t.Run(name, func(t *testing.T) { _, err := build(testCatalog(), tc.app, tc.shipped(shippedSet())) if err == nil || !strings.Contains(err.Error(), tc.want) { t.Fatalf("err = %v, want it to say %q", err, tc.want) } }) } } func TestBuildMergesAPackageSeenTwice(t *testing.T) { p := shippedSet() // The same crate from two builds: once shipped, once only as a tool. p = append(p, Package{Ecosystem: Cargo, Name: "wasmi", Version: "0.32.3", Notices: notice("wasmi")}) c, err := build(testCatalog(), "docs", p) if err != nil { t.Fatal(err) } w, _ := c.Entry("wasmi") if len(w.Packages) != 1 || w.Packages[0].Tool { t.Fatalf("wasmi = %+v, want one shipped package", w.Packages) } if c.sectionOf("wasmi") != "rust" { t.Errorf("a shipped wasmi is in %q", c.sectionOf("wasmi")) } } func parse(t *testing.T) *template.Template { t.Helper() return template.Must(template.New("").ParseFS(Templates(), "templates/*.html")) } func page(t *testing.T, c Colophon, labels Labels) string { t.Helper() var out bytes.Buffer if err := parse(t).ExecuteTemplate(&out, "estilo/colophon", Page{Title: "Colophon", Intro: []string{"What Docs is made with."}, Labels: labels, LicenceBase: "/help/colophon/", Colophon: c}); err != nil { t.Fatal(err) } return out.String() } func TestPageNamesEachProjectAndLinksItsLicenceHomeAndSource(t *testing.T) { c, err := build(testCatalog(), "docs", shippedSet()) if err != nil { t.Fatal(err) } html := page(t, c, English) for _, want := range []string{ "
What Docs is made with.
", `