use crate::gui::experimental_kinetics_gui::model::PlotModel;
use crate::Kinetics::experimental_kinetics::one_experiment_dataset::{ColumnNature, Unit};
use std::path::{Path, PathBuf};
fn test_data_path() -> PathBuf {
let executable_path = std::env::current_exe().ok();
let executable_dir = executable_path.as_deref().and_then(Path::parent);
let packaged_path = executable_dir.map(|dir| dir.join("TGAexample.txt"));
let development_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("TGAexample.txt");
packaged_path
.filter(|path| path.is_file())
.unwrap_or(development_path)
}
pub fn load_model_with_one_curve() -> PlotModel {
try_load_model_with_one_curve().expect("failed to load test data")
}
pub fn try_load_model_with_one_curve() -> Result<PlotModel, String> {
let mut model = PlotModel::new();
model
.push_from_file(&test_data_path())
.map_err(|error| error.to_string())?;
let experiments = model.list_of_experiments();
let exp_id = experiments
.first()
.cloned()
.ok_or_else(|| "No experiments loaded from TGAexample.txt".to_string())?;
model
.bind_time_of_experiment(&exp_id, "t", Unit::Second)
.map_err(|error| error.to_string())?;
model
.bind_temperature_of_experiment(&exp_id, "T", Unit::Celsius)
.map_err(|error| error.to_string())?;
model
.bind_mass_of_experiment(&exp_id, "m", Unit::MilliVolt)
.map_err(|error| error.to_string())?;
model
.set_x(&exp_id, "t")
.map_err(|error| error.to_string())?;
model
.set_y(&exp_id, "T")
.map_err(|error| error.to_string())?;
model
.create_points_for_curve(&exp_id)
.map_err(|error| error.to_string())?;
Ok(model)
}
pub fn load_model_with_two_curves() -> PlotModel {
let mut model = load_model_with_one_curve();
let exp_id = model.list_of_experiments()[0].clone();
model.set_x(&exp_id, "t").expect("failed to keep x column");
model
.set_y(&exp_id, "m")
.expect("failed to set second y column");
model
.create_points_for_curve(&exp_id)
.expect("failed to create second curve");
model
}
#[cfg(test)]
fn load_model_with_recorded_gap() -> PlotModel {
let observed_time = vec![0.0, 1.0, 2.0, 3.0, 7.0, 8.0, 9.0, 10.0];
let mut model = PlotModel::new();
model
.series
.create_from_synthetic_data(
&crate::Kinetics::experimental_kinetics::testing_mod::VirtualTGA {
time: observed_time.clone(),
temperature: observed_time.iter().map(|t| 300.0 + 4.0 * t).collect(),
mass: observed_time.iter().map(|t| 12.0 - 0.5 * t).collect(),
},
crate::Kinetics::experimental_kinetics::experiment_series_main::ExperimentMeta {
id: "gap_workflow".to_string(),
..Default::default()
},
)
.unwrap();
model.set_x("gap_workflow", "time").unwrap();
model.set_y("gap_workflow", "temperature").unwrap();
model.create_points_for_curve("gap_workflow").unwrap();
model
}
#[cfg(test)]
mod tests {
use crate::gui::experimental_kinetics_gui::controller_kinetics::DirectProblemDialogState;
use crate::gui::experimental_kinetics_gui::controller_methods::KineticMethodsWindowState;
use crate::gui::experimental_kinetics_gui::controller_table::ColumnManagerState;
use crate::gui::experimental_kinetics_gui::interaction::SelectionRect;
use crate::gui::experimental_kinetics_gui::test_options::TestOptions;
use super::*;
use crate::Kinetics::experimental_kinetics::exp_engine_api::XY;
use crate::Kinetics::experimental_kinetics::kinetic_methods::KineticMethod;
use crate::Kinetics::experimental_kinetics::kinetic_methods::isoconversion::{
IsoconversionalKineticMethod, IsoconversionalMethod,
};
use crate::Kinetics::experimental_kinetics::math::fitting::FittingModelName;
use crate::Kinetics::experimental_kinetics::testing_mod::{
AdvancedTGAConfig, ExperimentMode, KineticModel, NoiseConfig, NoiseKind,
};
use crate::Kinetics::solid_state_kinetics_IVP::KineticModelNames;
use crate::gui::experimental_kinetics_gui::experimental_kinetics_gui_main::PlotApp;
use egui::{Key, Modifiers};
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
use std::fs;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};
impl PlotApp {
pub fn for_testing(model: PlotModel) -> Self {
use super::super::controller_buttons_and_panels::{
NewExperimentDialogState, QuickActionPanelState,
};
use super::super::controller_filters::Mathematics;
use super::super::kitheplot_wrapper::KiThePlotWindowState;
Self {
model,
quick_actions_state: QuickActionPanelState::default(),
new_experiment_dialog: NewExperimentDialogState::new(),
mathematics: Mathematics::new(),
test_options: TestOptions::new(),
column_manager_state: ColumnManagerState::new(),
kithe_plot_window: KiThePlotWindowState::default(),
kinetic_methods_state: KineticMethodsWindowState::default(),
direct_problem_state: DirectProblemDialogState::new(),
keyboard_input_focused_last_frame: false,
}
}
}
fn with_plot_app_harness(mut app: PlotApp, mut run_test: impl FnMut(&mut Harness)) {
let mut open = true;
let mut harness = Harness::new_ui(move |ui| {
app.show(ui.ctx(), &mut open);
});
run_test(&mut harness);
}
struct PlotAppTestState {
app: PlotApp,
open: bool,
}
fn with_plot_app_state_harness(
app: PlotApp,
mut run_test: impl FnMut(&mut Harness<PlotAppTestState>),
) {
let state = PlotAppTestState { app, open: true };
let mut harness = Harness::new_ui_state(
|ui, state: &mut PlotAppTestState| {
state.app.show(ui.ctx(), &mut state.open);
},
state,
);
run_test(&mut harness);
}
fn click_menu_entry<State>(harness: &mut Harness<State>, menu_label: &str, entry_label: &str) {
harness.get_by_label(menu_label).click();
harness.run();
harness.get_by_label(entry_label).click();
harness.run();
}
fn queue_open_path(harness: &mut Harness<PlotAppTestState>, path: impl Into<PathBuf>) {
harness
.state_mut()
.app
.new_experiment_dialog
.test_next_open_file = Some(path.into());
}
fn queue_save_path(harness: &mut Harness<PlotAppTestState>, path: impl Into<PathBuf>) {
harness
.state_mut()
.app
.new_experiment_dialog
.test_next_save_file = Some(path.into());
}
fn pending_fit_batch(
experiment_id: &str,
) -> crate::Kinetics::experimental_kinetics::math::fitting::FittedEvaluationBatch {
use crate::Kinetics::experimental_kinetics::math::fitting::{
FitEvaluationMode, FittedEvaluationBatch,
};
use crate::Kinetics::experimental_kinetics::one_experiment_dataset::{ColumnNature, Unit};
FittedEvaluationBatch {
source_experiment_id: experiment_id.to_string(),
source_history_len: 0,
x_col: "t".to_string(),
x_unit: Unit::Second,
x_nature: ColumnNature::Time,
mode: FitEvaluationMode::Extrapolate {
start: 10.0,
end: 20.0,
points: 2,
},
evaluation_x: vec![10.0, 20.0],
source_x: vec![],
x_values: vec![10.0, 20.0],
columns: vec![],
}
}
fn assert_filter_output_via_ui(menu_item: &str, output_column: &str) {
let mut model = load_model_with_one_curve();
model.select_curve(0);
let experiment_id = model.list_of_experiments()[0].clone();
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "Math", menu_item);
harness.get_by_label("Apply").click();
harness.run();
let model = &harness.state().app.model;
assert!(
model
.list_of_columns(&experiment_id)
.unwrap()
.contains(&output_column.to_string()),
"{menu_item} did not create {output_column}; message={}",
model.message
);
assert!(model.plots.iter().any(|curve| {
curve.experiment_id == experiment_id && curve.y_name == output_column
}));
});
}
fn assert_menu_entry_clickable(menu_label: &str, entry_label: &str) {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_harness(app, |harness| {
harness.run();
click_menu_entry(harness, menu_label, entry_label);
});
}
fn unique_temp_dir(prefix: &str) -> PathBuf {
let stamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("system clock before UNIX_EPOCH")
.as_nanos();
std::env::temp_dir().join(format!("kithe_{prefix}_{stamp}"))
}
fn export_column_label(model: &PlotModel, experiment_id: &str, column: &str) -> String {
let experiment = model.series.get_experiment_by_id(experiment_id).unwrap();
let metadata = &experiment.dataset.schema.columns[column];
let nature = match metadata.nature {
crate::Kinetics::experimental_kinetics::one_experiment_dataset::ColumnNature::Time => {
"Time"
}
crate::Kinetics::experimental_kinetics::one_experiment_dataset::ColumnNature::Mass => {
"Mass"
}
_ => "Unclassified",
};
let active_binding = if experiment.dataset.schema.time.as_deref() == Some(column) {
Some("time")
} else if experiment.dataset.schema.mass.as_deref() == Some(column) {
Some("mass")
} else if experiment.dataset.schema.temperature.as_deref() == Some(column) {
Some("temperature")
} else {
None
};
let binding = active_binding
.map(|role| format!(" · active: {role}"))
.unwrap_or_default();
format!(
"{experiment_id}::{column} · {nature} · {}{binding}",
metadata.unit
)
}
fn non_isothermal_series_config() -> AdvancedTGAConfig {
AdvancedTGAConfig {
n_points: 180,
dt: 0.5,
experiment_mode: ExperimentMode::NonIsothermal {
t0: 300.0,
heating_rates: vec![2.0, 5.0, 10.0],
},
kinetic_model: KineticModel::ArrheniusSingle {
m0: 100.0,
k0: 2.5e5,
e: 85000.0,
r: 8.314,
},
mass_noise: Some(NoiseConfig {
kind: NoiseKind::Gaussian { sigma: 0.0 },
}),
temp_noise: Some(NoiseConfig {
kind: NoiseKind::Gaussian { sigma: 0.0 },
}),
spikes: None,
seed: 42,
}
}
fn last_value_for(model: &PlotModel, id: &str, column: &str) -> f64 {
let exp = model
.series
.get_experiment_by_id(id)
.expect("experiment not found");
let df = exp
.dataset
.frame
.clone()
.collect()
.expect("failed to collect frame");
df.column(column)
.expect("requested column missing")
.f64()
.expect("requested column is not f64")
.into_no_null_iter()
.last()
.expect("requested column is empty")
}
#[test]
fn tier1_smoke_renders_core_widgets() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_harness(app, |harness| {
harness.run();
harness.get_by_label("Data Plot");
harness.get_by_label("TGA Plot Information");
harness.get_by_label("File Manager");
assert!(harness.query_by_label("Edit").is_none());
harness.get_by_label("Math");
harness.get_by_label("Kinetic Methods");
harness.get_by_label("Direct Problem");
harness.get_by_label("Test Options");
harness.get_by_label("Create Plots");
harness.get_by_label("Undo");
harness.get_by_label("Clear Selected");
harness.get_by_label("Reset View");
harness.get_by_label("Refresh");
harness.get_by_label("Zoom To Selection");
harness.get_by_label("Input value:");
harness.get_by_label("output column:");
harness.get_by_label("X");
harness.get_by_label("Y");
harness.get_by_label("Add");
harness.get_by_label("Sub");
harness.get_by_label("Mul");
harness.get_by_label("Divide");
harness.get_by_label("ln");
harness.get_by_label("exp");
});
}
#[test]
fn tier2_hovering_plot_updates_cursor_coordinate_readout() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
let heading_rect = harness.get_by_label("Data Plot").rect();
let plot_point = egui::pos2(heading_rect.center().x, heading_rect.bottom() + 200.0);
harness.hover_at(plot_point);
harness.run();
assert!(
harness
.state()
.app
.model
.interaction
.pointer_coordinate
.is_some()
);
});
}
#[test]
fn tier1_smoke_top_menu_buttons_and_entries_clickable() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_harness(app, |harness| {
harness.run();
for label in [
"File Manager",
"Math",
"Kinetic Methods",
"Direct Problem",
"Test Options",
] {
harness.get_by_label(label).click();
harness.run();
}
});
assert_menu_entry_clickable("File Manager", "Import Data");
}
#[test]
fn tier2_file_manager_hides_unimplemented_actions() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_harness(app, |harness| {
harness.run();
harness.get_by_label("File Manager").click();
harness.run();
for label in [
"Append data to experiment",
"Export Data",
"Manage Data",
"Save As Excel",
] {
assert!(
harness.query_by_label(label).is_none(),
"unexpected menu item: {label}"
);
}
harness.get_by_label("Import Data");
harness.get_by_label("Open Document");
harness.get_by_label("Append to experiment");
harness.get_by_label("Add Plot");
harness.get_by_label("Save As");
harness.get_by_label("Export CSV");
harness.get_by_label("Export TXT");
harness.get_by_label("Plot Editor");
assert!(harness.query_by_label("Save As Image").is_none());
});
}
#[test]
fn tier3_plot_editor_action_opens_editor_with_loaded_data() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "File Manager", "Plot Editor");
let state = &harness.state().app;
assert!(
state.kithe_plot_window.open,
"Plot Editor did not open; system message: {}",
state.model.message
);
assert!(state.model.message.contains("Plot Redactor window opened"));
harness.run();
assert!(harness.state().app.kithe_plot_window.open);
assert!(
harness.query_by_label("Files").is_some(),
"the Plot Editor window should render its own Files menu"
);
});
}
#[test]
fn tier3_append_continuation_imports_and_adds_combined_experiment() {
let model = load_model_with_one_curve();
let original_count = model.series.len();
let target_id = model.list_of_experiments()[0].clone();
let original_rows = model
.series
.get_experiment_by_id(&target_id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap()
.height();
let continuation_dir = tempfile::tempdir().unwrap();
let continuation = continuation_dir.path().join("continuation.csv");
std::fs::write(&continuation, "t,T,m\n0,303,7\n1,304,6\n2,305,5\n").unwrap();
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.set_size(egui::vec2(800.0, 600.0));
harness.state_mut().app.new_experiment_dialog.current_dir =
continuation_dir.path().to_path_buf();
queue_open_path(harness, continuation.clone());
harness.run();
click_menu_entry(harness, "File Manager", "Append to experiment");
harness.get_by_label("Preview continuation").click();
harness.run();
assert!(
harness.query_by_label_contains("Preview:").is_some(),
"continuation preview failed: {:?}",
harness.state().app.new_experiment_dialog.last_error
);
harness.get_by_label("Add combined experiment").click();
harness.run();
harness.run();
let model = &harness.state().app.model;
let combined_id = format!("{target_id}_continued");
assert_eq!(
model.series.len(),
original_count + 1,
"append commit failed: error={:?}, open={}",
harness.state().app.new_experiment_dialog.last_error,
harness.state().app.new_experiment_dialog.open
);
let combined = model.series.get_experiment_by_id(&combined_id).unwrap();
let frame = combined.dataset.frame.clone().collect().unwrap();
let times = frame.column("t").unwrap().f64().unwrap();
assert_eq!(frame.height(), original_rows + 3);
assert!(
times
.into_no_null_iter()
.collect::<Vec<_>>()
.windows(2)
.all(|pair| pair[1] > pair[0])
);
assert!(
model
.plots
.iter()
.any(|curve| curve.experiment_id == combined_id)
);
});
}
#[test]
fn tier3_append_continuation_can_explicitly_replace_source() {
let model = load_model_with_one_curve();
let target_id = model.list_of_experiments()[0].clone();
let original_rows = model
.series
.get_experiment_by_id(&target_id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap()
.height();
let continuation_dir = tempfile::tempdir().unwrap();
let continuation = continuation_dir.path().join("continuation.csv");
std::fs::write(&continuation, "t,T,m\n0,303,7\n1,304,6\n").unwrap();
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.set_size(egui::vec2(800.0, 600.0));
harness.state_mut().app.new_experiment_dialog.current_dir =
continuation_dir.path().to_path_buf();
queue_open_path(harness, continuation.clone());
harness.run();
click_menu_entry(harness, "File Manager", "Append to experiment");
let replace_checkbox = harness.get_by_label("Replace the original experiment");
replace_checkbox.scroll_to_me();
replace_checkbox.focus();
harness.key_press(egui::Key::Space);
harness.run();
assert!(harness.query_by_label("New experiment ID:").is_none());
harness.get_by_label("Preview continuation").click();
harness.run();
assert!(
harness
.query_by_label("Replace source with combined experiment")
.is_some(),
"continuation preview failed: {:?}",
harness.state().app.new_experiment_dialog.last_error
);
harness
.get_by_label("Replace source with combined experiment")
.click();
harness.run();
let model = &harness.state().app.model;
assert_eq!(model.series.len(), 1);
let replaced = model.series.get_experiment_by_id(&target_id).unwrap();
let frame = replaced.dataset.frame.clone().collect().unwrap();
assert_eq!(frame.height(), original_rows + 2);
let times = frame.column("t").unwrap().f64().unwrap();
assert!(
times
.into_no_null_iter()
.collect::<Vec<_>>()
.windows(2)
.all(|pair| pair[1] > pair[0])
);
assert!(
model
.plots
.iter()
.any(|curve| curve.experiment_id == target_id)
);
});
}
#[test]
fn tier2_test_options_hides_unimplemented_run_tests_action() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("Test Options").click();
harness.run();
assert!(harness.query_by_label("Run Tests").is_none());
harness.get_by_label("Load testing data");
});
}
#[test]
fn tier2_criado_master_curve_is_disabled_without_placeholder_window() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("Kinetic Methods").click();
harness.run();
assert!(harness.query_by_label("Golden Pipeline").is_none());
harness.get_by_label("Criado Master Curve: unavailable pending scientific validation");
assert!(
harness
.query_by_label("TODO: Implement Criado master curve analysis.")
.is_none(),
"unimplemented analysis must not open a placeholder window"
);
});
}
#[test]
fn tier2_kissinger_hides_unimplemented_actions_and_keeps_calculate() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "Kinetic Methods", "Kissinger");
harness.run();
assert!(
harness
.query_by_label("Calculate kinetic expression")
.is_none()
);
assert!(
harness
.query_by_label("Calculate pre-exponential factor")
.is_none()
);
harness.get_by_label("Non-isothermal Kissinger peak method: estimates one activation energy from peak temperatures measured at different heating rates. No numeric or text input is required; results appear below and source data is unchanged.");
harness.get_by_label("Calculate");
});
}
#[test]
fn tier1_smoke_quick_actions_and_arithmetic_clickable() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_harness(app, |harness| {
harness.run();
for label in [
"Create Plots",
"Clear Selected",
"Reset View",
"Refresh",
"Zoom To Selection",
"move time to zero",
"Delete Plot",
"calc relative mass",
"calc conversion",
"from mV to mg",
"from s to h",
] {
harness.get_by_label(label).click();
harness.run();
}
harness.get_by_label("X").click();
harness.run();
harness.get_by_label("Y").click();
harness.run();
for label in ["Divide", "Sub", "Mul", "Add", "ln", "exp"] {
harness.get_by_label(label).click();
harness.run();
}
});
}
#[test]
fn tier2_import_picker_cancellation_does_not_open_import_form() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "File Manager", "Import Data");
assert!(!harness.state().app.new_experiment_dialog.open);
});
}
#[test]
fn tier2_import_picker_selection_opens_raw_import_form() {
let source = tempfile::NamedTempFile::new().unwrap();
fs::write(source.path(), "t,m\n0,1\n1,0.9\n").unwrap();
let app = PlotApp::for_testing(PlotModel::new());
with_plot_app_state_harness(app, |harness| {
harness.run();
queue_open_path(harness, source.path().to_path_buf());
click_menu_entry(harness, "File Manager", "Import Data");
assert!(harness.state().app.new_experiment_dialog.open);
assert_eq!(
harness
.state()
.app
.new_experiment_dialog
.selected_file
.as_deref(),
Some(source.path())
);
harness.get_by_label("Experiment ID:");
harness.get_by_label("Thermal regime:");
});
}
#[test]
fn tier3_open_document_loads_typed_series_without_import_controls() {
let source = load_model_with_one_curve();
let expected_id = source.list_of_experiments()[0].clone();
let file = tempfile::NamedTempFile::new().unwrap();
source.to_csv_series(file.path()).unwrap();
let mut app = PlotApp::for_testing(PlotModel::new());
app.kinetic_methods_state.fit_pending_batch = Some(pending_fit_batch("old_document"));
with_plot_app_state_harness(app, |harness| {
harness.run();
queue_open_path(harness, file.path().to_path_buf());
click_menu_entry(harness, "File Manager", "Open Document");
let state = harness.state();
assert_eq!(
state.app.model.list_of_experiments(),
vec![expected_id.clone()]
);
assert!(!state.app.new_experiment_dialog.open);
assert!(state.app.kinetic_methods_state.fit_pending_batch.is_none());
assert!(harness.query_by_label("Thermal regime:").is_none());
assert!(harness.query_by_label("bind t").is_none());
assert!(harness.query_by_label("bind T").is_none());
assert!(harness.query_by_label("bind m").is_none());
});
}
#[test]
fn tier2_file_manager_save_uses_save_series_dialog_when_path_is_not_set() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "File Manager", "Save *");
assert!(harness.state().app.new_experiment_dialog.save_series_open);
});
}
#[test]
fn tier2_save_picker_cancellation_keeps_save_as_open_without_writing() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "File Manager", "Save As");
harness.get_by_label("Save Document").click();
harness.run();
let dialog = &harness.state().app.new_experiment_dialog;
assert!(dialog.save_series_open);
assert!(dialog.last_error.is_none());
});
}
#[test]
fn tier3_typed_csv_exports_one_experiment_with_selected_columns_and_metadata() {
let mut model = load_model_with_one_curve();
let experiment_id = model.list_of_experiments()[0].clone();
model
.series
.try_mutate_by_id(&experiment_id, |experiment| {
experiment.meta.isothermal_temperature = Some(510.0);
experiment.meta.thermal_mode =
crate::Kinetics::experimental_kinetics::experiment_series_main::ExperimentThermalMode::Isothermal;
experiment.meta.comment = Some("single experiment export".to_string());
Ok(())
})
.unwrap();
let export_dir = unique_temp_dir("typed_single_export");
fs::create_dir_all(&export_dir).unwrap();
let mut app = PlotApp::for_testing(model);
app.new_experiment_dialog.current_dir = export_dir.clone();
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "File Manager", "Export CSV");
harness.get_by_label("One experiment").click();
harness.run();
harness.get_by_label("Clear column selection").click();
harness.run();
for column in ["t", "m"] {
let label = export_column_label(&harness.state().app.model, &experiment_id, column);
harness.get_by_label(&label).click();
harness.run();
}
queue_save_path(harness, export_dir.join("series_export.csv"));
harness.get_by_label("Export").click();
harness.run();
let path = export_dir.join("series_export.csv");
assert!(
path.is_file(),
"export failed: {:?}, open={}, files={:?}",
harness.state().app.new_experiment_dialog.last_error,
harness.state().app.new_experiment_dialog.save_series_open,
fs::read_dir(&export_dir)
.unwrap()
.map(|entry| entry.unwrap().file_name())
.collect::<Vec<_>>()
);
let mut restored = PlotModel::new();
restored.from_csv_series(&path).unwrap();
assert_eq!(restored.list_of_experiments(), vec![experiment_id.clone()]);
let experiment = restored
.series
.get_experiment_by_id(&experiment_id)
.unwrap();
assert_eq!(experiment.meta.isothermal_temperature, Some(510.0));
assert_eq!(
experiment.meta.comment.as_deref(),
Some("single experiment export")
);
assert_eq!(experiment.dataset.schema.time.as_deref(), Some("t"));
assert_eq!(experiment.dataset.schema.mass.as_deref(), Some("m"));
assert_eq!(experiment.dataset.schema.temperature, None);
assert_eq!(
experiment.dataset.frame.clone().collect().unwrap().width(),
2
);
});
fs::remove_dir_all(export_dir).unwrap();
}
#[test]
fn tier3_typed_csv_exports_selected_experiments_with_column_metadata_labels() {
let mut model = load_model_with_one_curve();
let base = model.series.experiments()[0].clone();
let mut condition_two = base.clone().with_id("condition_two");
condition_two.meta.identity = uuid::Uuid::new_v4().to_string();
let mut condition_three = base.with_id("condition_three");
condition_three.meta.identity = uuid::Uuid::new_v4().to_string();
model.series.push(condition_two).unwrap();
model.series.push(condition_three).unwrap();
let ids = model.list_of_experiments();
let export_dir = unique_temp_dir("typed_selected_experiments");
fs::create_dir_all(&export_dir).unwrap();
let mut app = PlotApp::for_testing(model);
app.new_experiment_dialog.current_dir = export_dir.clone();
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "File Manager", "Export CSV");
harness.get_by_label("Selected experiments").click();
harness.run();
harness.get_by_label("condition_two").click();
harness.run();
queue_save_path(harness, export_dir.join("series_export.csv"));
harness.get_by_label("Export").click();
harness.run();
let path = export_dir.join("series_export.csv");
assert!(
path.is_file(),
"export failed: {:?}, open={}, files={:?}",
harness.state().app.new_experiment_dialog.last_error,
harness.state().app.new_experiment_dialog.save_series_open,
fs::read_dir(&export_dir)
.unwrap()
.map(|entry| entry.unwrap().file_name())
.collect::<Vec<_>>()
);
let mut restored = PlotModel::new();
restored.from_csv_series(&path).unwrap();
assert_eq!(
restored.list_of_experiments(),
vec![ids[0].clone(), "condition_two".to_string()]
);
assert!(
!restored
.list_of_experiments()
.contains(&"condition_three".to_string())
);
});
fs::remove_dir_all(export_dir).unwrap();
}
#[test]
fn tier3_excel_export_writes_selected_experiment_and_columns_to_workbook() {
use calamine::{Data, Reader, Xlsx, open_workbook};
let mut model = load_model_with_one_curve();
let base_id = model.list_of_experiments()[0].clone();
let mut base = model.series.experiments()[0].clone();
base.dataset.frame = base.dataset.frame.with_columns([polars::prelude::when(
polars::prelude::col("t").eq(polars::prelude::lit(0.0)),
)
.then(polars::prelude::lit(polars::prelude::NULL))
.otherwise(polars::prelude::col("m"))
.alias("m")]);
let mut condition_two = base.with_id("condition_two");
condition_two.meta.identity = uuid::Uuid::new_v4().to_string();
model.series.push(condition_two).unwrap();
let export_dir = unique_temp_dir("excel_export");
fs::create_dir_all(&export_dir).unwrap();
let mut app = PlotApp::for_testing(model);
app.new_experiment_dialog.current_dir = export_dir.clone();
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "File Manager", "Export Excel");
harness.get_by_label("Excel XLSX").click();
harness.run();
harness.get_by_label("Selected experiments").click();
harness.run();
harness.get_by_label("Clear experiment selection").click();
harness.run();
harness.get_by_label("condition_two").click();
harness.run();
harness.get_by_label("Clear column selection").click();
harness.run();
for column in ["t", "m"] {
let label =
export_column_label(&harness.state().app.model, "condition_two", column);
harness.get_by_label(&label).click();
harness.run();
}
queue_save_path(harness, export_dir.join("series_export.xlsx"));
harness.get_by_label("Export").click();
harness.run();
let path = export_dir.join("series_export.xlsx");
assert!(
path.is_file(),
"export failed: {:?}",
harness.state().app.new_experiment_dialog.last_error
);
assert!(
fs::metadata(&path).unwrap().len() > 0,
"Excel export should not produce an empty file"
);
let mut workbook: Xlsx<_> = open_workbook(&path).expect("valid XLSX workbook");
assert_eq!(workbook.sheet_names(), vec!["condition_two"]);
let sheet = workbook.worksheet_range("condition_two").unwrap();
let headers = [
sheet.get_value((0, 0)).unwrap().to_string(),
sheet.get_value((0, 1)).unwrap().to_string(),
];
assert_eq!(
headers
.iter()
.filter(|header| header.as_str() == "t")
.count(),
1
);
assert_eq!(
headers
.iter()
.filter(|header| header.as_str() == "m")
.count(),
1
);
assert_eq!(sheet.get_value((0, 2)), None);
let time_column = headers.iter().position(|header| header == "t").unwrap() as u32;
let mass_column = headers.iter().position(|header| header == "m").unwrap() as u32;
assert_eq!(sheet.get_value((1, time_column)), Some(&Data::Float(0.0)));
assert_eq!(sheet.get_value((1, mass_column)), Some(&Data::Empty));
assert_eq!(sheet.get_value((2, time_column)), Some(&Data::Float(1.0)));
assert_eq!(sheet.get_value((2, mass_column)), Some(&Data::Float(-6.18)));
assert!(!workbook.sheet_names().contains(&base_id));
});
fs::remove_dir_all(export_dir).unwrap();
}
#[test]
fn tier3_open_typed_document_prompts_before_discarding_unsaved_series() {
let model = load_model_with_one_curve();
let original_id = model.list_of_experiments()[0].clone();
let mut replacement = model.series.clone();
replacement
.set_experiment_id(0, "replacement_document")
.unwrap();
let import_dir = unique_temp_dir("open_document_guard");
fs::create_dir_all(&import_dir).unwrap();
let document_path = import_dir.join("replacement.csv");
replacement.to_csv_series(&document_path).unwrap();
let mut app = PlotApp::for_testing(model);
app.kinetic_methods_state.fit_pending_batch = Some(pending_fit_batch(&original_id));
app.new_experiment_dialog.current_dir = import_dir.clone();
app.new_experiment_dialog.selected_file = Some(document_path.clone());
with_plot_app_state_harness(app, |harness| {
harness.run();
queue_open_path(harness, document_path.clone());
click_menu_entry(harness, "File Manager", "Import Data");
harness.get_by_label("Open as document").click();
harness.run();
harness.get_by_label("Open series").click();
harness.run();
harness.get_by_label("Unsaved document changes");
harness.get_by_label("Discard").click();
harness.run();
assert_eq!(
harness.state().app.model.list_of_experiments(),
vec!["replacement_document"]
);
assert!(
harness
.state()
.app
.model
.series
.get_experiment_by_id(&original_id)
.is_err()
);
assert!(
harness
.state()
.app
.kinetic_methods_state
.fit_pending_batch
.is_none()
);
});
fs::remove_dir_all(import_dir).unwrap();
}
#[test]
fn tier3_discard_fit_preview_preserves_the_stored_fitted_model() {
use crate::Kinetics::experimental_kinetics::math::fitting::{
FitColumnRequest, FittingModelName,
};
let mut model = load_model_with_one_curve();
let id = model.list_of_experiments()[0].clone();
let session = model
.fit_session_and_plot(FitColumnRequest::new(
id.clone(),
"t",
"m",
FittingModelName::Linear,
))
.unwrap();
let result = session.result.clone();
let mut app = PlotApp::for_testing(model);
app.kinetic_methods_state.fit_sessions.push(session);
app.kinetic_methods_state.fit_model_result = Some(result.clone());
app.kinetic_methods_state.fit_pending_batch = Some(pending_fit_batch(&id));
app.new_experiment_dialog.column_manager_open = true;
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("Discard preview").click();
harness.run();
let methods = &harness.state().app.kinetic_methods_state;
assert!(methods.fit_pending_batch.is_none());
assert_eq!(methods.fit_sessions.len(), 1);
assert_eq!(methods.fit_sessions[0].result.output_col, result.output_col);
assert_eq!(methods.fit_model_result.as_ref(), Some(&result));
});
}
#[test]
fn tier3_import_typed_series_adds_experiments_without_changing_document_target() {
let model = load_model_with_one_curve();
let original_id = model.list_of_experiments()[0].clone();
let mut imported =
crate::Kinetics::experimental_kinetics::experiment_series_main::TGASeries::new();
let mut imported_experiment = model.series.experiments()[0].clone();
imported_experiment.meta.id = "imported_typed".to_string();
imported_experiment.meta.identity = uuid::Uuid::new_v4().to_string();
imported.push(imported_experiment).unwrap();
let import_dir = unique_temp_dir("typed_series_add");
fs::create_dir_all(&import_dir).unwrap();
let import_path = import_dir.join("incoming_series.csv");
imported.to_csv_series(&import_path).unwrap();
let mut app = PlotApp::for_testing(model);
app.new_experiment_dialog.current_dir = import_dir.clone();
app.new_experiment_dialog.selected_file = Some(import_path.clone());
with_plot_app_state_harness(app, |harness| {
harness.run();
queue_open_path(harness, import_path.clone());
click_menu_entry(harness, "File Manager", "Import Data");
harness.get_by_label("Add to current series").click();
harness.run();
harness
.get_by_label("Import series into current document")
.click();
harness.run();
assert_eq!(
harness.state().app.model.list_of_experiments(),
vec![original_id.clone(), "imported_typed".to_string()]
);
harness.key_press_modifiers(Modifiers::COMMAND, Key::S);
harness.run();
harness.get_by_label("Save Document");
assert!(harness.state().app.new_experiment_dialog.save_series_open);
});
fs::remove_dir_all(import_dir).unwrap();
}
#[test]
fn tier3_open_guard_cancel_preserves_series_and_save_commits_before_open() {
let model = load_model_with_one_curve();
let original_id = model.list_of_experiments()[0].clone();
let mut replacement = model.series.clone();
replacement
.set_experiment_id(0, "replacement_document")
.unwrap();
let import_dir = unique_temp_dir("open_document_save_guard");
fs::create_dir_all(&import_dir).unwrap();
let document_path = import_dir.join("replacement.csv");
replacement.to_csv_series(&document_path).unwrap();
let mut app = PlotApp::for_testing(model);
app.new_experiment_dialog.current_dir = import_dir.clone();
app.new_experiment_dialog.selected_file = Some(document_path.clone());
with_plot_app_state_harness(app, |harness| {
harness.run();
queue_open_path(harness, document_path.clone());
click_menu_entry(harness, "File Manager", "Import Data");
harness.get_by_label("Open as document").click();
harness.run();
harness.get_by_label("Open series").click();
harness.run();
harness.get_by_label("Cancel").click();
harness.run();
assert_eq!(
harness.state().app.model.list_of_experiments(),
vec![original_id.clone()]
);
harness.key_press_modifiers(Modifiers::COMMAND, Key::S);
harness.run();
queue_save_path(harness, import_dir.join("kinetics_session.csv"));
harness.get_by_label("Save Document").click();
harness.run();
let saved_current_path = import_dir.join("kinetics_session.csv");
assert!(saved_current_path.is_file());
harness.state_mut().app.model.series.mark_changed();
queue_open_path(harness, document_path.clone());
click_menu_entry(harness, "File Manager", "Import Data");
harness.get_by_label("Open as document").click();
harness.run();
harness.get_by_label("Open series").click();
harness.run();
harness.get_by_label("Save").click();
harness.run();
assert_eq!(
harness.state().app.model.list_of_experiments(),
vec!["replacement_document"]
);
let saved_before_open = crate::Kinetics::experimental_kinetics::experiment_series_main::TGASeries::from_csv_series(&saved_current_path).unwrap();
assert!(saved_before_open.get_experiment_by_id(&original_id).is_ok());
assert!(
saved_before_open
.get_experiment_by_id("replacement_document")
.is_err()
);
});
fs::remove_dir_all(import_dir).unwrap();
}
#[test]
fn tier3_save_as_confirms_overwrite_and_cancel_keeps_existing_file() {
let model = load_model_with_one_curve();
let export_dir = unique_temp_dir("session_overwrite");
fs::create_dir_all(&export_dir).unwrap();
let target = export_dir.join("kinetics_session.csv");
fs::write(&target, "keep this file until confirmed").unwrap();
let mut app = PlotApp::for_testing(model);
app.new_experiment_dialog.current_dir = export_dir.clone();
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "File Manager", "Save As");
queue_save_path(harness, target.clone());
harness.get_by_label("Save Document").click();
harness.run();
harness.get_by_label("Confirm overwrite");
harness.get_by_label("Cancel").click();
harness.run();
assert_eq!(
fs::read_to_string(&target).unwrap(),
"keep this file until confirmed"
);
queue_save_path(harness, target.clone());
harness.get_by_label("Save Document").click();
harness.run();
harness.get_by_label("Replace file").click();
harness.run();
let content = fs::read_to_string(&target).unwrap();
assert!(content.starts_with("# KiThe TGA Series"));
});
fs::remove_dir_all(export_dir).unwrap();
}
#[test]
fn tier3_plain_txt_exports_only_selected_columns_without_kithe_metadata() {
let model = load_model_with_one_curve();
let experiment_id = model.list_of_experiments()[0].clone();
let export_dir = unique_temp_dir("plain_txt_export");
fs::create_dir_all(&export_dir).unwrap();
let mut app = PlotApp::for_testing(model);
app.new_experiment_dialog.current_dir = export_dir.clone();
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "File Manager", "Export TXT");
harness.get_by_label("Clear column selection").click();
harness.run();
for column in ["t", "m"] {
let label = export_column_label(&harness.state().app.model, &experiment_id, column);
harness.get_by_label(&label).click();
harness.run();
}
queue_save_path(
harness,
export_dir.join(if experiment_id.trim().is_empty() {
"experiment_export.txt".to_string()
} else {
format!("{experiment_id}_export.txt")
}),
);
harness.get_by_label("Export").click();
harness.run();
let path = export_dir.join(if experiment_id.trim().is_empty() {
"experiment_export.txt".to_string()
} else {
format!("{experiment_id}_export.txt")
});
assert!(path.is_file());
let text = fs::read_to_string(path).unwrap();
let header = text
.lines()
.next()
.unwrap()
.split('\t')
.collect::<std::collections::HashSet<_>>();
assert_eq!(header, std::collections::HashSet::from(["t", "m"]));
assert!(!text.contains("#META"));
assert!(!text.contains("KiThe TGA"));
});
fs::remove_dir_all(export_dir).unwrap();
}
#[test]
fn tier3_export_rejects_empty_column_selection_without_closing_dialog() {
let model = load_model_with_one_curve();
let export_dir = unique_temp_dir("empty_export_selection");
fs::create_dir_all(&export_dir).unwrap();
let mut app = PlotApp::for_testing(model);
app.new_experiment_dialog.current_dir = export_dir.clone();
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "File Manager", "Export TXT");
harness.get_by_label("Clear column selection").click();
harness.run();
harness.get_by_label("Export").click();
harness.run();
assert!(harness.state().app.new_experiment_dialog.save_series_open);
assert!(
harness
.state()
.app
.new_experiment_dialog
.last_error
.as_deref()
.is_some_and(|message| message.contains("at least one column"))
);
assert_eq!(fs::read_dir(&export_dir).unwrap().count(), 0);
});
fs::remove_dir_all(export_dir).unwrap();
}
#[test]
fn tier3_import_file_through_gui_creates_bound_experiment_and_curves() {
let import_dir = unique_temp_dir("gui_import");
fs::create_dir_all(&import_dir).expect("failed to create import fixture directory");
let raw_path = import_dir.join("TGAexample.txt");
fs::copy(test_data_path(), &raw_path).expect("failed to copy raw import fixture");
let raw_contents = fs::read(&raw_path).unwrap();
let mut app = PlotApp::for_testing(PlotModel::new());
app.new_experiment_dialog.current_dir = import_dir.clone();
with_plot_app_state_harness(app, |harness| {
harness.run();
queue_open_path(harness, raw_path.clone());
click_menu_entry(harness, "File Manager", "Import Data");
harness.get_by_label("Import experiment").click();
harness.run();
let model = &harness.state().app.model;
let ids = model.list_of_experiments();
assert_eq!(
ids.len(),
1,
"import dialog error: {:?}; open={}; selected={:?}",
harness.state().app.new_experiment_dialog.last_error,
harness.state().app.new_experiment_dialog.open,
harness.state().app.new_experiment_dialog.selected_file
);
let id = &ids[0];
assert_eq!(id, "TGAexample");
assert!(model.series.get_time_col(id).is_ok());
assert!(model.series.get_temperature_col(id).is_ok());
assert!(model.series.get_mass_col(id).is_ok());
assert!(
model.plots.iter().any(|curve| curve.experiment_id == *id),
"successful import should create default curves"
);
assert!(!harness.state().app.new_experiment_dialog.open);
harness.key_press_modifiers(Modifiers::COMMAND, Key::S);
harness.run();
harness.get_by_label("Save Document");
assert_eq!(fs::read(&raw_path).unwrap(), raw_contents);
});
fs::remove_dir_all(import_dir).expect("failed to remove import fixture directory");
}
#[test]
fn tier3_import_error_stays_visible_above_the_action_in_compact_dialog() {
let mut app = PlotApp::for_testing(PlotModel::new());
app.new_experiment_dialog.open = true;
app.new_experiment_dialog.last_error = Some("Initial validation error".to_string());
with_plot_app_state_harness(app, |harness| {
harness.set_size(egui::vec2(800.0, 600.0));
harness.run();
assert!(harness.query_by_label("Initial validation error").is_some());
harness.get_by_label("Import experiment").click();
harness.run();
assert_eq!(
harness
.state()
.app
.new_experiment_dialog
.last_error
.as_deref(),
Some("Please select a file")
);
assert!(harness.query_by_label("Please select a file").is_some());
assert!(harness.query_by_label("Import experiment").is_some());
});
}
#[test]
fn tier3_import_dialog_allows_manual_binding_for_custom_headers() {
let import_dir = unique_temp_dir("manual_binding");
fs::create_dir_all(&import_dir).expect("failed to create import fixture directory");
let mut fixture = String::from("elapsed probe signal\n");
for idx in 0..100 {
fixture.push_str(&format!(
"{:.1} {:.3} {:.4}\n",
idx as f64,
290.0 + idx as f64,
10.0 - idx as f64 * 0.01
));
}
fs::write(import_dir.join("custom_headers.txt"), fixture)
.expect("failed to write custom-header fixture");
let app = PlotApp::for_testing(PlotModel::new());
with_plot_app_state_harness(app, |harness| {
harness.state_mut().app.new_experiment_dialog.current_dir = import_dir.clone();
harness.run();
queue_open_path(harness, import_dir.join("custom_headers.txt"));
click_menu_entry(harness, "File Manager", "Import Data");
for (label, column) in [
("bind t", "elapsed"),
("bind T", "probe"),
("bind m", "signal"),
] {
harness.get_by_label(label).focus();
harness.get_by_label(label).type_text(column);
harness.run();
}
harness.get_by_label("Import experiment").click();
harness.run();
let model = &harness.state().app.model;
let id = "custom_headers";
assert_eq!(
model.list_of_experiments(),
vec![id.to_string()],
"import failed: {:?}",
harness.state().app.new_experiment_dialog.last_error
);
assert_eq!(model.series.get_time_col(id).unwrap(), "elapsed");
assert_eq!(model.series.get_temperature_col(id).unwrap(), "probe");
assert_eq!(model.series.get_mass_col(id).unwrap(), "signal");
assert!(model.plots.iter().any(|curve| curve.experiment_id == id));
assert!(
harness
.state()
.app
.new_experiment_dialog
.last_error
.is_none()
);
queue_open_path(harness, import_dir.join("custom_headers.txt"));
click_menu_entry(harness, "File Manager", "Import Data");
harness.get_by_label("Experiment ID:").focus();
harness.key_press_modifiers(Modifiers::COMMAND, Key::A);
harness.run();
harness.key_press(Key::Backspace);
harness.run();
harness
.get_by_label("Experiment ID:")
.type_text("custom_headers_second");
harness.run();
assert_eq!(
harness.get_by_label("Experiment ID:").value().as_deref(),
Some("custom_headers_second")
);
harness.get_by_label("Import experiment").click();
harness.run();
let model = &harness.state().app.model;
assert_eq!(
model.list_of_experiments(),
vec![
"custom_headers".to_string(),
"custom_headers_second".to_string()
]
);
assert!(model.plots.iter().any(|curve| {
curve.experiment_id == "custom_headers_second"
&& curve.x_name == "elapsed"
&& curve.y_name == "probe"
}));
assert!(model.plots.iter().any(|curve| {
curve.experiment_id == "custom_headers" && curve.y_name == "signal"
}));
let plots_before_duplicate = model.plots.len();
queue_open_path(harness, import_dir.join("custom_headers.txt"));
click_menu_entry(harness, "File Manager", "Import Data");
harness.get_by_label("Import experiment").click();
harness.run();
let state = harness.state();
assert_eq!(state.app.model.list_of_experiments().len(), 2);
assert_eq!(state.app.model.plots.len(), plots_before_duplicate);
assert!(state.app.new_experiment_dialog.last_error.is_some());
});
fs::remove_dir_all(import_dir).expect("failed to remove temporary import fixture");
}
#[test]
fn tier3_native_picker_selection_routes_raw_file_into_import_form() {
let dir = tempfile::tempdir().unwrap();
let raw = dir.path().join("raw.txt");
fs::write(&raw, "elapsed probe signal\n0 300 10\n1 301 9\n").unwrap();
let app = PlotApp::for_testing(PlotModel::new());
with_plot_app_state_harness(app, |harness| {
harness.run();
queue_open_path(harness, raw.clone());
click_menu_entry(harness, "File Manager", "Import Data");
harness.get_by_label("Experiment ID:");
assert_eq!(
harness
.state()
.app
.new_experiment_dialog
.selected_file
.as_deref(),
Some(raw.as_path())
);
assert!(harness.query_by_label("Search files:").is_none());
});
}
#[test]
fn tier3_quick_actions_build_conversion_and_rate_chain() {
let mut model = load_model_with_one_curve();
model.select_curve(0);
let id = model.get_experiment_by_selected_curve().unwrap();
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.state_mut().app.quick_actions_state.input_value = 100.0;
harness.get_by_label("from mV to mg").click();
harness.run();
harness.get_by_label("calc conversion").click();
harness.run();
assert!(
harness
.state()
.app
.model
.list_of_columns(&id)
.unwrap()
.iter()
.any(|column| column == "conversion"),
"conversion quick action did not create the output; message={:?}",
harness.state().app.model.message
);
harness.get_by_label("conversion rate").click();
harness.run();
let model = &harness.state().app.model;
let columns = model.list_of_columns(&id).unwrap();
assert!(columns.iter().any(|column| column == "deta_dt"));
assert_eq!(model.this_is_y().unwrap(), "T");
assert!(model.series.get_time_col(&id).is_ok());
let experiment = model.series.get_experiment_by_id(&id).unwrap();
assert_eq!(
experiment.dataset.schema.columns["conversion"].unit,
Unit::Dimensionless
);
assert_eq!(
experiment.dataset.schema.columns["deta_dt"].unit,
Unit::PerSecond
);
});
}
#[test]
fn tier3_direct_problem_solves_and_adds_visible_experiment() {
let app = PlotApp::for_testing(PlotModel::new());
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "Direct Problem", "Simple One-Step Kinetics");
assert!(harness.state().app.direct_problem_state.open);
{
let task = &mut harness.state_mut().app.direct_problem_state.task;
task.selected_model = Some(KineticModelNames::F1);
task.t_final = 10.0;
task.beta = 1.0;
task.t0 = 300.0;
task.e = 1_000.0;
task.a = 1.0;
}
harness.run();
harness.get_by_label("Solve IVP").click();
harness.run();
let state = harness.state();
assert_eq!(
state.app.direct_problem_state.last_success.as_deref(),
Some("Created experiment 'ivp_result'")
);
let model = &state.app.model;
assert!(
model
.list_of_experiments()
.contains(&"ivp_result".to_string())
);
let columns = model.list_of_columns("ivp_result").unwrap();
assert!(columns.contains(&"time".to_string()));
assert!(columns.contains(&"conversion".to_string()));
assert!(model.plots.iter().any(|curve| {
curve.experiment_id == "ivp_result"
&& curve.x_name == "time"
&& curve.y_name == "conversion"
&& curve.is_shown()
}));
{
let state = &mut harness.state_mut().app;
state.model.settings.set_n_points(None);
state.direct_problem_state.output_experiment_id = "ivp_plot_warning".into();
}
harness.run();
harness.get_by_label("Solve IVP").click();
harness.run();
let state = harness.state();
assert!(
state
.app
.direct_problem_state
.last_success
.as_deref()
.is_some_and(|message| message.starts_with("Warning:"))
);
assert!(state.app.direct_problem_state.last_error.is_none());
assert!(
state
.app
.model
.series
.get_experiment_by_id("ivp_plot_warning")
.is_ok()
);
assert!(
state
.app
.model
.plots
.iter()
.all(|curve| curve.experiment_id != "ivp_plot_warning")
);
assert!(state.app.model.message.starts_with("Warning:"));
assert!(
state
.app
.model
.plots
.iter()
.any(|curve| curve.experiment_id == "ivp_result")
);
});
}
#[test]
fn tier3_network_direct_problem_publishes_an_isothermal_series() {
let app = PlotApp::for_testing(PlotModel::new());
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "Direct Problem", "Complex Direct Problem");
harness.run();
harness.get_by_label("Run").click();
for _ in 0..80 {
harness.run();
if harness
.state()
.app
.direct_problem_state
.network_app
.completed_run_count()
.is_some()
{
break;
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
assert_eq!(
harness
.state()
.app
.direct_problem_state
.network_app
.completed_run_count(),
Some(3)
);
harness
.get_by_label("Add all runs to Experimental Kinetics")
.click();
harness.run();
let state = harness.state();
let model = &state.app.model;
for temperature in [400, 500, 600] {
let id = format!("network_{temperature}K");
let experiment = model.series.get_experiment_by_id(&id).unwrap();
assert_eq!(
experiment.meta.isothermal_temperature,
Some(temperature as f64)
);
assert_eq!(experiment.dataset.schema.time.as_deref(), Some("time"));
assert_eq!(
experiment.dataset.schema.temperature.as_deref(),
Some("temperature")
);
let concentration = experiment
.dataset
.schema
.columns
.get("concentration_A")
.unwrap();
assert_eq!(concentration.unit, Unit::MolePerCubicMeter);
assert_eq!(concentration.origin, crate::Kinetics::experimental_kinetics::one_experiment_dataset::ColumnOrigin::Simulated);
assert!(model.plots.iter().any(|curve| curve.experiment_id == id
&& curve.y_name == "concentration_A"
&& curve.is_shown()));
}
assert!(model.list_of_experiments().len() == 3);
});
}
#[test]
fn tier3_isoconversional_window_publishes_kinetic_result() {
let mut model = PlotModel::new();
model
.generate_synthetic_data_from_config(&non_isothermal_series_config())
.expect("failed to generate isoconversional fixtures");
let ids = model.list_of_experiments();
for id in &ids {
let t_end = last_value_for(&model, id, "time");
model
.conversion_for_experiment(id.clone(), t_end, "eta")
.expect("failed to prepare conversion");
model
.derive_deta_dt(id, Some("deta_dt"))
.expect("failed to prepare conversion rate");
}
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "Kinetic Methods", "Isoconversional");
harness.get_by_label("Calculate").click();
harness.run();
let state = harness.state();
let status = &state.app.kinetic_methods_state.isoconversional_status;
assert!(
status.starts_with("Done."),
"kinetic analysis failed: {status}"
);
let model = &state.app.model;
let result_id = "isoconversional_result";
assert!(model.list_of_experiments().contains(&result_id.to_string()));
let columns = model.list_of_columns(result_id).unwrap();
assert!(columns.contains(&"eta".to_string()));
assert!(columns.contains(&"Ea".to_string()));
assert!(model.plots.iter().any(|curve| {
curve.experiment_id == result_id
&& curve.x_name == "eta"
&& curve.y_name == "Ea"
&& curve.is_shown()
&& curve.selected
}));
assert!(
model
.plots
.iter()
.filter(|curve| curve.experiment_id != result_id)
.all(|curve| !curve.is_shown() && !curve.selected)
);
assert_eq!(model.get_experiment_by_selected_curve().unwrap(), result_id);
});
let app = PlotApp::for_testing(PlotModel::new());
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "Kinetic Methods", "Isoconversional");
harness.get_by_label("Calculate").click();
harness.run();
assert_eq!(
harness
.state()
.app
.kinetic_methods_state
.isoconversional_status,
"No experiments to analyze."
);
assert!(
harness
.state()
.app
.model
.message
.contains("No experiments to analyze.")
);
assert!(harness.state().app.model.list_of_experiments().is_empty());
assert!(harness.state().app.model.plots.is_empty());
});
}
#[test]
fn combined_analysis_labels_conversion_range_as_eta_not_alpha() {
let mut app = PlotApp::for_testing(PlotModel::new());
app.kinetic_methods_state.combined_kinetics_open = true;
let state = PlotAppTestState { app, open: true };
let mut harness = Harness::builder()
.with_accessibility_check(false)
.build_ui_state(
|ui, state: &mut PlotAppTestState| state.app.show(ui.ctx(), &mut state.open),
state,
);
harness.run();
assert!(harness.query_by_label("Conversion η min:").is_some());
assert!(harness.query_by_label("η max:").is_some());
assert!(harness.query_by_label("alpha_min:").is_none());
assert!(harness.query_by_label("alpha_max:").is_none());
}
#[test]
fn isoconversional_plot_failure_does_not_commit_result_or_hide_existing_curves() {
let mut model = PlotModel::new();
model
.generate_synthetic_data_from_config(&non_isothermal_series_config())
.expect("failed to generate isoconversional fixtures");
let ids = model.list_of_experiments();
for id in &ids {
let t_end = last_value_for(&model, id, "time");
model
.conversion_for_experiment(id.clone(), t_end, "eta")
.unwrap();
model.derive_deta_dt(id, Some("deta_dt")).unwrap();
}
model.set_x(&ids[0], "time").unwrap();
model.set_y(&ids[0], "mass").unwrap();
model.create_points_for_curve(&ids[0]).unwrap();
model.select_curve(0);
let original_plots = model.plots.clone();
let method = IsoconversionalKineticMethod {
method: IsoconversionalMethod::OFW,
};
let view = model
.create_kinetic_data_view_for_method(None, &method.method)
.unwrap();
let result = method.compute(&view).unwrap();
model.settings.set_n_points(Some(0));
assert!(
model
.push_isoconversional_result(&result, "unplottable_result")
.is_err()
);
assert_eq!(model.list_of_experiments(), ids);
assert_eq!(model.plots.len(), original_plots.len());
assert!(
model
.plots
.iter()
.zip(original_plots)
.all(
|(after, before)| after.experiment_id == before.experiment_id
&& after.shown == before.shown
&& after.selected == before.selected
&& after.points == before.points
)
);
}
#[test]
fn tier3_sublimation_window_fits_selected_experiment_with_optional_x_bounds() {
let mut model = PlotModel::new();
model
.generate_synthetic_data_from_config(&non_isothermal_series_config())
.expect("failed to generate sublimation fixture");
let ids = model.list_of_experiments();
let selected_id = ids[0].clone();
let t_end = last_value_for(&model, &selected_id, "time");
model
.conversion_for_experiment(selected_id.clone(), t_end, "eta")
.expect("failed to prepare conversion");
model
.derive_deta_dt(&selected_id, Some("deta_dt"))
.expect("failed to prepare conversion rate");
let conversion = model
.series
.get_experiment_by_id(&selected_id)
.unwrap()
.dataset
.materialize("time", &["eta"])
.unwrap()
.columns["eta"]
.clone();
let lower = conversion.iter().copied().fold(f64::INFINITY, f64::min);
let upper = conversion.iter().copied().fold(f64::NEG_INFINITY, f64::max);
model.set_x(&selected_id, "eta").unwrap();
model.set_y(&selected_id, "deta_dt").unwrap();
model.create_points_for_curve(&selected_id).unwrap();
model.select_curve(model.plots.len() - 1);
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "Kinetic Methods", "Is this a sublimation?");
harness.run();
harness.get_by_label("Conversion X min (optional):");
harness.get_by_label("Conversion X max (optional):");
{
let state = &mut harness.state_mut().app.kinetic_methods_state;
state.sublimation_x_min = lower.to_string();
state.sublimation_x_max = upper.to_string();
}
harness.get_by_label("Calculate").click();
harness.run();
let state = harness.state();
assert!(
state
.app
.kinetic_methods_state
.sublimation_status
.starts_with("Fit complete."),
"sublimation calculation failed: {}",
state.app.kinetic_methods_state.sublimation_status
);
assert!(state.app.kinetic_methods_state.sublimation_ea_kj.is_some());
assert!(state.app.kinetic_methods_state.sublimation_pending_rates.is_some());
assert!(
state
.app
.model
.list_of_columns(&selected_id)
.unwrap()
.contains(&"deta_dt".to_string())
);
assert!(!state
.app
.model
.list_of_columns(&selected_id)
.unwrap()
.contains(&"fitted_sublim_rate".to_string()));
harness.get_by_label("New rate column:");
harness.get_by_label("Publish fitted rate column").click();
harness.run();
let state = harness.state();
assert!(state
.app
.model
.list_of_columns(&selected_id)
.unwrap()
.contains(&"fitted_sublim_rate".to_string()));
let selected_dataset = &state
.app
.model
.series
.get_experiment_by_id(&selected_id)
.unwrap()
.dataset;
let fitted_frame = selected_dataset.frame.clone().collect().unwrap();
let fitted_column = fitted_frame
.column("fitted_sublim_rate")
.unwrap()
.f64()
.unwrap();
assert_eq!(fitted_column.len(), fitted_frame.height());
assert_eq!(fitted_column.null_count(), 0);
assert!(
ids.iter().skip(1).all(|id| !state
.app
.model
.list_of_columns(id)
.unwrap()
.contains(&"fitted_sublim_rate".to_string())),
"diagnostic must not mutate experiments other than the selected one"
);
});
}
#[test]
fn tier3_sublimation_plot_selection_restricts_fit_without_populating_manual_bounds() {
let mut model = PlotModel::new();
model
.generate_synthetic_data_from_config(&non_isothermal_series_config())
.expect("failed to generate sublimation fixture");
let selected_id = model.list_of_experiments()[0].clone();
let t_end = last_value_for(&model, &selected_id, "time");
model
.conversion_for_experiment(selected_id.clone(), t_end, "eta")
.unwrap();
model.derive_deta_dt(&selected_id, Some("deta_dt")).unwrap();
let eta = model
.series
.get_experiment_by_id(&selected_id)
.unwrap()
.dataset
.materialize("time", &["eta"])
.unwrap()
.columns["eta"]
.clone();
let eta_min = eta.iter().copied().fold(f64::INFINITY, f64::min);
let eta_max = eta.iter().copied().fold(f64::NEG_INFINITY, f64::max);
let selected_min = eta_min + 0.2 * (eta_max - eta_min);
let selected_max = eta_min + 0.8 * (eta_max - eta_min);
model.set_x(&selected_id, "eta").unwrap();
model.set_y(&selected_id, "deta_dt").unwrap();
model.create_points_for_curve(&selected_id).unwrap();
model.select_curve(model.plots.len() - 1);
model.plots.last_mut().unwrap().highlighted = true;
model.interaction.selection_rect = Some(SelectionRect::new(
[selected_min, 0.0],
[selected_max, 1.0],
));
let mut app = PlotApp::for_testing(model);
app.kinetic_methods_state.sublimation_check_open = true;
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("Calculate").click();
harness.run();
let state = harness.state();
assert!(
state
.app
.kinetic_methods_state
.sublimation_status
.starts_with("Fit complete."),
"selection-based calculation failed: {}",
state.app.kinetic_methods_state.sublimation_status
);
assert!(state.app.kinetic_methods_state.sublimation_x_min.is_empty());
assert!(state.app.kinetic_methods_state.sublimation_x_max.is_empty());
let pending = state
.app
.kinetic_methods_state
.sublimation_pending_result
.as_ref()
.expect("plot-selected fit should remain available for saving");
let crate::Kinetics::experimental_kinetics::analysis_results::AnalysisResultKind::ParameterSet(set) = &pending.kind else {
panic!("expected typed Sublimation parameters");
};
let range = set.range.as_ref().expect("fit range should be recorded");
assert_eq!(range.axis, "conversion");
assert!((range.start - selected_min).abs() < 1e-10);
assert!((range.end - selected_max).abs() < 1e-10);
assert!(state
.app
.model
.list_of_columns(&selected_id)
.unwrap()
.contains(&"deta_dt".to_string()));
assert!(!state
.app
.model
.list_of_columns(&selected_id)
.unwrap()
.contains(&"fitted_sublim_rate".to_string()));
});
}
#[test]
fn tier2_failed_kinetic_recalculation_clears_old_result_values() {
let app = PlotApp::for_testing(PlotModel::new());
let state = PlotAppTestState { app, open: true };
let mut harness = Harness::builder()
.with_accessibility_check(false)
.build_ui_state(
|ui, state: &mut PlotAppTestState| state.app.show(ui.ctx(), &mut state.open),
state,
);
{
harness.run();
click_menu_entry(&mut harness, "Kinetic Methods", "Kissinger");
{
let state = &mut harness.state_mut().app.kinetic_methods_state;
state.kissinger_ea_kj = Some(123.0);
state.kissinger_r2 = Some(0.99);
}
harness.get_by_label("Calculate").click();
harness.run();
assert!(
harness
.state()
.app
.kinetic_methods_state
.kissinger_ea_kj
.is_none()
);
assert!(
harness
.state()
.app
.kinetic_methods_state
.kissinger_r2
.is_none()
);
harness.state_mut().app.kinetic_methods_state.kissinger_open = false;
harness.run();
click_menu_entry(
&mut harness,
"Kinetic Methods",
"Combined Kinetics Analysis",
);
{
let state = &mut harness.state_mut().app.kinetic_methods_state;
state.combined_result_n = Some(1.0);
state.combined_result_m = Some(2.0);
state.combined_result_ea_kj = Some(123.0);
state.combined_result_r2 = Some(0.99);
}
harness.get_by_label("Calculate").click();
harness.run();
let state = &harness.state().app.kinetic_methods_state;
assert!(state.combined_result_n.is_none());
assert!(state.combined_result_m.is_none());
assert!(state.combined_result_ea_kj.is_none());
assert!(state.combined_result_r2.is_none());
}
}
#[test]
fn tier2_sublimation_rejects_only_one_entered_x_bound() {
let mut model = PlotModel::new();
model
.generate_synthetic_data_from_config(&non_isothermal_series_config())
.expect("failed to generate sublimation fixture");
let selected_id = model.list_of_experiments()[0].clone();
let t_end = last_value_for(&model, &selected_id, "time");
model
.conversion_for_experiment(selected_id.clone(), t_end, "eta")
.unwrap();
model.derive_deta_dt(&selected_id, Some("deta_dt")).unwrap();
model.set_x(&selected_id, "eta").unwrap();
model.set_y(&selected_id, "deta_dt").unwrap();
model.create_points_for_curve(&selected_id).unwrap();
model.select_curve(model.plots.len() - 1);
let mut app = PlotApp::for_testing(model);
app.kinetic_methods_state.sublimation_check_open = true;
app.kinetic_methods_state.sublimation_x_min = "0.2".into();
let before = app.model.list_of_columns(&selected_id).unwrap();
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("Calculate").click();
harness.run();
let state = harness.state();
assert!(
state
.app
.kinetic_methods_state
.sublimation_status
.contains("Enter both conversion X bounds")
);
assert_eq!(
state.app.model.list_of_columns(&selected_id).unwrap(),
before
);
});
}
#[test]
fn tier2_sublimation_rejects_selection_intersecting_multiple_curves() {
let mut model = PlotModel::new();
model
.generate_synthetic_data_from_config(&non_isothermal_series_config())
.expect("failed to generate sublimation fixture");
let selected_id = model.list_of_experiments()[0].clone();
let t_end = last_value_for(&model, &selected_id, "time");
model
.conversion_for_experiment(selected_id.clone(), t_end, "eta")
.unwrap();
model.derive_deta_dt(&selected_id, Some("deta_dt")).unwrap();
model.set_x(&selected_id, "eta").unwrap();
model.set_y(&selected_id, "deta_dt").unwrap();
model.create_points_for_curve(&selected_id).unwrap();
let mut second_curve = model.plots[0].clone();
second_curve.selected = false;
model.plots[0].highlighted = true;
second_curve.highlighted = true;
model.plots.push(second_curve);
model.select_curve(0);
model.interaction.selection_rect = Some(SelectionRect::new([0.2, 0.0], [0.8, 1.0]));
let mut app = PlotApp::for_testing(model);
app.kinetic_methods_state.sublimation_check_open = true;
let before = app.model.list_of_columns(&selected_id).unwrap();
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("Calculate").click();
harness.run();
let state = harness.state();
assert!(
state
.app
.kinetic_methods_state
.sublimation_status
.contains("intersects 2 plotted curves")
);
assert_eq!(
state.app.model.list_of_columns(&selected_id).unwrap(),
before
);
});
}
#[test]
fn golden_pipeline_failure_reaches_main_system_message() {
let mut app = PlotApp::for_testing(PlotModel::new());
app.quick_actions_state.golden_pipeline_dialog.open();
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("Calculate").click();
harness.run();
assert!(
harness
.state()
.app
.model
.message
.contains("Golden Pipeline could not start")
);
assert!(
harness
.state()
.app
.quick_actions_state
.golden_pipeline_dialog
.last_error
.is_some()
);
});
}
#[test]
fn tier2_keyboard_open_cancel_does_not_show_import_form_and_save_opens_dialog() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.key_press_modifiers(Modifiers::COMMAND, Key::O);
harness.run();
assert!(!harness.state().app.new_experiment_dialog.open);
harness.state_mut().app.new_experiment_dialog.open = false;
harness.key_press_modifiers(Modifiers::COMMAND, Key::S);
harness.run();
assert!(harness.state().app.new_experiment_dialog.save_series_open);
harness
.state_mut()
.app
.new_experiment_dialog
.save_series_open = false;
harness.key_press_modifiers(Modifiers::COMMAND.plus(Modifiers::SHIFT), Key::S);
harness.run();
assert!(harness.state().app.new_experiment_dialog.save_series_open);
});
}
#[test]
fn tier2_f1_opens_column_manager_without_modifiers() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
assert!(
!harness
.state()
.app
.new_experiment_dialog
.column_manager_open
);
harness.key_press(Key::F1);
harness.run();
assert!(
harness
.state()
.app
.new_experiment_dialog
.column_manager_open
);
harness.get_by_label("Compare experiments");
});
}
#[test]
fn tier2_function_keys_open_their_top_level_menus() {
for (key, menu_entry) in [
(Key::F2, "Import Data"),
(Key::F3, " Savitzky Golay"),
(Key::F4, "Isoconversional"),
] {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.key_press(key);
harness.run();
harness.get_by_label(menu_entry);
harness.key_press(Key::Escape);
harness.run();
});
}
}
#[test]
fn tier3_column_manager_exposes_separate_comparison_mode() {
let mut model = load_model_with_one_curve();
let source = model
.series
.get_experiment_by_id("")
.unwrap()
.dataset
.clone();
model
.series
.push(
crate::Kinetics::experimental_kinetics::experiment_series_main::TGAExperiment::new(
source,
)
.with_id("comparison_two"),
)
.unwrap();
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
harness.get_by_label("Compare experiments").click();
harness.run();
assert!(harness.query_by_label("Row").is_none());
harness.get_by_label("Sampled comparison");
assert!(harness.query_by_label("Next page").is_none());
harness.get_by_label("Experiments to compare");
harness.get_by_label("(unnamed experiment)").click();
harness.get_by_label("comparison_two").click();
harness.run();
harness.get_by_role_and_label(egui::accesskit::Role::Button, "(unnamed)::m");
harness.get_by_role_and_label(egui::accesskit::Role::Button, "comparison_two::m");
});
}
#[test]
fn tier3_column_manager_blocks_comparison_of_different_time_grids() {
use crate::Kinetics::experimental_kinetics::experiment_series_main::TGAExperiment;
use crate::Kinetics::experimental_kinetics::one_experiment_dataset::TGADataset;
let first_file = tempfile::Builder::new().suffix(".csv").tempfile().unwrap();
let second_file = tempfile::Builder::new().suffix(".csv").tempfile().unwrap();
std::fs::write(first_file.path(), "time,mass\n0.0,10.0\n1.0,9.0\n2.0,8.0\n").unwrap();
std::fs::write(
second_file.path(),
"time,mass\n0.0,10.0\n1.5,9.0\n2.0,8.0\n",
)
.unwrap();
let first = TGADataset::from_csv_universal(first_file.path())
.unwrap()
.bind_time("time", Unit::Second)
.unwrap();
let second = TGADataset::from_csv_universal(second_file.path())
.unwrap()
.bind_time("time", Unit::Second)
.unwrap();
let mut model = PlotModel::new();
model
.series
.push(TGAExperiment::new(first).with_id("grid_one"))
.unwrap();
model
.series
.push(TGAExperiment::new(second).with_id("grid_two"))
.unwrap();
with_plot_app_state_harness(PlotApp::for_testing(model), |harness| {
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
harness.get_by_label("Compare experiments").click();
harness.run();
harness.get_by_label("grid_one").click();
harness.get_by_label("grid_two").click();
harness.run();
harness.get_by_label_contains("time grids differ at source row 1");
assert!(
harness
.query_by_role_and_label(egui::accesskit::Role::Button, "grid_one::mass")
.is_none(),
"incompatible experiments must not render a side-by-side comparison table"
);
assert!(harness.query_by_label("No experiments loaded").is_none());
});
}
#[test]
fn tier3_column_manager_delete_all_requires_confirmation_and_clears_workspace() {
let mut model = load_model_with_one_curve();
let experiment_id = model.list_of_experiments()[0].clone();
model.select_curve(0);
model.set_active_mass_source_for_experiment(&experiment_id, "m");
model.interaction.selection_rect = Some(SelectionRect::new([1.0, 2.0], [3.0, 4.0]));
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
harness.get_by_label("Delete All").click();
harness.run();
harness.get_by_label("DELETE ALL WORKSPACE DATA?");
assert_eq!(
harness.state().app.model.list_of_experiments(),
vec![experiment_id.clone()]
);
harness.get_by_label("Cancel").click();
harness.run();
assert_eq!(
harness.state().app.model.list_of_experiments(),
vec![experiment_id.clone()]
);
harness.get_by_label("Delete All").click();
harness.run();
harness.get_by_label("DELETE EVERYTHING").click();
harness.run();
let model = &harness.state().app.model;
assert!(model.list_of_experiments().is_empty());
assert!(model.plots.is_empty());
assert!(model.get_selected_curve_index().is_none());
assert!(model.interaction.selection_rect.is_none());
assert!(model.active_mass_sources.is_empty());
assert!(model.message.contains("Workspace cleared"));
assert!(
!harness
.state()
.app
.new_experiment_dialog
.column_manager_open
);
});
}
#[test]
fn tier3_column_manager_more_actions_explain_effects_on_hover() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
harness.get_by_label("More").click();
harness.run();
harness.get_by_label("Sort by time").hover();
harness.run();
assert!(
harness
.query_by_label_contains("No new column is created")
.is_some()
);
});
}
#[test]
fn tier2_global_shortcuts_do_not_fire_while_menu_popup_is_open() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("Math").click();
harness.run();
harness.key_press(Key::F2);
harness.run();
assert!(
harness.query_by_label("Import Data").is_none(),
"function-key menu shortcuts must not open a second popup"
);
harness.key_press_modifiers(Modifiers::COMMAND, Key::O);
harness.run();
assert!(!harness.state().app.new_experiment_dialog.open);
});
}
#[test]
fn tier2_import_shortcuts_do_not_intercept_text_field_input() {
let mut model = load_model_with_one_curve();
model.select_curve(0);
let id = model.list_of_experiments()[0].clone();
model.set_active_temperature_source_for_experiment(&id, "T");
model
.sg_filter_column_for_selected_as("T", 5, 2, 0, 1.0, Some("sg_T"))
.expect("prepare an undoable change");
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.run();
let raw = tempfile::NamedTempFile::new().unwrap();
fs::write(raw.path(), "t,T,m\n0,300,10\n1,301,9\n").unwrap();
queue_open_path(harness, raw.path().to_path_buf());
click_menu_entry(harness, "File Manager", "Import Data");
harness.get_by_label("Experiment ID:").focus();
harness.run();
harness.key_press(Key::F3);
harness.run();
harness.key_press_modifiers(Modifiers::COMMAND, Key::S);
harness.run();
harness.key_press_modifiers(Modifiers::COMMAND, Key::Z);
harness.run();
assert!(harness.state().app.new_experiment_dialog.open);
assert!(
!harness.state().app.new_experiment_dialog.save_series_open,
"global Save shortcut must not fire while a text input has focus"
);
assert!(
harness.query_by_label(" Savitzky Golay").is_none(),
"menu shortcuts must not fire while a text input has focus"
);
assert!(
harness
.state()
.app
.model
.list_of_columns(&id)
.unwrap()
.contains(&"sg_T".into()),
"global Undo must not fire while a text input has focus"
);
});
}
#[test]
fn tier2_escape_closes_only_the_topmost_tool_window() {
let mut app = PlotApp::for_testing(load_model_with_one_curve());
app.kinetic_methods_state.fit_model_open = true;
app.direct_problem_state.open = true;
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.key_press(Key::Escape);
harness.run();
assert!(!harness.state().app.direct_problem_state.open);
assert!(harness.state().app.kinetic_methods_state.fit_model_open);
});
}
#[test]
fn tier2_escape_does_not_close_window_while_editing_text() {
let mut app = PlotApp::for_testing(load_model_with_one_curve());
app.new_experiment_dialog.open = true;
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("Experiment ID:").focus();
harness.run();
harness.key_press(Key::Escape);
harness.run();
assert!(harness.state().app.new_experiment_dialog.open);
});
}
#[test]
fn tier3_keyboard_save_creates_full_document_independently_of_exports() {
let model = load_model_with_one_curve();
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
let export_dir = unique_temp_dir("gui_keyboard_save");
fs::create_dir_all(&export_dir).expect("failed to create export dir");
harness.state_mut().app.new_experiment_dialog.current_dir = export_dir.clone();
harness.run();
harness.key_press_modifiers(Modifiers::COMMAND, Key::S);
harness.run();
queue_save_path(harness, export_dir.join("kinetics_session.csv"));
harness.get_by_label("Save Document").click();
harness.run();
let export_file = export_dir.join("kinetics_session.csv");
assert!(
export_file.exists(),
"first save should create the full typed document; dialog error: {:?}; dir: {:?}",
harness.state().app.new_experiment_dialog.last_error,
fs::read_dir(&export_dir)
.unwrap()
.map(|entry| entry.unwrap().file_name())
.collect::<Vec<_>>()
);
assert!(!harness.state().app.new_experiment_dialog.save_series_open);
harness.key_press_modifiers(Modifiers::COMMAND, Key::S);
harness.run();
assert!(
!harness.state().app.new_experiment_dialog.save_series_open,
"subsequent save should write directly to the document path"
);
let _ = fs::remove_file(&export_file);
let _ = fs::remove_dir_all(&export_dir);
});
}
#[test]
fn tier3_keyboard_save_keeps_plain_txt_format_and_single_experiment_scope() {
let model = load_model_with_one_curve();
let experiment_id = model.list_of_experiments()[0].clone();
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
let export_dir = unique_temp_dir("gui_keyboard_txt");
fs::create_dir_all(&export_dir).expect("failed to create export directory");
harness.state_mut().app.new_experiment_dialog.current_dir = export_dir.clone();
harness.run();
click_menu_entry(harness, "File Manager", "Export TXT");
queue_save_path(
harness,
export_dir.join(if experiment_id.trim().is_empty() {
"experiment_export.txt".to_string()
} else {
format!("{experiment_id}_export.txt")
}),
);
harness.get_by_label("Export").click();
harness.run();
let export_file = export_dir.join(if experiment_id.trim().is_empty() {
"experiment_export.txt".to_string()
} else {
format!("{experiment_id}_export.txt")
});
assert!(
export_file.is_file(),
"export error: {:?}; files: {:?}",
harness.state().app.new_experiment_dialog.last_error,
fs::read_dir(&export_dir)
.unwrap()
.map(|entry| entry.unwrap().file_name())
.collect::<Vec<_>>()
);
assert!(!fs::read_to_string(&export_file).unwrap().contains("#META"));
harness.key_press_modifiers(Modifiers::COMMAND, Key::S);
harness.run();
assert!(harness.state().app.new_experiment_dialog.save_series_open);
queue_save_path(harness, export_dir.join("kinetics_session.csv"));
harness.get_by_label("Save Document").click();
harness.run();
assert!(export_file.is_file());
let session_path = export_dir.join("kinetics_session.csv");
assert!(session_path.is_file());
let restored = crate::Kinetics::experimental_kinetics::experiment_series_main::TGASeries::from_csv_series(&session_path).unwrap();
let restored_columns = restored.list_of_columns(&experiment_id).unwrap();
assert!(restored_columns.contains(&"T".to_string()));
assert!(restored_columns.contains(&"m".to_string()));
let text = fs::read_to_string(&export_file).unwrap();
assert!(!text.contains("#META"));
assert!(!text.contains("KiThe TGA"));
fs::remove_dir_all(export_dir).expect("failed to remove export directory");
});
}
#[test]
fn tier2_file_manager_manage_plot_sets_dialog_open() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "File Manager", "Add Plot");
assert!(harness.state().app.new_experiment_dialog.manage_plot_open);
});
}
#[test]
fn tier2_kinetic_methods_fit_model_window_renders_controls() {
let mut model = load_model_with_one_curve();
model.series.set_experiment_id(0, "fit_model_test").unwrap();
model.plots[0].experiment_id = "fit_model_test".to_string();
model.select_curve(0);
let selected_experiment = model.get_experiment_by_selected_curve().unwrap();
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.run();
harness
.state_mut()
.app
.kinetic_methods_state
.fit_model_experiment_id = selected_experiment.clone();
click_menu_entry(harness, "Kinetic Methods", "Fit Model");
harness.run();
assert!(harness.state().app.kinetic_methods_state.fit_model_open);
assert_eq!(
harness
.state()
.app
.kinetic_methods_state
.fit_model_experiment_id,
selected_experiment
);
assert_eq!(
harness.state().app.kinetic_methods_state.fit_model_x_col,
"t"
);
assert_eq!(
harness.state().app.kinetic_methods_state.fit_model_y_col,
"T"
);
harness.get_by_label("Experiment:");
harness.get_by_label("X:");
harness.get_by_label("Y:");
harness.get_by_label("Model:");
harness.get_by_label("Formula:");
harness.get_by_label("Model equations");
harness.get_by_label("Model equations").click();
harness.run();
harness.get_by_label("DecExp");
harness.get_by_label("Output column:");
harness.get_by_label("Tolerance:");
harness.get_by_label("Max iter:");
harness.get_by_label("Initial guess (optional):");
harness.get_by_label("Fit data");
harness.get_by_label("Evaluate on a new X grid");
harness.get_by_label("Fit");
harness.get_by_label("Model equations").click();
harness.run();
{
let state = harness.state_mut();
state.app.kinetic_methods_state.fit_model_model =
FittingModelName::Polynom { n: 4 };
state.app.kinetic_methods_state.fit_model_polynomial_degree = 4;
}
harness.run();
harness.get_by_label("Polynomial degree:");
harness.get_by_label("Model:").click();
harness.run();
harness.get_by_label("Linear").click();
harness.run();
assert_eq!(
harness.state().app.kinetic_methods_state.fit_model_model,
FittingModelName::Linear
);
});
}
#[test]
fn tier3_fit_evaluate_and_column_manager_commit_are_separate_stages() {
let mut model = load_model_with_one_curve();
model.series.set_experiment_id(0, "fit_workflow").unwrap();
model.plots[0].experiment_id = "fit_workflow".to_string();
model
.create_experiment_from_columns_for_experiment(
"fit_workflow",
"another_experiment".to_string(),
&["t", "T", "m"],
)
.unwrap();
model.select_curve(0);
let source_height = model
.series
.get_experiment_by_id("fit_workflow")
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap()
.height();
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "Kinetic Methods", "Fit Model");
{
let state = &mut harness.state_mut().app.kinetic_methods_state;
state.fit_model_experiment_id = "fit_workflow".to_string();
state.fit_model_x_col = "t".to_string();
state.fit_model_y_col = "T".to_string();
state.fit_model_output_col = "fit_temperature".to_string();
state.fit_evaluation_start = 1.0e6;
state.fit_evaluation_end = 1.0e6 + 2.0;
state.fit_evaluation_points = 3;
}
harness.run();
harness.get_by_label("Model:").click();
harness.run();
harness.get_by_label("Linear").click();
harness.run();
harness.get_by_label("Fit").click();
harness.run();
let state = harness.state();
let fit = state
.app
.kinetic_methods_state
.fit_model_result
.as_ref()
.unwrap_or_else(|| {
panic!(
"Fit did not publish a result: {}",
state.app.kinetic_methods_state.fit_model_status
)
});
assert_eq!(fit.x_col, "t");
assert_eq!(fit.y_col, "T");
assert_eq!(fit.output_col, "fit_temperature");
assert!(fit.r2.is_finite());
assert!(
state
.app
.kinetic_methods_state
.fit_model_status
.starts_with("Fit added '")
);
assert!(state
.app
.kinetic_methods_state
.fit_model_status
.contains("used pairs:"));
assert!(state
.app
.kinetic_methods_state
.fit_model_status
.contains("source rows with null inputs:"));
let model = &state.app.model;
let experiment = model.series.get_experiment_by_id("fit_workflow").unwrap();
assert!(
experiment
.dataset
.schema
.columns
.contains_key("fit_temperature")
);
assert!(model.plots.iter().any(|curve| {
curve.experiment_id == "fit_workflow"
&& curve.x_name == "t"
&& curve.y_name == "fit_temperature"
&& curve.shown
&& !curve.points.is_empty()
}));
assert_eq!(state.app.kinetic_methods_state.fit_sessions.len(), 1);
{
let fit_state = &mut harness.state_mut().app.kinetic_methods_state;
fit_state.fit_pending_batch = Some(pending_fit_batch("fit_workflow"));
fit_state.fit_model_experiment_id = "another_experiment".to_string();
fit_state.fit_model_x_col = "t".to_string();
}
harness
.get_by_label("Evaluate fitted columns")
.scroll_to_me();
harness.run();
harness.get_by_label("Evaluate fitted columns").click();
harness.run();
assert!(
harness
.state()
.app
.kinetic_methods_state
.fit_pending_batch
.is_none()
);
assert!(
harness
.state()
.app
.kinetic_methods_state
.fit_model_status
.contains("no fitted model for experiment 'another_experiment'")
);
harness
.state_mut()
.app
.kinetic_methods_state
.fit_model_experiment_id = "fit_workflow".to_string();
{
let fit_state = &mut harness.state_mut().app.kinetic_methods_state;
fit_state.fit_model_y_col = "m".to_string();
fit_state.fit_model_output_col = "fit_mass".to_string();
}
harness.run();
harness.get_by_label("Fit").click();
harness.run();
assert_eq!(
harness.state().app.kinetic_methods_state.fit_sessions.len(),
2
);
harness
.get_by_label("Evaluate fitted columns")
.scroll_to_me();
harness.run();
harness.get_by_label("Evaluate fitted columns").click();
harness.run();
assert!(
harness
.state()
.app
.kinetic_methods_state
.fit_pending_batch
.is_some()
);
let experiment = harness
.state()
.app
.model
.series
.get_experiment_by_id("fit_workflow")
.unwrap();
assert!(
experiment
.dataset
.schema
.columns
.contains_key("fit_temperature")
);
assert!(experiment.dataset.schema.columns.contains_key("fit_mass"));
harness
.state_mut()
.app
.new_experiment_dialog
.column_manager_open = true;
harness.run();
harness.get_by_label("Add reconstructed experiment").click();
harness.run();
let state = harness.state();
let reconstructed = state
.app
.model
.series
.get_experiment_by_id("fit_workflow_reconstructed")
.unwrap();
let frame = reconstructed.dataset.frame.clone().collect().unwrap();
assert_eq!(frame.height(), source_height + 3);
assert_eq!(frame.width(), 3);
assert!(
frame
.columns()
.iter()
.all(|column| column.null_count() == 0)
);
assert!(
reconstructed
.dataset
.column_provenance_text("fit_temperature")
.is_some_and(|provenance| provenance.contains("extrapolation"))
);
assert!(
reconstructed
.dataset
.column_provenance_text("fit_mass")
.is_some_and(|provenance| provenance.contains("extrapolation"))
);
for y_name in ["fit_temperature", "fit_mass"] {
assert!(state.app.model.plots.iter().any(|curve| {
curve.experiment_id == "fit_workflow_reconstructed"
&& curve.x_name == "t"
&& curve.y_name == y_name
&& curve.is_shown()
}));
}
assert!(
state
.app
.model
.series
.get_experiment_by_id("fit_workflow")
.unwrap()
.dataset
.schema
.columns
.contains_key("fit_temperature")
);
});
}
#[test]
fn tier3_gui_reconstructs_a_missing_time_temperature_mass_interval() {
let source_height = 8;
let app = PlotApp::for_testing(load_model_with_recorded_gap());
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "Kinetic Methods", "Fit Model");
{
let state = &mut harness.state_mut().app.kinetic_methods_state;
state.fit_model_experiment_id = "gap_workflow".to_string();
state.fit_model_x_col = "time".to_string();
state.fit_model_y_col = "temperature".to_string();
state.fit_model_output_col = "temperature_rebuilt".to_string();
state.fit_gap_enabled = true;
state.fit_gap_start = 3.0;
state.fit_gap_end = 7.0;
state.fit_evaluation_points = 3;
}
harness.run();
harness.get_by_label("Model:").click();
harness.run();
harness.get_by_label("Linear").click();
harness.run();
harness.get_by_label("Fit").click();
harness.run();
assert!(
harness
.state()
.app
.kinetic_methods_state
.fit_model_result
.as_ref()
.unwrap()
.r2
> 0.999999
);
{
let state = &mut harness.state_mut().app.kinetic_methods_state;
state.fit_model_y_col = "mass".to_string();
state.fit_model_output_col = "mass_rebuilt".to_string();
}
harness.run();
harness.get_by_label("Fit").click();
harness.run();
assert_eq!(
harness.state().app.kinetic_methods_state.fit_sessions.len(),
2
);
assert!(
harness
.state()
.app
.model
.series
.get_experiment_by_id("gap_workflow")
.unwrap()
.dataset
.schema
.columns
.contains_key("mass_rebuilt")
);
assert!(harness.state().app.model.plots.iter().any(|curve| {
curve.experiment_id == "gap_workflow"
&& curve.y_name == "mass_rebuilt"
&& curve.shown
&& !curve.points.is_empty()
}));
harness
.get_by_label("Evaluate fitted columns")
.scroll_to_me();
harness.run();
harness.get_by_label("Evaluate fitted columns").click();
harness.run();
let batch = harness
.state()
.app
.kinetic_methods_state
.fit_pending_batch
.as_ref()
.unwrap();
assert_eq!(batch.columns.len(), 2);
assert_eq!(
batch
.x_values
.iter()
.filter(|x| **x > 3.0 && **x < 7.0)
.count(),
3
);
harness
.state_mut()
.app
.new_experiment_dialog
.column_manager_open = true;
harness.run();
harness.get_by_label("Add reconstructed experiment").click();
harness.run();
let state = harness.state();
let source = state
.app
.model
.series
.get_experiment_by_id("gap_workflow")
.unwrap();
assert_eq!(
source.dataset.frame.clone().collect().unwrap().height(),
source_height
);
assert!(
source
.dataset
.schema
.columns
.contains_key("temperature_rebuilt")
);
let reconstructed = state
.app
.model
.series
.get_experiment_by_id("gap_workflow_reconstructed")
.unwrap();
let frame = reconstructed.dataset.frame.clone().collect().unwrap();
assert_eq!(frame.height(), source_height + 3);
assert_eq!(frame.width(), 3);
assert!(
frame
.columns()
.iter()
.all(|column| column.null_count() == 0)
);
let times = frame.column("time").unwrap().f64().unwrap();
let temperature = frame.column("temperature_rebuilt").unwrap().f64().unwrap();
let mass = frame.column("mass_rebuilt").unwrap().f64().unwrap();
for (index, expected_t) in [(4, 316.0), (5, 320.0), (6, 324.0)] {
assert!((times.get(index).unwrap() - (index as f64 + 0.0)).abs() < 1e-10);
assert!((temperature.get(index).unwrap() - expected_t).abs() < 1e-8);
assert!((mass.get(index).unwrap() - (12.0 - 0.5 * index as f64)).abs() < 1e-8);
}
assert!(
reconstructed
.dataset
.column_provenance_text("temperature_rebuilt")
.is_some_and(|text| text.contains("gap [3"))
);
harness.get_by_label("Experiment").click();
harness.run();
harness.get_by_label("gap_workflow_reconstructed").click();
harness.run();
harness
.get_by_label("Delete experiment: gap_workflow_reconstructed")
.click();
harness.run();
harness.get_by_label("Yes").click();
harness.run();
assert!(
harness
.state()
.app
.model
.series
.get_experiment_by_id("gap_workflow_reconstructed")
.is_err()
);
assert!(
harness
.state()
.app
.model
.series
.get_experiment_by_id("gap_workflow")
.is_ok()
);
});
}
#[test]
fn tier2_quick_action_create_plots_opens_dialog() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("Create Plots").click();
harness.run();
assert!(harness.state().app.new_experiment_dialog.manage_plot_open);
});
}
#[test]
fn tier3_column_manager_shows_samples_and_selected_column_history() {
let mut model = load_model_with_one_curve();
model
.series
.set_experiment_id(0, "column_details_test")
.unwrap();
model.plots[0].experiment_id = "column_details_test".to_string();
model.select_curve(0);
model
.sg_filter_column_for_selected_as("T", 5, 2, 0, 1.0, Some("sg_T"))
.expect("failed to prepare a quality report");
assert!(model
.series
.get_experiment_by_id("column_details_test")
.unwrap()
.dataset
.column_provenance("sg_T")
.unwrap()
.steps
.last()
.unwrap()
.coverage
.is_some());
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.set_size(egui::vec2(1400.0, 1000.0));
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
harness.get_by_label("Source row");
harness.get_by_label("Rows 1-100 (source 0-99) | more rows available");
harness.get_by_label("Column details");
harness
.get_by_role_and_label(egui::accesskit::Role::Button, "sg_T")
.scroll_to_me();
harness.run();
harness
.get_by_role_and_label(egui::accesskit::Role::Button, "sg_T")
.click();
for _ in 0..5 {
if harness.query_by_label("Overview").is_some() {
break;
}
harness.run();
}
assert!(harness.query_by_label("Overview").is_some());
assert!(harness.query_by_label_contains("Schema binding:").is_some());
assert!(
harness
.query_by_label_contains("Calculation source:")
.is_some()
);
harness.get_by_label("Processing").click();
harness.run();
harness.get_by_label("Provenance");
harness.get_by_label("Quality").click();
harness.run();
assert!(
harness
.query_by_label_contains("Quality: savitzky_golay(window=5")
.is_some()
);
harness.get_by_label("RMSE");
harness.get_by_label("Normalized RMSE");
harness.get_by_label("Correlation");
harness.get_by_label("First-difference roughness ratio");
harness.get_by_label("Second-difference roughness ratio");
assert!(harness.query_by_label_contains("Data coverage:").is_some());
harness.get_by_label("These metrics describe change relative to the comparison column; they do not establish accuracy against a true signal.");
harness.get_by_label("History").click();
harness.run();
harness.get_by_label("Operation history");
});
}
#[test]
fn tier3_column_manager_pages_through_all_source_rows() {
let time = (0..205).map(|row| row as f64).collect::<Vec<_>>();
let model = {
let mut model = PlotModel::new();
model
.series
.create_from_synthetic_data(
&crate::Kinetics::experimental_kinetics::testing_mod::VirtualTGA {
time: time.clone(),
temperature: time.iter().map(|t| 300.0 + t).collect(),
mass: time.iter().map(|t| 1000.0 - t * 0.01).collect(),
},
crate::Kinetics::experimental_kinetics::experiment_series_main::ExperimentMeta {
id: "paging_test".to_string(),
..Default::default()
},
)
.unwrap();
model.set_x("paging_test", "time").unwrap();
model.set_y("paging_test", "mass").unwrap();
model.create_points_for_curve("paging_test").unwrap();
model
};
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.set_size(egui::vec2(1400.0, 1000.0));
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
harness.get_by_label("Rows 1-100 (source 0-99) | more rows available");
harness.get_by_label("0.0000");
harness.get_by_label("1000.0000");
harness
.state_mut()
.app
.model
.scale_column("paging_test", "mass", 2.0)
.unwrap();
harness.run();
harness.get_by_label("2000.0000");
harness.get_by_label("Next page").click();
harness.run();
harness.get_by_label("Rows 101-200 (source 100-199) | more rows available");
harness.get_by_label("100.0000");
harness.get_by_label("Next page").click();
harness.run();
harness.get_by_label("Rows 201-205 (source 200-204) | end of data");
harness.get_by_label("200.0000");
harness.get_by_label("Previous page").click();
harness.run();
harness.get_by_label("Rows 101-200 (source 100-199) | more rows available");
harness.get_by_label("First page").click();
harness.run();
harness.get_by_label("Rows 1-100 (source 0-99) | more rows available");
});
}
#[test]
fn tier3_column_manager_search_navigates_matches_across_pages() {
let time = (0..205).map(|row| row as f64).collect::<Vec<_>>();
let mass = time
.iter()
.map(|t| {
if *t == 0.0 || *t == 100.0 {
999.0
} else {
1000.0 - t * 0.01
}
})
.collect::<Vec<_>>();
let mut model = PlotModel::new();
model
.series
.create_from_synthetic_data(
&crate::Kinetics::experimental_kinetics::testing_mod::VirtualTGA {
time: time.clone(),
temperature: time.iter().map(|t| 300.0 + t).collect(),
mass,
},
crate::Kinetics::experimental_kinetics::experiment_series_main::ExperimentMeta {
id: "search_test".to_string(),
..Default::default()
},
)
.unwrap();
model.set_x("search_test", "time").unwrap();
model.set_y("search_test", "mass").unwrap();
model.create_points_for_curve("search_test").unwrap();
with_plot_app_state_harness(PlotApp::for_testing(model), |harness| {
harness.set_size(egui::vec2(1400.0, 1000.0));
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
harness
.get_by_role_and_label(egui::accesskit::Role::Button, "mass")
.click();
harness.get_by_label("Target").focus();
harness.get_by_label("Target").type_text("999");
harness.get_by_label("Tolerance").focus();
harness.get_by_label("Tolerance").type_text("0.000001");
harness.run();
assert_eq!(
harness.get_by_label("Target").value().as_deref(),
Some("999")
);
harness.get_by_label("Find next").click();
harness.run();
harness.get_by_label("Found mass = 999.0000 at source row 0 (display row 1).");
harness.get_by_label("Find next").click();
harness.run();
harness.get_by_label("Found mass = 999.0000 at source row 100 (display row 101).");
harness.get_by_label("Rows 101-200 (source 100-199) | more rows available");
harness.get_by_label("Find previous").click();
harness.run();
harness.get_by_label("Found mass = 999.0000 at source row 0 (display row 1).");
harness.get_by_label("Rows 1-100 (source 0-99) | more rows available");
harness.get_by_label("Find previous").click();
harness.run();
harness.get_by_label(
"No previous match; beginning of data reached (search does not wrap).",
);
});
}
#[test]
fn tier3_column_manager_deletes_only_the_confirmed_found_row_and_can_undo() {
let time = (0..205).map(|row| row as f64).collect::<Vec<_>>();
let mut model = PlotModel::new();
model
.series
.create_from_synthetic_data(
&crate::Kinetics::experimental_kinetics::testing_mod::VirtualTGA {
time: time.clone(),
temperature: time.iter().map(|t| 300.0 + t).collect(),
mass: time.iter().map(|t| 1000.0 - t * 0.01).collect(),
},
crate::Kinetics::experimental_kinetics::experiment_series_main::ExperimentMeta {
id: "row_delete_test".to_string(),
..Default::default()
},
)
.unwrap();
model.set_x("row_delete_test", "time").unwrap();
model.set_y("row_delete_test", "mass").unwrap();
model.create_points_for_curve("row_delete_test").unwrap();
with_plot_app_state_harness(PlotApp::for_testing(model), |harness| {
harness.set_size(egui::vec2(1400.0, 1000.0));
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
harness
.get_by_role_and_label(egui::accesskit::Role::Button, "mass")
.click();
harness.get_by_label("Target").focus();
harness.get_by_label("Target").type_text("999");
harness.get_by_label("Tolerance").focus();
harness.get_by_label("Tolerance").type_text("0.000001");
harness.run();
harness.get_by_label("Find next").click();
harness.run();
harness.get_by_label("Found mass = 999.0000 at source row 100 (display row 101).");
harness.get_by_label("Delete found row...").click();
harness.run();
harness.get_by_label("Delete the complete data row?");
harness.get_by_label("This removes that row from every column in the experiment. Undo is available from the action bar.");
assert_eq!(
harness
.state()
.app
.model
.series
.get_experiment_by_id("row_delete_test")
.unwrap()
.dataset
.frame()
.clone()
.collect()
.unwrap()
.height(),
205
);
harness.get_by_label("Cancel").click();
harness.run();
assert_eq!(
harness
.state()
.app
.model
.series
.get_experiment_by_id("row_delete_test")
.unwrap()
.dataset
.frame()
.clone()
.collect()
.unwrap()
.height(),
205
);
harness.get_by_label("Delete found row...").click();
harness.run();
harness.get_by_label("Delete entire row").click();
harness.run();
let model = &harness.state().app.model;
let experiment = model
.series
.get_experiment_by_id("row_delete_test")
.unwrap();
let frame = experiment.dataset.frame().clone().collect().unwrap();
assert_eq!(frame.height(), 204);
assert_eq!(
frame.column("time").unwrap().f64().unwrap().get(100),
Some(101.0)
);
assert_eq!(
frame.column("temperature").unwrap().f64().unwrap().get(100),
Some(401.0)
);
assert_eq!(
frame.column("mass").unwrap().f64().unwrap().get(100),
Some(998.99)
);
assert!(
experiment
.dataset
.history_of_operations
.feed_text()
.contains("delete_rows_by_index")
);
assert_eq!(model.plots[0].points.len(), 204);
assert!(model.message.contains("Undo is available"));
harness
.state_mut()
.app
.model
.reverse_experiment("row_delete_test")
.unwrap();
harness.run();
assert_eq!(
harness
.state()
.app
.model
.series
.get_experiment_by_id("row_delete_test")
.unwrap()
.dataset
.frame()
.clone()
.collect()
.unwrap()
.height(),
205
);
assert_eq!(harness.state().app.model.plots[0].points.len(), 205);
harness.get_by_label("Find next").click();
harness.run();
harness.get_by_label("Delete found row...").click();
harness.run();
harness
.state_mut()
.app
.model
.scale_column("row_delete_test", "mass", 2.0)
.unwrap();
harness.run();
harness.get_by_label("Delete entire row").click();
harness.run();
let model = &harness.state().app.model;
let experiment = model
.series
.get_experiment_by_id("row_delete_test")
.unwrap();
assert_eq!(
experiment
.dataset
.frame()
.clone()
.collect()
.unwrap()
.height(),
205
);
assert!(
model
.message
.contains("Data changed while confirmation was open")
);
});
}
#[test]
fn tier3_column_manager_replaces_only_the_found_cell_and_can_undo() {
let time = (0..8).map(|row| row as f64).collect::<Vec<_>>();
let mut model = PlotModel::new();
model
.series
.create_from_synthetic_data(
&crate::Kinetics::experimental_kinetics::testing_mod::VirtualTGA {
time: time.clone(),
temperature: time.iter().map(|t| 300.0 + t).collect(),
mass: vec![1000.0, 900.0, 800.0, 700.0, 600.0, 500.0, 400.0, 300.0],
},
crate::Kinetics::experimental_kinetics::experiment_series_main::ExperimentMeta {
id: "cell_replace_test".to_string(),
..Default::default()
},
)
.unwrap();
model.set_x("cell_replace_test", "time").unwrap();
model.set_y("cell_replace_test", "mass").unwrap();
model.create_points_for_curve("cell_replace_test").unwrap();
with_plot_app_state_harness(PlotApp::for_testing(model), |harness| {
harness.set_size(egui::vec2(1400.0, 1000.0));
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
harness
.get_by_role_and_label(egui::accesskit::Role::Button, "mass")
.click();
harness.get_by_label("Target").focus();
harness.get_by_label("Target").type_text("700");
harness.get_by_label("Tolerance").focus();
harness.get_by_label("Tolerance").type_text("0");
harness.run();
harness.get_by_label("Find next").click();
harness.run();
harness.get_by_label("Found mass = 700.0000 at source row 3 (display row 4).");
harness.get_by_label("Replace found value...").click();
harness.run();
harness.get_by_label("mass at source row 3 (display row 4) currently equals 700.0000");
harness.get_by_label("New value").focus();
harness.get_by_label("New value").type_text("725");
harness.run();
harness.get_by_label("Cancel").click();
harness.run();
let frame = harness
.state()
.app
.model
.series
.get_experiment_by_id("cell_replace_test")
.unwrap()
.dataset
.frame()
.clone()
.collect()
.unwrap();
assert_eq!(
frame.column("mass").unwrap().f64().unwrap().get(3),
Some(700.0)
);
harness.get_by_label("Replace found value...").click();
harness.run();
harness.get_by_label("New value").focus();
harness.get_by_label("New value").type_text("725");
harness.run();
harness.get_by_label("Replace cell").click();
harness.run();
let model = &harness.state().app.model;
let experiment = model
.series
.get_experiment_by_id("cell_replace_test")
.unwrap();
let frame = experiment.dataset.frame().clone().collect().unwrap();
assert_eq!(
frame.column("mass").unwrap().f64().unwrap().get(3),
Some(725.0)
);
assert_eq!(
frame.column("mass").unwrap().f64().unwrap().get(2),
Some(800.0)
);
assert_eq!(
frame.column("time").unwrap().f64().unwrap().get(3),
Some(3.0)
);
assert_eq!(
frame.column("temperature").unwrap().f64().unwrap().get(3),
Some(303.0)
);
assert!(
experiment
.dataset
.history_of_operations
.feed_text()
.contains("replace_numeric_cell_at_row")
);
assert_eq!(model.plots[0].points.len(), 8);
assert!(model.message.contains("Undo is available"));
harness
.state_mut()
.app
.model
.reverse_experiment("cell_replace_test")
.unwrap();
harness.run();
let frame = harness
.state()
.app
.model
.series
.get_experiment_by_id("cell_replace_test")
.unwrap()
.dataset
.frame()
.clone()
.collect()
.unwrap();
assert_eq!(
frame.column("mass").unwrap().f64().unwrap().get(3),
Some(700.0)
);
});
}
#[test]
fn tier3_column_manager_copies_full_column_without_mutating_data() {
let time = (0..6).map(|row| row as f64).collect::<Vec<_>>();
let mut model = PlotModel::new();
model
.series
.create_from_synthetic_data(
&crate::Kinetics::experimental_kinetics::testing_mod::VirtualTGA {
time: time.clone(),
temperature: time.iter().map(|t| 300.0 + t).collect(),
mass: vec![10.0, 9.0, 8.0, 7.0, 6.0, 5.0],
},
crate::Kinetics::experimental_kinetics::experiment_series_main::ExperimentMeta {
id: "column_copy_test".to_string(),
..Default::default()
},
)
.unwrap();
model.set_x("column_copy_test", "time").unwrap();
model.set_y("column_copy_test", "mass").unwrap();
model.create_points_for_curve("column_copy_test").unwrap();
with_plot_app_state_harness(PlotApp::for_testing(model), |harness| {
harness.set_size(egui::vec2(1400.0, 1000.0));
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
harness
.get_by_role_and_label(egui::accesskit::Role::Button, "mass")
.click();
harness.run();
harness.get_by_label("Copy full column").click();
harness.run();
assert!(
harness
.state()
.app
.model
.message
.contains("Copied 6 values")
);
let experiment = harness
.state()
.app
.model
.series
.get_experiment_by_id("column_copy_test")
.unwrap();
assert_eq!(
experiment
.dataset
.frame()
.clone()
.collect()
.unwrap()
.height(),
6
);
assert_eq!(harness.state().app.model.plots[0].points.len(), 6);
});
}
#[test]
fn tier3_column_manager_runs_and_invalidates_cached_quality_diagnostics() {
let time = (0..6).map(|row| row as f64).collect::<Vec<_>>();
let mut model = PlotModel::new();
model
.series
.create_from_synthetic_data(
&crate::Kinetics::experimental_kinetics::testing_mod::VirtualTGA {
time: time.clone(),
temperature: time.iter().map(|t| 300.0 + t).collect(),
mass: vec![10.0, 9.0, 8.0, 7.0, 6.0, 5.0],
},
crate::Kinetics::experimental_kinetics::experiment_series_main::ExperimentMeta {
id: "quality_diagnostics_test".to_string(),
..Default::default()
},
)
.unwrap();
model.set_x("quality_diagnostics_test", "time").unwrap();
model.set_y("quality_diagnostics_test", "mass").unwrap();
model
.create_points_for_curve("quality_diagnostics_test")
.unwrap();
with_plot_app_state_harness(PlotApp::for_testing(model), |harness| {
harness.set_size(egui::vec2(1400.0, 1000.0));
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
harness
.get_by_role_and_label(egui::accesskit::Role::Button, "mass")
.click();
harness.run();
harness.get_by_label("Quality").click();
harness.run();
harness.get_by_label("Run data diagnostics").click();
harness.run();
harness.get_by_label("6 rows | finite: 6 | null: 0 | NaN: 0 | infinite: 0");
harness.get_by_label("Finite range: 5.0000 to 10.0000");
harness
.state_mut()
.app
.model
.scale_column("quality_diagnostics_test", "mass", 2.0)
.unwrap();
harness.run();
harness.get_by_label(
"Run diagnostics to inspect all source rows. Results refresh after the next data/schema change.",
);
harness.get_by_label("Run data diagnostics").click();
harness.run();
harness.get_by_label("Finite range: 10.0000 to 20.0000");
});
}
#[test]
fn tier3_column_manager_warns_before_dropping_referenced_columns() {
let time = (0..6).map(|row| row as f64).collect::<Vec<_>>();
let mut model = PlotModel::new();
let synthetic = crate::Kinetics::experimental_kinetics::testing_mod::VirtualTGA {
time: time.clone(),
temperature: time.iter().map(|t| 300.0 + t).collect(),
mass: vec![10.0, 9.0, 8.0, 7.0, 6.0, 5.0],
};
let mut dataset = crate::Kinetics::experimental_kinetics::one_experiment_dataset::TGADataset::create_from_synthetic_data(&synthetic)
.unwrap();
dataset = dataset
.add_column_from_vec(
"mass_sg",
Unit::Milligram,
crate::Kinetics::experimental_kinetics::one_experiment_dataset::ColumnNature::Mass,
vec![10.0, 9.0, 8.0, 7.0, 6.0, 5.0],
)
.unwrap();
dataset
.schema
.columns
.get_mut("mass_sg")
.unwrap()
.provenance
.root_columns = vec!["mass".to_string()];
model
.series
.push(
crate::Kinetics::experimental_kinetics::experiment_series_main::TGAExperiment::new(
dataset,
)
.with_id("drop_warning_test"),
)
.unwrap();
model.set_x("drop_warning_test", "time").unwrap();
model.set_y("drop_warning_test", "mass").unwrap();
model.create_points_for_curve("drop_warning_test").unwrap();
model.set_active_mass_source_for_experiment("drop_warning_test", "mass");
with_plot_app_state_harness(PlotApp::for_testing(model), |harness| {
harness.set_size(egui::vec2(1400.0, 1000.0));
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
harness
.state_mut()
.app
.column_manager_state
.select_column_for_test("drop_warning_test", "mass");
harness.run();
harness.get_by_label("Delete columns (1)").click();
harness.run();
harness.get_by_label("Schema binding: mass");
harness.get_by_label("Active calculation source: mass");
harness.get_by_label("Derived descendants retained: mass_sg");
assert!(
harness
.query_by_label_contains("Used by plots: Y axis")
.is_some()
);
assert!(
harness
.query_by_label_contains("Plots using deleted columns will be removed")
.is_some()
);
assert!(
harness
.state()
.app
.model
.series
.get_experiment_by_id("drop_warning_test")
.unwrap()
.dataset
.schema
.mass
.is_some()
);
harness.get_by_label("Cancel").click();
harness.run();
harness.get_by_label("Delete columns (1)");
harness.get_by_label("Delete columns (1)").click();
harness.run();
harness.get_by_label("Delete columns").click();
harness.run();
let model = &harness.state().app.model;
let dataset = &model
.series
.get_experiment_by_id("drop_warning_test")
.unwrap()
.dataset;
assert!(dataset.schema.mass.is_none());
assert!(dataset.schema.columns.contains_key("mass_sg"));
assert!(
dataset
.frame()
.clone()
.collect()
.unwrap()
.column("mass_sg")
.is_ok()
);
assert!(model.plots.is_empty());
});
}
#[test]
fn tier3_column_manager_can_plot_selected_column_without_changing_sources() {
let time = (0..8).map(|row| row as f64).collect::<Vec<_>>();
let synthetic = crate::Kinetics::experimental_kinetics::testing_mod::VirtualTGA {
time: time.clone(),
temperature: time.iter().map(|t| 300.0 + 2.0 * t).collect(),
mass: time.iter().map(|t| 10.0 - 0.25 * t).collect(),
};
let dataset = crate::Kinetics::experimental_kinetics::one_experiment_dataset::TGADataset::create_from_synthetic_data(&synthetic)
.unwrap();
let mut model = PlotModel::new();
model
.series
.push(
crate::Kinetics::experimental_kinetics::experiment_series_main::TGAExperiment::new(
dataset,
)
.with_id("column_plot_test"),
)
.unwrap();
model.set_x("column_plot_test", "time").unwrap();
model.set_y("column_plot_test", "mass").unwrap();
model.create_points_for_curve("column_plot_test").unwrap();
model.set_active_mass_source_for_experiment("column_plot_test", "mass");
let before_schema = {
let dataset = &model
.series
.get_experiment_by_id("column_plot_test")
.unwrap()
.dataset;
(dataset.schema.time.clone(), dataset.schema.mass.clone())
};
let original_axes = (
model
.series
.oneframeplot_axis_name(
"column_plot_test",
crate::Kinetics::experimental_kinetics::exp_engine_api::XY::X,
)
.unwrap(),
model
.series
.oneframeplot_axis_name(
"column_plot_test",
crate::Kinetics::experimental_kinetics::exp_engine_api::XY::Y,
)
.unwrap(),
);
with_plot_app_state_harness(PlotApp::for_testing(model), |harness| {
harness.set_size(egui::vec2(1400.0, 1000.0));
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
harness
.state_mut()
.app
.column_manager_state
.select_inspector_column_for_test("column_plot_test", "temperature");
harness.run();
harness.get_by_label("Show on plot").click();
harness.run();
let model = &harness.state().app.model;
assert!(model.plots.iter().any(|curve| {
curve.experiment_id == "column_plot_test"
&& curve.x_name == "time"
&& curve.y_name == "temperature"
&& curve.shown
}));
assert_eq!(model.plots.len(), 2);
assert_eq!(
model
.active_mass_source_for_experiment("column_plot_test")
.unwrap(),
"mass"
);
assert_eq!(
model
.series
.oneframeplot_axis_name(
"column_plot_test",
crate::Kinetics::experimental_kinetics::exp_engine_api::XY::X
)
.unwrap(),
original_axes.0
);
assert_eq!(
model
.series
.oneframeplot_axis_name(
"column_plot_test",
crate::Kinetics::experimental_kinetics::exp_engine_api::XY::Y
)
.unwrap(),
original_axes.1
);
let dataset = &model
.series
.get_experiment_by_id("column_plot_test")
.unwrap()
.dataset;
assert_eq!(
(dataset.schema.time.clone(), dataset.schema.mass.clone()),
before_schema
);
harness.get_by_label("Show on plot").click();
harness.run();
assert_eq!(harness.state().app.model.plots.len(), 2);
});
}
#[test]
fn tier3_column_manager_zooms_graph_to_selected_source_row_interval() {
let time = (0..8).map(|row| row as f64).collect::<Vec<_>>();
let synthetic = crate::Kinetics::experimental_kinetics::testing_mod::VirtualTGA {
time: time.clone(),
temperature: time.iter().map(|t| 300.0 + 2.0 * t).collect(),
mass: time.iter().map(|t| 10.0 - 0.25 * t).collect(),
};
let dataset = crate::Kinetics::experimental_kinetics::one_experiment_dataset::TGADataset::create_from_synthetic_data(&synthetic)
.unwrap();
let mut model = PlotModel::new();
model
.series
.push(
crate::Kinetics::experimental_kinetics::experiment_series_main::TGAExperiment::new(
dataset,
)
.with_id("column_interval_test"),
)
.unwrap();
model.set_x("column_interval_test", "time").unwrap();
model.set_y("column_interval_test", "mass").unwrap();
model
.create_points_for_curve("column_interval_test")
.unwrap();
let full_view = model.interaction.view_range;
with_plot_app_state_harness(PlotApp::for_testing(model), |harness| {
harness.set_size(egui::vec2(1400.0, 1000.0));
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
{
let state = &mut harness.state_mut().app.column_manager_state;
state.select_inspector_column_for_test("column_interval_test", "mass");
state.select_source_row_interval_for_test("column_interval_test", 2, 5);
}
harness.run();
harness.get_by_label("Selected source rows: 3-6");
harness.get_by_label("Zoom plot to interval").click();
harness.run();
let model = &harness.state().app.model;
assert!(model.interaction.view_range.0 > full_view.0);
assert!(model.interaction.view_range.1 < full_view.1);
assert!((model.interaction.view_range.0 - 1.85).abs() < 1e-9);
assert!((model.interaction.view_range.1 - 5.15).abs() < 1e-9);
assert_eq!(model.plots.len(), 1);
assert_eq!(
model
.series
.get_experiment_by_id("column_interval_test")
.unwrap()
.dataset
.schema
.time
.as_deref(),
Some("time")
);
assert!(model.message.contains("source rows 3-6"));
});
}
#[test]
fn tier3_column_manager_compares_processed_columns_against_explicit_reference() {
let time = (0..8).map(|row| row as f64).collect::<Vec<_>>();
let mass = vec![10.0, 9.0, 8.2, 7.0, 6.3, 5.0, 4.5, 4.0];
let synthetic = crate::Kinetics::experimental_kinetics::testing_mod::VirtualTGA {
time: time.clone(),
temperature: time.iter().map(|t| 300.0 + 2.0 * t).collect(),
mass: mass.clone(),
};
let mut dataset = crate::Kinetics::experimental_kinetics::one_experiment_dataset::TGADataset::create_from_synthetic_data(&synthetic)
.unwrap();
let processed = mass
.iter()
.enumerate()
.map(|(index, value)| value + if index % 2 == 0 { 0.04 } else { -0.04 })
.collect();
dataset = dataset
.add_column_from_vec("mass_sg", Unit::Milligram, ColumnNature::Mass, processed)
.unwrap();
let mut model = PlotModel::new();
model
.series
.push(
crate::Kinetics::experimental_kinetics::experiment_series_main::TGAExperiment::new(
dataset,
)
.with_id("column_compare_test"),
)
.unwrap();
let original_values = model
.series
.get_experiment_by_id("column_compare_test")
.unwrap()
.dataset
.column_as_array1("mass_sg")
.unwrap();
let original_history_len = model
.series
.get_experiment_by_id("column_compare_test")
.unwrap()
.dataset
.history_of_operations
.vector_of_operations
.len();
with_plot_app_state_harness(PlotApp::for_testing(model), |harness| {
harness.set_size(egui::vec2(1400.0, 1000.0));
harness.run();
harness.get_by_label("Column Manager").click();
harness.run();
harness
.state_mut()
.app
.column_manager_state
.select_inspector_column_for_test("column_compare_test", "mass_sg");
harness
.state_mut()
.app
.column_manager_state
.configure_comparison_for_test("mass", &["mass_sg"]);
harness.run();
harness.get_by_label("Compare columns").click();
harness.run();
harness.get_by_label("Reference: column(mass) | common valid rows: 8");
harness.get_by_label_contains(
"The reference is a comparison choice, not a known true signal",
);
assert!(harness.query_by_label("RMSE").is_some());
assert!(harness.query_by_label("Normalized RMSE").is_some());
assert!(harness.query_by_label("Correlation").is_some());
assert!(harness.query_by_label("First differences").is_some());
assert!(harness.query_by_label("Second differences").is_some());
harness.get_by_label("mass_sg [Milligram]").click();
harness.run();
assert!(
harness
.query_by_label("Reference: column(mass) | common valid rows: 8")
.is_none()
);
harness.get_by_label("mass_sg [Milligram]").click();
harness.run();
harness.get_by_label("Compare columns").click();
harness.run();
harness.get_by_label("Reference: column(mass) | common valid rows: 8");
let experiment = harness
.state()
.app
.model
.series
.get_experiment_by_id("column_compare_test")
.unwrap();
assert_eq!(
experiment.dataset.column_as_array1("mass_sg").unwrap(),
original_values
);
assert_eq!(
experiment
.dataset
.history_of_operations
.vector_of_operations
.len(),
original_history_len
);
harness
.state_mut()
.app
.model
.add_column_from_vec(
"column_compare_test",
"mass_extra",
Unit::Milligram,
ColumnNature::Mass,
vec![10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0],
)
.unwrap();
harness.run();
assert!(
harness
.state()
.app
.model
.message
.contains("Comparison report invalidated because data or schema changed")
);
assert!(
harness
.query_by_label("Reference: column(mass) | common valid rows: 8")
.is_none()
);
});
}
#[test]
fn tier2_quick_action_clear_selected_clears_selection_state() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
{
let state = harness.state_mut();
state.app.model.select_curve(0);
state.app.model.start_selection([0.0, 0.0]);
state.app.model.update_selection([1.0, 1.0]);
state.app.model.end_selection();
}
harness.run();
harness.get_by_label("Clear Selected").click();
harness.run();
let model = &mut harness.state_mut().app.model;
model.clear_selection_rect();
model.clear_selection();
assert!(model.interaction.selection_rect.is_none());
assert!(!model.interaction.is_selecting);
assert_eq!(model.get_selected_curve_index(), None);
});
}
#[test]
fn source_selection_remaps_after_column_transformation() {
let mut model = load_model_with_one_curve();
model.select_curve(0);
let exp_id = model.list_of_experiments()[0].clone();
model.set_active_mass_source_for_experiment(&exp_id, "m");
model.set_active_temperature_source_for_experiment(&exp_id, "T");
model
.sg_filter_column_for_selected_as("m", 5, 2, 0, 1.0, Some("m_sg"))
.expect("mass smoothing should succeed");
model
.sg_filter_column_for_selected_as("T", 5, 2, 0, 1.0, Some("T_sg"))
.expect("temperature smoothing should succeed");
assert_eq!(
model
.active_mass_source_for_experiment(&exp_id)
.expect("mass source should be tracked"),
"m_sg"
);
assert_eq!(
model
.active_temperature_source_for_experiment(&exp_id)
.expect("temperature source should be tracked"),
"T_sg"
);
}
#[test]
fn tier3_selected_temperature_smoothing_feeds_temperature_rate() {
let mut model = load_model_with_one_curve();
model.select_curve(0);
let exp_id = model.list_of_experiments()[0].clone();
model.set_active_mass_source_for_experiment(&exp_id, "m");
model.set_active_temperature_source_for_experiment(&exp_id, "T");
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
let id = harness.state().app.model.list_of_experiments()[0].clone();
let before_time = harness
.state()
.app
.model
.series
.get_experiment_by_id(&id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap()
.column("t")
.unwrap()
.f64()
.unwrap()
.into_no_null_iter()
.collect::<Vec<_>>();
harness.run();
click_menu_entry(harness, "Math", " Savitzky Golay");
assert!(
harness
.query_by_label_contains("Input-grid width:")
.is_some(),
"SG window should show the time-equivalent of its sample window"
);
harness.get_by_label("Apply").click();
harness.run();
let model = &harness.state().app.model;
let columns = model.list_of_columns(&id).unwrap();
assert!(
columns.contains(&"sg_T".to_string()),
"SG should default to the selected Y column; columns={columns:?}, message={}",
model.message
);
assert_eq!(
model.active_temperature_source_for_experiment(&id).unwrap(),
"sg_T",
"the auto-named filtered output should become the active temperature source"
);
let after_filter = model
.series
.get_experiment_by_id(&id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap();
let after_time = after_filter
.column("t")
.unwrap()
.f64()
.unwrap()
.into_no_null_iter()
.collect::<Vec<_>>();
assert_eq!(before_time, after_time, "filtering T must not transform t");
assert!(
model
.plots
.iter()
.any(|curve| curve.experiment_id == id && curve.y_name == "sg_T"),
"the new filtered temperature curve should be plotted immediately"
);
harness.get_by_label("T rate").click();
harness.run();
let after_rate = harness
.state()
.app
.model
.series
.get_experiment_by_id(&id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap();
assert!(after_rate.column("dT_dt").is_ok());
});
}
#[test]
fn tier3_hampel_rolling_and_spline_filters_create_visible_outputs() {
assert_filter_output_via_ui("Hampel filter", "hampel_T");
assert_filter_output_via_ui("rolling mean", "rolling_T");
assert_filter_output_via_ui("Splines", "spline_T");
assert_filter_output_via_ui("LSQ Splines", "lsq_spline_T");
}
#[test]
fn tier3_lowess_filter_creates_visible_output_for_selected_column() {
let mut model = load_model_with_one_curve();
model.select_curve(0);
let experiment_id = model.list_of_experiments()[0].clone();
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "Math", "LOWESS");
harness
.get_by_role_and_label(egui::accesskit::Role::CheckBox, "T")
.click();
harness.run();
harness.get_by_label("Apply").click();
harness.run();
let model = &harness.state().app.model;
assert!(
model
.list_of_columns(&experiment_id)
.unwrap()
.contains(&"lowess_T".into())
);
assert!(model.plots.iter().any(|curve| {
curve.experiment_id == experiment_id && curve.y_name == "lowess_T"
}));
});
}
#[test]
fn tier2_savitzky_golay_without_selected_curve_reports_error_without_mutation() {
let app = PlotApp::for_testing(PlotModel::new());
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "Math", " Savitzky Golay");
harness.get_by_label("Apply").click();
harness.run();
let model = &harness.state().app.model;
assert!(!model.message.is_empty());
assert!(model.series.ids().is_empty());
assert!(model.plots.is_empty());
});
}
#[test]
fn tier3_selected_mass_smoothing_preserves_temperature_curve() {
let mut model = load_model_with_two_curves();
let id = model.list_of_experiments()[0].clone();
let mass_curve_index = model
.plots
.iter()
.position(|curve| curve.y_name == "m")
.expect("mass curve should be present");
let temperature_curve_before = model
.plots
.iter()
.find(|curve| curve.y_name == "T")
.expect("temperature curve should be present")
.points
.clone();
model.select_curve(mass_curve_index);
model.set_active_mass_source_for_experiment(&id, "m");
model.set_active_temperature_source_for_experiment(&id, "T");
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
click_menu_entry(harness, "Math", " Savitzky Golay");
harness.get_by_label("Apply").click();
harness.run();
let model = &harness.state().app.model;
assert!(model.list_of_columns(&id).unwrap().contains(&"sg_m".into()));
assert_eq!(
model.active_mass_source_for_experiment(&id).unwrap(),
"sg_m"
);
let temperature_curve_after = model
.plots
.iter()
.find(|curve| curve.y_name == "T")
.expect("temperature curve should remain visible");
assert_eq!(temperature_curve_after.points, temperature_curve_before);
assert!(
model
.plots
.iter()
.any(|curve| curve.experiment_id == id && curve.y_name == "sg_m"),
"the new mass curve should be plotted immediately"
);
});
}
#[test]
fn tier3_filter_can_be_undone_from_quick_actions_and_keyboard() {
let mut model = load_model_with_one_curve();
model.select_curve(0);
let id = model.list_of_experiments()[0].clone();
model.set_active_mass_source_for_experiment(&id, "m");
model.set_active_temperature_source_for_experiment(&id, "T");
let (original_history, original_provenance) = {
let experiment = model.series.get_experiment_by_id(&id).unwrap();
(
experiment.dataset.history_of_operations.feed_text(),
experiment.dataset.column_provenance_text("T"),
)
};
let original_temperature = model
.series
.get_experiment_by_id(&id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap()
.column("T")
.unwrap()
.f64()
.unwrap()
.into_no_null_iter()
.collect::<Vec<_>>();
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
click_menu_entry(harness, "Math", " Savitzky Golay");
harness.get_by_label("Apply").click();
harness.run();
assert!(
harness
.state()
.app
.model
.list_of_columns(&id)
.unwrap()
.contains(&"sg_T".into()),
"filter should add its output column before undo"
);
harness.get_by_label("Undo").click();
harness.run();
let model = &harness.state().app.model;
assert!(
!model.list_of_columns(&id).unwrap().contains(&"sg_T".into()),
"quick-action undo should restore the pre-filter schema"
);
assert!(
model
.plots
.iter()
.any(|curve| curve.experiment_id == id && curve.y_name == "T"),
"undo should leave the original curve visible"
);
let restored_temperature = model
.series
.get_experiment_by_id(&id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap()
.column("T")
.unwrap()
.f64()
.unwrap()
.into_no_null_iter()
.collect::<Vec<_>>();
assert_eq!(restored_temperature, original_temperature);
assert!(model.message.contains("undone"));
let restored = model.series.get_experiment_by_id(&id).unwrap();
let restored_history = restored.dataset.history_of_operations.feed_text();
assert!(restored_history.starts_with(&original_history));
assert!(
restored_history
.lines()
.last()
.unwrap()
.contains("undo_last")
);
assert_eq!(
restored.dataset.column_provenance_text("T"),
original_provenance
);
click_menu_entry(harness, "Math", " Savitzky Golay");
harness.get_by_label("Apply").click();
harness.run();
harness.key_press_modifiers(Modifiers::COMMAND, Key::Z);
harness.run();
assert!(
!harness
.state()
.app
.model
.list_of_columns(&id)
.unwrap()
.contains(&"sg_T".into()),
"Ctrl/Cmd+Z should undo the latest filter"
);
assert!(harness.state().app.model.message.contains("undone"));
});
}
#[test]
fn tier2_undo_without_snapshot_reports_failure_without_mutation() {
let mut model = load_model_with_one_curve();
model.select_curve(0);
let id = model.list_of_experiments()[0].clone();
model
.series
.mutate_by_id(&id, |experiment| {
experiment.dataset.undo_stack.clear();
})
.unwrap();
let columns_before = model.list_of_columns(&id).unwrap();
let points_before = model.plots[0].points.clone();
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("Undo").click();
harness.run();
let model = &harness.state().app.model;
assert!(model.message.contains("Undo failed"));
assert_eq!(model.list_of_columns(&id).unwrap(), columns_before);
assert_eq!(model.plots[0].points, points_before);
});
}
#[test]
fn cut_before_time_rebuilds_selected_curve() {
let mut model = load_model_with_one_curve();
model.select_curve(0);
let selected = model.get_selected_curve_index().unwrap();
let before_points = model.plots[selected].points.clone();
let before_first = before_points.first().unwrap()[0];
let cut_time = before_points[before_points.len() / 2][0];
model
.cut_before_time_for_selected(cut_time)
.expect("cut_before_time_for_selected should succeed");
let selected = model.get_selected_curve_index().unwrap();
let after_points = &model.plots[selected].points;
assert!(!after_points.is_empty());
assert!(after_points.first().unwrap()[0] > before_first + 1e-9);
assert!(after_points.first().unwrap()[0] >= cut_time - 1e-9);
}
#[test]
fn cut_before_time_rebuilds_all_live_curves_for_experiment() {
let mut model = load_model_with_two_curves();
model.select_curve(0);
let exp_id = model.list_of_experiments()[0].clone();
let cut_time = model.plots[0].points[model.plots[0].points.len() / 2][0];
model
.cut_before_time_for_selected(cut_time)
.expect("cut before time should succeed");
let curves = model
.plots
.iter()
.filter(|curve| curve.experiment_id == exp_id)
.collect::<Vec<_>>();
assert_eq!(curves.len(), 2);
for curve in curves {
assert!(!curve.points.is_empty());
assert!(curve.points.iter().all(|point| point[0] >= cut_time - 1e-9));
}
assert_eq!(model.get_selected_curve_index(), Some(0));
}
#[test]
fn cut_selected_rebuilds_selected_curve() {
let mut model = load_model_with_one_curve();
model.select_curve(0);
let selected = model.get_selected_curve_index().unwrap();
let points = model.plots[selected].points.clone();
let x_min = points[points.len() / 3][0];
let x_max = points[points.len() * 2 / 3][0];
let y_min = points.iter().map(|p| p[1]).fold(f64::INFINITY, f64::min);
let y_max = points
.iter()
.map(|p| p[1])
.fold(f64::NEG_INFINITY, f64::max);
model.interaction.selection_rect = Some(SelectionRect::new([x_min, y_min], [x_max, y_max]));
model
.cut_range_x_or_y_for_selected(XY::X)
.expect("cut_range_x_or_y_for_selected should succeed");
let selected = model.get_selected_curve_index().unwrap();
let after_points = &model.plots[selected].points;
assert!(!after_points.is_empty());
assert!(
after_points
.iter()
.all(|p| p[0] <= x_min + 1e-9 || p[0] >= x_max - 1e-9)
);
assert!(after_points.first().unwrap()[0] < x_min);
assert!(after_points.last().unwrap()[0] > x_max);
}
#[test]
fn cut_selected_rebuilds_all_live_curves_for_experiment() {
let mut model = load_model_with_two_curves();
model.select_curve(0);
let exp_id = model.list_of_experiments()[0].clone();
let points = model.plots[0].points.clone();
let x_min = points[points.len() / 3][0];
let x_max = points[points.len() * 2 / 3][0];
let y_min = points
.iter()
.map(|point| point[1])
.fold(f64::INFINITY, f64::min);
let y_max = points
.iter()
.map(|point| point[1])
.fold(f64::NEG_INFINITY, f64::max);
model.interaction.selection_rect = Some(SelectionRect::new([x_min, y_min], [x_max, y_max]));
model
.cut_range_x_or_y_for_selected(XY::X)
.expect("selected interval should be cut");
let curves = model
.plots
.iter()
.filter(|curve| curve.experiment_id == exp_id)
.collect::<Vec<_>>();
assert_eq!(curves.len(), 2);
for curve in curves {
assert!(
curve
.points
.iter()
.all(|point| { point[0] <= x_min + 1e-9 || point[0] >= x_max - 1e-9 })
);
}
assert_eq!(model.get_selected_curve_index(), Some(0));
}
#[test]
fn from_s_to_h_is_idempotent_and_reports_already_converted() {
let mut model = load_model_with_one_curve();
model.select_curve(0);
let exp_id = model.list_of_experiments()[0].clone();
model.from_s_to_h_of_selected().expect("first conversion");
let after_first = model
.series
.get_experiment_by_id(&exp_id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap();
let first_time: Vec<f64> = after_first
.column("t")
.unwrap()
.f64()
.unwrap()
.into_no_null_iter()
.collect();
model
.from_s_to_h_of_selected()
.expect("second conversion should be ignored");
assert!(
model
.message
.contains("Time is already in hours; conversion skipped.")
);
let after = model
.series
.get_experiment_by_id(&exp_id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap();
let after_time: Vec<f64> = after
.column("t")
.unwrap()
.f64()
.unwrap()
.into_no_null_iter()
.collect();
assert_eq!(first_time, after_time);
}
#[test]
fn arithmetic_uses_selected_curve_columns_and_rebuilds_each_live_curve() {
let mut model = load_model_with_two_curves();
let exp_id = model.list_of_experiments()[0].clone();
model.select_curve(0);
assert_eq!(model.this_is_x().unwrap(), "t");
assert_eq!(model.this_is_y().unwrap(), "T");
let before_curves = model
.plots
.iter()
.map(|curve| (curve.y_name.clone(), curve.points.clone()))
.collect::<std::collections::HashMap<_, _>>();
let before_frame = model
.series
.get_experiment_by_id(&exp_id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap();
let mass_before = before_frame
.column("m")
.unwrap()
.f64()
.unwrap()
.into_no_null_iter()
.collect::<Vec<_>>();
let temperature_before = before_frame
.column("T")
.unwrap()
.f64()
.unwrap()
.into_no_null_iter()
.collect::<Vec<_>>();
model.add_column_for_selected("T", 5.0).unwrap();
let after_frame = model
.series
.get_experiment_by_id(&exp_id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap();
let mass_after = after_frame
.column("m")
.unwrap()
.f64()
.unwrap()
.into_no_null_iter()
.collect::<Vec<_>>();
let temperature_after = after_frame
.column("T")
.unwrap()
.f64()
.unwrap()
.into_no_null_iter()
.collect::<Vec<_>>();
assert_eq!(mass_before, mass_after);
assert!(
temperature_before
.iter()
.zip(&temperature_after)
.all(|(before, after)| (after - before - 5.0).abs() < 1e-10)
);
let rebuilt_curves = model
.plots
.iter()
.map(|curve| (curve.y_name.as_str(), curve.points.as_slice()))
.collect::<std::collections::HashMap<_, _>>();
let temperature_curve = rebuilt_curves["T"];
let previous_temperature_curve = &before_curves["T"];
assert_eq!(temperature_curve.len(), previous_temperature_curve.len());
assert!(
temperature_curve
.iter()
.zip(previous_temperature_curve)
.all(|(after, before)| (after[1] - before[1] - 5.0).abs() < 1e-10)
);
assert_eq!(rebuilt_curves["m"], before_curves["m"]);
assert_eq!(model.get_selected_curve_index(), Some(0));
}
#[test]
fn from_c_to_k_is_idempotent_and_reports_already_converted() {
let mut model = load_model_with_two_curves();
model.select_curve(0);
let exp_id = model.list_of_experiments()[0].clone();
model
.from_C_to_K_of_selected()
.expect("first conversion should succeed");
let after_first = model
.series
.get_experiment_by_id(&exp_id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap();
let first_temp: Vec<f64> = after_first
.column("T")
.unwrap()
.f64()
.unwrap()
.into_no_null_iter()
.collect();
let first_curves = model
.plots
.iter()
.filter(|curve| curve.experiment_id == exp_id)
.map(|curve| (curve.plot_short_name.clone(), curve.points.clone()))
.collect::<Vec<_>>();
model
.from_C_to_K_of_selected()
.expect("second conversion should be ignored");
assert!(
model
.message
.contains("Temperature is already in Kelvin; conversion skipped.")
);
let after = model
.series
.get_experiment_by_id(&exp_id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap();
let after_temp: Vec<f64> = after
.column("T")
.unwrap()
.f64()
.unwrap()
.into_no_null_iter()
.collect();
assert_eq!(first_temp, after_temp);
let after_curves = model
.plots
.iter()
.filter(|curve| curve.experiment_id == exp_id)
.map(|curve| (curve.plot_short_name.clone(), curve.points.clone()))
.collect::<Vec<_>>();
assert_eq!(first_curves.len(), after_curves.len());
assert_eq!(first_curves, after_curves);
}
#[test]
fn from_mv_to_mg_is_idempotent_and_reports_already_converted() {
let mut model = load_model_with_two_curves();
model.select_curve(0);
let exp_id = model.list_of_experiments()[0].clone();
model
.calibrate_mass_from_voltage_for_selected()
.expect("first mass conversion should succeed");
let after_first = model
.series
.get_experiment_by_id(&exp_id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap();
let first_mass: Vec<f64> = after_first
.column("m")
.unwrap()
.f64()
.unwrap()
.into_no_null_iter()
.collect();
let first_curves = model
.plots
.iter()
.filter(|curve| curve.experiment_id == exp_id)
.map(|curve| (curve.plot_short_name.clone(), curve.points.clone()))
.collect::<Vec<_>>();
model
.calibrate_mass_from_voltage_for_selected()
.expect("second mass conversion should be ignored");
assert!(
model
.message
.contains("Mass is already in milligrams; conversion skipped.")
);
let after = model
.series
.get_experiment_by_id(&exp_id)
.unwrap()
.dataset
.frame
.clone()
.collect()
.unwrap();
let after_mass: Vec<f64> = after
.column("m")
.unwrap()
.f64()
.unwrap()
.into_no_null_iter()
.collect();
assert_eq!(first_mass, after_mass);
let after_curves = model
.plots
.iter()
.filter(|curve| curve.experiment_id == exp_id)
.map(|curve| (curve.plot_short_name.clone(), curve.points.clone()))
.collect::<Vec<_>>();
assert_eq!(first_curves.len(), after_curves.len());
assert_eq!(first_curves, after_curves);
}
#[test]
fn from_s_to_h_rebuilds_all_curves_of_the_experiment() {
let mut model = load_model_with_two_curves();
let exp_id = model.list_of_experiments()[0].clone();
let before_t = model
.plots
.iter()
.filter(|curve| curve.experiment_id == exp_id)
.map(|curve| (curve.plot_short_name.clone(), curve.points.clone()))
.collect::<Vec<_>>();
assert_eq!(before_t.len(), 2);
model
.from_s_to_h_for_experiment(&exp_id)
.expect("time conversion should succeed");
let after = model
.plots
.iter()
.filter(|curve| curve.experiment_id == exp_id)
.map(|curve| (curve.plot_short_name.clone(), curve.points.clone()))
.collect::<Vec<_>>();
assert_eq!(after.len(), 2);
for (name, before_points) in before_t {
let after_points = after
.iter()
.find(|(short_name, _)| short_name == &name)
.expect("curve missing after rebuild")
.1
.clone();
assert_eq!(before_points.len(), after_points.len());
for (before, after) in before_points.iter().zip(after_points.iter()) {
assert!((after[0] - before[0] / 3600.0).abs() < 1e-12);
}
}
}
#[test]
fn move_time_to_zero_rebuilds_all_curves_of_the_experiment() {
let mut model = load_model_with_two_curves();
let exp_id = model.list_of_experiments()[0].clone();
model
.move_time_to_zero_for_experiment(&exp_id)
.expect("move_time_to_zero should succeed");
let curves = model
.plots
.iter()
.filter(|curve| curve.experiment_id == exp_id)
.collect::<Vec<_>>();
assert_eq!(curves.len(), 2);
for curve in curves {
assert!(!curve.points.is_empty());
assert!(curve.points.first().unwrap()[0].abs() < 1e-9);
}
}
#[test]
fn relative_mass_zero_baseline_reports_warning() {
let mut model = load_model_with_one_curve();
model.select_curve(0);
model
.relative_mass_for_selected(0.0, None)
.expect("zero baseline should be handled as a warning");
assert!(
model
.message
.contains("Baseline end time must be greater than 0.0")
);
}
#[test]
fn conversion_zero_baseline_reports_warning() {
let mut model = load_model_with_one_curve();
model.select_curve(0);
model
.conversion_for_selected(0.0, None)
.expect("zero baseline should be handled as a warning");
assert!(
model
.message
.contains("Baseline end time must be greater than 0.0")
);
}
#[test]
fn tier2_quick_action_reset_view_sets_reset_request() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
{
let model = &mut harness.state_mut().app.model;
model.interaction.view_range = (123.0, 456.0);
model.interaction.view_y_range = (-789.0, -700.0);
}
harness.run();
harness.get_by_label("Reset View").click();
harness.run();
assert!(harness.state().app.model.reset_view_requested);
});
}
#[test]
fn tier2_quick_action_zoom_to_selection_updates_view() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
{
let model = &mut harness.state_mut().app.model;
model.start_selection([1.0, 2.0]);
model.update_selection([3.0, 6.0]);
model.end_selection();
model.interaction.view_range = (0.0, 100.0);
model.interaction.view_y_range = (0.0, 100.0);
}
harness.run();
harness.get_by_label("Zoom To Selection").click();
harness.run();
let model = &harness.state().app.model;
assert_eq!(model.interaction.view_range, (0.9, 3.1));
assert_eq!(model.interaction.view_y_range, (1.8, 6.2));
});
}
#[test]
fn tier2_quick_action_delete_plot_removes_selected_curve() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
{
let model = &mut harness.state_mut().app.model;
model.select_curve(0);
}
harness.run();
let before = harness.state().app.model.plots.len();
harness.get_by_label("Delete Plot").click();
harness.run();
let model = &harness.state().app.model;
assert_eq!(model.plots.len(), before.saturating_sub(1));
});
}
#[test]
fn tier2_show_checkbox_restores_hidden_curve_without_recreating_it() {
let mut model = load_model_with_one_curve();
model.select_curve(0);
let show_label = format!("Show {}", model.plots[0].get_label());
let original_points = model.plots[0].points.clone();
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.set_size(egui::vec2(1500.0, 1100.0));
harness.run();
harness.get_by_label(&show_label).click();
harness.run();
let model = &harness.state().app.model;
assert_eq!(model.plots.len(), 1);
assert!(!model.plots[0].shown);
assert!(!model.plots[0].selected);
harness.get_by_label(&show_label).click();
harness.run();
let model = &harness.state().app.model;
assert_eq!(model.plots.len(), 1);
assert!(model.plots[0].shown);
assert_eq!(model.plots[0].points, original_points);
});
}
#[test]
fn deleting_curve_from_its_controls_preserves_experiment_data() {
let model = load_model_with_one_curve();
let experiment_id = model.plots[0].experiment_id.clone();
let columns_before = model.list_of_columns(&experiment_id).unwrap();
let app = PlotApp::for_testing(model);
with_plot_app_state_harness(app, |harness| {
harness.set_size(egui::vec2(1500.0, 1100.0));
harness.run();
harness.get_by_label("Delete").click();
harness.run();
let model = &harness.state().app.model;
assert!(model.plots.is_empty());
assert!(model.series.get_experiment_by_id(&experiment_id).is_ok());
assert_eq!(
model.list_of_columns(&experiment_id).unwrap(),
columns_before
);
});
}
#[test]
fn tier2_quick_action_xy_buttons_change_axis_mode() {
let app = PlotApp::for_testing(load_model_with_one_curve());
with_plot_app_state_harness(app, |harness| {
harness.run();
harness.get_by_label("X").click();
harness.run();
assert_eq!(harness.state().app.quick_actions_state.x_or_y, Some(XY::X));
harness.get_by_label("Y").click();
harness.run();
assert_eq!(harness.state().app.quick_actions_state.x_or_y, Some(XY::Y));
});
}
#[test]
fn test_plotmodel_new_and_default() {
let model1 = PlotModel::new();
let model2 = PlotModel::default();
assert_eq!(model1.plots.len(), 0);
assert_eq!(model2.plots.len(), 0);
assert!(!model1.reset_view_requested);
assert!(!model2.reset_view_requested);
}
#[test]
fn test_experiment_loading() {
let mut model = PlotModel::new();
let test_file = test_data_path();
let result = model.push_from_file(&test_file);
assert!(
result.is_ok(),
"Failed to load test data: {:?}",
result.err()
);
let experiments = model.list_of_experiments();
assert!(!experiments.is_empty(), "No experiments loaded");
}
#[test]
fn test_column_binding_operations() {
use crate::Kinetics::experimental_kinetics::one_experiment_dataset::Unit;
let mut model = PlotModel::new();
let test_file = test_data_path();
model.push_from_file(&test_file).unwrap();
let experiments = model.list_of_experiments();
let exp_id = &experiments[0];
let bind_time = model.bind_time_of_experiment(exp_id, "t", Unit::Second);
assert!(bind_time.is_ok());
let bind_temp = model.bind_temperature_of_experiment(exp_id, "T", Unit::Celsius);
assert!(bind_temp.is_ok());
let bind_mass = model.bind_mass_of_experiment(exp_id, "m", Unit::MilliVolt);
assert!(bind_mass.is_ok());
}
#[test]
fn test_data_transformations() {
use crate::Kinetics::experimental_kinetics::one_experiment_dataset::Unit;
let mut model = PlotModel::new();
let test_file = test_data_path();
model.push_from_file(&test_file).unwrap();
let experiments = model.list_of_experiments();
let exp_id = &experiments[0];
model
.bind_time_of_experiment(exp_id, "t", Unit::Second)
.unwrap();
model
.bind_temperature_of_experiment(exp_id, "T", Unit::Celsius)
.unwrap();
model
.bind_mass_of_experiment(exp_id, "m", Unit::MilliVolt)
.unwrap();
model.set_x(exp_id, "t").unwrap();
model.set_y(exp_id, "T").unwrap();
let result = model.from_C_to_K_for_experiment(exp_id);
assert!(result.is_ok());
model.set_x(exp_id, "t").unwrap();
model.set_y(exp_id, "m").unwrap();
let result = model.from_s_to_h_for_experiment(exp_id);
assert!(result.is_ok());
}
#[test]
fn test_curve_creation() {
use crate::Kinetics::experimental_kinetics::one_experiment_dataset::Unit;
let mut model = PlotModel::new();
let test_file = test_data_path();
model.push_from_file(&test_file).unwrap();
let experiments = model.list_of_experiments();
let exp_id = &experiments[0];
model
.bind_time_of_experiment(exp_id, "t", Unit::Second)
.unwrap();
model
.bind_temperature_of_experiment(exp_id, "T", Unit::Celsius)
.unwrap();
model.set_x(exp_id, "t").unwrap();
model.set_y(exp_id, "T").unwrap();
let result = model.create_points_for_curve(exp_id);
assert!(result.is_ok());
assert!(!model.plots.is_empty());
}
#[test]
fn invalid_plot_sample_count_returns_settings_error() {
let mut model = load_model_with_one_curve();
let experiment_id = model.list_of_experiments()[0].clone();
model.settings.set_n_points(None);
assert!(matches!(
model.calculate_samplepoints_for_plot(&experiment_id, [0.0, 1.0]),
Err(crate::gui::experimental_kinetics_gui::model::TGAGUIError::SettingsErrors(_))
));
}
#[test]
fn failed_plot_refresh_keeps_the_committed_data_change_and_reports_warning() {
let mut model = load_model_with_two_curves();
let experiment_id = model.list_of_experiments()[0].clone();
let before = model.series.get_experiment_by_id(&experiment_id).unwrap();
let before_temperature = before
.dataset
.materialize("t", &["T"])
.unwrap()
.columns
.get("T")
.unwrap()
.iter()
.copied()
.collect::<Vec<_>>();
let before_history = before.dataset.history_of_operations.feed_lines();
let before_undo = before.dataset.undo_depth();
model.settings.set_n_points(None);
let error = model
.scale_column(&experiment_id, "T", 2.0)
.expect_err("invalid plot settings should report a refresh warning");
assert!(matches!(
error,
crate::gui::experimental_kinetics_gui::model::TGAGUIError::PostCommitWarning(_)
));
let after = model.series.get_experiment_by_id(&experiment_id).unwrap();
let after_temperature = after
.dataset
.materialize("t", &["T"])
.unwrap()
.columns
.get("T")
.unwrap()
.iter()
.copied()
.collect::<Vec<_>>();
assert_eq!(
after_temperature,
before_temperature
.iter()
.map(|value| value * 2.0)
.collect::<Vec<_>>()
);
assert_eq!(
after
.dataset
.history_of_operations
.vector_of_operations
.len(),
before_history.len() + 1
);
assert_eq!(after.dataset.undo_depth(), before_undo + 1);
assert!(
model
.plots
.iter()
.all(|plot| plot.experiment_id != experiment_id)
);
}
#[test]
fn spline_plot_failure_reports_that_resampled_columns_were_committed() {
use RustedSciThe::numerical::data_processing::splines::SplineKind;
let mut model = load_model_with_one_curve();
let experiment_id = model.list_of_experiments()[0].clone();
model.select_curve(0);
model.settings.set_n_points(None);
let error = model
.splines_for_selected_as("spline_time", 100, SplineKind::Linear, Some("spline_T"))
.expect_err("plot sampling failure should be reported after the spline is committed");
assert!(matches!(
error,
crate::gui::experimental_kinetics_gui::model::TGAGUIError::PostCommitWarning(_)
));
let columns = model.list_of_columns(&experiment_id).unwrap();
assert!(columns.contains(&"spline_T".to_string()));
assert!(columns.contains(&"spline_time".to_string()));
}
#[test]
fn ivp_experiment_is_kept_when_its_plot_cannot_be_created() {
let mut model = load_model_with_one_curve();
let source_id = model.list_of_experiments()[0].clone();
let dataset = model
.series
.get_experiment_by_id(&source_id)
.unwrap()
.dataset
.clone();
let experiment =
crate::Kinetics::experimental_kinetics::experiment_series_main::TGAExperiment::new(
dataset,
)
.with_id("ivp_plot_failure");
let previous_count = model.series.len();
let error = model
.push_experiment_and_create_plot(
experiment,
crate::gui::experimental_kinetics_gui::model::Colours::Red,
)
.expect_err("missing IVP plot columns should produce a post-commit warning");
assert!(matches!(
error,
crate::gui::experimental_kinetics_gui::model::TGAGUIError::PostCommitWarning(_)
));
assert_eq!(model.series.len(), previous_count + 1);
assert!(
model
.series
.get_experiment_by_id("ivp_plot_failure")
.is_ok()
);
assert!(
model
.plots
.iter()
.all(|plot| plot.experiment_id != "ivp_plot_failure")
);
}
#[test]
fn rebuilding_existing_curve_marks_that_curve_shown() {
let mut model = load_model_with_two_curves();
let experiment_id = model.list_of_experiments()[0].clone();
model.select_curve(0);
for curve in &mut model.plots {
curve.shown = false;
}
model
.create_plots_with_columns(&experiment_id, vec!["T".to_string()], None)
.expect("rebuild existing temperature curve");
let temperature = model
.plots
.iter()
.find(|curve| curve.x_name == "t" && curve.y_name == "T")
.expect("temperature curve should remain present");
let mass = model
.plots
.iter()
.find(|curve| curve.x_name == "t" && curve.y_name == "m")
.expect("mass curve should remain present");
assert!(temperature.shown);
assert!(!mass.shown);
}
#[test]
fn gui_batch_column_drop_is_atomic_and_removes_all_affected_curves() {
let mut model = load_model_with_two_curves();
let experiment_id = model.list_of_experiments()[0].clone();
let before_plots = model.plots.len();
model
.drop_columns_for_experiment(&experiment_id, &["T".to_string(), "m".to_string()])
.expect("drop both selected Y columns");
assert_eq!(model.plots.len(), 0);
assert!(
model
.list_of_columns(&experiment_id)
.unwrap()
.contains(&"t".to_string())
);
assert!(
!model
.list_of_columns(&experiment_id)
.unwrap()
.contains(&"T".to_string())
);
assert!(
!model
.list_of_columns(&experiment_id)
.unwrap()
.contains(&"m".to_string())
);
assert!(before_plots > model.plots.len());
}
#[test]
fn gui_batch_column_drop_failure_preserves_columns_and_plots() {
let mut model = load_model_with_two_curves();
let experiment_id = model.list_of_experiments()[0].clone();
let before_columns = model.list_of_columns(&experiment_id).unwrap();
let before_plots = model.plots.clone();
assert!(
model
.drop_columns_for_experiment(
&experiment_id,
&["T".to_string(), "missing".to_string()],
)
.is_err()
);
assert_eq!(
model.list_of_columns(&experiment_id).unwrap(),
before_columns
);
assert_eq!(model.plots.len(), before_plots.len());
assert!(
model
.plots
.iter()
.zip(before_plots.iter())
.all(|(after, before)| after.plot_short_name == before.plot_short_name)
);
}
#[test]
fn test_select_curve() {
let mut model = PlotModel::new();
use super::super::model::PlotCurve;
let curve1 = PlotCurve::default();
let curve2 = PlotCurve::default();
model.plots.push(curve1);
model.plots.push(curve2);
model.select_curve(0);
assert!(model.plots[0].selected);
assert!(!model.plots[1].selected);
model.select_curve(1);
assert!(!model.plots[0].selected);
assert!(model.plots[1].selected);
}
#[test]
fn test_clear_selection() {
let mut model = PlotModel::new();
use super::super::model::PlotCurve;
let mut curve1 = PlotCurve::default();
curve1.selected = true;
model.plots.push(curve1);
model.clear_selection();
assert!(!model.plots[0].selected);
}
#[test]
fn test_get_selected_curve_index() {
let mut model = PlotModel::new();
use super::super::model::PlotCurve;
let curve1 = PlotCurve::default();
let mut curve2 = PlotCurve::default();
curve2.selected = true;
model.plots.push(curve1);
model.plots.push(curve2);
let selected = model.get_selected_curve_index();
assert_eq!(selected, Some(1));
}
#[test]
fn test_start_selection() {
let mut model = PlotModel::new();
let start_point = [1.0, 2.0];
model.start_selection(start_point);
assert!(model.interaction.is_selecting);
assert_eq!(model.interaction.selection_start, Some(start_point));
}
#[test]
fn test_update_pan() {
let mut model = PlotModel::new();
let initial_range = model.interaction.view_range;
model.interaction.start_pan([0.0, 0.0]);
model.update_pan([1.0, 1.0]);
assert_ne!(model.interaction.view_range, initial_range);
}
#[test]
fn test_zoom() {
let mut model = PlotModel::new();
let initial_range = model.interaction.view_range;
model.zoom(2.0, [5.0, 5.0]);
let new_range = model.interaction.view_range;
let initial_width = initial_range.1 - initial_range.0;
let new_width = new_range.1 - new_range.0;
assert!(new_width < initial_width);
}
#[test]
fn test_reset_view() {
let mut model = PlotModel::new();
model.interaction.view_range = (100.0, 200.0);
model.reset_view();
assert!(model.reset_view_requested);
}
#[test]
fn test_find_nearest_curve() {
use super::super::model::PlotCurve;
let mut model = PlotModel::new();
let mut curve = PlotCurve::default();
curve.points = vec![[0.0, 0.0], [1.0, 1.0], [2.0, 2.0]];
curve.shown = true;
curve.ranges.x_min = 0.0;
curve.ranges.x_max = 2.0;
curve.ranges.y_min = 0.0;
curve.ranges.y_max = 2.0;
model.plots.push(curve);
let nearest = model.find_nearest_curve([0.1, 0.1]);
assert_eq!(nearest, Some(0));
}
#[test]
fn tier3_test_options_load_testing_data_imports_fixture() {
let app = PlotApp::for_testing(PlotModel::new());
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "Test Options", "Load testing data");
harness.get_by_label("Replace workspace with test data?");
harness.get_by_label("Replace with test data").click();
harness.run();
let ids = harness.state().app.model.list_of_experiments();
assert!(
!ids.is_empty(),
"Load testing data should populate the model"
);
});
}
#[test]
fn tier3_loading_test_data_requires_confirmation_and_clears_save_target() {
let export_dir = unique_temp_dir("test_data_save_target");
fs::create_dir_all(&export_dir).unwrap();
let target = export_dir.join("working_document.csv");
let app = PlotApp::for_testing(load_model_with_one_curve());
let mut app = app;
app.new_experiment_dialog.current_dir = export_dir.clone();
with_plot_app_state_harness(app, |harness| {
harness.run();
click_menu_entry(harness, "File Manager", "Save *");
queue_save_path(harness, target.clone());
harness.get_by_label("Save Document").click();
harness.run();
let saved_bytes = fs::read(&target).unwrap();
click_menu_entry(harness, "Test Options", "Load testing data");
harness.get_by_label("Replace workspace with test data?");
assert_eq!(fs::read(&target).unwrap(), saved_bytes);
harness.get_by_label("Replace with test data").click();
harness.run();
assert_eq!(fs::read(&target).unwrap(), saved_bytes);
harness.key_press_modifiers(Modifiers::COMMAND, Key::S);
harness.run();
harness.get_by_label("Save Document");
assert!(harness.state().app.new_experiment_dialog.save_series_open);
assert!(harness.state().app.model.message.contains("Save As"));
});
fs::remove_dir_all(export_dir).unwrap();
}
#[test]
fn spline_uses_the_selected_curve_axes_not_the_experiment_plot_binding() {
use RustedSciThe::numerical::data_processing::splines::SplineKind;
let mut model = load_model_with_two_curves();
let experiment_id = model.list_of_experiments()[0].clone();
let temperature_curve = model
.plots
.iter()
.position(|curve| curve.y_name == "T")
.unwrap();
model.select_curve(temperature_curve);
assert_eq!(model.this_is_y().unwrap(), "T");
model
.splines_for_selected_as("time_spline", 100, SplineKind::Linear, Some("T_spline"))
.unwrap();
let block = model
.series
.get_experiment_by_id(&experiment_id)
.unwrap()
.dataset
.materialize("time_spline", &["T_spline"])
.unwrap();
assert!(block.columns["T_spline"][0] > 100.0);
assert_eq!(
model
.active_temperature_source_for_experiment(&experiment_id)
.unwrap(),
"T_spline"
);
}
#[test]
fn rolling_mean_does_not_trim_source_rows_because_another_column_has_null_edges() {
let mut model = load_model_with_one_curve();
let experiment_id = model.list_of_experiments()[0].clone();
model.select_curve(0);
model
.sg_filter_column_for_selected_as("T", 11, 3, 1, 1.0, Some("dT_dt"))
.unwrap();
let rows_before = model
.series
.get_experiment_by_id(&experiment_id)
.unwrap()
.dataset
.materialize("t", &["m"])
.unwrap()
.grid
.len();
model
.rolling_mean_for_selected_as("m", 11, Some("rolling_m"))
.unwrap();
let rows_after = model
.series
.get_experiment_by_id(&experiment_id)
.unwrap()
.dataset
.materialize("t", &["m"])
.unwrap()
.grid
.len();
assert_eq!(rows_after, rows_before);
assert!(
model
.list_of_columns(&experiment_id)
.unwrap()
.contains(&"rolling_m".to_string())
);
}
#[test]
fn savitzky_golay_derivative_does_not_become_active_mass_and_rejects_in_place_overwrite() {
let mut model = load_model_with_one_curve();
let experiment_id = model.list_of_experiments()[0].clone();
model.select_curve(0);
let mass_before = model
.series
.get_experiment_by_id(&experiment_id)
.unwrap()
.dataset
.materialize("t", &["m"])
.unwrap()
.columns["m"][10];
model
.sg_filter_column_for_selected_as("m", 11, 3, 1, 1.0, Some("dm_dt"))
.unwrap();
assert_eq!(
model
.active_mass_source_for_experiment(&experiment_id)
.unwrap(),
"m"
);
assert!(
model
.sg_filter_column_for_selected_as("m", 11, 3, 1, 1.0, Some("m"))
.is_err()
);
let mass_after = model
.series
.get_experiment_by_id(&experiment_id)
.unwrap()
.dataset
.materialize("t", &["m"])
.unwrap()
.columns["m"][10];
assert_eq!(mass_after, mass_before);
}
#[test]
fn tier3_bind_preprocess_and_export_roundtrip_via_gui_dialog() {
let app = PlotApp::for_testing(PlotModel::new());
with_plot_app_state_harness(app, |harness| {
harness
.state_mut()
.app
.model
.push_from_file(&test_data_path())
.expect("failed to import raw fixture");
let id = harness.state().app.model.list_of_experiments()[0].clone();
{
let model = &mut harness.state_mut().app.model;
model
.bind_time_of_experiment(&id, "t", Unit::Second)
.expect("failed to bind time");
model
.bind_temperature_of_experiment(&id, "T", Unit::Celsius)
.expect("failed to bind temperature");
model
.bind_mass_of_experiment(&id, "m", Unit::MilliVolt)
.expect("failed to bind mass");
model.set_x(&id, "t").expect("failed to set x");
model.set_y(&id, "T").expect("failed to set y");
model
.create_points_for_curve(&id)
.expect("failed to create plot");
model
.from_C_to_K_for_experiment(&id)
.expect("failed to convert temperature unit");
model
.from_s_to_h_for_experiment(&id)
.expect("failed to convert time unit");
}
click_menu_entry(harness, "File Manager", "Export CSV");
let export_dir = unique_temp_dir("gui_export");
fs::create_dir_all(&export_dir).expect("failed to create export dir");
harness.state_mut().app.new_experiment_dialog.current_dir = export_dir.clone();
harness.run();
queue_save_path(harness, export_dir.join("series_export.csv"));
harness.get_by_label("Export").click();
harness.run();
let export_file = export_dir.join("series_export.csv");
assert!(export_file.exists(), "expected exported CSV to be created");
let roundtrip = {
let mut model = PlotModel::new();
model
.from_csv_series(&export_file)
.expect("failed to reload exported CSV");
model
};
assert_eq!(roundtrip.list_of_experiments().len(), 1);
let roundtrip_id = roundtrip.list_of_experiments()[0].clone();
let cols = roundtrip
.list_of_columns(&roundtrip_id)
.expect("roundtrip columns unavailable");
assert!(cols.contains(&"t".to_string()));
assert!(cols.contains(&"T".to_string()));
assert!(cols.contains(&"m".to_string()));
let _ = fs::remove_file(&export_file);
let _ = fs::remove_dir_all(&export_dir);
});
}
#[test]
fn tier3_create_experiment_from_columns_keeps_series_consistent() {
let mut model = load_model_with_one_curve();
let source_id = model.list_of_experiments()[0].clone();
let before = model.list_of_experiments().len();
let derived_id = format!("{source_id}_core");
model
.create_experiment_from_columns_for_experiment(
&source_id,
derived_id.clone(),
&["t", "T", "m"],
)
.expect("failed to create reduced experiment");
let ids = model.list_of_experiments();
assert_eq!(ids.len(), before + 1);
assert!(ids.contains(&derived_id));
let cols = model
.list_of_columns(&derived_id)
.expect("derived experiment columns unavailable");
assert_eq!(cols.len(), 3);
assert!(cols.contains(&"t".to_string()));
assert!(cols.contains(&"T".to_string()));
assert!(cols.contains(&"m".to_string()));
}
#[test]
fn tier3_build_kinetic_view_and_run_ofw_on_preprocessed_series() {
let mut model = PlotModel::new();
model
.generate_synthetic_data_from_config(&non_isothermal_series_config())
.expect("failed to generate synthetic series");
let ids = model.list_of_experiments();
assert_eq!(ids.len(), 3);
for id in &ids {
let t_end = last_value_for(&model, id, "time");
model
.conversion_for_experiment(id.clone(), t_end, "eta")
.expect("failed to derive conversion");
model
.derive_deta_dt(id, Some("deta_dt"))
.expect("failed to derive conversion rate");
}
let method = IsoconversionalKineticMethod {
method: IsoconversionalMethod::OFW,
};
let view = model
.create_kinetic_data_view_for_method(None, &method.method)
.expect("failed to build kinetic data view");
assert_eq!(view.experiments.len(), 3);
let result = method.compute(&view).expect("OFW computation failed");
assert!(
!result.layers.is_empty(),
"OFW should produce at least one α-layer"
);
}
#[test]
fn undo_resamples_existing_live_curve_after_in_place_change() {
let mut model = load_model_with_one_curve();
let id = model.list_of_experiments()[0].clone();
let original_points = model.plots[0].points.clone();
model
.scale_column(&id, "T", 2.0)
.expect("scale temperature in place");
assert_ne!(model.plots[0].points, original_points);
model
.reverse_experiment(&id)
.expect("undo temperature scaling");
assert_eq!(model.plots[0].points, original_points);
assert_eq!(model.plots[0].y_name, "T");
}
#[test]
fn renaming_a_plotted_column_keeps_the_curve_bound_to_its_new_name() {
let mut model = load_model_with_one_curve();
let id = model.list_of_experiments()[0].clone();
model.select_curve(0);
model
.rename_column_for_experiment(&id, "T", "temperature_clean")
.expect("rename plotted temperature column");
assert_eq!(model.plots.len(), 1);
assert_eq!(model.plots[0].x_name, "t");
assert_eq!(model.plots[0].y_name, "temperature_clean");
assert!(model.plots[0].selected);
assert!(!model.plots[0].points.is_empty());
}
#[test]
fn dropping_an_experiment_updates_remaining_plot_indices() {
use crate::Kinetics::experimental_kinetics::experiment_series_main::ExperimentMeta;
let mut model = load_model_with_one_curve();
let second_id = "second_experiment";
model
.push_from_file_with_meta(
&test_data_path(),
ExperimentMeta {
id: second_id.to_string(),
..Default::default()
},
)
.expect("load second experiment");
model
.bind_time_of_experiment(second_id, "t", Unit::Second)
.unwrap();
model
.bind_temperature_of_experiment(second_id, "T", Unit::Celsius)
.unwrap();
model.set_x(second_id, "t").unwrap();
model.set_y(second_id, "T").unwrap();
model.create_points_for_curve(second_id).unwrap();
model.drop_experiment(0).expect("drop first experiment");
assert_eq!(model.plots.len(), 1);
assert_eq!(model.plots[0].experiment_id, second_id);
assert_eq!(model.plots[0].experiment_index, 0);
}
#[test]
fn gui_error_display_preserves_the_user_facing_message() {
let error =
super::super::model::TGAGUIError::BindingError("Select a curve first.".to_string());
assert_eq!(error.to_string(), "Select a curve first.");
assert!(std::error::Error::source(&error).is_none());
let domain_error = crate::Kinetics::experimental_kinetics::one_experiment_dataset::TGADomainError::InvalidOperation(
"invalid operation".to_string(),
);
let gui_error = super::super::model::TGAGUIError::from(domain_error);
assert_eq!(gui_error.to_string(), "invalid operation");
assert!(std::error::Error::source(&gui_error).is_some());
}
}