public void Update_RemovedEntries_RemoveMissingItems(
            SubtitleStateEntry[] entries,
            SubtitleState state)
        {
            var failedFiles = entries.Skip(1).Select(e => e.File);
            state.Entries.Clear();
            state.Entries.AddRange(entries);

            state.Update(failedFiles);

            Assert.That(state.Entries.Select(e => e.File), Is.EquivalentTo(failedFiles));
        }
        public void Update_NewEntries_AddNewEntries(
            SubtitleStateEntry[] newEntries,
            SubtitleStateEntry[] existingEntries,
            SubtitleState state)
        {
            var failedFiles = existingEntries.Concat(newEntries).Select(e => e.File);
            state.Entries.Clear();
            state.Entries.AddRange(existingEntries);

            state.Update(failedFiles);

            Assert.That(state.Entries.Select(e => e.File), Is.EquivalentTo(failedFiles));
        }
        public void Update_ExistingEntriesMatching_LeaveExistingItemsUnchanged(
            SubtitleStateEntry[] entries,
            SubtitleState state
            )
        {
            var failedFiles = entries.Select(e => e.File);
            state.Entries.Clear();
            state.Entries.AddRange(entries);

            state.Update(failedFiles);

            Assert.That(entries.Select(e => new { e.File, e.Timestamp }),
                Is.EquivalentTo(state.Entries.Select(e => new { e.File, e.Timestamp })));
        }
        public void Run_Always_SavesState()
        {
            var fileSystem = A.Fake<IFileOperations>();
            var serializer = A.Fake<IStateSerializer>();
            var subtitleState = new SubtitleState();
            A.CallTo(() => serializer.LoadState()).Returns(subtitleState);
            var fileProcessor = A.Fake<IFileProcessor>();
            var application = new DownloaderApplication(fileSystem, serializer, fileProcessor);
            var options = new Options();

            application.Run(options);

            A.CallTo(() => serializer.SaveState(subtitleState)).MustHaveHappened();
        }
        public void Cleanup_EntriesAreOld_RemoveAllEntriesOlderThanGiveupdays(
            int giveupdays,
            Action<SubtitleStateEntry> removalAction,
            SubtitleStateEntry[] entries,
            SubtitleState state)
        {
            var toBeRemoved = entries.Take(2);
            toBeRemoved.ForEach(e => e.Timestamp = DateTime.Now.AddDays(-giveupdays - 1));
            var toKeep = entries.Skip(2);
            toKeep.ForEach(e => e.Timestamp = DateTime.Now);
            state.Entries.Clear();
            state.Entries.AddRange(entries);

            state.Cleanup(giveupdays, removalAction);

            Assert.That(state.Entries.Select(s => s.File), Is.EquivalentTo(toKeep.Select(s => s.File)));
        }
 public void SaveState(SubtitleState state)
 {
     var xs = new XmlSerializer(typeof(SubtitleState));
     try
     {
         using (var writer = new StreamWriter(stateFileName))
         {
             logger.Debug("StateSerializer", string.Format("Saving state to {0}...", stateFileName));
             xs.Serialize(writer, state);
             logger.Debug("StateSerializer", "State saved.");
         }
     }
     catch (Exception e)
     {
         logger.Error("StateSerializer", "Could not save state. Exception: {0}.", e.Message);
     }
 }
        public void Run_IfFilesFail_AddsThemToState()
        {
            var files = new[] {"fail1.avi", "success.avi", "fail2.avi"};
            var fileSystem = A.Fake<IFileOperations>();
            A.CallTo(() => fileSystem.GetFilesToProcess(A<IEnumerable<string>>._, A<IEnumerable<string>>._)).Returns(files);
            var serializer = A.Fake<IStateSerializer>();
            var subtitleState = new SubtitleState();
            A.CallTo(() => serializer.LoadState()).Returns(subtitleState);
            var fileProcessor = A.Fake<IFileProcessor>();
            A.CallTo(() => fileProcessor.ProcessFile(A<string>.That.StartsWith("fail"), A<IEnumerable<string>>._)).Returns(false);
            A.CallTo(() => fileProcessor.ProcessFile(A<string>.That.StartsWith("success"), A<IEnumerable<string>>._)).Returns(true);
            var application = new DownloaderApplication(fileSystem, serializer, fileProcessor);
            var options = new Options { GiveupDays = 99 };

            application.Run(options);

            Assert.That(subtitleState.Entries.Select(s => s.File), Is.EquivalentTo(new []{ "fail1.avi", "fail2.avi"}));
        }
        public void Run_FilesAreCleanedUp_CleanedFilesGetsNosrtWritten()
        {
            var fileSystem = A.Fake<IFileOperations>();
            var serializer = A.Fake<IStateSerializer>();
            var subtitleState = new SubtitleState();
            subtitleState.Entries.Add(new SubtitleStateEntry("cleaned1", DateTime.Now.AddDays(-100)));
            subtitleState.Entries.Add(new SubtitleStateEntry("cleaned2", DateTime.Now.AddDays(-101)));
            subtitleState.Entries.Add(new SubtitleStateEntry("stays", DateTime.Now.AddDays(-1)));
            A.CallTo(() => serializer.LoadState()).Returns(subtitleState);
            var fileProcessor = A.Fake<IFileProcessor>();
            var application = new DownloaderApplication(fileSystem, serializer, fileProcessor);
            var options = new Options {GiveupDays = 99};

            application.Run(options);

            A.CallTo(() => fileSystem.CreateNosrtFile(A<SubtitleStateEntry>.That.Matches(s => s.File.StartsWith("cleaned")))).MustHaveHappened(Repeated.Exactly.Times(2));
            A.CallTo(() => fileSystem.CreateNosrtFile(A<SubtitleStateEntry>.That.Matches(s => s.File.StartsWith("stays")))).MustNotHaveHappened();
        }