internal/server/delegation/delegation_doc_test.go
Ref: Size: 2.3 KiB History
package delegation
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
// TestDocsMatchThePendingGrace keeps the prose from drifting from the constant.
// defaultPendingGrace is the only place the grace is decided, and three surfaces
// restate it in words for readers who never see the source: the delegation
// section of docs/mcp.md, the delegate_begin tool description, and the route
// summary that becomes the OpenAPI spec and the TypeScript types. Each says the
// number exactly once, so this reads the real files and fails if the constant
// moves without them, rather than letting the promise quietly lie in three
// places at once.
func TestDocsMatchThePendingGrace(t *testing.T) {
want := graceProse(t, defaultPendingGrace)
for _, rel := range []string{
"docs/mcp.md",
"internal/mcpserver/server.go",
"internal/server/api/routes.go",
} {
if !strings.Contains(string(readRepoFile(t, rel)), want) {
t.Errorf("%s no longer tells a caller a begin is abandoned after %q — update the wording "+
"there and in the other two sites when defaultPendingGrace changes", rel, want)
}
}
}
// graceProse renders a grace the way the three surfaces say it in English. Only
// the durations the prose has words for are listed: changing the constant to
// anything else fails here first, which is the prompt to decide the wording
// once and then let the test above find every site that needs it.
func graceProse(t *testing.T, d time.Duration) string {
t.Helper()
if d == time.Hour {
return "an hour"
}
t.Fatalf("no documented wording for a grace of %s — write it here, then update the sites this test reads", d)
return ""
}
// readRepoFile reads a path relative to the repo root, found by walking up from
// this source file to the go.mod. Tests run from the package directory, so the
// repo root is not the working directory.
func readRepoFile(t *testing.T, rel string) []byte {
t.Helper()
_, self, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("runtime.Caller failed")
}
dir := filepath.Dir(self)
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
break
}
parent := filepath.Dir(dir)
if parent == dir {
t.Fatal("could not find repo root (go.mod) above " + self)
}
dir = parent
}
b, err := os.ReadFile(filepath.Join(dir, rel))
if err != nil {
t.Fatal(err)
}
return b
}