Files
magpie/site/docs-index.mjs
yetone 31dcfcc336 testenv: a docs page edited without its site index fails the package run
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.
2026-11-17 15:17:35 +08:00

40 lines
1.8 KiB
JavaScript

// docs-index.mjs writes everything built from the docs pages:
// public/llms-full.txt (llms-full.mjs), public/search/<lang>.json
// (search-index.mjs) and docs-index.sums, a sha256sum line for each file
// they are built from. `node llms-full.mjs` and `node search-index.mjs`
// each run all of it. internal/testenv fails every Go package's test run
// while a page's sum differs from docs-index.sums, so a Go change that
// edits a docs page and runs only Go tests can't leave the two files
// stale (five times on main from 11-13 to 11-17).
import { createHash } from "node:crypto";
import { readFile, readdir, writeFile } from "node:fs/promises";
import { build as llmsFull } from "./llms-full.mjs";
import { build as searchIndex } from "./search-index.mjs";
const pub = new URL("./public/", import.meta.url);
export const SUMS = new URL("./docs-index.sums", import.meta.url);
// inputs are the files under public/ both builds read, in name order
export async function inputs() {
const names = ["llms.txt"];
for (const dir of ["docs/", "docs/zh/", "docs/ja/"]) {
for (const f of await readdir(new URL(dir, pub))) if (f.endsWith(".html") && f !== "contributors.html") names.push(dir + f);
}
return names.sort();
}
// sums is docs-index.sums' text: "<sha256> <path under public/>" a line
export async function sums() {
const lines = [];
for (const n of await inputs()) lines.push(createHash("sha256").update(await readFile(new URL(n, pub))).digest("hex") + " " + n);
return lines.join("\n") + "\n";
}
export async function writeAll() {
await writeFile(new URL("llms-full.txt", pub), await llmsFull());
for (const [lang, json] of Object.entries(await searchIndex())) await writeFile(new URL(`search/${lang}.json`, pub), json);
await writeFile(SUMS, await sums());
}
if (import.meta.url === `file://${process.argv[1]}`) await writeAll();