blob: 4e08d149cc813536aff68eca0bd080ee66ea6f0d [file] [log] [blame]
Akron32d53de2025-05-22 13:45:32 +02001package mapper
2
3import (
4 "encoding/json"
Akron32d53de2025-05-22 13:45:32 +02005 "testing"
6
Akron2ef703c2025-07-03 15:57:42 +02007 "github.com/KorAP/Koral-Mapper/ast"
8 "github.com/KorAP/Koral-Mapper/config"
9 "github.com/KorAP/Koral-Mapper/matcher"
Akron32d53de2025-05-22 13:45:32 +020010 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12)
13
14func TestMapper(t *testing.T) {
Akrona00d4752025-05-26 17:34:36 +020015 // Create test mapping list
16 mappingList := config.MappingList{
17 ID: "test-mapper",
18 FoundryA: "opennlp",
19 LayerA: "p",
20 FoundryB: "upos",
21 LayerB: "p",
22 Mappings: []config.MappingRule{
23 "[PIDAT] <> [opennlp/p=PIDAT & opennlp/p=AdjType:Pdt]",
24 "[DET] <> [opennlp/p=DET]",
25 },
26 }
Akron32d53de2025-05-22 13:45:32 +020027
28 // Create a new mapper
Akrona00d4752025-05-26 17:34:36 +020029 m, err := NewMapper([]config.MappingList{mappingList})
Akron32d53de2025-05-22 13:45:32 +020030 require.NoError(t, err)
31
32 tests := []struct {
33 name string
34 mappingID string
35 opts MappingOptions
36 input string
37 expected string
38 expectError bool
39 }{
40 {
41 name: "Simple A to B mapping",
42 mappingID: "test-mapper",
43 opts: MappingOptions{
44 Direction: AtoB,
45 },
46 input: `{
47 "@type": "koral:token",
48 "wrap": {
49 "@type": "koral:term",
50 "foundry": "opennlp",
51 "key": "PIDAT",
52 "layer": "p",
53 "match": "match:eq"
54 }
55 }`,
56 expected: `{
57 "@type": "koral:token",
58 "wrap": {
59 "@type": "koral:termGroup",
60 "operands": [
61 {
62 "@type": "koral:term",
63 "foundry": "opennlp",
64 "key": "PIDAT",
65 "layer": "p",
66 "match": "match:eq"
67 },
68 {
69 "@type": "koral:term",
70 "foundry": "opennlp",
71 "key": "AdjType",
72 "layer": "p",
73 "match": "match:eq",
74 "value": "Pdt"
75 }
76 ],
77 "relation": "relation:and"
78 }
79 }`,
80 },
81 {
Akron0d9117c2025-05-27 15:20:21 +020082 name: "Simple A to B mapping with rewrites",
Akron32d53de2025-05-22 13:45:32 +020083 mappingID: "test-mapper",
84 opts: MappingOptions{
Akron0d9117c2025-05-27 15:20:21 +020085 Direction: AtoB,
86 AddRewrites: true,
Akron32d53de2025-05-22 13:45:32 +020087 },
88 input: `{
89 "@type": "koral:token",
90 "wrap": {
Akrona1a183f2025-05-26 17:47:33 +020091 "@type": "koral:term",
92 "foundry": "opennlp",
93 "key": "PIDAT",
94 "layer": "p",
95 "match": "match:eq"
Akron32d53de2025-05-22 13:45:32 +020096 }
97 }`,
98 expected: `{
99 "@type": "koral:token",
100 "wrap": {
Akron0d9117c2025-05-27 15:20:21 +0200101 "@type": "koral:termGroup",
102 "operands": [
103 {
104 "@type": "koral:term",
105 "foundry": "opennlp",
106 "key": "PIDAT",
107 "layer": "p",
108 "match": "match:eq"
109 },
110 {
111 "@type": "koral:term",
112 "foundry": "opennlp",
113 "key": "AdjType",
114 "layer": "p",
115 "match": "match:eq",
116 "value": "Pdt"
117 }
118 ],
119 "relation": "relation:and",
120 "rewrites": [
121 {
122 "@type": "koral:rewrite",
Akron2f93c582026-02-19 16:49:13 +0100123 "editor": "Koral-Mapper",
Akron8a87d9a2025-05-27 15:30:48 +0200124 "original": {
Akron0d9117c2025-05-27 15:20:21 +0200125 "@type": "koral:term",
126 "foundry": "opennlp",
127 "key": "PIDAT",
128 "layer": "p",
129 "match": "match:eq"
130 }
131 }
132 ]
Akron32d53de2025-05-22 13:45:32 +0200133 }
134 }`,
135 },
136 {
Akron0d9117c2025-05-27 15:20:21 +0200137 name: "Mapping with foundry override and rewrites",
Akron32d53de2025-05-22 13:45:32 +0200138 mappingID: "test-mapper",
139 opts: MappingOptions{
Akron0d9117c2025-05-27 15:20:21 +0200140 Direction: AtoB,
141 FoundryB: "custom",
142 AddRewrites: true,
Akron32d53de2025-05-22 13:45:32 +0200143 },
144 input: `{
145 "@type": "koral:token",
146 "wrap": {
147 "@type": "koral:term",
148 "foundry": "opennlp",
149 "key": "PIDAT",
150 "layer": "p",
151 "match": "match:eq"
152 }
153 }`,
154 expected: `{
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 ],
Akron0d9117c2025-05-27 15:20:21 +0200175 "relation": "relation:and",
176 "rewrites": [
177 {
178 "@type": "koral:rewrite",
Akron2f93c582026-02-19 16:49:13 +0100179 "editor": "Koral-Mapper",
Akron8a87d9a2025-05-27 15:30:48 +0200180 "original": {
181 "@type": "koral:term",
182 "foundry": "opennlp",
183 "key": "PIDAT",
184 "layer": "p",
185 "match": "match:eq"
186 }
Akron0d9117c2025-05-27 15:20:21 +0200187 }
188 ]
Akron32d53de2025-05-22 13:45:32 +0200189 }
190 }`,
191 },
192 {
Akron0d9117c2025-05-27 15:20:21 +0200193 name: "B to A direction",
194 mappingID: "test-mapper",
195 opts: MappingOptions{
196 Direction: BtoA,
197 },
198 input: `{
199 "@type": "koral:token",
200 "wrap": {
201 "@type": "koral:term",
202 "foundry": "opennlp",
203 "key": "PIDAT",
204 "layer": "p",
205 "match": "match:eq"
206 }
207 }`,
208 expected: `{
209 "@type": "koral:token",
210 "wrap": {
211 "@type": "koral:term",
212 "foundry": "opennlp",
213 "key": "PIDAT",
214 "layer": "p",
215 "match": "match:eq"
216 }
217 }`,
218 expectError: false,
219 },
220 {
Akron32d53de2025-05-22 13:45:32 +0200221 name: "Invalid mapping ID",
222 mappingID: "nonexistent",
223 opts: MappingOptions{
224 Direction: AtoB,
225 },
226 input: `{
227 "@type": "koral:token",
228 "wrap": {
229 "@type": "koral:term",
230 "foundry": "opennlp",
231 "key": "PIDAT",
232 "layer": "p",
233 "match": "match:eq"
234 }
235 }`,
236 expectError: true,
237 },
238 {
239 name: "Invalid direction",
240 mappingID: "test-mapper",
241 opts: MappingOptions{
Akrona1a183f2025-05-26 17:47:33 +0200242 Direction: Direction(false),
Akron32d53de2025-05-22 13:45:32 +0200243 },
244 input: `{
245 "@type": "koral:token",
246 "wrap": {
247 "@type": "koral:term",
248 "foundry": "opennlp",
249 "key": "PIDAT",
250 "layer": "p",
251 "match": "match:eq"
252 }
253 }`,
Akrona1a183f2025-05-26 17:47:33 +0200254 expected: `{
255 "@type": "koral:token",
256 "wrap": {
257 "@type": "koral:term",
258 "foundry": "opennlp",
259 "key": "PIDAT",
260 "layer": "p",
261 "match": "match:eq"
262 }
263 }`,
264 expectError: false,
Akron32d53de2025-05-22 13:45:32 +0200265 },
Akron8f1970f2025-05-30 12:52:03 +0200266 {
267 name: "Query with legacy rewrite field names",
268 mappingID: "test-mapper",
269 opts: MappingOptions{
270 Direction: AtoB,
271 },
272 input: `{
273 "@type": "koral:token",
274 "rewrites": [
275 {
276 "@type": "koral:rewrite",
277 "_comment": "Legacy rewrite with source instead of editor",
278 "source": "LegacyEditor",
279 "operation": "operation:legacy",
280 "origin": "LegacySource"
281 }
282 ],
283 "wrap": {
284 "@type": "koral:term",
285 "foundry": "opennlp",
286 "key": "PIDAT",
287 "layer": "p",
288 "match": "match:eq"
289 }
290 }`,
291 expected: `{
292 "@type": "koral:token",
293 "rewrites": [
294 {
295 "@type": "koral:rewrite",
296 "_comment": "Legacy rewrite with source instead of editor",
297 "editor": "LegacyEditor",
298 "operation": "operation:legacy",
299 "src": "LegacySource"
300 }
301 ],
302 "wrap": {
303 "@type": "koral:termGroup",
304 "operands": [
305 {
306 "@type": "koral:term",
307 "foundry": "opennlp",
308 "key": "PIDAT",
309 "layer": "p",
310 "match": "match:eq"
311 },
312 {
313 "@type": "koral:term",
314 "foundry": "opennlp",
315 "key": "AdjType",
316 "layer": "p",
317 "match": "match:eq",
318 "value": "Pdt"
319 }
320 ],
321 "relation": "relation:and"
322 }
323 }`,
324 },
325 {
326 name: "Query with mixed legacy and modern rewrite fields",
327 mappingID: "test-mapper",
328 opts: MappingOptions{
329 Direction: AtoB,
330 },
331 input: `{
332 "@type": "koral:token",
333 "rewrites": [
334 {
335 "@type": "koral:rewrite",
336 "_comment": "Modern rewrite",
337 "editor": "ModernEditor",
338 "operation": "operation:modern",
339 "original": {
340 "@type": "koral:term",
341 "foundry": "original",
342 "key": "original-key"
343 }
344 },
345 {
346 "@type": "koral:rewrite",
347 "_comment": "Legacy rewrite with precedence test",
348 "editor": "PreferredEditor",
349 "source": "IgnoredSource",
350 "operation": "operation:precedence",
351 "original": "PreferredOriginal",
352 "src": "IgnoredSrc",
353 "origin": "IgnoredOrigin"
354 }
355 ],
356 "wrap": {
357 "@type": "koral:term",
358 "foundry": "opennlp",
359 "key": "PIDAT",
360 "layer": "p",
361 "match": "match:eq"
362 }
363 }`,
364 expected: `{
365 "@type": "koral:token",
366 "rewrites": [
367 {
368 "@type": "koral:rewrite",
369 "_comment": "Modern rewrite",
370 "editor": "ModernEditor",
371 "operation": "operation:modern",
372 "original": {
373 "@type": "koral:term",
374 "foundry": "original",
375 "key": "original-key"
376 }
377 },
378 {
379 "@type": "koral:rewrite",
380 "_comment": "Legacy rewrite with precedence test",
381 "editor": "PreferredEditor",
382 "operation": "operation:precedence",
383 "original": "PreferredOriginal"
384 }
385 ],
386 "wrap": {
387 "@type": "koral:termGroup",
388 "operands": [
389 {
390 "@type": "koral:term",
391 "foundry": "opennlp",
392 "key": "PIDAT",
393 "layer": "p",
394 "match": "match:eq"
395 },
396 {
397 "@type": "koral:term",
398 "foundry": "opennlp",
399 "key": "AdjType",
400 "layer": "p",
401 "match": "match:eq",
402 "value": "Pdt"
403 }
404 ],
405 "relation": "relation:and"
406 }
407 }`,
408 },
Akron32d53de2025-05-22 13:45:32 +0200409 }
410
411 for _, tt := range tests {
412 t.Run(tt.name, func(t *testing.T) {
413 // Parse input JSON
Akron121c66e2025-06-02 16:34:05 +0200414 var inputData any
Akron32d53de2025-05-22 13:45:32 +0200415 err := json.Unmarshal([]byte(tt.input), &inputData)
416 require.NoError(t, err)
417
418 // Apply mappings
Akron7b4984e2025-05-26 19:12:20 +0200419 result, err := m.ApplyQueryMappings(tt.mappingID, tt.opts, inputData)
Akron32d53de2025-05-22 13:45:32 +0200420 if tt.expectError {
421 assert.Error(t, err)
422 return
423 }
424 require.NoError(t, err)
425
426 // Parse expected JSON
Akron121c66e2025-06-02 16:34:05 +0200427 var expectedData any
Akron32d53de2025-05-22 13:45:32 +0200428 err = json.Unmarshal([]byte(tt.expected), &expectedData)
429 require.NoError(t, err)
430
431 // Compare results
432 assert.Equal(t, expectedData, result)
433 })
434 }
435}
Akrond5850f82025-05-23 16:44:44 +0200436
Akroncc83eb52025-05-27 14:39:12 +0200437func TestTokenToTermGroupWithRewrites(t *testing.T) {
438 // Create test mapping list specifically for token to termGroup test
439 mappingList := config.MappingList{
440 ID: "test-token-to-termgroup",
441 FoundryA: "opennlp",
442 LayerA: "p",
Akron422cd252026-05-19 16:31:19 +0200443 FoundryB: "tt",
444 LayerB: "pos",
Akroncc83eb52025-05-27 14:39:12 +0200445 Mappings: []config.MappingRule{
446 "[PIDAT] <> [opennlp/p=PIDAT & opennlp/p=AdjType:Pdt]",
447 },
448 }
449
450 // Create a new mapper
451 m, err := NewMapper([]config.MappingList{mappingList})
452 require.NoError(t, err)
453
454 input := `{
455 "@type": "koral:token",
456 "rewrites": [
457 {
458 "@type": "koral:rewrite",
459 "_comment": "This rewrite should be preserved",
460 "editor": "TestEditor",
461 "operation": "operation:test",
462 "src": "TestSource"
463 }
464 ],
465 "wrap": {
466 "@type": "koral:term",
467 "foundry": "opennlp",
468 "key": "PIDAT",
469 "layer": "p",
470 "match": "match:eq"
471 }
472 }`
473
474 expected := `{
475 "@type": "koral:token",
476 "rewrites": [
477 {
478 "@type": "koral:rewrite",
479 "_comment": "This rewrite should be preserved",
480 "editor": "TestEditor",
481 "operation": "operation:test",
482 "src": "TestSource"
483 }
484 ],
485 "wrap": {
486 "@type": "koral:termGroup",
487 "operands": [
488 {
489 "@type": "koral:term",
490 "foundry": "opennlp",
491 "key": "PIDAT",
492 "layer": "p",
493 "match": "match:eq"
494 },
495 {
496 "@type": "koral:term",
497 "foundry": "opennlp",
498 "key": "AdjType",
499 "layer": "p",
500 "match": "match:eq",
501 "value": "Pdt"
502 }
503 ],
504 "relation": "relation:and"
505 }
506 }`
507
508 // Parse input JSON
Akron121c66e2025-06-02 16:34:05 +0200509 var inputData any
Akroncc83eb52025-05-27 14:39:12 +0200510 err = json.Unmarshal([]byte(input), &inputData)
511 require.NoError(t, err)
512
513 // Apply mappings
514 result, err := m.ApplyQueryMappings("test-token-to-termgroup", MappingOptions{Direction: AtoB}, inputData)
515 require.NoError(t, err)
516
517 // Parse expected JSON
Akron121c66e2025-06-02 16:34:05 +0200518 var expectedData any
Akroncc83eb52025-05-27 14:39:12 +0200519 err = json.Unmarshal([]byte(expected), &expectedData)
520 require.NoError(t, err)
521
522 // Compare results
523 assert.Equal(t, expectedData, result)
524}
525
Akrond5850f82025-05-23 16:44:44 +0200526func TestMatchComplexPatterns(t *testing.T) {
527 tests := []struct {
528 name string
529 pattern ast.Pattern
530 replacement ast.Replacement
531 input ast.Node
532 expected ast.Node
533 }{
534 {
535 name: "Deep nested pattern with mixed operators",
536 pattern: ast.Pattern{
537 Root: &ast.TermGroup{
538 Operands: []ast.Node{
539 &ast.Term{
540 Key: "A",
541 Match: ast.MatchEqual,
542 },
543 &ast.TermGroup{
544 Operands: []ast.Node{
545 &ast.Term{
546 Key: "B",
547 Match: ast.MatchEqual,
548 },
549 &ast.TermGroup{
550 Operands: []ast.Node{
551 &ast.Term{
552 Key: "C",
553 Match: ast.MatchEqual,
554 },
555 &ast.Term{
556 Key: "D",
557 Match: ast.MatchEqual,
558 },
559 },
560 Relation: ast.AndRelation,
561 },
562 },
563 Relation: ast.OrRelation,
564 },
565 },
566 Relation: ast.AndRelation,
567 },
568 },
569 replacement: ast.Replacement{
570 Root: &ast.Term{
571 Key: "RESULT",
572 Match: ast.MatchEqual,
573 },
574 },
575 input: &ast.TermGroup{
576 Operands: []ast.Node{
577 &ast.Term{
578 Key: "A",
579 Match: ast.MatchEqual,
580 },
581 &ast.TermGroup{
582 Operands: []ast.Node{
583 &ast.Term{
584 Key: "C",
585 Match: ast.MatchEqual,
586 },
587 &ast.Term{
588 Key: "D",
589 Match: ast.MatchEqual,
590 },
591 },
592 Relation: ast.AndRelation,
593 },
594 },
595 Relation: ast.AndRelation,
596 },
597 expected: &ast.Term{
598 Key: "RESULT",
599 Match: ast.MatchEqual,
600 },
601 },
602 }
603
604 for _, tt := range tests {
605 t.Run(tt.name, func(t *testing.T) {
606 m, err := matcher.NewMatcher(tt.pattern, tt.replacement)
607 require.NoError(t, err)
608 result := m.Replace(tt.input)
609 assert.Equal(t, tt.expected, result)
610 })
611 }
612}
613
614func TestInvalidPatternReplacement(t *testing.T) {
Akrona00d4752025-05-26 17:34:36 +0200615 // Create test mapping list
616 mappingList := config.MappingList{
617 ID: "test-mapper",
618 FoundryA: "opennlp",
619 LayerA: "p",
620 FoundryB: "upos",
621 LayerB: "p",
622 Mappings: []config.MappingRule{
623 "[PIDAT] <> [opennlp/p=PIDAT & opennlp/p=AdjType:Pdt]",
624 },
625 }
Akrond5850f82025-05-23 16:44:44 +0200626
627 // Create a new mapper
Akrona00d4752025-05-26 17:34:36 +0200628 m, err := NewMapper([]config.MappingList{mappingList})
Akrond5850f82025-05-23 16:44:44 +0200629 require.NoError(t, err)
630
631 tests := []struct {
632 name string
633 input string
634 expectError bool
635 errorMsg string
636 }{
637 {
638 name: "Invalid input - empty term group",
639 input: `{
640 "@type": "koral:token",
641 "wrap": {
642 "@type": "koral:termGroup",
643 "operands": [],
644 "relation": "relation:and"
645 }
646 }`,
647 expectError: true,
648 errorMsg: "failed to parse JSON into AST: error parsing wrapped node: term group must have at least one operand",
649 },
650 }
651
652 for _, tt := range tests {
653 t.Run(tt.name, func(t *testing.T) {
654 var inputData any
655 err := json.Unmarshal([]byte(tt.input), &inputData)
656 require.NoError(t, err)
657
Akron7b4984e2025-05-26 19:12:20 +0200658 result, err := m.ApplyQueryMappings("test-mapper", MappingOptions{Direction: AtoB}, inputData)
Akrond5850f82025-05-23 16:44:44 +0200659 if tt.expectError {
660 assert.Error(t, err)
661 assert.Equal(t, tt.errorMsg, err.Error())
662 assert.Nil(t, result)
663 } else {
664 assert.NoError(t, err)
665 assert.NotNil(t, result)
666 }
667 })
668 }
669}
Akron7b4984e2025-05-26 19:12:20 +0200670
Akron958fc472026-05-19 13:58:52 +0200671func TestMultiFieldRewritesAreReversible(t *testing.T) {
672 mappingList := config.MappingList{
673 ID: "multi-field",
674 FoundryA: "opennlp",
675 LayerA: "p",
676 FoundryB: "upos",
677 LayerB: "pos",
678 Mappings: []config.MappingRule{
679 "[DET] <> [PRON]",
680 },
681 }
682
683 m, err := NewMapper([]config.MappingList{mappingList})
684 require.NoError(t, err)
685
686 tests := []struct {
687 name string
688 opts MappingOptions
689 input string
690 expected string
691 }{
692 {
693 name: "Multi-field change: foundry + layer + key all change",
694 opts: MappingOptions{
695 Direction: AtoB,
696 AddRewrites: true,
697 },
698 input: `{
699 "@type": "koral:token",
700 "wrap": {
701 "@type": "koral:term",
702 "foundry": "opennlp",
703 "key": "DET",
704 "layer": "p",
705 "match": "match:eq"
706 }
707 }`,
708 expected: `{
709 "@type": "koral:token",
710 "wrap": {
711 "@type": "koral:term",
712 "foundry": "upos",
713 "key": "PRON",
714 "layer": "pos",
715 "match": "match:eq",
716 "rewrites": [
717 {
718 "@type": "koral:rewrite",
719 "editor": "Koral-Mapper",
720 "scope": "foundry",
721 "original": "opennlp"
722 },
723 {
724 "@type": "koral:rewrite",
725 "editor": "Koral-Mapper",
726 "scope": "layer",
727 "original": "p"
728 },
729 {
730 "@type": "koral:rewrite",
731 "editor": "Koral-Mapper",
732 "scope": "key",
733 "original": "DET"
734 }
735 ]
736 }
737 }`,
738 },
739 {
740 name: "Reverse direction: foundry + layer + key all change back",
741 opts: MappingOptions{
742 Direction: BtoA,
743 AddRewrites: true,
744 },
745 input: `{
746 "@type": "koral:token",
747 "wrap": {
748 "@type": "koral:term",
749 "foundry": "upos",
750 "key": "PRON",
751 "layer": "pos",
752 "match": "match:eq"
753 }
754 }`,
755 expected: `{
756 "@type": "koral:token",
757 "wrap": {
758 "@type": "koral:term",
759 "foundry": "opennlp",
760 "key": "DET",
761 "layer": "p",
762 "match": "match:eq",
763 "rewrites": [
764 {
765 "@type": "koral:rewrite",
766 "editor": "Koral-Mapper",
767 "scope": "foundry",
768 "original": "upos"
769 },
770 {
771 "@type": "koral:rewrite",
772 "editor": "Koral-Mapper",
773 "scope": "layer",
774 "original": "pos"
775 },
776 {
777 "@type": "koral:rewrite",
778 "editor": "Koral-Mapper",
779 "scope": "key",
780 "original": "PRON"
781 }
782 ]
783 }
784 }`,
785 },
786 }
787
788 for _, tt := range tests {
789 t.Run(tt.name, func(t *testing.T) {
790 var inputData any
791 err := json.Unmarshal([]byte(tt.input), &inputData)
792 require.NoError(t, err)
793
794 result, err := m.ApplyQueryMappings("multi-field", tt.opts, inputData)
795 require.NoError(t, err)
796
797 var expectedData any
798 err = json.Unmarshal([]byte(tt.expected), &expectedData)
799 require.NoError(t, err)
800
801 assert.Equal(t, expectedData, result)
802 })
803 }
804}
805
806func TestSingleFieldRewrite(t *testing.T) {
807 mappingList := config.MappingList{
808 ID: "same-fl",
809 FoundryA: "opennlp",
810 LayerA: "p",
811 FoundryB: "opennlp",
Akron422cd252026-05-19 16:31:19 +0200812 LayerB: "pos",
Akron958fc472026-05-19 13:58:52 +0200813 Mappings: []config.MappingRule{
814 "[DET] <> [PRON]",
815 },
816 }
817
818 m, err := NewMapper([]config.MappingList{mappingList})
819 require.NoError(t, err)
820
821 var inputData any
822 err = json.Unmarshal([]byte(`{
823 "@type": "koral:token",
824 "wrap": {
825 "@type": "koral:term",
826 "foundry": "opennlp",
827 "key": "DET",
828 "layer": "p",
829 "match": "match:eq"
830 }
831 }`), &inputData)
832 require.NoError(t, err)
833
834 result, err := m.ApplyQueryMappings("same-fl", MappingOptions{
835 Direction: AtoB,
836 AddRewrites: true,
837 }, inputData)
838 require.NoError(t, err)
839
840 var expectedData any
841 err = json.Unmarshal([]byte(`{
842 "@type": "koral:token",
843 "wrap": {
844 "@type": "koral:term",
845 "foundry": "opennlp",
846 "key": "PRON",
Akron422cd252026-05-19 16:31:19 +0200847 "layer": "pos",
Akron958fc472026-05-19 13:58:52 +0200848 "match": "match:eq",
849 "rewrites": [
850 {
851 "@type": "koral:rewrite",
852 "editor": "Koral-Mapper",
Akron422cd252026-05-19 16:31:19 +0200853 "scope": "layer",
854 "original": "p"
855 },
856 {
857 "@type": "koral:rewrite",
858 "editor": "Koral-Mapper",
Akron958fc472026-05-19 13:58:52 +0200859 "scope": "key",
860 "original": "DET"
861 }
862 ]
863 }
864 }`), &expectedData)
865 require.NoError(t, err)
866
867 assert.Equal(t, expectedData, result)
868}
869
870func TestBuildRewritesFieldInjection(t *testing.T) {
871 tests := []struct {
872 name string
873 original *ast.Term
874 new_ *ast.Term
875 expectedScopes []string
876 hasOriginals []bool
877 }{
878 {
879 name: "All fields change with originals",
880 original: &ast.Term{Foundry: "a", Layer: "l1", Key: "k1", Value: "v1", Match: ast.MatchEqual},
881 new_: &ast.Term{Foundry: "b", Layer: "l2", Key: "k2", Value: "v2", Match: ast.MatchEqual},
882 expectedScopes: []string{"foundry", "layer", "key", "value"},
883 hasOriginals: []bool{true, true, true, true},
884 },
885 {
886 name: "Injection: empty value becomes non-empty",
887 original: &ast.Term{Foundry: "a", Layer: "l", Key: "k", Match: ast.MatchEqual},
888 new_: &ast.Term{Foundry: "a", Layer: "l", Key: "k", Value: "v", Match: ast.MatchEqual},
889 expectedScopes: []string{"value"},
890 hasOriginals: []bool{false},
891 },
892 {
893 name: "Deletion: non-empty value becomes empty",
894 original: &ast.Term{Foundry: "a", Layer: "l", Key: "k", Value: "v", Match: ast.MatchEqual},
895 new_: &ast.Term{Foundry: "a", Layer: "l", Key: "k", Match: ast.MatchEqual},
896 expectedScopes: []string{"value"},
897 hasOriginals: []bool{true},
898 },
899 }
900
901 for _, tt := range tests {
902 t.Run(tt.name, func(t *testing.T) {
903 rewrites := buildRewrites(tt.original, tt.new_)
904 require.Len(t, rewrites, len(tt.expectedScopes))
905 for i, rw := range rewrites {
906 assert.Equal(t, RewriteEditor, rw.Editor)
907 assert.Equal(t, tt.expectedScopes[i], rw.Scope)
908 if tt.hasOriginals[i] {
909 assert.NotNil(t, rw.Original, "expected original for scope %s", tt.expectedScopes[i])
910 } else {
911 assert.Nil(t, rw.Original, "expected no original for scope %s (injection)", tt.expectedScopes[i])
912 }
913 }
914 })
915 }
916}
917
Akron7b4984e2025-05-26 19:12:20 +0200918func TestQueryWrapperMappings(t *testing.T) {
919
920 mappingList := config.MappingList{
921 ID: "test-wrapper",
922 FoundryA: "opennlp",
923 LayerA: "orth",
924 FoundryB: "upos",
925 LayerB: "orth",
926 Mappings: []config.MappingRule{
927 "[opennlp/orth=Baum] <> [opennlp/orth=X]",
928 },
929 }
930
931 // Create a new mapper
932 m, err := NewMapper([]config.MappingList{mappingList})
933 require.NoError(t, err)
934
935 tests := []struct {
936 name string
937 mappingID string
938 opts MappingOptions
939 input string
940 expected string
941 expectError bool
942 }{
943 {
Akroncc83eb52025-05-27 14:39:12 +0200944 name: "Query wrapper case with rewrites preservation",
Akron7b4984e2025-05-26 19:12:20 +0200945 mappingID: "test-wrapper",
946 opts: MappingOptions{
947 Direction: AtoB,
948 },
949 input: `{
950 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
951 "collection": {
952 "@type": "koral:doc",
953 "key": "availability",
954 "match": "match:eq",
Akron7b4984e2025-05-26 19:12:20 +0200955 "type": "type:regex",
956 "value": "CC.*"
957 },
958 "query": {
959 "@type": "koral:token",
Akroncc83eb52025-05-27 14:39:12 +0200960 "rewrites": [
961 {
962 "@type": "koral:rewrite",
963 "_comment": "Original rewrite that should be preserved",
964 "editor": "Original",
965 "operation": "operation:original",
966 "src": "Original"
967 }
968 ],
Akron7b4984e2025-05-26 19:12:20 +0200969 "wrap": {
970 "@type": "koral:term",
971 "foundry": "opennlp",
972 "key": "Baum",
973 "layer": "orth",
974 "match": "match:eq"
975 }
976 }
977 }`,
978 expected: `{
979 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
980 "collection": {
981 "@type": "koral:doc",
982 "key": "availability",
983 "match": "match:eq",
Akron7b4984e2025-05-26 19:12:20 +0200984 "type": "type:regex",
985 "value": "CC.*"
986 },
987 "query": {
988 "@type": "koral:token",
Akroncc83eb52025-05-27 14:39:12 +0200989 "rewrites": [
990 {
991 "@type": "koral:rewrite",
992 "_comment": "Original rewrite that should be preserved",
993 "editor": "Original",
994 "operation": "operation:original",
995 "src": "Original"
996 }
997 ],
Akron7b4984e2025-05-26 19:12:20 +0200998 "wrap": {
999 "@type": "koral:term",
1000 "foundry": "opennlp",
1001 "key": "X",
1002 "layer": "orth",
1003 "match": "match:eq"
1004 }
1005 }
1006 }`,
1007 },
1008 {
1009 name: "Empty query field",
1010 mappingID: "test-wrapper",
1011 opts: MappingOptions{
1012 Direction: AtoB,
1013 },
1014 input: `{
1015 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1016 "query": null
1017 }`,
1018 expected: `{
1019 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1020 "query": null
1021 }`,
1022 },
1023 {
1024 name: "Missing query field",
1025 mappingID: "test-wrapper",
1026 opts: MappingOptions{
1027 Direction: AtoB,
1028 },
1029 input: `{
1030 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1031 "collection": {
1032 "@type": "koral:doc"
1033 }
1034 }`,
1035 expected: `{
1036 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1037 "collection": {
1038 "@type": "koral:doc"
1039 }
1040 }`,
1041 },
1042 {
1043 name: "Query field with non-object value",
1044 mappingID: "test-wrapper",
1045 opts: MappingOptions{
1046 Direction: AtoB,
1047 },
1048 input: `{
1049 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1050 "query": "invalid"
1051 }`,
1052 expected: `{
1053 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1054 "query": "invalid"
1055 }`,
1056 },
Akroncc83eb52025-05-27 14:39:12 +02001057 {
1058 name: "Query with rewrites in nested token",
1059 mappingID: "test-wrapper",
1060 opts: MappingOptions{
1061 Direction: AtoB,
1062 },
1063 input: `{
1064 "@type": "koral:token",
1065 "rewrites": [
1066 {
1067 "@type": "koral:rewrite",
1068 "_comment": "Nested rewrite that should be preserved",
1069 "editor": "Nested",
1070 "operation": "operation:nested",
1071 "src": "Nested"
1072 }
1073 ],
1074 "wrap": {
1075 "@type": "koral:term",
1076 "foundry": "opennlp",
1077 "key": "Baum",
1078 "layer": "orth",
1079 "match": "match:eq"
1080 }
1081 }`,
1082 expected: `{
1083 "@type": "koral:token",
1084 "rewrites": [
1085 {
1086 "@type": "koral:rewrite",
1087 "_comment": "Nested rewrite that should be preserved",
1088 "editor": "Nested",
1089 "operation": "operation:nested",
1090 "src": "Nested"
1091 }
1092 ],
1093 "wrap": {
1094 "@type": "koral:term",
1095 "foundry": "opennlp",
1096 "key": "X",
1097 "layer": "orth",
1098 "match": "match:eq"
1099 }
1100 }`,
1101 },
Akron7b4984e2025-05-26 19:12:20 +02001102 }
1103
1104 for _, tt := range tests {
1105 t.Run(tt.name, func(t *testing.T) {
1106 // Parse input JSON
Akron121c66e2025-06-02 16:34:05 +02001107 var inputData any
Akron7b4984e2025-05-26 19:12:20 +02001108 err := json.Unmarshal([]byte(tt.input), &inputData)
1109 require.NoError(t, err)
1110
1111 // Apply mappings
1112 result, err := m.ApplyQueryMappings(tt.mappingID, tt.opts, inputData)
1113 if tt.expectError {
1114 assert.Error(t, err)
1115 return
1116 }
1117 require.NoError(t, err)
1118
1119 // Parse expected JSON
Akron121c66e2025-06-02 16:34:05 +02001120 var expectedData any
Akron7b4984e2025-05-26 19:12:20 +02001121 err = json.Unmarshal([]byte(tt.expected), &expectedData)
1122 require.NoError(t, err)
1123
1124 // Compare results
1125 assert.Equal(t, expectedData, result)
1126 })
1127 }
1128}
Akron422cd252026-05-19 16:31:19 +02001129
1130func TestIdenticalEffectiveFoundryLayerRejected(t *testing.T) {
1131 tests := []struct {
1132 name string
1133 list config.MappingList
1134 opts MappingOptions
1135 wantErr string
1136 }{
1137 {
1138 name: "YAML defaults identical",
1139 list: config.MappingList{
1140 ID: "test", FoundryA: "opennlp", LayerA: "p",
1141 FoundryB: "opennlp", LayerB: "p",
1142 Mappings: []config.MappingRule{"[A] <> [B]"},
1143 },
1144 opts: MappingOptions{Direction: AtoB},
1145 wantErr: "identical source and target",
1146 },
1147 {
1148 name: "Query param override makes them identical",
1149 list: config.MappingList{
1150 ID: "test", FoundryA: "opennlp", LayerA: "p",
1151 FoundryB: "upos", LayerB: "p",
1152 Mappings: []config.MappingRule{"[A] <> [B]"},
1153 },
1154 opts: MappingOptions{Direction: AtoB, FoundryB: "opennlp"},
1155 wantErr: "identical source and target",
1156 },
1157 {
1158 name: "Query param override resolves the conflict",
1159 list: config.MappingList{
1160 ID: "test", FoundryA: "opennlp", LayerA: "p",
1161 FoundryB: "opennlp", LayerB: "p",
1162 Mappings: []config.MappingRule{"[A] <> [B]"},
1163 },
1164 opts: MappingOptions{Direction: AtoB, FoundryB: "upos"},
1165 wantErr: "",
1166 },
1167 {
1168 name: "Different foundry same layer is allowed",
1169 list: config.MappingList{
1170 ID: "test", FoundryA: "opennlp", LayerA: "p",
1171 FoundryB: "upos", LayerB: "p",
1172 Mappings: []config.MappingRule{"[A] <> [B]"},
1173 },
1174 opts: MappingOptions{Direction: AtoB},
1175 wantErr: "",
1176 },
1177 {
1178 name: "Both foundries empty is allowed",
1179 list: config.MappingList{
1180 ID: "test",
1181 Mappings: []config.MappingRule{"[A] <> [B]"},
1182 },
1183 opts: MappingOptions{Direction: AtoB},
1184 wantErr: "",
1185 },
1186 }
1187
1188 for _, tt := range tests {
1189 t.Run(tt.name, func(t *testing.T) {
1190 m, err := NewMapper([]config.MappingList{tt.list})
1191 require.NoError(t, err)
1192
1193 input := map[string]any{
1194 "@type": "koral:token",
1195 "wrap": map[string]any{
1196 "@type": "koral:term",
1197 "key": "A",
1198 },
1199 }
1200
1201 _, err = m.ApplyQueryMappings("test", tt.opts, input)
1202 if tt.wantErr != "" {
1203 require.Error(t, err)
1204 assert.Contains(t, err.Error(), tt.wantErr)
1205 } else {
1206 assert.NoError(t, err)
1207 }
1208 })
1209 }
1210}
1211
1212func TestIdenticalEffectiveFieldRejected(t *testing.T) {
1213 tests := []struct {
1214 name string
1215 list config.MappingList
1216 opts MappingOptions
1217 wantErr string
1218 }{
1219 {
1220 name: "YAML defaults identical",
1221 list: config.MappingList{
1222 ID: "test", Type: "corpus",
1223 FieldA: "textClass", FieldB: "textClass",
1224 Mappings: []config.MappingRule{"novel <> fiction"},
1225 },
1226 opts: MappingOptions{Direction: AtoB},
1227 wantErr: "identical source and target field",
1228 },
1229 {
1230 name: "Query param override makes them identical",
1231 list: config.MappingList{
1232 ID: "test", Type: "corpus",
1233 FieldA: "textClass", FieldB: "genre",
1234 Mappings: []config.MappingRule{"novel <> fiction"},
1235 },
1236 opts: MappingOptions{Direction: AtoB, FieldB: "textClass"},
1237 wantErr: "identical source and target field",
1238 },
1239 {
1240 name: "Query param override resolves the conflict",
1241 list: config.MappingList{
1242 ID: "test", Type: "corpus",
1243 FieldA: "textClass", FieldB: "textClass",
1244 Mappings: []config.MappingRule{"novel <> fiction"},
1245 },
1246 opts: MappingOptions{Direction: AtoB, FieldB: "genre"},
1247 wantErr: "",
1248 },
1249 {
1250 name: "Different fields is allowed",
1251 list: config.MappingList{
1252 ID: "test", Type: "corpus",
1253 FieldA: "textClass", FieldB: "genre",
1254 Mappings: []config.MappingRule{"novel <> fiction"},
1255 },
1256 opts: MappingOptions{Direction: AtoB},
1257 wantErr: "",
1258 },
1259 {
1260 name: "Both fields empty is allowed",
1261 list: config.MappingList{
1262 ID: "test", Type: "corpus",
1263 Mappings: []config.MappingRule{"textClass=novel <> genre=fiction"},
1264 },
1265 opts: MappingOptions{Direction: AtoB},
1266 wantErr: "",
1267 },
1268 }
1269
1270 for _, tt := range tests {
1271 t.Run(tt.name, func(t *testing.T) {
1272 m, err := NewMapper([]config.MappingList{tt.list})
1273 require.NoError(t, err)
1274
1275 input := map[string]any{
1276 "collection": map[string]any{
1277 "@type": "koral:doc",
1278 "key": "textClass",
1279 "value": "novel",
1280 "match": "match:eq",
1281 },
1282 }
1283
1284 _, err = m.ApplyQueryMappings("test", tt.opts, input)
1285 if tt.wantErr != "" {
1286 require.Error(t, err)
1287 assert.Contains(t, err.Error(), tt.wantErr)
1288 } else {
1289 assert.NoError(t, err)
1290 }
1291 })
1292 }
1293}
1294
1295func TestIdenticalEffectiveValuesResponseEndpoint(t *testing.T) {
1296 t.Run("annotation response rejects identical effective foundry/layer", func(t *testing.T) {
1297 m, err := NewMapper([]config.MappingList{{
1298 ID: "test", FoundryA: "marmot", LayerA: "p",
1299 FoundryB: "marmot", LayerB: "p",
1300 Mappings: []config.MappingRule{"[DET] <> [PRON]"},
1301 }})
1302 require.NoError(t, err)
1303
1304 input := map[string]any{
1305 "snippet": `<span title="marmot/p:DET">Der</span>`,
1306 }
1307
1308 _, err = m.ApplyResponseMappings("test", MappingOptions{Direction: AtoB}, input)
1309 require.Error(t, err)
1310 assert.Contains(t, err.Error(), "identical source and target")
1311 })
1312
1313 t.Run("corpus response rejects identical effective field", func(t *testing.T) {
1314 m, err := NewMapper([]config.MappingList{{
1315 ID: "test", Type: "corpus",
1316 FieldA: "textClass", FieldB: "textClass",
1317 Mappings: []config.MappingRule{"novel <> fiction"},
1318 }})
1319 require.NoError(t, err)
1320
1321 input := map[string]any{
1322 "fields": []any{
1323 map[string]any{
1324 "@type": "koral:field",
1325 "key": "textClass",
1326 "value": "novel",
1327 "type": "type:string",
1328 },
1329 },
1330 }
1331
1332 _, err = m.ApplyResponseMappings("test", MappingOptions{Direction: AtoB}, input)
1333 require.Error(t, err)
1334 assert.Contains(t, err.Error(), "identical source and target field")
1335 })
1336}