xcstrings-mcp 0.4.0

MCP server for iOS/macOS .xcstrings localization file management
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
mod helpers;

use std::path::PathBuf;

use helpers::MemoryStore;
use indexmap::IndexMap;
use xcstrings_mcp::_test_support::service::{
    context, coverage, diff, extractor, file_validator, formatter, glossary, locale, merger,
    parser, plural_extractor, validator, xliff,
};
use xcstrings_mcp::model::translation::CompletedTranslation;
use xcstrings_mcp::model::xcstrings::{
    Localization, StringEntry, StringUnit, TranslationState, XcStringsFile,
};

const SIMPLE_FIXTURE: &str = include_str!("fixtures/simple.xcstrings");
const SHOULD_NOT_TRANSLATE_FIXTURE: &str = include_str!("fixtures/should_not_translate.xcstrings");
const GOLDEN: &str = include_str!("fixtures/golden.xcstrings");
const WITH_STALE: &str = include_str!("fixtures/with_stale.xcstrings");
const WITH_PLURALS: &str = include_str!("fixtures/with_plurals.xcstrings");
const WITH_SUBSTITUTIONS: &str = include_str!("fixtures/with_substitutions.xcstrings");
const WITH_DEVICE_VARIANTS: &str = include_str!("fixtures/with_device_variants.xcstrings");
const WITH_INTERPOLATION: &str = include_str!("fixtures/with_interpolation.xcstrings");
const WITH_MULTILINE: &str = include_str!("fixtures/with_multiline.xcstrings");

// ── Integration test 1: parse → get_untranslated ──

#[test]
fn parse_then_get_untranslated() {
    let file = parser::parse(SIMPLE_FIXTURE).unwrap();

    // "de" locale doesn't exist → both translatable keys should be untranslated
    let (batch, total) = extractor::get_untranslated(&file, "de", 100, 0).unwrap();

    assert_eq!(total, 2);
    assert_eq!(batch.len(), 2);

    let keys: Vec<&str> = batch.iter().map(|u| u.key.as_str()).collect();
    assert!(keys.contains(&"greeting"));
    assert!(keys.contains(&"welcome_message"));
}

// ── Integration test 2: parse → validate → merge → verify ──

#[test]
fn parse_validate_merge_roundtrip() {
    let mut file = parser::parse(SIMPLE_FIXTURE).unwrap();

    let translations = vec![CompletedTranslation {
        key: "welcome_message".to_string(),
        locale: "uk".to_string(),
        value: "Ласкаво просимо до застосунку".to_string(),
        plural_forms: None,
        substitution_name: None,
    }];

    // Validate first
    let rejected = validator::validate_translations(&file, &translations);
    assert!(
        rejected.is_empty(),
        "valid translation should not be rejected"
    );

    // Merge
    let result = merger::merge_translations(&mut file, &translations);
    assert_eq!(result.accepted, 1);
    assert!(result.rejected.is_empty());

    // Verify: welcome_message should now be translated for "uk"
    let (batch, total) = extractor::get_untranslated(&file, "uk", 100, 0).unwrap();
    assert_eq!(total, 0, "no more untranslated keys for uk");
    assert!(batch.is_empty());
}

// ── Integration test 3: format output matches Xcode style ──

#[test]
fn format_preserves_xcode_style() {
    let file = parser::parse(SIMPLE_FIXTURE).unwrap();
    let formatted = formatter::format_xcstrings(&file).unwrap();

    // Xcode uses " : " (space-colon-space)
    assert!(formatted.contains("\"sourceLanguage\" : \"en\""));
    assert!(formatted.contains("\"state\" : \"translated\""));
    // Must end with newline
    assert!(formatted.ends_with('\n'));
    // Colons inside string values should not be affected
    // (no string values with colons in simple fixture, but structural ones should be correct)
}

// ── Integration test 4: specifier mismatch rejected ──

#[test]
fn specifier_mismatch_rejected() {
    let json = r#"{
        "sourceLanguage": "en",
        "strings": {
            "greeting": {
                "localizations": {
                    "en": {
                        "stringUnit": {
                            "state": "translated",
                            "value": "Hello %@"
                        }
                    }
                }
            }
        },
        "version": "1.0"
    }"#;
    let file = parser::parse(json).unwrap();

    // Submit translation without the required %@ specifier
    let translations = vec![CompletedTranslation {
        key: "greeting".to_string(),
        locale: "uk".to_string(),
        value: "Привіт".to_string(), // missing %@
        plural_forms: None,
        substitution_name: None,
    }];

    let rejected = validator::validate_translations(&file, &translations);
    assert_eq!(rejected.len(), 1);
    assert!(rejected[0].reason.contains("mismatch"));
}

// ── Integration test 5: should_not_translate filtered correctly ──

#[test]
fn should_not_translate_filtered_in_flow() {
    let file = parser::parse(SHOULD_NOT_TRANSLATE_FIXTURE).unwrap();

    let summary = parser::summarize(&file);
    assert_eq!(summary.total_keys, 2);
    assert_eq!(summary.translatable_keys, 1);

    let (batch, total) = extractor::get_untranslated(&file, "de", 100, 0).unwrap();
    assert_eq!(total, 1);
    assert_eq!(batch[0].key, "hello");

    // Trying to translate shouldTranslate=false key should be rejected
    let translations = vec![CompletedTranslation {
        key: "CFBundleName".to_string(),
        locale: "de".to_string(),
        value: "MeineApp".to_string(),
        plural_forms: None,
        substitution_name: None,
    }];
    let rejected = validator::validate_translations(&file, &translations);
    assert_eq!(rejected.len(), 1);
    assert!(rejected[0].reason.contains("shouldTranslate=false"));
}

// ── Integration test 6: merge multiple translations sequentially ──

#[test]
fn sequential_merges_no_corruption() {
    let mut file = parser::parse(SIMPLE_FIXTURE).unwrap();

    // First merge: translate greeting to de
    let t1 = vec![CompletedTranslation {
        key: "greeting".to_string(),
        locale: "de".to_string(),
        value: "Hallo".to_string(),
        plural_forms: None,
        substitution_name: None,
    }];
    let r1 = merger::merge_translations(&mut file, &t1);
    assert_eq!(r1.accepted, 1);

    // Second merge: translate welcome_message to de
    let t2 = vec![CompletedTranslation {
        key: "welcome_message".to_string(),
        locale: "de".to_string(),
        value: "Willkommen in der App".to_string(),
        plural_forms: None,
        substitution_name: None,
    }];
    let r2 = merger::merge_translations(&mut file, &t2);
    assert_eq!(r2.accepted, 1);

    // Both translations should exist, and original en/uk should be preserved
    let (batch, total) = extractor::get_untranslated(&file, "de", 100, 0).unwrap();
    assert_eq!(total, 0);
    assert!(batch.is_empty());

    // Verify original uk translation is still intact
    let greeting = &file.strings["greeting"];
    let locs = greeting.localizations.as_ref().unwrap();
    let uk = locs["uk"].string_unit.as_ref().unwrap();
    assert_eq!(uk.value, "Привіт");

    // Verify formatted output is valid JSON
    let formatted = formatter::format_xcstrings(&file).unwrap();
    let reparsed = parser::parse(&formatted).unwrap();
    assert_eq!(reparsed.strings.len(), 2);
}

// ── Integration test 7: full roundtrip through MemoryStore ──

#[test]
fn full_roundtrip_with_memory_store() {
    let store = MemoryStore::new();
    let path = PathBuf::from("/test/Localizable.xcstrings");

    store.add_file(&path, SIMPLE_FIXTURE);

    // Read from store
    let content = store.get_content(&path).unwrap();
    let mut file = parser::parse(&content).unwrap();

    // Get untranslated for "de"
    let (batch, _) = extractor::get_untranslated(&file, "de", 100, 0).unwrap();
    assert_eq!(batch.len(), 2);

    // Translate and merge
    let translations: Vec<CompletedTranslation> = batch
        .iter()
        .map(|unit| CompletedTranslation {
            key: unit.key.clone(),
            locale: "de".to_string(),
            value: format!("DE: {}", unit.source_text),
            plural_forms: None,
            substitution_name: None,
        })
        .collect();

    let rejected = validator::validate_translations(&file, &translations);
    assert!(rejected.is_empty());

    let result = merger::merge_translations(&mut file, &translations);
    assert_eq!(result.accepted, 2);

    // Format and write back to store
    let formatted = formatter::format_xcstrings(&file).unwrap();
    store.add_file(&path, &formatted);

    // Re-read and verify
    let content2 = store.get_content(&path).unwrap();
    let file2 = parser::parse(&content2).unwrap();

    let (batch2, total2) = extractor::get_untranslated(&file2, "de", 100, 0).unwrap();
    assert_eq!(total2, 0);
    assert!(batch2.is_empty());
}

// ── Xcode golden fixture roundtrip ──

#[test]
fn xcode_generated_roundtrip_byte_identical() {
    let file = parser::parse(GOLDEN).unwrap();

    // Verify summary
    let summary = parser::summarize(&file);
    assert_eq!(summary.source_language, "en");
    assert_eq!(summary.total_keys, 638);
    assert!(summary.locales.contains(&"uk".to_string()));
    assert!(summary.locales.contains(&"de".to_string()));

    // Format back — verify structural roundtrip (parse → format → parse produces same data)
    let formatted = formatter::format_xcstrings(&file).unwrap();
    let reparsed = parser::parse(&formatted).unwrap();
    assert_eq!(reparsed.strings.len(), file.strings.len());
    assert_eq!(reparsed.source_language, file.source_language);
    assert_eq!(reparsed.version, file.version);

    // Verify Xcode colon formatting preserved
    assert!(formatted.contains("\"sourceLanguage\" : \"en\""));
    assert!(formatted.contains("\"state\" : \"translated\""));
    assert!(formatted.ends_with('\n'));

    // Verify key order preserved (IndexMap insertion order = Xcode's Finder-like sort)
    let orig_keys: Vec<&str> = file.strings.keys().map(|s| s.as_str()).collect();
    let round_keys: Vec<&str> = reparsed.strings.keys().map(|s| s.as_str()).collect();
    assert_eq!(
        orig_keys, round_keys,
        "key order must be preserved through roundtrip"
    );
}

#[test]
fn xcode_generated_get_untranslated() {
    let file = parser::parse(GOLDEN).unwrap();

    // All 9 locales should exist and have translations
    let summary = parser::summarize(&file);
    assert_eq!(summary.locales.len(), 9);

    // Try to get untranslated for a locale that exists (should be 0 or few)
    let (batch, total) = extractor::get_untranslated(&file, "uk", 100, 0).unwrap();
    // This real file has all keys translated for uk
    assert_eq!(
        total, 0,
        "Xcode generated file should have all keys translated for uk"
    );
    assert!(batch.is_empty());
}

#[test]
fn xcode_generated_submit_and_reformat() {
    let mut file = parser::parse(GOLDEN).unwrap();

    // Add a translation for a hypothetical new locale "ko"
    let translations = vec![CompletedTranslation {
        key: "Available Products".to_string(),
        locale: "ko".to_string(),
        value: "사용 가능한 제품".to_string(),
        plural_forms: None,
        substitution_name: None,
    }];

    let rejected = validator::validate_translations(&file, &translations);
    assert!(rejected.is_empty());

    let result = merger::merge_translations(&mut file, &translations);
    assert_eq!(result.accepted, 1);

    // Format and re-parse — must survive roundtrip
    let formatted = formatter::format_xcstrings(&file).unwrap();
    let reparsed = parser::parse(&formatted).unwrap();
    assert_eq!(reparsed.strings.len(), 638);

    // Verify new translation exists
    let ko_loc = reparsed.strings["Available Products"]
        .localizations
        .as_ref()
        .unwrap()["ko"]
        .string_unit
        .as_ref()
        .unwrap();
    assert_eq!(ko_loc.value, "사용 가능한 제품");
}

// ── Phase 2 integration tests ──

#[test]
fn coverage_full_flow() {
    let file = parser::parse(GOLDEN).unwrap();
    let report = coverage::get_coverage(&file);

    assert_eq!(report.source_language, "en");
    assert_eq!(report.total_keys, 638);
    assert!(report.translatable_keys > 0);
    // Golden file has 9 locales
    assert_eq!(report.locales.len(), 9);
    // All locales should have high coverage (>90%)
    for lc in &report.locales {
        assert!(
            lc.percentage > 90.0,
            "locale {} has only {:.1}% coverage",
            lc.locale,
            lc.percentage
        );
    }
    // Locales should be sorted alphabetically
    let locale_codes: Vec<&str> = report.locales.iter().map(|l| l.locale.as_str()).collect();
    let mut sorted = locale_codes.clone();
    sorted.sort();
    assert_eq!(locale_codes, sorted, "locales should be sorted");
}

#[test]
fn add_locale_then_get_untranslated() {
    let mut file = parser::parse(SIMPLE_FIXTURE).unwrap();
    let translatable = file.strings.values().filter(|e| e.should_translate).count();

    let added = locale::add_locale(&mut file, "ko").unwrap();
    assert_eq!(added, translatable);

    // New locale should have all keys as untranslated (state=New, empty value)
    let (batch, total) = extractor::get_untranslated(&file, "ko", 100, 0).unwrap();
    assert_eq!(total, translatable);
    assert_eq!(batch.len(), translatable);
}

#[test]
fn validate_after_bad_submit() {
    // File with specifier mismatch between source and translation
    let json = r#"{
        "sourceLanguage": "en",
        "strings": {
            "msg": {
                "localizations": {
                    "en": { "stringUnit": { "state": "translated", "value": "Hello %@" } },
                    "uk": { "stringUnit": { "state": "translated", "value": "Привіт" } }
                }
            }
        },
        "version": "1.0"
    }"#;
    let file_with_bad = parser::parse(json).unwrap();

    let reports = file_validator::validate_file(&file_with_bad, Some("uk"));
    assert_eq!(reports.len(), 1);
    assert_eq!(reports[0].locale, "uk");
    assert!(
        !reports[0].errors.is_empty(),
        "should have specifier mismatch error"
    );
    assert!(
        reports[0]
            .errors
            .iter()
            .any(|e| e.issue_type.contains("specifier"))
    );
}

#[test]
fn stale_keys_from_fixture() {
    let file = parser::parse(WITH_STALE).unwrap();

    let (batch, total) = extractor::get_stale(&file, "uk", 100, 0).unwrap();
    // with_stale.xcstrings has 2 stale+translatable keys: removed_feature, renamed_key
    // (no_translate_stale has shouldTranslate=false)
    assert_eq!(total, 2);
    assert_eq!(batch.len(), 2);

    let keys: Vec<&str> = batch.iter().map(|u| u.key.as_str()).collect();
    assert!(keys.contains(&"removed_feature"));
    assert!(keys.contains(&"renamed_key"));
}

#[test]
fn add_locale_format_roundtrip() {
    let mut file = parser::parse(SIMPLE_FIXTURE).unwrap();

    locale::add_locale(&mut file, "ja").unwrap();

    // Format and re-parse — locale must survive roundtrip
    let formatted = formatter::format_xcstrings(&file).unwrap();
    let reparsed = parser::parse(&formatted).unwrap();

    // Verify "ja" exists in the reparsed file
    let locales = locale::list_locales(&reparsed);
    assert!(
        locales.iter().any(|l| l.locale == "ja"),
        "ja locale should exist after roundtrip"
    );

    // Verify Xcode formatting
    assert!(formatted.contains("\"state\" : \"new\""));
    assert!(formatted.ends_with('\n'));
}

// ── Snapshot tests ──

#[test]
fn snapshot_roundtrip_formatting() {
    let file = parser::parse(SIMPLE_FIXTURE).unwrap();
    let formatted = formatter::format_xcstrings(&file).unwrap();
    insta::assert_snapshot!(formatted);
}

#[test]
fn snapshot_file_summary() {
    let file = parser::parse(SIMPLE_FIXTURE).unwrap();
    let summary = parser::summarize(&file);
    insta::assert_json_snapshot!(summary);
}

#[test]
fn snapshot_untranslated_batch() {
    let file = parser::parse(SIMPLE_FIXTURE).unwrap();
    let (batch, _) = extractor::get_untranslated(&file, "de", 100, 0).unwrap();
    insta::assert_json_snapshot!(batch);
}

// ── Phase 3 integration tests ──

#[test]
fn plural_extract_then_submit() {
    let mut file = parser::parse(WITH_PLURALS).unwrap();

    // Get untranslated plural keys for "de"
    let (batch, total) = plural_extractor::get_untranslated_plurals(&file, "de", 100, 0).unwrap();
    assert!(total > 0, "should have untranslated plural keys for de");

    // Find "days_remaining" — needs plural translation
    let days = batch.iter().find(|u| u.key == "days_remaining").unwrap();
    assert!(days.required_forms.contains(&"one".to_string()));
    assert!(days.required_forms.contains(&"other".to_string()));

    // Submit plural forms
    let mut plural_forms = std::collections::BTreeMap::new();
    plural_forms.insert("one".to_string(), "%lld Tag verbleibend".to_string());
    plural_forms.insert("other".to_string(), "%lld Tage verbleibend".to_string());

    let translations = vec![CompletedTranslation {
        key: "days_remaining".to_string(),
        locale: "de".to_string(),
        value: String::new(),
        plural_forms: Some(plural_forms),
        substitution_name: None,
    }];

    // Note: validator specifier check uses string_unit fallback (key name) for
    // plural-only keys, so we skip validate_translations here and go straight
    // to merge which is format-agnostic.

    let result = merger::merge_translations(&mut file, &translations);
    assert_eq!(result.accepted, 1);

    // Verify merged correctly
    let locs = file.strings["days_remaining"]
        .localizations
        .as_ref()
        .unwrap();
    let de = &locs["de"];
    let plural = de.variations.as_ref().unwrap().plural.as_ref().unwrap();
    assert_eq!(plural["one"].string_unit.value, "%lld Tag verbleibend");
    assert_eq!(plural["other"].string_unit.value, "%lld Tage verbleibend");
}

#[test]
fn substitution_roundtrip() {
    let mut file = parser::parse(WITH_SUBSTITUTIONS).unwrap();

    // Get untranslated plurals — should find substitution keys
    let (batch, total) = plural_extractor::get_untranslated_plurals(&file, "de", 100, 0).unwrap();
    assert!(total > 0);

    let bird = batch.iter().find(|u| u.key == "bird_sighting").unwrap();
    assert!(bird.has_substitutions);

    // Submit substitution translation with substitution_name
    let mut plural_forms = std::collections::BTreeMap::new();
    plural_forms.insert("one".to_string(), "%arg Vogel".to_string());
    plural_forms.insert("other".to_string(), "%arg Vögel".to_string());

    let translations = vec![CompletedTranslation {
        key: "bird_sighting".to_string(),
        locale: "de".to_string(),
        value: String::new(),
        plural_forms: Some(plural_forms),
        substitution_name: Some("BIRDS".to_string()),
    }];

    let result = merger::merge_translations(&mut file, &translations);
    assert_eq!(result.accepted, 1);

    // Verify written to substitutions
    let locs = file.strings["bird_sighting"]
        .localizations
        .as_ref()
        .unwrap();
    let de = &locs["de"];
    let subs = de.substitutions.as_ref().unwrap();
    let birds = &subs["BIRDS"];
    let one_val = birds["variations"]["plural"]["one"]["stringUnit"]["value"]
        .as_str()
        .unwrap();
    assert_eq!(one_val, "%arg Vogel");

    // Format roundtrip
    let formatted = formatter::format_xcstrings(&file).unwrap();
    let reparsed = parser::parse(&formatted).unwrap();
    let de2 = &reparsed.strings["bird_sighting"]
        .localizations
        .as_ref()
        .unwrap()["de"];
    let subs2 = de2.substitutions.as_ref().unwrap();
    assert!(subs2.contains_key("BIRDS"));
}

#[test]
fn context_nearby_keys() {
    // Build a file with dot-separated keys to test prefix matching
    let json = r#"{
        "sourceLanguage": "en",
        "strings": {
            "settings.notifications.title": {
                "localizations": {
                    "en": { "stringUnit": { "state": "translated", "value": "Notifications" } }
                }
            },
            "settings.notifications.body": {
                "localizations": {
                    "en": { "stringUnit": { "state": "translated", "value": "Body text" } },
                    "uk": { "stringUnit": { "state": "translated", "value": "Текст тіла" } }
                }
            },
            "settings.general.title": {
                "localizations": {
                    "en": { "stringUnit": { "state": "translated", "value": "General" } }
                }
            },
            "login.title": {
                "localizations": {
                    "en": { "stringUnit": { "state": "translated", "value": "Login" } }
                }
            }
        },
        "version": "1.0"
    }"#;
    let file = parser::parse(json).unwrap();

    let result = context::get_context(&file, "settings.notifications.title", "uk", 10);
    assert!(!result.is_empty());

    // First result should be the key with longest shared prefix
    assert_eq!(result[0].key, "settings.notifications.body");
    assert_eq!(result[0].source_text, "Body text");
    assert_eq!(result[0].translated_text.as_deref(), Some("Текст тіла"));

    // Second should be settings.general.title (1 shared segment)
    assert_eq!(result[1].key, "settings.general.title");

    // Third should be login.title (0 shared segments)
    assert_eq!(result[2].key, "login.title");
}

#[test]
fn device_variant_extraction() {
    let file = parser::parse(WITH_DEVICE_VARIANTS).unwrap();

    let (batch, total) = plural_extractor::get_untranslated_plurals(&file, "de", 100, 0).unwrap();
    assert!(total > 0, "should find device variant keys");

    let tap = batch.iter().find(|u| u.key == "tap_action").unwrap();
    assert!(!tap.device_forms.is_empty());
    assert!(tap.device_forms.contains(&"iphone".to_string()));
    assert!(tap.device_forms.contains(&"ipad".to_string()));
    assert!(tap.device_forms.contains(&"mac".to_string()));
}

#[test]
fn plural_validate_then_merge_full_flow() {
    let mut file = parser::parse(WITH_PLURALS).unwrap();

    // Get plural keys for "de"
    let (batch, _) = plural_extractor::get_untranslated_plurals(&file, "de", 100, 0).unwrap();
    let days = batch.iter().find(|u| u.key == "days_remaining").unwrap();

    // Build valid plural forms using required_forms from PluralUnit
    let mut plural_forms = std::collections::BTreeMap::new();
    plural_forms.insert("one".to_string(), "%lld Tag verbleibend".to_string());
    plural_forms.insert("other".to_string(), "%lld Tage verbleibend".to_string());

    let translations = vec![CompletedTranslation {
        key: days.key.clone(),
        locale: "de".to_string(),
        value: String::new(),
        plural_forms: Some(plural_forms),
        substitution_name: None,
    }];

    // Validate — should pass (validator now handles plural-only source keys)
    let rejected = validator::validate_translations(&file, &translations);
    assert!(
        rejected.is_empty(),
        "valid plural translation should pass validation: {:?}",
        rejected
    );

    // Merge
    let result = merger::merge_translations(&mut file, &translations);
    assert_eq!(result.accepted, 1);

    // Format roundtrip
    let formatted = formatter::format_xcstrings(&file).unwrap();
    let reparsed = parser::parse(&formatted).unwrap();
    let de = &reparsed.strings["days_remaining"]
        .localizations
        .as_ref()
        .unwrap()["de"];
    let plural = de.variations.as_ref().unwrap().plural.as_ref().unwrap();
    assert_eq!(plural["one"].string_unit.value, "%lld Tag verbleibend");
}

// ── Phase 4 fixture tests ──

#[test]
fn interpolation_fixture_specifiers() {
    let file = parser::parse(WITH_INTERPOLATION).unwrap();

    assert_eq!(file.strings.len(), 3);

    // greeting_name source contains %@
    let greeting = &file.strings["greeting_name"];
    let en = greeting.localizations.as_ref().unwrap()["en"]
        .string_unit
        .as_ref()
        .unwrap();
    assert!(en.value.contains("%@"), "greeting_name should contain %@");

    // items_count_format source contains both %lld and %@
    let items = &file.strings["items_count_format"];
    let en_items = items.localizations.as_ref().unwrap()["en"]
        .string_unit
        .as_ref()
        .unwrap();
    assert!(
        en_items.value.contains("%lld"),
        "items_count_format should contain %lld"
    );
    assert!(
        en_items.value.contains("%@"),
        "items_count_format should contain %@"
    );

    // Long key name survives parse
    let long_key = "MyApp.Features.Settings.Notifications.PushNotificationPermissionAlert.Title";
    assert!(
        file.strings.contains_key(long_key),
        "long auto-generated key should survive parse"
    );
}

#[test]
fn interpolation_long_key_full_flow() {
    let file = parser::parse(WITH_INTERPOLATION).unwrap();

    let long_key = "MyApp.Features.Settings.Notifications.PushNotificationPermissionAlert.Title";

    let (batch, total) = extractor::get_untranslated(&file, "de", 100, 0).unwrap();
    assert_eq!(total, 3, "all 3 keys should be untranslated for de");

    let keys: Vec<&str> = batch.iter().map(|u| u.key.as_str()).collect();
    assert!(
        keys.contains(&long_key),
        "long key should appear in untranslated batch"
    );
}

#[test]
fn multiline_roundtrip() {
    let file = parser::parse(WITH_MULTILINE).unwrap();

    // Verify multiline_message source value contains \n
    let msg = &file.strings["multiline_message"];
    let en = msg.localizations.as_ref().unwrap()["en"]
        .string_unit
        .as_ref()
        .unwrap();
    assert!(
        en.value.contains('\n'),
        "multiline_message source should contain newline characters"
    );

    // Format → re-parse → format must be idempotent
    let formatted1 = formatter::format_xcstrings(&file).unwrap();
    let reparsed = parser::parse(&formatted1).unwrap();
    let formatted2 = formatter::format_xcstrings(&reparsed).unwrap();

    assert_eq!(
        formatted1, formatted2,
        "multiline fixture formatting must be idempotent"
    );
}

#[test]
fn multiline_specifier_safe() {
    let mut file = parser::parse(WITH_MULTILINE).unwrap();

    let translations = vec![CompletedTranslation {
        key: "multiline_message".to_string(),
        locale: "uk".to_string(),
        value: "Рядок 1\nРядок 2\nРядок 3".to_string(),
        plural_forms: None,
        substitution_name: None,
    }];

    let rejected = validator::validate_translations(&file, &translations);
    assert!(
        rejected.is_empty(),
        "multiline translation should pass validation"
    );

    let result = merger::merge_translations(&mut file, &translations);
    assert_eq!(result.accepted, 1);

    // Verify merged value preserves newlines
    let uk = file.strings["multiline_message"]
        .localizations
        .as_ref()
        .unwrap()["uk"]
        .string_unit
        .as_ref()
        .unwrap();
    assert!(
        uk.value.contains('\n'),
        "merged translation should preserve newline characters"
    );
    assert_eq!(uk.value, "Рядок 1\nРядок 2\nРядок 3");
}

// ── Phase 5 integration tests ──

#[test]
fn multi_file_parse_and_switch() {
    // File A: two translatable keys
    let file_a = parser::parse(SIMPLE_FIXTURE).unwrap();
    let summary_a = parser::summarize(&file_a);
    assert_eq!(summary_a.translatable_keys, 2);

    // File B: a different file with one key
    let json_b = r#"{
        "sourceLanguage": "en",
        "strings": {
            "logout_button": {
                "localizations": {
                    "en": { "stringUnit": { "state": "translated", "value": "Log Out" } }
                }
            }
        },
        "version": "1.0"
    }"#;
    let file_b = parser::parse(json_b).unwrap();
    let summary_b = parser::summarize(&file_b);
    assert_eq!(summary_b.translatable_keys, 1);

    // get_untranslated on file B (the "active" file)
    let (batch_b, total_b) = extractor::get_untranslated(&file_b, "de", 100, 0).unwrap();
    assert_eq!(total_b, 1);
    assert_eq!(batch_b[0].key, "logout_button");

    // get_untranslated on file A (switch back)
    let (batch_a, total_a) = extractor::get_untranslated(&file_a, "de", 100, 0).unwrap();
    assert_eq!(total_a, 2);
    let keys_a: Vec<&str> = batch_a.iter().map(|u| u.key.as_str()).collect();
    assert!(keys_a.contains(&"greeting"));
    assert!(keys_a.contains(&"welcome_message"));
}

#[test]
fn add_then_remove_locale_roundtrip() {
    let mut file = parser::parse(SIMPLE_FIXTURE).unwrap();
    let source_lang = file.source_language.clone();

    // Add locale "fr"
    let added = locale::add_locale(&mut file, "fr").unwrap();
    assert!(added > 0);

    // Verify "fr" exists
    let locales = locale::list_locales(&file);
    assert!(
        locales.iter().any(|l| l.locale == "fr"),
        "fr should exist after add"
    );

    // Remove locale "fr"
    let removed = locale::remove_locale(&mut file, "fr", &source_lang).unwrap();
    assert_eq!(removed, added);

    // Verify "fr" gone
    let locales = locale::list_locales(&file);
    assert!(
        !locales.iter().any(|l| l.locale == "fr"),
        "fr should be gone after remove"
    );

    // Verify file is still valid (parse roundtrip)
    let formatted = formatter::format_xcstrings(&file).unwrap();
    let reparsed = parser::parse(&formatted).unwrap();
    assert_eq!(reparsed.strings.len(), file.strings.len());
}

#[test]
fn batch_retry_continue_on_error_writes_valid() {
    let json = r#"{
        "sourceLanguage": "en",
        "strings": {
            "plain_key": {
                "localizations": {
                    "en": { "stringUnit": { "state": "translated", "value": "Hello" } }
                }
            },
            "specifier_key": {
                "localizations": {
                    "en": { "stringUnit": { "state": "translated", "value": "Hello %@" } }
                }
            }
        },
        "version": "1.0"
    }"#;
    let mut file = parser::parse(json).unwrap();

    let translations = vec![
        // Valid: plain_key has no specifiers, translation has none
        CompletedTranslation {
            key: "plain_key".to_string(),
            locale: "de".to_string(),
            value: "Hallo".to_string(),
            plural_forms: None,
            substitution_name: None,
        },
        // Invalid: specifier_key needs %@ but translation lacks it
        CompletedTranslation {
            key: "specifier_key".to_string(),
            locale: "de".to_string(),
            value: "Hallo ohne Spezifizierer".to_string(),
            plural_forms: None,
            substitution_name: None,
        },
    ];

    // Validate to find rejected ones
    let rejected = validator::validate_translations(&file, &translations);
    assert_eq!(rejected.len(), 1, "specifier_key should be rejected");
    assert_eq!(rejected[0].key, "specifier_key");

    // Filter out rejected and merge only valid translations
    let rejected_keys: std::collections::HashSet<&str> =
        rejected.iter().map(|r| r.key.as_str()).collect();
    let valid: Vec<CompletedTranslation> = translations
        .into_iter()
        .filter(|t| !rejected_keys.contains(t.key.as_str()))
        .collect();

    let result = merger::merge_translations(&mut file, &valid);
    assert_eq!(result.accepted, 1);
    assert!(result.accepted_keys.contains(&"plain_key".to_string()));

    // Verify plain_key is translated, specifier_key is not
    let (batch, total) = extractor::get_untranslated(&file, "de", 100, 0).unwrap();
    assert_eq!(total, 1);
    assert_eq!(batch[0].key, "specifier_key");
}

#[test]
fn diff_detects_external_changes() {
    let old = parser::parse(SIMPLE_FIXTURE).unwrap();

    // Build a modified version: add a key, remove a key, change source text
    let modified_json = r#"{
        "sourceLanguage": "en",
        "strings": {
            "greeting": {
                "localizations": {
                    "en": { "stringUnit": { "state": "translated", "value": "Hi there!" } },
                    "uk": { "stringUnit": { "state": "translated", "value": "Привіт" } }
                }
            },
            "new_key": {
                "localizations": {
                    "en": { "stringUnit": { "state": "translated", "value": "New!" } }
                }
            }
        },
        "version": "1.0"
    }"#;
    let new = parser::parse(modified_json).unwrap();

    let report = diff::compute_diff(&old, &new);

    // "new_key" was added
    assert!(report.added.contains(&"new_key".to_string()));
    // "welcome_message" was removed
    assert!(report.removed.contains(&"welcome_message".to_string()));
    // "greeting" source changed from "Hello" to "Hi there!"
    assert_eq!(report.modified.len(), 1);
    assert_eq!(report.modified[0].key, "greeting");
    assert_eq!(report.modified[0].old_value, "Hello");
    assert_eq!(report.modified[0].new_value, "Hi there!");
}

#[test]
fn xliff_export_import_roundtrip() {
    let file = parser::parse(SIMPLE_FIXTURE).unwrap();

    // Export to XLIFF (all entries, not just untranslated)
    let (xml, exported_count) =
        xliff::export_xliff(&file, "uk", "Localizable.xcstrings", false).unwrap();
    assert!(exported_count > 0);

    // Import the XLIFF back
    let (locale, translations) = xliff::import_xliff(&xml).unwrap();
    assert_eq!(locale, "uk");

    // Only entries with non-empty target text are imported.
    // "greeting" has uk translation -> imported. "welcome_message" does not -> skipped.
    assert!(
        !translations.is_empty(),
        "should import at least one translation"
    );

    // The imported translations should have the correct locale
    for t in &translations {
        assert_eq!(t.locale, "uk");
    }

    // Verify the greeting translation survived the roundtrip
    let greeting = translations.iter().find(|t| t.key == "greeting");
    assert!(greeting.is_some());
    assert_eq!(greeting.unwrap().value, "Привіт");
}

#[test]
fn glossary_create_update_read() {
    let mut g = glossary::parse_glossary(None).unwrap();

    // Add terms
    let mut terms = std::collections::BTreeMap::new();
    terms.insert("Settings".to_string(), "Einstellungen".to_string());
    terms.insert("Cancel".to_string(), "Abbrechen".to_string());
    let count = glossary::update_entries(&mut g, "en", "de", terms);
    assert_eq!(count, 2);

    // Read back
    let entries = glossary::get_entries(&g, "en", "de", None);
    assert_eq!(entries.len(), 2);
    assert_eq!(entries["Settings"], "Einstellungen");
    assert_eq!(entries["Cancel"], "Abbrechen");

    // Overwrite "Settings"
    let mut update = std::collections::BTreeMap::new();
    update.insert("Settings".to_string(), "Optionen".to_string());
    glossary::update_entries(&mut g, "en", "de", update);

    let entries = glossary::get_entries(&g, "en", "de", None);
    assert_eq!(entries["Settings"], "Optionen");
    // Cancel should still be there
    assert_eq!(entries["Cancel"], "Abbrechen");

    // Filter
    let filtered = glossary::get_entries(&g, "en", "de", Some("cancel"));
    assert_eq!(filtered.len(), 1);
    assert!(filtered.contains_key("Cancel"));

    // Serialize and re-parse
    let json = glossary::serialize_glossary(&g).unwrap();
    let reloaded = glossary::parse_glossary(Some(&json)).unwrap();
    let entries = glossary::get_entries(&reloaded, "en", "de", None);
    assert_eq!(entries.len(), 2);
}

// ── Property-based tests ──

mod proptest_tests {
    use super::*;
    use proptest::prelude::*;

    fn arb_translation_state() -> impl Strategy<Value = TranslationState> {
        prop_oneof![
            Just(TranslationState::New),
            Just(TranslationState::Translated),
            Just(TranslationState::NeedsReview),
            Just(TranslationState::Stale),
        ]
    }

    fn arb_string_entry() -> impl Strategy<Value = (String, StringEntry)> {
        ("[a-z_]{1,30}", any::<bool>(), arb_translation_state()).prop_map(
            |(key, should_translate, state)| {
                let mut localizations = IndexMap::new();
                localizations.insert(
                    "en".to_string(),
                    Localization {
                        string_unit: Some(StringUnit {
                            state: state.clone(),
                            value: format!("Value for {key}"),
                        }),
                        variations: None,
                        substitutions: None,
                    },
                );
                (
                    key,
                    StringEntry {
                        extraction_state: None,
                        should_translate,
                        comment: None,
                        localizations: Some(localizations),
                    },
                )
            },
        )
    }

    fn arb_xcstrings_file() -> impl Strategy<Value = XcStringsFile> {
        proptest::collection::vec(arb_string_entry(), 1..20).prop_map(|entries| {
            let strings: IndexMap<String, StringEntry> = entries.into_iter().collect();
            XcStringsFile {
                source_language: "en".to_string(),
                strings,
                version: "1.0".to_string(),
            }
        })
    }

    proptest! {
        #[test]
        fn parse_format_roundtrip(file in arb_xcstrings_file()) {
            // format → parse → format must produce identical output
            let formatted1 = formatter::format_xcstrings(&file).unwrap();
            let reparsed = parser::parse(&formatted1).unwrap();
            let formatted2 = formatter::format_xcstrings(&reparsed).unwrap();
            prop_assert_eq!(formatted1, formatted2, "roundtrip must be idempotent");
        }

        #[test]
        fn merge_never_decreases_key_count(file in arb_xcstrings_file()) {
            let original_count = file.strings.len();
            let mut file = file;

            // Translate all translatable keys to "de"
            let translations: Vec<CompletedTranslation> = file.strings.iter()
                .filter(|(_, e)| e.should_translate)
                .map(|(k, _)| CompletedTranslation {
                    key: k.clone(),
                    locale: "de".to_string(),
                    value: format!("DE: {k}"),
                    plural_forms: None,
                    substitution_name: None,
                })
                .collect();

            if !translations.is_empty() {
                merger::merge_translations(&mut file, &translations);
            }

            prop_assert!(file.strings.len() >= original_count,
                "merge must never remove keys");
        }

        #[test]
        fn coverage_monotonic_after_submit(file in arb_xcstrings_file()) {
            // Get untranslated count before
            let translatable = file.strings.values().filter(|e| e.should_translate).count();
            let (_, before) = extractor::get_untranslated(&file, "de", 100, 0).unwrap();

            // Translate everything
            let mut file = file;
            let translations: Vec<CompletedTranslation> = file.strings.iter()
                .filter(|(_, e)| e.should_translate)
                .map(|(k, _)| CompletedTranslation {
                    key: k.clone(),
                    locale: "de".to_string(),
                    value: format!("DE: {k}"),
                    plural_forms: None,
                    substitution_name: None,
                })
                .collect();

            if !translations.is_empty() {
                merger::merge_translations(&mut file, &translations);
            }

            let (_, after) = extractor::get_untranslated(&file, "de", 100, 0).unwrap();
            prop_assert!(after <= before,
                "submitting translations must not increase untranslated count: before={before}, after={after}, translatable={translatable}");
        }

        // ── Phase 5 property tests ──

        #[test]
        fn diff_identity(keys in prop::collection::vec("[a-z]{1,10}", 0..20)) {
            // Deduplicate keys (IndexMap collapses dupes)
            let mut strings = IndexMap::new();
            for key in &keys {
                let mut localizations = IndexMap::new();
                localizations.insert(
                    "en".to_string(),
                    Localization {
                        string_unit: Some(StringUnit {
                            state: TranslationState::Translated,
                            value: format!("Value for {key}"),
                        }),
                        variations: None,
                        substitutions: None,
                    },
                );
                strings.insert(
                    key.clone(),
                    StringEntry {
                        extraction_state: None,
                        should_translate: true,
                        comment: None,
                        localizations: Some(localizations),
                    },
                );
            }
            let file = XcStringsFile {
                source_language: "en".to_string(),
                strings,
                version: "1.0".to_string(),
            };

            let report = diff::compute_diff(&file, &file);
            prop_assert!(report.added.is_empty(), "diff of identical files should have no added keys");
            prop_assert!(report.removed.is_empty(), "diff of identical files should have no removed keys");
            prop_assert!(report.modified.is_empty(), "diff of identical files should have no modified keys");
        }

        #[test]
        fn remove_add_locale_preserves_key_count(
            keys in prop::collection::vec("[a-z]{1,10}", 1..10)
        ) {
            // Build file with unique keys
            let mut strings = IndexMap::new();
            for key in &keys {
                let mut localizations = IndexMap::new();
                localizations.insert(
                    "en".to_string(),
                    Localization {
                        string_unit: Some(StringUnit {
                            state: TranslationState::Translated,
                            value: format!("Value for {key}"),
                        }),
                        variations: None,
                        substitutions: None,
                    },
                );
                strings.insert(
                    key.clone(),
                    StringEntry {
                        extraction_state: None,
                        should_translate: true,
                        comment: None,
                        localizations: Some(localizations),
                    },
                );
            }
            let mut file = XcStringsFile {
                source_language: "en".to_string(),
                strings,
                version: "1.0".to_string(),
            };

            let translatable = file.strings.values().filter(|e| e.should_translate).count();

            // Add locale "test_xx"
            locale::add_locale(&mut file, "test_xx").unwrap();
            let locales_after_add = locale::list_locales(&file);
            let test_locale = locales_after_add.iter().find(|l| l.locale == "test_xx").unwrap();
            prop_assert_eq!(test_locale.total, translatable);

            // Remove locale "test_xx"
            locale::remove_locale(&mut file, "test_xx", "en").unwrap();
            let locales_after_remove = locale::list_locales(&file);
            prop_assert!(
                !locales_after_remove.iter().any(|l| l.locale == "test_xx"),
                "test_xx locale should be gone after remove"
            );

            // Key count unchanged
            prop_assert_eq!(file.strings.len(), keys.iter().collect::<std::collections::HashSet<_>>().len(),
                "key count should match unique input keys");
        }
    }
}