internal/mcp: keep an ordered list of feature keys
This change is necessary to support pagination which will be implemented in subsequent CLs. Change-Id: I8bc616a57463ea569dc7cadf1d4f06c69602ddd5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/674135 Reviewed-by: Jonathan Amsterdam <jba@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
parent
eb76b5a6e0
commit
172901f3ac
1 changed files with 120 additions and 9 deletions
75
mcp/features_test.go
Normal file
75
mcp/features_test.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
// 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 (
|
||||
"context"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/modelcontextprotocol/go-sdk/jsonschema"
|
||||
)
|
||||
|
||||
type SayHiParams struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func SayHi(ctx context.Context, cc *ServerSession, params *SayHiParams) ([]*Content, error) {
|
||||
return []*Content{
|
||||
NewTextContent("Hi " + params.Name),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestFeatureSetOrder(t *testing.T) {
|
||||
toolA := NewTool("apple", "apple tool", SayHi).Tool
|
||||
toolB := NewTool("banana", "banana tool", SayHi).Tool
|
||||
toolC := NewTool("cherry", "cherry tool", SayHi).Tool
|
||||
|
||||
testCases := []struct {
|
||||
tools []*Tool
|
||||
want []*Tool
|
||||
}{
|
||||
{[]*Tool{toolA, toolB, toolC}, []*Tool{toolA, toolB, toolC}},
|
||||
{[]*Tool{toolB, toolC, toolA}, []*Tool{toolA, toolB, toolC}},
|
||||
{[]*Tool{toolA, toolC}, []*Tool{toolA, toolC}},
|
||||
{[]*Tool{toolA, toolA, toolA}, []*Tool{toolA}},
|
||||
{[]*Tool{}, nil},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
fs := newFeatureSet(func(t *Tool) string { return t.Name })
|
||||
fs.add(tc.tools...)
|
||||
got := slices.Collect(fs.all())
|
||||
if diff := cmp.Diff(got, tc.want, cmpopts.IgnoreUnexported(jsonschema.Schema{})); diff != "" {
|
||||
t.Errorf("expected %v, got %v, (-want +got):\n%s", tc.want, got, diff)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFeatureSetAbove(t *testing.T) {
|
||||
toolA := NewTool("apple", "apple tool", SayHi).Tool
|
||||
toolB := NewTool("banana", "banana tool", SayHi).Tool
|
||||
toolC := NewTool("cherry", "cherry tool", SayHi).Tool
|
||||
|
||||
testCases := []struct {
|
||||
tools []*Tool
|
||||
above string
|
||||
want []*Tool
|
||||
}{
|
||||
{[]*Tool{toolA, toolB, toolC}, "apple", []*Tool{toolB, toolC}},
|
||||
{[]*Tool{toolA, toolB, toolC}, "banana", []*Tool{toolC}},
|
||||
{[]*Tool{toolA, toolB, toolC}, "cherry", nil},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
fs := newFeatureSet(func(t *Tool) string { return t.Name })
|
||||
fs.add(tc.tools...)
|
||||
got := slices.Collect(fs.above(tc.above))
|
||||
if diff := cmp.Diff(got, tc.want, cmpopts.IgnoreUnexported(jsonschema.Schema{})); diff != "" {
|
||||
t.Errorf("expected %v, got %v, (-want +got):\n%s", tc.want, got, diff)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue