Add configurable Kalamar integration

Change-Id: Ic07423dd7cc605509a364154bf4f37e4c13dc0d1
diff --git a/cmd/termmapper/fuzz_test.go b/cmd/termmapper/fuzz_test.go
index ee5c80d..650a08d 100644
--- a/cmd/termmapper/fuzz_test.go
+++ b/cmd/termmapper/fuzz_test.go
@@ -50,7 +50,7 @@
 	}
 
 	// Create mock config for testing
-	mockConfig := &tmconfig.MappingLists{
+	mockConfig := &tmconfig.MappingConfig{
 		Lists: []tmconfig.MappingList{mappingList},
 	}
 
@@ -164,7 +164,7 @@
 	require.NoError(t, err)
 
 	// Create mock config for testing
-	mockConfig := &tmconfig.MappingLists{
+	mockConfig := &tmconfig.MappingConfig{
 		Lists: []tmconfig.MappingList{mappingList},
 	}
 
diff --git a/cmd/termmapper/main.go b/cmd/termmapper/main.go
index 211dadc..60dc0a0 100644
--- a/cmd/termmapper/main.go
+++ b/cmd/termmapper/main.go
@@ -33,6 +33,8 @@
 	Hash        string
 	Date        string
 	Description string
+	Server      string
+	SDK         string
 	MappingIDs  []string
 }
 
@@ -114,7 +116,7 @@
 	}
 }
 
-func setupRoutes(app *fiber.App, m *mapper.Mapper, yamlConfig *config.MappingLists) {
+func setupRoutes(app *fiber.App, m *mapper.Mapper, yamlConfig *config.MappingConfig) {
 	// Health check endpoint
 	app.Get("/health", func(c *fiber.Ctx) error {
 		return c.SendString("OK")
@@ -224,7 +226,7 @@
 	return nil
 }
 
-func handleKalamarPlugin(yamlConfig *config.MappingLists) fiber.Handler {
+func handleKalamarPlugin(yamlConfig *config.MappingConfig) fiber.Handler {
 	return func(c *fiber.Ctx) error {
 		// Get list of available mapping IDs
 		var mappingIDs []string
@@ -232,6 +234,10 @@
 			mappingIDs = append(mappingIDs, list.ID)
 		}
 
+		// Use values from config (defaults are already applied during parsing)
+		server := yamlConfig.Server
+		sdk := yamlConfig.SDK
+
 		// Prepare template data
 		data := TemplateData{
 			Title:       config.Title,
@@ -239,6 +245,8 @@
 			Hash:        config.Buildhash,
 			Date:        config.Buildtime,
 			Description: config.Description,
+			Server:      server,
+			SDK:         sdk,
 			MappingIDs:  mappingIDs,
 		}
 
@@ -258,6 +266,8 @@
 <head>
     <meta charset="UTF-8">
     <title>` + data.Title + `</title>
+    <script src="` + data.SDK + `"
+            data-server="` + data.Server + `"></script>
 </head>
 <body>
     <div class="container">
@@ -291,6 +301,29 @@
 
 	html += `
     </ul>
+
+    <script>
+  		<!-- activates/deactivates Mapper. -->
+  		  
+       let data = {
+         'action'  : 'pipe',
+         'service' : 'https://korap.ids-mannheim.de/plugin/termmapper/query'
+       };
+
+       function pluginit (p) {
+         p.onMessage = function(msg) {
+           if (msg.key == 'termmapper') {
+             if (msg.value) {
+               data['job'] = 'add';
+             }
+             else {
+               data['job'] = 'del';
+             };
+             KorAPlugin.sendMsg(data);
+           };
+         };
+       };
+    </script>
   </body>
 </html>`
 
diff --git a/cmd/termmapper/main_test.go b/cmd/termmapper/main_test.go
index 5a9826b..292d3d9 100644
--- a/cmd/termmapper/main_test.go
+++ b/cmd/termmapper/main_test.go
@@ -34,7 +34,7 @@
 	require.NoError(t, err)
 
 	// Create mock config for testing
-	mockConfig := &tmconfig.MappingLists{
+	mockConfig := &tmconfig.MappingConfig{
 		Lists: []tmconfig.MappingList{mappingList},
 	}
 
@@ -269,7 +269,7 @@
 	require.NoError(t, err)
 
 	// Create mock config for testing
-	mockConfig := &tmconfig.MappingLists{
+	mockConfig := &tmconfig.MappingConfig{
 		Lists: []tmconfig.MappingList{mappingList},
 	}
 
@@ -299,3 +299,89 @@
 	assert.Contains(t, string(body), "KoralPipe-TermMapper")
 
 }
+
+func TestKalamarPluginWithCustomSdkAndServer(t *testing.T) {
+	// Create test mapping list
+	mappingList := tmconfig.MappingList{
+		ID: "test-mapper",
+		Mappings: []tmconfig.MappingRule{
+			"[A] <> [B]",
+		},
+	}
+
+	// Create mapper
+	m, err := mapper.NewMapper([]tmconfig.MappingList{mappingList})
+	require.NoError(t, err)
+
+	tests := []struct {
+		name           string
+		customSDK      string
+		customServer   string
+		expectedSDK    string
+		expectedServer string
+	}{
+		{
+			name:           "Custom SDK and Server values",
+			customSDK:      "https://custom.example.com/custom-sdk.js",
+			customServer:   "https://custom.example.com/",
+			expectedSDK:    "https://custom.example.com/custom-sdk.js",
+			expectedServer: "https://custom.example.com/",
+		},
+		{
+			name:           "Only custom SDK value",
+			customSDK:      "https://custom.example.com/custom-sdk.js",
+			customServer:   "https://korap.ids-mannheim.de/", // defaults applied during parsing
+			expectedSDK:    "https://custom.example.com/custom-sdk.js",
+			expectedServer: "https://korap.ids-mannheim.de/",
+		},
+		{
+			name:           "Only custom Server value",
+			customSDK:      "https://korap.ids-mannheim.de/js/korap-plugin-latest.js", // defaults applied during parsing
+			customServer:   "https://custom.example.com/",
+			expectedSDK:    "https://korap.ids-mannheim.de/js/korap-plugin-latest.js",
+			expectedServer: "https://custom.example.com/",
+		},
+		{
+			name:           "Defaults applied during parsing",
+			customSDK:      "https://korap.ids-mannheim.de/js/korap-plugin-latest.js", // defaults applied during parsing
+			customServer:   "https://korap.ids-mannheim.de/",                          // defaults applied during parsing
+			expectedSDK:    "https://korap.ids-mannheim.de/js/korap-plugin-latest.js",
+			expectedServer: "https://korap.ids-mannheim.de/",
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			// Create mock config with custom values
+			mockConfig := &tmconfig.MappingConfig{
+				SDK:    tt.customSDK,
+				Server: tt.customServer,
+				Lists:  []tmconfig.MappingList{mappingList},
+			}
+
+			// Create fiber app
+			app := fiber.New()
+			setupRoutes(app, m, mockConfig)
+
+			// Test Kalamar plugin endpoint
+			req := httptest.NewRequest(http.MethodGet, "/", nil)
+			resp, err := app.Test(req)
+			require.NoError(t, err)
+			defer resp.Body.Close()
+
+			assert.Equal(t, http.StatusOK, resp.StatusCode)
+			body, err := io.ReadAll(resp.Body)
+			require.NoError(t, err)
+
+			htmlContent := string(body)
+
+			// Check that the HTML contains the expected SDK and Server values
+			assert.Contains(t, htmlContent, `src="`+tt.expectedSDK+`"`)
+			assert.Contains(t, htmlContent, `data-server="`+tt.expectedServer+`"`)
+
+			// Ensure it's still a valid HTML page
+			assert.Contains(t, htmlContent, "KoralPipe-TermMapper")
+			assert.Contains(t, htmlContent, "<!DOCTYPE html>")
+		})
+	}
+}