using System; using System.IO; using System.Threading.Tasks; using ReminderOrganizer.Models; using ReminderOrganizer.Services; using Xunit; namespace ReminderOrganizer.Tests { public class SyncTests : IDisposable { private readonly string _root = Path.Combine(Path.GetTempPath(), "ro-sync-" + Guid.NewGuid().ToString("N")); public void Dispose() { try { Directory.Delete(_root, true); } catch { } } [Fact] public void Pointer_file_round_trip() { var app = Path.Combine(_root, "app"); var cloud = Path.Combine(_root, "OneDrive", "Reminders"); Directory.CreateDirectory(cloud); Assert.Null(DataService.ReadPointer(app)); DataService.WritePointer(app, cloud); Assert.Equal(cloud, DataService.ReadPointer(app)); DataService.WritePointer(app, app); // back to the app folder = no pointer Assert.Null(DataService.ReadPointer(app)); } [Fact] public void Pointer_to_missing_folder_is_ignored() { var app = Path.Combine(_root, "app"); DataService.WritePointer(app, Path.Combine(_root, "gone")); Directory.CreateDirectory(Path.Combine(_root, "gone")); Directory.Delete(Path.Combine(_root, "gone")); Assert.Null(DataService.ReadPointer(app)); } [Fact] public async Task Own_writes_are_not_external_but_other_writes_are() { var ds = new DataService(Path.Combine(_root, "data")); await ds.SaveAsync(new AppData()); Assert.False(ds.IsExternalChange()); await Task.Delay(30); File.WriteAllText(ds.DataFilePath, "{\"Folders\":[]}"); // "another PC" File.SetLastWriteTimeUtc(ds.DataFilePath, DateTime.UtcNow.AddSeconds(2)); Assert.True(ds.IsExternalChange()); Assert.False(ds.IsExternalChange()); // reported once } [Fact] public void Switching_folder_uses_new_file() { var ds = new DataService(Path.Combine(_root, "a")); ds.SwitchTo(Path.Combine(_root, "b")); Assert.Equal(Path.Combine(_root, "b", "tasks.json"), ds.DataFilePath); } } }