|
| 1 | +package converters |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + upstream "github.com/modelcontextprotocol/registry/pkg/api/v0" |
| 10 | + "github.com/stacklok/toolhive/pkg/registry" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// TestConverters_Fixtures validates converter functions using JSON fixture files |
| 16 | +// This provides a clear, maintainable way to test conversions with real-world data |
| 17 | +func TestConverters_Fixtures(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + fixtureDir string |
| 23 | + inputFile string |
| 24 | + expectedFile string |
| 25 | + serverName string |
| 26 | + convertFunc string // "ImageToServer", "ServerToImage", "RemoteToServer", "ServerToRemote" |
| 27 | + validateFunc func(t *testing.T, input, output []byte) |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "ImageMetadata to ServerJSON - GitHub", |
| 31 | + fixtureDir: "testdata/image_to_server", |
| 32 | + inputFile: "input_github.json", |
| 33 | + expectedFile: "expected_github.json", |
| 34 | + serverName: "github", |
| 35 | + convertFunc: "ImageToServer", |
| 36 | + validateFunc: validateImageToServerConversion, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "ServerJSON to ImageMetadata - GitHub", |
| 40 | + fixtureDir: "testdata/server_to_image", |
| 41 | + inputFile: "input_github.json", |
| 42 | + expectedFile: "expected_github.json", |
| 43 | + serverName: "", |
| 44 | + convertFunc: "ServerToImage", |
| 45 | + validateFunc: validateServerToImageConversion, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "RemoteServerMetadata to ServerJSON - Example", |
| 49 | + fixtureDir: "testdata/remote_to_server", |
| 50 | + inputFile: "input_example.json", |
| 51 | + expectedFile: "expected_example.json", |
| 52 | + serverName: "example-remote", |
| 53 | + convertFunc: "RemoteToServer", |
| 54 | + validateFunc: validateRemoteToServerConversion, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "ServerJSON to RemoteServerMetadata - Example", |
| 58 | + fixtureDir: "testdata/server_to_remote", |
| 59 | + inputFile: "input_example.json", |
| 60 | + expectedFile: "expected_example.json", |
| 61 | + serverName: "", |
| 62 | + convertFunc: "ServerToRemote", |
| 63 | + validateFunc: validateServerToRemoteConversion, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tt := range tests { |
| 68 | + t.Run(tt.name, func(t *testing.T) { |
| 69 | + t.Parallel() |
| 70 | + |
| 71 | + // Read input fixture |
| 72 | + inputPath := filepath.Join(tt.fixtureDir, tt.inputFile) |
| 73 | + inputData, err := os.ReadFile(inputPath) |
| 74 | + require.NoError(t, err, "Failed to read input fixture: %s", inputPath) |
| 75 | + |
| 76 | + // Read expected output fixture |
| 77 | + expectedPath := filepath.Join(tt.fixtureDir, tt.expectedFile) |
| 78 | + expectedData, err := os.ReadFile(expectedPath) |
| 79 | + require.NoError(t, err, "Failed to read expected fixture: %s", expectedPath) |
| 80 | + |
| 81 | + // Perform conversion based on type |
| 82 | + var actualData []byte |
| 83 | + switch tt.convertFunc { |
| 84 | + case "ImageToServer": |
| 85 | + actualData = convertImageToServer(t, inputData, tt.serverName) |
| 86 | + case "ServerToImage": |
| 87 | + actualData = convertServerToImage(t, inputData) |
| 88 | + case "RemoteToServer": |
| 89 | + actualData = convertRemoteToServer(t, inputData, tt.serverName) |
| 90 | + case "ServerToRemote": |
| 91 | + actualData = convertServerToRemote(t, inputData) |
| 92 | + default: |
| 93 | + t.Fatalf("Unknown conversion function: %s", tt.convertFunc) |
| 94 | + } |
| 95 | + |
| 96 | + // Compare output with expected |
| 97 | + var expected, actual interface{} |
| 98 | + require.NoError(t, json.Unmarshal(expectedData, &expected), "Failed to parse expected JSON") |
| 99 | + require.NoError(t, json.Unmarshal(actualData, &actual), "Failed to parse actual JSON") |
| 100 | + |
| 101 | + // Deep equal comparison |
| 102 | + assert.Equal(t, expected, actual, "Conversion output doesn't match expected fixture") |
| 103 | + |
| 104 | + // Run additional validation if provided |
| 105 | + if tt.validateFunc != nil { |
| 106 | + tt.validateFunc(t, inputData, actualData) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// Helper functions for conversions |
| 113 | + |
| 114 | +func convertImageToServer(t *testing.T, inputData []byte, serverName string) []byte { |
| 115 | + t.Helper() |
| 116 | + var imageMetadata registry.ImageMetadata |
| 117 | + require.NoError(t, json.Unmarshal(inputData, &imageMetadata)) |
| 118 | + |
| 119 | + serverJSON, err := ImageMetadataToServerJSON(serverName, &imageMetadata) |
| 120 | + require.NoError(t, err) |
| 121 | + |
| 122 | + output, err := json.MarshalIndent(serverJSON, "", " ") |
| 123 | + require.NoError(t, err) |
| 124 | + return output |
| 125 | +} |
| 126 | + |
| 127 | +func convertServerToImage(t *testing.T, inputData []byte) []byte { |
| 128 | + t.Helper() |
| 129 | + var serverJSON upstream.ServerJSON |
| 130 | + require.NoError(t, json.Unmarshal(inputData, &serverJSON)) |
| 131 | + |
| 132 | + imageMetadata, err := ServerJSONToImageMetadata(&serverJSON) |
| 133 | + require.NoError(t, err) |
| 134 | + |
| 135 | + output, err := json.MarshalIndent(imageMetadata, "", " ") |
| 136 | + require.NoError(t, err) |
| 137 | + return output |
| 138 | +} |
| 139 | + |
| 140 | +func convertRemoteToServer(t *testing.T, inputData []byte, serverName string) []byte { |
| 141 | + t.Helper() |
| 142 | + var remoteMetadata registry.RemoteServerMetadata |
| 143 | + require.NoError(t, json.Unmarshal(inputData, &remoteMetadata)) |
| 144 | + |
| 145 | + serverJSON, err := RemoteServerMetadataToServerJSON(serverName, &remoteMetadata) |
| 146 | + require.NoError(t, err) |
| 147 | + |
| 148 | + output, err := json.MarshalIndent(serverJSON, "", " ") |
| 149 | + require.NoError(t, err) |
| 150 | + return output |
| 151 | +} |
| 152 | + |
| 153 | +func convertServerToRemote(t *testing.T, inputData []byte) []byte { |
| 154 | + t.Helper() |
| 155 | + var serverJSON upstream.ServerJSON |
| 156 | + require.NoError(t, json.Unmarshal(inputData, &serverJSON)) |
| 157 | + |
| 158 | + remoteMetadata, err := ServerJSONToRemoteServerMetadata(&serverJSON) |
| 159 | + require.NoError(t, err) |
| 160 | + |
| 161 | + output, err := json.MarshalIndent(remoteMetadata, "", " ") |
| 162 | + require.NoError(t, err) |
| 163 | + return output |
| 164 | +} |
| 165 | + |
| 166 | +// Validation functions - additional checks beyond JSON equality |
| 167 | + |
| 168 | +func validateImageToServerConversion(t *testing.T, inputData, outputData []byte) { |
| 169 | + t.Helper() |
| 170 | + var input registry.ImageMetadata |
| 171 | + var output upstream.ServerJSON |
| 172 | + |
| 173 | + require.NoError(t, json.Unmarshal(inputData, &input)) |
| 174 | + require.NoError(t, json.Unmarshal(outputData, &output)) |
| 175 | + |
| 176 | + // Verify core mappings |
| 177 | + assert.Equal(t, input.Description, output.Description, "Description should match") |
| 178 | + assert.Len(t, output.Packages, 1, "Should have exactly one package") |
| 179 | + assert.Equal(t, input.Image, output.Packages[0].Identifier, "Image identifier should match") |
| 180 | + assert.Equal(t, input.Transport, output.Packages[0].Transport.Type, "Transport type should match") |
| 181 | + |
| 182 | + // Verify environment variables count |
| 183 | + assert.Len(t, output.Packages[0].EnvironmentVariables, len(input.EnvVars), |
| 184 | + "Environment variables count should match") |
| 185 | + |
| 186 | + // Verify publisher extensions exist |
| 187 | + require.NotNil(t, output.Meta, "Meta should not be nil") |
| 188 | + require.NotNil(t, output.Meta.PublisherProvided, "PublisherProvided should not be nil") |
| 189 | + |
| 190 | + stacklokData, ok := output.Meta.PublisherProvided["io.github.stacklok"].(map[string]interface{}) |
| 191 | + require.True(t, ok, "Should have io.github.stacklok namespace") |
| 192 | + |
| 193 | + extensions, ok := stacklokData[input.Image].(map[string]interface{}) |
| 194 | + require.True(t, ok, "Should have image-specific extensions") |
| 195 | + |
| 196 | + // Verify key extension fields |
| 197 | + assert.Equal(t, input.Status, extensions["status"], "Status should be in extensions") |
| 198 | + assert.Equal(t, input.Tier, extensions["tier"], "Tier should be in extensions") |
| 199 | + assert.NotNil(t, extensions["tools"], "Tools should be in extensions") |
| 200 | + assert.NotNil(t, extensions["tags"], "Tags should be in extensions") |
| 201 | +} |
| 202 | + |
| 203 | +func validateServerToImageConversion(t *testing.T, inputData, outputData []byte) { |
| 204 | + t.Helper() |
| 205 | + var input upstream.ServerJSON |
| 206 | + var output registry.ImageMetadata |
| 207 | + |
| 208 | + require.NoError(t, json.Unmarshal(inputData, &input)) |
| 209 | + require.NoError(t, json.Unmarshal(outputData, &output)) |
| 210 | + |
| 211 | + // Verify core mappings |
| 212 | + assert.Equal(t, input.Description, output.Description, "Description should match") |
| 213 | + require.Len(t, input.Packages, 1, "Input should have exactly one package") |
| 214 | + assert.Equal(t, input.Packages[0].Identifier, output.Image, "Image identifier should match") |
| 215 | + assert.Equal(t, input.Packages[0].Transport.Type, output.Transport, "Transport type should match") |
| 216 | + |
| 217 | + // Verify environment variables were extracted |
| 218 | + assert.Len(t, output.EnvVars, len(input.Packages[0].EnvironmentVariables), |
| 219 | + "Environment variables count should match") |
| 220 | +} |
| 221 | + |
| 222 | +func validateRemoteToServerConversion(t *testing.T, inputData, outputData []byte) { |
| 223 | + t.Helper() |
| 224 | + var input registry.RemoteServerMetadata |
| 225 | + var output upstream.ServerJSON |
| 226 | + |
| 227 | + require.NoError(t, json.Unmarshal(inputData, &input)) |
| 228 | + require.NoError(t, json.Unmarshal(outputData, &output)) |
| 229 | + |
| 230 | + // Verify core mappings |
| 231 | + assert.Equal(t, input.Description, output.Description, "Description should match") |
| 232 | + require.Len(t, output.Remotes, 1, "Should have exactly one remote") |
| 233 | + assert.Equal(t, input.URL, output.Remotes[0].URL, "Remote URL should match") |
| 234 | + assert.Equal(t, input.Transport, output.Remotes[0].Type, "Transport type should match") |
| 235 | + |
| 236 | + // Verify headers count |
| 237 | + assert.Len(t, output.Remotes[0].Headers, len(input.Headers), |
| 238 | + "Headers count should match") |
| 239 | +} |
| 240 | + |
| 241 | +func validateServerToRemoteConversion(t *testing.T, inputData, outputData []byte) { |
| 242 | + t.Helper() |
| 243 | + var input upstream.ServerJSON |
| 244 | + var output registry.RemoteServerMetadata |
| 245 | + |
| 246 | + require.NoError(t, json.Unmarshal(inputData, &input)) |
| 247 | + require.NoError(t, json.Unmarshal(outputData, &output)) |
| 248 | + |
| 249 | + // Verify core mappings |
| 250 | + assert.Equal(t, input.Description, output.Description, "Description should match") |
| 251 | + require.Len(t, input.Remotes, 1, "Input should have exactly one remote") |
| 252 | + assert.Equal(t, input.Remotes[0].URL, output.URL, "Remote URL should match") |
| 253 | + assert.Equal(t, input.Remotes[0].Type, output.Transport, "Transport type should match") |
| 254 | + |
| 255 | + // Verify headers were extracted |
| 256 | + assert.Len(t, output.Headers, len(input.Remotes[0].Headers), |
| 257 | + "Headers count should match") |
| 258 | +} |
0 commit comments