a83b79fc
test: docs can't drift from the hosted defaults or the reserved-port range
a73x 2026-08-12 19:21
Commit message
internal/cli/hosted_defaults_doc_test.go
| Old | New | ||
|---|---|---|---|
| @@ -0,0 +1,51 @@ | |||
| 1 | package cli | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "os" | ||
| 5 | "path/filepath" | ||
| 6 | "runtime" | ||
| 7 | "strings" | ||
| 8 | "testing" | ||
| 9 | ) | ||
| 10 | |||
| 11 | // TestDocsMatchHostedDefaults keeps the prose from drifting from the code. The | ||
| 12 | // client's hosted fallback lives once, in defaultURL and defaultGate; the | ||
| 13 | // docs restate those addresses for a reader who never sees the source. This | ||
| 14 | // test reads the real doc and fails if either the constant or the doc moves | ||
| 15 | // without the other, so the two can never quietly disagree. | ||
| 16 | func TestDocsMatchHostedDefaults(t *testing.T) { | ||
| 17 | doc := string(readRepoFile(t, "docs/ssh-access.md")) | ||
| 18 | for _, want := range []string{defaultURL, defaultGate} { | ||
| 19 | if !strings.Contains(doc, "`"+want+"`") { | ||
| 20 | t.Errorf("docs/ssh-access.md no longer states the hosted default `%s` — "+ | ||
| 21 | "update the doc and this test together when the constant changes", want) | ||
| 22 | } | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | // readRepoFile reads a path relative to the repo root, found by walking up from | ||
| 27 | // this source file to the go.mod. Tests run from the package directory, so the | ||
| 28 | // repo root is not the working directory. | ||
| 29 | func readRepoFile(t *testing.T, rel string) []byte { | ||
| 30 | t.Helper() | ||
| 31 | _, self, _, ok := runtime.Caller(0) | ||
| 32 | if !ok { | ||
| 33 | t.Fatal("runtime.Caller failed") | ||
| 34 | } | ||
| 35 | dir := filepath.Dir(self) | ||
| 36 | for { | ||
| 37 | if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { | ||
| 38 | break | ||
| 39 | } | ||
| 40 | parent := filepath.Dir(dir) | ||
| 41 | if parent == dir { | ||
| 42 | t.Fatal("could not find repo root (go.mod) above " + self) | ||
| 43 | } | ||
| 44 | dir = parent | ||
| 45 | } | ||
| 46 | b, err := os.ReadFile(filepath.Join(dir, rel)) | ||
| 47 | if err != nil { | ||
| 48 | t.Fatal(err) | ||
| 49 | } | ||
| 50 | return b | ||
| 51 | } | ||
internal/server/store/exposures_doc_test.go
| Old | New | ||
|---|---|---|---|
| @@ -0,0 +1,56 @@ | |||
| 1 | package store | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "os" | ||
| 5 | "path/filepath" | ||
| 6 | "runtime" | ||
| 7 | "strconv" | ||
| 8 | "strings" | ||
| 9 | "testing" | ||
| 10 | ) | ||
| 11 | |||
| 12 | // TestDocsMatchReservedPortRange keeps the documented reserved range in step | ||
| 13 | // with the constants that define it. MinAllocatedHostPort and MaxAllocatedHostPort | ||
| 14 | // are the host contract; docs/mcp.md restates the span for a reader who never | ||
| 15 | // sees the source. This reads the real doc and fails if either the constants or | ||
| 16 | // the doc moves without the other. It matches on the bare numbers so it is | ||
| 17 | // indifferent to whether the doc joins them with a hyphen or an en-dash. | ||
| 18 | func TestDocsMatchReservedPortRange(t *testing.T) { | ||
| 19 | doc := string(readRepoFile(t, "docs/mcp.md")) | ||
| 20 | for _, want := range []string{ | ||
| 21 | strconv.Itoa(MinAllocatedHostPort), | ||
| 22 | strconv.Itoa(MaxAllocatedHostPort), | ||
| 23 | } { | ||
| 24 | if !strings.Contains(doc, want) { | ||
| 25 | t.Errorf("docs/mcp.md no longer states the reserved-range bound %s — "+ | ||
| 26 | "update the doc and this test's constants together when the range changes", want) | ||
| 27 | } | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | // readRepoFile reads a path relative to the repo root, found by walking up from | ||
| 32 | // this source file to the go.mod. Tests run from the package directory, so the | ||
| 33 | // repo root is not the working directory. | ||
| 34 | func readRepoFile(t *testing.T, rel string) []byte { | ||
| 35 | t.Helper() | ||
| 36 | _, self, _, ok := runtime.Caller(0) | ||
| 37 | if !ok { | ||
| 38 | t.Fatal("runtime.Caller failed") | ||
| 39 | } | ||
| 40 | dir := filepath.Dir(self) | ||
| 41 | for { | ||
| 42 | if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { | ||
| 43 | break | ||
| 44 | } | ||
| 45 | parent := filepath.Dir(dir) | ||
| 46 | if parent == dir { | ||
| 47 | t.Fatal("could not find repo root (go.mod) above " + self) | ||
| 48 | } | ||
| 49 | dir = parent | ||
| 50 | } | ||
| 51 | b, err := os.ReadFile(filepath.Join(dir, rel)) | ||
| 52 | if err != nil { | ||
| 53 | t.Fatal(err) | ||
| 54 | } | ||
| 55 | return b | ||
| 56 | } | ||