|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/boostsecurityio/poutine/results" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +// TestLightweightMCPResponse tests that the MCP server responses are lightweight |
| 14 | +// and don't include heavy fields like github_actions_workflows |
| 15 | +func TestLightweightMCPResponse(t *testing.T) { |
| 16 | + ctx := context.Background() |
| 17 | + |
| 18 | + analyzer, err := createTestAnalyzer(ctx) |
| 19 | + require.NoError(t, err) |
| 20 | + |
| 21 | + t.Run("analyze_manifest returns lightweight response", func(t *testing.T) { |
| 22 | + request := NewCallToolRequest("analyze_manifest", map[string]interface{}{ |
| 23 | + "content": `name: Test Workflow |
| 24 | +on: push |
| 25 | +jobs: |
| 26 | + test: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + steps: |
| 29 | + - uses: actions/checkout@v4`, |
| 30 | + "manifest_type": "github-actions", |
| 31 | + }) |
| 32 | + |
| 33 | + result, err := handleAnalyzeManifest(ctx, request, analyzer) |
| 34 | + require.NoError(t, err) |
| 35 | + require.NotNil(t, result) |
| 36 | + assert.False(t, result.IsError) |
| 37 | + |
| 38 | + contentText := extractTextFromContent(t, result.Content[0]) |
| 39 | + |
| 40 | + // Verify it doesn't contain heavy fields |
| 41 | + assert.NotContains(t, contentText, "github_actions_workflows") |
| 42 | + assert.NotContains(t, contentText, "github_actions_metadata") |
| 43 | + assert.NotContains(t, contentText, "azure_pipelines") |
| 44 | + assert.NotContains(t, contentText, "gitlabci_configs") |
| 45 | + assert.NotContains(t, contentText, "package_dependencies") |
| 46 | + assert.NotContains(t, contentText, "build_dependencies") |
| 47 | + |
| 48 | + // Verify it contains the essential fields |
| 49 | + assert.Contains(t, contentText, "findings") |
| 50 | + assert.Contains(t, contentText, "rules") |
| 51 | + |
| 52 | + // Parse and verify structure |
| 53 | + var response map[string]interface{} |
| 54 | + err = json.Unmarshal([]byte(contentText), &response) |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + // Should only have findings and rules |
| 58 | + expectedFields := map[string]bool{ |
| 59 | + "findings": true, |
| 60 | + "rules": true, |
| 61 | + } |
| 62 | + |
| 63 | + for key := range response { |
| 64 | + _, expected := expectedFields[key] |
| 65 | + assert.True(t, expected, "Unexpected field '%s' in lightweight response", key) |
| 66 | + } |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +// TestMCPResponseStructure verifies the new mcpAnalysisResponse structure |
| 71 | +func TestMCPResponseStructure(t *testing.T) { |
| 72 | + // Create a sample response to verify JSON marshaling |
| 73 | + response := mcpAnalysisResponse{ |
| 74 | + Findings: []results.Finding{}, |
| 75 | + Rules: map[string]results.Rule{}, |
| 76 | + Purl: "pkg:github/owner/repo@main", |
| 77 | + Repository: "owner/repo", |
| 78 | + ScmType: "github", |
| 79 | + GitRef: "main", |
| 80 | + CommitSha: "abc123", |
| 81 | + LastCommit: "2023-01-01T00:00:00Z", |
| 82 | + } |
| 83 | + |
| 84 | + data, err := json.Marshal(response) |
| 85 | + require.NoError(t, err) |
| 86 | + |
| 87 | + jsonStr := string(data) |
| 88 | + |
| 89 | + // Verify lightweight structure |
| 90 | + assert.Contains(t, jsonStr, "\"findings\":") |
| 91 | + assert.Contains(t, jsonStr, "\"rules\":") |
| 92 | + assert.Contains(t, jsonStr, "\"purl\":") |
| 93 | + assert.Contains(t, jsonStr, "\"repository\":") |
| 94 | + assert.Contains(t, jsonStr, "\"scm_type\":") |
| 95 | + assert.Contains(t, jsonStr, "\"git_ref\":") |
| 96 | + assert.Contains(t, jsonStr, "\"commit_sha\":") |
| 97 | + assert.Contains(t, jsonStr, "\"last_commit\":") |
| 98 | + |
| 99 | + // Verify it doesn't contain heavy fields |
| 100 | + assert.NotContains(t, jsonStr, "github_actions_workflows") |
| 101 | + assert.NotContains(t, jsonStr, "package_dependencies") |
| 102 | + assert.NotContains(t, jsonStr, "org_id") |
| 103 | + assert.NotContains(t, jsonStr, "repo_size") |
| 104 | + assert.NotContains(t, jsonStr, "forks_count") |
| 105 | + assert.NotContains(t, jsonStr, "stars_count") |
| 106 | +} |
| 107 | + |
| 108 | +// TestMCPResponseOmitsEmptyFields verifies that empty optional fields are omitted |
| 109 | +func TestMCPResponseOmitsEmptyFields(t *testing.T) { |
| 110 | + response := mcpAnalysisResponse{ |
| 111 | + Findings: []results.Finding{}, |
| 112 | + Rules: map[string]results.Rule{}, |
| 113 | + // Leave repository metadata fields empty |
| 114 | + } |
| 115 | + |
| 116 | + data, err := json.Marshal(response) |
| 117 | + require.NoError(t, err) |
| 118 | + |
| 119 | + jsonStr := string(data) |
| 120 | + |
| 121 | + // These fields should be omitted when empty due to omitempty tag |
| 122 | + assert.NotContains(t, jsonStr, "\"purl\":") |
| 123 | + assert.NotContains(t, jsonStr, "\"repository\":") |
| 124 | + assert.NotContains(t, jsonStr, "\"scm_type\":") |
| 125 | + assert.NotContains(t, jsonStr, "\"git_ref\":") |
| 126 | + assert.NotContains(t, jsonStr, "\"commit_sha\":") |
| 127 | + assert.NotContains(t, jsonStr, "\"last_commit\":") |
| 128 | + |
| 129 | + // These required fields should always be present |
| 130 | + assert.Contains(t, jsonStr, "\"findings\":") |
| 131 | + assert.Contains(t, jsonStr, "\"rules\":") |
| 132 | +} |
| 133 | + |
| 134 | +// TestMCPResponseSizeReduction demonstrates the size reduction benefit |
| 135 | +func TestMCPResponseSizeReduction(t *testing.T) { |
| 136 | + // This test documents the reduction in response size |
| 137 | + // by comparing what the old response would have contained vs new response |
| 138 | + |
| 139 | + lightweightResponse := mcpAnalysisResponse{ |
| 140 | + Findings: []results.Finding{}, |
| 141 | + Rules: map[string]results.Rule{}, |
| 142 | + Purl: "pkg:github/test/repo@main", |
| 143 | + Repository: "test/repo", |
| 144 | + ScmType: "github", |
| 145 | + GitRef: "main", |
| 146 | + CommitSha: "abc123", |
| 147 | + LastCommit: "2023-01-01T00:00:00Z", |
| 148 | + } |
| 149 | + |
| 150 | + lightweightData, err := json.Marshal(lightweightResponse) |
| 151 | + require.NoError(t, err) |
| 152 | + |
| 153 | + t.Logf("Lightweight response size: %d bytes", len(lightweightData)) |
| 154 | + t.Logf("Lightweight response: %s", string(lightweightData)) |
| 155 | + |
| 156 | + // The lightweight response should be significantly smaller than a full PackageInsights response |
| 157 | + // which would include many more fields like workflows, dependencies, repo stats, etc. |
| 158 | + assert.Less(t, len(lightweightData), 1000, "Lightweight response should be under 1KB for empty findings") |
| 159 | +} |
0 commit comments