oxcache 0.5.0-rc.7

A production-grade multi-level cache library for Rust: L1 memory (Moka/DashMap) + L2 distributed (Redis/Valkey/Dragonfly/Aerospike) + optional L3 disk (redb).
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
#![allow(clippy::module_inception)]
#[allow(unused_imports)]
pub use super::*;

#[cfg(test)]
mod tests {
    // locale 探测测试操作进程级 env(LANG/LC_ALL/LC_MESSAGES),
    // 并行执行互相污染 → 以下 5 个测试共用互斥锁。
    // 修改进程级默认 locale(DEFAULT_LOCALE 全局单例)的测试也必须持锁:
    // 并行测试互相覆盖会让 Display 语言随机(经典 flaky)。
    static LOCALE_ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

    use super::*;
    use std::cmp::Ordering;

    #[test]
    fn test_locale_parsing_en() {
        let fmt = CacheI18nFormatter::new("en-US");
        assert!(fmt.is_ok(), "en-US should parse successfully");
    }

    #[test]
    fn test_locale_parsing_zh() {
        let fmt = CacheI18nFormatter::new("zh-CN");
        assert!(fmt.is_ok(), "zh-CN should parse successfully");
    }

    #[test]
    fn test_invalid_locale() {
        let result = CacheI18nFormatter::new("not-a-valid-locale!!!");
        assert!(result.is_err(), "invalid locale should return error");
        match result.err().unwrap() {
            I18nError::InvalidLocale { input, .. } => assert_eq!(input, "not-a-valid-locale!!!"),
            other => panic!("expected InvalidLocale, got {other:?}"),
        }
    }

    #[test]
    fn test_format_count() {
        let fmt = CacheI18nFormatter::new("en").expect("en locale");
        assert_eq!(
            fmt.format_count(1).expect("plural 1"),
            "One",
            "en: count=1 should be One"
        );
        assert_eq!(
            fmt.format_count(2).expect("plural 2"),
            "Other",
            "en: count=2 should be Other"
        );
    }

    #[test]
    fn test_format_number_en() {
        let fmt = CacheI18nFormatter::new("en-US").expect("en-US locale");
        let result = fmt.format_number(1_234_567.89_f64).expect("format number");
        // en-US: thousands separator is comma, decimal separator is period
        assert!(
            result.contains(','),
            "en-US number should contain thousands separator: got '{result}'"
        );
        assert!(
            result.contains('.'),
            "en-US number should contain decimal point: got '{result}'"
        );
    }

    #[test]
    fn test_format_number_not_finite() {
        let fmt = CacheI18nFormatter::new("en-US").expect("en-US locale");
        assert!(fmt.format_number(f64::NAN).is_err());
        assert!(fmt.format_number(f64::INFINITY).is_err());
    }

    #[test]
    fn test_format_cache_key() {
        let fmt = CacheI18nFormatter::new("en-US").expect("en-US locale");
        let key = fmt.format_cache_key("user", 1234).expect("cache key");
        assert!(
            key.starts_with("user:"),
            "cache key should start with namespace: got '{key}'"
        );
        assert!(
            key.contains('1'),
            "cache key should contain the count: got '{key}'"
        );
    }

    #[test]
    fn test_compare_keys() {
        let fmt = CacheI18nFormatter::new("en").expect("en locale");
        assert_eq!(
            fmt.compare_keys("apple", "banana").expect("compare"),
            Ordering::Less,
            "apple < banana"
        );
        assert_eq!(
            fmt.compare_keys("banana", "apple").expect("compare"),
            Ordering::Greater,
            "banana > apple"
        );
        assert_eq!(
            fmt.compare_keys("apple", "apple").expect("compare"),
            Ordering::Equal,
            "apple == apple"
        );
    }

    #[test]
    fn test_format_expiry() {
        let fmt = CacheI18nFormatter::new("en-US").expect("en-US locale");
        let result = fmt.format_expiry(2026, 7, 11).expect("format expiry");
        assert!(
            result.contains("2026"),
            "expiry should contain year: got '{result}'"
        );
        assert!(
            !result.is_empty(),
            "expiry should be non-empty: got '{result}'"
        );
    }

    // ========================================================================
    // Message catalog tests
    // ========================================================================

    #[test]
    fn test_format_message_en_not_found() {
        let fmt = CacheI18nFormatter::new("en").expect("en locale");
        let msg = fmt
            .format_message(messages::MSG_ERR_NOT_FOUND, &[("detail", "user:42")])
            .expect("format message");
        assert!(
            msg.contains("Key not found: user:42"),
            "en message should contain 'Key not found: user:42': got '{msg}'"
        );
    }

    #[test]
    fn test_format_message_zh_not_found() {
        let fmt = CacheI18nFormatter::new("zh-CN").expect("zh-CN locale");
        let msg = fmt
            .format_message(messages::MSG_ERR_NOT_FOUND, &[("detail", "user:42")])
            .expect("format message");
        assert!(
            msg.contains("键未找到:user:42"),
            "zh message should contain '键未找到:user:42': got '{msg}'"
        );
    }

    #[test]
    fn test_format_message_en_key_too_long() {
        let fmt = CacheI18nFormatter::new("en").expect("en locale");
        let msg = fmt
            .format_message(
                messages::MSG_ERR_KEY_TOO_LONG,
                &[("actual", "600"), ("max", "512")],
            )
            .expect("format message");
        assert!(
            msg.contains("600") && msg.contains("512"),
            "en message should contain actual and max: got '{msg}'"
        );
    }

    #[test]
    fn test_format_message_zh_key_too_long() {
        let fmt = CacheI18nFormatter::new("zh-CN").expect("zh-CN locale");
        let msg = fmt
            .format_message(
                messages::MSG_ERR_KEY_TOO_LONG,
                &[("actual", "600"), ("max", "512")],
            )
            .expect("format message");
        assert!(
            msg.contains("键过长") && msg.contains("600") && msg.contains("512"),
            "zh message should contain '键过长' with values: got '{msg}'"
        );
    }

    #[test]
    fn test_format_message_unknown_id_returns_id() {
        let fmt = CacheI18nFormatter::new("en").expect("en locale");
        let msg = fmt
            .format_message("unknown.message.id", &[])
            .expect("format message");
        assert_eq!(msg, "unknown.message.id", "unknown ID should return raw ID");
    }

    #[test]
    fn test_format_message_unsupported_locale_falls_back_to_en() {
        let fmt = CacheI18nFormatter::new("en-US").expect("en-US locale");
        let msg = fmt
            .format_message(messages::MSG_ERR_CONNECTION, &[("detail", "timeout")])
            .expect("format message");
        assert!(
            msg.contains("Connection error: timeout"),
            "unsupported locale should fall back to English: got '{msg}'"
        );
    }

    #[test]
    fn test_locale_getter() {
        let fmt = CacheI18nFormatter::new("zh-CN").expect("zh-CN locale");
        assert!(
            fmt.locale_tag().starts_with("zh"),
            "locale_tag should start with 'zh': got '{}'",
            fmt.locale_tag()
        );
        let expected: icu::locale::Locale = "zh-CN".parse().expect("valid tag");
        assert_eq!(fmt.locale(), &expected, "locale() returns the parsed tag");
    }

    /// 阿拉伯语复数规则覆盖 Zero/One/Two/Few/Many/Other 六类
    /// (en 只有 One/Other,覆盖不到 plural_category_name 的全部分支)
    #[test]
    fn test_format_count_arabic_plural_categories() {
        let fmt = CacheI18nFormatter::new("ar").expect("ar locale");
        let cases = [
            (0, "Zero"),
            (1, "One"),
            (2, "Two"),
            (3, "Few"),
            (11, "Many"),
            (100, "Other"),
        ];
        for (count, expected) in cases {
            assert_eq!(
                fmt.format_count(count).expect("plural category"),
                expected,
                "ar: count={count} should be {expected}"
            );
        }
    }

    /// 有限但超出 FixedDecimal 容量的数值(f64::MAX 约 309 位十进制)
    /// 必须返回 InvalidNumber 而非 panic
    #[test]
    fn test_format_number_finite_value_beyond_decimal_capacity() {
        let fmt = CacheI18nFormatter::new("en").expect("en locale");
        match fmt.format_number(f64::MAX) {
            Err(I18nError::InvalidNumber { input, .. }) => {
                assert!(!input.is_empty(), "input should echo the raw value");
            }
            Ok(formatted) => {
                // 实现侧 FixedDecimal 若能承载该量级,则格式化必须无失真告警路径
                assert!(
                    formatted.len() >= 300,
                    "unexpectedly short formatting of f64::MAX: '{formatted}'"
                );
            }
            other => panic!("unexpected result for f64::MAX: {other:?}"),
        }
    }

    #[test]
    fn test_i18n_error_message_id() {
        let err = I18nError::InvalidLocale {
            input: "bad".to_string(),
            reason: "parse failed".to_string(),
        };
        assert_eq!(err.message_id(), messages::MSG_I18N_INVALID_LOCALE);
    }

    #[test]
    fn test_i18n_error_message_id_number_and_format_variants() {
        let err = I18nError::InvalidNumber {
            input: "NaN".to_string(),
            reason: "not finite".to_string(),
        };
        assert_eq!(err.message_id(), messages::MSG_I18N_INVALID_NUMBER);
        let err = I18nError::FormatError("compiled data missing".to_string());
        assert_eq!(err.message_id(), messages::MSG_I18N_FORMAT_ERROR);
    }

    #[test]
    fn test_i18n_error_localized_message_locale_and_number_variants() {
        let err = I18nError::InvalidLocale {
            input: "not-a-locale!".to_string(),
            reason: "syntax error".to_string(),
        };
        let msg = err.localized_message("en");
        assert!(
            msg.contains("not-a-locale!") && msg.contains("syntax error"),
            "en InvalidLocale message should carry input and reason: got '{msg}'"
        );
        let err = I18nError::InvalidNumber {
            input: "Infinity".to_string(),
            reason: "value is not finite".to_string(),
        };
        let msg = err.localized_message("en");
        assert!(
            msg.contains("Infinity"),
            "en InvalidNumber message should carry input: got '{msg}'"
        );
    }

    #[test]
    fn test_i18n_error_localized_message_format_variant() {
        let err = I18nError::FormatError("decimal formatter unavailable".to_string());
        let msg = err.localized_message("en");
        assert!(
            msg.contains("decimal formatter unavailable"),
            "en FormatError message should carry detail: got '{msg}'"
        );
    }

    #[test]
    fn test_i18n_error_localized_message_en() {
        let err = I18nError::DateError("month out of range".to_string());
        let msg = err.localized_message("en");
        assert!(
            msg.contains("date error: month out of range"),
            "en I18nError message: got '{msg}'"
        );
    }

    #[test]
    fn test_i18n_error_localized_message_zh() {
        let err = I18nError::DateError("月份超出范围".to_string());
        let msg = err.localized_message("zh-CN");
        assert!(
            msg.contains("日期错误:月份超出范围"),
            "zh I18nError message: got '{msg}'"
        );
    }

    // ========================================================================
    // Global default locale tests
    // ========================================================================

    #[test]
    fn test_i18n_error_display_en() {
        let _guard = LOCALE_ENV_LOCK.lock();
        set_default_locale("en");
        let err = I18nError::DateError("month out of range".to_string());
        let s = err.to_string();
        assert!(
            s.contains("date error: month out of range"),
            "en I18nError Display: got '{s}'"
        );
    }

    #[test]
    fn test_i18n_error_display_zh() {
        let _guard = LOCALE_ENV_LOCK.lock();
        set_default_locale("zh-CN");
        let err = I18nError::DateError("月份超出范围".to_string());
        let s = err.to_string();
        assert!(
            s.contains("日期错误:月份超出范围"),
            "zh I18nError Display: got '{s}'"
        );
        set_default_locale("en");
    }

    #[test]
    fn test_set_get_default_locale() {
        let _guard = LOCALE_ENV_LOCK.lock();
        set_default_locale("en");
        assert_eq!(get_default_locale(), "en");
        set_default_locale("zh-CN");
        assert_eq!(get_default_locale(), "zh-CN");
        set_default_locale("en");
    }

    // ========================================================================
    // System locale detection tests
    // ========================================================================

    #[test]
    fn test_parse_locale_tag_en() {
        assert_eq!(super::parse_locale_tag("en_US.UTF-8"), "en-US");
    }

    #[test]
    fn test_parse_locale_tag_zh() {
        assert_eq!(super::parse_locale_tag("zh_CN.UTF-8"), "zh-CN");
    }

    #[test]
    fn test_parse_locale_tag_c() {
        assert_eq!(super::parse_locale_tag("C"), "C");
    }

    #[test]
    fn test_parse_locale_tag_posix() {
        assert_eq!(super::parse_locale_tag("POSIX"), "POSIX");
    }

    #[test]
    fn test_parse_locale_tag_with_modifier() {
        assert_eq!(super::parse_locale_tag("en_US.UTF-8@collation"), "en-US");
    }

    #[test]
    fn test_parse_locale_tag_simple() {
        assert_eq!(super::parse_locale_tag("fr"), "fr");
    }

    #[test]
    #[allow(unsafe_code)]
    fn test_detect_system_locale_with_lang_zh() {
        let _locale_guard = LOCALE_ENV_LOCK.lock().unwrap();
        // Save original
        let orig_lang = std::env::var("LANG").ok();
        let orig_lc_all = std::env::var("LC_ALL").ok();
        let orig_lc_messages = std::env::var("LC_MESSAGES").ok();
        let orig_oxcache_lang = std::env::var("OXCACHE_LANG").ok();

        // Clear higher-priority vars
        // SAFETY: test-only; serialised by `--test-threads=1` or env mutex in practice.
        unsafe {
            std::env::remove_var("OXCACHE_LANG");
            std::env::remove_var("LC_ALL");
            std::env::remove_var("LC_MESSAGES");
            std::env::set_var("LANG", "zh_CN.UTF-8");
        }

        let locale = detect_system_locale();
        assert_eq!(locale, "zh-CN", "should detect zh-CN from LANG");

        // Restore
        // SAFETY: edition 2024 下 set_var/remove_var 为 unsafe;测试单线程且持 LOCALE_ENV_LOCK,回写块前保存的原值恢复进程环境。
        unsafe {
            std::env::remove_var("LANG");
            if let Some(ref v) = orig_lang {
                std::env::set_var("LANG", v);
            }
            if let Some(ref v) = orig_lc_all {
                std::env::set_var("LC_ALL", v);
            }
            if let Some(ref v) = orig_lc_messages {
                std::env::set_var("LC_MESSAGES", v);
            }
            if let Some(ref v) = orig_oxcache_lang {
                std::env::set_var("OXCACHE_LANG", v);
            }
        }
    }

    #[test]
    #[allow(unsafe_code)]
    fn test_detect_system_locale_with_lang_en() {
        let _locale_guard = LOCALE_ENV_LOCK.lock().unwrap();
        let orig_lang = std::env::var("LANG").ok();
        let orig_lc_all = std::env::var("LC_ALL").ok();
        let orig_lc_messages = std::env::var("LC_MESSAGES").ok();
        let orig_oxcache_lang = std::env::var("OXCACHE_LANG").ok();

        // SAFETY: edition 2024 下 set_var/remove_var 为 unsafe;测试单线程且持 LOCALE_ENV_LOCK,清除高优先级 env 并设置测试 locale。
        unsafe {
            std::env::remove_var("OXCACHE_LANG");
            std::env::remove_var("LC_ALL");
            std::env::remove_var("LC_MESSAGES");
            std::env::set_var("LANG", "en_US.UTF-8");
        }

        let locale = detect_system_locale();
        assert_eq!(locale, "en-US", "should detect en-US from LANG");

        // SAFETY: edition 2024 下 set_var/remove_var 为 unsafe;测试单线程且持 LOCALE_ENV_LOCK,回写块前保存的原值恢复进程环境。
        unsafe {
            std::env::remove_var("LANG");
            if let Some(ref v) = orig_lang {
                std::env::set_var("LANG", v);
            }
            if let Some(ref v) = orig_lc_all {
                std::env::set_var("LC_ALL", v);
            }
            if let Some(ref v) = orig_lc_messages {
                std::env::set_var("LC_MESSAGES", v);
            }
            if let Some(ref v) = orig_oxcache_lang {
                std::env::set_var("OXCACHE_LANG", v);
            }
        }
    }

    #[test]
    #[allow(unsafe_code)]
    fn test_detect_system_locale_c_fallback_to_en() {
        let _locale_guard = LOCALE_ENV_LOCK.lock().unwrap();
        let orig_lang = std::env::var("LANG").ok();
        let orig_lc_all = std::env::var("LC_ALL").ok();
        let orig_lc_messages = std::env::var("LC_MESSAGES").ok();
        let orig_oxcache_lang = std::env::var("OXCACHE_LANG").ok();

        // SAFETY: edition 2024 下 set_var/remove_var 为 unsafe;测试单线程且持 LOCALE_ENV_LOCK,清除高优先级 env 并设置测试 locale。
        unsafe {
            std::env::remove_var("OXCACHE_LANG");
            std::env::remove_var("LC_ALL");
            std::env::remove_var("LC_MESSAGES");
            std::env::set_var("LANG", "C");
        }

        let locale = detect_system_locale();
        assert_eq!(locale, "en", "C locale should fall back to en");

        // SAFETY: edition 2024 下 set_var/remove_var 为 unsafe;测试单线程且持 LOCALE_ENV_LOCK,回写块前保存的原值恢复进程环境。
        unsafe {
            std::env::remove_var("LANG");
            if let Some(ref v) = orig_lang {
                std::env::set_var("LANG", v);
            }
            if let Some(ref v) = orig_lc_all {
                std::env::set_var("LC_ALL", v);
            }
            if let Some(ref v) = orig_lc_messages {
                std::env::set_var("LC_MESSAGES", v);
            }
            if let Some(ref v) = orig_oxcache_lang {
                std::env::set_var("OXCACHE_LANG", v);
            }
        }
    }

    #[test]
    #[allow(unsafe_code)]
    fn test_detect_system_locale_unsupported_fallback_to_en() {
        let _locale_guard = LOCALE_ENV_LOCK.lock().unwrap();
        let orig_lang = std::env::var("LANG").ok();
        let orig_lc_all = std::env::var("LC_ALL").ok();
        let orig_lc_messages = std::env::var("LC_MESSAGES").ok();
        let orig_oxcache_lang = std::env::var("OXCACHE_LANG").ok();

        // SAFETY: edition 2024 下 set_var/remove_var 为 unsafe;测试单线程且持 LOCALE_ENV_LOCK,清除高优先级 env 并设置测试 locale。
        unsafe {
            std::env::remove_var("OXCACHE_LANG");
            std::env::remove_var("LC_ALL");
            std::env::remove_var("LC_MESSAGES");
            std::env::set_var("LANG", "ja_JP.UTF-8");
        }

        let locale = detect_system_locale();
        assert_eq!(
            locale, "en",
            "unsupported locale (ja) should fall back to en"
        );

        // SAFETY: edition 2024 下 set_var/remove_var 为 unsafe;测试单线程且持 LOCALE_ENV_LOCK,回写块前保存的原值恢复进程环境。
        unsafe {
            std::env::remove_var("LANG");
            if let Some(ref v) = orig_lang {
                std::env::set_var("LANG", v);
            }
            if let Some(ref v) = orig_lc_all {
                std::env::set_var("LC_ALL", v);
            }
            if let Some(ref v) = orig_lc_messages {
                std::env::set_var("LC_MESSAGES", v);
            }
            if let Some(ref v) = orig_oxcache_lang {
                std::env::set_var("OXCACHE_LANG", v);
            }
        }
    }

    #[test]
    #[allow(unsafe_code)]
    fn test_detect_system_locale_lc_all_priority() {
        let _locale_guard = LOCALE_ENV_LOCK.lock().unwrap();
        let orig_lang = std::env::var("LANG").ok();
        let orig_lc_all = std::env::var("LC_ALL").ok();
        let orig_lc_messages = std::env::var("LC_MESSAGES").ok();
        let orig_oxcache_lang = std::env::var("OXCACHE_LANG").ok();

        // LC_ALL should take priority over LC_MESSAGES and LANG
        // SAFETY: edition 2024 下 set_var/remove_var 为 unsafe;测试单线程且持 LOCALE_ENV_LOCK,设置多个 locale env 验证优先级。
        unsafe {
            std::env::remove_var("OXCACHE_LANG");
            std::env::set_var("LC_ALL", "zh_CN.UTF-8");
            std::env::set_var("LC_MESSAGES", "en_US.UTF-8");
            std::env::set_var("LANG", "fr_FR.UTF-8");
        }

        let locale = detect_system_locale();
        assert_eq!(
            locale, "zh-CN",
            "LC_ALL should take priority: got '{locale}'"
        );

        // Restore
        // SAFETY: edition 2024 下 set_var/remove_var 为 unsafe;测试单线程且持 LOCALE_ENV_LOCK,回写块前保存的原值恢复进程环境。
        unsafe {
            if let Some(ref v) = orig_lc_all {
                std::env::set_var("LC_ALL", v);
            } else {
                std::env::remove_var("LC_ALL");
            }
            if let Some(ref v) = orig_lc_messages {
                std::env::set_var("LC_MESSAGES", v);
            } else {
                std::env::remove_var("LC_MESSAGES");
            }
            if let Some(ref v) = orig_lang {
                std::env::set_var("LANG", v);
            } else {
                std::env::remove_var("LANG");
            }
            if let Some(ref v) = orig_oxcache_lang {
                std::env::set_var("OXCACHE_LANG", v);
            } else {
                std::env::remove_var("OXCACHE_LANG");
            }
        }
    }

    #[test]
    #[allow(unsafe_code)]
    fn test_detect_system_locale_oxcache_lang_priority() {
        let _locale_guard = LOCALE_ENV_LOCK.lock().unwrap();
        let orig_oxcache_lang = std::env::var("OXCACHE_LANG").ok();
        let orig_lc_all = std::env::var("LC_ALL").ok();
        let orig_lang = std::env::var("LANG").ok();

        // OXCACHE_LANG should take priority over LC_ALL and LANG
        // SAFETY: edition 2024 下 set_var/remove_var 为 unsafe;测试单线程且持 LOCALE_ENV_LOCK,设置多个 locale env 验证优先级。
        unsafe {
            std::env::set_var("OXCACHE_LANG", "zh_CN.UTF-8");
            std::env::set_var("LC_ALL", "en_US.UTF-8");
            std::env::remove_var("LC_MESSAGES");
            std::env::set_var("LANG", "fr_FR.UTF-8");
        }

        let locale = detect_system_locale();
        assert_eq!(
            locale, "zh-CN",
            "OXCACHE_LANG should take priority: got '{locale}'"
        );

        // Empty OXCACHE_LANG must fall through the chain (LC_ALL wins)
        // SAFETY: edition 2024 下 set_var/remove_var 为 unsafe;测试单线程且持 LOCALE_ENV_LOCK,验证空值穿透。
        unsafe {
            std::env::set_var("OXCACHE_LANG", "");
        }
        let locale = detect_system_locale();
        assert_eq!(
            locale, "en-US",
            "empty OXCACHE_LANG should fall through to LC_ALL: got '{locale}'"
        );

        // Restore
        // SAFETY: edition 2024 下 set_var/remove_var 为 unsafe;测试单线程且持 LOCALE_ENV_LOCK,回写块前保存的原值恢复进程环境。
        unsafe {
            if let Some(ref v) = orig_oxcache_lang {
                std::env::set_var("OXCACHE_LANG", v);
            } else {
                std::env::remove_var("OXCACHE_LANG");
            }
            if let Some(ref v) = orig_lc_all {
                std::env::set_var("LC_ALL", v);
            } else {
                std::env::remove_var("LC_ALL");
            }
            if let Some(ref v) = orig_lang {
                std::env::set_var("LANG", v);
            } else {
                std::env::remove_var("LANG");
            }
        }
    }

    #[test]
    fn test_is_supported() {
        assert!(messages::is_supported("en"));
        assert!(messages::is_supported("zh"));
        assert!(!messages::is_supported("fr"));
        assert!(!messages::is_supported("ja"));
        assert!(!messages::is_supported("de"));
    }
}