calnexus 0.1.4

A command-line math expression evaluator with 11 core and 4 optional (numerical/time/unit/fx) computation domains, symbolic calculus, REPL, and batch processing
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
// Copyright (c) 2026 Kirky.X. Licensed under the MIT License.

//! L1 缓存管理器:基于 oxcache 的进程内缓存,使用 BLAKE3 哈希规范形式生成缓存键。
//!
//! 设计依据:
//! - ADD ADR-001:oxcache L1-only(Moka 进程内封装),无 L2/Redis
//! - design.md D5:BLAKE3 作为缓存键哈希,256-bit 碰撞概率可忽略
//! - l1-cache spec:7 个 requirements / 22 个 scenarios
//!
//! 实现说明:
//! - oxcache v0.3 为 async/tokio 架构,本模块通过全局 tokio Runtime + block_on 同步调用
//! - 保持 CacheManager 公共 API 同步,对调用方透明
//! - oxcache 使用 `core` feature(L1+L2 能力),但配置为 memory_only(仅 L1 实际使用)
//!
//! 核心类型:
//! - [`CacheKeyGen`]:将 `CanonicalForm` 哈希为 `[u8; 32]` 键,再转为 hex String 供 oxcache 使用
//! - [`CacheManager`]:线程安全的 L1 缓存,仅存储 `Ok(EvalResult)`,容量上限 10000

use crate::core::types::{CalcError, CanonicalForm, EvalResult};
use std::sync::OnceLock;

/// 全局 tokio Runtime,用于同步调用 oxcache async API。
///
/// 使用 OnceLock 实现懒初始化,进程内只创建一次。
static RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();

fn runtime() -> &'static tokio::runtime::Runtime {
    RUNTIME.get_or_init(|| {
        tokio::runtime::Runtime::new().expect("failed to create tokio runtime for oxcache")
    })
}

/// 缓存键生成器:使用 BLAKE3 对 `CanonicalForm` 的 S-表达式字符串进行哈希。
///
/// 生成 256-bit(32 字节)键,转为 hex 字符串后作为 oxcache 的 `K`。
pub struct CacheKeyGen;

impl CacheKeyGen {
    /// 对 `CanonicalForm` 生成 256-bit BLAKE3 哈希键。
    ///
    /// 返回 `[u8; 32]`,BLAKE3 对空输入也有定义输出(Req 4 Scen 4)。
    pub fn hash(cf: &CanonicalForm) -> [u8; 32] {
        *blake3::hash(cf.as_str().as_bytes()).as_bytes()
    }

    /// 将 `[u8; 32]` 键转为 hex 字符串(oxcache 键要求 `String`)。
    fn to_key_string(bytes: &[u8; 32]) -> String {
        use std::fmt::Write;
        let mut s = String::with_capacity(64);
        for b in bytes {
            write!(s, "{:02x}", b).unwrap();
        }
        s
    }

    /// 生成 oxcache 缓存键(hex 字符串)。
    fn make_key(cf: &CanonicalForm) -> String {
        Self::to_key_string(&Self::hash(cf))
    }
}

/// 默认容量上限(ADD.md §6.3:max_capacity = 10000)。
const DEFAULT_MAX_CAPACITY: u64 = 10_000;

/// L1 缓存管理器。
///
/// 基于 oxcache(Moka 封装),线程安全(`Send + Sync`),进程内有效。
/// 仅缓存 `Ok(EvalResult)`,错误结果不写入。
/// 容量上限 10000 条目,无时间 TTL(仅容量驱逐,Req 5)。
pub struct CacheManager {
    inner: oxcache::Cache<String, EvalResult>,
}

impl CacheManager {
    /// 创建默认配置的缓存管理器(容量 10000,L1-only)。
    ///
    /// 使用 oxcache `CacheBuilder` 配置 Moka 内存后端,
    /// 通过全局 tokio Runtime 同步构建。
    pub fn new() -> Self {
        let cache = runtime()
            .block_on(
                oxcache::Cache::builder()
                    .capacity(DEFAULT_MAX_CAPACITY)
                    .build(),
            )
            .expect("failed to build oxcache L1 cache");
        Self { inner: cache }
    }

    /// 查询缓存。
    ///
    /// 命中时返回缓存值的克隆(`EvalResult` 实现 `Clone`)。
    /// 未命中返回 `None`(Req 2)。
    ///
    /// **错误处理(Rule 12 + H-1 修复)**:oxcache 后端错误降级为 cache miss 而非 panic。
    /// cache 层错误不应影响业务逻辑(用户表达式仍可正确求值),降级为 cache miss 是
    /// 行业标准做法(Redis/Memcached 客户端都这么做)。错误通过 `eprintln!` 显性化记录,
    /// 保留 API 兼容性(`Option<EvalResult>` 签名不变)。
    /// Moka 后端的 get 实际不会返回 Err(永远 `Ok(...)`),但反序列化失败
    /// 等理论错误路径必须显性化而非 panic 影响请求处理。
    pub fn get(&self, cf: &CanonicalForm) -> Option<EvalResult> {
        let key = CacheKeyGen::make_key(cf);
        match runtime().block_on(self.inner.get(&key)) {
            Ok(v) => v,
            Err(e) => {
                // 降级为 cache miss:避免 panic 影响请求处理(H-1 修复)
                eprintln!(
                    "WARN: cache get failed (key={}), degrading to cache miss: {}",
                    key, e
                );
                None
            }
        }
    }

    /// 写入缓存(仅成功结果,Req 7)。
    ///
    /// 接受 `&Result`,仅在 `Ok` 时写入,`Err` 时无操作。
    ///
    /// **错误处理(Rule 12 + H-1 修复)**:oxcache 后端错误降级为忽略写入而非 panic。
    /// cache 写入失败不应影响业务逻辑(用户表达式仍可正确求值并返回结果)。
    /// 错误通过 `eprintln!` 显性化记录,避免静默吞没(规则 12)。
    pub fn insert(&self, cf: &CanonicalForm, result: &Result<EvalResult, CalcError>) {
        if let Ok(value) = result {
            let key = CacheKeyGen::make_key(cf);
            if let Err(e) = runtime().block_on(self.inner.set(&key, value)) {
                // 降级为忽略写入:避免 panic 影响请求处理(H-1 修复)
                eprintln!("WARN: cache insert failed (key={}), ignoring: {}", key, e);
            }
        }
    }

    /// 查询或计算:缓存命中则返回克隆,未命中则调用 `compute`,
    /// 成功时写入缓存,返回结果(Req 1 + Req 2 + Req 7)。
    ///
    /// 错误结果不写入缓存,直接返回 `Err`。
    pub fn get_or_compute<F>(&self, cf: &CanonicalForm, compute: F) -> Result<EvalResult, CalcError>
    where
        F: FnOnce() -> Result<EvalResult, CalcError>,
    {
        if let Some(cached) = self.get(cf) {
            return Ok(cached);
        }
        let result = compute()?;
        self.insert(cf, &Ok(result.clone()));
        Ok(result)
    }

    /// 当前缓存条目数(用于测试验证)。
    ///
    /// 注意:oxcache L1 基于 Moka,entry_count 为最终一致值。
    ///
    /// **错误处理(Rule 12 + H-1 修复)**:oxcache 后端错误降级为返回 0 而非 panic。
    /// 返回 0 不影响业务逻辑(仅用于测试验证),避免 panic 在 HTTP server 上下文中
    /// 走 `spawn_blocking` panic 路径导致客户端仅见通用 500。
    pub fn entry_count(&self) -> u64 {
        match runtime().block_on(self.inner.len()) {
            Ok(n) => n,
            Err(e) => {
                // 降级为 0:避免 panic 影响请求处理(H-1 修复)
                eprintln!("WARN: cache len failed, returning 0: {}", e);
                0
            }
        }
    }
}

impl Default for CacheManager {
    fn default() -> Self {
        Self::new()
    }
}

// 编译期 Send + Sync 约束检查(Req 6 Scen 1)
// coverage 运行时排除:const fn 在编译期执行,无法被行覆盖
#[cfg(not(coverage))]
const _: () = {
    const fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<CacheManager>();
    assert_send_sync::<CacheKeyGen>();
};

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::canonicalizer::AstCanonicalizer;
    use crate::core::parser::parse;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;
    use std::thread;

    // 辅助函数:解析 + 规范化,返回 CanonicalForm
    fn canon(input: &str) -> CanonicalForm {
        let ast = parse(input).unwrap();
        let (_, cf) = AstCanonicalizer::canonicalize(&ast).unwrap();
        cf
    }

    // ===== Requirement 1: 缓存命中 =====

    #[test]
    fn test_cache_hit_returns_cached_value() {
        // 相同表达式第二次求值命中(Req 1 Scen 1)
        let cache = CacheManager::new();
        let cf = CanonicalForm::new("(+ 2 3)");
        cache.insert(&cf, &Ok(EvalResult::Scalar(5.0)));

        let hit = cache.get(&cf);
        assert_eq!(hit, Some(EvalResult::Scalar(5.0)));
    }

    #[test]
    fn test_cache_hit_returns_clone_not_reference() {
        // 命中返回克隆而非引用(Req 1 Scen 3)
        let cache = CacheManager::new();
        let cf = CanonicalForm::new("test");
        cache.insert(&cf, &Ok(EvalResult::Scalar(42.0)));

        let _hit1 = cache.get(&cf).unwrap();
        // 再次读取应得到相同值(克隆语义,不受前一次读取影响)
        let hit2 = cache.get(&cf).unwrap();
        assert_eq!(hit2, EvalResult::Scalar(42.0));
    }

    // ===== Requirement 2: 缓存未命中 =====

    #[test]
    fn test_cache_miss_returns_none() {
        // 首次求值未命中(Req 2 Scen 1)
        let cache = CacheManager::new();
        let cf = CanonicalForm::new("(+ 1 2)");
        assert_eq!(cache.get(&cf), None);
    }

    #[test]
    fn test_insert_then_immediate_hit() {
        // 写入后立即可命中(Req 2 Scen 2)
        let cache = CacheManager::new();
        let cf = CanonicalForm::new("6*7");
        cache.insert(&cf, &Ok(EvalResult::Scalar(42.0)));

        let hit = cache.get(&cf);
        assert_eq!(hit, Some(EvalResult::Scalar(42.0)));
    }

    #[test]
    fn test_get_or_compute_calls_compute_on_miss() {
        // 未命中时调用 compute(Req 2 Scen 3)
        let cache = CacheManager::new();
        let cf = canon("6*7");

        let call_count = Arc::new(AtomicUsize::new(0));
        let cc = Arc::clone(&call_count);

        let result = cache.get_or_compute(&cf, || {
            cc.fetch_add(1, Ordering::SeqCst);
            Ok(EvalResult::Scalar(42.0))
        });

        assert_eq!(result.unwrap(), EvalResult::Scalar(42.0));
        assert_eq!(call_count.load(Ordering::SeqCst), 1);
    }

    // ===== Requirement 3: 等价表达式缓存去重 =====

    #[test]
    fn test_commutative_equivalent_share_cache() {
        // 交换律等价表达式共享缓存(Req 3 Scen 1)
        let cache = CacheManager::new();
        let cf_2plus3 = canon("2+3");
        let cf_3plus2 = canon("3+2");
        assert_eq!(cf_2plus3, cf_3plus2, "规范形式应相同");

        cache.insert(&cf_2plus3, &Ok(EvalResult::Scalar(5.0)));
        assert_eq!(cache.get(&cf_3plus2), Some(EvalResult::Scalar(5.0)));
    }

    #[test]
    fn test_constant_folding_equivalent_share_cache() {
        // 常量折叠等价表达式共享缓存(Req 3 Scen 2)
        let cache = CacheManager::new();
        let cf_1 = canon("2*3+1");
        let cf_2 = canon("1+6");
        assert_eq!(cf_1, cf_2, "规范形式应相同");

        cache.insert(&cf_1, &Ok(EvalResult::Scalar(7.0)));
        assert_eq!(cache.get(&cf_2), Some(EvalResult::Scalar(7.0)));
    }

    #[test]
    fn test_non_equivalent_do_not_share_cache() {
        // 非等价表达式不共享缓存(Req 3 Scen 3)
        let cache = CacheManager::new();
        let cf_2minus3 = canon("2-3");
        let cf_3minus2 = canon("3-2");
        assert_ne!(cf_2minus3, cf_3minus2, "规范形式应不同");

        cache.insert(&cf_2minus3, &Ok(EvalResult::Scalar(-1.0)));
        assert_eq!(cache.get(&cf_3minus2), None, "不同规范形式不应命中");
    }

    // ===== Requirement 4: 缓存键生成 =====

    #[test]
    fn test_same_canonical_form_same_key() {
        // 相同规范形式生成相同键(Req 4 Scen 1)
        let cf1 = CanonicalForm::new("(+ 2 3)");
        let cf2 = CanonicalForm::new("(+ 2 3)");
        assert_eq!(CacheKeyGen::hash(&cf1), CacheKeyGen::hash(&cf2));
    }

    #[test]
    fn test_different_canonical_form_different_key() {
        // 不同规范形式生成不同键(Req 4 Scen 2)
        let cf1 = CanonicalForm::new("(+ 2 3)");
        let cf2 = CanonicalForm::new("(* 2 3)");
        assert_ne!(CacheKeyGen::hash(&cf1), CacheKeyGen::hash(&cf2));
    }

    #[test]
    fn test_key_length_is_32_bytes() {
        // 键长度为 256 位(Req 4 Scen 3)
        let cf = CanonicalForm::new("(+ 2 3)");
        let key = CacheKeyGen::hash(&cf);
        assert_eq!(key.len(), 32);
    }

    #[test]
    fn test_empty_string_generates_key() {
        // 空字符串也可生成键(Req 4 Scen 4)
        let cf = CanonicalForm::new("");
        let key = CacheKeyGen::hash(&cf);
        assert_eq!(key.len(), 32);
        // BLAKE3 对空输入有定义输出,非全零
        assert!(key.iter().any(|&b| b != 0));
    }

    // ===== Requirement 5: 缓存 TTL 等于进程生命周期 =====

    #[test]
    fn test_cache_persists_within_process() {
        // 进程内缓存持续有效(Req 5 Scen 1)
        let cache = CacheManager::new();
        let cf = CanonicalForm::new("(+ 1 1)");
        cache.insert(&cf, &Ok(EvalResult::Scalar(2.0)));

        for _ in 0..10 {
            assert_eq!(cache.get(&cf), Some(EvalResult::Scalar(2.0)));
        }
    }

    #[test]
    fn test_no_time_based_eviction() {
        // 无 TTL 过期驱逐(Req 5 Scen 3)
        let cache = CacheManager::new();
        let cf = CanonicalForm::new("(+ 1 1)");
        cache.insert(&cf, &Ok(EvalResult::Scalar(2.0)));

        thread::sleep(std::time::Duration::from_millis(50));

        // 仍应命中(仅容量驱逐,无时间 TTL)
        assert_eq!(cache.get(&cf), Some(EvalResult::Scalar(2.0)));
    }

    // ===== Requirement 6: 缓存线程安全 =====

    #[test]
    fn test_cache_manager_is_send_sync() {
        // Engine/CacheManager 为 Send + sync(Req 6 Scen 1)
        fn assert_send_sync<T: Send + Sync>(_: &T) {}
        let cache = CacheManager::new();
        assert_send_sync(&cache);
    }

    #[test]
    fn test_concurrent_read_hits() {
        // 多线程并发读命中(Req 6 Scen 2)
        let cache = Arc::new(CacheManager::new());
        let cf = Arc::new(CanonicalForm::new("(+ 2 3)"));
        cache.insert(&cf, &Ok(EvalResult::Scalar(5.0)));

        let mut handles = vec![];
        for _ in 0..8 {
            let cache = Arc::clone(&cache);
            let cf = Arc::clone(&cf);
            handles.push(thread::spawn(move || {
                for _ in 0..1000 {
                    assert_eq!(cache.get(&cf), Some(EvalResult::Scalar(5.0)));
                }
            }));
        }
        for h in handles {
            h.join().unwrap();
        }
    }

    #[test]
    fn test_concurrent_writes_no_conflict() {
        // 多线程并发写不冲突(Req 6 Scen 3)
        let cache = Arc::new(CacheManager::new());

        let mut handles = vec![];
        for i in 0..8u64 {
            let cache = Arc::clone(&cache);
            handles.push(thread::spawn(move || {
                let cf = CanonicalForm::new(format!("(+ {} {})", i, i).as_str());
                let result = EvalResult::Scalar((i * 2) as f64);
                cache.insert(&cf, &Ok(result.clone()));
                assert_eq!(cache.get(&cf), Some(result));
            }));
        }
        for h in handles {
            h.join().unwrap();
        }

        // 验证所有线程的条目都可读回
        for i in 0..8u64 {
            let cf = CanonicalForm::new(format!("(+ {} {})", i, i).as_str());
            let expected = EvalResult::Scalar((i * 2) as f64);
            assert_eq!(
                cache.get(&cf),
                Some(expected),
                "线程 {} 的缓存条目应可读",
                i
            );
        }
    }

    // ===== Requirement 7: 缓存不存储错误结果 =====

    #[test]
    fn test_error_result_not_cached() {
        // 除零错误不写入缓存(Req 7 Scen 1)
        let cache = CacheManager::new();
        let cf = CanonicalForm::new("1/0");

        let result = cache.get_or_compute(&cf, || Err(CalcError::division_by_zero()));

        assert!(result.is_err());
        assert_eq!(cache.get(&cf), None, "错误结果不应写入缓存");
    }

    #[test]
    fn test_nan_error_not_cached() {
        // NaN 结果不写入缓存(Req 7 Scen 2)
        let cache = CacheManager::new();
        let cf = CanonicalForm::new("sqrt(-1)");

        let result = cache.get_or_compute(&cf, || Err(CalcError::nan_or_inf()));

        assert!(result.is_err());
        assert_eq!(cache.get(&cf), None, "NaN 错误不应写入缓存");
    }

    #[test]
    fn test_only_success_cached() {
        // 仅成功结果写入缓存(Req 7 Scen 3)
        let cache = CacheManager::new();
        let cf_ok = CanonicalForm::new("(+ 1 2)");
        let cf_err = CanonicalForm::new("(1/0)");

        cache.insert(&cf_ok, &Ok(EvalResult::Scalar(3.0)));
        cache.insert(&cf_err, &Err(CalcError::division_by_zero()));

        assert_eq!(cache.get(&cf_ok), Some(EvalResult::Scalar(3.0)));
        assert_eq!(cache.get(&cf_err), None, "错误结果不应写入");
    }

    // ===== get_or_compute 完整流程 =====

    #[test]
    fn test_get_or_compute_hits_cache_on_second_call() {
        // 验证第二次调用命中缓存且不调用 compute
        let cache = CacheManager::new();
        let cf = canon("2+3");

        let call_count = Arc::new(AtomicUsize::new(0));

        let cc = Arc::clone(&call_count);
        let r1 = cache.get_or_compute(&cf, || {
            cc.fetch_add(1, Ordering::SeqCst);
            Ok(EvalResult::Scalar(5.0))
        });
        assert_eq!(r1.unwrap(), EvalResult::Scalar(5.0));
        assert_eq!(call_count.load(Ordering::SeqCst), 1);

        // 第二次调用应命中缓存:返回 5.0 而非 999.0,call_count 不变
        let r2 = cache.get_or_compute(&cf, || Ok(EvalResult::Scalar(999.0)));
        assert_eq!(r2.unwrap(), EvalResult::Scalar(5.0), "应返回缓存值");
        assert_eq!(call_count.load(Ordering::SeqCst), 1, "compute 不应被调用");
    }

    // ===== entry_count / Default 覆盖 =====

    #[test]
    fn test_entry_count_zero_on_empty_cache() {
        // 空缓存 entry_count 应为 0(覆盖 entry_count 方法体)
        let cache = CacheManager::new();
        // 新建的空缓存无任何插入,entry_count 必须为 0
        assert_eq!(cache.entry_count(), 0, "fresh cache should have 0 entries");
    }

    #[test]
    fn test_entry_count_increases_after_insert() {
        // 写入后 entry_count 方法应可调用(覆盖 entry_count 方法体)
        // 注意:oxcache L1 基于 Moka,entry_count 为最终一致值,可能不立即反映插入
        let cache = CacheManager::new();
        let cf = CanonicalForm::new("(+ 1 2)");
        cache.insert(&cf, &Ok(EvalResult::Scalar(3.0)));
        // 调用 entry_count 不应 panic
        let _count = cache.entry_count();
    }

    #[test]
    fn test_default_creates_working_cache() {
        // Default::default() 应创建可用缓存(覆盖 Default impl)
        let cache = CacheManager::default();
        let cf = CanonicalForm::new("(+ 5 7)");
        assert_eq!(cache.get(&cf), None);
        cache.insert(&cf, &Ok(EvalResult::Scalar(12.0)));
        assert_eq!(cache.get(&cf), Some(EvalResult::Scalar(12.0)));
    }

    #[test]
    fn test_get_or_compute_closure_runs_on_miss() {
        // 覆盖 get_or_compute 闭包体实际执行路径(不同 CF 触发闭包)
        let cache = CacheManager::new();
        let cf1 = CanonicalForm::new("(+ 100 1)");
        let cf2 = CanonicalForm::new("(+ 200 2)");

        let r1 = cache.get_or_compute(&cf1, || Ok(EvalResult::Scalar(101.0)));
        assert_eq!(r1.unwrap(), EvalResult::Scalar(101.0));

        // 不同 CF:缓存未命中,闭包体实际执行
        let r2 = cache.get_or_compute(&cf2, || Ok(EvalResult::Scalar(202.0)));
        assert_eq!(r2.unwrap(), EvalResult::Scalar(202.0));
    }

    #[test]
    fn test_cache_keygen_to_key_string_via_make_key() {
        // 间接覆盖 make_key(pub get 路径调用 make_key)
        let cache = CacheManager::new();
        let cf = CanonicalForm::new("(unique-key-test 42)");
        cache.insert(&cf, &Ok(EvalResult::Scalar(7.0)));
        // 通过 get 命中验证 make_key 生成一致键
        assert_eq!(cache.get(&cf), Some(EvalResult::Scalar(7.0)));
    }

    // ===== H-1 修复:cache 层错误降级契约测试 =====
    //
    // 以下测试验证 cache 层错误降级行为契约:
    // - get/insert/entry_count 永不 panic(避免 HTTP server 上下文中的 spawn_blocking panic)
    // - 错误路径降级为 cache miss / 忽略 / 返回 0
    // - 保留 API 兼容性(签名不变)
    //
    // 注:oxcache Moka 后端实际不会返回 Err(永远 Ok(...)),故无法直接触发错误路径。
    // 这些测试作为行为契约,确保后续 cache 后端替换或扩展时降级行为不被破坏。

    #[test]
    fn test_get_never_panics_on_normal_path() {
        // H-1 契约:get 在正常路径下不 panic,返回 Option<EvalResult>
        let cache = CacheManager::new();
        let cf = CanonicalForm::new("(+ 1 2)");
        // 空缓存 → None
        assert_eq!(cache.get(&cf), None);
        // 写入后 → Some
        cache.insert(&cf, &Ok(EvalResult::Scalar(3.0)));
        assert_eq!(cache.get(&cf), Some(EvalResult::Scalar(3.0)));
    }

    #[test]
    fn test_insert_never_panics_on_normal_path() {
        // H-1 契约:insert 在正常路径下不 panic
        let cache = CacheManager::new();
        let cf = CanonicalForm::new("(+ 100 1)");
        // 写入 Ok
        cache.insert(&cf, &Ok(EvalResult::Scalar(101.0)));
        assert_eq!(cache.get(&cf), Some(EvalResult::Scalar(101.0)));
        // 写入 Err(应被忽略,不写入缓存)
        cache.insert(&cf, &Err(CalcError::division_by_zero()));
        assert_eq!(cache.get(&cf), Some(EvalResult::Scalar(101.0)));
    }

    #[test]
    fn test_entry_count_never_panics_on_normal_path() {
        // H-1 契约:entry_count 在正常路径下不 panic,返回 u64
        let cache = CacheManager::new();
        // 空缓存 → 0
        let count = cache.entry_count();
        assert_eq!(count, 0);
        // 写入后仍可调用且不 panic
        let cf = CanonicalForm::new("(+ 5 7)");
        cache.insert(&cf, &Ok(EvalResult::Scalar(12.0)));
        let _count_after = cache.entry_count();
    }

    // ===== proptest 属性测试 =====

    use proptest::prelude::*;

    proptest! {
        #![proptest_config(ProptestConfig { cases: 256, ..ProptestConfig::default() })]

        // 交换律:a+b 与 b+a 共享缓存(使用变量避免常量折叠)
        #[test]
        fn prop_commutative_expressions_share_cache(
            a in (0u8..26u8).prop_map(|i| ((b'a' + i) as char).to_string()),
            b in (0u8..26u8).prop_map(|i| ((b'a' + i) as char).to_string())
        ) {
            let cache = CacheManager::new();
            let cf_ab = canon(&format!("{}+{}", a, b));
            let cf_ba = canon(&format!("{}+{}", b, a));
            prop_assert_eq!(&cf_ab, &cf_ba, "规范形式应相同");

            cache.insert(&cf_ab, &Ok(EvalResult::Scalar(5.0)));
            prop_assert_eq!(cache.get(&cf_ba), Some(EvalResult::Scalar(5.0)));
        }

        // 缓存命中可重复:同一规范化 Key 二次求值返回缓存值
        #[test]
        fn prop_cache_hit_repeatable(
            x in (1u8..100u8).prop_map(|i| format!("{}+{}", i, i+1))
        ) {
            let cache = CacheManager::new();
            let cf = canon(&x);
            cache.insert(&cf, &Ok(EvalResult::Scalar(42.0)));
            prop_assert_eq!(cache.get(&cf), Some(EvalResult::Scalar(42.0)));
            prop_assert_eq!(cache.get(&cf), Some(EvalResult::Scalar(42.0)));
        }

        // 不同表达式不应共享缓存(排除碰撞概率)
        #[test]
        fn prop_distinct_expressions_distinct_cache(
            a in (1u8..50u8).prop_map(|i| format!("{}*2", i)),
            b in (51u8..100u8).prop_map(|i| format!("{}*2", i))
        ) {
            let cache = CacheManager::new();
            let cf_a = canon(&a);
            let cf_b = canon(&b);
            if cf_a != cf_b {
                cache.insert(&cf_a, &Ok(EvalResult::Scalar(1.0)));
                prop_assert_eq!(cache.get(&cf_b), None, "不同表达式不应命中");
            }
        }
    }
}