msime-engine 0.2.0

Input method engine for Chinese and other languages: quanpin, shuangpin, nine-key, wubi, zhuyin, Cantonese, Japanese, Korean and more, ranked over a word lattice with typo correction and learning from what the user picks. The engine of 水杉输入法 (Metasequoia IME).
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
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
//! Learning on commit (core-session.md §7.3-§7.5, overlays.md §3.3, §9.2, §9.4): frequency adjustment, sentence and online-row phrases, accepted typos, rejected corrections, pick pairs and personal context. Every failure becomes the matching `diagnostics` string on the key result; learning never blocks the commit.
//!
//! A `Session` always configures frequency adjustment (session.cpp:21-23 rejects invalid options instead), so the reference's unconfigured compatibility path, which bumped a picked row to its key's maximum plus one, is unreachable and not ported.

use std::{borrow::Cow, time::Duration};

use super::composition::{
    append_canonical_pinyin, fold_autocorrect_letters, normalize_canonical_pinyin_for_word,
};
use super::input::InputSession;
use crate::assets;
use crate::diagnostics;
use crate::ime::queries::counts_toward_quick_phrase_slot;
use crate::lattice::personal::PersonalTransition;
use crate::local::jianpin::jianpin_ranking_context;
use crate::pinyin::autocorrect::autocorrect_suppression_key;
use crate::pinyin::segment::split_segments;
use crate::pinyin::syllables::normalize_umlaut_aliases;
use crate::pinyin::typos::syllable_typo_kind;
use crate::shuangpin::typo_edges::{touch_correction_pairs, CorrectionPair, LearnedTypos};
use crate::text::{count_utf8_chars, is_all_han, is_han};
use crate::types::{
    CandidateSource, FrequencyAdjustmentMode, FrequencyAdjustmentOptions, LocalInputMode,
    PersonalDictionaryKind, SchemeType, ShuangpinProfileKind, WordItem,
};
use crate::user_dictionary::journal::is_user_deleted;
use crate::user_dictionary::picks::{clear_pick_transition, record_pick_transition};
use crate::user_dictionary::ranking::{
    adjust_candidate_ranking, adjust_english_candidate_ranking, learn_quick_phrase_bypass,
    learn_quick_phrase_selection, RankingRequest,
};
use crate::user_dictionary::typo_profile::{
    is_storable_autocorrect_suppression, is_storable_shuangpin_suppression, ShuangpinTypoOutcome,
};

/// Match the engine's own maximum stored phrase width. Longer generated sentences are useful for this commit only and would otherwise grow the user dictionary without bound (ISC:51-53).
pub const MAX_LEARNED_SENTENCE_SYLLABLES: usize = 7;
/// Two explicit picks from separate compositions become one word after the same pair has been seen this many times.
pub const PICK_PAIR_WORD_THRESHOLD: i64 = 3;
pub const MAX_PICK_PAIR_WORD_CHARS: usize = 4;
/// Hosts do not report everything between two compositions (their own English mode, modifier shortcuts, caret moves by mouse), so picks further apart than this are not treated as one phrase.
pub const PICK_PAIR_MAX_GAP_SECONDS: u64 = 10;

/// An explicit pick among alternatives says more than accepting a decoded sentence, so it counts twice (IS:1181).
pub(super) const DICTIONARY_PICK_TIMES: u32 = 2;

/// Split and umlaut-normalised syllables of a reading.
fn normalized_syllables(pinyin: &str) -> Vec<String> {
    let mut segments = split_segments(pinyin);
    normalize_umlaut_aliases(&mut segments);
    segments
}

/// 只需判断句子长度时统计分隔符,避免为每个音节复制字符串。
fn segment_count(pinyin: &str) -> usize {
    if pinyin.is_empty() {
        0
    } else {
        pinyin.bytes().filter(|&byte| byte == b'\'').count() + 1
    }
}

fn join_words(first: &str, second: &str) -> String {
    let mut word = String::with_capacity(first.len() + second.len());
    word.push_str(first);
    word.push_str(second);
    word
}

fn english_context_key(context: &str) -> String {
    let mut key = String::with_capacity("english:".len() + context.len());
    key.push_str("english:");
    key.extend(
        context
            .chars()
            .map(|character| character.to_ascii_lowercase()),
    );
    key
}

/// 只复制实际参与排名的行,避免先复制完整排序列表再筛选一遍。
fn clone_matching_rows(
    ordered: &[WordItem],
    mut matches: impl FnMut(&WordItem) -> bool,
) -> Vec<WordItem> {
    ordered
        .iter()
        .filter(|item| matches(item))
        .cloned()
        .collect()
}

impl InputSession {
    /// input_session.cpp:1274-1319; `index` is in ranking order.
    pub(super) fn learn_candidate(&mut self, index: usize) -> Option<String> {
        // 五笔的临时拼音反查只是查编码:选中的拼音词不调词频、不记整句,免得一次查询改了拼音的排序。
        if !self.learning_enabled || self.engine.wubi_pinyin_lookup_active() {
            return None;
        }
        let quick_phrase = self.learn_quick_phrase_order(index);
        let shuangpin_typo = self.learn_shuangpin_touch_correction(index);
        let ranked = self.learn_ranked_candidate(index);
        quick_phrase.or(ranked).or(shuangpin_typo)
    }

    /// 快捷短语参与调频(`mixed_input.quick_phrase_frequency`,Windows `utility.quick_phrase_frequency`):组在普通候选里时,选中组内短语让整组按调频规则前移,越过整组选普通候选让整组后退一格、最多退到首页末位(`learn_quick_phrase_order`,input_session.cpp:1054-1096)。
    fn learn_quick_phrase_order(&mut self, index: usize) -> Option<String> {
        if !self.quick_phrase_frequency
            || self.frequency.mode == FrequencyAdjustmentMode::Disabled
            || !self.quick_phrase_candidates
            || self.local_mode != LocalInputMode::None
            || self.dedicated_english
            || self.prefix_active
        {
            return None;
        }
        let code = self.engine.request().raw_input.clone();
        if code.is_empty() || !code.bytes().all(|byte| byte.is_ascii_lowercase()) {
            return None;
        }
        let ordered = self.ranking_list();
        let selected = ordered.get(index)?;
        let group_start = ordered
            .iter()
            .position(|item| item.source == CandidateSource::QuickPhrase)?;
        let slot = ordered[..group_start]
            .iter()
            .filter(|item| counts_toward_quick_phrase_slot(item))
            .count();
        let journal = self.journal_path().to_owned();
        if selected.source == CandidateSource::QuickPhrase {
            let first_in_group = index == group_start;
            // 组在首位、选的又是组内第一条:这就是默认上屏的那一个,没有可学的。
            if slot == 0 && first_in_group {
                return None;
            }
            let main_db = self.paths.dictionary(assets::MAIN_DICTIONARY);
            return learn_quick_phrase_selection(
                &main_db,
                &journal,
                &code,
                &selected.word,
                first_in_group,
                slot,
                &self.frequency,
            )
            .err()
            .map(|_| diagnostics::FREQUENCY_NOT_PERSISTED.to_owned());
        }
        if !counts_toward_quick_phrase_slot(selected) {
            return None;
        }
        let ordinary_rank = ordered[..index]
            .iter()
            .filter(|item| counts_toward_quick_phrase_slot(item))
            .count();
        let max_slot = self.first_page_rows.saturating_sub(1);
        learn_quick_phrase_bypass(
            &journal,
            &code,
            ordinary_rank,
            slot,
            max_slot,
            &self.frequency,
        )
        .err()
        .map(|_| diagnostics::FREQUENCY_NOT_PERSISTED.to_owned())
    }

    fn learn_ranked_candidate(&mut self, index: usize) -> Option<String> {
        let selected_source = self.ranking_list().get(index)?.source;
        let temporary_english = self.local_mode == LocalInputMode::TemporaryEnglish;
        if (self.dedicated_english || temporary_english)
            && selected_source == CandidateSource::EnglishDictionary
            && self.frequency.mode != FrequencyAdjustmentMode::Disabled
            && index != 0
        {
            return self.adjust_candidate_frequency(index, self.frequency, false);
        }
        // Generated/Fallback and injected online sentences are not dictionary rows, so frequency adjustment has nowhere to persist them. Store the selected sentence as a user phrase instead. This applies even at index zero and is independent of the frequency-adjustment mode.
        if selected_source.is_sentence_learning() {
            return self.learn_sentence_candidate(index);
        }
        // 英文、日文或连续混合行占了首位时,紧跟在这些领先行后面的那一行才是拼音给的首选:选它仍是默认的选择,不调词频,也不写日志。
        let leading = self
            .ranking_list()
            .iter()
            .take_while(|item| {
                matches!(
                    item.source,
                    CandidateSource::EnglishDictionary
                        | CandidateSource::MixedJapanese
                        | CandidateSource::Multilingual
                )
            })
            .count();
        let default_behind_leaders = leading > 0 && index == leading;
        if self.frequency.mode == FrequencyAdjustmentMode::Disabled
            || index == 0
            || default_behind_leaders
        {
            return None;
        }
        if !selected_source.is_dictionary()
            || !self
                .engine
                .current_scheme_type()
                .learns_into_main_dictionary()
        {
            return None;
        }
        self.adjust_candidate_frequency(index, self.frequency, false)
    }

    /// input_session.cpp:1352-1412, with the ranking set filtered to the selected row's producer in mixed wubi.
    pub(super) fn adjust_candidate_frequency(
        &mut self,
        index: usize,
        options: FrequencyAdjustmentOptions,
        force_top: bool,
    ) -> Option<String> {
        // Ranks are read from the order before personal context reordering.
        let ordered = self.ranking_list();
        let selected = ordered.get(index)?;
        let user_db = self.journal_path();
        if selected.source == CandidateSource::EnglishDictionary {
            let context = if self.dedicated_english {
                self.dedicated_english_preedit.clone()
            } else if self.local_mode == LocalInputMode::TemporaryEnglish {
                self.local_preedit.get(1..).unwrap_or_default().to_owned()
            } else {
                self.engine.request().raw_input.clone()
            };
            let context_key = english_context_key(&context);
            let english_db = self.paths.dictionary(assets::ENGLISH_DICTIONARY);
            let adjusted = adjust_english_candidate_ranking(&RankingRequest {
                main_db: &english_db,
                user_db,
                context_key: &context_key,
                ordered,
                entry_key: &selected.pinyin,
                value: &selected.word,
                mode: options.mode,
                linear_step: options.linear_step,
                trigger_count: options.trigger_count,
                force_top,
                kind: PersonalDictionaryKind::English,
            });
            return adjusted
                .err()
                .map(|_| diagnostics::ENGLISH_FREQUENCY_NOT_PERSISTED.to_owned());
        }

        let super_jianpin = self.local_mode == LocalInputMode::SuperJianpin;
        // In a mixed wubi list the selected row's producer decides: a quanpin row is ranked, keyed and stored as pinyin, and only a row the wubi table answered is ranked under the code itself (overlays.md §3.3). The pinyin fallback reuses the context the fixed positions are written under, otherwise a pinned candidate would not be recognised here.
        let wubi = Self::is_wubi_native_candidate(selected);
        let pinyin_fallback = self.is_wubi() && !wubi;
        let request = self.engine.request();
        let mut context_key = if super_jianpin {
            jianpin_ranking_context(
                self.local_preedit.get(1..).unwrap_or_default(),
                self.scheme(),
                self.shuangpin_profile(),
            )
        } else if wubi {
            request.raw_input.clone()
        } else if pinyin_fallback {
            self.position_context(false, false).into_owned()
        } else {
            request.normalized_segmentation.clone()
        };
        if !super_jianpin && context_key.is_empty() {
            context_key = self.engine.request().segmentation.clone();
        }
        let wubi_row = wubi && !super_jianpin;
        let entry_key = if wubi_row {
            selected.pinyin.as_str()
        } else if selected.canonical_pinyin.is_empty() {
            context_key.as_str()
        } else {
            selected.canonical_pinyin.as_str()
        };
        let mixed_wubi = self.is_wubi();
        let ranked_storage;
        let ranked = if mixed_wubi {
            ranked_storage = clone_matching_rows(ordered, |item| item.scheme == selected.scheme);
            ranked_storage.as_slice()
        } else {
            ordered
        };
        let main_db = self.paths.dictionary(assets::MAIN_DICTIONARY);
        let adjusted = adjust_candidate_ranking(&RankingRequest {
            main_db: &main_db,
            user_db,
            context_key: &context_key,
            ordered: ranked,
            entry_key,
            value: &selected.word,
            mode: options.mode,
            linear_step: options.linear_step,
            trigger_count: options.trigger_count,
            force_top,
            kind: if wubi_row {
                self.engine.wubi_input_options().profile.dictionary_kind()
            } else {
                PersonalDictionaryKind::Pinyin
            },
        });
        match adjusted {
            Ok(changed) => {
                if changed {
                    self.engine.reset_cache();
                }
                None
            }
            Err(_) => Some(diagnostics::FREQUENCY_NOT_PERSISTED.to_owned()),
        }
    }

    /// input_session_composition.cpp:562-600.
    pub(super) fn learn_sentence_candidate(&mut self, index: usize) -> Option<String> {
        // Local shortcuts and English/Japanese modes also use Generated candidates, but they are not pinyin sentences and must never enter the pinyin user dictionary. A native wubi row is not one either, while a pinyin row beside it in a mixed list is (overlays.md §3.3).
        let (typo_diagnostic, online, canonical, word) = {
            let selected = self.ranking_list().get(index)?;
            if !self.can_learn_sentence_candidate(selected) {
                return None;
            }
            let typo_diagnostic = self.learn_accepted_typos(selected);
            // Both quanpin and shuangpin sentences carry canonical quanpin; require a complete reading with one syllable per Han character before creating the row. Online cloud/AI rows are injected after the local query and carry no canonical reading: for a complete full-pinyin query the session's explicit segmentation is the canonical key, because the committed letters without apostrophes would let correction re-segment a reading such as qi'e'huan before it is stored.
            let online = selected.source.is_online();
            let selected_canonical: Cow<'_, str> = if selected.canonical_pinyin.is_empty()
                && online
                && self.is_all_complete_pure_pinyin()
            {
                self.pinyin_segmentation()
            } else {
                Cow::Borrowed(&selected.canonical_pinyin)
            };
            let canonical =
                normalize_canonical_pinyin_for_word(&selected_canonical, &selected.word);
            let word = selected.word.clone();
            (typo_diagnostic, online, canonical, word)
        };
        if canonical.is_empty() || segment_count(&canonical) > MAX_LEARNED_SENTENCE_SYLLABLES {
            return typo_diagnostic;
        }
        if online && !self.online_word_matches_reading(&canonical, &word) {
            return typo_diagnostic;
        }
        if self
            .store_user_phrase_from_canonical_pinyin(&canonical, &word)
            .is_err()
        {
            return Some(diagnostics::SENTENCE_NOT_PERSISTED.to_owned());
        }
        typo_diagnostic
    }

    pub(super) fn can_learn_sentence_candidate(&self, selected: &WordItem) -> bool {
        self.local_mode == LocalInputMode::None
            && !self.dedicated_english
            && self
                .engine
                .current_scheme_type()
                .learns_into_main_dictionary()
            && !Self::is_wubi_native_candidate(selected)
    }

    /// Providers return bare words, so the only reading to check an online row against is the typed one. A character absent from every single-character table cannot be checked and is accepted so rare words stay learnable (input_session_composition.cpp:538-560).
    fn online_word_matches_reading(&mut self, canonical: &str, word: &str) -> bool {
        let syllables = normalized_syllables(canonical);
        let mut characters = word.chars();
        let engine = self.canonical_phrase_engine();
        for syllable in &syllables {
            let Some(character) = characters.next() else {
                return false;
            };
            if !is_han(character) {
                return false;
            }
            let han = character.to_string();
            if engine.find_candidate(syllable, &han).is_none() && engine.knows_han_char(&han) {
                return false;
            }
        }
        characters.next().is_none()
    }

    /// input_session_composition.cpp:602-627.
    pub(super) fn learn_accepted_typos(&self, selected: &WordItem) -> Option<String> {
        if selected.source != CandidateSource::Generated
            || !selected.sentence_association
            || selected.corrected_from.is_empty()
            || !self.scheme().supports_autocorrect()
        {
            return None;
        }
        let typed = normalized_syllables(&selected.pinyin.to_ascii_lowercase());
        let intended = normalized_syllables(&selected.canonical_pinyin);
        if typed.is_empty() || typed.len() != intended.len() {
            return None;
        }
        let pairs: Vec<(String, String)> = typed
            .into_iter()
            .zip(intended)
            .filter(|(typed, intended)| {
                typed != intended && syllable_typo_kind(typed, intended).is_some()
            })
            .collect();
        if pairs.is_empty() {
            return None;
        }
        self.typo_profile
            .record_accepted(&pairs)
            .err()
            .map(|_| diagnostics::TYPO_NOT_PERSISTED.to_owned())
    }

    /// 双拼的触点纠错能学的时候(当前请求带触点、开着纠错、内置方案、普通组字),返回方案。桌面和实体键盘的请求从不带触点,所以双拼学习只发生在触屏上。
    fn shuangpin_touch_learning(&self) -> Option<&'static crate::shuangpin::ShuangpinProfile> {
        let profile = self.shuangpin_profile();
        let request = self.engine.request();
        (self.scheme() == SchemeType::Shuangpin
            && self.local_mode == LocalInputMode::None
            && !self.dedicated_english
            && self.autocorrect_types != 0
            && profile.kind != ShuangpinProfileKind::Custom
            && !request.key_touches.is_empty()
            && request.key_geometry.is_some())
        .then_some(profile)
    }

    /// 双拼的纠错行:带 `corrected_from` 的整句纠错行(Generated,`sentence_association`),或带 `corrected_from` 的词库行(1–2 个音节的短输入纠错)。
    fn is_shuangpin_correction_row(row: &WordItem) -> bool {
        !row.corrected_from.is_empty()
            && ((row.source == CandidateSource::Generated && row.sentence_association)
                || row.source.is_dictionary())
    }

    /// 纠错行改动过的每个音节在本次请求的触点下是哪一对两键码(`touch_correction_pairs`)。行回答的按键要是带触点那串输入的前缀。
    fn shuangpin_correction_pairs(
        &self,
        profile: &'static crate::shuangpin::ShuangpinProfile,
        row: &WordItem,
    ) -> Option<Vec<CorrectionPair>> {
        let request = self.engine.request();
        let geometry = request.key_geometry.as_deref()?;
        let typed = row.pinyin.as_str();
        if !request.raw_input.starts_with(typed) {
            return None;
        }
        touch_correction_pairs(
            profile,
            typed,
            &request.key_touches[..typed.len()],
            geometry,
            &request.touch_tuning,
            LearnedTypos::new(&self.typo_profile, profile.kind),
            &row.canonical_pinyin,
            self.autocorrect_types,
        )
    }

    /// 双拼按两键码学触点纠错(#10018)。选中纠错行是接受:每个改动过的音节记一次 (按到的码 → 本次变体集里解到纠正后音节的码)。首选是纠错行、用户却选了别的行是拒绝:选中的行覆盖到的、读音不是纠正后那个音节的位置各记一次拒绝;没覆盖到的位置用户没表态,不记。`index` 是学习顺序里的位置,首选看的是显示出来的第一行。
    fn learn_shuangpin_touch_correction(&self, index: usize) -> Option<String> {
        let profile = self.shuangpin_touch_learning()?;
        let selected = self.ranking_list().get(index)?;
        let (pairs, outcome) = if Self::is_shuangpin_correction_row(selected) {
            let pairs = self.shuangpin_correction_pairs(profile, selected)?;
            (pairs, ShuangpinTypoOutcome::Accepted)
        } else {
            let first = self.candidates().first()?;
            if !Self::is_shuangpin_correction_row(first)
                || (first.word == selected.word
                    && first.canonical_pinyin == selected.canonical_pinyin)
            {
                return None;
            }
            let chosen = normalized_syllables(&selected.canonical_pinyin);
            let mut pairs = self.shuangpin_correction_pairs(profile, first)?;
            pairs.retain(|pair| {
                chosen
                    .get(pair.position)
                    .is_some_and(|syllable| syllable != pair.syllable)
            });
            (pairs, ShuangpinTypoOutcome::Rejected)
        };
        if pairs.is_empty() {
            return None;
        }
        let codes: Vec<([u8; 2], [u8; 2])> = pairs
            .iter()
            .map(|pair| (pair.typed, pair.intended))
            .collect();
        self.typo_profile
            .record_shuangpin(profile.kind, &codes, outcome)
            .err()
            .map(|_| diagnostics::TYPO_NOT_PERSISTED.to_owned())
    }

    /// 双拼:带触点的输入有落点支持的纠错可选(`touch_supported` 的整句纠错行,或带 `corrected_from` 的词库行),用户却按原样上屏,记一次抑制,之后同一串输入不再纠(`apply_autocorrect_suppression`)。沉到表尾、落点不支持的纠错行用户多半没看到,不算。
    fn learn_rejected_shuangpin_correction(&self) -> Option<String> {
        if !self.learning_enabled || !self.has_composition() {
            return None;
        }
        let profile = self.shuangpin_touch_learning()?;
        let input = self.engine.request().raw_input.as_str();
        if !is_storable_shuangpin_suppression(input) {
            return None;
        }
        let offered = self.candidates().iter().any(|item| {
            Self::is_shuangpin_correction_row(item)
                && (item.touch_supported || item.source.is_dictionary())
        });
        if !offered {
            return None;
        }
        self.typo_profile
            .record_shuangpin_suppression(profile.kind, input)
            .err()
            .map(|_| diagnostics::AUTOCORRECT_PREFERENCE_NOT_PERSISTED.to_owned())
    }

    /// On CommitRaw while a correction is offered (input_session_composition.cpp:629-656).
    pub(super) fn learn_rejected_correction(&mut self) -> Option<String> {
        if self.scheme() == SchemeType::Shuangpin {
            return self.learn_rejected_shuangpin_correction();
        }
        if !self.learning_enabled
            || !self.scheme().supports_autocorrect()
            || self.local_mode != LocalInputMode::None
            || self.dedicated_english
            || self.autocorrect_types == 0
            || !self.has_composition()
        {
            return None;
        }
        let request = self.engine.request();
        let cases = if request.enable_quanpin_helpcode {
            &request.raw_input_with_cases
        } else {
            &request.raw_input
        };
        let key = autocorrect_suppression_key(&request.raw_input, cases);
        // An input the store cannot hold (longer than its limit) is not a write failure to report on every Enter.
        if !is_storable_autocorrect_suppression(&key) {
            return None;
        }
        // A row labelled as corrected whose reading is the typed letters after all (a v/u spelling) offered nothing to reject.
        let typed_letters = fold_autocorrect_letters(&key);
        let correction_offered = self.candidates().iter().any(|item| {
            if item.corrected_from.is_empty() {
                return false;
            }
            let reading = if item.canonical_pinyin.is_empty() {
                &item.pinyin
            } else {
                &item.canonical_pinyin
            };
            fold_autocorrect_letters(reading) != typed_letters
        });
        if !correction_offered {
            return None;
        }
        self.typo_profile
            .record_suppression(&key)
            .err()
            .map(|_| diagnostics::AUTOCORRECT_PREFERENCE_NOT_PERSISTED.to_owned())
    }

    /// input_session_composition.cpp:658-691.
    pub(super) fn learn_pick_pair(
        &mut self,
        previous: &super::chain::Pick,
        current: &super::chain::Pick,
    ) -> Option<String> {
        if current
            .committed_at
            .saturating_duration_since(previous.committed_at)
            > Duration::from_secs(PICK_PAIR_MAX_GAP_SECONDS)
        {
            return None;
        }
        let word = join_words(&previous.word, &current.word);
        if count_utf8_chars(&word) > MAX_PICK_PAIR_WORD_CHARS || !is_all_han(&word) {
            return None;
        }
        let previous_key =
            normalize_canonical_pinyin_for_word(&previous.canonical_pinyin, &previous.word);
        let current_key =
            normalize_canonical_pinyin_for_word(&current.canonical_pinyin, &current.word);
        if previous_key.is_empty() || current_key.is_empty() {
            return None;
        }
        let key = append_canonical_pinyin(&previous_key, &current_key);
        let Ok(count) = record_pick_transition(
            self.journal_path(),
            &previous_key,
            &previous.word,
            &current_key,
            &current.word,
        ) else {
            return Some(diagnostics::PICK_TRANSITION_NOT_PERSISTED.to_owned());
        };
        if count < PICK_PAIR_WORD_THRESHOLD {
            return None;
        }
        // A word the user deleted must not come back from the same habit that created it.
        if !is_user_deleted(
            self.journal_path(),
            PersonalDictionaryKind::Pinyin,
            &key,
            &word,
        ) {
            // The counter stays so that the next occurrence retries the insert.
            if self
                .store_user_phrase_from_canonical_pinyin(&key, &word)
                .is_err()
            {
                return Some(diagnostics::PHRASE_NOT_PERSISTED.to_owned());
            }
            // The phrase engine only resets its own cache; the typing engine must see the new word on its next query.
            self.engine.reset_cache();
        }
        // The reference discards this result (ISC:686): a counter left behind only makes the next occurrence of the pair try the insert again, which finds the word and changes nothing.
        let _ = clear_pick_transition(
            self.journal_path(),
            &previous_key,
            &previous.word,
            &current_key,
            &current.word,
        );
        None
    }

    /// input_session.cpp:1158-1200.
    pub(super) fn record_personal_context(
        &mut self,
        selected: Option<&WordItem>,
        learned_sentence_word: bool,
        composition_left: bool,
    ) -> Option<String> {
        // Wubi never reaches here with a context: personal_context_applies() admits only quanpin and shuangpin.
        let Some(selected) = selected else {
            self.chain.reset();
            return None;
        };
        if !self.personal_context_applies() || learned_sentence_word {
            self.chain.reset();
            return None;
        }
        let dictionary = selected.source.is_dictionary();
        // 借用现有词文本:词库行直接借用词面,整句只收集各段的借用。
        let single = [selected.word.as_str()];
        let sentence: Vec<&str>;
        let words: &[&str] = if dictionary {
            &single
        } else if selected.source.is_generated_or_fallback() && !selected.sentence_words.is_empty()
        {
            sentence = selected.sentence_words.iter().map(String::as_str).collect();
            &sentence
        } else {
            self.chain.reset();
            return None;
        };
        let now = self.steady_now();
        let continues = self.chain.same_composition && self.chain.previous.is_some();
        let mut transitions = Vec::with_capacity(words.len());
        for (index, word) in words.iter().enumerate() {
            let times = if dictionary {
                DICTIONARY_PICK_TIMES
            } else if index == 0 && continues {
                2
            } else {
                1
            };
            transitions.push(PersonalTransition {
                earlier: self.chain.earlier.clone().unwrap_or_default(),
                previous: self.chain.previous.clone().unwrap_or_default(),
                word: (*word).to_owned(),
                times,
            });
            self.chain.advance(word, composition_left, now);
        }
        self.personal_context
            .record(&transitions)
            .err()
            .map(|_| diagnostics::PERSONAL_CONTEXT_NOT_PERSISTED.to_owned())
    }

    /// learning, personal context, engine view, quanpin or shuangpin.
    pub(super) fn personal_context_applies(&self) -> bool {
        self.learning_enabled
            && self.personal_context_enabled
            && self.local_mode == LocalInputMode::None
            && !self.dedicated_english
            && self.scheme().reranks_with_sentence_model()
    }
}

#[cfg(test)]
mod tests {
    use std::path::{Path, PathBuf};
    use std::sync::{Arc, Mutex};
    use std::time::Instant;

    use rusqlite::Connection;

    use super::*;
    use crate::paths::RuntimePaths;
    use crate::session::{Clock, Session, SessionOptions};
    use crate::types::{autocorrect_type, SchemeType, ShuangpinProfileKind};
    use crate::user_dictionary::ngram_store::flush_all;

    #[test]
    fn sentence_segment_count_avoids_temporary_strings() {
        let (count, allocations) = crate::ime::personal_rerank::allocations::count(|| {
            super::segment_count("yi'er'san'si")
        });

        assert_eq!(count, 4);
        assert_eq!(allocations, 0);
        assert_eq!(super::segment_count(""), 0);
        assert_eq!(super::segment_count("yi"), 1);
        for input in ["'", "yi''er'", "'yi"] {
            assert_eq!(
                super::segment_count(input),
                crate::pinyin::segment::split_segments(input).len()
            );
        }
    }

    struct Fixture {
        _root: tempfile::TempDir,
        paths: RuntimePaths,
    }

    impl Fixture {
        /// Pinyin rows `(key, word, weight)` and wubi rows `(code, word, weight)` in the shipped schemas, plus the lantian helpcode table the default options load.
        fn new(pinyin: &[(&str, &str, i64)], wubi: &[(&str, &str, i64)]) -> Self {
            let root = tempfile::tempdir().unwrap();
            let paths = RuntimePaths {
                resources: root.path().join("resources"),
                user_data: root.path().join("user"),
                cache: root.path().join("cache"),
                dictionaries: root.path().join("dictionaries"),
            };
            for directory in [
                &paths.resources,
                &paths.user_data,
                &paths.cache,
                &paths.dictionaries,
            ] {
                std::fs::create_dir_all(directory).unwrap();
            }
            std::fs::create_dir_all(paths.resources.join("helpcodes")).unwrap();
            std::fs::write(paths.resources.join("helpcodes/helpcode.txt"), "你=ab\n").unwrap();
            let main = Connection::open(paths.dictionary(assets::MAIN_DICTIONARY)).unwrap();
            for (key, word, weight) in pinyin {
                let table = crate::user_dictionary::journal::pinyin_table(key).unwrap();
                main.execute_batch(&format!(
                    "CREATE TABLE IF NOT EXISTS \"{table}\"(key TEXT, jp TEXT, value TEXT, weight INTEGER);"
                ))
                .unwrap();
                let jp: String = key
                    .split('\'')
                    .filter_map(|syllable| syllable.chars().next())
                    .collect();
                main.execute(
                    &format!("INSERT INTO \"{table}\" VALUES(?1,?2,?3,?4)"),
                    (key, jp, word, weight),
                )
                .unwrap();
            }
            main.execute_batch(
                "CREATE TABLE IF NOT EXISTS wubi86(key TEXT, value TEXT, weight INTEGER);",
            )
            .unwrap();
            for row in wubi {
                main.execute("INSERT INTO wubi86 VALUES(?1,?2,?3)", *row)
                    .unwrap();
            }
            crate::dictionary::english::ensure_english_schema(
                &paths.dictionary(assets::ENGLISH_DICTIONARY),
            )
            .unwrap();
            Self { _root: root, paths }
        }

        fn journal(&self) -> PathBuf {
            self.paths.user(assets::USER_JOURNAL)
        }

        fn session(&self, adjust: impl FnOnce(&mut SessionOptions)) -> Session {
            let mut options = SessionOptions::new(self.paths.clone());
            // 按参照实现的默认值打开辅助码(引擎库默认关,见 `SessionOptions::new`)。
            options.helpcode = true;
            options.helpcode_schema = "lantian".to_owned();
            adjust(&mut options);
            Session::new(options).unwrap()
        }

        fn query(&self, path: &Path, sql: &str) -> i64 {
            flush_all();
            Connection::open(path)
                .unwrap()
                .query_row(sql, [], |row| row.get(0))
                .unwrap_or(0)
        }

        fn journal_count(&self, sql: &str) -> i64 {
            self.query(&self.journal(), sql)
        }

        fn main_count(&self, sql: &str) -> i64 {
            self.query(&self.paths.dictionary(assets::MAIN_DICTIONARY), sql)
        }
    }

    fn type_text(session: &mut Session, text: &str) {
        for byte in text.bytes() {
            session.character(byte, false);
        }
    }

    fn words(session: &Session) -> Vec<String> {
        session
            .snapshot()
            .candidates
            .into_iter()
            .map(|item| item.word.into_string())
            .collect()
    }

    fn select_word(session: &mut Session, word: &str) -> crate::types::KeyResult {
        let index = words(session)
            .iter()
            .position(|candidate| candidate == word)
            .unwrap_or_else(|| panic!("{word} is not offered: {:?}", words(session)));
        session.select(index)
    }

    fn cancel(session: &mut Session) {
        session.command(crate::types::Command::Cancel);
    }

    const FQ: [(&str, &str, i64); 6] = [
        ("ni", "甲", 100),
        ("ni", "乙", 90),
        ("ni", "丙", 80),
        ("ni", "丁", 70),
        ("ni", "戊", 60),
        ("ni", "己", 50),
    ];

    fn frequency(
        mode: FrequencyAdjustmentMode,
        trigger_count: i32,
        linear_step: i32,
    ) -> FrequencyAdjustmentOptions {
        FrequencyAdjustmentOptions {
            mode,
            trigger_count,
            linear_step,
        }
    }

    #[test]
    fn joined_words_allocates_only_result_bytes() {
        let word = join_words("你好", "世界");
        assert_eq!(word, "你好世界");
        assert_eq!(word.capacity(), word.len());
    }

    #[test]
    fn english_context_key_allocates_only_result_bytes() {
        let key = english_context_key("HeLLo");
        assert_eq!(key, "english:hello");
        assert_eq!(key.capacity(), key.len());
    }

    #[test]
    fn copying_rows_for_ranking_clones_only_matching_rows() {
        let rows = [
            WordItem::new("ni", "甲", 3, CandidateSource::Database, "ni"),
            WordItem::new("ni", "乙", 2, CandidateSource::EnglishDictionary, "ni"),
            WordItem::new("ni", "丙", 1, CandidateSource::Database, "ni"),
        ];

        let copied = clone_matching_rows(&rows, |item| item.source == CandidateSource::Database);

        assert_eq!(
            copied
                .iter()
                .map(|item| item.word.as_str())
                .collect::<Vec<_>>(),
            ["甲", "丙"]
        );
    }

    /// F1 (test_input_session.cpp:1091-1119): the pick's place in a new session, per mode.
    #[test]
    fn frequency_modes_reorder_the_next_session() {
        for (mode, step, expected) in [
            (FrequencyAdjustmentMode::Disabled, 1, 5),
            (FrequencyAdjustmentMode::Pin, 1, 0),
            (FrequencyAdjustmentMode::Halve, 1, 2),
            (FrequencyAdjustmentMode::Linear, 2, 3),
            (FrequencyAdjustmentMode::Promote, 1, 4),
        ] {
            let fixture = Fixture::new(&FQ, &[]);
            let options = frequency(mode, 1, step);
            {
                let mut session = fixture.session(|o| o.frequency = options);
                type_text(&mut session, "ni");
                let result = select_word(&mut session, "己");
                assert_eq!(result.commit.as_deref(), Some("己"));
                assert_eq!(result.diagnostic, None, "{mode:?}");
            }
            let mut session = fixture.session(|o| o.frequency = options);
            type_text(&mut session, "ni");
            assert_eq!(
                words(&session).iter().position(|word| word == "己"),
                Some(expected),
                "{mode:?}"
            );
            let state = fixture.journal_count("SELECT count(*) FROM user_dictionary_operations")
                + fixture.journal_count("SELECT count(*) FROM candidate_selection_state");
            assert_eq!(
                state > 0,
                mode != FrequencyAdjustmentMode::Disabled,
                "{mode:?}"
            );
        }
    }

    /// 不重建会话换调频方式:关着时选「己」不留记录,换成固定首位后再选一次就排到首位;越界的次数被拒绝,原来的方式不变。
    #[test]
    fn the_frequency_mode_changes_without_a_new_session() {
        let fixture = Fixture::new(&FQ, &[]);
        let mut session = fixture.session(|_| {});
        assert_eq!(
            session.frequency_adjustment().mode,
            FrequencyAdjustmentMode::Disabled
        );
        type_text(&mut session, "ni");
        select_word(&mut session, "己");
        type_text(&mut session, "ni");
        assert_eq!(words(&session)[5], "己");
        cancel(&mut session);

        let pin = frequency(FrequencyAdjustmentMode::Pin, 1, 1);
        let revision = session.candidate_revision();
        session.set_frequency_adjustment(pin).unwrap();
        assert_ne!(session.candidate_revision(), revision);
        assert_eq!(session.frequency_adjustment(), pin);
        type_text(&mut session, "ni");
        select_word(&mut session, "己");
        type_text(&mut session, "ni");
        assert_eq!(words(&session)[0], "己");
        cancel(&mut session);

        let error = session
            .set_frequency_adjustment(frequency(FrequencyAdjustmentMode::Halve, 0, 1))
            .unwrap_err();
        assert_eq!(error.code(), crate::ErrorCode::InvalidArgument);
        assert_eq!(session.frequency_adjustment(), pin);
    }

    /// F2 and F3 (test_input_session.cpp:1122-1151).
    #[test]
    fn the_trigger_count_and_the_leader() {
        let fixture = Fixture::new(&FQ, &[]);
        let options = frequency(FrequencyAdjustmentMode::Pin, 2, 1);
        let mut session = fixture.session(|o| o.frequency = options);
        type_text(&mut session, "ni");
        select_word(&mut session, "己");
        type_text(&mut session, "ni");
        assert_eq!(words(&session)[5], "己");
        select_word(&mut session, "己");
        type_text(&mut session, "ni");
        assert_eq!(words(&session)[0], "己");
        cancel(&mut session);

        let fixture = Fixture::new(&FQ, &[]);
        let mut session =
            fixture.session(|o| o.frequency = frequency(FrequencyAdjustmentMode::Promote, 1, 1));
        type_text(&mut session, "ni");
        session.select(0);
        assert_eq!(
            fixture.journal_count("SELECT count(*) FROM user_dictionary_operations"),
            0
        );
        assert_eq!(
            fixture.journal_count("SELECT count(*) FROM candidate_selection_state"),
            0
        );
    }

    /// F5 and F6 (test_input_session.cpp:1174-1202): shuangpin learns under the canonical quanpin key, wubi under its code.
    #[test]
    fn shuangpin_and_wubi_persist_their_picks() {
        let fixture = Fixture::new(
            &[("ni'hao", "你好", 200), ("ni'hao", "拟好", 100)],
            &[("aaaa", "工", 200), ("aaaa", "或", 100)],
        );
        let options = frequency(FrequencyAdjustmentMode::Pin, 1, 1);
        {
            let mut session = fixture.session(|o| {
                o.frequency = options;
                o.scheme = SchemeType::Shuangpin;
                o.shuangpin_profile = ShuangpinProfileKind::Xiaohe;
            });
            type_text(&mut session, "nihc");
            select_word(&mut session, "拟好");
            session.switch_scheme(SchemeType::Wubi).unwrap();
            type_text(&mut session, "aaaa");
            select_word(&mut session, "或");
        }
        assert_eq!(
            fixture.journal_count("SELECT count(*) FROM user_dictionary_operations WHERE dictionary='pinyin' AND key='ni''hao' AND value='拟好'"),
            1
        );
        assert_eq!(
            fixture.journal_count("SELECT count(*) FROM user_dictionary_operations WHERE dictionary='wubi' AND key='aaaa' AND value='或'"),
            1
        );
        let mut session = fixture.session(|o| {
            o.frequency = options;
            o.scheme = SchemeType::Shuangpin;
        });
        type_text(&mut session, "nihc");
        assert_eq!(words(&session)[0], "拟好");
        session.switch_scheme(SchemeType::Wubi).unwrap();
        type_text(&mut session, "aaaa");
        assert_eq!(words(&session)[0], "或");
    }

    #[test]
    fn shuangpin_fuzzy_selection_survives_a_new_session() {
        for input in ["nihc", "lihc"] {
            let fixture = Fixture::new(
                &[
                    ("ni'hao", "你好", 100),
                    ("ni'hao", "拟好", 90),
                    ("li'hao", "利好", 200),
                ],
                &[],
            );
            let create = || {
                fixture.session(|options| {
                    options.scheme = SchemeType::Shuangpin;
                    options.helpcode = false;
                    options.fuzzy_pinyin.rules = crate::types::fuzzy_rule::N_L;
                    options.frequency = frequency(FrequencyAdjustmentMode::Promote, 1, 1);
                })
            };
            {
                let mut session = create();
                for round in 0..3 {
                    type_text(&mut session, input);
                    if round == 0 {
                        assert_ne!(words(&session)[0], "拟好", "夹具必须从非首选学习");
                    }
                    let result = select_word(&mut session, "拟好");
                    assert_eq!(result.commit.as_deref(), Some("拟好"));
                    assert_eq!(result.diagnostic, None);
                }
            }
            assert!(fixture.journal_count("SELECT count(*) FROM user_dictionary_operations WHERE key='ni''hao' AND value='拟好'") > 0);
            for spelling in [input.to_owned(), format!("{}'{}", &input[..2], &input[2..])] {
                let mut session = create();
                type_text(&mut session, &spelling);
                assert_eq!(
                    words(&session)[0],
                    "拟好",
                    "重建会话后 {spelling} 的选择退回"
                );
            }
        }
    }

    /// 发布布局:`msime-pinyin.db` 没有五笔表,码表单独在只读的 `msime-wubi.db` 里。准备出的代次把码表并回工作主词库,五笔选词学到的权重写得进去、下一次会话读得到,换一个代次(升级)时日志里的五笔行也能回放。
    #[test]
    fn wubi_learning_survives_the_split_dictionary_layout() {
        let root = tempfile::tempdir().unwrap();
        let resources = root.path().join("resources");
        std::fs::create_dir_all(resources.join("helpcodes")).unwrap();
        std::fs::write(resources.join("helpcodes/helpcode.txt"), "你=ab\n").unwrap();
        Connection::open(resources.join(assets::MAIN_DICTIONARY))
            .unwrap()
            .execute_batch(
                "CREATE TABLE tbl_1_n(key TEXT, jp TEXT, value TEXT, weight INTEGER);
                 INSERT INTO tbl_1_n VALUES('ni','n','你',100);",
            )
            .unwrap();
        Connection::open(resources.join(assets::WUBI_DICTIONARY))
            .unwrap()
            .execute_batch(
                "CREATE TABLE wubi86(key TEXT NOT NULL, value TEXT NOT NULL, weight INTEGER NOT NULL DEFAULT 0, UNIQUE(key, value));
                 CREATE TABLE wubi98(key TEXT NOT NULL, value TEXT NOT NULL, weight INTEGER NOT NULL DEFAULT 0, UNIQUE(key, value));
                 INSERT INTO wubi86 VALUES('aaaa','工',200),('aaaa','或',100);",
            )
            .unwrap();
        crate::dictionary::english::ensure_english_schema(
            &resources.join(assets::ENGLISH_DICTIONARY),
        )
        .unwrap();
        let user = root.path().join("user");
        let cache = root.path().join("cache");
        let open = |content_id: &str| {
            let paths = crate::user_dictionary::generation::prepare_runtime_paths(
                &resources, &user, &cache, content_id,
            )
            .unwrap();
            let mut options = SessionOptions::new(paths);
            options.helpcode = true;
            options.helpcode_schema = "lantian".to_owned();
            options.frequency = frequency(FrequencyAdjustmentMode::Pin, 1, 1);
            options.scheme = SchemeType::Wubi;
            Session::new(options).unwrap()
        };
        {
            let mut session = open("v1");
            type_text(&mut session, "aaaa");
            assert_eq!(words(&session)[..2], ["工", "或"]);
            assert_eq!(select_word(&mut session, "或").diagnostic, None);
        }
        flush_all();
        for content_id in ["v1", "v2"] {
            let mut session = open(content_id);
            type_text(&mut session, "aaaa");
            assert_eq!(words(&session)[0], "或", "{content_id}");
        }
    }

    /// F7 (test_input_session.cpp:1204-1217): the commit survives, and the diagnostic carries no input text.
    #[test]
    fn a_failed_write_keeps_the_commit_with_a_diagnostic() {
        let fixture = Fixture::new(&FQ, &[]);
        std::fs::create_dir(fixture.journal()).unwrap();
        let mut session =
            fixture.session(|o| o.frequency = frequency(FrequencyAdjustmentMode::Promote, 1, 1));
        type_text(&mut session, "ni");
        let result = select_word(&mut session, "己");
        assert_eq!(result.commit.as_deref(), Some("己"));
        assert_eq!(
            result.diagnostic.as_deref(),
            Some(diagnostics::FREQUENCY_NOT_PERSISTED)
        );
        assert_eq!(
            fixture.main_count("SELECT weight FROM tbl_1_n WHERE value='己'"),
            50
        );
    }

    /// test_input_session.cpp:790-803.
    #[test]
    fn learning_off_writes_nothing() {
        let fixture = Fixture::new(&[("bu'hao", "不好", 200), ("bu'hao", "补好", 100)], &[]);
        let mut session = fixture.session(|o| {
            o.learning = false;
            o.frequency = frequency(FrequencyAdjustmentMode::Pin, 1, 1);
        });
        type_text(&mut session, "buhao");
        select_word(&mut session, "补好");
        assert_eq!(
            fixture.journal_count("SELECT count(*) FROM user_dictionary_operations"),
            0
        );
        assert_eq!(
            fixture.journal_count("SELECT count(*) FROM personal_bigram"),
            0
        );
        let mut session = fixture.session(|_| {});
        type_text(&mut session, "buhao");
        assert_eq!(words(&session)[0], "不好");
    }

    const PC: [(&str, &str, i64); 6] = [
        ("ni", "甲", 300),
        ("ni", "乙", 200),
        ("ni", "丙", 100),
        ("hao", "子", 300),
        ("hao", "丑", 200),
        ("hao", "寅", 100),
    ];

    /// test_personal_context_input_session.cpp:545-589, 681-762: each pick is counted twice after the word before it, a pause or a break starts a new chain, and the switch or learning off records nothing.
    #[test]
    fn picks_record_their_context_and_breaks_reset_it() {
        let fixture = Fixture::new(&PC, &[]);
        let now = Arc::new(Mutex::new(Instant::now()));
        let clock_now = Arc::clone(&now);
        let mut session = fixture.session(|_| {});
        session.set_clock(Clock {
            steady: Box::new(move || *clock_now.lock().unwrap()),
            ..Clock::default()
        });
        type_text(&mut session, "ni");
        select_word(&mut session, "丙");
        type_text(&mut session, "hao");
        select_word(&mut session, "寅");
        assert_eq!(
            fixture.journal_count(
                "SELECT count FROM personal_bigram WHERE previous=char(1) AND word='丙'"
            ),
            2
        );
        assert_eq!(
            fixture.journal_count(
                "SELECT count FROM personal_bigram WHERE previous='丙' AND word='寅'"
            ),
            2
        );
        assert_eq!(fixture.journal_count("SELECT count FROM personal_trigram WHERE earlier=char(1) AND previous='丙' AND word='寅'"), 2);

        // Punctuation ends the run: the next pick starts a chain.
        session.punctuation(b',');
        type_text(&mut session, "hao");
        select_word(&mut session, "丑");
        assert_eq!(
            fixture.journal_count(
                "SELECT count FROM personal_bigram WHERE previous=char(1) AND word='丑'"
            ),
            2
        );

        // A pause longer than the chain window starts one too.
        *now.lock().unwrap() += Duration::from_secs(30);
        type_text(&mut session, "ni");
        select_word(&mut session, "乙");
        assert_eq!(
            fixture.journal_count("SELECT count(*) FROM personal_bigram WHERE previous='丑'"),
            0
        );

        // Personal context off: nothing further is recorded.
        let before = fixture.journal_count("SELECT sum(count) FROM personal_bigram");
        session.set_personal_context_enabled(false);
        type_text(&mut session, "ni");
        select_word(&mut session, "甲");
        assert_eq!(
            fixture.journal_count("SELECT sum(count) FROM personal_bigram"),
            before
        );
    }

    const PP: [(&str, &str, i64); 5] = [
        ("shan", "闪", 200),
        ("shan", "山", 100),
        ("shui", "睡", 200),
        ("shui", "水", 100),
        // Gives the two-syllable table the learned word goes into.
        ("shan'shan", "闪闪", 10),
    ];

    /// test_pick_pair_input_session.cpp:168-197, 300-330: a pair picked three times becomes a word; a removed word does not come back; picks too far apart do not count.
    #[test]
    fn a_pair_picked_three_times_becomes_a_word() {
        let fixture = Fixture::new(&PP, &[]);
        let mut session = fixture.session(|_| {});
        for round in 1..=3 {
            type_text(&mut session, "shan");
            assert_eq!(select_word(&mut session, "山").diagnostic, None);
            type_text(&mut session, "shui");
            assert_eq!(select_word(&mut session, "水").diagnostic, None);
            // A space the host inserts breaks the run, so every round starts afresh.
            session.character(b' ', false);
            let stored = fixture
                .main_count("SELECT count(*) FROM tbl_2_s WHERE key='shan''shui' AND value='山水'");
            assert_eq!(stored, i64::from(round == 3), "round {round}");
        }
        assert_eq!(
            fixture.journal_count(
                "SELECT count(*) FROM pick_transitions WHERE previous_value='山' AND value='水'"
            ),
            0,
            "the counter outlived the word it created"
        );
        assert_eq!(
            fixture.journal_count("SELECT user_inserted FROM user_dictionary_operations WHERE key='shan''shui' AND value='山水'"),
            1
        );

        // Deleted, the habit does not bring it back.
        type_text(&mut session, "shanshui");
        let index = words(&session)
            .iter()
            .position(|word| word == "山水")
            .unwrap();
        assert_eq!(session.remove(index).diagnostic, None);
        cancel(&mut session);
        for _ in 0..3 {
            type_text(&mut session, "shan");
            select_word(&mut session, "山");
            type_text(&mut session, "shui");
            select_word(&mut session, "水");
            session.character(b' ', false);
        }
        assert_eq!(
            fixture.main_count("SELECT count(*) FROM tbl_2_s WHERE value='山水'"),
            0
        );
    }

    /// test_pick_pair_input_session.cpp:236-250: picks with a long pause between them are not one phrase.
    #[test]
    fn picks_after_a_long_pause_are_not_a_pair() {
        let fixture = Fixture::new(&PP, &[]);
        let now = Arc::new(Mutex::new(Instant::now()));
        let clock_now = Arc::clone(&now);
        let mut session = fixture.session(|_| {});
        session.set_clock(Clock {
            steady: Box::new(move || *clock_now.lock().unwrap()),
            ..Clock::default()
        });
        type_text(&mut session, "shan");
        select_word(&mut session, "山");
        *now.lock().unwrap() += Duration::from_secs(PICK_PAIR_MAX_GAP_SECONDS + 1);
        type_text(&mut session, "shui");
        select_word(&mut session, "水");
        assert_eq!(
            fixture.journal_count("SELECT count(*) FROM pick_transitions"),
            0
        );
    }

    /// test_typo_correction_input_session.cpp:400-440: committing the raw letters while a correction is offered records the input once, and then that input is answered as typed.
    #[test]
    fn committing_raw_letters_suppresses_the_correction() {
        let fixture = Fixture::new(&[("shang", "上", 100), ("sha", "沙", 50)], &[]);
        let types = autocorrect_type::TRANSPOSITION | autocorrect_type::NEIGHBOR;
        let mut session = fixture.session(|o| o.autocorrect_types = types);
        type_text(&mut session, "shabg");
        let corrected = session
            .snapshot()
            .candidates
            .iter()
            .any(|item| !item.corrected_from.is_empty());
        assert!(
            corrected,
            "the neighbor correction was not offered: {:?}",
            words(&session)
        );
        let raw = session.command(crate::types::Command::CommitRaw);
        assert_eq!(raw.commit.as_deref(), Some("shabg"));
        assert_eq!(raw.diagnostic, None);
        assert_eq!(
            fixture.journal_count(
                "SELECT commits FROM pinyin_autocorrect_suppressions WHERE input='shabg'"
            ),
            1
        );
        type_text(&mut session, "shabg");
        assert!(session
            .snapshot()
            .candidates
            .iter()
            .all(|item| item.corrected_from.is_empty()));
        cancel(&mut session);

        // With learning off nothing is recorded.
        let quiet = Fixture::new(&[("shang", "上", 100)], &[]);
        let mut session = quiet.session(|o| {
            o.autocorrect_types = types;
            o.learning = false;
        });
        type_text(&mut session, "shabg");
        session.command(crate::types::Command::CommitRaw);
        assert_eq!(
            quiet.journal_count("SELECT count(*) FROM pinyin_autocorrect_suppressions"),
            0
        );
    }

    /// 904bd0976 / 8d046e269: mixed Wubi asks quanpin for the same letters, and that request honours the refusal too, so a correction the user turned down by committing raw does not come back through the Wubi pinyin fallback.
    #[test]
    fn a_refused_correction_stays_refused_in_mixed_wubi() {
        let fixture = Fixture::new(&[("shang", "上", 100), ("sha", "沙", 50)], &[]);
        let types = autocorrect_type::TRANSPOSITION | autocorrect_type::NEIGHBOR;
        let mixed_wubi = |o: &mut SessionOptions| {
            o.scheme = SchemeType::Wubi;
            o.wubi.mixed_pinyin = true;
            o.autocorrect_types = types;
        };
        let offers_correction = |session: &Session| {
            session
                .snapshot()
                .candidates
                .iter()
                .any(|item| !item.corrected_from.is_empty())
        };

        // Without a refusal the fallback offers the neighbor correction, otherwise the check below proves nothing.
        let mut wubi = fixture.session(mixed_wubi);
        type_text(&mut wubi, "shabg");
        assert_eq!(wubi.snapshot().editing_text, "shabg");
        assert!(
            offers_correction(&wubi),
            "mixed wubi did not offer the correction: {:?}",
            words(&wubi)
        );
        cancel(&mut wubi);
        drop(wubi);

        // The refusal is learned where it can be: in quanpin, by committing the raw letters.
        let mut quanpin = fixture.session(|o| o.autocorrect_types = types);
        type_text(&mut quanpin, "shabg");
        let raw = quanpin.command(crate::types::Command::CommitRaw);
        assert_eq!(raw.commit.as_deref(), Some("shabg"));
        assert_eq!(
            fixture.journal_count(
                "SELECT commits FROM pinyin_autocorrect_suppressions WHERE input='shabg'"
            ),
            1
        );
        drop(quanpin);

        let mut wubi = fixture.session(mixed_wubi);
        type_text(&mut wubi, "shabg");
        assert_eq!(wubi.snapshot().editing_text, "shabg");
        assert!(
            !offers_correction(&wubi),
            "the refused correction came back through mixed wubi: {:?}",
            words(&wubi)
        );
    }

    /// 904bd0976: a sentence a neural model picked has no dictionary row to re-rank, so selecting it stores the sentence as a user phrase like a lattice sentence. Needs the keyboard model from `MSIME_EVAL_RESOURCES`.
    #[test]
    fn a_neural_sentence_is_learned_as_a_sentence() {
        let model = match crate::lattice::neural::test_model_path(assets::NEURAL_MODEL_KEYBOARD) {
            Ok(model) => model,
            Err(reason) => {
                eprintln!("skipping a_neural_sentence_is_learned_as_a_sentence: {reason}");
                return;
            }
        };
        let fixture = Fixture::new(
            &[
                ("shu'ru", "输入", 20000),
                ("fa", "法", 800000),
                ("fa", "发", 900000),
                ("fa", "罚", 100000),
            ],
            &[],
        );
        std::fs::copy(
            &model,
            fixture.paths.resource(assets::NEURAL_MODEL_KEYBOARD),
        )
        .unwrap();
        Connection::open(fixture.paths.dictionary(assets::MAIN_DICTIONARY))
            .unwrap()
            .execute_batch("CREATE TABLE IF NOT EXISTS tbl_3_s(key TEXT, jp TEXT, value TEXT, weight INTEGER);")
            .unwrap();
        let mut session = fixture.session(|o| {
            o.sentence_association.neural_keyboard = true;
            o.sentence_association.show_next_on_duplicate = true;
        });
        type_text(&mut session, "shurufa");
        let neural = session
            .snapshot()
            .candidates
            .into_iter()
            .find(|item| item.source == CandidateSource::NeuralKeyboard)
            .unwrap_or_else(|| panic!("no neural row: {:?}", words(&session)));
        let result = select_word(&mut session, &neural.word);
        assert_eq!(result.commit.as_deref(), Some(neural.word.as_str()));
        assert_eq!(result.diagnostic, None);
        assert_eq!(
            fixture.main_count(&format!(
                "SELECT count(*) FROM tbl_3_s WHERE key='shu''ru''fa' AND value='{}'",
                neural.word
            )),
            1
        );
        assert_eq!(
            fixture.journal_count(&format!(
                "SELECT user_inserted FROM user_dictionary_operations WHERE key='shu''ru''fa' AND value='{}'",
                neural.word
            )),
            1
        );
    }

    /// test_online_input_session.cpp (personal-learning overlay): a selected cloud word is stored under the typed reading when every character can have it; a character known under other readings only is refused, one no table knows is accepted.
    #[test]
    fn online_words_are_learned_only_under_a_reading_they_can_have() {
        let fixture = Fixture::new(
            &[
                ("ni", "你", 100),
                ("hao", "好", 100),
                ("ma", "马", 100),
                // Gives the two-syllable table the learned word goes into.
                ("ni'ni", "妮妮", 1),
            ],
            &[],
        );
        let mut session = fixture.session(|_| {});
        for (word, stored) in [("拟好", true), ("你马", false)] {
            type_text(&mut session, "nihao");
            let query = session
                .online_query()
                .expect("a complete pinyin input asks online");
            assert!(session.apply_online_candidates(
                &query,
                &[word.to_owned()],
                CandidateSource::CloudSuggestion
            ));
            let result = select_word(&mut session, word);
            assert_eq!(result.commit.as_deref(), Some(word));
            assert_eq!(result.diagnostic, None);
            let rows = fixture.main_count(&format!(
                "SELECT count(*) FROM tbl_2_n WHERE key='ni''hao' AND value='{word}'"
            ));
            assert_eq!(rows, i64::from(stored), "{word}");
        }
        assert_eq!(
            fixture.journal_count("SELECT user_inserted FROM user_dictionary_operations WHERE key='ni''hao' AND value='拟好'"),
            1
        );
    }

    /// Fixture F_TC (test_typo_correction_input_session.cpp:120-142).
    fn typo_fixture() -> Fixture {
        let fixture = Fixture::new(
            &[
                ("mei", "没", 1000),
                ("mei", "美", 900),
                ("gan", "干", 1000),
                ("guan", "关", 500),
                ("xi", "系", 1000),
                ("guan'xi", "关系", 100_000),
            ],
            &[],
        );
        Connection::open(fixture.paths.dictionary(assets::MAIN_DICTIONARY))
            .unwrap()
            .execute_batch("CREATE TABLE IF NOT EXISTS tbl_3_m(key TEXT, jp TEXT, value TEXT, weight INTEGER);")
            .unwrap();
        fixture
    }

    /// test_typo_correction_input_session.cpp:314-335: selecting the typo sentence counts the accepted syllable typo and stores the sentence under its corrected reading; with learning off nothing is counted.
    #[test]
    fn an_accepted_typo_sentence_is_counted_and_stored() {
        let all_types = autocorrect_type::TRANSPOSITION
            | autocorrect_type::NEIGHBOR
            | autocorrect_type::MISSING_OR_EXTRA;
        for learning in [true, false] {
            let fixture = typo_fixture();
            let mut session = fixture.session(|o| {
                o.autocorrect_types = all_types;
                o.learning = learning;
            });
            type_text(&mut session, "meiganxi");
            let result = select_word(&mut session, "没关系");
            assert_eq!(result.commit.as_deref(), Some("没关系"));
            assert_eq!(result.diagnostic, None);
            let typos = fixture.journal_count(
                "SELECT accepted FROM pinyin_typo_counts WHERE typed='gan' AND intended='guan'",
            );
            assert_eq!(typos, i64::from(learning));
            let stored = fixture.main_count(
                "SELECT count(*) FROM tbl_3_m WHERE key='mei''guan''xi' AND value='没关系'",
            );
            assert_eq!(stored, i64::from(learning));
            if learning {
                assert_eq!(
                    crate::user_dictionary::typo_profile::PersonalTypoProfile::shared(
                        &fixture.journal()
                    )
                    .accepted("gan", "guan"),
                    1
                );
            }
        }
    }

    /// The first row the session would store as a sentence, if any.
    fn sentence_row(session: &Session) -> Option<(usize, String)> {
        session
            .snapshot()
            .candidates
            .iter()
            .position(|item| item.source.is_sentence_learning())
            .map(|index| (index, session.snapshot().candidates[index].word.to_string()))
    }

    /// ISC:51-53: a generated sentence longer than `MAX_LEARNED_SENTENCE_SYLLABLES` is committed but never stored; one at the cap is.
    #[test]
    fn a_sentence_longer_than_the_cap_is_committed_but_not_stored() {
        let fixture = Fixture::new(&[("yi", "一", 100), ("er", "二", 100)], &[]);
        // Both tables exist, so only the cap can keep the longer sentence out.
        Connection::open(fixture.paths.dictionary(assets::MAIN_DICTIONARY))
            .unwrap()
            .execute_batch(
                "CREATE TABLE tbl_7_y(key TEXT, jp TEXT, value TEXT, weight INTEGER);\
                 CREATE TABLE tbl_others_y(key TEXT, jp TEXT, value TEXT, weight INTEGER);",
            )
            .unwrap();
        for syllables in [
            MAX_LEARNED_SENTENCE_SYLLABLES + 1,
            MAX_LEARNED_SENTENCE_SYLLABLES,
        ] {
            let mut session = fixture.session(|_| {});
            type_text(&mut session, &"yi".repeat(syllables));
            let (index, word) = sentence_row(&session).unwrap_or_else(|| {
                panic!(
                    "no generated sentence for {syllables} syllables: {:?}",
                    words(&session)
                )
            });
            assert_eq!(word.chars().count(), syllables);
            let result = session.select(index);
            assert_eq!(result.commit.as_deref(), Some(word.as_str()));
            assert_eq!(result.diagnostic, None);
            // The SQL literal of yi'yi'...: each apostrophe doubled.
            let key = vec!["yi"; syllables].join("''");
            let stored = fixture.journal_count(&format!(
                "SELECT count(*) FROM user_dictionary_operations WHERE key='{key}' AND value='{word}' AND user_inserted=1"
            ));
            let table = crate::format::quanpin_table(syllables, b'y').unwrap();
            let in_main = fixture.main_count(&format!(
                "SELECT count(*) FROM {table} WHERE value='{word}'"
            ));
            if syllables > MAX_LEARNED_SENTENCE_SYLLABLES {
                assert_eq!(
                    fixture.journal_count("SELECT count(*) FROM user_dictionary_operations"),
                    0
                );
                assert_eq!(in_main, 0);
            } else {
                assert_eq!(stored, 1);
                assert_eq!(in_main, 1);
            }
        }
    }

    /// input_session_composition.cpp:562-600: the English and Japanese modes offer Generated and online rows too, but none of them is a pinyin sentence, so selecting one stores nothing. Today these rows also carry no canonical pinyin, so the empty-reading check rejects them as well; the test pins the outcome, whichever check makes it.
    #[test]
    fn generated_rows_outside_pinyin_composition_are_not_stored() {
        let fixture = Fixture::new(&[("ni", "你", 100), ("hao", "好", 100)], &[]);
        for mode in [
            "temporary English",
            "dedicated English",
            "temporary Japanese",
        ] {
            let mut session = fixture.session(|_| {});
            match mode {
                "temporary English" => {
                    assert!(session.character(b'Y', true).handled);
                    type_text(&mut session, "zzq");
                }
                "dedicated English" => {
                    session.set_dedicated_english(true);
                    type_text(&mut session, "zzq");
                }
                _ => {
                    assert!(session.character(b'R', true).handled);
                    type_text(&mut session, "ka");
                }
            }
            let (index, word) = sentence_row(&session)
                .unwrap_or_else(|| panic!("{mode} offers no generated row: {:?}", words(&session)));
            let result = session.select(index);
            assert_eq!(result.commit.as_deref(), Some(word.as_str()), "{mode}");
            assert_eq!(
                fixture.journal_count("SELECT count(*) FROM user_dictionary_operations"),
                0,
                "{mode} stored {word}"
            );
        }
        assert_eq!(fixture.main_count("SELECT count(*) FROM tbl_1_n"), 1);

        // A cloud answer to Japanese `ka` is an online row, which the session otherwise stores under the typed reading.
        let mut session = fixture.session(|o| o.scheme = SchemeType::JapaneseRomaji);
        type_text(&mut session, "ka");
        let query = session.online_query().expect("a Japanese cloud query");
        assert!(session.apply_online_candidates(
            &query,
            &["蚊".to_owned()],
            CandidateSource::CloudSuggestion
        ));
        let result = select_word(&mut session, "蚊");
        assert_eq!(result.commit.as_deref(), Some("蚊"));
        assert_eq!(
            fixture.journal_count("SELECT count(*) FROM user_dictionary_operations"),
            0
        );
    }

    /// test_runtime_isolation.cpp:618-642: a pin the journal refuses is reported, and the list keeps its order.
    #[test]
    fn a_failed_pin_reports_and_keeps_the_order() {
        let fixture = Fixture::new(&FQ, &[]);
        std::fs::create_dir(fixture.journal()).unwrap();
        let mut session = fixture.session(|o| o.learning = false);
        type_text(&mut session, "ni");
        let before = session.snapshot();
        let index = words(&session)
            .iter()
            .position(|word| word == "己")
            .unwrap();
        let result = session.pin(index);
        assert!(result.handled);
        assert_eq!(result.commit, None);
        assert_eq!(
            result.diagnostic.as_deref(),
            Some(diagnostics::FREQUENCY_NOT_PERSISTED)
        );
        let after = session.snapshot();
        assert_eq!(after.preedit, before.preedit);
        assert_eq!(after.candidates[0].word, before.candidates[0].word);
        assert_eq!(
            fixture.main_count("SELECT weight FROM tbl_1_n WHERE value='己'"),
            50
        );
    }
}