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

Function TestSimple

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

Source from the content-addressed store, hash-verified

634}
635
636func TestSimple(t *testing.T) {
637 t.Parallel()
638
639 getAnthropicResponseID := func(streaming bool, resp *http.Response) (string, error) {
640 if streaming {
641 decoder := ssestream.NewDecoder(resp)
642 stream := ssestream.NewStream[anthropic.MessageStreamEventUnion](decoder, nil)
643 var message anthropic.Message
644 for stream.Next() {
645 event := stream.Current()
646 if err := message.Accumulate(event); err != nil {
647 return "", xerrors.Errorf("accumulate event: %w", err)
648 }
649 }
650 if stream.Err() != nil {
651 return "", xerrors.Errorf("stream error: %w", stream.Err())
652 }
653 return message.ID, nil
654 }
655
656 body, err := io.ReadAll(resp.Body)
657 if err != nil {
658 return "", xerrors.Errorf("read body: %w", err)
659 }
660
661 var message anthropic.Message
662 if err := json.Unmarshal(body, &message); err != nil {
663 return "", xerrors.Errorf("unmarshal response: %w", err)
664 }
665 return message.ID, nil
666 }
667
668 getOpenAIResponseID := func(streaming bool, resp *http.Response) (string, error) {
669 if streaming {
670 // Parse the response stream.
671 decoder := oaissestream.NewDecoder(resp)
672 stream := oaissestream.NewStream[openai.ChatCompletionChunk](decoder, nil)
673 var message openai.ChatCompletionAccumulator
674 for stream.Next() {
675 chunk := stream.Current()
676 message.AddChunk(chunk)
677 }
678 if stream.Err() != nil {
679 return "", xerrors.Errorf("stream error: %w", stream.Err())
680 }
681 return message.ID, nil
682 }
683
684 // Parse & unmarshal the response.
685 body, err := io.ReadAll(resp.Body)
686 if err != nil {
687 return "", xerrors.Errorf("read body: %w", err)
688 }
689
690 var message openai.ChatCompletion
691 if err := json.Unmarshal(body, &message); err != nil {
692 return "", xerrors.Errorf("unmarshal response: %w", err)
693 }

Callers

nothing calls this directly

Calls 12

ParseFunction · 0.92
newMockUpstreamFunction · 0.85
newFixtureResponseFunction · 0.85
newBridgeTestServerFunction · 0.85
RequestMethod · 0.80
makeRequestMethod · 0.80
receivedRequestsMethod · 0.80
RecordedPromptUsagesMethod · 0.80
RecordedTokenUsagesMethod · 0.80
RecordedInterceptionsMethod · 0.80
CloseMethod · 0.45

Tested by

no test coverage detected