Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 9 | "testing" |
| 10 | |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 11 | tmconfig "github.com/KorAP/KoralPipe-TermMapper/config" |
Akron | fa55bb2 | 2025-05-26 15:10:42 +0200 | [diff] [blame] | 12 | "github.com/KorAP/KoralPipe-TermMapper/mapper" |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 13 | "github.com/gofiber/fiber/v2" |
| 14 | "github.com/stretchr/testify/assert" |
| 15 | "github.com/stretchr/testify/require" |
| 16 | ) |
| 17 | |
| 18 | func TestTransformEndpoint(t *testing.T) { |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 19 | // Create test mapping list |
| 20 | mappingList := tmconfig.MappingList{ |
| 21 | ID: "test-mapper", |
| 22 | FoundryA: "opennlp", |
| 23 | LayerA: "p", |
| 24 | FoundryB: "upos", |
| 25 | LayerB: "p", |
| 26 | Mappings: []tmconfig.MappingRule{ |
| 27 | "[PIDAT] <> [opennlp/p=PIDAT & opennlp/p=AdjType:Pdt]", |
| 28 | "[DET] <> [opennlp/p=DET]", |
| 29 | }, |
| 30 | } |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 31 | |
| 32 | // Create mapper |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 33 | m, err := mapper.NewMapper([]tmconfig.MappingList{mappingList}) |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 34 | require.NoError(t, err) |
| 35 | |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 36 | // Create mock config for testing |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 37 | mockConfig := &tmconfig.MappingConfig{ |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 38 | Lists: []tmconfig.MappingList{mappingList}, |
| 39 | } |
| 40 | |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 41 | // Create fiber app |
| 42 | app := fiber.New() |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 43 | setupRoutes(app, m, mockConfig) |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 44 | |
| 45 | tests := []struct { |
| 46 | name string |
| 47 | mapID string |
| 48 | direction string |
| 49 | foundryA string |
| 50 | foundryB string |
| 51 | layerA string |
| 52 | layerB string |
| 53 | input string |
| 54 | expectedCode int |
| 55 | expectedBody string |
| 56 | expectedError string |
| 57 | }{ |
| 58 | { |
| 59 | name: "Simple A to B mapping", |
| 60 | mapID: "test-mapper", |
| 61 | direction: "atob", |
| 62 | input: `{ |
| 63 | "@type": "koral:token", |
| 64 | "wrap": { |
| 65 | "@type": "koral:term", |
| 66 | "foundry": "opennlp", |
| 67 | "key": "PIDAT", |
| 68 | "layer": "p", |
| 69 | "match": "match:eq" |
| 70 | } |
| 71 | }`, |
| 72 | expectedCode: http.StatusOK, |
| 73 | expectedBody: `{ |
| 74 | "@type": "koral:token", |
| 75 | "wrap": { |
| 76 | "@type": "koral:termGroup", |
| 77 | "operands": [ |
| 78 | { |
| 79 | "@type": "koral:term", |
| 80 | "foundry": "opennlp", |
| 81 | "key": "PIDAT", |
| 82 | "layer": "p", |
| 83 | "match": "match:eq" |
| 84 | }, |
| 85 | { |
| 86 | "@type": "koral:term", |
| 87 | "foundry": "opennlp", |
| 88 | "key": "AdjType", |
| 89 | "layer": "p", |
| 90 | "match": "match:eq", |
| 91 | "value": "Pdt" |
| 92 | } |
| 93 | ], |
| 94 | "relation": "relation:and" |
| 95 | } |
| 96 | }`, |
| 97 | }, |
| 98 | { |
| 99 | name: "B to A mapping", |
| 100 | mapID: "test-mapper", |
| 101 | direction: "btoa", |
| 102 | input: `{ |
| 103 | "@type": "koral:token", |
| 104 | "wrap": { |
| 105 | "@type": "koral:termGroup", |
| 106 | "operands": [ |
| 107 | { |
| 108 | "@type": "koral:term", |
| 109 | "foundry": "opennlp", |
| 110 | "key": "PIDAT", |
| 111 | "layer": "p", |
| 112 | "match": "match:eq" |
| 113 | }, |
| 114 | { |
| 115 | "@type": "koral:term", |
| 116 | "foundry": "opennlp", |
| 117 | "key": "AdjType", |
| 118 | "layer": "p", |
| 119 | "match": "match:eq", |
| 120 | "value": "Pdt" |
| 121 | } |
| 122 | ], |
| 123 | "relation": "relation:and" |
| 124 | } |
| 125 | }`, |
| 126 | expectedCode: http.StatusOK, |
| 127 | expectedBody: `{ |
| 128 | "@type": "koral:token", |
| 129 | "wrap": { |
| 130 | "@type": "koral:term", |
| 131 | "foundry": "opennlp", |
| 132 | "key": "PIDAT", |
| 133 | "layer": "p", |
| 134 | "match": "match:eq" |
| 135 | } |
| 136 | }`, |
| 137 | }, |
| 138 | { |
| 139 | name: "Mapping with foundry override", |
| 140 | mapID: "test-mapper", |
| 141 | direction: "atob", |
| 142 | foundryB: "custom", |
| 143 | input: `{ |
| 144 | "@type": "koral:token", |
| 145 | "wrap": { |
| 146 | "@type": "koral:term", |
| 147 | "foundry": "opennlp", |
| 148 | "key": "PIDAT", |
| 149 | "layer": "p", |
| 150 | "match": "match:eq" |
| 151 | } |
| 152 | }`, |
| 153 | expectedCode: http.StatusOK, |
| 154 | expectedBody: `{ |
| 155 | "@type": "koral:token", |
| 156 | "wrap": { |
| 157 | "@type": "koral:termGroup", |
| 158 | "operands": [ |
| 159 | { |
| 160 | "@type": "koral:term", |
| 161 | "foundry": "custom", |
| 162 | "key": "PIDAT", |
| 163 | "layer": "p", |
| 164 | "match": "match:eq" |
| 165 | }, |
| 166 | { |
| 167 | "@type": "koral:term", |
| 168 | "foundry": "custom", |
| 169 | "key": "AdjType", |
| 170 | "layer": "p", |
| 171 | "match": "match:eq", |
| 172 | "value": "Pdt" |
| 173 | } |
| 174 | ], |
| 175 | "relation": "relation:and" |
| 176 | } |
| 177 | }`, |
| 178 | }, |
| 179 | { |
| 180 | name: "Invalid mapping ID", |
| 181 | mapID: "nonexistent", |
| 182 | direction: "atob", |
| 183 | input: `{"@type": "koral:token"}`, |
| 184 | expectedCode: http.StatusInternalServerError, |
| 185 | expectedError: "mapping list with ID nonexistent not found", |
| 186 | }, |
| 187 | { |
| 188 | name: "Invalid direction", |
| 189 | mapID: "test-mapper", |
| 190 | direction: "invalid", |
| 191 | input: `{"@type": "koral:token"}`, |
| 192 | expectedCode: http.StatusBadRequest, |
| 193 | expectedError: "invalid direction, must be 'atob' or 'btoa'", |
| 194 | }, |
| 195 | { |
| 196 | name: "Invalid JSON", |
| 197 | mapID: "test-mapper", |
| 198 | direction: "atob", |
| 199 | input: `invalid json`, |
| 200 | expectedCode: http.StatusBadRequest, |
| 201 | expectedError: "invalid JSON in request body", |
| 202 | }, |
| 203 | } |
| 204 | |
| 205 | for _, tt := range tests { |
| 206 | t.Run(tt.name, func(t *testing.T) { |
| 207 | // Build URL with query parameters |
| 208 | url := "/" + tt.mapID + "/query" |
| 209 | if tt.direction != "" { |
| 210 | url += "?dir=" + tt.direction |
| 211 | } |
| 212 | if tt.foundryA != "" { |
| 213 | url += "&foundryA=" + tt.foundryA |
| 214 | } |
| 215 | if tt.foundryB != "" { |
| 216 | url += "&foundryB=" + tt.foundryB |
| 217 | } |
| 218 | if tt.layerA != "" { |
| 219 | url += "&layerA=" + tt.layerA |
| 220 | } |
| 221 | if tt.layerB != "" { |
| 222 | url += "&layerB=" + tt.layerB |
| 223 | } |
| 224 | |
| 225 | // Make request |
| 226 | req := httptest.NewRequest(http.MethodPost, url, bytes.NewBufferString(tt.input)) |
| 227 | req.Header.Set("Content-Type", "application/json") |
| 228 | resp, err := app.Test(req) |
| 229 | require.NoError(t, err) |
| 230 | defer resp.Body.Close() |
| 231 | |
| 232 | // Check status code |
| 233 | assert.Equal(t, tt.expectedCode, resp.StatusCode) |
| 234 | |
| 235 | // Read response body |
| 236 | body, err := io.ReadAll(resp.Body) |
| 237 | require.NoError(t, err) |
| 238 | |
| 239 | if tt.expectedError != "" { |
| 240 | // Check error message |
| 241 | var errResp fiber.Map |
| 242 | err = json.Unmarshal(body, &errResp) |
| 243 | require.NoError(t, err) |
| 244 | assert.Equal(t, tt.expectedError, errResp["error"]) |
| 245 | } else { |
| 246 | // Compare JSON responses |
Akron | 121c66e | 2025-06-02 16:34:05 +0200 | [diff] [blame] | 247 | var expected, actual any |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 248 | err = json.Unmarshal([]byte(tt.expectedBody), &expected) |
| 249 | require.NoError(t, err) |
| 250 | err = json.Unmarshal(body, &actual) |
| 251 | require.NoError(t, err) |
| 252 | assert.Equal(t, expected, actual) |
| 253 | } |
| 254 | }) |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | func TestHealthEndpoint(t *testing.T) { |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 259 | // Create test mapping list |
| 260 | mappingList := tmconfig.MappingList{ |
| 261 | ID: "test-mapper", |
| 262 | Mappings: []tmconfig.MappingRule{ |
| 263 | "[A] <> [B]", |
| 264 | }, |
| 265 | } |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 266 | |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 267 | // Create mapper |
| 268 | m, err := mapper.NewMapper([]tmconfig.MappingList{mappingList}) |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 269 | require.NoError(t, err) |
| 270 | |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 271 | // Create mock config for testing |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 272 | mockConfig := &tmconfig.MappingConfig{ |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 273 | Lists: []tmconfig.MappingList{mappingList}, |
| 274 | } |
| 275 | |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 276 | // Create fiber app |
| 277 | app := fiber.New() |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 278 | setupRoutes(app, m, mockConfig) |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 279 | |
| 280 | // Test health endpoint |
| 281 | req := httptest.NewRequest(http.MethodGet, "/health", nil) |
| 282 | resp, err := app.Test(req) |
| 283 | require.NoError(t, err) |
| 284 | defer resp.Body.Close() |
| 285 | |
| 286 | assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 287 | body, err := io.ReadAll(resp.Body) |
| 288 | require.NoError(t, err) |
| 289 | assert.Equal(t, "OK", string(body)) |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 290 | |
Akron | c471c0a | 2025-06-04 11:56:22 +0200 | [diff] [blame] | 291 | req = httptest.NewRequest(http.MethodGet, "/", nil) |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 292 | resp, err = app.Test(req) |
| 293 | require.NoError(t, err) |
| 294 | defer resp.Body.Close() |
| 295 | |
| 296 | assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 297 | body, err = io.ReadAll(resp.Body) |
| 298 | require.NoError(t, err) |
Akron | fc77b5e | 2025-06-04 11:44:43 +0200 | [diff] [blame] | 299 | assert.Contains(t, string(body), "KoralPipe-TermMapper") |
Akron | 40aaa63 | 2025-06-03 17:57:52 +0200 | [diff] [blame] | 300 | |
Akron | 49ceeb4 | 2025-05-23 17:46:01 +0200 | [diff] [blame] | 301 | } |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 302 | |
| 303 | func TestKalamarPluginWithCustomSdkAndServer(t *testing.T) { |
| 304 | // Create test mapping list |
| 305 | mappingList := tmconfig.MappingList{ |
| 306 | ID: "test-mapper", |
| 307 | Mappings: []tmconfig.MappingRule{ |
| 308 | "[A] <> [B]", |
| 309 | }, |
| 310 | } |
| 311 | |
| 312 | // Create mapper |
| 313 | m, err := mapper.NewMapper([]tmconfig.MappingList{mappingList}) |
| 314 | require.NoError(t, err) |
| 315 | |
| 316 | tests := []struct { |
| 317 | name string |
| 318 | customSDK string |
| 319 | customServer string |
| 320 | expectedSDK string |
| 321 | expectedServer string |
| 322 | }{ |
| 323 | { |
| 324 | name: "Custom SDK and Server values", |
| 325 | customSDK: "https://custom.example.com/custom-sdk.js", |
| 326 | customServer: "https://custom.example.com/", |
| 327 | expectedSDK: "https://custom.example.com/custom-sdk.js", |
| 328 | expectedServer: "https://custom.example.com/", |
| 329 | }, |
| 330 | { |
| 331 | name: "Only custom SDK value", |
| 332 | customSDK: "https://custom.example.com/custom-sdk.js", |
| 333 | customServer: "https://korap.ids-mannheim.de/", // defaults applied during parsing |
| 334 | expectedSDK: "https://custom.example.com/custom-sdk.js", |
| 335 | expectedServer: "https://korap.ids-mannheim.de/", |
| 336 | }, |
| 337 | { |
| 338 | name: "Only custom Server value", |
| 339 | customSDK: "https://korap.ids-mannheim.de/js/korap-plugin-latest.js", // defaults applied during parsing |
| 340 | customServer: "https://custom.example.com/", |
| 341 | expectedSDK: "https://korap.ids-mannheim.de/js/korap-plugin-latest.js", |
| 342 | expectedServer: "https://custom.example.com/", |
| 343 | }, |
| 344 | { |
| 345 | name: "Defaults applied during parsing", |
| 346 | customSDK: "https://korap.ids-mannheim.de/js/korap-plugin-latest.js", // defaults applied during parsing |
| 347 | customServer: "https://korap.ids-mannheim.de/", // defaults applied during parsing |
| 348 | expectedSDK: "https://korap.ids-mannheim.de/js/korap-plugin-latest.js", |
| 349 | expectedServer: "https://korap.ids-mannheim.de/", |
| 350 | }, |
| 351 | } |
| 352 | |
| 353 | for _, tt := range tests { |
| 354 | t.Run(tt.name, func(t *testing.T) { |
| 355 | // Create mock config with custom values |
| 356 | mockConfig := &tmconfig.MappingConfig{ |
| 357 | SDK: tt.customSDK, |
| 358 | Server: tt.customServer, |
| 359 | Lists: []tmconfig.MappingList{mappingList}, |
| 360 | } |
| 361 | |
| 362 | // Create fiber app |
| 363 | app := fiber.New() |
| 364 | setupRoutes(app, m, mockConfig) |
| 365 | |
| 366 | // Test Kalamar plugin endpoint |
| 367 | req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 368 | resp, err := app.Test(req) |
| 369 | require.NoError(t, err) |
| 370 | defer resp.Body.Close() |
| 371 | |
| 372 | assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 373 | body, err := io.ReadAll(resp.Body) |
| 374 | require.NoError(t, err) |
| 375 | |
| 376 | htmlContent := string(body) |
| 377 | |
| 378 | // Check that the HTML contains the expected SDK and Server values |
| 379 | assert.Contains(t, htmlContent, `src="`+tt.expectedSDK+`"`) |
| 380 | assert.Contains(t, htmlContent, `data-server="`+tt.expectedServer+`"`) |
| 381 | |
| 382 | // Ensure it's still a valid HTML page |
| 383 | assert.Contains(t, htmlContent, "KoralPipe-TermMapper") |
| 384 | assert.Contains(t, htmlContent, "<!DOCTYPE html>") |
| 385 | }) |
| 386 | } |
| 387 | } |