Skip to content

Commit da8da48

Browse files
committed
fix(server/emulator): normalize mocks once
1 parent 0ad6b6f commit da8da48

2 files changed

Lines changed: 38 additions & 23 deletions

File tree

‎internal/server/emulator.go‎

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,24 @@ func loadEmulatorMocks(path string) (map[string][]emulatorMock, error) {
5656
}
5757

5858
mocks := make(map[string][]emulatorMock)
59-
for i, m := range file.Mocks {
59+
for i := range file.Mocks {
60+
m := &file.Mocks[i]
6061
if m.ToolName == "" {
6162
return nil, fmt.Errorf("invalid emulator mock at index %d: tool_name is required", i)
6263
}
6364
if m.Parameters == nil {
6465
m.Parameters = map[string]any{}
6566
}
66-
mocks[m.ToolName] = append(mocks[m.ToolName], m)
67+
normalized, err := normalizeJSONValue(m.Parameters)
68+
if err != nil {
69+
return nil, fmt.Errorf("failed to normalize parameters for mock at index %d: %w", i, err)
70+
}
71+
normalizedMap, ok := normalized.(map[string]any)
72+
if !ok {
73+
return nil, fmt.Errorf("failed to normalize parameters for mock at index %d: expected object parameters", i)
74+
}
75+
m.Parameters = normalizedMap
76+
mocks[m.ToolName] = append(mocks[m.ToolName], *m)
6777
}
6878

6979
return mocks, nil
@@ -93,37 +103,28 @@ func normalizeJSONValue(v any) (any, error) {
93103
return normalized, nil
94104
}
95105

96-
func (e emulatorTool) findMock(params parameters.ParamValues) (any, bool) {
97-
in, err := normalizeJSONValue(params.AsMap())
98-
if err != nil {
99-
return nil, false
100-
}
101-
inMap, ok := in.(map[string]any)
102-
if !ok {
103-
return nil, false
104-
}
105-
106+
func (e emulatorTool) findMock(inMap map[string]any) (any, bool) {
106107
for _, mock := range e.mocks {
107-
expected, err := normalizeJSONValue(mock.Parameters)
108-
if err != nil {
109-
continue
110-
}
111-
expectedMap, ok := expected.(map[string]any)
112-
if !ok {
113-
continue
114-
}
115-
if reflect.DeepEqual(inMap, expectedMap) {
108+
if reflect.DeepEqual(inMap, mock.Parameters) {
116109
return mock.Response, true
117110
}
118111
}
119112
return nil, false
120113
}
121114

122115
func (e emulatorTool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
123-
if response, found := e.findMock(params); found {
116+
in, err := normalizeJSONValue(params.AsMap())
117+
if err != nil {
118+
return nil, util.NewAgentError("emulator mode: failed to normalize input parameters", err)
119+
}
120+
inMap, ok := in.(map[string]any)
121+
if !ok {
122+
return nil, util.NewAgentError("emulator mode: failed to normalize input parameters", nil)
123+
}
124+
if response, found := e.findMock(inMap); found {
124125
return response, nil
125126
}
126-
return nil, util.NewAgentError(fmt.Sprintf("emulator mode: no mock matched for tool %q with parameters %v", e.name, params.AsMap()), nil)
127+
return nil, util.NewAgentError(fmt.Sprintf("emulator mode: no mock matched for tool %q with parameters %v", e.name, inMap), nil)
127128
}
128129

129130
func (e emulatorTool) EmbedParams(ctx context.Context, params parameters.ParamValues, embeddingModels map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {

‎internal/server/emulator_test.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package server
216

317
import (

0 commit comments

Comments
 (0)