Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/shm11C3/hardware-monitor
Browse files Browse the repository at this point in the history
…into develop
  • Loading branch information
shm11C3 committed Dec 21, 2024
2 parents 7160af3 + 1911f76 commit f541faa
Show file tree
Hide file tree
Showing 19 changed files with 407 additions and 26 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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 -- --test-threads=1 --nocapture

- uses: tauri-apps/tauri-action@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@ features = [
[lib]
name = "hardware_monitor_lib"
crate-type = ["staticlib", "cdylib", "rlib"]

[dev-dependencies]
tauri = { version = "2.1.1", features = ["test"] }
30 changes: 15 additions & 15 deletions src-tauri/src/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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>;
}
Expand All @@ -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<hardware::HardwareType>,
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<String>,
pub version: String,
pub language: String,
pub theme: enums::config::Theme,
pub display_targets: Vec<hardware::HardwareType>,
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<String>,
}

///
Expand Down
3 changes: 1 addition & 2 deletions src-tauri/src/commands/hardware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Mutex<System>>,
Expand Down
4 changes: 3 additions & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ mod services;
mod structs;
mod utils;

#[cfg(test)]
mod tests;

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;
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/services/directx_gpu_service.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/services/nvidia_gpu_service.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/services/wmi_service.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 1 addition & 4 deletions src-tauri/src/structs/hardware.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
enums::{self, hardware::DiskKind},
utils::formatter::SizeUnit,
};
use crate::{enums::hardware::DiskKind, utils::formatter::SizeUnit};
use serde::{Deserialize, Serialize};
use specta::Type;

Expand Down
28 changes: 28 additions & 0 deletions src-tauri/src/tests/commands/background_image.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}
151 changes: 151 additions & 0 deletions src-tauri/src/tests/commands/config.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
55 changes: 55 additions & 0 deletions src-tauri/src/tests/commands/hardware.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
6 changes: 6 additions & 0 deletions src-tauri/src/tests/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[cfg(test)]
pub mod background_image;
#[cfg(test)]
pub mod config;
//#[cfg(test)]
//pub mod hardware;
4 changes: 4 additions & 0 deletions src-tauri/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[cfg(test)]
pub mod commands;
#[cfg(test)]
pub mod utils;
Loading

0 comments on commit f541faa

Please sign in to comment.