refactor: update dependencies and remove unused code
- Added new dependencies: `adler2`, `crc32fast`, `flate2`, `miniz_oxide`, and `libredox`. - Updated existing dependencies: `tokio-rustls` to version 0.26.4 and `filetime` to version 0.2.27. - Removed the `backup.rs` file as it is no longer needed. - Introduced tests for configuration and credential management. - Enhanced the `identity` module to generate W3C compliant DID documents. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
fd2a837bea
commit
f07ce10b1a
@@ -112,3 +112,167 @@ pub async fn get_networking_profits(data_dir: &Path) -> Result<ProfitsSummary> {
|
||||
|
||||
Ok(summary)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tempfile::TempDir;
|
||||
|
||||
#[test]
|
||||
fn test_profits_summary_default() {
|
||||
let summary = ProfitsSummary::default();
|
||||
assert_eq!(summary.total_sats, 0);
|
||||
assert_eq!(summary.content_sales_sats, 0);
|
||||
assert_eq!(summary.routing_fees_sats, 0);
|
||||
assert!(summary.recent.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_load_profits_returns_default_when_missing() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let summary = load_profits(tmp.path()).await.unwrap();
|
||||
assert_eq!(summary.total_sats, 0);
|
||||
assert_eq!(summary.content_sales_sats, 0);
|
||||
assert_eq!(summary.routing_fees_sats, 0);
|
||||
assert!(summary.recent.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_save_and_load_profits_roundtrip() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let summary = ProfitsSummary {
|
||||
total_sats: 5000,
|
||||
content_sales_sats: 3000,
|
||||
routing_fees_sats: 2000,
|
||||
recent: vec![ProfitEntry {
|
||||
source: ProfitSource::ContentSale,
|
||||
amount_sats: 3000,
|
||||
timestamp: "2025-06-01T00:00:00Z".to_string(),
|
||||
description: "Test sale".to_string(),
|
||||
}],
|
||||
};
|
||||
|
||||
save_profits(tmp.path(), &summary).await.unwrap();
|
||||
let loaded = load_profits(tmp.path()).await.unwrap();
|
||||
assert_eq!(loaded.total_sats, 5000);
|
||||
assert_eq!(loaded.content_sales_sats, 3000);
|
||||
assert_eq!(loaded.routing_fees_sats, 2000);
|
||||
assert_eq!(loaded.recent.len(), 1);
|
||||
assert_eq!(loaded.recent[0].amount_sats, 3000);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_save_profits_creates_wallet_dir() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let wallet_dir = tmp.path().join("wallet");
|
||||
assert!(!wallet_dir.exists());
|
||||
|
||||
save_profits(tmp.path(), &ProfitsSummary::default()).await.unwrap();
|
||||
assert!(wallet_dir.exists());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_record_content_sale() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
record_content_sale(tmp.path(), 500, "First sale").await.unwrap();
|
||||
|
||||
let summary = load_profits(tmp.path()).await.unwrap();
|
||||
assert_eq!(summary.total_sats, 500);
|
||||
assert_eq!(summary.content_sales_sats, 500);
|
||||
assert_eq!(summary.routing_fees_sats, 0);
|
||||
assert_eq!(summary.recent.len(), 1);
|
||||
assert_eq!(summary.recent[0].amount_sats, 500);
|
||||
assert_eq!(summary.recent[0].description, "First sale");
|
||||
assert!(matches!(summary.recent[0].source, ProfitSource::ContentSale));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_record_multiple_content_sales() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
record_content_sale(tmp.path(), 100, "Sale 1").await.unwrap();
|
||||
record_content_sale(tmp.path(), 200, "Sale 2").await.unwrap();
|
||||
record_content_sale(tmp.path(), 300, "Sale 3").await.unwrap();
|
||||
|
||||
let summary = load_profits(tmp.path()).await.unwrap();
|
||||
assert_eq!(summary.total_sats, 600);
|
||||
assert_eq!(summary.content_sales_sats, 600);
|
||||
assert_eq!(summary.recent.len(), 3);
|
||||
// Newest first (inserted at index 0)
|
||||
assert_eq!(summary.recent[0].description, "Sale 3");
|
||||
assert_eq!(summary.recent[1].description, "Sale 2");
|
||||
assert_eq!(summary.recent[2].description, "Sale 1");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_record_content_sale_truncates_at_100() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
for i in 0..110 {
|
||||
record_content_sale(tmp.path(), 1, &format!("Sale {}", i))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let summary = load_profits(tmp.path()).await.unwrap();
|
||||
assert_eq!(summary.recent.len(), 100);
|
||||
assert_eq!(summary.total_sats, 110);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_networking_profits_empty() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let summary = get_networking_profits(tmp.path()).await.unwrap();
|
||||
assert_eq!(summary.total_sats, 0);
|
||||
assert_eq!(summary.content_sales_sats, 0);
|
||||
assert_eq!(summary.routing_fees_sats, 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_networking_profits_includes_ecash_receives() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
|
||||
// Simulate receiving ecash tokens
|
||||
ecash::receive_token(tmp.path(), "cashuSend_500_uuid1_1700000000")
|
||||
.await
|
||||
.unwrap();
|
||||
ecash::receive_token(tmp.path(), "cashuSend_300_uuid2_1700000001")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let summary = get_networking_profits(tmp.path()).await.unwrap();
|
||||
// ecash receives (800) should be reflected as content sales
|
||||
assert_eq!(summary.content_sales_sats, 800);
|
||||
assert_eq!(summary.total_sats, 800);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_networking_profits_uses_higher_of_tracked_or_ecash() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
|
||||
// Record a larger tracked profit
|
||||
record_content_sale(tmp.path(), 2000, "Big sale").await.unwrap();
|
||||
// Receive a smaller ecash amount
|
||||
ecash::receive_token(tmp.path(), "cashuSend_100_uuid_170")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let summary = get_networking_profits(tmp.path()).await.unwrap();
|
||||
// Should use tracked (2000) since it's larger than ecash receives (100)
|
||||
assert_eq!(summary.content_sales_sats, 2000);
|
||||
assert_eq!(summary.total_sats, 2000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_profit_source_serialization() {
|
||||
let entry = ProfitEntry {
|
||||
source: ProfitSource::RoutingFee,
|
||||
amount_sats: 42,
|
||||
timestamp: "2025-01-01T00:00:00Z".to_string(),
|
||||
description: "routing".to_string(),
|
||||
};
|
||||
let json = serde_json::to_string(&entry).unwrap();
|
||||
assert!(json.contains("\"routing_fee\""));
|
||||
|
||||
let parsed: ProfitEntry = serde_json::from_str(&json).unwrap();
|
||||
assert!(matches!(parsed.source, ProfitSource::RoutingFee));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user