#[cfg(test)]
mod tests {
    use regenerator2000::state::{
        AppState, Block, BlockType, DocumentSettings, LabelKind, ProjectState,
    };
    use std::collections::BTreeMap;

    #[test]
    fn test_load_project_regenerates_auto_labels() {
        let mut app_state = AppState::new();

        // 1. Create a dummy project JSON
        // We need code that produces a label.
        // 4C 05 10  -> JMP $1005. Should generate auto label j1005 (or b1005 depending on logic, but definitely an auto label).
        // The project JSON will have NO labels map (empty).

        // Create raw bytes: 4C 05 10 EA EA EA
        let raw_bytes: Vec<u8> = vec![0x4C, 0x05, 0x10, 0xEA, 0xEA, 0xEA];
        let raw_data_base64 =
            regenerator2000::state::encode_raw_data_to_base64(&raw_bytes).unwrap();

        let project = ProjectState {
            origin: 0x1000,
            raw_data: raw_data_base64,
            blocks: vec![Block {
                start: 0,
                end: 5,
                type_: BlockType::Code,
                collapsed: false,
            }],
            labels: BTreeMap::new(), // EMPTY! No saved labels.
            user_side_comments: BTreeMap::new(),
            user_line_comments: BTreeMap::new(),
            immediate_value_formats: BTreeMap::new(),
            settings: DocumentSettings::default(),
            cursor_address: None,
            hex_dump_cursor_address: None,
            sprites_cursor_address: None,
            charset_cursor_address: None,
            bitmap_cursor_address: None,
            right_pane_visible: None,
            sprite_multicolor_mode: false,
            charset_multicolor_mode: false,
            bitmap_multicolor_mode: false,
            hexdump_view_mode: regenerator2000::state::HexdumpViewMode::default(),
            splitters: std::collections::BTreeSet::new(),
            blocks_view_cursor: None,
            bookmarks: BTreeMap::new(),
        };

        let json = serde_json::to_string(&project).unwrap();

        let mut path = std::env::temp_dir();
        path.push("test_regen_labels.regen2000proj");
        std::fs::write(&path, json).unwrap();

        // 2. Load it
        app_state
            .load_project(path.clone())
            .expect("Failed to load project");

        // 3. Verify labels exist
        // We expect a label at 0x1005.
        let label = app_state.labels.get(&0x1005);
        assert!(
            label.is_some(),
            "Autogenerated label at 0x1005 should exist after load"
        );
        let label = label.unwrap();
        assert_eq!(
            label.first().unwrap().kind,
            LabelKind::Auto,
            "Should be Auto label"
        );
        // Name might be j1005 or b1005 depending on priority, checking existence is main goal.

        // Cleanup
        let _ = std::fs::remove_file(path);
    }
}
