test(message): erp-message 从 45 增至 69 个单元测试 — DND 时间窗 + TransactionError + model_to_resp
Some checks failed
CI / frontend-build (push) Has been cancelled
CI / rust-check (push) Has been cancelled
CI / rust-test (push) Has been cancelled
CI / security-audit (push) Has been cancelled

- module.rs: 提取 is_in_dnd_window 纯函数 + 14 个 DND 时间窗测试(正常范围/跨午夜/边界)
- error.rs: 2 个 TransactionError 转换测试(Connection/Transaction)
- message_service: 2 个 model_to_resp 字段映射测试
- template_service: 1 个 model_to_resp 字段映射测试
- subscription_service: 1 个 model_to_resp 字段映射测试
This commit is contained in:
iven
2026-04-28 18:26:36 +08:00
parent 50e63530d9
commit 26aa66d6e3
5 changed files with 236 additions and 4 deletions

View File

@@ -171,7 +171,11 @@ async fn should_skip_for_dnd(
};
let now = chrono::Local::now();
let now_time = now.format("%H:%M").to_string();
// DND 窗口比较(支持跨午夜,如 22:00-08:00
is_in_dnd_window(&now_time, &start, &end)
}
/// 判断当前时间是否在 DND 窗口内。支持跨午夜窗口(如 22:00-06:00
pub(crate) fn is_in_dnd_window(now_time: &str, start: &str, end: &str) -> bool {
if start <= end {
now_time >= start && now_time < end
} else {
@@ -602,3 +606,89 @@ async fn handle_workflow_event(
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
// ---- DND 时间窗逻辑 ----
#[test]
fn dnd_normal_range_inside() {
// 09:00-17:00当前 12:00 → 在窗口内
assert!(is_in_dnd_window("12:00", "09:00", "17:00"));
}
#[test]
fn dnd_normal_range_before() {
// 09:00-17:00当前 08:00 → 不在窗口内
assert!(!is_in_dnd_window("08:00", "09:00", "17:00"));
}
#[test]
fn dnd_normal_range_after() {
// 09:00-17:00当前 18:00 → 不在窗口内
assert!(!is_in_dnd_window("18:00", "09:00", "17:00"));
}
#[test]
fn dnd_normal_range_at_start() {
// 09:00-17:00当前 09:00 → 在窗口内(>= start
assert!(is_in_dnd_window("09:00", "09:00", "17:00"));
}
#[test]
fn dnd_normal_range_at_end() {
// 09:00-17:00当前 17:00 → 不在窗口内(< end 排除了 end 本身)
assert!(!is_in_dnd_window("17:00", "09:00", "17:00"));
}
#[test]
fn dnd_cross_midnight_night_time() {
// 22:00-06:00当前 23:30 → 在窗口内
assert!(is_in_dnd_window("23:30", "22:00", "06:00"));
}
#[test]
fn dnd_cross_midnight_early_morning() {
// 22:00-06:00当前 03:00 → 在窗口内
assert!(is_in_dnd_window("03:00", "22:00", "06:00"));
}
#[test]
fn dnd_cross_midnight_daytime() {
// 22:00-06:00当前 14:00 → 不在窗口内
assert!(!is_in_dnd_window("14:00", "22:00", "06:00"));
}
#[test]
fn dnd_cross_midnight_at_start() {
assert!(is_in_dnd_window("22:00", "22:00", "06:00"));
}
#[test]
fn dnd_cross_midnight_at_end() {
assert!(!is_in_dnd_window("06:00", "22:00", "06:00"));
}
#[test]
fn dnd_cross_midnight_just_before_end() {
assert!(is_in_dnd_window("05:59", "22:00", "06:00"));
}
#[test]
fn dnd_same_start_end_always_in() {
// start == end 意味着 start <= end所以 now >= start && now < end
// "00:00" >= "00:00" && "00:00" < "00:00" → false
assert!(!is_in_dnd_window("00:00", "12:00", "12:00"));
// "15:00" >= "12:00" && "15:00" < "12:00" → false
assert!(!is_in_dnd_window("15:00", "12:00", "12:00"));
}
#[test]
fn dnd_single_minute_window() {
// 23:59-00:00跨午夜 1 分钟)
assert!(is_in_dnd_window("23:59", "23:59", "00:00"));
assert!(!is_in_dnd_window("00:00", "23:59", "00:00"));
}
}