opencc-sys 0.5.0+1.4.0

OpenCC bindings for Rust
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
/*
 * Open Chinese Convert
 *
 * Copyright 2015-2026 Carbo Kuo and contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <filesystem>
#include <map>
#include <sstream>
#include <unordered_map>
#include <vector>

#include "test/PortableUtil.hpp"

#include "src/Common.hpp"
#include "rapidjson/document.h"
#include "gtest/gtest.h"

#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#else
#include <sys/stat.h>
#endif

#ifdef BAZEL
#include "tools/cpp/runfiles/runfiles.h"
using bazel::tools::cpp::runfiles::Runfiles;
#endif

namespace opencc {

namespace fs = std::filesystem;

#ifdef _WIN32
std::wstring WideFromUtf8(const std::string& utf8) {
  if (utf8.empty()) {
    return L"";
  }
  const int required =
      MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, nullptr, 0);
  if (required <= 1) {
    return L"";
  }
  std::wstring wide(static_cast<size_t>(required), L'\0');
  MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, wide.data(), required);
  wide.resize(static_cast<size_t>(required - 1));
  return wide;
}
#endif

class CommandLineConvertTest : public ::testing::Test {
protected:
  CommandLineConvertTest() { GetCurrentWorkingDirectory(); }

  virtual ~CommandLineConvertTest() { free(originalWorkingDirectory); }

  virtual void SetUp() {
#ifdef BAZEL
    runfiles_.reset(Runfiles::CreateForTest());
#else
    ASSERT_NE("", PROJECT_BINARY_DIR);
    ASSERT_NE("", PROJECT_SOURCE_DIR);
    ASSERT_EQ(0, portable_chdir(PROJECT_BINARY_DIR "/data"));
#endif
  }

  virtual void TearDown() { ASSERT_EQ(0, portable_chdir(originalWorkingDirectory)); }

  std::string GetFileContents(const std::string& fileName) const {
    std::ifstream fs(fileName, std::ios::binary);
    EXPECT_TRUE(fs.is_open()) << fileName;
    const std::string content((std::istreambuf_iterator<char>(fs)),
                              (std::istreambuf_iterator<char>()));
    fs.close();
    return content;
  }

  std::string GetFileContents(const fs::path& fileName) const {
    std::ifstream stream(fileName, std::ios::binary);
    EXPECT_TRUE(stream.is_open()) << fileName.u8string();
    const std::string content((std::istreambuf_iterator<char>(stream)),
                              (std::istreambuf_iterator<char>()));
    stream.close();
    return content;
  }

  void GetCurrentWorkingDirectory() {
    originalWorkingDirectory = portable_getcwd();
  }

  std::string OpenccCommand() const {
#ifdef BAZEL
#ifdef _WIN32
    // On Windows, cc_binary executables have .exe extension in the runfiles manifest
    std::string path = runfiles_->Rlocation("_main/src/tools/command_line.exe");
    if (path.empty()) {
      path = runfiles_->Rlocation("_main/src/tools/command_line");
    }
    return path;
#else
    return runfiles_->Rlocation("_main/src/tools/command_line");
#endif
#else
#ifndef _MSC_VER
    return PROJECT_BINARY_DIR "/src/tools/opencc";
#else
#ifdef NDEBUG
    return PROJECT_BINARY_DIR "/src/tools/Release/opencc.exe";
#else
    return PROJECT_BINARY_DIR "/src/tools/Debug/opencc.exe";
#endif
#endif
#endif
  }

  // Quote a path for use in a shell command, in case the path contains spaces.
  static std::string QuotePath(const std::string& path) {
    return "\"" + path + "\"";
  }

  std::string OutputDirectory() const {
#ifdef BAZEL
    return ::testing::TempDir() + "/";
#else
    return PROJECT_BINARY_DIR "/test/";
#endif
  }

#if defined(BAZEL) && defined(OPENCC_ENABLE_RESOURCE_ZIP_TEST)
  std::string ResourceZipFile() const {
    return runfiles_->Rlocation("_main/data/opencc-resources.zip");
  }

  std::string ResourceOcd2ZipFile() const {
    return runfiles_->Rlocation("_main/data/opencc-resources-ocd2.zip");
  }
#endif

  std::string ConfigurationDirectory() const {
#ifdef BAZEL
    return "";
#else
    return PROJECT_SOURCE_DIR "/data/config/";
#endif
  }

  std::string InputFile(const char* config) const {
    return OutputDirectory() + config + ".in";
  }

  std::string OutputFile(const char* config) const {
    return OutputDirectory() + config + ".out";
  }

  std::string TestCommand(const std::string& config,
                          const std::string& inputFile,
                          const std::string& outputFile,
                          const std::string& measuredResultFile = "",
                          const std::string& extraFlags = "") const {
    std::string cmd = QuotePath(OpenccCommand()) + " -i " +
                      QuotePath(inputFile) + " -o " +
                      QuotePath(outputFile) + " -c " +
                      QuotePath(ConfigurationDirectory() + config + ".json");
    if (!extraFlags.empty()) {
      cmd += " " + extraFlags;
    }
    if (!measuredResultFile.empty()) {
      cmd += " --measured_result " + QuotePath(measuredResultFile);
    }
#ifdef BAZEL
    const std::string dictFile =
        runfiles_->Rlocation("_main/data/dictionary/STCharacters.ocd2");
    const std::string dictDir =
        dictFile.substr(0, dictFile.find_last_of("/\\"));
    const std::string configFile =
        runfiles_->Rlocation("_main/data/config/s2t.json");
    const std::string configDir =
        configFile.substr(0, configFile.find_last_of("/\\"));
    cmd += " --path " + QuotePath(dictDir + "/") +
           " --path " + QuotePath(configDir + "/");
#endif
#ifdef _WIN32
    // On Windows, cmd.exe /C strips the first and last quote characters when
    // the command starts with a quoted path, corrupting the command. Wrapping
    // the entire command in an extra pair of outer quotes causes cmd.exe to
    // strip only those outer quotes, leaving the inner quoted paths intact.
    return "\"" + cmd + "\"";
#else
    return cmd;
#endif
  }

  static int RunCommand(const std::string& cmd) {
#ifdef _WIN32
    return _wsystem(WideFromUtf8(cmd).c_str());
#else
    return system(cmd.c_str());
#endif
  }

#if defined(BAZEL) && defined(OPENCC_ENABLE_RESOURCE_ZIP_TEST)
  std::string TestResourceZipCommand(const std::string& config,
                                     const std::string& inputFile,
                                     const std::string& outputFile) const {
    std::string cmd = QuotePath(OpenccCommand()) + " -i " +
                      QuotePath(inputFile) + " -o " + QuotePath(outputFile) +
                      " -c " + QuotePath(config + ".json") +
                      " --resource-zip " + QuotePath(ResourceZipFile());
#ifdef _WIN32
    return "\"" + cmd + "\"";
#else
    return cmd;
#endif
  }
#endif

  std::string TestCommandWithFlags(const std::string& config,
                                   const std::string& inputFile,
                                   const std::string& outputFile,
                                   const std::string& extraFlags,
                                   const std::string& measuredResultFile = "")
      const {
    return TestCommand(config, inputFile, outputFile, measuredResultFile,
                       extraFlags);
  }

  std::string TestStdinCommand(const std::string& config,
                               const std::string& inputFile,
                               const std::string& outputFile) const {
    std::string cmd = QuotePath(OpenccCommand()) + " -c " +
                      QuotePath(ConfigurationDirectory() + config + ".json");
#ifdef BAZEL
    const std::string dictFile =
        runfiles_->Rlocation("_main/data/dictionary/STCharacters.ocd2");
    const std::string dictDir =
        dictFile.substr(0, dictFile.find_last_of("/\\"));
    const std::string configFile =
        runfiles_->Rlocation("_main/data/config/s2t.json");
    const std::string configDir =
        configFile.substr(0, configFile.find_last_of("/\\"));
    cmd += " --path " + QuotePath(dictDir + "/") +
           " --path " + QuotePath(configDir + "/");
#endif
    cmd += " < " + QuotePath(inputFile) + " > " + QuotePath(outputFile);
#ifdef _WIN32
    return "\"" + cmd + "\"";
#else
    return cmd;
#endif
  }

#ifndef _WIN32
  std::string TestPipeCommand(const std::string& config,
                              const std::string& outputFile) const {
    std::string cmd = "(printf '后台'; sleep 0.1; printf '老板') | " +
                      QuotePath(OpenccCommand()) + " -c " +
                      QuotePath(ConfigurationDirectory() + config + ".json");
#ifdef BAZEL
    const std::string dictFile =
        runfiles_->Rlocation("_main/data/dictionary/STCharacters.ocd2");
    const std::string dictDir =
        dictFile.substr(0, dictFile.find_last_of("/\\"));
    const std::string configFile =
        runfiles_->Rlocation("_main/data/config/s2t.json");
    const std::string configDir =
        configFile.substr(0, configFile.find_last_of("/\\"));
    cmd += " --path " + QuotePath(dictDir + "/") +
           " --path " + QuotePath(configDir + "/");
#endif
    cmd += " > " + QuotePath(outputFile);
    return cmd;
  }
#endif

  char* originalWorkingDirectory;

#ifdef BAZEL
  std::unique_ptr<Runfiles> runfiles_;
#endif
};

struct CaseInput {
  std::string input;
  std::string expected;
};

using CasesByConfig = std::unordered_map<std::string, std::vector<CaseInput>>;

CasesByConfig LoadCases(const std::string& jsonPath) {
  CasesByConfig cases;
  std::string content;
  {
    std::ifstream ifs(jsonPath);
    if (!ifs.is_open()) {
      throw std::runtime_error("Cannot open " + jsonPath);
    }
    std::stringstream buffer;
    buffer << ifs.rdbuf();
    content = buffer.str();
  }

  rapidjson::Document doc;
  doc.Parse<rapidjson::kParseCommentsFlag |
            rapidjson::kParseTrailingCommasFlag>(content.c_str());
  if (doc.HasParseError() || !doc.IsObject() || !doc.HasMember("cases") ||
      !doc["cases"].IsArray()) {
    throw std::runtime_error("Invalid testcases.json format");
  }

  for (auto& entry : doc["cases"].GetArray()) {
    if (!entry.IsObject() || !entry.HasMember("input") ||
        !entry["input"].IsString() || !entry.HasMember("expected") ||
        !entry["expected"].IsObject()) {
      continue;
    }
    const std::string input = entry["input"].GetString();
    for (auto itr = entry["expected"].MemberBegin();
         itr != entry["expected"].MemberEnd(); ++itr) {
      if (!itr->value.IsString()) {
        continue;
      }
      const std::string config = itr->name.GetString();
      cases[config].push_back({input, itr->value.GetString()});
    }
  }
  return cases;
}

TEST_F(CommandLineConvertTest, ConvertFromJson) {
#ifdef BAZEL
  const std::string casesPath =
      runfiles_->Rlocation("_main/test/testcases/testcases.json");
#else
  const std::string casesPath = PROJECT_SOURCE_DIR "/test/testcases/testcases.json";
#endif
  const CasesByConfig cases = LoadCases(casesPath);

  for (const auto& entry : cases) {
    const std::string& config = entry.first;
    const std::string inputFile = InputFile(config.c_str());
    const std::string outputFile = OutputFile(config.c_str());

    // Write inputs into a temp file (one per line).
    {
      std::ofstream ofs(inputFile, std::ios::binary);
      ASSERT_TRUE(ofs.is_open()) << "Failed to open input file for writing: "
                                 << inputFile;
      for (const auto& item : entry.second) {
        ofs << item.input << "\n";
      }
    }

    ASSERT_EQ(0,
              system(TestCommandWithFlags(
                         config, inputFile, outputFile,
                         "--include-tofu-risk-dictionaries")
                         .c_str()));

    // Read outputs and compare line by line.
    std::ifstream ifs(outputFile, std::ios::binary);
    ASSERT_TRUE(ifs.is_open());
    std::string line;
    size_t idx = 0;
    while (std::getline(ifs, line)) {
      if (!line.empty() && line.back() == '\r') {
        line.pop_back(); // normalize Windows CRLF
      }
      ASSERT_LT(idx, entry.second.size());
      EXPECT_EQ(entry.second[idx].expected, line)
          << "config=" << config << " index=" << idx;
      idx++;
    }
    EXPECT_EQ(idx, entry.second.size()) << "config=" << config;
  }
}

TEST_F(CommandLineConvertTest, SkipsTofuRiskDictionariesByDefault) {
  const std::string inputFile = InputFile("tofu_risk_default");
  const std::string outputFile = OutputFile("tofu_risk_default");

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open());
    ofs << "";
  }

  ASSERT_EQ(0, system(TestCommand("t2s", inputFile, outputFile).c_str()));
  EXPECT_EQ("", GetFileContents(outputFile));
}

TEST_F(CommandLineConvertTest, IncludeTofuRiskDictionariesFlagRestoresLegacy) {
  const std::string inputFile = InputFile("tofu_risk_include");
  const std::string outputFile = OutputFile("tofu_risk_include");

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open());
    ofs << "";
  }

  ASSERT_EQ(0, system(TestCommandWithFlags(
                   "t2s", inputFile, outputFile,
                   "--include-tofu-risk-dictionaries")
                   .c_str()));
  EXPECT_EQ("𫝈", GetFileContents(outputFile));
}

#if defined(BAZEL) && defined(OPENCC_ENABLE_RESOURCE_ZIP_TEST)
TEST_F(CommandLineConvertTest, ResourceZipConvertsWithoutResourcePaths) {
  const std::string inputFile = InputFile("resource_zip");
  const std::string outputFile = OutputFile("resource_zip");

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open());
    ofs << "打印机和鼠标";
  }

  ASSERT_EQ(0,
            RunCommand(TestResourceZipCommand("s2twp", inputFile, outputFile)));
  EXPECT_EQ("印表機和滑鼠", GetFileContents(outputFile));
}

TEST_F(CommandLineConvertTest, ResourceOcd2ZipConvertsWithoutResourcePaths) {
  const std::string inputFile = InputFile("resource_ocd2_zip");
  const std::string outputFile = OutputFile("resource_ocd2_zip");

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open());
    ofs << "打印机和鼠标";
  }

  std::string cmd = QuotePath(OpenccCommand()) + " -i " +
                    QuotePath(inputFile) + " -o " + QuotePath(outputFile) +
                    " -c s2twp.json" +
                    " --resource-zip " + QuotePath(ResourceOcd2ZipFile());
#ifdef _WIN32
  cmd = "\"" + cmd + "\"";
#endif

  ASSERT_EQ(0, RunCommand(cmd));
  EXPECT_EQ("印表機和滑鼠", GetFileContents(outputFile));
}
#endif

TEST_F(CommandLineConvertTest, StdinPreservesLineEndingsAndUnknownCharacters) {
  const std::string config = "s2t";
  const std::string inputFile = InputFile("stdin_line_endings");
  const std::string outputFile = OutputFile("stdin_line_endings");

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open());
    ofs << "鼠标=mouse\r\n123\n未登录";
  }

  ASSERT_EQ(0, system(TestStdinCommand(config, inputFile, outputFile).c_str()));
  EXPECT_EQ("鼠標=mouse\r\n123\n未登錄", GetFileContents(outputFile));
}

#ifndef _WIN32
TEST_F(CommandLineConvertTest, WarnsWhenInputContainsVariationSelector) {
  const std::string inputFile = InputFile("ivs_warning");
  const std::string outputFile = OutputFile("ivs_warning");
  const std::string stderrFile = OutputFile("ivs_warning.stderr");
  const std::string variationSelector = "\xF3\xA0\x84\x80";

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open());
    ofs << "汉禰" << variationSelector;
  }

  const std::string command =
      TestCommand("s2t", inputFile, outputFile) + " 2> " +
      QuotePath(stderrFile);
  ASSERT_EQ(0, system(command.c_str()));
  EXPECT_EQ("漢禰" + variationSelector, GetFileContents(outputFile));
  EXPECT_NE(std::string::npos,
            GetFileContents(stderrFile).find(
                "warning: input contains Unicode variation selectors"));
}

TEST_F(CommandLineConvertTest,
       WarnsWhenSupplementaryVariationSelectorCrossesChunkBoundary) {
  const std::string inputFile = InputFile("ivs_warning_chunk_boundary");
  const std::string outputFile = OutputFile("ivs_warning_chunk_boundary");
  const std::string stderrFile =
      OutputFile("ivs_warning_chunk_boundary.stderr");

  std::string input(1048576 - 1, 'a');
  input.push_back(static_cast<char>(0xF3));
  input.push_back(static_cast<char>(0xA0));
  input.push_back(static_cast<char>(0x84));
  input.push_back(static_cast<char>(0x80));

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open());
    ofs << input;
  }

  const std::string command =
      TestCommand("s2t", inputFile, outputFile) + " 2> " +
      QuotePath(stderrFile);
  ASSERT_EQ(0, system(command.c_str()));
  EXPECT_EQ(input, GetFileContents(outputFile));
  EXPECT_NE(std::string::npos,
            GetFileContents(stderrFile).find(
                "warning: input contains Unicode variation selectors"));
}
#endif

#ifndef _WIN32
TEST_F(CommandLineConvertTest, PipeShortReadContinuesUntilEof) {
  const std::string outputFile = OutputFile("pipe_short_read");

  ASSERT_EQ(0, system(TestPipeCommand("s2t", outputFile).c_str()));

  ASSERT_EQ("後臺老闆", GetFileContents(outputFile));
}
#endif

TEST_F(CommandLineConvertTest, ConvertsFilesWithUnicodePaths) {
  const fs::path unicodeDir =
      fs::u8path(OutputDirectory()) / fs::u8path("opencc-中文路径-cli-😀-𠮷");
  fs::create_directories(unicodeDir);

  const fs::path inputFile = unicodeDir / fs::u8path("输入 文件.txt");
  const fs::path outputFile = unicodeDir / fs::u8path("輸出 文件.txt");
  const fs::path measuredResultFile =
      unicodeDir / fs::u8path("測量 結果.json");

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open()) << inputFile.u8string();
    ofs << "开放中文转换";
  }

  ASSERT_EQ(0, RunCommand(TestCommand("s2t", inputFile.u8string(),
                                      outputFile.u8string(),
                                      measuredResultFile.u8string())));
  EXPECT_EQ("開放中文轉換", GetFileContents(outputFile));

  const std::string measured = GetFileContents(measuredResultFile);
  rapidjson::Document doc;
  doc.Parse(measured.c_str());
  ASSERT_FALSE(doc.HasParseError()) << measured;
  ASSERT_TRUE(doc.IsObject());
  ASSERT_TRUE(doc.HasMember("input"));
  EXPECT_EQ(inputFile.u8string(), std::string(doc["input"].GetString()));
}

TEST_F(CommandLineConvertTest, ConvertsInPlaceWithUnicodePath) {
  const fs::path unicodeDir =
      fs::u8path(OutputDirectory()) / fs::u8path("opencc-就地转换-cli-😀");
  fs::create_directories(unicodeDir);

  const fs::path file = unicodeDir / fs::u8path("输入输出同一文件.txt");

  {
    std::ofstream ofs(file, std::ios::binary);
    ASSERT_TRUE(ofs.is_open()) << file.u8string();
    ofs << "开放中文转换";
  }

  ASSERT_EQ(0, RunCommand(TestCommand("s2t", file.u8string(), file.u8string(),
                                      "", "--in-place")));
  EXPECT_EQ("開放中文轉換", GetFileContents(file));
}

TEST_F(CommandLineConvertTest, RejectsInPlaceConversionWithoutFlag) {
  const fs::path unicodeDir =
      fs::u8path(OutputDirectory()) / fs::u8path("opencc-拒绝就地转换-cli");
  fs::create_directories(unicodeDir);

  const fs::path file = unicodeDir / fs::u8path("输入输出同一文件.txt");

  {
    std::ofstream ofs(file, std::ios::binary);
    ASSERT_TRUE(ofs.is_open()) << file.u8string();
    ofs << "开放中文转换";
  }

  EXPECT_NE(0, RunCommand(TestCommand("s2t", file.u8string(), file.u8string())));
  EXPECT_EQ("开放中文转换", GetFileContents(file));
}

TEST_F(CommandLineConvertTest, MissingInputDoesNotTruncateOutput) {
  const fs::path unicodeDir =
      fs::u8path(OutputDirectory()) / fs::u8path("opencc-缺失输入-cli");
  fs::create_directories(unicodeDir);

  const fs::path inputFile = unicodeDir / fs::u8path("不存在.txt");
  const fs::path outputFile = unicodeDir / fs::u8path("已有输出.txt");

  {
    std::ofstream ofs(outputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open()) << outputFile.u8string();
    ofs << "existing output";
  }

  EXPECT_NE(0, RunCommand(TestCommand("s2t", inputFile.u8string(),
                                      outputFile.u8string())));
  EXPECT_EQ("existing output", GetFileContents(outputFile));
}

TEST_F(CommandLineConvertTest, ConvertsInPlaceWithDifferentPathSpellings) {
  const fs::path unicodeDir =
      fs::u8path(OutputDirectory()) / fs::u8path("opencc-同文件不同路径-cli");
  fs::create_directories(unicodeDir);

  const fs::path file = unicodeDir / fs::u8path("输入输出同一文件.txt");

  {
    std::ofstream ofs(file, std::ios::binary);
    ASSERT_TRUE(ofs.is_open()) << file.u8string();
    ofs << "开放中文转换";
  }

  const fs::path spelledDifferently = unicodeDir / fs::u8path(".") /
                                      fs::u8path("输入输出同一文件.txt");
  ASSERT_EQ(0, RunCommand(TestCommand("s2t", file.u8string(),
                                      spelledDifferently.u8string(), "",
                                      "--in-place")));
  EXPECT_EQ("開放中文轉換", GetFileContents(file));
}

#ifndef _WIN32
TEST_F(CommandLineConvertTest, InPlaceConversionPreservesFileMode) {
  const fs::path unicodeDir =
      fs::u8path(OutputDirectory()) / fs::u8path("opencc-保留权限-cli");
  fs::create_directories(unicodeDir);

  const fs::path file = unicodeDir / fs::u8path("输入输出同一文件.txt");

  {
    std::ofstream ofs(file, std::ios::binary);
    ASSERT_TRUE(ofs.is_open()) << file.u8string();
    ofs << "开放中文转换";
  }
  ASSERT_EQ(0, chmod(file.c_str(), 0644));

  ASSERT_EQ(0, RunCommand(TestCommand("s2t", file.u8string(), file.u8string(),
                                      "", "--in-place")));
  EXPECT_EQ("開放中文轉換", GetFileContents(file));

  struct stat info;
  ASSERT_EQ(0, stat(file.c_str(), &info));
  EXPECT_EQ(0644, info.st_mode & 07777);
}

TEST_F(CommandLineConvertTest, InPlaceConversionRejectsHardLinks) {
  const fs::path unicodeDir =
      fs::u8path(OutputDirectory()) / fs::u8path("opencc-拒绝硬链接-cli");
  fs::create_directories(unicodeDir);

  const fs::path file = unicodeDir / fs::u8path("真实输入输出.txt");
  const fs::path hardLink = unicodeDir / fs::u8path("硬链接输出.txt");

  {
    std::ofstream ofs(file, std::ios::binary);
    ASSERT_TRUE(ofs.is_open()) << file.u8string();
    ofs << "开放中文转换";
  }

  std::error_code error;
  fs::remove(hardLink, error);
  fs::create_hard_link(file, hardLink, error);
  if (error) {
    GTEST_SKIP() << "Hard-link creation failed: " << error.message();
  }

  EXPECT_NE(0, RunCommand(TestCommand("s2t", file.u8string(),
                                      hardLink.u8string(), "",
                                      "--in-place")));
  EXPECT_EQ("开放中文转换", GetFileContents(file));
  EXPECT_EQ("开放中文转换", GetFileContents(hardLink));
}
#endif

TEST_F(CommandLineConvertTest, ConvertsInPlaceThroughSymlink) {
  const fs::path unicodeDir =
      fs::u8path(OutputDirectory()) / fs::u8path("opencc-符号链接-cli");
  fs::create_directories(unicodeDir);

  const fs::path file = unicodeDir / fs::u8path("真实输入输出.txt");
  const fs::path symlink = unicodeDir / fs::u8path("符号链接输出.txt");

  {
    std::ofstream ofs(file, std::ios::binary);
    ASSERT_TRUE(ofs.is_open()) << file.u8string();
    ofs << "开放中文转换";
  }

  std::error_code error;
  fs::remove(symlink, error);
  fs::create_symlink(file.filename(), symlink, error);
  if (error) {
    GTEST_SKIP() << "Symlink creation failed: " << error.message();
  }

  ASSERT_EQ(0, RunCommand(TestCommand("s2t", file.u8string(),
                                      symlink.u8string(), "", "--in-place")));
  EXPECT_EQ("開放中文轉換", GetFileContents(file));
}

TEST_F(CommandLineConvertTest, WritesMeasuredResultJson) {
  const std::string config = "s2t";
  const std::string inputFile = InputFile(config.c_str()) + ".measured";
  const std::string outputFile = OutputFile(config.c_str()) + ".measured";
  const std::string measuredResultFile =
      OutputDirectory() + config + ".measured_result.json";

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open()) << "Failed to open input file for writing: "
                               << inputFile;
    ofs << "开放中文转换" << std::endl;
  }

  ASSERT_EQ(0, system(TestCommand(config, inputFile, outputFile,
                                  measuredResultFile).c_str()));

  const std::string content = GetFileContents(measuredResultFile);
  rapidjson::Document doc;
  doc.Parse(content.c_str());
  ASSERT_FALSE(doc.HasParseError());
  ASSERT_TRUE(doc.IsObject());
  ASSERT_TRUE(doc.HasMember("config"));
  ASSERT_TRUE(doc["config"].IsString());
  EXPECT_EQ(ConfigurationDirectory() + config + ".json",
            std::string(doc["config"].GetString()));
  ASSERT_TRUE(doc.HasMember("mode"));
  EXPECT_STREQ("file", doc["mode"].GetString());
  ASSERT_TRUE(doc.HasMember("load_ms"));
  EXPECT_TRUE(doc["load_ms"].IsNumber());
  ASSERT_TRUE(doc.HasMember("convert_ms"));
  EXPECT_TRUE(doc["convert_ms"].IsNumber());
  ASSERT_TRUE(doc.HasMember("write_ms"));
  EXPECT_TRUE(doc["write_ms"].IsNumber());
  ASSERT_TRUE(doc.HasMember("total_ms"));
  EXPECT_TRUE(doc["total_ms"].IsNumber());
  ASSERT_TRUE(doc.HasMember("input_bytes"));
  EXPECT_TRUE(doc["input_bytes"].IsUint64());
  ASSERT_TRUE(doc.HasMember("output_bytes"));
  EXPECT_TRUE(doc["output_bytes"].IsUint64());
}

TEST_F(CommandLineConvertTest, SegmentationOutputIsJson) {
  const std::string config = "s2twp";
  const std::string inputFile = InputFile("segmentation_test");
  const std::string outputFile = OutputFile("segmentation_test");

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open());
    ofs << "\xe5\xbc\x80\xe6\x94\xbe\xe4\xb8\xad\xe6\x96\x87\xe8\xbd\xac\xe6"
           "\x8d\xa2"  // 开放中文转换
        << "\n";
  }

  ASSERT_EQ(0, system(TestCommandWithFlags(config, inputFile, outputFile,
                                           "--segmentation").c_str()));

  const std::string content = GetFileContents(outputFile);
  rapidjson::Document doc;
  doc.Parse(content.c_str());
  ASSERT_FALSE(doc.HasParseError()) << "Output is not valid JSON: " << content;
  ASSERT_TRUE(doc.IsObject());
  ASSERT_TRUE(doc.HasMember("input"));
  ASSERT_TRUE(doc["input"].IsString());
  ASSERT_TRUE(doc.HasMember("segments"));
  ASSERT_TRUE(doc["segments"].IsArray());
  EXPECT_FALSE(doc["segments"].GetArray().Empty());
  // Should NOT have 'stages' or 'output' keys
  EXPECT_FALSE(doc.HasMember("stages"));
  EXPECT_FALSE(doc.HasMember("output"));
}

TEST_F(CommandLineConvertTest, InspectOutputIsJson) {
  const std::string config = "s2t";
  const std::string inputFile = InputFile("inspect_test");
  const std::string outputFile = OutputFile("inspect_test");

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open());
    ofs << "\xe5\xbc\x80\xe6\x94\xbe\xe4\xb8\xad\xe6\x96\x87\xe8\xbd\xac\xe6"
           "\x8d\xa2"  // 开放中文转换
        << "\n";
  }

  ASSERT_EQ(0, system(TestCommandWithFlags(config, inputFile, outputFile,
                                           "--inspect").c_str()));

  const std::string content = GetFileContents(outputFile);
  rapidjson::Document doc;
  doc.Parse(content.c_str());
  ASSERT_FALSE(doc.HasParseError()) << "Output is not valid JSON: " << content;
  ASSERT_TRUE(doc.IsObject());
  ASSERT_TRUE(doc.HasMember("input"));
  ASSERT_TRUE(doc["input"].IsString());
  ASSERT_TRUE(doc.HasMember("segments"));
  ASSERT_TRUE(doc["segments"].IsArray());
  ASSERT_TRUE(doc.HasMember("stages"));
  ASSERT_TRUE(doc["stages"].IsArray());
  ASSERT_TRUE(doc.HasMember("output"));
  ASSERT_TRUE(doc["output"].IsString());
  // Validate stage schema
  for (auto& stage : doc["stages"].GetArray()) {
    ASSERT_TRUE(stage.IsObject());
    ASSERT_TRUE(stage.HasMember("index"));
    ASSERT_TRUE(stage["index"].IsUint64());
    ASSERT_TRUE(stage.HasMember("segments"));
    ASSERT_TRUE(stage["segments"].IsArray());
  }
}

TEST_F(CommandLineConvertTest, SegmentationAndInspectAreMutuallyExclusive) {
  const std::string config = "s2t";
  const std::string inputFile = InputFile("mutex_test");
  const std::string outputFile = OutputFile("mutex_test");

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open());
    ofs << "test\n";
  }

  // Should fail with non-zero exit code
  const int exitCode =
      system(TestCommandWithFlags(config, inputFile, outputFile,
                                  "--segmentation --inspect")
                 .c_str());
  EXPECT_NE(0, exitCode);
}

TEST_F(CommandLineConvertTest, SegmentationFailsWhenConfigHasNoSegmentation) {
  // s2t has no segmentation step; --segmentation should exit non-zero.
  const std::string config = "s2t";
  const std::string inputFile = InputFile("seg_no_seg_test");
  const std::string outputFile = OutputFile("seg_no_seg_test");
  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open());
    ofs << "开放中文转换\n";
  }
  EXPECT_NE(0, system(TestCommandWithFlags(config, inputFile, outputFile,
                                           "--segmentation").c_str()));
}

TEST_F(CommandLineConvertTest, MeasuredResultIncludesOutputMode) {
  const std::string config = "s2t";
  const std::string inputFile = InputFile("inspect_measured_test");
  const std::string outputFile = OutputFile("inspect_measured_test");
  const std::string measuredResultFile =
      OutputDirectory() + "inspect_measured_result.json";

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open());
    ofs << "\xe5\xbc\x80\xe6\x94\xbe\xe4\xb8\xad\xe6\x96\x87\xe8\xbd\xac\xe6"
           "\x8d\xa2"  // 开放中文转换
        << "\n";
  }

  ASSERT_EQ(0,
            system(TestCommandWithFlags(config, inputFile, outputFile,
                                        "--inspect", measuredResultFile)
                       .c_str()));

  const std::string content = GetFileContents(measuredResultFile);
  rapidjson::Document doc;
  doc.Parse(content.c_str());
  ASSERT_FALSE(doc.HasParseError());
  ASSERT_TRUE(doc.IsObject());
  ASSERT_TRUE(doc.HasMember("output_mode"));
  ASSERT_TRUE(doc["output_mode"].IsString());
  EXPECT_STREQ("inspect", doc["output_mode"].GetString());
}

// Verify --segmentation does NOT run the conversion chain: the segments in the
// output must be the raw segmenter tokens, not post-conversion text.
// With s2t.json on "开放中文转换", segmentation splits into simplified-Chinese
// tokens; none of those tokens should already be Traditional Chinese unless
// the conversion chain ran.
TEST_F(CommandLineConvertTest, SegmentationDoesNotRunConversionChain) {
  const std::string config = "s2twp";
  const std::string inputFile = InputFile("seg_no_convert_test");
  const std::string outputFile = OutputFile("seg_no_convert_test");

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open());
    // 开放中文转换 — all simplified; after s2t conversion it would become
    // 開放中文轉換.  --segmentation must return the simplified tokens.
    ofs << "\xe5\xbc\x80\xe6\x94\xbe\xe4\xb8\xad\xe6\x96\x87\xe8\xbd\xac\xe6"
           "\x8d\xa2"  // 开放中文转换
        << "\n";
  }

  ASSERT_EQ(0, system(TestCommandWithFlags(config, inputFile, outputFile,
                                           "--segmentation").c_str()));

  const std::string content = GetFileContents(outputFile);
  rapidjson::Document doc;
  doc.Parse(content.c_str());
  ASSERT_FALSE(doc.HasParseError());
  ASSERT_TRUE(doc.IsObject());
  ASSERT_TRUE(doc.HasMember("segments"));
  ASSERT_TRUE(doc["segments"].IsArray());

  // Reconstruct the concatenation of all segments.
  std::string joined;
  for (auto& seg : doc["segments"].GetArray()) {
    ASSERT_TRUE(seg.IsString());
    joined += seg.GetString();
  }
  // The joined segments must equal the *original* simplified input,
  // not the converted traditional output.
  const std::string simplified =
      "\xe5\xbc\x80\xe6\x94\xbe\xe4\xb8\xad\xe6\x96\x87\xe8\xbd\xac\xe6"
      "\x8d\xa2";  // 开放中文转换
  EXPECT_EQ(simplified, joined)
      << "Segments were converted — conversion chain must not run in "
         "--segmentation mode";
}

// Verify that a multi-line input in --segmentation mode produces exactly one
// JSON object per input line with no spurious extra record at EOF.
TEST_F(CommandLineConvertTest, SegmentationNoSpuriousEofRecord) {
  const std::string config = "s2twp";
  const std::string inputFile = InputFile("seg_eof_test");
  const std::string outputFile = OutputFile("seg_eof_test");

  {
    std::ofstream ofs(inputFile, std::ios::binary);
    ASSERT_TRUE(ofs.is_open());
    // Two lines, each terminated by \n (file ends with a newline).
    ofs << "\xe5\xbc\x80\xe6\x94\xbe\xe4\xb8\xad\xe6\x96\x87\xe8\xbd\xac\xe6"
           "\x8d\xa2\n"  // 开放中文转换
        << "\xe6\xb1\x89\xe5\xad\x97\n";  // 汉字
  }

  ASSERT_EQ(0, system(TestCommandWithFlags(config, inputFile, outputFile,
                                           "--segmentation").c_str()));

  // Output should be exactly two JSON objects separated by a newline.
  const std::string content = GetFileContents(outputFile);
  // Count newlines — the format is "obj\nobj" so there should be exactly 1
  // newline separator for 2 records.
  size_t newlines = 0;
  for (char c : content) {
    if (c == '\n') {
      ++newlines;
    }
  }
  // Allow at most one trailing newline; the important thing is that we have
  // exactly two JSON objects.
  const size_t separators = (newlines > 0 && content.back() == '\n')
                                ? newlines - 1
                                : newlines;
  EXPECT_EQ(1u, separators) << "Expected exactly 2 JSON records (1 separator),"
                                " got extra records. Content: "
                             << content;

  // Also parse both records to confirm they're valid JSON.
  // Records are separated by exactly one '\n' with no trailing newline in the
  // payload itself.
  size_t sep = content.find('\n');
  ASSERT_NE(std::string::npos, sep) << "No separator found";
  std::string first = content.substr(0, sep);
  // skip trailing newline if present
  std::string rest = content.substr(sep + 1);
  if (!rest.empty() && rest.back() == '\n') {
    rest.pop_back();
  }

  rapidjson::Document d1, d2;
  d1.Parse(first.c_str());
  EXPECT_FALSE(d1.HasParseError()) << "First record invalid JSON: " << first;
  EXPECT_TRUE(d1.IsObject());
  d2.Parse(rest.c_str());
  EXPECT_FALSE(d2.HasParseError()) << "Second record invalid JSON: " << rest;
  EXPECT_TRUE(d2.IsObject());
}
  TEST_F(CommandLineConvertTest, PreservesLineEnding_LF) {
    const std::string config = "s2t";
    const std::string inputFile = InputFile("lf_input");
    const std::string outputFile = OutputFile("lf_output");

    {
      std::ofstream ofs(inputFile, std::ios::binary);
      ASSERT_TRUE(ofs.is_open());
      ofs << "第一行\n第二行\n第三行\n";
    }

    ASSERT_EQ(0, system(TestCommand(config, inputFile, outputFile).c_str()));

    // Read output in binary mode to check line endings
    std::ifstream ifs(outputFile, std::ios::binary);
    ASSERT_TRUE(ifs.is_open());
    std::string content((std::istreambuf_iterator<char>(ifs)),
                              (std::istreambuf_iterator<char>()));
    ifs.close();

    // Should contain LF (0a) but not CR (0d)
    ASSERT_NE(std::string::npos, content.find('\n'));
    ASSERT_EQ(std::string::npos, content.find('\r'));
  }

  TEST_F(CommandLineConvertTest, PreservesLineEnding_CRLF) {
    const std::string config = "s2t";
    const std::string inputFile = InputFile("crlf_input");
    const std::string outputFile = OutputFile("crlf_output");

    {
      std::ofstream ofs(inputFile, std::ios::binary);
      ASSERT_TRUE(ofs.is_open());
      ofs << "第一行\r\n第二行\r\n第三行\r\n";
    }

    ASSERT_EQ(0, system(TestCommand(config, inputFile, outputFile).c_str()));

    // Read output in binary mode to check line endings
    std::ifstream ifs(outputFile, std::ios::binary);
    ASSERT_TRUE(ifs.is_open());
    std::string content((std::istreambuf_iterator<char>(ifs)),
                              (std::istreambuf_iterator<char>()));
    ifs.close();

    // Should contain both CR (0d) and LF (0a)
    ASSERT_NE(std::string::npos, content.find('\r'));
    ASSERT_NE(std::string::npos, content.find('\n'));
  }

  TEST_F(CommandLineConvertTest, PreservesLineEnding_CRLF_ASCII) {
    const std::string config = "s2t";
    const std::string inputFile = InputFile("crlf_ascii_input");
    const std::string outputFile = OutputFile("crlf_ascii_output");

    {
      std::ofstream ofs(inputFile, std::ios::binary);
      ASSERT_TRUE(ofs.is_open());
      ofs << "hello\r\nworld\r\n";
    }

    ASSERT_EQ(0, system(TestCommand(config, inputFile, outputFile).c_str()));

    // Read output in binary mode
    std::ifstream ifs(outputFile, std::ios::binary);
    ASSERT_TRUE(ifs.is_open());
    std::string content((std::istreambuf_iterator<char>(ifs)),
                              (std::istreambuf_iterator<char>()));
    ifs.close();

    // Should preserve CRLF
    ASSERT_NE(std::string::npos, content.find('\r'));
    ASSERT_NE(std::string::npos, content.find('\n'));
  }

  TEST_F(CommandLineConvertTest, PreservesLineEnding_NoTrailingNewline_LF) {
    const std::string config = "s2t";
    const std::string inputFile = InputFile("no_trailing_lf_input");
    const std::string outputFile = OutputFile("no_trailing_lf_output");

    {
      std::ofstream ofs(inputFile, std::ios::binary);
      ASSERT_TRUE(ofs.is_open());
      ofs << "第一行\n第二行";  // No trailing newline
    }

    ASSERT_EQ(0, system(TestCommand(config, inputFile, outputFile).c_str()));

    // Read output in binary mode
    std::ifstream ifs(outputFile, std::ios::binary);
    ASSERT_TRUE(ifs.is_open());
    std::string content((std::istreambuf_iterator<char>(ifs)),
                              (std::istreambuf_iterator<char>()));
    ifs.close();

    // Should have LF but no CR, and no trailing newline
    ASSERT_NE(std::string::npos, content.find('\n'));
    ASSERT_EQ(std::string::npos, content.find('\r'));
    ASSERT_NE('\n', content.back());
  }

  TEST_F(CommandLineConvertTest, PreservesLineEnding_NoTrailingNewline_CRLF) {
    const std::string config = "s2t";
    const std::string inputFile = InputFile("no_trailing_crlf_input");
    const std::string outputFile = OutputFile("no_trailing_crlf_output");

    {
      std::ofstream ofs(inputFile, std::ios::binary);
      ASSERT_TRUE(ofs.is_open());
      ofs << "第一行\r\n第二行";  // No trailing newline
    }

    ASSERT_EQ(0, system(TestCommand(config, inputFile, outputFile).c_str()));

    // Read output in binary mode
    std::ifstream ifs(outputFile, std::ios::binary);
    ASSERT_TRUE(ifs.is_open());
    std::string content((std::istreambuf_iterator<char>(ifs)),
                              (std::istreambuf_iterator<char>()));
    ifs.close();

    // Should preserve CRLF but no trailing newline
    ASSERT_NE(std::string::npos, content.find('\r'));
    ASSERT_NE(std::string::npos, content.find('\n'));
    ASSERT_NE('\r', content.back());
    ASSERT_NE('\n', content.back());
  }

  TEST_F(CommandLineConvertTest, ChunkingBoundaryTruncationFix) {
    const std::string config = "s2t";
    const std::string inputFile = InputFile("chunk_boundary_test");
    const std::string outputFile = OutputFile("chunk_boundary_test");

    {
      std::ofstream ofs(inputFile, std::ios::binary);
      ASSERT_TRUE(ofs.is_open());
      // Create exactly 1MB - 3 bytes of padding. 
      // This forces the "头" character (3 bytes) to end exactly at the 1MB 
      // chunk boundary, while "发尾巴" falls into the next read chunk.
      std::string padding(1048576 - 3, 'a');
      ofs << padding << "头发尾巴";
    }

    ASSERT_EQ(0, system(TestCommand(config, inputFile, outputFile).c_str()));

    // Read output in binary mode
    std::ifstream ifs(outputFile, std::ios::binary);
    ASSERT_TRUE(ifs.is_open());
    std::string content((std::istreambuf_iterator<char>(ifs)),
                        (std::istreambuf_iterator<char>()));
    ifs.close();

    // 1. Verify EOF truncation bug is fixed (the output shouldn't be truncated)
    // 2. Verify Semantic-safe chunking (the word shouldn't be split into 頭發)
    const std::string expectedEnd = "頭髮尾巴";
    ASSERT_GE(content.size(), expectedEnd.size());
    EXPECT_EQ(expectedEnd, content.substr(content.size() - expectedEnd.size()));
  }
  
  TEST_F(CommandLineConvertTest, ExactChunkSizeDoesNotTruncate) {
    const std::string config = "s2t";
    const std::string inputFile = InputFile("exact_chunk_test");
    const std::string outputFile = OutputFile("exact_chunk_test");

    {
      std::ofstream ofs(inputFile, std::ios::binary);
      ASSERT_TRUE(ofs.is_open());
      // Create EXACTLY 1MB of data (1048576 bytes).
      // This triggers the bug where fread reads exactly bufferSizeAvailable,
      // defers MAX_KEEP_CHARS to the next iteration, and then the next fread
      // returns 0. If the loop breaks immediately on fread returning 0, the
      // deferred tail is lost.
      std::string padding(1048576, 'a');
      ofs << padding;
    }

    ASSERT_EQ(0, system(TestCommand(config, inputFile, outputFile).c_str()));

    // Read output in binary mode
    std::ifstream ifs(outputFile, std::ios::binary);
    ASSERT_TRUE(ifs.is_open());
    std::string content((std::istreambuf_iterator<char>(ifs)),
                        (std::istreambuf_iterator<char>()));
    ifs.close();

    // Verify output length is exactly 1MB.
    EXPECT_EQ(1048576, content.size());
  }
} // namespace opencc