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
417
418
419
420
use smallvec::smallvec;
use std::path::PathBuf;

use modde_core::GameId;
use modde_core::ModdeDb;
use modde_core::error::CoreError;
use modde_core::installer::{InstallMethod, InstallStatus};
use modde_core::profile::{EnabledMod, Profile, ProfileManager, ProfileSource};
use modde_core::resolver::{LoadOrderRule, ModId};
use pretty_assertions::assert_eq;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn make_manual_profile(name: &str, game_id: &str, mods: Vec<EnabledMod>) -> Profile {
    Profile {
        id: None,
        name: name.to_string(),
        game_id: GameId::from(game_id),
        source: ProfileSource::Manual,
        mods,
        overrides: PathBuf::from("/tmp/overrides"),
        load_order_rules: smallvec![],
        load_order_lock: None,
    }
}

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

/// Assert two profiles have identical content by comparing every field.
/// The types intentionally do not derive `PartialEq`, so we compare field-by-field
/// via their TOML serialization which is deterministic.
fn assert_profiles_eq(a: &Profile, b: &Profile) {
    let a_toml = toml::to_string_pretty(a).expect("serialize a");
    let b_toml = toml::to_string_pretty(b).expect("serialize b");
    assert_eq!(a_toml, b_toml);
}

#[test]
fn legacy_enabled_mod_metadata_deserializes_to_typed_fields() {
    let method_raw = toml::to_string(&InstallMethod::BareExtract).unwrap();
    let legacy = format!(
        "mod_id = \"legacy\"\n\
         enabled = true\n\
         install_status = \"pending_user_input\"\n\
         install_method = {method_raw:?}\n\
         tags = '[\"quest\",\"ui\"]'\n"
    );

    let enabled: EnabledMod = toml::from_str(&legacy).unwrap();
    assert_eq!(
        enabled.install_status,
        Some(InstallStatus::PendingUserInput)
    );
    assert_eq!(enabled.install_method, Some(InstallMethod::BareExtract));
    assert_eq!(enabled.tags, vec!["quest".to_string(), "ui".to_string()]);

    let missing_status: EnabledMod =
        toml::from_str("mod_id = \"legacy-missing\"\nenabled = true\n").unwrap();
    assert_eq!(missing_status.install_status, None);
    assert!(missing_status.tags.is_empty());
}

// ===========================================================================
// Profile serialization tests
// ===========================================================================

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

    let profile = make_manual_profile(
        "my-profile",
        "skyrim-se",
        vec![
            simple_mod("skse", true),
            simple_mod("ussep", true),
            simple_mod("broken-mod", false),
        ],
    );

    pm.create(&profile).unwrap();
    let loaded = pm.load("my-profile", None).unwrap();

    assert_profiles_eq(&profile, &loaded);
}

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

    let profile = make_manual_profile("nested", "fallout4", vec![]);
    pm.create(&profile).unwrap();

    let loaded = pm.load("nested", None).unwrap();
    assert_eq!(loaded.name, "nested");
}

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

    let err = pm.load("does-not-exist", None).unwrap_err();
    assert!(
        matches!(err, CoreError::ProfileNotFound(_)),
        "expected ProfileNotFound error, got: {err:?}"
    );
}

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

    let err = pm
        .load("does-not-exist", Some(&GameId::from("skyrim-se")))
        .unwrap_err();
    assert!(
        matches!(err, CoreError::ProfileNotFound(_)),
        "expected ProfileNotFound error, got: {err:?}"
    );
}

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

    let profile = Profile {
        id: None,
        name: "nexus-collection".to_string(),
        game_id: GameId::from("skyrim-se"),
        source: ProfileSource::NexusCollection {
            slug: "living-skyrim".to_string(),
            version: "4.2.0".to_string(),
        },
        mods: vec![simple_mod("skse", true)],
        overrides: PathBuf::from("/tmp/overrides"),
        load_order_rules: smallvec![],
        load_order_lock: None,
    };

    pm.create(&profile).unwrap();
    let loaded = pm.load("nexus-collection", None).unwrap();

    assert_profiles_eq(&profile, &loaded);

    // Also verify the variant fields explicitly
    match &loaded.source {
        ProfileSource::NexusCollection { slug, version } => {
            assert_eq!(slug, "living-skyrim");
            assert_eq!(version, "4.2.0");
        }
        other => panic!("expected NexusCollection, got: {other:?}"),
    }
}

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

    let profile = Profile {
        id: None,
        name: "wj-list".to_string(),
        game_id: GameId::from("skyrim-se"),
        source: ProfileSource::Wabbajack {
            manifest_hash: "abc123def456".to_string(),
        },
        mods: vec![simple_mod("engine-fixes", true)],
        overrides: PathBuf::from("/data/overrides"),
        load_order_rules: smallvec![],
        load_order_lock: None,
    };

    pm.create(&profile).unwrap();
    let loaded = pm.load("wj-list", None).unwrap();

    assert_profiles_eq(&profile, &loaded);

    match &loaded.source {
        ProfileSource::Wabbajack { manifest_hash } => {
            assert_eq!(manifest_hash, "abc123def456");
        }
        other => panic!("expected Wabbajack, got: {other:?}"),
    }
}

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

    let rules = smallvec![
        LoadOrderRule::LoadAfter {
            mod_id: ModId::from("mod_b"),
            after: ModId::from("mod_a"),
        },
        LoadOrderRule::LoadBefore {
            mod_id: ModId::from("mod_c"),
            before: ModId::from("mod_d"),
        },
        LoadOrderRule::Incompatible {
            mod_a: ModId::from("mod_x"),
            mod_b: ModId::from("mod_y"),
        },
    ];

    let profile = Profile {
        id: None,
        name: "with-rules".to_string(),
        game_id: GameId::from("skyrim-se"),
        source: ProfileSource::Manual,
        mods: vec![
            simple_mod("mod_a", true),
            simple_mod("mod_b", true),
            simple_mod("mod_c", true),
            simple_mod("mod_d", true),
        ],
        overrides: PathBuf::from("/tmp/overrides"),
        load_order_rules: rules,
        load_order_lock: None,
    };

    pm.create(&profile).unwrap();
    let loaded = pm.load("with-rules", None).unwrap();

    assert_profiles_eq(&profile, &loaded);
    assert_eq!(loaded.load_order_rules.len(), 3);
}

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

    let mods: Vec<EnabledMod> = (0..500)
        .map(|i| EnabledMod {
            mod_id: format!("mod_{i:04}"),
            enabled: i % 3 != 0, // disable every third
            version: if i % 5 == 0 {
                Some(format!("{}.{}.0", i / 100, i % 100))
            } else {
                None
            },
            fomod_config: None,
            ..Default::default()
        })
        .collect();

    let profile = make_manual_profile("big-list", "fallout4", mods);

    pm.create(&profile).unwrap();
    let loaded = pm.load("big-list", None).unwrap();

    assert_profiles_eq(&profile, &loaded);
    assert_eq!(loaded.mods.len(), 500);
}

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

    let profile = make_manual_profile(
        "versioned",
        "skyrim-se",
        vec![
            EnabledMod {
                mod_id: "skse".to_string(),
                enabled: true,
                version: Some("2.2.6".to_string()),
                fomod_config: None,
                ..Default::default()
            },
            EnabledMod {
                mod_id: "ussep".to_string(),
                enabled: true,
                version: None,
                fomod_config: None,
                ..Default::default()
            },
        ],
    );

    pm.create(&profile).unwrap();
    let loaded = pm.load("versioned", None).unwrap();

    assert_profiles_eq(&profile, &loaded);
    assert_eq!(loaded.mods[0].version.as_deref(), Some("2.2.6"));
    assert_eq!(loaded.mods[1].version, None);
}

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

    let profile = make_manual_profile("empty", "skyrim-se", vec![]);

    pm.create(&profile).unwrap();
    let loaded = pm.load("empty", None).unwrap();

    assert_profiles_eq(&profile, &loaded);
    assert!(loaded.mods.is_empty());
}

// ===========================================================================
// ProfileManager tests
// ===========================================================================

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

    let profiles = pm.list().unwrap();
    assert!(profiles.is_empty());
}

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

    let profile = make_manual_profile(
        "test-profile",
        "skyrim-se",
        vec![simple_mod("skse", true), simple_mod("ussep", true)],
    );

    pm.create(&profile).unwrap();
    let loaded = pm.load("test-profile", None).unwrap();

    assert_eq!(loaded.name, "test-profile");
    assert_eq!(loaded.game_id, "skyrim-se");
    assert_eq!(loaded.mods.len(), 2);
    assert_eq!(loaded.mods[0].mod_id, "skse");
    assert_eq!(loaded.mods[1].mod_id, "ussep");
}

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

    let profile = make_manual_profile("dup", "skyrim-se", vec![]);
    pm.create(&profile).unwrap();

    let err = pm.create(&profile).unwrap_err();
    // SQLite backend returns a constraint violation for duplicate profiles
    assert!(
        matches!(err, CoreError::ProfileAlreadyExists(ref name) if name == "dup")
            || format!("{err:?}").contains("UNIQUE constraint"),
        "expected ProfileAlreadyExists or UNIQUE constraint error, got: {err:?}"
    );
}

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

    let profile = make_manual_profile("to-delete", "skyrim-se", vec![]);
    pm.create(&profile).unwrap();

    // Confirm it exists
    assert_eq!(pm.list().unwrap().len(), 1);

    pm.delete("to-delete", None).unwrap();

    // Confirm it is gone
    assert!(pm.list().unwrap().is_empty());

    let err = pm.load("to-delete", None).unwrap_err();
    assert!(
        matches!(err, CoreError::ProfileNotFound(_)),
        "expected ProfileNotFound after delete, got: {err:?}"
    );
}

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

    let err = pm.delete("ghost", None).unwrap_err();
    assert!(
        matches!(err, CoreError::ProfileNotFound(ref name) if name == "ghost"),
        "expected ProfileNotFound, got: {err:?}"
    );
}

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

    let names = ["alpha", "beta", "gamma", "delta"];
    for name in &names {
        let profile = make_manual_profile(name, "skyrim-se", vec![]);
        pm.create(&profile).unwrap();
    }

    let mut listed: Vec<String> = pm.list().unwrap().into_iter().map(|s| s.name).collect();
    listed.sort();
    let mut expected = names.map(String::from).to_vec();
    expected.sort();

    assert_eq!(listed, expected);
}

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

    // Create a single profile
    let profile = make_manual_profile("real-profile", "skyrim-se", vec![]);
    pm.create(&profile).unwrap();

    let listed = pm.list().unwrap();
    assert_eq!(listed.len(), 1);
    assert_eq!(listed[0].name, "real-profile");
}