MCPcopy Create free account
hub / github.com/coder/aibridge / TestAnthropicInjectedTools

Function TestAnthropicInjectedTools

internal/integrationtest/bridge_test.go:998–1080  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

996}
997
998func TestAnthropicInjectedTools(t *testing.T) {
999 t.Parallel()
1000
1001 for _, streaming := range []bool{true, false} {
1002 t.Run(fmt.Sprintf("streaming=%v", streaming), func(t *testing.T) {
1003 t.Parallel()
1004
1005 // Build the requirements & make the assertions which are common to all providers.
1006 bridgeServer, mockMCP, resp := setupInjectedToolTest(t, fixtures.AntSingleInjectedTool, streaming, defaultTracer, pathAnthropicMessages, anthropicToolResultValidator(t))
1007 defer resp.Body.Close()
1008
1009 // Ensure expected tool was invoked with expected input.
1010 toolUsages := bridgeServer.Recorder.RecordedToolUsages()
1011 require.Len(t, toolUsages, 1)
1012 require.Equal(t, mockToolName, toolUsages[0].Tool)
1013 expected, err := json.Marshal(map[string]any{"owner": "admin"})
1014 require.NoError(t, err)
1015 actual, err := json.Marshal(toolUsages[0].Args)
1016 require.NoError(t, err)
1017 require.EqualValues(t, expected, actual)
1018 invocations := mockMCP.getCallsByTool(mockToolName)
1019 require.Len(t, invocations, 1)
1020 actual, err = json.Marshal(invocations[0])
1021 require.NoError(t, err)
1022 require.EqualValues(t, expected, actual)
1023
1024 var (
1025 content *anthropic.ContentBlockUnion
1026 message anthropic.Message
1027 )
1028 if streaming {
1029 // Parse the response stream.
1030 decoder := ssestream.NewDecoder(resp)
1031 stream := ssestream.NewStream[anthropic.MessageStreamEventUnion](decoder, nil)
1032 for stream.Next() {
1033 event := stream.Current()
1034 require.NoError(t, message.Accumulate(event), "accumulate event")
1035 }
1036
1037 require.NoError(t, stream.Err(), "stream error")
1038 require.Len(t, message.Content, 2)
1039
1040 content = &message.Content[1]
1041 } else {
1042 // Parse & unmarshal the response.
1043 body, err := io.ReadAll(resp.Body)
1044 require.NoError(t, err, "read response body")
1045
1046 require.NoError(t, json.Unmarshal(body, &message), "unmarshal response")
1047 require.GreaterOrEqual(t, len(message.Content), 1)
1048
1049 content = &message.Content[0]
1050 }
1051
1052 // Ensure tool returned expected value.
1053 require.NotNil(t, content)
1054 require.Contains(t, content.Text, "dd711d5c-83c6-4c08-a0af-b73055906e8c") // The ID of the workspace to be returned.
1055

Callers

nothing calls this directly

Calls 8

setupInjectedToolTestFunction · 0.85
RecordedToolUsagesMethod · 0.80
TotalInputTokensMethod · 0.80
TotalOutputTokensMethod · 0.80
RecordedPromptUsagesMethod · 0.80
CloseMethod · 0.45
getCallsByToolMethod · 0.45

Tested by

no test coverage detected