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

Function TestOpenAIChatCompletions

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

Source from the content-addressed store, hash-verified

470}
471
472func TestOpenAIChatCompletions(t *testing.T) {
473 t.Parallel()
474
475 t.Run("single builtin tool", func(t *testing.T) {
476 t.Parallel()
477
478 cases := []struct {
479 name string
480 streaming bool
481 expectedInputTokens, expectedOutputTokens int
482 expectedToolCallID string
483 }{
484 {
485 name: "streaming",
486 streaming: true,
487 expectedInputTokens: 60,
488 expectedOutputTokens: 15,
489 expectedToolCallID: "call_HjeqP7YeRkoNj0de9e3U4X4B",
490 },
491 {
492 name: "non-streaming",
493 streaming: false,
494 expectedInputTokens: 60,
495 expectedOutputTokens: 15,
496 expectedToolCallID: "call_KjzAbhiZC6nk81tQzL7pwlpc",
497 },
498 }
499
500 for _, tc := range cases {
501 t.Run(tc.name, func(t *testing.T) {
502 t.Parallel()
503
504 ctx, cancel := context.WithTimeout(t.Context(), testutil.WaitLong)
505 t.Cleanup(cancel)
506
507 fix := fixtures.Parse(t, fixtures.OaiChatSingleBuiltinTool)
508 upstream := newMockUpstream(ctx, t, newFixtureResponse(fix))
509
510 bridgeServer := newBridgeTestServer(ctx, t, upstream.URL)
511
512 // Make API call to aibridge for OpenAI /v1/chat/completions
513 reqBody, err := sjson.SetBytes(fix.Request(), "stream", tc.streaming)
514 require.NoError(t, err)
515 resp, err := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIChatCompletions, reqBody)
516 require.NoError(t, err)
517 defer resp.Body.Close()
518 require.Equal(t, http.StatusOK, resp.StatusCode)
519
520 // Response-specific checks.
521 if tc.streaming {
522 sp := aibridge.NewSSEParser()
523 require.NoError(t, sp.Parse(resp.Body))
524
525 // OpenAI sends all events under the same type.
526 messageEvents := sp.MessageEvents()
527 assert.NotEmpty(t, messageEvents)
528
529 // OpenAI streaming ends with [DONE]

Callers

nothing calls this directly

Calls 15

ParseMethod · 0.95
MessageEventsMethod · 0.95
ParseFunction · 0.92
NewSSEParserFunction · 0.92
newMockUpstreamFunction · 0.85
newFixtureResponseFunction · 0.85
newBridgeTestServerFunction · 0.85
newFixtureToolResponseFunction · 0.85
setupMCPForTestFunction · 0.85
withMCPFunction · 0.85
RequestMethod · 0.80
makeRequestMethod · 0.80

Tested by

no test coverage detected