Files
flutter-rimworld-modman/test/mod_list_test.dart
2025-03-17 20:53:38 +01:00

677 lines
21 KiB
Dart

import 'package:rimworld_modman/mod.dart';
import 'package:rimworld_modman/mod_list.dart';
import 'package:test/test.dart';
const root = r'C:/Users/Administrator/Seafile/Games-RimWorld';
const modsRoot = '$root/294100';
const configRoot = '$root/AppData/RimWorld by Ludeon Studios/Config';
const configPath = '$configRoot/ModsConfig.xml';
const logsPath = '$root/ModManager';
// Few things we missed:
// 1. BuildLoadOrder should either return or throw a List<String> in addition to... List<String>
// That will represent errors
// Those would be incompatibilities, missing dependencies, unable to setup before/after and so on
// 2. Write tests for missing dependencies
// 3. Maybe write tests for missing loadBefore/loadAfter
Mod makeDummy() {
return Mod(
name: 'Dummy Mod',
id: 'dummy',
path: '',
versions: ["1.5"],
description: '',
dependencies: [],
loadAfter: [],
loadBefore: [],
incompatibilities: [],
size: 0,
isBaseGame: false,
isExpansion: false,
enabled: false,
);
}
void main() {
group('Test sorting', () {
test('Harmony should load before RimWorld', () {
final list = ModList();
list.mods = {
'harmony': makeDummy().copyWith(name: 'Harmony', id: 'harmony'),
'ludeon.rimworld': makeDummy().copyWith(
name: 'RimWorld',
id: 'ludeon.rimworld',
),
};
list.enableAll();
final order = list.generateLoadOrder();
final harmonyIndex = order.loadOrder.indexOf('harmony');
final rimworldIndex = order.loadOrder.indexOf('ludeon.rimworld');
expect(order.errors, isEmpty);
expect(harmonyIndex, lessThan(rimworldIndex));
});
test('Prepatcher should load after Harmony and RimWorld', () {
final list = ModList();
list.mods = {
'prepatcher': makeDummy().copyWith(
name: 'Prepatcher',
id: 'prepatcher',
dependencies: ['harmony'],
loadAfter: ['ludeon.rimworld'],
),
'harmony': makeDummy().copyWith(
name: 'Harmony',
id: 'harmony',
loadBefore: ['ludeon.rimworld'],
),
'ludeon.rimworld': makeDummy().copyWith(
name: 'RimWorld',
id: 'ludeon.rimworld',
),
};
list.enableAll();
final order = list.generateLoadOrder();
final prepatcherIndex = order.loadOrder.indexOf('prepatcher');
final harmonyIndex = order.loadOrder.indexOf('harmony');
final rimworldIndex = order.loadOrder.indexOf('ludeon.rimworld');
expect(order.errors, isEmpty);
expect(prepatcherIndex, greaterThan(harmonyIndex));
expect(prepatcherIndex, greaterThan(rimworldIndex));
});
test('RimWorld should load before Anomaly', () {
final list = ModList();
list.mods = {
'ludeon.rimworld': makeDummy().copyWith(
name: 'RimWorld',
id: 'ludeon.rimworld',
),
'ludeon.rimworld.anomaly': makeDummy().copyWith(
name: 'RimWorld Anomaly',
id: 'ludeon.rimworld.anomaly',
),
};
list.enableAll();
final order = list.generateLoadOrder();
final rimworldIndex = order.loadOrder.indexOf('ludeon.rimworld');
final anomalyIndex = order.loadOrder.indexOf('ludeon.rimworld.anomaly');
expect(order.errors, isEmpty);
expect(rimworldIndex, lessThan(anomalyIndex));
});
test('Disabled dummy mod should not be loaded', () {
final list = ModList();
list.mods = {
'disabledDummy': makeDummy().copyWith(
name: 'Disabled Dummy',
id: 'disabledDummy',
),
};
list.disableAll();
final order = list.generateLoadOrder();
final disabledIndex = order.loadOrder.indexOf('disabledDummy');
expect(disabledIndex, isNegative);
});
test('Larger mods should load before smaller ones', () {
final list = ModList();
list.mods = {
'smol': makeDummy().copyWith(name: 'Smol', id: 'smol', size: 100),
'yuuuge': makeDummy().copyWith(
name: 'Yuuuge',
id: 'yuuuge',
size: 10000,
),
};
list.enableAll();
final order = list.generateLoadOrder();
final smolIndex = order.loadOrder.indexOf('smol');
final yuuugeIndex = order.loadOrder.indexOf('yuuuge');
expect(order.errors, isEmpty);
expect(yuuugeIndex, lessThan(smolIndex));
});
test('Incompatible mods should return errors', () {
final list = ModList();
list.mods = {
'incompatible': makeDummy().copyWith(
name: 'Incompatible',
id: 'incompatible',
incompatibilities: ['harmony'],
),
'harmony': makeDummy().copyWith(name: 'Harmony', id: 'harmony'),
};
list.enableAll();
final result = list.generateLoadOrder();
expect(result.errors, isNotEmpty);
expect(result.errors.any((e) => e.contains('incompatible')), isTrue);
expect(result.errors.any((e) => e.contains('harmony')), isTrue);
});
});
group('Test loadRequired', () {
test('Dependencies should be automatically enabled', () {
final list = ModList();
list.mods = {
'prepatcher': makeDummy().copyWith(
name: 'Prepatcher',
id: 'prepatcher',
dependencies: ['harmony'],
),
'harmony': makeDummy().copyWith(name: 'Harmony', id: 'harmony'),
};
list.disableAll();
list.setEnabled('prepatcher', true);
final order = list.loadRequired();
expect(order.errors, isEmpty);
expect(order.loadOrder.indexOf('harmony'), isNot(-1));
});
test('Only required mods should be enabled', () {
final list = ModList();
list.mods = {
'prepatcher': makeDummy().copyWith(
name: 'Prepatcher',
id: 'prepatcher',
dependencies: ['harmony'],
),
'harmony': makeDummy().copyWith(name: 'Harmony', id: 'harmony'),
'dummy': makeDummy(),
};
list.disableAll();
list.setEnabled('prepatcher', true);
final order = list.loadRequired();
expect(order.errors, isEmpty);
expect(order.loadOrder.indexOf('harmony'), isNot(-1));
expect(order.loadOrder.indexOf('dummy'), -1);
});
test('Incompatible mods should return errors', () {
final list = ModList();
list.mods = {
'incompatible': makeDummy().copyWith(
name: 'Incompatible',
id: 'incompatible',
incompatibilities: ['harmony'],
),
'prepatcher': makeDummy().copyWith(
name: 'Prepatcher',
id: 'prepatcher',
dependencies: ['harmony'],
),
'harmony': makeDummy().copyWith(name: 'Harmony', id: 'harmony'),
};
list.disableAll();
list.setEnabled('incompatible', true);
list.setEnabled('prepatcher', true);
final result = list.loadRequired();
expect(result.errors, isNotEmpty);
expect(result.errors.any((e) => e.contains('incompatible')), isTrue);
expect(result.errors.any((e) => e.contains('harmony')), isTrue);
});
test('Dependencies of dependencies should be loaded', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(
name: 'Mod A',
id: 'modA',
dependencies: ['modB'],
),
'modB': makeDummy().copyWith(
name: 'Mod B',
id: 'modB',
dependencies: ['modC'],
),
'modC': makeDummy().copyWith(name: 'Mod C', id: 'modC'),
};
list.disableAll();
list.setEnabled('modA', true);
final order = list.loadRequired();
expect(order.errors, isEmpty);
expect(order.loadOrder.indexOf('modA'), isNot(-1));
expect(order.loadOrder.indexOf('modB'), isNot(-1));
expect(order.loadOrder.indexOf('modC'), isNot(-1));
});
});
group('Test cyclic dependencies', () {
test('Cyclic dependencies should return errors', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(
name: 'Mod A',
id: 'modA',
dependencies: ['modB'],
),
'modB': makeDummy().copyWith(
name: 'Mod B',
id: 'modB',
dependencies: ['modC'],
),
'modC': makeDummy().copyWith(
name: 'Mod C',
id: 'modC',
dependencies: ['modA'],
),
};
list.disableAll();
list.setEnabled('modA', true);
final result = list.loadRequired();
expect(result.errors, isNotEmpty);
expect(result.errors.any((e) => e.contains('modA')), isTrue);
expect(result.errors.any((e) => e.contains('modB')), isTrue);
expect(result.errors.any((e) => e.contains('modC')), isTrue);
});
});
group('Test soft constraints', () {
test('Load preferences should be respected when possible', () {
final dummy = makeDummy();
final list = ModList();
list.mods = {
'modA': dummy.copyWith(
name: 'Mod A',
id: 'modA',
loadAfter: ['modB'],
loadBefore: ['modC'],
),
'modB': dummy.copyWith(name: 'Mod B', id: 'modB'),
'modC': dummy.copyWith(name: 'Mod C', id: 'modC'),
};
list.enableAll();
final order = list.generateLoadOrder();
final aIndex = order.loadOrder.indexOf('modA');
final bIndex = order.loadOrder.indexOf('modB');
final cIndex = order.loadOrder.indexOf('modC');
expect(order.errors, isEmpty);
expect(aIndex, greaterThan(bIndex));
expect(aIndex, lessThan(cIndex));
});
});
group('Test conflict detection', () {
test('All conflicts should be correctly identified', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(
name: 'Mod A',
id: 'modA',
incompatibilities: ['modB', 'modC'],
),
'modB': makeDummy().copyWith(name: 'Mod B', id: 'modB'),
'modC': makeDummy().copyWith(name: 'Mod C', id: 'modC'),
};
list.enableAll();
final conflicts = list.checkIncompatibilities(
list.activeMods.keys.toList(),
);
expect(conflicts.length, equals(2));
// Check if conflicts contain these pairs (order doesn't matter)
expect(
conflicts.any(
(c) =>
(c[0] == 'modA' && c[1] == 'modB') ||
(c[0] == 'modB' && c[1] == 'modA'),
),
isTrue,
);
expect(
conflicts.any(
(c) =>
(c[0] == 'modA' && c[1] == 'modC') ||
(c[0] == 'modC' && c[1] == 'modA'),
),
isTrue,
);
});
});
group('Test enable/disable functionality', () {
test('Enable and disable methods should work correctly', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(name: 'Mod A', id: 'modA'),
'modB': makeDummy().copyWith(name: 'Mod B', id: 'modB'),
};
list.enableAll();
for (final mod in list.mods.values) {
expect(mod.enabled, isTrue);
}
list.disableAll();
for (final mod in list.mods.values) {
expect(mod.enabled, isFalse);
}
});
});
group('Test base game and expansion handling', () {
test('Base game and expansions should be correctly ordered', () {
final list = ModList();
list.mods = {
'ludeon.rimworld': makeDummy().copyWith(
name: 'RimWorld',
id: 'ludeon.rimworld',
isBaseGame: true,
),
'ludeon.rimworld.anomaly': makeDummy().copyWith(
name: 'RimWorld Anomaly',
id: 'ludeon.rimworld.anomaly',
dependencies: ['ludeon.rimworld'],
isExpansion: true,
),
};
list.enableAll();
final order = list.generateLoadOrder();
// Base game should load before any expansions
final baseGameIndex = order.loadOrder.indexOf('ludeon.rimworld');
final expansionIndex = order.loadOrder.indexOf('ludeon.rimworld.anomaly');
expect(order.errors, isEmpty);
expect(baseGameIndex, lessThan(expansionIndex));
});
});
group('Test complex dependency chains', () {
test('Complex dependency chains should resolve correctly', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(
name: 'Mod A',
id: 'modA',
dependencies: ['modB'],
),
'modB': makeDummy().copyWith(
name: 'Mod B',
id: 'modB',
dependencies: ['modC'],
),
'modC': makeDummy().copyWith(
name: 'Mod C',
id: 'modC',
dependencies: ['modD'],
),
'modD': makeDummy().copyWith(name: 'Mod D', id: 'modD'),
};
list.disableAll();
list.setEnabled('modA', true);
final result = list.loadRequired();
expect(result.errors, isEmpty);
// All mods in the chain should be enabled
expect(result.loadOrder.contains('modA'), isTrue);
expect(result.loadOrder.contains('modB'), isTrue);
expect(result.loadOrder.contains('modC'), isTrue);
expect(result.loadOrder.contains('modD'), isTrue);
// The order should be D -> C -> B -> A
expect(
result.loadOrder.indexOf('modD'),
lessThan(result.loadOrder.indexOf('modC')),
);
expect(
result.loadOrder.indexOf('modC'),
lessThan(result.loadOrder.indexOf('modB')),
);
expect(
result.loadOrder.indexOf('modB'),
lessThan(result.loadOrder.indexOf('modA')),
);
});
});
group('Test missing dependencies', () {
test('Should detect missing dependencies and return errors', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(
name: 'Mod A',
id: 'modA',
dependencies: ['modB', 'nonExistentMod'],
),
'modB': makeDummy().copyWith(name: 'Mod B', id: 'modB'),
};
list.enableAll();
// This should throw an exception because the dependency doesn't exist
final result = list.generateLoadOrder();
expect(result.errors, isNotEmpty);
expect(result.errors.any((e) => e.contains('nonExistentMod')), isTrue);
});
test('Should handle multiple missing dependencies correctly', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(
name: 'Mod A',
id: 'modA',
dependencies: ['missing1'],
),
'modB': makeDummy().copyWith(
name: 'Mod B',
id: 'modB',
dependencies: ['missing2'],
),
};
list.enableAll();
// Should return errors with both missing dependencies mentioned
final result = list.generateLoadOrder();
expect(result.errors, isNotEmpty);
expect(result.errors.any((e) => e.contains('missing1')), isTrue);
expect(result.errors.any((e) => e.contains('missing2')), isTrue);
});
});
group('Test missing loadBefore/loadAfter relationships', () {
test('Should handle missing loadBefore relationships', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(
name: 'Mod A',
id: 'modA',
loadBefore: ['nonExistentMod'],
),
};
list.enableAll();
// Should not throw exception for soft constraints
// But might generate a warning that we could check for
final order = list.generateLoadOrder();
expect(order.errors, isEmpty);
expect(order.loadOrder.contains('modA'), isTrue);
});
test('Should handle missing loadAfter relationships', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(
name: 'Mod A',
id: 'modA',
loadAfter: ['nonExistentMod'],
),
};
list.enableAll();
// Should not throw exception for soft constraints
final order = list.generateLoadOrder();
expect(order.errors, isEmpty);
expect(order.loadOrder.contains('modA'), isTrue);
});
});
group('Test BuildLoadOrder error handling', () {
test('Should return errors for incompatibilities', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(
name: 'Mod A',
id: 'modA',
incompatibilities: ['modB'],
),
'modB': makeDummy().copyWith(name: 'Mod B', id: 'modB'),
};
list.enableAll();
final result = list.generateLoadOrder();
expect(result.errors, isNotEmpty);
expect(result.errors.any((e) => e.contains('Incompatible')), isTrue);
expect(result.errors.any((e) => e.contains('modA')), isTrue);
expect(result.errors.any((e) => e.contains('modB')), isTrue);
});
test('Should handle a combination of errors simultaneously', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(
name: 'Mod A',
id: 'modA',
dependencies: ['missingDep'],
incompatibilities: ['modB'],
),
'modB': makeDummy().copyWith(name: 'Mod B', id: 'modB'),
};
list.enableAll();
final result = list.generateLoadOrder();
expect(result.errors, isNotEmpty);
expect(result.errors.any((e) => e.contains('missingDep')), isTrue);
expect(result.errors.any((e) => e.contains('Incompatible')), isTrue);
});
});
group('Test dependency resolution with constraints', () {
test(
'Should resolve dependencies while respecting load order constraints',
() {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(
name: 'Mod A',
id: 'modA',
dependencies: ['modB'],
loadAfter: ['modC'],
),
'modB': makeDummy().copyWith(name: 'Mod B', id: 'modB'),
'modC': makeDummy().copyWith(name: 'Mod C', id: 'modC'),
};
list.enableAll();
final order = list.generateLoadOrder();
expect(order.errors, isEmpty);
// modB should load before modA due to dependency
expect(
order.loadOrder.indexOf('modB'),
lessThan(order.loadOrder.indexOf('modA')),
);
// modC should load before modA due to loadAfter constraint
expect(
order.loadOrder.indexOf('modC'),
lessThan(order.loadOrder.indexOf('modA')),
);
},
);
test('Should detect and report conflicting constraints', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(
name: 'Mod A',
id: 'modA',
loadBefore: ['modB'],
),
'modB': makeDummy().copyWith(
name: 'Mod B',
id: 'modB',
loadBefore: ['modA'],
),
};
list.enableAll();
// These constraints create a circular dependency which should cause an error
try {
list.generateLoadOrder();
fail('Expected an exception to be thrown due to circular constraints');
} catch (e) {
// Verify error is about circular dependencies or conflicting constraints
expect(
e.toString().toLowerCase().contains('conflict') ||
e.toString().toLowerCase().contains('circular'),
isTrue,
);
}
});
});
group('Test BuildLoadOrder with result object', () {
test('Should return successful load order with no errors', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(name: 'Mod A', id: 'modA'),
'modB': makeDummy().copyWith(name: 'Mod B', id: 'modB'),
};
list.enableAll();
final result = list.generateLoadOrder();
expect(result.errors, isEmpty);
expect(result.loadOrder.length, equals(2));
expect(result.loadOrder.contains('modA'), isTrue);
expect(result.loadOrder.contains('modB'), isTrue);
});
test('Should return errors for missing dependencies', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(
name: 'Mod A',
id: 'modA',
dependencies: ['nonExistentMod'],
),
};
list.enableAll();
final result = list.generateLoadOrder();
expect(result.errors, isNotEmpty);
expect(result.errors.any((e) => e.contains('nonExistentMod')), isTrue);
// The load order might be empty or contain valid mods only
});
test('Should return both valid load order and errors', () {
final list = ModList();
list.mods = {
'modA': makeDummy().copyWith(name: 'Mod A', id: 'modA'),
'modB': makeDummy().copyWith(
name: 'Mod B',
id: 'modB',
dependencies: ['nonExistentMod'],
),
};
list.enableAll();
final result = list.generateLoadOrder();
expect(result.errors, isNotEmpty);
expect(result.errors.any((e) => e.contains('nonExistentMod')), isTrue);
// modA should still be in the load order even though modB has errors
expect(result.loadOrder.contains('modA'), isTrue);
// modB might be excluded from load order due to its error
});
});
}