Skip to content

Commit cc965b0

Browse files
committed
test(coverage): channels providers (+0.9%) — qq ensure_https, lark parsers
- qq: ensure_https accept/reject, QQ_API_BASE/AUTH_URL constants, constructor. - lark: parse_post_content zh_cn/en_us fallback + links/mentions; invalid JSON returns None; strip_at_placeholders for @_user_N tokens; group should_respond_in_group mention gating.
1 parent 7a0a774 commit cc965b0

2 files changed

Lines changed: 119 additions & 0 deletions

File tree

‎src/openhuman/channels/providers/lark.rs‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,4 +1268,94 @@ mod tests {
12681268
assert_eq!(msgs.len(), 1);
12691269
assert_eq!(msgs[0].sender, "ou_user");
12701270
}
1271+
1272+
// ── parse_post_content ─────────────────────────────────────────
1273+
1274+
#[test]
1275+
fn parse_post_content_returns_zh_cn_locale_content() {
1276+
let post = serde_json::json!({
1277+
"zh_cn": {
1278+
"title": "标题",
1279+
"content": [[{"tag": "text", "text": "你好"}]]
1280+
}
1281+
})
1282+
.to_string();
1283+
let out = parse_post_content(&post).expect("parsed");
1284+
assert!(out.contains("标题"));
1285+
assert!(out.contains("你好"));
1286+
}
1287+
1288+
#[test]
1289+
fn parse_post_content_falls_back_to_en_us_when_zh_cn_missing() {
1290+
let post = serde_json::json!({
1291+
"en_us": {
1292+
"title": "Hello",
1293+
"content": [[{"tag": "text", "text": "world"}]]
1294+
}
1295+
})
1296+
.to_string();
1297+
let out = parse_post_content(&post).expect("parsed");
1298+
assert!(out.contains("Hello"));
1299+
assert!(out.contains("world"));
1300+
}
1301+
1302+
#[test]
1303+
fn parse_post_content_returns_none_for_invalid_json() {
1304+
assert!(parse_post_content("not json").is_none());
1305+
}
1306+
1307+
#[test]
1308+
fn parse_post_content_handles_links_and_mentions() {
1309+
let post = serde_json::json!({
1310+
"zh_cn": {
1311+
"title": "T",
1312+
"content": [[
1313+
{"tag": "text", "text": "pre "},
1314+
{"tag": "a", "text": "link", "href": "https://x"},
1315+
{"tag": "at", "user_name": "alice"}
1316+
]]
1317+
}
1318+
})
1319+
.to_string();
1320+
let out = parse_post_content(&post).expect("parsed");
1321+
assert!(out.contains("link"));
1322+
assert!(out.contains("@alice"));
1323+
}
1324+
1325+
#[test]
1326+
fn parse_post_content_returns_none_when_all_sections_empty() {
1327+
let post = serde_json::json!({ "zh_cn": { "title": "" } }).to_string();
1328+
assert!(parse_post_content(&post).is_none());
1329+
}
1330+
1331+
// ── strip_at_placeholders ──────────────────────────────────────
1332+
1333+
#[test]
1334+
fn strip_at_placeholders_removes_user_tokens() {
1335+
assert_eq!(strip_at_placeholders("hello @_user_1 world"), "hello world");
1336+
assert_eq!(
1337+
strip_at_placeholders("@_user_42 message here"),
1338+
"message here"
1339+
);
1340+
}
1341+
1342+
#[test]
1343+
fn strip_at_placeholders_preserves_real_at_mentions() {
1344+
assert_eq!(strip_at_placeholders("hello @alice"), "hello @alice");
1345+
}
1346+
1347+
#[test]
1348+
fn strip_at_placeholders_handles_multiple_placeholders() {
1349+
assert_eq!(strip_at_placeholders("@_user_1 hi @_user_2 bye"), "hi bye");
1350+
}
1351+
1352+
// ── should_respond_in_group ────────────────────────────────────
1353+
1354+
#[test]
1355+
fn should_respond_in_group_requires_nonempty_mentions() {
1356+
assert!(!should_respond_in_group(&[]));
1357+
assert!(should_respond_in_group(&[
1358+
serde_json::json!({"key": "val"})
1359+
]));
1360+
}
12711361
}

‎src/openhuman/channels/providers/qq.rs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,4 +503,33 @@ allowed_users = ["user1"]
503503
assert_eq!(config.app_secret, "secret_abc");
504504
assert_eq!(config.allowed_users, vec!["user1"]);
505505
}
506+
507+
#[test]
508+
fn ensure_https_accepts_https_urls() {
509+
assert!(ensure_https("https://api.example.com").is_ok());
510+
assert!(ensure_https("https://api.sgroup.qq.com/v1").is_ok());
511+
}
512+
513+
#[test]
514+
fn ensure_https_rejects_http_and_other_schemes() {
515+
assert!(ensure_https("http://example.com").is_err());
516+
assert!(ensure_https("ws://example.com").is_err());
517+
assert!(ensure_https("ftp://example.com").is_err());
518+
assert!(ensure_https("").is_err());
519+
assert!(ensure_https("example.com").is_err());
520+
}
521+
522+
#[test]
523+
fn api_base_and_auth_url_are_https_constants() {
524+
assert!(QQ_API_BASE.starts_with("https://"));
525+
assert!(QQ_AUTH_URL.starts_with("https://"));
526+
}
527+
528+
#[test]
529+
fn new_constructor_stores_fields() {
530+
let ch = QQChannel::new("a".into(), "b".into(), vec!["u1".into()]);
531+
assert_eq!(ch.app_id, "a");
532+
assert_eq!(ch.app_secret, "b");
533+
assert_eq!(ch.allowed_users, vec!["u1".to_string()]);
534+
}
506535
}

0 commit comments

Comments
 (0)