// Package docs_test checks the hand-written documentation against what it // describes, so the two cannot drift apart unnoticed. package docs_test import ( "os" "regexp" "sort" "strings" "testing" "github.com/gin-gonic/gin" "adam-french.co.uk/usaco-site/internal/handlers" ) // README.md's route table lists every API route, and nothing else. func TestReadmeRoutes(t *testing.T) { b, err := os.ReadFile("README.md") if err != nil { t.Fatal(err) } row := regexp.MustCompile("(?m)^\\| `(GET|POST|PUT|DELETE) /usaco/api([^`?]*)") param := regexp.MustCompile(`<([a-z]+)>`) documented := map[string]bool{} for _, m := range row.FindAllStringSubmatch(string(b), -1) { documented[m[1]+" "+param.ReplaceAllString(m[2], ":$1")] = true } gin.SetMode(gin.TestMode) r := gin.New() (&handlers.Server{}).Routes(r.Group("/usaco/api")) served := map[string]bool{} for _, rt := range r.Routes() { served[rt.Method+" "+strings.TrimPrefix(rt.Path, "/usaco/api")] = true } for _, missing := range diff(served, documented) { t.Errorf("README.md's route table is missing %s", missing) } for _, stale := range diff(documented, served) { t.Errorf("README.md documents %s, which is not served", stale) } } // The CLI page (web/src/views/CliPage.vue) is a condensed copy of the root // README's commands, since the image's build context cannot reach the // README. Every command there must be on the page. CI exports only site/, // so there this is skipped; `make check` in a full checkout runs it. func TestCliPageCoversTheCommands(t *testing.T) { readme, err := os.ReadFile("../README.md") if os.IsNotExist(err) { t.Skip("no root README (CI builds site/ alone)") } if err != nil { t.Fatal(err) } page, err := os.ReadFile("web/src/views/CliPage.vue") if err != nil { t.Fatal(err) } // Commands the page gives as another one's alias. aliases := map[string]string{"vim": "(also vim, edit)"} for _, m := range regexp.MustCompile(`(?m)^\./usaco ([a-z]+)`).FindAllStringSubmatch(string(readme), -1) { cmd := m[1] want := "usaco " + cmd if a, ok := aliases[cmd]; ok { want = a } if !strings.Contains(string(page), want) { t.Errorf("CliPage.vue does not mention %q (README.md has ./usaco %s)", want, cmd) } } } // diff is the keys of a that are not in b, sorted. func diff(a, b map[string]bool) []string { var out []string for k := range a { if !b[k] { out = append(out, k) } } sort.Strings(out) return out }