Skip to content

Commit a9a0729

Browse files
Merge pull request #176 from NathanNeurotic/codex/update-tests-to-use-actiondescriptor
Update tests to dispatch actions
2 parents e7e0696 + 2970907 commit a9a0729

2 files changed

Lines changed: 144 additions & 46 deletions

File tree

crates/psu-packer-gui/src/ui/pack_controls.rs

Lines changed: 93 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -318,38 +318,67 @@ impl From<FileListKind> for ListKind {
318318
#[cfg(test)]
319319
mod tests {
320320
use super::*;
321-
use crate::TimestampStrategy;
322321
use chrono::NaiveDate;
322+
use gui_core::actions::{
323+
Action, ActionDescriptor, FileListAction, FileListKind, IconSysAction, TimestampAction,
324+
TimestampStrategyAction,
325+
};
323326
use ps2_filetypes::sjis;
324327
use std::path::PathBuf;
325328

326-
#[test]
327-
fn config_from_state_appends_psu_toml_once() {
329+
fn dispatch_action(app: &mut PackerApp, descriptor: &ActionDescriptor) {
330+
let action = descriptor.action.clone();
331+
assert!(app.supports_action(action.clone()));
332+
assert!(app.is_action_enabled(action.clone()));
333+
app.trigger_action(action);
334+
}
335+
336+
fn set_manual_entry(app: &mut PackerApp, kind: FileListKind, value: &str) {
337+
let manual_entry = app.packer_state_mut().manual_entry_mut(kind);
338+
manual_entry.clear();
339+
manual_entry.push_str(value);
340+
}
341+
342+
fn app_with_prefix(prefix: SasPrefix) -> PackerApp {
328343
let mut app = PackerApp::default();
329344
app.packer_state.set_folder_base_name("SAVE".to_string());
330345
app.packer_state.set_psu_file_base_name("SAVE".to_string());
331-
app.packer_state.set_selected_prefix(SasPrefix::App);
346+
app.packer_state.set_selected_prefix(prefix);
347+
app
348+
}
332349

333-
let config = app.config_from_state().expect("configuration should build");
350+
#[test]
351+
fn config_from_state_appends_psu_toml_once() {
352+
let base_app = app_with_prefix(SasPrefix::App);
353+
let config = base_app
354+
.config_from_state()
355+
.expect("configuration should build");
334356
assert_eq!(config.exclude, Some(vec!["psu.toml".to_string()]));
335357
assert!(
336-
app.packer_state.exclude_files.is_empty(),
358+
base_app.packer_state.exclude_files.is_empty(),
337359
"building the configuration should not modify the exclude list"
338360
);
339361

340-
app.packer_state
341-
.set_file_list_entries(FileListKind::Exclude, vec!["DATA.BIN".to_string()]);
342-
let config_with_manual_entry = app
362+
let manual_add_exclude = ActionDescriptor::new(
363+
Action::FileList(FileListAction::ManualAdd(FileListKind::Exclude)),
364+
"Add",
365+
);
366+
367+
let mut manual_entry_app = app_with_prefix(SasPrefix::App);
368+
set_manual_entry(&mut manual_entry_app, FileListKind::Exclude, "DATA.BIN");
369+
dispatch_action(&mut manual_entry_app, &manual_add_exclude);
370+
let config_with_manual_entry = manual_entry_app
343371
.config_from_state()
344372
.expect("configuration should include manual exclude");
345373
assert_eq!(
346374
config_with_manual_entry.exclude,
347375
Some(vec!["DATA.BIN".to_string(), "psu.toml".to_string()])
348376
);
349377

350-
app.packer_state
351-
.set_file_list_entries(FileListKind::Exclude, vec!["psu.toml".to_string()]);
352-
let config_with_duplicate = app
378+
let mut duplicate_app = app_with_prefix(SasPrefix::App);
379+
set_manual_entry(&mut duplicate_app, FileListKind::Exclude, "psu.toml");
380+
dispatch_action(&mut duplicate_app, &manual_add_exclude);
381+
let config_with_duplicate = duplicate_app
353382
.config_from_state()
354383
.expect("configuration should handle duplicate entries");
355384
assert_eq!(
@@ -360,20 +389,34 @@ mod tests {
360389

361390
#[test]
362391
fn build_config_uses_loaded_psu_edits() {
363-
let mut app = PackerApp::default();
392+
let mut app = app_with_prefix(SasPrefix::Emu);
364393
app.packer_state.loaded_psu_path = Some(PathBuf::from("input.psu"));
365-
app.packer_state.set_selected_prefix(SasPrefix::Emu);
366-
app.packer_state.set_folder_base_name("SAVE".to_string());
367-
app.packer_state.set_psu_file_base_name("SAVE".to_string());
368394
let timestamp = NaiveDate::from_ymd_opt(2023, 11, 14)
369395
.and_then(|date| date.and_hms_opt(12, 34, 56))
370396
.expect("valid timestamp");
371397
app.packer_state.set_manual_timestamp(Some(timestamp));
372-
app.set_timestamp_strategy(TimestampStrategy::Manual);
373-
app.packer_state
374-
.set_file_list_entries(FileListKind::Include, vec!["FILE.BIN".to_string()]);
375-
app.packer_state
376-
.set_file_list_entries(FileListKind::Exclude, vec!["SKIP.DAT".to_string()]);
398+
399+
let select_manual_strategy = ActionDescriptor::new(
400+
Action::Timestamp(TimestampAction::SelectStrategy(
401+
TimestampStrategyAction::Manual,
402+
)),
403+
"Manual",
404+
);
405+
dispatch_action(&mut app, &select_manual_strategy);
406+
407+
let include_manual_add = ActionDescriptor::new(
408+
Action::FileList(FileListAction::ManualAdd(FileListKind::Include)),
409+
"Add include",
410+
);
411+
let exclude_manual_add = ActionDescriptor::new(
412+
Action::FileList(FileListAction::ManualAdd(FileListKind::Exclude)),
413+
"Add exclude",
414+
);
415+
416+
set_manual_entry(&mut app, FileListKind::Include, "FILE.BIN");
417+
dispatch_action(&mut app, &include_manual_add);
418+
set_manual_entry(&mut app, FileListKind::Exclude, "SKIP.DAT");
419+
dispatch_action(&mut app, &exclude_manual_add);
377420

378421
let config = app.build_config().expect("config builds successfully");
379422
assert_eq!(config.name, "EMU_SAVE");
@@ -387,12 +430,20 @@ mod tests {
387430

388431
#[test]
389432
fn manual_filter_entries_allowed_without_folder() {
390-
let mut app = PackerApp::default();
391-
app.packer_state.set_selected_prefix(SasPrefix::App);
392-
app.packer_state.set_folder_base_name("SAVE".to_string());
433+
let mut app = app_with_prefix(SasPrefix::App);
434+
let include_manual_add = ActionDescriptor::new(
435+
Action::FileList(FileListAction::ManualAdd(FileListKind::Include)),
436+
"Add include",
437+
);
438+
let exclude_manual_add = ActionDescriptor::new(
439+
Action::FileList(FileListAction::ManualAdd(FileListKind::Exclude)),
440+
"Add exclude",
441+
);
393442

394-
assert!(app.handle_add_file_from_entry(ListKind::Include, "BOOT.ELF"));
395-
assert!(app.handle_add_file_from_entry(ListKind::Exclude, "THUMBS.DB"));
443+
set_manual_entry(&mut app, FileListKind::Include, "BOOT.ELF");
444+
dispatch_action(&mut app, &include_manual_add);
445+
set_manual_entry(&mut app, FileListKind::Exclude, "THUMBS.DB");
446+
dispatch_action(&mut app, &exclude_manual_add);
396447

397448
let config = app.build_config().expect("config builds successfully");
398449
assert_eq!(config.include, Some(vec!["BOOT.ELF".to_string()]));
@@ -404,24 +455,31 @@ mod tests {
404455

405456
#[test]
406457
fn manual_filter_entries_trim_and_reject_duplicates() {
407-
let mut app = PackerApp::default();
458+
let mut app = app_with_prefix(SasPrefix::App);
459+
let include_manual_add = ActionDescriptor::new(
460+
Action::FileList(FileListAction::ManualAdd(FileListKind::Include)),
461+
"Add include",
462+
);
408463

409-
assert!(app.handle_add_file_from_entry(ListKind::Include, " DATA.BIN "));
464+
set_manual_entry(&mut app, FileListKind::Include, " DATA.BIN ");
465+
dispatch_action(&mut app, &include_manual_add);
410466
assert_eq!(app.packer_state.include_files, vec!["DATA.BIN"]);
411467

412-
assert!(!app.handle_add_file_from_entry(ListKind::Include, "DATA.BIN"));
468+
let initial_len = app.packer_state.include_files.len();
469+
set_manual_entry(&mut app, FileListKind::Include, "DATA.BIN");
470+
dispatch_action(&mut app, &include_manual_add);
471+
assert_eq!(app.packer_state.include_files.len(), initial_len);
413472
assert_eq!(app.packer_state.include_files, vec!["DATA.BIN"]);
414473
assert!(app.packer_state.error_message.is_some());
415474
}
416475

417476
#[test]
418477
fn config_from_state_uses_shift_jis_byte_linebreaks() {
419-
let mut app = PackerApp::default();
420-
app.packer_state.set_selected_prefix(SasPrefix::App);
421-
app.packer_state.set_folder_base_name("SAVE".to_string());
422-
app.packer_state.set_psu_file_base_name("SAVE".to_string());
423-
app.icon_sys_enabled = true;
424-
app.icon_sys_use_existing = false;
478+
let mut app = app_with_prefix(SasPrefix::App);
479+
let enable_descriptor =
480+
ActionDescriptor::new(Action::IconSys(IconSysAction::Enable), "Enable");
481+
dispatch_action(&mut app, &enable_descriptor);
482+
425483
app.icon_sys_title_line1 = "メモ".to_string();
426484
app.icon_sys_title_line2 = "リーカード".to_string();
427485

crates/psu-packer-gui/src/view/mod.rs

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,14 @@ impl eframe::App for PackerApp {
317317
#[cfg(test)]
318318
mod tests {
319319
use super::*;
320-
use crate::TIMESTAMP_FORMAT;
320+
use crate::{SasPrefix, TIMESTAMP_FORMAT};
321321
use chrono::NaiveDateTime;
322322
use eframe::egui;
323+
use gui_core::actions::{
324+
Action, ActionDescriptor, EditorAction, MetadataAction, TimestampAction,
325+
TimestampStrategyAction,
326+
};
327+
use gui_core::ActionDispatcher;
323328
use std::{fs, path::Path, thread, time::Duration};
324329
use tempfile::tempdir;
325330

@@ -330,6 +335,13 @@ mod tests {
330335
}
331336
}
332337

338+
fn dispatch_action(app: &mut PackerApp, descriptor: &ActionDescriptor) {
339+
let action = descriptor.action.clone();
340+
assert!(app.supports_action(action.clone()));
341+
assert!(app.is_action_enabled(action.clone()));
342+
app.trigger_action(action);
343+
}
344+
333345
fn wait_for_pack_completion(app: &mut PackerApp) {
334346
while app.pack_job_active() {
335347
thread::sleep(Duration::from_millis(10));
@@ -340,10 +352,17 @@ mod tests {
340352
#[test]
341353
fn timestamp_panel_serializes_rules() {
342354
let mut app = PackerApp::default();
355+
let open_timestamp_editor = ActionDescriptor::new(
356+
Action::OpenEditor(EditorAction::TimestampAutomation),
357+
"Timestamp rules",
358+
);
359+
dispatch_action(&mut app, &open_timestamp_editor);
343360
app.packer_state
344361
.timestamp_rules_ui
345362
.set_seconds_between_items(8);
346-
app.mark_timestamp_rules_modified();
363+
app.packer_state
364+
.timestamp_rules_ui
365+
.apply_to_rules(&mut app.packer_state.timestamp_rules);
347366

348367
let mut panel = TimestampRulesPanel::default();
349368
let ctx = egui::Context::default();
@@ -369,11 +388,37 @@ mod tests {
369388
write_required_files(&project_dir);
370389

371390
let mut app = PackerApp::default();
391+
app.packer_state.folder = Some(project_dir.clone());
392+
app.packer_state.refresh_missing_required_project_files();
372393
app.packer_state.timestamp = Some(
373394
NaiveDateTime::parse_from_str("2024-01-01 12:00:00", TIMESTAMP_FORMAT)
374395
.expect("parse timestamp"),
375396
);
376397

398+
let select_app_prefix = ActionDescriptor::new(
399+
Action::Metadata(MetadataAction::SelectPrefix(SasPrefix::App)),
400+
"Select prefix",
401+
);
402+
let set_folder_name = ActionDescriptor::new(
403+
Action::Metadata(MetadataAction::SetFolderBaseName("SAVE".to_string())),
404+
"Set folder name",
405+
);
406+
let set_psu_base = ActionDescriptor::new(
407+
Action::Metadata(MetadataAction::SetPsuFileBaseName("SAVE".to_string())),
408+
"Set PSU base",
409+
);
410+
let manual_strategy = ActionDescriptor::new(
411+
Action::Timestamp(TimestampAction::SelectStrategy(
412+
TimestampStrategyAction::Manual,
413+
)),
414+
"Manual strategy",
415+
);
416+
417+
dispatch_action(&mut app, &select_app_prefix);
418+
dispatch_action(&mut app, &set_folder_name);
419+
dispatch_action(&mut app, &set_psu_base);
420+
dispatch_action(&mut app, &manual_strategy);
421+
377422
let mut panel = TimestampRulesPanel::default();
378423
let ctx = egui::Context::default();
379424
let palette = app.theme.clone();
@@ -383,15 +428,10 @@ mod tests {
383428
});
384429

385430
let output_path = workspace.path().join("output.psu");
386-
let config = psu_packer::Config {
387-
name: "APP_TEST".to_string(),
388-
timestamp: None,
389-
include: None,
390-
exclude: None,
391-
icon_sys: None,
392-
};
393-
394-
app.start_pack_job(project_dir, output_path.clone(), config);
431+
app.packer_state.output = output_path.display().to_string();
432+
433+
let pack_descriptor = ActionDescriptor::new(Action::PackPsu, "Pack");
434+
dispatch_action(&mut app, &pack_descriptor);
395435
wait_for_pack_completion(&mut app);
396436

397437
assert!(output_path.exists());

0 commit comments

Comments
 (0)