|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + ghcontext "github.com/github/github-mcp-server/pkg/context" |
| 8 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestWithOperationID_PreservesRequestIDAndAddsOperationID(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + var capturedRequestID string |
| 17 | + var capturedOperationID string |
| 18 | + handler := withOperationID(func(ctx context.Context, _ string, _ mcp.Request) (mcp.Result, error) { |
| 19 | + var ok bool |
| 20 | + capturedRequestID, ok = ghcontext.RequestID(ctx) |
| 21 | + require.True(t, ok) |
| 22 | + |
| 23 | + capturedOperationID, ok = ghcontext.OperationID(ctx) |
| 24 | + require.True(t, ok) |
| 25 | + return nil, nil |
| 26 | + }) |
| 27 | + |
| 28 | + _, err := handler(ghcontext.WithRequestID(context.Background(), "req_client"), "tools/call", nil) |
| 29 | + require.NoError(t, err) |
| 30 | + |
| 31 | + assert.Equal(t, "req_client", capturedRequestID) |
| 32 | + assert.Regexp(t, `^op_[0-9a-f]+$`, capturedOperationID) |
| 33 | +} |
| 34 | + |
| 35 | +func TestWithOperationID_GeneratesUniqueOperationIDs(t *testing.T) { |
| 36 | + t.Parallel() |
| 37 | + |
| 38 | + var operationIDs []string |
| 39 | + handler := withOperationID(func(ctx context.Context, _ string, _ mcp.Request) (mcp.Result, error) { |
| 40 | + operationID, ok := ghcontext.OperationID(ctx) |
| 41 | + require.True(t, ok) |
| 42 | + operationIDs = append(operationIDs, operationID) |
| 43 | + return nil, nil |
| 44 | + }) |
| 45 | + |
| 46 | + _, err := handler(context.Background(), "tools/call", nil) |
| 47 | + require.NoError(t, err) |
| 48 | + _, err = handler(context.Background(), "tools/call", nil) |
| 49 | + require.NoError(t, err) |
| 50 | + |
| 51 | + require.Len(t, operationIDs, 2) |
| 52 | + assert.NotEqual(t, operationIDs[0], operationIDs[1]) |
| 53 | +} |
0 commit comments