Files
Genarrative/server-rs/crates/module-runtime/tests/invite_code_validity.rs
2026-05-11 16:15:48 +08:00

95 lines
3.3 KiB
Rust

use module_runtime::{
RuntimeProfileFieldError, RuntimeProfileInviteCodeSnapshot, RuntimeProfileInviteCodeStatus,
build_runtime_profile_invite_code_record, normalize_invite_code_metadata_json,
resolve_runtime_profile_invite_code_status,
validate_runtime_profile_invite_code_validity_window,
};
#[test]
fn invite_code_validity_window_rejects_start_after_expire() {
let result = validate_runtime_profile_invite_code_validity_window(Some(20), Some(10));
assert_eq!(
result,
Err(RuntimeProfileFieldError::InvalidInviteCodeValidityWindow)
);
}
#[test]
fn invite_code_validity_window_allows_open_ended_and_rejects_equal_boundary() {
assert!(validate_runtime_profile_invite_code_validity_window(None, None).is_ok());
assert!(validate_runtime_profile_invite_code_validity_window(Some(10), None).is_ok());
assert!(validate_runtime_profile_invite_code_validity_window(None, Some(10)).is_ok());
assert_eq!(
validate_runtime_profile_invite_code_validity_window(Some(10), Some(10)),
Err(RuntimeProfileFieldError::InvalidInviteCodeValidityWindow)
);
}
#[test]
fn invite_code_status_uses_inclusive_start_and_exclusive_expire_boundary() {
assert_eq!(
resolve_runtime_profile_invite_code_status(Some(20), None, 19),
RuntimeProfileInviteCodeStatus::Pending
);
assert_eq!(
resolve_runtime_profile_invite_code_status(Some(20), Some(30), 20),
RuntimeProfileInviteCodeStatus::Active
);
assert_eq!(
resolve_runtime_profile_invite_code_status(Some(20), Some(30), 29),
RuntimeProfileInviteCodeStatus::Active
);
assert_eq!(
resolve_runtime_profile_invite_code_status(Some(20), Some(30), 30),
RuntimeProfileInviteCodeStatus::Expired
);
}
#[test]
fn invite_code_record_formats_window_and_status() {
let record = build_runtime_profile_invite_code_record(RuntimeProfileInviteCodeSnapshot {
user_id: "user-1".to_string(),
invite_code: "SY00000001".to_string(),
metadata_json: "{}".to_string(),
starts_at_micros: Some(0),
expires_at_micros: Some(1_000_000),
created_at_micros: 0,
updated_at_micros: 1_000_000,
});
assert_eq!(record.starts_at.as_deref(), Some("1970-01-01T00:00:00Z"));
assert_eq!(record.expires_at.as_deref(), Some("1970-01-01T00:00:01Z"));
assert_eq!(record.status, RuntimeProfileInviteCodeStatus::Expired);
}
#[test]
fn invite_code_metadata_normalizes_user_tags() {
let normalized = normalize_invite_code_metadata_json(
r#"{"source":"admin","user_tags":[" 北科 ","北科",""]}"#.to_string(),
)
.expect("metadata should normalize");
assert_eq!(normalized, r#"{"source":"admin","userTags":["北科"]}"#);
}
#[test]
fn invite_code_metadata_removes_empty_user_tags() {
let normalized = normalize_invite_code_metadata_json(r#"{"userTags":[]}"#.to_string())
.expect("empty tags should be valid");
assert_eq!(normalized, "{}");
}
#[test]
fn invite_code_metadata_rejects_invalid_user_tags_shape() {
assert_eq!(
normalize_invite_code_metadata_json(r#"{"userTags":"北科"}"#.to_string()),
Err(RuntimeProfileFieldError::InvalidUserTag)
);
assert_eq!(
normalize_invite_code_metadata_json(r#"{"userTags":["北科",1]}"#.to_string()),
Err(RuntimeProfileFieldError::InvalidUserTag)
);
}