site/public/llms-full.txt and search/*.json are built from the docs pages and committed. A Go change that edited a docs page and ran only Go tests left them stale on main five times from 11-13 to 11-17 (459d668b3,256910f17,9dcec6751,dbbb2f26b,00b2c11a6), each caught by CI's node tests after the push. site/docs-index.mjs now writes all three: llms-full.txt, search/*.json, and docs-index.sums, a sha256sum line per page they are built from. `node llms-full.mjs` and `node search-index.mjs` each run all of it, so running either one is enough. After a package's tests, testenv checks the pages against the sums the way it checks eagerHere, and fails the run when any page is edited, added or gone, naming each page. That covers the 64 packages that use testenv, a -run of one test included. search.test.mjs checks that the sums are current. The code standards' criterion 3 and LESSONS say so. Generating at this base changed neither llms-full.txt nor the search indexes, so they were current. Run: - mote: go vet ./internal/testenv; go test ./internal/testenv (TestDocsIndexStaleNamesEachChangedPage, TestDocsIndexCurrent) and internal/agent's TestEveryFieldLabelHasTUIZh: ok. - mote: node --test site/search.test.mjs site/worker.test.mjs: 47 pass. - Linux (wsl-run) and Windows (win-run): go test ./internal/testenv ok. - mote: GOOS=windows/linux go vet ./internal/testenv: ok. gofmt -l: clean. Broken by hand: appending a <p> to docs/alma.html without regenerating made `go test -run TestEveryFieldLabelHasTUIZh ./internal/agent` fail ("testenv: docs pages changed … docs/alma.html") with its test passing. search.test.mjs went red too. Not run: the full Go suite. It starts after the push.
73 lines
2.3 KiB
Go
73 lines
2.3 KiB
Go
package testenv
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
// TestDocsIndexStaleNamesEachChangedPage holds DocsIndexStale to its
|
|
// cases: a page edited, added or gone since the sums were written is
|
|
// named, the sums' own pages pass, and a tree with no docs has nothing to
|
|
// check.
|
|
func TestDocsIndexStaleNamesEachChangedPage(t *testing.T) {
|
|
root := t.TempDir()
|
|
if got, err := DocsIndexStale(root); err != nil || got != nil {
|
|
t.Fatalf("no docs: %v, %v", got, err)
|
|
}
|
|
pub := filepath.Join(root, "site", "public")
|
|
files := map[string]string{
|
|
"llms.txt": "# magpie\n", "docs/a.html": "<article>a</article>", "docs/zh/a.html": "<article>甲</article>",
|
|
"docs/ja/a.html": "<article>あ</article>", "docs/contributors.html": "not built from",
|
|
}
|
|
sums := ""
|
|
for _, n := range []string{"docs/a.html", "docs/ja/a.html", "docs/zh/a.html", "llms.txt"} {
|
|
h := sha256.Sum256([]byte(files[n]))
|
|
sums += hex.EncodeToString(h[:]) + " " + n + "\n"
|
|
}
|
|
for n, b := range files {
|
|
p := filepath.Join(pub, filepath.FromSlash(n))
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, []byte(b), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := os.WriteFile(filepath.Join(root, "site", "docs-index.sums"), []byte(sums), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, err := DocsIndexStale(root); err != nil || len(got) != 0 {
|
|
t.Fatalf("current: %v, %v", got, err)
|
|
}
|
|
write := func(n, b string) {
|
|
if err := os.WriteFile(filepath.Join(pub, filepath.FromSlash(n)), []byte(b), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
write("docs/zh/a.html", "<article>乙</article>")
|
|
write("docs/b.html", "<article>b</article>")
|
|
write("docs/contributors.html", "changed, still not built from")
|
|
if err := os.Remove(filepath.Join(pub, "docs", "ja", "a.html")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := DocsIndexStale(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if want := []string{"docs/b.html (new)", "docs/ja/a.html (gone)", "docs/zh/a.html"}; !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("got %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
// TestDocsIndexCurrent checks this tree's own docs against its sums, the
|
|
// check every package using testenv makes after its tests.
|
|
func TestDocsIndexCurrent(t *testing.T) {
|
|
if err := docsIndexHere(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|