-
Notifications
You must be signed in to change notification settings - Fork 30
[test-improver] Improve tests for registerPromptsFromBackend #8307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lpcox
merged 4 commits into
main
from
test-improver/register-prompts-coverage-9e2d79a510ab9a3a
Jun 30, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/github/gh-aw-mcpg/internal/config" | ||
| "github.com/github/gh-aw-mcpg/internal/launcher" | ||
| "github.com/github/gh-aw-mcpg/internal/mcp" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // newStreamableBackendWithPromptsCapability creates an httptest.Server that speaks the | ||
| // streamable HTTP MCP protocol and declares prompts capability in its initialize response. | ||
| // The onPromptsList callback is invoked for each prompts/list request and receives the | ||
| // http.ResponseWriter and the JSON-RPC request-ID so the caller can write the desired | ||
| // response. | ||
| func newStreamableBackendWithPromptsCapability( | ||
| t *testing.T, | ||
| onPromptsList func(w http.ResponseWriter, reqID interface{}), | ||
| ) *httptest.Server { | ||
| t.Helper() | ||
| return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodPost { | ||
| w.WriteHeader(http.StatusMethodNotAllowed) | ||
| return | ||
| } | ||
|
|
||
| body, err := io.ReadAll(r.Body) | ||
| if err != nil || len(body) == 0 { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| var req map[string]interface{} | ||
| if err := json.Unmarshal(body, &req); err != nil { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| method, _ := req["method"].(string) | ||
| switch method { | ||
| case "initialize": | ||
| w.Header().Set("Content-Type", "application/json") | ||
| w.Header().Set("Mcp-Session-Id", "test-prompts-session") | ||
| _ = json.NewEncoder(w).Encode(map[string]interface{}{ | ||
| "jsonrpc": "2.0", | ||
| "id": req["id"], | ||
| "result": map[string]interface{}{ | ||
| "protocolVersion": "2024-11-05", | ||
| "capabilities": map[string]interface{}{ | ||
| "prompts": map[string]interface{}{}, | ||
| }, | ||
| "serverInfo": map[string]interface{}{ | ||
| "name": "prompts-capable-backend", | ||
| "version": "1.0.0", | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| case "notifications/initialized": | ||
| w.WriteHeader(http.StatusAccepted) | ||
|
|
||
| case "prompts/list": | ||
| onPromptsList(w, req["id"]) | ||
|
|
||
| default: | ||
| w.WriteHeader(http.StatusNotFound) | ||
| } | ||
| })) | ||
| } | ||
|
|
||
| // connectStreamableBackend establishes a launcher connection to the given server | ||
| // and verifies that the resulting connection declares prompts capability. | ||
| func connectStreamableBackend(t *testing.T, srv *httptest.Server) *mcp.Connection { | ||
| t.Helper() | ||
|
|
||
| cfg := &config.Config{ | ||
| Servers: map[string]*config.ServerConfig{ | ||
| "prompts-server": {Type: "http", URL: srv.URL}, | ||
| }, | ||
| } | ||
|
|
||
| l := launcher.New(context.Background(), cfg) | ||
| t.Cleanup(func() { l.Close() }) | ||
|
|
||
| conn, err := launcher.GetOrLaunch(l, "prompts-server") | ||
| require.NoError(t, err, "GetOrLaunch should succeed for streamable backend") | ||
| require.NotNil(t, conn, "connection should not be nil") | ||
| require.True(t, conn.BackendHasPromptsCapability(), | ||
| "connection to a backend that returned capabilities.prompts should report prompts capability") | ||
| return conn | ||
| } | ||
|
|
||
| // minimalPromptsTestServer constructs just enough of a UnifiedServer for tests that call | ||
| // registerPromptsFromBackend directly. Only the sdk.Server field is required during | ||
| // prompt registration; the launcher and other fields are only needed when the registered | ||
| // prompt handler is actually invoked. | ||
| func minimalPromptsTestServer(t *testing.T) *UnifiedServer { | ||
| t.Helper() | ||
| return &UnifiedServer{ | ||
| server: newSDKServer("test-prompts", logUnified), | ||
| } | ||
| } | ||
|
|
||
| // TestRegisterPromptsFromBackend_NoCapability verifies that a backend without prompts | ||
| // capability causes registerPromptsFromBackend to return immediately without error. | ||
| // This exercises the !BackendHasPromptsCapability() early-return branch. | ||
| func TestRegisterPromptsFromBackend_NoCapability(t *testing.T) { | ||
| // Plain-JSON-RPC backend: no Mcp-Session-Id header → no SDK session → BackendHasPromptsCapability() == false | ||
| plainBackend := newMockBackend(t, "no-prompts-backend", []string{"some_tool"}) | ||
| defer plainBackend.Close() | ||
|
|
||
| cfg := &config.Config{ | ||
| Servers: map[string]*config.ServerConfig{ | ||
| "no-prompts-server": {Type: "http", URL: plainBackend.URL}, | ||
| }, | ||
| } | ||
| l := launcher.New(context.Background(), cfg) | ||
| defer l.Close() | ||
|
|
||
| conn, err := launcher.GetOrLaunch(l, "no-prompts-server") | ||
| require.NoError(t, err) | ||
| assert.False(t, conn.BackendHasPromptsCapability(), | ||
| "plain-JSON-RPC connection should not declare prompts capability") | ||
|
|
||
| us := minimalPromptsTestServer(t) | ||
| err = us.registerPromptsFromBackend(context.Background(), "no-prompts-server", conn) | ||
| assert.NoError(t, err, "should return nil when backend has no prompts capability") | ||
| } | ||
|
|
||
| // TestRegisterPromptsFromBackend_RequestError verifies graceful handling when the backend | ||
| // returns an HTTP error for prompts/list. The function should swallow the error (non-fatal) | ||
| // and return nil. | ||
| func TestRegisterPromptsFromBackend_RequestError(t *testing.T) { | ||
| promptsListCalled := make(chan struct{}, 10) | ||
| t.Cleanup(func() { | ||
| assert.Equal(t, 1, len(promptsListCalled), "expected prompts/list to be called exactly once") | ||
| }) | ||
|
|
||
| srv := newStreamableBackendWithPromptsCapability(t, func(w http.ResponseWriter, _ interface{}) { | ||
| promptsListCalled <- struct{}{} | ||
| // Return HTTP 500 → SDK treats this as a request-level failure | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| }) | ||
|
|
||
| conn := connectStreamableBackend(t, srv) | ||
| us := minimalPromptsTestServer(t) | ||
|
|
||
| err := us.registerPromptsFromBackend(context.Background(), "prompts-server", conn) | ||
| assert.NoError(t, err, "request error should be treated as a graceful skip, not a fatal error") | ||
| } | ||
|
|
||
| // TestRegisterPromptsFromBackend_EmptyPromptsList verifies that a backend with prompts | ||
| // capability that returns an empty prompts/list causes registerPromptsFromBackend to | ||
| // return nil without registering anything. | ||
| func TestRegisterPromptsFromBackend_EmptyPromptsList(t *testing.T) { | ||
| promptsListCalled := make(chan struct{}, 10) | ||
| t.Cleanup(func() { | ||
| assert.Equal(t, 1, len(promptsListCalled), "expected prompts/list to be called exactly once") | ||
| }) | ||
|
|
||
| srv := newStreamableBackendWithPromptsCapability(t, func(w http.ResponseWriter, reqID interface{}) { | ||
| promptsListCalled <- struct{}{} | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _ = json.NewEncoder(w).Encode(map[string]interface{}{ | ||
| "jsonrpc": "2.0", | ||
| "id": reqID, | ||
| "result": map[string]interface{}{ | ||
| "prompts": []interface{}{}, | ||
| }, | ||
| }) | ||
| }) | ||
| defer srv.Close() | ||
|
|
||
| conn := connectStreamableBackend(t, srv) | ||
| us := minimalPromptsTestServer(t) | ||
|
|
||
| err := us.registerPromptsFromBackend(context.Background(), "prompts-server", conn) | ||
| assert.NoError(t, err, "empty prompts list should return nil without registering anything") | ||
| } | ||
|
|
||
| // TestRegisterPromptsFromBackend_RegistersPrompts verifies that prompts returned by the | ||
| // backend are registered on the unified server. The function should return nil and the | ||
| // SDK server should have the prompt added. | ||
| func TestRegisterPromptsFromBackend_RegistersPrompts(t *testing.T) { | ||
| promptsListCalled := make(chan struct{}, 10) | ||
| t.Cleanup(func() { | ||
| assert.Equal(t, 1, len(promptsListCalled), "expected prompts/list to be called exactly once") | ||
| }) | ||
|
|
||
| srv := newStreamableBackendWithPromptsCapability(t, func(w http.ResponseWriter, reqID interface{}) { | ||
| promptsListCalled <- struct{}{} | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _ = json.NewEncoder(w).Encode(map[string]interface{}{ | ||
| "jsonrpc": "2.0", | ||
| "id": reqID, | ||
| "result": map[string]interface{}{ | ||
| "prompts": []map[string]interface{}{ | ||
| { | ||
| "name": "summarize", | ||
| "description": "Summarizes the given text", | ||
| }, | ||
| { | ||
| "name": "translate", | ||
| "description": "Translates text to another language", | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
|
Copilot marked this conversation as resolved.
|
||
| defer srv.Close() | ||
|
|
||
| conn := connectStreamableBackend(t, srv) | ||
| us := minimalPromptsTestServer(t) | ||
|
|
||
| err := us.registerPromptsFromBackend(context.Background(), "prompts-server", conn) | ||
| assert.NoError(t, err, "should return nil when prompts are successfully registered") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.