garrison 0.9.0

Garrison - 面向 Rust 生态的一站式身份认证鉴权框架
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
// Copyright (c) 2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

//! DbnexusUserDeviceRepository 实现(app_user_device 表)。

use super::{read_bool, v_i64, v_opt_str, v_str, DbnexusUserDeviceRepository};
use crate::dao::dao_session;
use crate::dao::repository::{make_statement, UserDeviceRepository, UserDeviceRow, MAX_DEVICES};
use crate::error::{GarrisonError, GarrisonResult};
use async_trait::async_trait;
use dbnexus::sea_orm::{ConnectionTrait, QueryResult};
use dbnexus::DbPool;

impl DbnexusUserDeviceRepository {
    /// 创建实例。
    pub fn new(pool: DbPool) -> Self {
        Self { pool }
    }
}

#[async_trait]
impl UserDeviceRepository for DbnexusUserDeviceRepository {
    async fn register_device(
        &self,
        tenant_id: i64,
        login_id: &str,
        identifier: &str,
        ua: &str,
    ) -> GarrisonResult<String> {
        dao_session!(
            self.pool,
            "dao-app-user-device-register-device",
            session,
            conn
        );

        // 幂等:相同 tenant_id + login_id + identifier 返回已有 ID
        let find_sql = "SELECT id FROM app_user_device \
                        WHERE tenant_id = ? AND login_id = ? AND device_identifier = ?";
        let stmt = make_statement(
            conn,
            find_sql,
            vec![v_i64(tenant_id), v_str(login_id), v_str(identifier)],
        );
        let existing = conn
            .query_one_raw(stmt)
            .await
            .map_err(|e| GarrisonError::Dao(format!("dao-app-user-device-query-exists::{}", e)))?;

        if let Some(row) = existing {
            let existing_id: String = row.try_get("", "id").map_err(|e| {
                GarrisonError::Dao(format!("dao-app-user-device-parse-exists-id::{}", e))
            })?;
            // 更新 last_seen_at(幂等注册视为一次活跃)
            let now = chrono::Utc::now().timestamp();
            let update_sql = "UPDATE app_user_device SET last_seen_at = ? WHERE id = ?";
            let stmt = make_statement(conn, update_sql, vec![v_i64(now), v_str(&existing_id)]);
            conn.execute_raw(stmt).await.map_err(|e| {
                GarrisonError::Dao(format!("dao-app-user-device-update-last-seen-at::{}", e))
            })?;
            return Ok(existing_id);
        }

        // 2+3. MAX_DEVICES 上限检查 + 插入合并为单条条件 INSERT(原子,消除
        // check-then-act TOCTOU):WHERE 子查询与 INSERT 同语句执行,
        // SQLite(单写者)下无并发窗口;PG/MySQL 在 READ COMMITTED 下
        // 仍存在语句级快照窗口(见 `UserDeviceRepository::register_device` 文档)。
        // `FROM (SELECT 1) AS _g` 为占位 derived table:MySQL 不支持无 FROM
        // 的 SELECT...WHERE,SQLite/PG/MySQL 三方言均兼容该写法。
        // rows_affected == 0 表示条件未满足(计数已达上限,含并发下被抢先填满)。
        let device_id = uuid::Uuid::new_v4().to_string();
        let now = chrono::Utc::now().timestamp();
        let device_name = parse_device_name(ua);
        let insert_sql = "INSERT INTO app_user_device \
                          (id, tenant_id, login_id, device_identifier, device_name, user_agent, is_blocked, last_seen_at, created_at) \
                          SELECT ?, ?, ?, ?, ?, ?, 0, ?, ? FROM (SELECT 1) AS _g \
                          WHERE (SELECT COUNT(*) FROM app_user_device \
                                 WHERE tenant_id = ? AND login_id = ?) < ?";
        let stmt = make_statement(
            conn,
            insert_sql,
            vec![
                v_str(&device_id),
                v_i64(tenant_id),
                v_str(login_id),
                v_str(identifier),
                v_opt_str(&device_name),
                v_opt_str(&Some(ua.to_string())),
                v_i64(now),
                v_i64(now),
                v_i64(tenant_id),
                v_str(login_id),
                v_i64(MAX_DEVICES as i64),
            ],
        );
        let result = conn
            .execute_raw(stmt)
            .await
            .map_err(|e| GarrisonError::Dao(format!("dao-app-user-device-insert::{}", e)))?;
        if result.rows_affected() == 0 {
            let who = format!("tenant_id={}, login_id={}", tenant_id, login_id);
            return Err(GarrisonError::InvalidParam(format!(
                "dao-user-device-limit-exceeded::{}::{}",
                who, MAX_DEVICES
            )));
        }
        Ok(device_id)
    }

    async fn block_device(&self, tenant_id: i64, device_id: &str) -> GarrisonResult<()> {
        dao_session!(self.pool, "dao-app-user-device-block-device", session, conn);
        // 租户谓词强制(渗透-租户隔离-设备接口-1):本方法为 app_user_device
        // 唯一按 id 直写路径,仅 WHERE id = ? 会构成跨租户封禁原语
        // (PG/MySQL 经 pub use 共用本实现)。
        let sql = "UPDATE app_user_device SET is_blocked = 1 WHERE tenant_id = ? AND id = ?";
        let stmt = make_statement(conn, sql, vec![v_i64(tenant_id), v_str(device_id)]);
        conn.execute_raw(stmt)
            .await
            .map_err(|e| GarrisonError::Dao(format!("dao-app-user-device-block-update::{}", e)))?;
        Ok(())
    }

    async fn unblock_device(&self, tenant_id: i64, device_id: &str) -> GarrisonResult<()> {
        dao_session!(
            self.pool,
            "dao-app-user-device-unblock-device",
            session,
            conn
        );
        // 租户谓词强制:契约同 block_device
        let sql = "UPDATE app_user_device SET is_blocked = 0 WHERE tenant_id = ? AND id = ?";
        let stmt = make_statement(conn, sql, vec![v_i64(tenant_id), v_str(device_id)]);
        conn.execute_raw(stmt).await.map_err(|e| {
            GarrisonError::Dao(format!("dao-app-user-device-unblock-update::{}", e))
        })?;
        Ok(())
    }

    async fn list_user_devices(
        &self,
        tenant_id: i64,
        login_id: &str,
    ) -> GarrisonResult<Vec<UserDeviceRow>> {
        dao_session!(self.pool, "dao-app-user-device-list", session, conn);
        let sql = "SELECT id, tenant_id, login_id, device_identifier, device_name, user_agent, \
                  is_blocked, last_seen_at, created_at \
                  FROM app_user_device WHERE tenant_id = ? AND login_id = ?";
        let stmt = make_statement(conn, sql, vec![v_i64(tenant_id), v_str(login_id)]);
        let rows = conn
            .query_all_raw(stmt)
            .await
            .map_err(|e| GarrisonError::Dao(format!("dao-app-user-device-list-query::{}", e)))?;
        rows.iter().map(parse_user_device_row).collect()
    }

    async fn count_user_devices(&self, tenant_id: i64, login_id: &str) -> GarrisonResult<usize> {
        dao_session!(self.pool, "dao-app-user-device-count", session, conn);
        let sql =
            "SELECT COUNT(*) AS cnt FROM app_user_device WHERE tenant_id = ? AND login_id = ?";
        let stmt = make_statement(conn, sql, vec![v_i64(tenant_id), v_str(login_id)]);
        let row = conn
            .query_one_raw(stmt)
            .await
            .map_err(|e| GarrisonError::Dao(format!("dao-app-user-device-count-query::{}", e)))?
            .ok_or_else(|| GarrisonError::Dao("dao-app-user-device-count-empty::".into()))?;
        let count: i64 = row
            .try_get("", "cnt")
            .map_err(|e| GarrisonError::Dao(format!("dao-app-user-device-parse-count::{}", e)))?;
        Ok(count as usize)
    }
}

/// 解析 app_user_device 行。
fn parse_user_device_row(row: &QueryResult) -> GarrisonResult<UserDeviceRow> {
    Ok(UserDeviceRow {
        id: row
            .try_get("", "id")
            .map_err(|e| GarrisonError::Dao(format!("dao-app-user-device-row-parse-id::{}", e)))?,
        tenant_id: row.try_get("", "tenant_id").map_err(|e| {
            GarrisonError::Dao(format!("dao-app-user-device-row-parse-tenant-id::{}", e))
        })?,
        login_id: row.try_get("", "login_id").map_err(|e| {
            GarrisonError::Dao(format!("dao-app-user-device-row-parse-login-id::{}", e))
        })?,
        device_identifier: row.try_get("", "device_identifier").map_err(|e| {
            GarrisonError::Dao(format!(
                "dao-app-user-device-row-parse-device-identifier::{}",
                e
            ))
        })?,
        device_name: row.try_get("", "device_name").map_err(|e| {
            GarrisonError::Dao(format!("dao-app-user-device-row-parse-device-name::{}", e))
        })?,
        user_agent: row.try_get("", "user_agent").map_err(|e| {
            GarrisonError::Dao(format!("dao-app-user-device-row-parse-user-agent::{}", e))
        })?,
        is_blocked: read_bool(row, "is_blocked")?,
        last_seen_at: row.try_get("", "last_seen_at").map_err(|e| {
            GarrisonError::Dao(format!("dao-app-user-device-row-parse-last-seen-at::{}", e))
        })?,
        created_at: row.try_get("", "created_at").map_err(|e| {
            GarrisonError::Dao(format!("dao-app-user-device-row-parse-created-at::{}", e))
        })?,
    })
}

/// 从 User-Agent 字符串解析设备名(简单字符串启发式)。
///
/// 完整 `ua-parser` regex 集需启用 `ua-parser-precompiled` feature(设计决策延后),
/// 当前用关键字匹配提取 Browser + OS 信息。
fn parse_device_name(ua: &str) -> Option<String> {
    if ua.is_empty() {
        return None;
    }
    let browser = detect_browser(ua);
    let os = detect_os(ua);
    match (browser, os) {
        (Some(b), Some(o)) => Some(format!("{} on {}", b, o)),
        (Some(b), None) => Some(b),
        (None, Some(o)) => Some(o),
        (None, None) => None,
    }
}

/// 从 UA 检测浏览器名。
fn detect_browser(ua: &str) -> Option<String> {
    // 注意:检测顺序重要——Edg 必须在 Chrome 之前(Edge UA 包含 Chrome)
    if ua.contains("Edg/") {
        Some("Edge".into())
    } else if ua.contains("Firefox/") {
        Some("Firefox".into())
    } else if ua.contains("Chrome/") {
        Some("Chrome".into())
    } else if ua.contains("Safari/") {
        Some("Safari".into())
    } else if ua.contains("OPR/") || ua.contains("Opera/") {
        Some("Opera".into())
    } else {
        None
    }
}

/// 从 UA 检测操作系统名。
fn detect_os(ua: &str) -> Option<String> {
    if ua.contains("Windows NT") {
        Some("Windows".into())
    } else if ua.contains("iPhone") || ua.contains("iPad") {
        Some("iOS".into())
    } else if ua.contains("Mac OS X") || ua.contains("Macintosh") {
        Some("macOS".into())
    } else if ua.contains("Android") {
        Some("Android".into())
    } else if ua.contains("Linux") {
        Some("Linux".into())
    } else {
        None
    }
}

#[cfg(all(test, feature = "db-sqlite"))]
mod tests {
    use super::*;
    use crate::dao::repository::sqlite::test_support::setup_db;

    /// register_device 首次注册应返回合法 UUID,且 list 中能查到。
    #[tokio::test(flavor = "multi_thread")]
    async fn register_device_returns_uuid() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool);

        let device_id = repo
            .register_device(
                1,
                "login-001",
                "fingerprint-abc",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0",
            )
            .await
            .expect("register_device 应成功");

        let parsed = uuid::Uuid::parse_str(&device_id).expect("返回的 device_id 应为合法 UUID");
        assert_eq!(
            parsed.get_version(),
            Some(uuid::Version::Random),
            "应为 UUID v4"
        );

        let devices = repo
            .list_user_devices(1, "login-001")
            .await
            .expect("list_user_devices 应成功");
        assert_eq!(devices.len(), 1);
        assert_eq!(devices[0].id, device_id);
        assert_eq!(devices[0].device_identifier, "fingerprint-abc");
        assert!(!devices[0].is_blocked);
        // device_name 应从 UA 解析(Chrome on Windows)
        assert_eq!(devices[0].device_name.as_deref(), Some("Chrome on Windows"));
    }

    /// register_device 幂等:相同 (tenant, login_id, identifier) 返回同一 ID,且 last_seen_at 更新。
    #[tokio::test(flavor = "multi_thread")]
    async fn register_device_idempotent_returns_same_id() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool);

        let id1 = repo
            .register_device(1, "login-002", "fp-same", "Firefox/120.0")
            .await
            .expect("首次 register 应成功");

        let id2 = repo
            .register_device(1, "login-002", "fp-same", "Firefox/121.0")
            .await
            .expect("幂等 register 应成功");

        assert_eq!(id1, id2, "幂等注册应返回相同 ID");

        // 仍只有 1 条记录
        let count = repo
            .count_user_devices(1, "login-002")
            .await
            .expect("count 应成功");
        assert_eq!(count, 1, "幂等注册不应新增记录");
    }

    /// register_device 达到 MAX_DEVICES 后拒绝新设备,返回 InvalidParam 错误。
    #[tokio::test(flavor = "multi_thread")]
    async fn register_device_exceeds_max_devices() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool);

        // 注册 MAX_DEVICES(5)个不同设备
        for i in 0..MAX_DEVICES {
            repo.register_device(
                1,
                "login-max",
                &format!("fp-{}", i),
                "Mozilla/5.0 Chrome/120.0",
            )
            .await
            .expect("注册设备应成功");
        }

        let count = repo
            .count_user_devices(1, "login-max")
            .await
            .expect("count 应成功");
        assert_eq!(count, MAX_DEVICES, "应有 MAX_DEVICES 个设备");

        let result = repo
            .register_device(1, "login-max", "fp-overflow", "Chrome/120.0")
            .await;
        assert!(result.is_err(), "超过 MAX_DEVICES 应返回错误");
        let err = result.unwrap_err();
        match err {
            GarrisonError::InvalidParam(msg) => {
                assert!(
                    msg.contains("dao-user-device-limit-exceeded")
                        || msg.contains("MAX_DEVICES")
                        || msg.contains("设备数已达上限"),
                    "错误信息应包含设备上限,实际: {}",
                    msg
                );
            },
            other => panic!("应为 InvalidParam 错误,实际: {:?}", other),
        }
    }

    /// block_device / unblock_device 切换 is_blocked 状态。
    #[tokio::test(flavor = "multi_thread")]
    async fn block_and_unblock_device() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool);

        let device_id = repo
            .register_device(1, "login-block", "fp-block", "Safari/17.0")
            .await
            .expect("register 应成功");

        let devices = repo
            .list_user_devices(1, "login-block")
            .await
            .expect("list 应成功");
        assert!(!devices[0].is_blocked, "初始状态应未阻断");

        repo.block_device(1, &device_id)
            .await
            .expect("block_device 应成功");
        let devices = repo
            .list_user_devices(1, "login-block")
            .await
            .expect("list 应成功");
        assert!(devices[0].is_blocked, "阻断后 is_blocked 应为 true");

        repo.unblock_device(1, &device_id)
            .await
            .expect("unblock_device 应成功");
        let devices = repo
            .list_user_devices(1, "login-block")
            .await
            .expect("list 应成功");
        assert!(!devices[0].is_blocked, "解除后 is_blocked 应为 false");
    }

    /// list_user_devices 按 (tenant_id, login_id) 过滤,不同 login_id 互不干扰。
    #[tokio::test(flavor = "multi_thread")]
    async fn list_user_devices_filters_by_login_id() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool);

        // login-a 注册 2 个设备
        repo.register_device(1, "login-a", "fp-a1", "Chrome/120.0")
            .await
            .expect("register 应成功");
        repo.register_device(1, "login-a", "fp-a2", "Edge/120.0")
            .await
            .expect("register 应成功");

        // login-b 注册 1 个设备
        repo.register_device(1, "login-b", "fp-b1", "Firefox/120.0")
            .await
            .expect("register 应成功");

        let list_a = repo
            .list_user_devices(1, "login-a")
            .await
            .expect("list login-a 应成功");
        let list_b = repo
            .list_user_devices(1, "login-b")
            .await
            .expect("list login-b 应成功");
        assert_eq!(list_a.len(), 2, "login-a 应有 2 个设备");
        assert_eq!(list_b.len(), 1, "login-b 应有 1 个设备");
    }

    /// count_user_devices 返回正确计数,空用户返回 0。
    #[tokio::test(flavor = "multi_thread")]
    async fn count_user_devices_empty_returns_zero() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool);

        // 空用户
        let count = repo
            .count_user_devices(1, "nonexistent-login")
            .await
            .expect("count 应成功");
        assert_eq!(count, 0, "空用户设备数应为 0");

        repo.register_device(1, "login-count", "fp-1", "Chrome/120.0")
            .await
            .expect("register 应成功");
        let count = repo
            .count_user_devices(1, "login-count")
            .await
            .expect("count 应成功");
        assert_eq!(count, 1, "注册 1 个后应为 1");
    }

    // 纯函数测试:parse_device_name / detect_browser / detect_os
    // (不依赖数据库,验证 UA 解析逻辑)

    /// parse_device_name 从 Chrome on Windows UA 提取 "Chrome on Windows"。
    #[test]
    fn parse_device_name_chrome_on_windows() {
        let ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0";
        assert_eq!(parse_device_name(ua).as_deref(), Some("Chrome on Windows"));
    }

    /// parse_device_name 从 Firefox on macOS UA 提取 "Firefox on macOS"。
    #[test]
    fn parse_device_name_firefox_on_macos() {
        let ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) Firefox/121.0";
        assert_eq!(parse_device_name(ua).as_deref(), Some("Firefox on macOS"));
    }

    /// parse_device_name 从 Edge UA 提取 "Edge"(Edg/ 必须在 Chrome/ 之前检测)。
    #[test]
    fn parse_device_name_edge_before_chrome() {
        let ua = "Mozilla/5.0 (Windows NT 10.0) Edg/120.0 Chrome/120.0";
        assert_eq!(parse_device_name(ua).as_deref(), Some("Edge on Windows"));
    }

    /// parse_device_name 从 Safari on iOS UA 提取 "Safari on iOS"。
    #[test]
    fn parse_device_name_safari_on_ios() {
        let ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0) Safari/604.1";
        assert_eq!(parse_device_name(ua).as_deref(), Some("Safari on iOS"));
    }

    /// parse_device_name 从 Opera UA 提取 "Opera"。
    #[test]
    fn parse_device_name_opera() {
        let ua = "Mozilla/5.0 (Windows NT 10.0) OPR/120.0";
        assert_eq!(parse_device_name(ua).as_deref(), Some("Opera on Windows"));
    }

    /// parse_device_name 从 Android UA 提取浏览器 + Android。
    #[test]
    fn parse_device_name_chrome_on_android() {
        let ua = "Mozilla/5.0 (Linux; Android 14) Chrome/120.0";
        assert_eq!(parse_device_name(ua).as_deref(), Some("Chrome on Android"));
    }

    /// parse_device_name 从 Linux UA 提取。
    #[test]
    fn parse_device_name_firefox_on_linux() {
        let ua = "Mozilla/5.0 (X11; Linux x86_64) Firefox/121.0";
        assert_eq!(parse_device_name(ua).as_deref(), Some("Firefox on Linux"));
    }

    /// parse_device_name 空字符串返回 None。
    #[test]
    fn parse_device_name_empty_returns_none() {
        assert!(parse_device_name("").is_none());
    }

    /// parse_device_name 未知浏览器但有 OS 时仅返回 OS。
    #[test]
    fn parse_device_name_unknown_browser_with_os() {
        let ua = "SomeBot/1.0 (Windows NT 10.0)";
        assert_eq!(parse_device_name(ua).as_deref(), Some("Windows"));
    }

    /// parse_device_name 已知浏览器但无 OS 时仅返回浏览器名。
    #[test]
    fn parse_device_name_browser_without_os() {
        let ua = "Mozilla/5.0 Chrome/120.0";
        assert_eq!(parse_device_name(ua).as_deref(), Some("Chrome"));
    }

    /// parse_device_name 未知浏览器且无 OS 时返回 None。
    #[test]
    fn parse_device_name_unknown_no_browser_no_os() {
        let ua = "curl/8.0";
        assert!(parse_device_name(ua).is_none());
    }

    /// detect_browser 检测 Edge(Edg/ 优先于 Chrome/)。
    #[test]
    fn detect_browser_edge_priority() {
        assert_eq!(
            detect_browser("Edg/120 Chrome/120").as_deref(),
            Some("Edge")
        );
    }

    /// detect_browser 检测 Firefox。
    #[test]
    fn detect_browser_firefox() {
        assert_eq!(detect_browser("Firefox/121").as_deref(), Some("Firefox"));
    }

    /// detect_browser 检测 Chrome。
    #[test]
    fn detect_browser_chrome() {
        assert_eq!(detect_browser("Chrome/120").as_deref(), Some("Chrome"));
    }

    /// detect_browser 检测 Safari。
    #[test]
    fn detect_browser_safari() {
        assert_eq!(detect_browser("Safari/604").as_deref(), Some("Safari"));
    }

    /// detect_browser 检测 Opera(OPR/ 变体)。
    #[test]
    fn detect_browser_opera_opr() {
        assert_eq!(detect_browser("OPR/120").as_deref(), Some("Opera"));
    }

    /// detect_browser 检测 Opera(Opera/ 变体)。
    #[test]
    fn detect_browser_opera_opera() {
        assert_eq!(detect_browser("Opera/120").as_deref(), Some("Opera"));
    }

    /// detect_browser 未知浏览器返回 None。
    #[test]
    fn detect_browser_unknown_returns_none() {
        assert!(detect_browser("Bot/1.0").is_none());
    }

    /// detect_os 检测 Windows。
    #[test]
    fn detect_os_windows() {
        assert_eq!(detect_os("Windows NT 10.0").as_deref(), Some("Windows"));
    }

    /// detect_os 检测 iOS(iPhone)。
    #[test]
    fn detect_os_ios_iphone() {
        assert_eq!(detect_os("iPhone OS 17").as_deref(), Some("iOS"));
    }

    /// detect_os 检测 iOS(iPad)。
    #[test]
    fn detect_os_ios_ipad() {
        assert_eq!(detect_os("iPad CPU OS 17").as_deref(), Some("iOS"));
    }

    /// detect_os 检测 macOS。
    #[test]
    fn detect_os_macos() {
        assert_eq!(detect_os("Mac OS X 10_15").as_deref(), Some("macOS"));
    }

    /// detect_os 检测 macOS(Macintosh 变体)。
    #[test]
    fn detect_os_macos_macintosh() {
        assert_eq!(detect_os("Macintosh").as_deref(), Some("macOS"));
    }

    /// detect_os 检测 Android。
    #[test]
    fn detect_os_android() {
        assert_eq!(detect_os("Android 14").as_deref(), Some("Android"));
    }

    /// detect_os 检测 Linux。
    #[test]
    fn detect_os_linux() {
        assert_eq!(detect_os("Linux x86_64").as_deref(), Some("Linux"));
    }

    /// detect_os 未知 OS 返回 None。
    #[test]
    fn detect_os_unknown_returns_none() {
        assert!(detect_os("UnknownOS").is_none());
    }

    // DB 边界场景测试

    /// register_device 空 UA 字符串时 device_name 为 None,但仍能注册。
    #[tokio::test(flavor = "multi_thread")]
    async fn register_device_with_empty_ua() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool);

        let _device_id = repo
            .register_device(1, "login-empty-ua", "fp-empty", "")
            .await
            .expect("空 UA register 应成功");

        let devices = repo
            .list_user_devices(1, "login-empty-ua")
            .await
            .expect("list 应成功");
        assert_eq!(devices.len(), 1);
        assert!(
            devices[0].device_name.is_none(),
            "空 UA device_name 应为 None"
        );
        assert!(devices[0].user_agent.as_deref() == Some(""));
    }

    /// block_device / unblock_device 对不存在的 device_id 不报错(幂等)。
    #[tokio::test(flavor = "multi_thread")]
    async fn block_nonexistent_device_is_noop() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool);

        repo.block_device(1, "nonexistent-device")
            .await
            .expect("block 不存在的设备应为 no-op");
        repo.unblock_device(1, "nonexistent-device")
            .await
            .expect("unblock 不存在的设备应为 no-op");
    }

    /// 跨租户封禁必须 no-op(渗透-租户隔离-设备接口-1 回归):
    /// 租户 1 的管理员持租户 2 设备的 device_id 调 block_device,
    /// 0 行受影响——租户 2 设备状态不变;仅匹配租户时才生效。
    #[tokio::test(flavor = "multi_thread")]
    async fn block_device_cross_tenant_is_noop() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool);

        // 租户 1 注册设备
        let device_id = repo
            .register_device(1, "login-cross", "fp-cross", "Chrome/120.0")
            .await
            .expect("register 应成功");

        // 租户 2 的管理员按 device_id 封禁:0 行受影响(幂等 Ok,不报错)
        repo.block_device(2, &device_id)
            .await
            .expect("跨租户 block 应为 no-op 而非错误");
        let devices = repo
            .list_user_devices(1, "login-cross")
            .await
            .expect("list 应成功");
        assert!(
            !devices[0].is_blocked,
            "跨租户 block 不得改动他租户设备的 is_blocked"
        );

        repo.unblock_device(2, &device_id)
            .await
            .expect("跨租户 unblock 应为 no-op 而非错误");

        // 同租户封禁正常生效(对照)
        repo.block_device(1, &device_id)
            .await
            .expect("同租户 block 应生效");
        let devices = repo
            .list_user_devices(1, "login-cross")
            .await
            .expect("list 应成功");
        assert!(devices[0].is_blocked, "同租户 block 应置 is_blocked = 1");
    }

    /// list_user_devices 查询不存在的 login_id 应返回空列表。
    #[tokio::test(flavor = "multi_thread")]
    async fn list_user_devices_nonexistent_returns_empty() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool);

        let devices = repo
            .list_user_devices(1, "nonexistent-login")
            .await
            .expect("list 应成功");
        assert!(devices.is_empty(), "不存在的 login_id 应返回空列表");
    }

    /// register_device 不同 tenant_id 下相同 login_id 互不干扰。
    #[tokio::test(flavor = "multi_thread")]
    async fn register_device_isolates_by_tenant() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool);

        repo.register_device(1, "shared-login", "fp-1", "Chrome/120")
            .await
            .expect("tenant 1 register 应成功");
        repo.register_device(2, "shared-login", "fp-2", "Chrome/120")
            .await
            .expect("tenant 2 register 应成功");

        let count_1 = repo
            .count_user_devices(1, "shared-login")
            .await
            .expect("count tenant 1 应成功");
        let count_2 = repo
            .count_user_devices(2, "shared-login")
            .await
            .expect("count tenant 2 应成功");
        assert_eq!(count_1, 1, "tenant 1 应有 1 个设备");
        assert_eq!(count_2, 1, "tenant 2 应有 1 个设备");
    }

    // DROP TABLE 错误路径测试:覆盖 map_err 闭包

    /// 删除 app_user_device 表后 register_device 应返回 Dao 错误。
    #[tokio::test(flavor = "multi_thread")]
    async fn register_device_returns_error_when_table_dropped() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool.clone());
        {
            let session = pool.get_session("admin").await.expect("获取 session 失败");
            let conn = session.connection().expect("获取 connection 失败");
            conn.execute_unprepared("DROP TABLE IF EXISTS app_user_device")
                .await
                .expect("DROP TABLE 失败");
        }
        let result = repo.register_device(1, "u1", "fp1", "Chrome/120").await;
        assert!(result.is_err(), "表删除后 register_device 应返回错误");
    }

    /// 删除 app_user_device 表后 list_user_devices 应返回 Dao 错误。
    #[tokio::test(flavor = "multi_thread")]
    async fn list_user_devices_returns_error_when_table_dropped() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool.clone());
        {
            let session = pool.get_session("admin").await.expect("获取 session 失败");
            let conn = session.connection().expect("获取 connection 失败");
            conn.execute_unprepared("DROP TABLE IF EXISTS app_user_device")
                .await
                .expect("DROP TABLE 失败");
        }
        let result = repo.list_user_devices(1, "u1").await;
        assert!(result.is_err(), "表删除后 list_user_devices 应返回错误");
    }

    /// 删除 app_user_device 表后 count_user_devices 应返回 Dao 错误。
    #[tokio::test(flavor = "multi_thread")]
    async fn count_user_devices_returns_error_when_table_dropped() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool.clone());
        {
            let session = pool.get_session("admin").await.expect("获取 session 失败");
            let conn = session.connection().expect("获取 connection 失败");
            conn.execute_unprepared("DROP TABLE IF EXISTS app_user_device")
                .await
                .expect("DROP TABLE 失败");
        }
        let result = repo.count_user_devices(1, "u1").await;
        assert!(result.is_err(), "表删除后 count_user_devices 应返回错误");
    }

    /// 删除 app_user_device 表后 block_device 应返回 Dao 错误。
    #[tokio::test(flavor = "multi_thread")]
    async fn block_device_returns_error_when_table_dropped() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool.clone());
        {
            let session = pool.get_session("admin").await.expect("获取 session 失败");
            let conn = session.connection().expect("获取 connection 失败");
            conn.execute_unprepared("DROP TABLE IF EXISTS app_user_device")
                .await
                .expect("DROP TABLE 失败");
        }
        let result = repo.block_device(1, "some-id").await;
        assert!(result.is_err(), "表删除后 block_device 应返回错误");
    }

    /// 删除 app_user_device 表后 unblock_device 应返回 Dao 错误。
    #[tokio::test(flavor = "multi_thread")]
    async fn unblock_device_returns_error_when_table_dropped() {
        let pool = setup_db().await;
        let repo = DbnexusUserDeviceRepository::new(pool.clone());
        {
            let session = pool.get_session("admin").await.expect("获取 session 失败");
            let conn = session.connection().expect("获取 connection 失败");
            conn.execute_unprepared("DROP TABLE IF EXISTS app_user_device")
                .await
                .expect("DROP TABLE 失败");
        }
        let result = repo.unblock_device(1, "some-id").await;
        assert!(result.is_err(), "表删除后 unblock_device 应返回错误");
    }
}