Files
mcp/internal/index/builder_test.go
Rene Nochebuena 13a186c60a feat(mcp): index struct fields and interface method sets (#1)
Minor release. The indexer named composite types but could not describe
their shape: every declaration was truncated at the first brace, so a
struct symbol carried only its `type X struct` header and an interface
symbol only its `type X interface` header. Field names, field types, and
— most painfully — struct tags such as `env:"EINHERJAR_PG_HOST"` were
dropped, as were the method sets of every port interface. An assistant
could be told that `db-postgres` has a `Config` and a `Provider`, but not
what env vars configure the one or what methods the other requires. This
change captures both.

internal/index (schema):
- Symbol gains two optional fields. `fields` ([]Field) carries a struct's
  field set — name, type, raw struct tag (surrounding backticks stripped),
  doc comment, and an `embedded` marker. `methods` ([]Method) carries an
  interface's method set — name, signature without the leading `func`, and
  doc comment, including embedded interfaces. Both are omitempty and absent
  for every other kind.
- SchemaVersion is deliberately unchanged. The two additions are additive
  and omitempty, so an older consumer parses the new index unchanged; per
  the existing rule the constant only bumps on a breaking format change.

internal/index (builder):
- collectSymbols now inspects each type's TypeSpec and, for a *ast.StructType
  or *ast.InterfaceType, fills the new Symbol members. A grouped field
  declaration (`x, y int`) yields one Field per name; an embedded field or
  interface yields an entry with an empty name.
- New helpers: typeSpecType (underlying type expr of a lone type spec),
  extractFields, extractIfaceMethods, fieldDoc (doc comment or trailing
  line comment), and nodeString — a non-truncating printer used for field
  types, tags, and method signatures, distinct from formatNode which keeps
  truncating to produce the one-line header.

internal/index (search):
- matches() now also tests the query against struct field names, field
  types, and struct tags, and against interface method names and
  signatures. A query like an env-var key or a method name now resolves to
  the type that declares it.

internal/tools:
- get_symbol and search_symbols descriptions updated to advertise the new
  struct-field and interface-method coverage. No input/output schema change
  beyond the additive Symbol fields, which get_symbol already returns whole.

internal/index (tests):
- New builder_test.go — the package previously had no tests. Builds a
  temporary module fixture and asserts capture of struct fields (with tags
  and docs), embedded fields, interface methods (with signatures and docs),
  embedded interfaces, and discovery of a struct by one of its struct tags
  (the failure mode that motivated the change).

Docs:
- CHANGELOG.md gains an [Unreleased] entry; README.md tool table updated to
  state that get_symbol returns struct fields and interface methods and
  that search_symbols matches fields, tags, and methods.

No new dependencies. The committed data/index.json placeholder is untouched
— the index is regenerated at image build (Dockerfile runs cmd/indexer),
so a deployment must be rebuilt to serve the richer index; a server still
running the prior image keeps serving the older, member-less one.

Reviewed-on: #1
Co-authored-by: Rene Nochebuena Guerrero <rene@nochebuena.dev>
Co-committed-by: Rene Nochebuena Guerrero <rene@nochebuena.dev>
2026-06-10 10:38:30 -06:00

150 lines
3.7 KiB
Go

package index
import (
"os"
"path/filepath"
"testing"
)
// writeFixture lays down a single-module Einherjar-like repo under a temp root
// and returns the root path for Build to walk.
func writeFixture(t *testing.T) string {
t.Helper()
root := t.TempDir()
modDir := filepath.Join(root, "demo")
if err := os.MkdirAll(modDir, 0o755); err != nil {
t.Fatal(err)
}
write := func(name, body string) {
if err := os.WriteFile(filepath.Join(modDir, name), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
write("go.mod", "module example.com/demo\n\ngo 1.26\n")
write("demo.go", `// Package demo is a build fixture.
package demo
import "context"
// Config holds settings.
type Config struct {
// Host is the bind address.
Host string `+"`"+`env:"DEMO_HOST" envDefault:"0.0.0.0"`+"`"+`
Port int `+"`"+`env:"DEMO_PORT"`+"`"+`
Inner
}
// Inner is embedded into Config.
type Inner struct {
Name string
}
// Store reads values.
type Store interface {
// Get fetches one value.
Get(ctx context.Context, id string) (string, error)
context.Context
}
`)
return root
}
func findSymbol(t *testing.T, idx *Index, mod, name string) Symbol {
t.Helper()
m := idx.FindModule(mod)
if m == nil {
t.Fatalf("module %q not found", mod)
}
for _, s := range m.Symbols {
if s.Name == name {
return s
}
}
t.Fatalf("symbol %q not found in module %q", name, mod)
return Symbol{}
}
func TestBuildCapturesStructFields(t *testing.T) {
idx, err := Build(writeFixture(t))
if err != nil {
t.Fatal(err)
}
cfg := findSymbol(t, idx, "demo", "Config")
if cfg.Kind != "type" {
t.Errorf("Config kind = %q, want type", cfg.Kind)
}
if len(cfg.Fields) != 3 {
t.Fatalf("Config has %d fields, want 3: %+v", len(cfg.Fields), cfg.Fields)
}
host := cfg.Fields[0]
if host.Name != "Host" || host.Type != "string" {
t.Errorf("field[0] = %+v, want Host string", host)
}
if host.Tag != `env:"DEMO_HOST" envDefault:"0.0.0.0"` {
t.Errorf("Host tag = %q, want the env/envDefault tag (backticks stripped)", host.Tag)
}
if host.Doc != "Host is the bind address." {
t.Errorf("Host doc = %q", host.Doc)
}
if port := cfg.Fields[1]; port.Name != "Port" || port.Tag != `env:"DEMO_PORT"` {
t.Errorf("field[1] = %+v, want Port with env tag", port)
}
if inner := cfg.Fields[2]; !inner.Embedded || inner.Name != "" || inner.Type != "Inner" {
t.Errorf("field[2] = %+v, want embedded Inner", inner)
}
}
func TestBuildCapturesInterfaceMethods(t *testing.T) {
idx, err := Build(writeFixture(t))
if err != nil {
t.Fatal(err)
}
store := findSymbol(t, idx, "demo", "Store")
if store.Kind != "interface" {
t.Errorf("Store kind = %q, want interface", store.Kind)
}
if len(store.Methods) != 2 {
t.Fatalf("Store has %d methods, want 2: %+v", len(store.Methods), store.Methods)
}
get := store.Methods[0]
if get.Name != "Get" {
t.Errorf("method[0] name = %q, want Get", get.Name)
}
if get.Signature != "Get(ctx context.Context, id string) (string, error)" {
t.Errorf("Get signature = %q", get.Signature)
}
if get.Doc != "Get fetches one value." {
t.Errorf("Get doc = %q", get.Doc)
}
if emb := store.Methods[1]; emb.Name != "" || emb.Signature != "context.Context" {
t.Errorf("method[1] = %+v, want embedded context.Context", emb)
}
}
// A struct must be discoverable by one of its struct tags — the failure mode
// that motivated capturing fields in the first place.
func TestSearchSymbolsByStructTag(t *testing.T) {
idx, err := Build(writeFixture(t))
if err != nil {
t.Fatal(err)
}
got := idx.SearchSymbols("DEMO_HOST", 10)
found := false
for _, s := range got {
if s.Name == "Config" {
found = true
}
}
if !found {
t.Errorf("searching a struct tag did not surface its type; got %d results", len(got))
}
}