go-sdk/mcp/util_test.go
Jonathan Amsterdam 7130f0f081 internal/mcp: meta and progress token
Meta is a struct that contains a map[string]any, and a ProgressToken
field.

Each Params and Result type has a Meta as a value field.

Meta marshals and unmarshals specially, so the ProgressToken field
becomes part of the map. The marshalling code is generalized in
anticipation of every Params and Result growing a map of additional
properties.

Change-Id: I6d2f34270d33501c96437f24f91ef3e51d20d955
Reviewed-on: https://go-review.googlesource.com/c/tools/+/674956
Reviewed-by: Sam Thanawalla <samthanawalla@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
2025-05-27 12:53:53 -07:00

48 lines
1.2 KiB
Go

// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package mcp
import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestMarshalStructWithMap(t *testing.T) {
type S struct {
A int
B string `json:"b,omitempty"`
u bool
M map[string]any `json:",omitempty"`
}
t.Run("basic", func(t *testing.T) {
s := S{A: 1, B: "two", M: map[string]any{"!@#": true}}
got, err := marshalStructWithMap(&s, "M")
if err != nil {
t.Fatal(err)
}
want := `{"A":1,"b":"two","!@#":true}`
if g := string(got); g != want {
t.Errorf("\ngot %s\nwant %s", g, want)
}
var un S
if err := unmarshalStructWithMap(got, &un, "M"); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(s, un, cmpopts.IgnoreUnexported(S{})); diff != "" {
t.Errorf("mismatch (-want, +got):\n%s", diff)
}
})
t.Run("duplicate", func(t *testing.T) {
s := S{A: 1, B: "two", M: map[string]any{"b": "dup"}}
_, err := marshalStructWithMap(&s, "M")
if err == nil || !strings.Contains(err.Error(), "duplicate") {
t.Errorf("got %v, want error with 'duplicate'", err)
}
})
}