Files
archie/tools/docsgen/main_test.go
T
sam 1f975f37ad feat(docsgen): add a non-destructive check mode for generated drift
`docs/data/generated/contracts.json` sat stale since 4b340d2b with nothing
to notice: no task, no test, and no CI step regenerates or compares it, so
the committed contract reference quietly drifted from the Go types it
claims to describe. It was only found by hand-running the generator.

Implements Step 6 of docs/architecture/generated-documentation.md, which
already specified this command and closed the gap filed in
docs/prds/contract-conformance-audit.md ("`docsgen check` is documented and
does not exist"):

1. generates into a temporary directory;
2. normalizes line endings;
3. compares the schema trees with the committed output;
4. reports missing, changed, and obsolete files;
5. names the authoritative definition behind each mismatch;
6. exits non-zero without modifying the working tree.

The check never writes to the working tree, so the author applies the
regenerated bytes rather than the gate silently doing it. That is why this
is a `check` and not a CI auto-update: the code change that shifted the
contract surface should be reviewed with its regenerated artifact in one
commit, and a bot commit would bless an unintended contract change with no
reviewer ever seeing it. Step 9 states the same prohibition directly.

Diagnostics name the Go type, so a failure says where to look:

    docs/data/generated/contracts.json is stale; regenerate it with:
    go -C tools run ./docsgen --repo-root ..
      schema "AgentExecutionResult" changed (authoritative definition:
        github.com/samcharles93/archie-core/internal/agentexec.Result)
      schema "MediaAttachment" changed (authoritative definition:
        reached from .../internal/gateway.MessageEvent)

Nested definitions are attributed through the published contract that
references them, iterated in sorted order so the diagnostic is
deterministic when several contracts share a definition.

Usage: `go -C tools run ./docsgen check --repo-root ..`, plus the existing
`--out` to compare an arbitrary artifact.

Reuses the write path rather than reimplementing encoding, so check and
generate cannot disagree about the expected bytes.

Tests cover: the committed artifact is current (the regression guard that
would have caught this), changed/removed/missing/obsolete detection, and
that a failing check leaves the artifact's bytes, mode, and directory
contents untouched.

`task docs:check` wiring into `task check` is deliberately not in this
commit: Taskfile.yml is being edited concurrently in this working tree.

Verified with `task check` (green).
2026-09-18 12:46:21 +10:00

399 lines
11 KiB
Go

package main
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/invopop/jsonschema"
)
type contractWithConflictingDefinition struct {
Value conflictingDefinition `json:"value"`
}
type conflictingDefinition struct {
Value string `json:"value"`
}
func TestRunWritesDeterministicContractData(t *testing.T) {
repoRoot := filepath.Clean("../..")
outputDir := t.TempDir()
outputs := []string{
filepath.Join(outputDir, "first.json"),
filepath.Join(outputDir, "second.json"),
}
for _, output := range outputs {
if err := run(repoRoot, output); err != nil {
t.Fatalf("run(%q) error = %v", output, err)
}
}
first, err := os.ReadFile(outputs[0])
if err != nil {
t.Fatalf("read first output: %v", err)
}
second, err := os.ReadFile(outputs[1])
if err != nil {
t.Fatalf("read second output: %v", err)
}
if !bytes.Equal(first, second) {
t.Fatal("repeated generation produced different bytes")
}
var data struct {
GeneratedBy string `json:"generatedBy"`
Schemas map[string]json.RawMessage `json:"schemas"`
}
if err := json.Unmarshal(first, &data); err != nil {
t.Fatalf("decode generated data: %v", err)
}
if data.GeneratedBy != "tools/docsgen" {
t.Fatalf("generatedBy = %q, want %q", data.GeneratedBy, "tools/docsgen")
}
for _, name := range []string{
"AgentExecutionRequest",
"AgentExecutionResult",
"MessageEvent",
} {
if _, ok := data.Schemas[name]; !ok {
t.Errorf("schemas[%q] is missing", name)
}
}
for name, schema := range data.Schemas {
assertSchemaRefsResolve(t, name, schema, data.Schemas)
}
}
func TestRunHonoursAbsoluteOutputPath(t *testing.T) {
repoRoot := filepath.Clean("../..")
outputDir := t.TempDir()
originalDir, err := os.Getwd()
if err != nil {
t.Fatalf("get working directory: %v", err)
}
for _, name := range []string{"first.yaml", "second.yaml"} {
output := filepath.Join(outputDir, name)
if err := run(repoRoot, output); err != nil {
t.Fatalf("run(%q) error = %v", name, err)
}
if _, err := os.Stat(output); err != nil {
t.Fatalf("generated output %q: %v", name, err)
}
}
currentDir, err := os.Getwd()
if err != nil {
t.Fatalf("get restored working directory: %v", err)
}
if currentDir != originalDir {
t.Fatalf("working directory = %q, want %q", currentDir, originalDir)
}
}
func TestCollectSchemaRejectsDuplicatePublishedName(t *testing.T) {
reflector := &jsonschema.Reflector{ExpandedStruct: true}
schemas := map[string]any{}
const name = "PublishedContract"
if err := collectSchema(reflector, schemas, name, reflect.TypeFor[struct {
First string `json:"first"`
}]()); err != nil {
t.Fatalf("collect first schema: %v", err)
}
err := collectSchema(reflector, schemas, name, reflect.TypeFor[struct {
Second int `json:"second"`
}]())
if err == nil {
t.Fatal("collect duplicate schema returned nil error")
}
}
func TestCollectSchemaRejectsConflictingDefinition(t *testing.T) {
reflector := &jsonschema.Reflector{ExpandedStruct: true}
schemas := map[string]any{
"conflictingDefinition": map[string]any{"type": "integer"},
}
err := collectSchema(
reflector,
schemas,
"PublishedContract",
reflect.TypeFor[contractWithConflictingDefinition](),
)
if err == nil {
t.Fatal("collect conflicting definition returned nil error")
}
if !strings.Contains(err.Error(), `conflicting schema definition "conflictingDefinition"`) {
t.Fatalf("collect conflicting definition error = %q", err)
}
}
func TestRunReportsFilesystemAndSourceErrors(t *testing.T) {
repoRoot := filepath.Clean("../..")
blocker := filepath.Join(t.TempDir(), "file")
if err := os.WriteFile(blocker, []byte("occupied"), 0o600); err != nil {
t.Fatalf("create blocker: %v", err)
}
tests := []struct {
name string
root string
output string
contains string
}{
{
name: "missing source tree",
root: t.TempDir(),
output: filepath.Join(t.TempDir(), "contracts.json"),
contains: "load Go comments",
},
{
name: "output parent is a file",
root: repoRoot,
output: filepath.Join(blocker, "contracts.json"),
contains: "create output directory",
},
{
name: "output is a directory",
root: repoRoot,
output: t.TempDir(),
contains: "write ",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := run(test.root, test.output)
if err == nil {
t.Fatal("run() returned nil error")
}
if !bytes.Contains([]byte(err.Error()), []byte(test.contains)) {
t.Fatalf("run() error = %q, want substring %q", err, test.contains)
}
})
}
}
func TestCheckAcceptsTheCommittedArtifact(t *testing.T) {
repoRoot := filepath.Clean("../..")
if err := check(repoRoot, defaultOutputPath); err != nil {
t.Fatalf("check(%q) error = %v; committed generated data is stale", defaultOutputPath, err)
}
}
func TestCheckReportsChangedSchema(t *testing.T) {
repoRoot := filepath.Clean("../..")
output := filepath.Join(t.TempDir(), "contracts.json")
if err := run(repoRoot, output); err != nil {
t.Fatalf("run() error = %v", err)
}
mutateGeneratedData(t, output, func(data map[string]any) {
schemas := data["schemas"].(map[string]any)
schemas["AgentExecutionResult"].(map[string]any)["title"] = "tampered"
})
err := check(repoRoot, output)
if err == nil {
t.Fatal("check() returned nil error for a changed artifact")
}
for _, want := range []string{"changed", "AgentExecutionResult", "agentexec.Result"} {
if !strings.Contains(err.Error(), want) {
t.Errorf("check() error = %q, want substring %q", err, want)
}
}
}
func TestCheckReportsRemovedSchema(t *testing.T) {
repoRoot := filepath.Clean("../..")
output := filepath.Join(t.TempDir(), "contracts.json")
if err := run(repoRoot, output); err != nil {
t.Fatalf("run() error = %v", err)
}
mutateGeneratedData(t, output, func(data map[string]any) {
delete(data["schemas"].(map[string]any), "MessageEvent")
})
err := check(repoRoot, output)
if err == nil {
t.Fatal("check() returned nil error for a removed schema")
}
if !strings.Contains(err.Error(), "MessageEvent") {
t.Errorf("check() error = %q, want the removed schema named", err)
}
}
func TestCheckReportsMissingArtifact(t *testing.T) {
repoRoot := filepath.Clean("../..")
output := filepath.Join(t.TempDir(), "contracts.json")
err := check(repoRoot, output)
if err == nil {
t.Fatal("check() returned nil error for a missing artifact")
}
if !strings.Contains(err.Error(), "missing") {
t.Fatalf("check() error = %q, want it to report a missing artifact", err)
}
if _, statErr := os.Stat(output); statErr == nil {
t.Fatal("check() created the missing artifact instead of reporting it")
}
}
func TestCheckReportsObsoleteArtifact(t *testing.T) {
repoRoot := filepath.Clean("../..")
outputDir := t.TempDir()
if err := run(repoRoot, filepath.Join(outputDir, "contracts.json")); err != nil {
t.Fatalf("run() error = %v", err)
}
obsolete := filepath.Join(outputDir, "orphaned.json")
if err := os.WriteFile(obsolete, []byte("{}\n"), 0o600); err != nil {
t.Fatalf("write obsolete artifact: %v", err)
}
err := check(repoRoot, filepath.Join(outputDir, "contracts.json"))
if err == nil {
t.Fatal("check() returned nil error for an obsolete artifact")
}
if !strings.Contains(err.Error(), "obsolete") || !strings.Contains(err.Error(), "orphaned.json") {
t.Fatalf("check() error = %q, want the obsolete file named", err)
}
}
func TestCheckNeverModifiesTheWorkingTree(t *testing.T) {
repoRoot := filepath.Clean("../..")
outputDir := t.TempDir()
output := filepath.Join(outputDir, "contracts.json")
if err := run(repoRoot, output); err != nil {
t.Fatalf("run() error = %v", err)
}
mutateGeneratedData(t, output, func(data map[string]any) {
data["generatedBy"] = "tampered"
})
before, err := os.ReadFile(output)
if err != nil {
t.Fatalf("read before: %v", err)
}
beforeInfo, err := os.Stat(output)
if err != nil {
t.Fatalf("stat before: %v", err)
}
if err := check(repoRoot, output); err == nil {
t.Fatal("check() returned nil error for a changed artifact")
}
after, err := os.ReadFile(output)
if err != nil {
t.Fatalf("read after: %v", err)
}
if !bytes.Equal(before, after) {
t.Fatal("check() rewrote the artifact under test")
}
afterInfo, err := os.Stat(output)
if err != nil {
t.Fatalf("stat after: %v", err)
}
if beforeInfo.Mode() != afterInfo.Mode() {
t.Fatalf("check() changed the artifact mode: %v -> %v", beforeInfo.Mode(), afterInfo.Mode())
}
if entries, err := os.ReadDir(outputDir); err != nil {
t.Fatalf("read output dir: %v", err)
} else if len(entries) != 1 {
t.Fatalf("check() left %d files in the output dir, want 1", len(entries))
}
}
func mutateGeneratedData(t *testing.T, path string, mutate func(map[string]any)) {
t.Helper()
raw, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
var data map[string]any
if err := json.Unmarshal(raw, &data); err != nil {
t.Fatalf("decode %s: %v", path, err)
}
mutate(data)
body, err := json.MarshalIndent(data, "", " ")
if err != nil {
t.Fatalf("encode %s: %v", path, err)
}
if err := os.WriteFile(path, append(body, '\n'), 0o600); err != nil {
t.Fatalf("write %s: %v", path, err)
}
}
func TestRewriteRef(t *testing.T) {
tests := map[string]string{
"definition": "#/schemas/Contract",
"external": "https://example.com/schema.json",
"empty name": "#/$defs/",
}
for name, want := range tests {
t.Run(name, func(t *testing.T) {
input := want
if name == "definition" {
input = "#/$defs/Contract"
}
if got := rewriteRef(input); got != want {
t.Fatalf("rewriteRef(%q) = %q, want %q", input, got, want)
}
})
}
}
func assertSchemaRefsResolve(
t *testing.T,
name string,
raw json.RawMessage,
schemas map[string]json.RawMessage,
) {
t.Helper()
var value any
if err := json.Unmarshal(raw, &value); err != nil {
t.Fatalf("decode schema %q: %v", name, err)
}
walkJSON(value, func(ref string) {
const prefix = "#/schemas/"
if !strings.HasPrefix(ref, "#") {
return
}
if !strings.HasPrefix(ref, prefix) {
t.Errorf("schema %q contains unresolved local reference %q", name, ref)
return
}
target := ref[len(prefix):]
if _, ok := schemas[target]; !ok {
t.Errorf("schema %q references missing schema %q", name, target)
}
})
}
func walkJSON(value any, visitRef func(string)) {
switch node := value.(type) {
case map[string]any:
for key, child := range node {
if key == "$ref" {
if ref, ok := child.(string); ok {
visitRef(ref)
}
continue
}
walkJSON(child, visitRef)
}
case []any:
for _, child := range node {
walkJSON(child, visitRef)
}
}
}