modde-core 0.2.1

Core types and logic for the modde mod manager
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! End-to-end test: full deploy pipeline
//!
//! Profile creation -> load order resolution -> VFS build ->
//! materialize -> deploy -> verify content

use smallvec::smallvec;
use std::collections::HashMap;
use std::path::PathBuf;

use modde_core::GameId;
use modde_core::ModdeDb;
use modde_core::profile::{EnabledMod, Profile, ProfileManager, ProfileSource};
use modde_core::resolver::{ConflictMap, LoadOrderRule, ModId, resolve};
use modde_core::vfs::SymlinkFarm;
use tempfile::TempDir;

fn simple_mod(id: &str, enabled: bool) -> EnabledMod {
    EnabledMod {
        mod_id: id.to_string(),
        enabled,
        version: Some("1.0".to_string()),
        fomod_config: None,
        ..Default::default()
    }
}

#[tokio::test]
async fn test_full_deploy_pipeline() {
    let tmp = TempDir::new().unwrap();

    // Step 1: Create a profile with mods and load order rules
    let profile = Profile {
        id: None,
        name: "e2e_test".to_string(),
        game_id: GameId::from("skyrim-se"),
        source: ProfileSource::Manual,
        mods: vec![
            simple_mod("base_textures", true),
            simple_mod("texture_overhaul", true),
            simple_mod("disabled_mod", false),
            simple_mod("patch_mod", true),
        ],
        overrides: PathBuf::from("/tmp/overrides"),
        load_order_rules: smallvec![
            // texture_overhaul must load after base_textures
            LoadOrderRule::LoadAfter {
                mod_id: ModId::from("texture_overhaul"),
                after: ModId::from("base_textures"),
            },
            // patch_mod must load after texture_overhaul
            LoadOrderRule::LoadAfter {
                mod_id: ModId::from("patch_mod"),
                after: ModId::from("texture_overhaul"),
            },
        ],
        load_order_lock: None,
    };

    // Step 2: Save and reload the profile via DB
    let pm = ProfileManager::with_db(ModdeDb::open_memory().unwrap());
    pm.create(&profile).unwrap();
    let loaded_profile = pm.load("e2e_test", None).unwrap();
    assert_eq!(loaded_profile.name, "e2e_test");
    assert_eq!(loaded_profile.mods.len(), 4);

    // Step 3: Resolve load order
    let resolved = resolve(&loaded_profile).unwrap();
    // disabled_mod should be excluded
    assert_eq!(resolved.order.len(), 3);
    assert!(!resolved.order.contains(&ModId::from("disabled_mod")));

    // Verify ordering constraints
    let pos_base = resolved
        .order
        .iter()
        .position(|m| m == "base_textures")
        .unwrap();
    let pos_overhaul = resolved
        .order
        .iter()
        .position(|m| m == "texture_overhaul")
        .unwrap();
    let pos_patch = resolved
        .order
        .iter()
        .position(|m| m == "patch_mod")
        .unwrap();
    assert!(
        pos_base < pos_overhaul,
        "base_textures must come before texture_overhaul"
    );
    assert!(
        pos_overhaul < pos_patch,
        "texture_overhaul must come before patch_mod"
    );

    // Step 4: Create mod files in a mock store
    let store = tmp.path().join("store");

    // base_textures provides sky.dds and ground.dds
    let base_dir = store.join("base_textures");
    std::fs::create_dir_all(&base_dir).unwrap();
    std::fs::write(base_dir.join("sky.dds"), "base_sky").unwrap();
    std::fs::write(base_dir.join("ground.dds"), "base_ground").unwrap();

    // texture_overhaul overrides sky.dds
    let overhaul_dir = store.join("texture_overhaul");
    std::fs::create_dir_all(&overhaul_dir).unwrap();
    std::fs::write(overhaul_dir.join("sky.dds"), "overhaul_sky").unwrap();

    // patch_mod adds patch.esp and overrides ground.dds
    let patch_dir = store.join("patch_mod");
    std::fs::create_dir_all(&patch_dir).unwrap();
    std::fs::write(patch_dir.join("patch.esp"), "patch_plugin").unwrap();
    std::fs::write(patch_dir.join("ground.dds"), "patch_ground").unwrap();

    // Build mod_files map
    let mut mod_files: HashMap<ModId, Vec<(String, PathBuf)>> = HashMap::new();
    mod_files.insert(
        ModId::from("base_textures"),
        vec![
            ("textures/sky.dds".into(), base_dir.join("sky.dds")),
            ("textures/ground.dds".into(), base_dir.join("ground.dds")),
        ],
    );
    mod_files.insert(
        ModId::from("texture_overhaul"),
        vec![("textures/sky.dds".into(), overhaul_dir.join("sky.dds"))],
    );
    mod_files.insert(
        ModId::from("patch_mod"),
        vec![
            ("patch.esp".into(), patch_dir.join("patch.esp")),
            ("textures/ground.dds".into(), patch_dir.join("ground.dds")),
        ],
    );

    // Step 5: Build the symlink farm
    // Use a custom staging dir within our temp
    let farm = SymlinkFarm::from_links(tmp.path().join("staging"), {
        let mut links = HashMap::new();
        for mod_id in &resolved.order {
            if let Some(files) = mod_files.get(mod_id) {
                for (rel_path, source) in files {
                    links.insert(rel_path.clone(), source.clone());
                }
            }
        }
        links
    });

    // Verify override resolution: 3 unique files
    assert_eq!(farm.links.len(), 3);
    // sky.dds should come from texture_overhaul (loaded after base_textures)
    assert!(
        farm.links["textures/sky.dds"]
            .to_string_lossy()
            .contains("texture_overhaul")
    );
    // ground.dds should come from patch_mod (loaded last)
    assert!(
        farm.links["textures/ground.dds"]
            .to_string_lossy()
            .contains("patch_mod")
    );
    // patch.esp only from patch_mod
    assert!(
        farm.links["patch.esp"]
            .to_string_lossy()
            .contains("patch_mod")
    );

    // Step 6: Materialize the symlink farm
    let farm = farm.materialize().await.unwrap();

    // Verify staging directory structure
    assert!(
        tmp.path()
            .join("staging/textures/sky.dds")
            .symlink_metadata()
            .unwrap()
            .file_type()
            .is_symlink()
    );
    assert!(
        tmp.path()
            .join("staging/textures/ground.dds")
            .symlink_metadata()
            .unwrap()
            .file_type()
            .is_symlink()
    );
    assert!(
        tmp.path()
            .join("staging/patch.esp")
            .symlink_metadata()
            .unwrap()
            .file_type()
            .is_symlink()
    );

    // Verify content through symlinks
    assert_eq!(
        std::fs::read_to_string(tmp.path().join("staging/textures/sky.dds")).unwrap(),
        "overhaul_sky"
    );
    assert_eq!(
        std::fs::read_to_string(tmp.path().join("staging/textures/ground.dds")).unwrap(),
        "patch_ground"
    );
    assert_eq!(
        std::fs::read_to_string(tmp.path().join("staging/patch.esp")).unwrap(),
        "patch_plugin"
    );

    // Step 7: Deploy to a game directory
    let game_dir = tmp.path().join("game/Data");
    farm.deploy_to(&game_dir).await.unwrap();

    // Verify deployment - symlinks in game dir point to staging
    assert!(
        game_dir
            .join("textures/sky.dds")
            .symlink_metadata()
            .unwrap()
            .file_type()
            .is_symlink()
    );
    // Content accessible through the double-symlink chain
    assert_eq!(
        std::fs::read_to_string(game_dir.join("textures/sky.dds")).unwrap(),
        "overhaul_sky"
    );
    assert_eq!(
        std::fs::read_to_string(game_dir.join("textures/ground.dds")).unwrap(),
        "patch_ground"
    );
    assert_eq!(
        std::fs::read_to_string(game_dir.join("patch.esp")).unwrap(),
        "patch_plugin"
    );
}

#[tokio::test]
async fn test_profile_roundtrip_all_sources() {
    let profiles = vec![
        Profile {
            id: None,
            name: "manual".to_string(),
            game_id: GameId::from("skyrim-se"),
            source: ProfileSource::Manual,
            mods: vec![simple_mod("mod_a", true)],
            overrides: PathBuf::from("/tmp"),
            load_order_rules: smallvec![],
            load_order_lock: None,
        },
        Profile {
            id: None,
            name: "nexus".to_string(),
            game_id: GameId::from("fallout4"),
            source: ProfileSource::NexusCollection {
                slug: "cool-collection".to_string(),
                version: "2.1".to_string(),
            },
            mods: vec![
                simple_mod("nexus_mod", true),
                simple_mod("optional_mod", false),
            ],
            overrides: PathBuf::from("/tmp"),
            load_order_rules: smallvec![LoadOrderRule::Incompatible {
                mod_a: ModId::from("nexus_mod"),
                mod_b: ModId::from("conflicting_mod"),
            },],
            load_order_lock: None,
        },
        Profile {
            id: None,
            name: "wabbajack".to_string(),
            game_id: GameId::from("skyrim-se"),
            source: ProfileSource::Wabbajack {
                manifest_hash: "abc123def456".to_string(),
            },
            mods: vec![],
            overrides: PathBuf::from("/tmp"),
            load_order_rules: smallvec![],
            load_order_lock: None,
        },
    ];

    let pm = ProfileManager::with_db(ModdeDb::open_memory().unwrap());
    for profile in &profiles {
        pm.create(profile).unwrap();
        let loaded = pm.load(&profile.name, None).unwrap();
        assert_eq!(loaded.name, profile.name);
        assert_eq!(loaded.game_id, profile.game_id);
        assert_eq!(loaded.mods.len(), profile.mods.len());
    }
}

#[tokio::test]
async fn test_resolve_and_detect_incompatible() {
    let profile = Profile {
        id: None,
        name: "conflict".to_string(),
        game_id: GameId::from("skyrim-se"),
        source: ProfileSource::Manual,
        mods: vec![simple_mod("enbseries", true), simple_mod("reshade", true)],
        overrides: PathBuf::from("/tmp"),
        load_order_rules: smallvec![LoadOrderRule::Incompatible {
            mod_a: ModId::from("enbseries"),
            mod_b: ModId::from("reshade"),
        },],
        load_order_lock: None,
    };

    let result = resolve(&profile);
    assert!(result.is_err(), "should detect incompatible mods");
}

#[tokio::test]
async fn test_conflict_map_populated_during_build() {
    let tmp = TempDir::new().unwrap();
    let store = tmp.path().join("store");

    // Two mods providing the same file
    let mod_a_dir = store.join("mod_a");
    std::fs::create_dir_all(&mod_a_dir).unwrap();
    std::fs::write(mod_a_dir.join("shared.esp"), "mod_a_version").unwrap();

    let mod_b_dir = store.join("mod_b");
    std::fs::create_dir_all(&mod_b_dir).unwrap();
    std::fs::write(mod_b_dir.join("shared.esp"), "mod_b_version").unwrap();

    let mut mod_files: HashMap<ModId, Vec<(String, PathBuf)>> = HashMap::new();
    mod_files.insert(
        ModId::from("mod_a"),
        vec![("shared.esp".into(), mod_a_dir.join("shared.esp"))],
    );
    mod_files.insert(
        ModId::from("mod_b"),
        vec![("shared.esp".into(), mod_b_dir.join("shared.esp"))],
    );

    // Build conflict map manually (as the resolver would)
    let mut conflict_map = ConflictMap::default();
    for (mod_id, files) in &mod_files {
        for (rel_path, _) in files {
            conflict_map.register(rel_path.clone(), mod_id.clone());
        }
    }

    let conflicts = conflict_map.conflicts();
    assert_eq!(conflicts.len(), 1);
    assert_eq!(conflicts[0].0, "shared.esp");
    assert!(conflicts[0].1.contains("mod_a"));
    assert!(conflicts[0].1.contains("mod_b"));
}

// ── Smoke test: profile manager lifecycle ───────────────────────────

#[test]
fn test_profile_manager_full_lifecycle() {
    let mgr = ProfileManager::with_db(ModdeDb::open_memory().unwrap());

    // Initially empty
    assert!(mgr.list().unwrap().is_empty());

    // Create profiles
    let p1 = Profile {
        id: None,
        name: "alpha".to_string(),
        game_id: GameId::from("skyrim-se"),
        source: ProfileSource::Manual,
        mods: vec![simple_mod("mod_a", true)],
        overrides: PathBuf::from("/tmp"),
        load_order_rules: smallvec![],
        load_order_lock: None,
    };
    let p2 = Profile {
        id: None,
        name: "beta".to_string(),
        game_id: GameId::from("fallout4"),
        source: ProfileSource::Manual,
        mods: vec![],
        overrides: PathBuf::from("/tmp"),
        load_order_rules: smallvec![],
        load_order_lock: None,
    };

    mgr.create(&p1).unwrap();
    mgr.create(&p2).unwrap();

    // List should have both
    let mut names: Vec<String> = mgr.list().unwrap().into_iter().map(|s| s.name).collect();
    names.sort();
    assert_eq!(names, vec!["alpha", "beta"]);

    // Load and verify
    let loaded = mgr.load("alpha", None).unwrap();
    assert_eq!(loaded.game_id, "skyrim-se");
    assert_eq!(loaded.mods.len(), 1);

    // Duplicate create should fail
    assert!(mgr.create(&p1).is_err());

    // Delete
    mgr.delete("alpha", None).unwrap();
    let names: Vec<String> = mgr.list().unwrap().into_iter().map(|s| s.name).collect();
    assert_eq!(names, vec!["beta"]);

    // Load deleted should fail
    assert!(mgr.load("alpha", None).is_err());

    // Delete nonexistent should fail
    assert!(mgr.delete("alpha", None).is_err());
}