using System.Security.Cryptography; using XFEExtension.NetCore.XUnit.Attributes; using XFEEDToolBox.Core.Plugins; using XFEEDToolBox.Core.Session; using XFEEDToolBox.Core.Models; namespace XFEEDToolBox.Test; [TestFixture] public class TranslationTests { private sealed class Fixture:IDisposable { public readonly TemporaryDirectory Temp=new();public string Game{get;}public string Source{get;}public ChineseTranslationManager Manager{get;} public Fixture() { Game=Temp.File("EliteDangerous64.exe");File.WriteAllText(Game,"test game");Source=Temp.File("source");Directory.CreateDirectory(Source); File.WriteAllText(Path.Combine(Source,"dbghelp.dll"),"test translation loader");File.WriteAllText(Path.Combine(Source,"edchs.pkg"),"test translation resources"); Manager=new(new(Hash(Game),new Dictionary{{"dbghelp.dll",Hash(Path.Combine(Source,"dbghelp.dll"))},{"edchs.pkg",Hash(Path.Combine(Source,"edchs.pkg"))}})); } private static string Hash(string path)=>Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(path))); public void Dispose()=>Temp.Dispose(); } [Test]public async Task LocalTranslationInstallAndRestoreLeaveTradePluginAndGameUnchanged() { using var f=new Fixture();File.WriteAllText(f.Temp.File("dinput8.dll"),"trade plugin");var before=File.ReadAllText(f.Game); var state=await f.Manager.InstallAsync(f.Game,f.Source);Check.That(state.Installed&&state.Owned); Check.That(!(await f.Manager.UninstallAsync(f.Game)).Installed);Check.Equal(before,File.ReadAllText(f.Game));Check.Equal("trade plugin",File.ReadAllText(f.Temp.File("dinput8.dll"))); } [Test]public async Task ExternalTranslationIsRecognizedWithoutTakingOwnership() { using var f=new Fixture();foreach(var file in Directory.GetFiles(f.Source))File.Copy(file,f.Temp.File(Path.GetFileName(file))); var state=await f.Manager.InspectAsync(f.Game);Check.That(state.Installed&&!state.Owned); try{await f.Manager.UninstallAsync(f.Game);throw new Exception("Expected refusal");}catch(InvalidOperationException){} Check.That(File.Exists(f.Temp.File("edchs.pkg"))); } [Test]public async Task ImportedTranslationInstallsAfterOriginalSourceIsRemoved() { using var f=new Fixture();var cached=await f.Manager.ImportBundleAsync(f.Source,f.Temp.File("resource-store")); Directory.Move(f.Source,f.Source+".removed"); var state=await f.Manager.InstallAsync(f.Game,cached);Check.That(state.Installed&&state.Owned); Check.Equal("test translation resources",File.ReadAllText(f.Temp.File("edchs.pkg"))); Check.That(!(await f.Manager.UninstallAsync(f.Game)).Installed); } [Test]public async Task CorruptOrConflictingComponentsCannotBeInstalled() { using var f=new Fixture();File.WriteAllText(f.Temp.File("dbghelp.dll"),"unrelated component"); try{await f.Manager.InstallAsync(f.Game,f.Source);throw new Exception("Expected refusal");}catch(InvalidOperationException){} Check.Equal("unrelated component",File.ReadAllText(f.Temp.File("dbghelp.dll")));Check.That(!File.Exists(f.Temp.File("edchs.pkg"))); } [Test]public void DefaultsContainBindingsWhileRegistrationIsOff() { var settings=new HotkeySettings();Check.That(!settings.Enabled);Check.Equal("Ctrl+Alt+F9",settings.Relog);Check.Equal("Ctrl+Alt+F10",settings.Restart); using var temp=new TemporaryDirectory();var file=temp.File("settings.json");File.WriteAllText(file,"""{"hotkeys":{"enabled":false,"relog":"","restart":""}}"""); var migrated=ToolboxSettings.Load(file);Check.Equal(settings,migrated.Hotkeys); (migrated with{Hotkeys=new(false,"","")}).Save(file);Check.Equal("",ToolboxSettings.Load(file).Hotkeys.Relog); } }