docs/architecture is declared authoritative for settled design, so a page that cites code which has moved actively misleads every session and every subagent brief that reads it. Nothing in the gate compared a prose citation against the tree: docsgen checks the contract schema against Go types, docsite checks generated artifacts against their Markdown sources, prdlint checks PRD writing style. All three verify internal consistency, so a page can describe a package deleted months ago and every check stays green -- which is how a page describing a removed system survives behind a hand-written "removed" banner. Add tools/docdrift. It resolves every internal/... citation under docs/ against the tree and fails on one that dangles, handling the shapes prose actually uses: a package with a symbol (internal/config.Config), a test with its package (internal/app/archieui/TestFoo), a file-shape template (internal/webui/api_<concern>.go), and a Go package pattern (internal/...). Generated artifacts are skipped so embedded page bodies are not double-counted. The baseline is a burn-down list of the 48 citations already broken -- packages that moved (internal/store, internal/memory, internal/nats) and ones never built (internal/policy, the image and research capability docs). It is not a licence: the check also fails when an allowlisted citation starts resolving again, so an exemption cannot outlive its reason. Regenerate with `docdrift list`. Wire it into `task check` as docs:drift. Also correct AGENTS.md, which listed `policy` among the cross-cutting packages sitting under internal/. No such package has ever existed in this tree; the design is approved and unbuilt (docs/architecture/policy.md). That is exactly the class of stale claim this check now catches.
224 lines
7.5 KiB
Go
224 lines
7.5 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestNormalize(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want string
|
|
}{
|
|
{"bare package", "internal/config", "internal/config"},
|
|
{"trailing slash", "internal/policy/", "internal/policy"},
|
|
{"file", "internal/daemon/daemon.go", "internal/daemon/daemon.go"},
|
|
{"go wildcard", "internal/infrastructure/...", "internal/infrastructure"},
|
|
{"package and symbol", "internal/config.Config", "internal/config.Config"},
|
|
{"sentence punctuation", "internal/store,", "internal/store"},
|
|
{"line number", "internal/store/store.go:120", "internal/store/store.go"},
|
|
{"deep wildcard", "internal/domain/...", "internal/domain"},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := normalize(tc.in); got != tc.want {
|
|
t.Fatalf("normalize(%q) = %q, want %q", tc.in, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCandidates(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want []string
|
|
}{
|
|
{"package with symbol drops the symbol", "internal/config.Config", []string{"internal/config.Config", "internal/config"}},
|
|
{"file keeps only itself", "internal/daemon/daemon.go", []string{"internal/daemon/daemon.go"}},
|
|
{"bare package", "internal/webui", []string{"internal/webui"}},
|
|
{"dotted directory is not a symbol", "internal/domain/eda/module", []string{"internal/domain/eda/module"}},
|
|
{"test cited with its package drops the test name", "internal/app/archieui/TestFoo", []string{"internal/app/archieui/TestFoo", "internal/app/archieui"}},
|
|
{"lowercase final segment is a package, not a symbol", "internal/domain/curator", []string{"internal/domain/curator"}},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := candidates(tc.in)
|
|
if len(got) != len(tc.want) {
|
|
t.Fatalf("candidates(%q) = %v, want %v", tc.in, got, tc.want)
|
|
}
|
|
for i := range got {
|
|
if got[i] != tc.want[i] {
|
|
t.Fatalf("candidates(%q) = %v, want %v", tc.in, got, tc.want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// tree builds a throwaway repository root with the given files and directories.
|
|
func tree(t *testing.T, files, dirs []string) string {
|
|
t.Helper()
|
|
root := t.TempDir()
|
|
for _, d := range dirs {
|
|
if err := os.MkdirAll(filepath.Join(root, d), 0o750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for _, f := range files {
|
|
p := filepath.Join(root, f)
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, []byte("package x\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return root
|
|
}
|
|
|
|
func TestResolves(t *testing.T) {
|
|
root := tree(t,
|
|
[]string{"internal/config/config.go", "internal/daemon/daemon.go", "internal/webui/api_logs.go"},
|
|
[]string{"internal/webui", "internal/domain/workflow", "internal/app/archieui"},
|
|
)
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want bool
|
|
}{
|
|
{"package directory", "internal/webui", true},
|
|
{"nested package directory", "internal/domain/workflow", true},
|
|
{"file", "internal/daemon/daemon.go", true},
|
|
{"package and symbol resolves via the package", "internal/config.Config", true},
|
|
{"package cited without the .go file present", "internal/daemon", true},
|
|
{"moved package", "internal/memory", false},
|
|
{"deleted file", "internal/app/archied/config_update.go", false},
|
|
{"never built", "internal/policy", false},
|
|
{"whole package tree", "internal", true},
|
|
{"file-shape template resolves against a sibling", "internal/webui/api_", true},
|
|
{"test cited with its package resolves via the package", "internal/app/archieui/TestComposeUIServerHoldsNoDaemonState", true},
|
|
{"file-shape template with no sibling", "internal/webui/nothing_", false},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := resolves(root, tc.in); got != tc.want {
|
|
t.Fatalf("resolves(%q) = %v, want %v", tc.in, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestAnalyseSeparatesNewDriftFromKnownDebt pins the gate's core distinction: a
|
|
// citation already broken before this change must not fail the build, while one
|
|
// that dangles now must.
|
|
func TestAnalyseSeparatesNewDriftFromKnownDebt(t *testing.T) {
|
|
root := tree(t,
|
|
[]string{"internal/config/config.go"},
|
|
[]string{"internal/webui"},
|
|
)
|
|
doc := filepath.Join(root, "docs", "architecture")
|
|
if err := os.MkdirAll(doc, 0o750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := "See `internal/webui` and `internal/memory`.\nAlso `internal/nats` and `internal/config`.\n"
|
|
if err := os.WriteFile(filepath.Join(doc, "page.md"), []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got, err := analyse(root, "docs", map[string]bool{"internal/memory": true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got.newDrift) != 1 || got.newDrift[0].Path != "internal/nats" {
|
|
t.Fatalf("newDrift = %v, want exactly internal/nats", got.newDrift)
|
|
}
|
|
if got.newDrift[0].Line != 2 {
|
|
t.Fatalf("newDrift line = %d, want 2", got.newDrift[0].Line)
|
|
}
|
|
if got.known != 1 {
|
|
t.Fatalf("known = %d, want 1 (internal/memory is allowlisted)", got.known)
|
|
}
|
|
if got.scanned != 2 {
|
|
t.Fatalf("scanned = %d, want 2 (internal/webui, internal/config)", got.scanned)
|
|
}
|
|
if len(got.stale) != 0 {
|
|
t.Fatalf("stale = %v, want none", got.stale)
|
|
}
|
|
}
|
|
|
|
// TestAnalyseFlagsAnAllowlistEntryThatResolvesAgain keeps the burn-down list
|
|
// honest: once the code comes back, the exemption must go.
|
|
func TestAnalyseFlagsAnAllowlistEntryThatResolvesAgain(t *testing.T) {
|
|
root := tree(t, []string{"internal/config/config.go"}, []string{"internal/webui", "internal/policy"})
|
|
if err := os.MkdirAll(filepath.Join(root, "docs"), 0o750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(root, "docs", "p.md"), []byte("`internal/policy`\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got, err := analyse(root, "docs", map[string]bool{"internal/policy": true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got.stale) != 1 || got.stale[0] != "internal/policy" {
|
|
t.Fatalf("stale = %v, want [internal/policy]", got.stale)
|
|
}
|
|
if len(got.newDrift) != 0 {
|
|
t.Fatalf("newDrift = %v, want none", got.newDrift)
|
|
}
|
|
}
|
|
|
|
// TestScanSkipsGeneratedAndNonMarkdown guards the double-count: the generated
|
|
// dev-docs artifact embeds every page body, so scanning it would inflate both
|
|
// the findings and the baseline.
|
|
func TestScanSkipsGeneratedAndNonMarkdown(t *testing.T) {
|
|
root := tree(t, nil, []string{"docs/architecture", "docs/data/generated"})
|
|
write := func(rel, body string) {
|
|
if err := os.WriteFile(filepath.Join(root, rel), []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
write("docs/architecture/page.md", "`internal/gone`\n")
|
|
write("docs/data/generated/dev-docs.json", "{\"body\":\"internal/also-gone\"}\n")
|
|
write("docs/notes.txt", "internal/not-markdown\n")
|
|
|
|
got, err := scan(root, "docs")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 1 || got[0].Path != "internal/gone" {
|
|
t.Fatalf("scan = %v, want exactly [internal/gone]", got)
|
|
}
|
|
}
|
|
|
|
func TestReadBaseline(t *testing.T) {
|
|
root := t.TempDir()
|
|
p := filepath.Join(root, "baseline.txt")
|
|
body := "# a comment\n\ninternal/store\ninternal/memory \n"
|
|
if err := os.WriteFile(p, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := readBaseline(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 2 || !got["internal/store"] || !got["internal/memory"] {
|
|
t.Fatalf("readBaseline = %v, want internal/store and internal/memory", got)
|
|
}
|
|
}
|
|
|
|
// TestReadBaselineMissingIsEmpty lets the tool run before a baseline exists.
|
|
func TestReadBaselineMissingIsEmpty(t *testing.T) {
|
|
got, err := readBaseline(filepath.Join(t.TempDir(), "absent.txt"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 0 {
|
|
t.Fatalf("readBaseline = %v, want empty", got)
|
|
}
|
|
}
|