use crate::FileId;
use chrono::Utc;
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
pub struct FileInfo {
pub id: FileId,
pub path: PathBuf,
pub hash: String,
pub last_indexed_utc: u64,
pub mtime: u64,
}
impl FileInfo {
pub fn new(id: FileId, path: PathBuf, content: &str) -> Self {
let mtime = get_file_mtime(&path).unwrap_or(0);
Self {
id,
path,
hash: calculate_hash(content),
last_indexed_utc: get_utc_timestamp(),
mtime,
}
}
pub fn has_changed(&self, content: &str) -> bool {
self.hash != calculate_hash(content)
}
}
pub fn get_file_mtime(path: &Path) -> Option<u64> {
std::fs::metadata(path)
.ok()
.and_then(|m| m.modified().ok())
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
.map(|d| d.as_secs())
}
pub fn calculate_hash(content: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(content.as_bytes());
format!("{:x}", hasher.finalize())
}
pub fn get_utc_timestamp() -> u64 {
Utc::now().timestamp() as u64
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hash_calculation() {
let content1 = "Hello, World!";
let content2 = "Hello, World!";
let content3 = "Hello, world!";
let hash1 = calculate_hash(content1);
let hash2 = calculate_hash(content2);
let hash3 = calculate_hash(content3);
assert_eq!(hash1, hash2);
assert_ne!(hash1, hash3);
assert_eq!(hash1.len(), 64);
}
#[test]
fn test_utc_timestamp() {
let ts1 = get_utc_timestamp();
std::thread::sleep(std::time::Duration::from_millis(10));
let ts2 = get_utc_timestamp();
assert!(ts2 >= ts1);
assert!(ts1 > 1577836800); }
#[test]
fn test_file_info_change_detection() {
let file_id = FileId::new(1).unwrap();
let path = PathBuf::from("test.rs");
let content = "fn main() {}";
let info = FileInfo::new(file_id, path, content);
assert!(!info.has_changed(content));
assert!(info.has_changed("fn main() { println!(\"Hello\"); }"));
}
#[test]
fn test_hot_reload_trigger() {
let test_content = "Testing hot reload functionality";
let hash = calculate_hash(test_content);
assert_eq!(hash.len(), 64);
let timestamp = get_utc_timestamp();
assert!(timestamp > 1700000000); }
#[test]
fn test_https_server_hot_reload() {
let timestamp = get_utc_timestamp();
assert!(timestamp > 0);
let hash1 = calculate_hash("test");
let hash2 = calculate_hash("test");
assert_eq!(hash1, hash2);
}
#[test]
fn test_hot_reload_detection() {
let test_data = "Hot reload cache test";
let hash = calculate_hash(test_data);
assert_eq!(hash.len(), 64);
}
#[test]
fn test_stdio_server_refresh() {
let timestamp = get_utc_timestamp();
assert!(timestamp > 0);
println!("Stdio server refresh test executed at {timestamp}");
}
#[test]
fn test_quantum_flux_embedding_validation() {
let timestamp = get_utc_timestamp();
assert!(timestamp > 0);
println!("Quantum flux embedding validation test executed at {timestamp}");
}
#[test]
fn test_stdio_server_refresh_v4() {
let data = "Testing stdio server refresh v3 with quantum flux";
let hash = calculate_hash(data);
assert_eq!(hash.len(), 64);
println!("Stdio refresh v3 test with quantum hash: {}", &hash[..8]);
}
}