public void Create_SourceLiftLocked_GivesMessageReturnsFalse()
        {
            using (var lift = new TempLiftFile(""))
            {
                using (var stream = File.OpenWrite(lift.Path))
                {
                    var targetDir = Path.Combine(Path.GetTempPath(), "ProjectFromFLExCreatorTests");

                    using (var x = new ErrorReport.NonFatalErrorReportExpected())
                    {
                        Assert.IsFalse(ProjectFromRawFLExLiftFilesCreator.Create(targetDir, lift.Path));
                    }
                }
            }
        }
        public void SyncLocalAndRepoCollectionFiles_SyncsInRightDirection()
        {
            using (var collectionFolder =
                       new TemporaryFolder("SyncLocalAndRepoCollectionFiles_SyncsInRightDirection_Collection"))
            {
                using (var repoFolder =
                           new TemporaryFolder("SyncLocalAndRepoCollectionFiles_SyncsInRightDirection_Shared"))
                {
                    var settingsFileName =
                        Path.ChangeExtension(Path.GetFileName(collectionFolder.FolderPath), "bloomCollection");
                    var settingsPath = Path.Combine(collectionFolder.FolderPath, settingsFileName);
                    var tcManager    = new TeamCollectionManager(settingsPath, null, new BookRenamedEvent(),
                                                                 new BookStatusChangeEvent(), null, null);
                    var tc = new FolderTeamCollection(tcManager, collectionFolder.FolderPath, repoFolder.FolderPath);
                    tc.CollectionId = Bloom.TeamCollection.TeamCollection.GenerateCollectionId();
                    var bloomCollectionPath =
                        Bloom.TeamCollection.TeamCollection.CollectionPath(collectionFolder.FolderPath);
                    Assert.That(tc.LocalCollectionFilesRecordedSyncTime, Is.EqualTo(DateTime.MinValue));
                    File.WriteAllText(bloomCollectionPath, "This is a fake collection file");
                    var collectionStylesPath = Path.Combine(collectionFolder.FolderPath, "customCollectionStyles.css");
                    RobustFile.WriteAllText(collectionStylesPath, "This is the collection styles");

                    // SUT 1: nothing in repo, no sync time file. Copies to repo.
                    tc.SyncLocalAndRepoCollectionFiles();

                    var localWriteTime1 = tc.LocalCollectionFilesRecordedSyncTime();
                    Assert.That(localWriteTime1, Is.LessThanOrEqualTo(DateTime.Now));
                    Assert.That(localWriteTime1, Is.GreaterThan(DateTime.Now.Subtract(new TimeSpan(0, 0, 5, 0))));
                    var otherFilesPath = FolderTeamCollection.GetRepoProjectFilesZipPath(repoFolder.FolderPath);
                    Assert.That(File.Exists(otherFilesPath));
                    var anotherPlace = Path.Combine(repoFolder.FolderPath, "anotherPlace.zip");
                    RobustFile.Copy(otherFilesPath, anotherPlace);
                    var repoWriteTime1       = new FileInfo(otherFilesPath).LastWriteTime;
                    var collectionWriteTime1 = new FileInfo(bloomCollectionPath).LastWriteTime;

                    // SUT 2: nothing has changed. But it's a startup, so sync still happens to local.
                    tc.SyncLocalAndRepoCollectionFiles();
                    var localWriteTime2 = tc.LocalCollectionFilesRecordedSyncTime();
                    Assert.That(localWriteTime2, Is.GreaterThanOrEqualTo(localWriteTime1));
                    Assert.That(new FileInfo(otherFilesPath).LastWriteTime, Is.EqualTo(repoWriteTime1));
                    Assert.That(new FileInfo(bloomCollectionPath).LastWriteTime,
                                Is.GreaterThanOrEqualTo(collectionWriteTime1));

                    File.WriteAllText(bloomCollectionPath, "This is a modified fake collection file");
                    var collectionWriteTime2 = new FileInfo(bloomCollectionPath).LastWriteTime;

                    // SUT 3: local change copied to repo (only when not at startup)
                    tc.SyncLocalAndRepoCollectionFiles(false);
                    var localWriteTime3 = tc.LocalCollectionFilesRecordedSyncTime();
                    Assert.That(localWriteTime3, Is.GreaterThan(localWriteTime1),
                                "localWriteTime3 should be greater than localWriteTime1");
                    var repoWriteTime2 = new FileInfo(otherFilesPath).LastWriteTime;
                    Assert.That(repoWriteTime2, Is.GreaterThan(repoWriteTime1),
                                "repoWriteTime2 should be greater than repoWriteTime1");
                    // not modified by sync
                    Assert.That(new FileInfo(bloomCollectionPath).LastWriteTime, Is.EqualTo(collectionWriteTime2));

                    File.WriteAllText(bloomCollectionPath, "This is a further modified fake collection file");
                    var collectionWriteTime3 = new FileInfo(bloomCollectionPath).LastWriteTime;
                    var version2Path         = Path.Combine(repoFolder.FolderPath, "version2.zip");
                    RobustFile.Copy(otherFilesPath, version2Path);
                    // modify the remote version by copying the old one back.
                    Thread.Sleep(10);
                    RobustFile.Copy(anotherPlace, otherFilesPath, true);
                    var repoWriteTime3 = new FileInfo(otherFilesPath).LastWriteTime;
                    Assert.That(repoWriteTime3, Is.GreaterThan(collectionWriteTime3),
                                "repo file written after local collection file [sanity check]");

                    // SUT 4: both changed: repo wins
                    tc.SyncLocalAndRepoCollectionFiles();
                    var localWriteTime4 = tc.LocalCollectionFilesRecordedSyncTime();
                    Assert.That(localWriteTime4, Is.GreaterThan(localWriteTime3),
                                "localWriteTime4 should be greater than localWriteTime3");
                    var repoWriteTime4 = new FileInfo(otherFilesPath).LastWriteTime;
                    Assert.That(repoWriteTime4, Is.EqualTo(repoWriteTime3));                     // not modified by sync
                    Assert.That(new FileInfo(bloomCollectionPath).LastWriteTime, Is.GreaterThan(collectionWriteTime3),
                                "bloomCollection LastWriteTime should be greater than collectionWriteTime3");
                    // We got the original back.
                    Assert.That(File.ReadAllText(bloomCollectionPath), Is.EqualTo("This is a fake collection file"));

                    Thread.Sleep(10);
                    var allowedWords = Path.Combine(collectionFolder.FolderPath, "Allowed Words");
                    Directory.CreateDirectory(allowedWords);
                    File.WriteAllText(Path.Combine(allowedWords, "file1.txt"), "fake word list");

                    // SUT5: local allowed words added
                    tc.SyncLocalAndRepoCollectionFiles(false);
                    var localWriteTime5 = tc.LocalCollectionFilesRecordedSyncTime();
                    Assert.That(localWriteTime5, Is.GreaterThan(localWriteTime4),
                                "localWriteTime5 should be greater than localWriteTime4");
                    var repoWriteTime5 = new FileInfo(otherFilesPath).LastWriteTime;
                    Assert.That(repoWriteTime5, Is.GreaterThan(repoWriteTime4),
                                "repoWriteTime5 should be greater than repoWriteTime4");

                    Thread.Sleep(5);
                    var sampleTexts = Path.Combine(collectionFolder.FolderPath, "Sample Texts");
                    Directory.CreateDirectory(sampleTexts);
                    File.WriteAllText(Path.Combine(allowedWords, "sample1.txt"), "fake sample list");

                    // SUT6: local sample texts added
                    tc.SyncLocalAndRepoCollectionFiles(false);
                    var localWriteTime6 = tc.LocalCollectionFilesRecordedSyncTime();
                    Assert.That(localWriteTime6, Is.GreaterThan(localWriteTime5),
                                "localWriteTime6 should be greater than localWriteTime5");
                    var repoWriteTime6 = new FileInfo(otherFilesPath).LastWriteTime;
                    Assert.That(repoWriteTime6, Is.GreaterThan(repoWriteTime5),
                                "repoWriteTime6 should be greater than repoWriteTime5");

                    Thread.Sleep(10);
                    File.WriteAllText(Path.Combine(allowedWords, "sample1.txt"), "fake sample list");

                    // SUT7: local file write time modified, but not actually changed. Want the sync time to
                    // update, but NOT to write the remote file.
                    tc.SyncLocalAndRepoCollectionFiles(false);
                    var localWriteTime7 = tc.LocalCollectionFilesRecordedSyncTime();
                    Assert.That(localWriteTime7, Is.GreaterThan(localWriteTime6),
                                "localWriteTime7 should be greater than localWriteTime6");
                    var repoWriteTime7 = new FileInfo(otherFilesPath).LastWriteTime;
                    Assert.That(repoWriteTime7, Is.EqualTo(repoWriteTime6));

                    tc._haveShownRemoteSettingsChangeWarning = false;
                    File.WriteAllText(bloomCollectionPath, "This is a modified fake collection file, for SUT 8");
                    var collectionWriteTimeBeforeSut8 = new FileInfo(bloomCollectionPath).LastWriteTime;
                    var localWriteTimeBeforeSut8      = tc.LocalCollectionFilesRecordedSyncTime();
                    var repoWriteTimeBeforeSut8       = new FileInfo(otherFilesPath).LastWriteTime;

                    // SUT 8: local change copied to repo on idle
                    tc.SyncLocalAndRepoCollectionFiles(false);
                    Assert.That(tc._haveShownRemoteSettingsChangeWarning, Is.False, "user should not have been warned");
                    var localWriteTimeAfterSut8 = tc.LocalCollectionFilesRecordedSyncTime();
                    Assert.That(localWriteTimeAfterSut8, Is.GreaterThan(localWriteTimeBeforeSut8),
                                "localWriteTime should increase copying on idle");
                    var repoWriteTimeAfterSut8 = new FileInfo(otherFilesPath).LastWriteTime;
                    Assert.That(repoWriteTimeAfterSut8, Is.GreaterThan(repoWriteTimeBeforeSut8),
                                "repoWriteTime should increase copying on idle");
                    // not modified by sync
                    Assert.That(new FileInfo(bloomCollectionPath).LastWriteTime,
                                Is.EqualTo(collectionWriteTimeBeforeSut8));

                    // modify the remote version by copying version2 back.
                    Thread.Sleep(10);
                    var repoWriteTimeBeforeSut9Copy = new FileInfo(otherFilesPath).LastWriteTime;
                    RobustFile.Copy(version2Path, otherFilesPath, true);
                    var collectionWriteTimeBeforeSut9 = new FileInfo(bloomCollectionPath).LastWriteTime;
                    var repoWriteTimeBeforeSut9       = new FileInfo(otherFilesPath).LastWriteTime;
                    Assert.That(repoWriteTimeBeforeSut9, Is.GreaterThan(repoWriteTimeBeforeSut9Copy),
                                "repo file written after local collection file [sanity check]");
                    tc._haveShownRemoteSettingsChangeWarning = false;

                    // SUT9: repo modified, doing check on idle. No changes or warning.
                    tc.SyncLocalAndRepoCollectionFiles(false);
                    Assert.That(tc._haveShownRemoteSettingsChangeWarning, Is.False, "user should not have been warned");
                    var collectionWriteTimeAfterSut9 = new FileInfo(bloomCollectionPath).LastWriteTime;
                    Assert.That(collectionWriteTimeAfterSut9, Is.EqualTo(collectionWriteTimeBeforeSut9),
                                "local settings should not have been modified");

                    File.WriteAllText(bloomCollectionPath, "This is a modified fake collection file, for SUT 10");
                    var collectionWriteTimeBeforeSut10 = new FileInfo(bloomCollectionPath).LastWriteTime;
                    var localWriteTimeBeforeSut10      = tc.LocalCollectionFilesRecordedSyncTime();
                    var repoWriteTimeBeforeSut10       = new FileInfo(otherFilesPath).LastWriteTime;

                    // SUT10: both modified, doing check on idle. No changes. User warned.
                    using (var nfes = new ErrorReport.NonFatalErrorReportExpected())
                    {
                        tc.SyncLocalAndRepoCollectionFiles(false);
                    }

                    Assert.That(tc._haveShownRemoteSettingsChangeWarning, Is.True, "user should have been warned");
                    var localWriteTimeAfterSut10 = tc.LocalCollectionFilesRecordedSyncTime();
                    Assert.That(localWriteTimeAfterSut10, Is.EqualTo(localWriteTimeBeforeSut10),
                                "localWriteTime should not be changed by idle sync where both changed");
                    var repoWriteTimeAfterSut10 = new FileInfo(otherFilesPath).LastWriteTime;
                    Assert.That(repoWriteTimeAfterSut10, Is.EqualTo(repoWriteTimeBeforeSut10),
                                "repo should not be modified by idle sync where both changed");                 // not modified by sync
                    Assert.That(new FileInfo(bloomCollectionPath).LastWriteTime,
                                Is.EqualTo(collectionWriteTimeBeforeSut10),
                                "bloomCollection LastWriteTime should not be changed by idle sync both changed");

                    // Get everything back in sync
                    tc.SyncLocalAndRepoCollectionFiles();
                    var localWriteTimeBeforeSut11 = tc.LocalCollectionFilesRecordedSyncTime();
                    var repoWriteTimeBeforeSut11  = new FileInfo(otherFilesPath).LastWriteTime;
                    RobustFile.WriteAllText(collectionStylesPath, "This is the modified collection styles");

                    // SUT11: custom collection styles modified while Bloom was not running. Copied to repo.
                    tc.SyncLocalAndRepoCollectionFiles();
                    var repoWriteTimeAfterSut11 = new FileInfo(otherFilesPath).LastWriteTime;
                    Assert.That(repoWriteTimeAfterSut11, Is.GreaterThanOrEqualTo(repoWriteTimeBeforeSut11));
                    var localWriteTimeAfterSut11 = tc.LocalCollectionFilesRecordedSyncTime();
                    // We will update the sync time even though the write is the other way.
                    Assert.That(localWriteTimeAfterSut11, Is.GreaterThan(localWriteTimeBeforeSut11));
                    Assert.That(File.ReadAllText(collectionStylesPath),
                                Is.EqualTo("This is the modified collection styles"));
                }
            }
        }