use rvd::auth::qrcode::QRCodeDisplay;
use std::fs;
use tempfile::tempdir;
#[test]
fn test_qrcode_save_to_file() {
let url = "https://www.bilibili.com/";
let dir = tempdir().unwrap();
let file_path = dir.path().join("test_qrcode.png");
let result = QRCodeDisplay::save_to_file(url, &file_path);
assert!(result.is_ok(), "Failed to save QR code: {:?}", result.err());
assert!(file_path.exists(), "QR code file was not created");
let metadata = fs::metadata(&file_path).unwrap();
assert!(metadata.len() > 0, "QR code file is empty");
assert!(
metadata.len() > 1024,
"QR code file is too small: {} bytes",
metadata.len()
);
}
#[test]
fn test_qrcode_save_with_long_url() {
let url = "https://passport.bilibili.com/x/passport-login/web/qrcode/poll?qrcode_key=1234567890abcdef";
let dir = tempdir().unwrap();
let file_path = dir.path().join("test_long_qrcode.png");
let result = QRCodeDisplay::save_to_file(url, &file_path);
assert!(result.is_ok(), "Failed to save QR code with long URL");
assert!(file_path.exists());
}
#[test]
fn test_qrcode_invalid_url() {
let url = "";
let dir = tempdir().unwrap();
let file_path = dir.path().join("test_empty_qrcode.png");
let result = QRCodeDisplay::save_to_file(url, &file_path);
assert!(result.is_ok());
}
#[test]
fn test_qrcode_display_in_terminal() {
let url = "https://www.bilibili.com/";
let result = QRCodeDisplay::display_in_terminal(url);
match result {
Ok(_) => {
}
Err(e) => {
println!("Terminal display failed (expected in test env): {}", e);
}
}
}
#[test]
fn test_qrcode_multiple_saves() {
let url = "https://www.bilibili.com/";
let dir = tempdir().unwrap();
let file_path = dir.path().join("test_multiple_qrcode.png");
let result1 = QRCodeDisplay::save_to_file(url, &file_path);
assert!(result1.is_ok());
let size1 = fs::metadata(&file_path).unwrap().len();
let result2 = QRCodeDisplay::save_to_file(url, &file_path);
assert!(result2.is_ok());
let size2 = fs::metadata(&file_path).unwrap().len();
assert_eq!(size1, size2);
}
#[test]
fn test_qrcode_different_urls_different_sizes() {
let dir = tempdir().unwrap();
let short_url = "https://b23.tv/";
let short_path = dir.path().join("short_qrcode.png");
QRCodeDisplay::save_to_file(short_url, &short_path).unwrap();
let short_size = fs::metadata(&short_path).unwrap().len();
let long_url = "https://passport.bilibili.com/x/passport-login/web/qrcode/poll?qrcode_key=1234567890abcdef1234567890abcdef";
let long_path = dir.path().join("long_qrcode.png");
QRCodeDisplay::save_to_file(long_url, &long_path).unwrap();
let long_size = fs::metadata(&long_path).unwrap().len();
assert!(
long_size > short_size,
"Long URL QR code should be larger than short URL QR code"
);
}