public void programs_is_populated_from_cloud_if_cache_errors_and_cloud_is_populated()
        {
            var document = "# First Program";
            var stateService = new StateServiceMock();
            var scheduler = new TestSchedulerService();

            stateService
                .When(x => x.GetAsync<string>(It.IsAny<string>()))
                .Return(Observable.Throw<string>(new InvalidOperationException()));

            stateService
                .When(x => x.SetAsync<string>(It.IsAny<string>(), It.IsAny<string>()))
                .Return(Observable.Return(Unit.Default));

            var sut = new ExerciseProgramsViewModelBuilder()
                .WithCloudDocument(document)
                .WithSchedulerService(scheduler)
                .WithStateService(stateService)
                .Build();

            scheduler.AdvanceMinimal();
            Assert.NotNull(sut.Programs);
            Assert.Equal(1, sut.Programs.Count);
        }
        public void skip_forwards_command_is_disabled_if_on_last_exercise()
        {
            var scheduler = new TestSchedulerService();
            var action = new ActionMock();

            action
                .When(x => x.Duration)
                .Return(TimeSpan.FromSeconds(10));

            action
                .When(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                .Return<ExecutionContext>(
                    ec =>
                    {
                        ec.IsPaused = true;

                        return ec.WaitWhilePausedAsync();
                    });

            var exercise1 = new ExerciseBuilder()
                .WithName("Exercise 1")
                .WithBeforeExerciseAction(action)
                .Build();

            var exercise2 = new ExerciseBuilder()
                .WithName("Exercise 2")
                .WithBeforeExerciseAction(action)
                .Build();

            var sut = new ExerciseProgramViewModelBuilder()
                .WithModel(new ExerciseProgramBuilder()
                    .AddExercise(exercise1)
                    .AddExercise(exercise2))
                .WithSchedulerService(scheduler)
                .Build();

            var canExecute = sut
                .SkipForwardsCommand
                .CanExecuteObservable
                .CreateCollection();

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();

            sut.SkipForwardsCommand.Execute(null);
            scheduler.AdvanceMinimal();

            Assert.Equal(5, canExecute.Count);
            Assert.False(canExecute[0]);
            Assert.True(canExecute[1]);
            Assert.False(canExecute[2]);
            Assert.True(canExecute[3]);
            Assert.False(canExecute[4]);
        }
        public void current_exercise_reflects_that_in_the_execution_context()
        {
            var scheduler = new TestSchedulerService();
            var action = new ActionMock();

            action
                .When(x => x.Duration)
                .Return(TimeSpan.FromSeconds(10));

            action
                .When(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                .Return<ExecutionContext>(
                    ec =>
                    {
                        ec.IsPaused = true;
                        return ec.WaitWhilePausedAsync();
                    });

            var exercise1 = new ExerciseBuilder()
                .WithName("Exercise 1")
                .WithBeforeExerciseAction(action)
                .Build();

            var exercise2 = new ExerciseBuilder()
                .WithName("Exercise 2")
                .WithBeforeExerciseAction(action)
                .Build();

            var exercise3 = new ExerciseBuilder()
                .WithName("Exercise 3")
                .WithBeforeExerciseAction(action)
                .Build();

            var sut = new ExerciseProgramViewModelBuilder()
                .WithModel(new ExerciseProgramBuilder()
                    .AddExercise(exercise1)
                    .AddExercise(exercise2)
                    .AddExercise(exercise3))
                .WithSchedulerService(scheduler)
                .Build();

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();
            Assert.Equal("Exercise 1", sut.CurrentExercise?.Name);

            sut.SkipForwardsCommand.Execute(null);
            scheduler.AdvanceMinimal();
            Assert.Equal("Exercise 2", sut.CurrentExercise?.Name);

            sut.SkipForwardsCommand.Execute(null);
            scheduler.AdvanceMinimal();
            Assert.Equal("Exercise 3", sut.CurrentExercise?.Name);

            sut.SkipBackwardsCommand.Execute(null);
            scheduler.AdvanceMinimal();
            Assert.Equal("Exercise 2", sut.CurrentExercise?.Name);
        }
        public void skip_backwards_command_is_disabled_if_on_first_exercise()
        {
            var scheduler = new TestSchedulerService();
            var action = new ActionMock(MockBehavior.Loose);

            action
                .When(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                .Return<ExecutionContext>(
                    ec =>
                    {
                        ec.IsPaused = true;
                        return ec.WaitWhilePausedAsync();
                    });

            var sut = new ExerciseProgramViewModelBuilder()
                .WithModel(new ExerciseProgramBuilder()
                    .AddExercise(new ExerciseBuilder()
                        .WithBeforeExerciseAction(action)))
                .WithSchedulerService(scheduler)
                .Build();

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();

            Assert.False(sut.SkipBackwardsCommand.CanExecute(null));
        }
        public void skip_backwards_command_restarts_the_execution_context_from_the_start_of_the_current_exercise_if_sufficient_progress_has_been_made()
        {
            var scheduler = new TestSchedulerService();
            var action = new ActionMock(MockBehavior.Loose);

            action
                .When(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                .Return<ExecutionContext>(
                    ec =>
                    {
                        ec.AddProgress(TimeSpan.FromSeconds(4));
                        ec.IsPaused = true;

                        return ec.WaitWhilePausedAsync();
                    });

            var sut = new ExerciseProgramViewModelBuilder()
                .WithModel(new ExerciseProgramBuilder()
                    .AddExercise(new ExerciseBuilder()
                        .WithBeforeExerciseAction(action)))
                .WithSchedulerService(scheduler)
                .Build();

            var progress = sut
                .WhenAnyValue(x => x.ProgressTimeSpan)
                .CreateCollection();

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();

            sut.SkipBackwardsCommand.Execute(null);
            scheduler.AdvanceMinimal();

            Assert.Equal(3, progress.Count);
            Assert.Equal(TimeSpan.Zero, progress[0]);
            Assert.Equal(TimeSpan.FromSeconds(4), progress[1]);
            Assert.Equal(TimeSpan.Zero, progress[2]);
        }
        public void is_paused_cycles_correctly_if_pause_command_is_executed()
        {
            var scheduler = new TestSchedulerService();
            var sut = new ExerciseProgramViewModelBuilder()
                .WithModel(new ExerciseProgramBuilder()
                    .AddExercise(new ExerciseBuilder()
                        .WithBeforeExerciseAction(
                            new WaitActionBuilder()
                                .WithDelayService(new DelayServiceBuilder().Build())
                                .WithDelay(TimeSpan.FromMinutes(1))
                                .Build())))
                .WithSchedulerService(scheduler)
                .Build();

            Assert.False(sut.IsPaused);

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();
            Assert.False(sut.IsPaused);

            sut.PauseCommand.Execute(null);
            scheduler.AdvanceMinimal();
            Assert.True(sut.IsPaused);
        }
        public void is_resume_visible_cycles_correctly_if_start_command_is_executed_and_execution_is_paused()
        {
            var scheduler = new TestSchedulerService();
            var sut = new ExerciseProgramViewModelBuilder()
                .WithModel(new ExerciseProgramBuilder()
                    .AddExercise(new ExerciseBuilder()
                        .WithBeforeExerciseAction(
                            new WaitActionBuilder()
                                .WithDelayService(new DelayServiceBuilder().Build())
                                .WithDelay(TimeSpan.FromMinutes(1))
                                .Build())))
                .WithSchedulerService(scheduler)
                .Build();

            var isResumeVisible = sut
                .WhenAnyValue(x => x.IsResumeVisible)
                .CreateCollection();

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();

            sut.PauseCommand.Execute(null);
            scheduler.AdvanceMinimal();

            Assert.Equal(2, isResumeVisible.Count);
            Assert.False(isResumeVisible[0]);
            Assert.True(isResumeVisible[1]);
        }
        public void document_is_not_stored_in_cache_if_loaded_from_cache()
        {
            var document = "# First Program";
            var scheduler = new TestSchedulerService();
            var stateService = new StateServiceMock();

            stateService
                .When(x => x.GetAsync<string>(It.IsAny<string>()))
                .Return(Observable.Return(document));

            stateService
                .When(x => x.SetAsync<string>(It.IsAny<string>(), It.IsAny<string>()))
                .Return(Observable.Return(Unit.Default));

            var sut = new ExerciseProgramsViewModelBuilder()
                .WithSchedulerService(scheduler)
                .WithStateService(stateService)
                .Build();

            scheduler.AdvanceMinimal();
            Assert.Equal(ExerciseProgramsViewModelStatus.LoadedFromCache, sut.Status);

            stateService
                .Verify(x => x.SetAsync<string>("ExerciseProgramsDocument", document))
                .WasNotCalled();
        }
        public void parse_error_message_returns_appropriate_message_if_the_document_could_not_be_parsed()
        {
            var document = @"# First Program

## First Exercise

 whatever!";
            var scheduler = new TestSchedulerService();

            var sut = new ExerciseProgramsViewModelBuilder()
                .WithCloudDocument(document)
                .WithSchedulerService(scheduler)
                .Build();

            scheduler.AdvanceMinimal();
            Assert.NotNull(sut.ParseErrorMessage);
            Assert.Equal("Parsing failure: unexpected '#'; expected end of input (Line 3, Column 1); recently consumed: rogram\r\n\r\n", sut.ParseErrorMessage);
        }
        public void status_is_loaded_from_cloud_if_document_is_successfully_loaded_from_cloud()
        {
            var document = "# First Program";
            var scheduler = new TestSchedulerService();

            var sut = new ExerciseProgramsViewModelBuilder()
                .WithCloudDocument(document)
                .WithSchedulerService(scheduler)
                .Build();

            scheduler.AdvanceMinimal();
            Assert.Equal(ExerciseProgramsViewModelStatus.LoadedFromService, sut.Status);
        }
        public void status_is_load_failed_if_the_document_fails_to_load_altogether()
        {
            var exerciseDocumentService = new ExerciseDocumentServiceMock();
            var scheduler = new TestSchedulerService();

            exerciseDocumentService
                .When(x => x.ExerciseDocument)
                .Return(Observable.Throw<string>(new InvalidOperationException()));

            var sut = new ExerciseProgramsViewModelBuilder()
                .WithExerciseDocumentService(exerciseDocumentService)
                .WithSchedulerService(scheduler)
                .Build();

            scheduler.AdvanceMinimal();
            Assert.Equal(ExerciseProgramsViewModelStatus.LoadFailed, sut.Status);
        }
        public void status_is_parse_failed_if_the_document_could_not_be_parsed()
        {
            var document = @"# First Program

## First Exercise

 whatever!";
            var scheduler = new TestSchedulerService();

            var sut = new ExerciseProgramsViewModelBuilder()
                .WithCloudDocument(document)
                .WithSchedulerService(scheduler)
                .Build();

            scheduler.AdvanceMinimal();
            Assert.Equal(ExerciseProgramsViewModelStatus.ParseFailed, sut.Status);
        }
        public void programs_is_populated_from_cache_whilst_document_from_cloud_loads()
        {
            var cacheDocument = "# First Program";
            var cloudDocument = @"
# First Program
# Second Program";
            var scheduler = new TestSchedulerService();

            var exerciseDocumentService = new ExerciseDocumentServiceMock(MockBehavior.Loose);

            exerciseDocumentService
                .When(x => x.ExerciseDocument)
                .Return(
                    Observable
                        .Return(cloudDocument)
                        .Delay(TimeSpan.FromSeconds(3), scheduler.TaskPoolScheduler));

            var sut = new ExerciseProgramsViewModelBuilder()
                .WithExerciseDocumentService(exerciseDocumentService)
                .WithCachedDocument(cacheDocument)
                .WithSchedulerService(scheduler)
                .Build();

            scheduler.AdvanceMinimal();

            Assert.NotNull(sut.Programs);
            Assert.Equal(1, sut.Programs.Count);

            scheduler.AdvanceBy(TimeSpan.FromSeconds(2));
            Assert.NotNull(sut.Programs);
            Assert.Equal(1, sut.Programs.Count);

            scheduler.AdvanceBy(TimeSpan.FromSeconds(2));
            Assert.NotNull(sut.Programs);
            Assert.Equal(2, sut.Programs.Count);
        }
        public void programs_is_populated_from_cloud_if_cache_is_populated_and_cloud_is_populated()
        {
            var cacheDocument = "# First Program";
            var cloudDocument = @"
# First Program
# Second Program";
            var scheduler = new TestSchedulerService();

            var sut = new ExerciseProgramsViewModelBuilder()
                .WithCloudDocument(cloudDocument)
                .WithCachedDocument(cacheDocument)
                .WithSchedulerService(scheduler)
                .Build();

            scheduler.AdvanceMinimal();
            Assert.NotNull(sut.Programs);
            Assert.Equal(2, sut.Programs.Count);
        }
        public void is_start_visible_is_true_by_default()
        {
            var scheduler = new TestSchedulerService();
            var sut = new ExerciseProgramViewModelBuilder()
                .WithSchedulerService(scheduler)
                .Build();
            scheduler.AdvanceMinimal();

            Assert.True(sut.IsStartVisible);
        }
        public void progress_time_span_reflects_any_progression_through_the_exercise()
        {
            var scheduler = new TestSchedulerService();
            var model = new ExerciseBuilder()
                .Build();
            var executionContext = new ExecutionContext();
            var sut = new ExerciseViewModelBuilder()
                .WithSchedulerService(scheduler)
                .WithExecutionContext(executionContext)
                .WithModel(model)
                .Build();

            executionContext.SetCurrentExercise(model);

            scheduler.AdvanceMinimal();
            Assert.Equal(TimeSpan.Zero, sut.ProgressTimeSpan);

            executionContext.AddProgress(TimeSpan.FromSeconds(3));
            scheduler.AdvanceMinimal();
            Assert.Equal(TimeSpan.FromSeconds(3), sut.ProgressTimeSpan);

            executionContext.AddProgress(TimeSpan.FromSeconds(2));
            scheduler.AdvanceMinimal();
            Assert.Equal(TimeSpan.FromSeconds(5), sut.ProgressTimeSpan);
        }
        public void is_start_visible_cycles_correctly_if_start_command_is_executed()
        {
            var scheduler = new TestSchedulerService();
            var sut = new ExerciseProgramViewModelBuilder()
                .WithSchedulerService(scheduler)
                .Build();
            scheduler.AdvanceMinimal();

            var isStartVisible = sut
                .WhenAnyValue(x => x.IsStartVisible)
                .CreateCollection();

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();

            // TODO: the extra 2 values here appears to be due to ReactiveCommand's CanExecuteObservable implementation
            //       try this again once ReactiveCommand is re-written
            Assert.Equal(5, isStartVisible.Count);
            Assert.True(isStartVisible[0]);
            Assert.False(isStartVisible[1]);
            Assert.True(isStartVisible[2]);
            Assert.False(isStartVisible[3]);
            Assert.True(isStartVisible[4]);
        }
        public void progress_time_span_is_not_reset_to_zero_if_another_exercise_is_started()
        {
            var scheduler = new TestSchedulerService();
            var model1 = new ExerciseBuilder()
                .WithName("model 1")
                .Build();
            var model2 = new ExerciseBuilder()
                .WithName("model 2")
                .Build();
            var executionContext = new ExecutionContext();
            var sut = new ExerciseViewModelBuilder()
                .WithSchedulerService(scheduler)
                .WithExecutionContext(executionContext)
                .WithModel(model1)
                .Build();

            executionContext.SetCurrentExercise(model1);
            executionContext.AddProgress(TimeSpan.FromSeconds(3));

            scheduler.AdvanceMinimal();
            Assert.Equal(TimeSpan.FromSeconds(3), sut.ProgressTimeSpan);

            executionContext.SetCurrentExercise(model2);
            scheduler.AdvanceMinimal();
            Assert.Equal(TimeSpan.FromSeconds(3), sut.ProgressTimeSpan);

            executionContext.AddProgress(TimeSpan.FromSeconds(3));
            scheduler.AdvanceMinimal();
            Assert.Equal(TimeSpan.FromSeconds(3), sut.ProgressTimeSpan);
        }
        public void is_pause_visible_cycles_correctly_if_start_command_is_executed()
        {
            var scheduler = new TestSchedulerService();
            var action = new ActionMock(MockBehavior.Loose);
            action
                .When(x => x.Duration)
                .Return(TimeSpan.FromMinutes(1));
            action
                .When(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                .Return<ExecutionContext>(
                    ec =>
                        Observable
                            .Return(Unit.Default)
                            .Do(_ => { })
                            .Delay(TimeSpan.FromMinutes(1), scheduler.DefaultScheduler)
                            .Do(_ => ec.AddProgress(TimeSpan.FromMinutes(1))));
            var sut = new ExerciseProgramViewModelBuilder()
                .WithModel(
                    new ExerciseProgramBuilder()
                        .AddExercise(
                            new ExerciseBuilder()
                                .WithBeforeExerciseAction(action)))
                .WithSchedulerService(scheduler)
                .Build();

            Assert.False(sut.IsPauseVisible);

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();
            Assert.True(sut.IsPauseVisible);

            scheduler.AdvanceBy(TimeSpan.FromMinutes(10));
            Assert.False(sut.IsPauseVisible);
        }
        public void progress_time_span_is_reset_to_zero_if_the_execution_context_changes_to_null()
        {
            var scheduler = new TestSchedulerService();
            var model = new ExerciseBuilder()
                .Build();
            var executionContext = new ExecutionContext();
            var executionContextSubject = new Subject<ExecutionContext>();
            var sut = new ExerciseViewModelBuilder()
                .WithSchedulerService(scheduler)
                .WithExecutionContext(executionContextSubject)
                .WithModel(model)
                .Build();

            executionContextSubject.OnNext(executionContext);
            executionContext.SetCurrentExercise(model);

            executionContext.AddProgress(TimeSpan.FromSeconds(3));
            scheduler.AdvanceMinimal();
            Assert.Equal(TimeSpan.FromSeconds(3), sut.ProgressTimeSpan);

            executionContextSubject.OnNext(null);
            scheduler.AdvanceMinimal();
            Assert.Equal(TimeSpan.Zero, sut.ProgressTimeSpan);
        }
        public void progress_is_updated_throughout_execution()
        {
            var scheduler = new TestSchedulerService();
            var action = new ActionMock(MockBehavior.Loose);

            action
                .When(x => x.Duration)
                .Return(TimeSpan.FromMinutes(1));

            action
                .When(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                .Return<ExecutionContext>(
                    ec =>
                    {
                        ec.AddProgress(TimeSpan.FromSeconds(15));
                        ec.AddProgress(TimeSpan.FromSeconds(30));

                        ec.IsPaused = true;

                        return ec.WaitWhilePausedAsync();
                    });

            var sut = new ExerciseProgramViewModelBuilder()
                .WithModel(new ExerciseProgramBuilder()
                    .AddExercise(new ExerciseBuilder()
                        .WithBeforeExerciseAction(action)))
                .WithSchedulerService(scheduler)
                .Build();

            var progress = sut
                .WhenAnyValue(x => x.Progress)
                .CreateCollection();

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();

            Assert.Equal(3, progress.Count);
            Assert.Equal(0, progress[0]);
            Assert.Equal(0.25, progress[1]);
            Assert.Equal(0.75, progress[2]);
        }
        public void progress_is_calculated_based_on_duration_and_progress_time_span(int durationInMs, int progressInMs, double expectedProgress)
        {
            var scheduler = new TestSchedulerService();
            var action = new ActionMock(MockBehavior.Loose);

            action
                .When(x => x.Duration)
                .Return(TimeSpan.FromMilliseconds(durationInMs));

            var model = new ExerciseBuilder()
                .WithBeforeExerciseAction(action)
                .Build();

            var executionContext = new ExecutionContext();
            var sut = new ExerciseViewModelBuilder()
                .WithSchedulerService(scheduler)
                .WithExecutionContext(executionContext)
                .WithModel(model)
                .Build();

            executionContext.SetCurrentExercise(model);
            executionContext.AddProgress(TimeSpan.FromMilliseconds(progressInMs));

            scheduler.AdvanceMinimal();

            Assert.Equal(expectedProgress, sut.Progress);
        }
        public void skip_backwards_command_is_enabled_if_sufficient_progress_has_been_made_through_first_exercise()
        {
            var scheduler = new TestSchedulerService();
            var action = new ActionMock(MockBehavior.Loose);

            action
                .When(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                .Return<ExecutionContext>(
                    ec =>
                    {
                        ec.AddProgress(TimeSpan.FromSeconds(1));
                        ec.IsPaused = true;
                        return ec.WaitWhilePausedAsync();
                    });

            var sut = new ExerciseProgramViewModelBuilder()
                .WithModel(new ExerciseProgramBuilder()
                    .AddExercise(new ExerciseBuilder()
                        .WithBeforeExerciseAction(action)))
                .WithSchedulerService(scheduler)
                .Build();

            // TODO: technically, I should just check CanExecute(null) at the end, but without this subscription the RxCommand does not update CanExecute correctly
            //       try changing this once I'm using new RxCommand
            var canExecute = sut
                .SkipBackwardsCommand
                .CanExecuteObservable
                .CreateCollection();

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();

            Assert.Equal(2, canExecute.Count);
            Assert.False(canExecute[0]);
            Assert.True(canExecute[1]);
        }
        public void is_active_is_false_if_there_is_no_execution_context()
        {
            var scheduler = new TestSchedulerService();
            var sut = new ExerciseViewModelBuilder()
                .WithSchedulerService(scheduler)
                .Build();

            scheduler.AdvanceMinimal();
            Assert.False(sut.IsActive);
        }
        public void skip_backwards_command_restarts_the_execution_context_from_the_start_of_the_previous_exercise_if_the_current_exercise_if_only_recently_started()
        {
            var scheduler = new TestSchedulerService();
            var action = new ActionMock();

            action
                .When(x => x.Duration)
                .Return(TimeSpan.FromSeconds(10));

            action
                .When(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                .Return<ExecutionContext>(
                    ec =>
                    {
                        ec.AddProgress(TimeSpan.FromSeconds(0.5));
                        ec.IsPaused = true;

                        return ec.WaitWhilePausedAsync();
                    });

            var exercise1 = new ExerciseBuilder()
                .WithName("Exercise 1")
                .WithBeforeExerciseAction(action)
                .Build();

            var exercise2 = new ExerciseBuilder()
                .WithName("Exercise 2")
                .WithBeforeExerciseAction(action)
                .Build();

            var sut = new ExerciseProgramViewModelBuilder()
                .WithModel(new ExerciseProgramBuilder()
                    .AddExercise(exercise1)
                    .AddExercise(exercise2))
                .WithSchedulerService(scheduler)
                .Build();

            var progress = sut
                .WhenAnyValue(x => x.ProgressTimeSpan)
                .CreateCollection();

            // start from the second exercise
            sut.StartCommand.Execute(exercise1.Duration);
            scheduler.AdvanceMinimal();

            sut.SkipBackwardsCommand.Execute(null);
            scheduler.AdvanceMinimal();

            Assert.Equal(5, progress.Count);
            Assert.Equal(TimeSpan.Zero, progress[0]);
            Assert.Equal(TimeSpan.FromSeconds(10), progress[1]);
            Assert.Equal(TimeSpan.FromSeconds(10.5), progress[2]);
            Assert.Equal(TimeSpan.Zero, progress[3]);
            Assert.Equal(TimeSpan.FromSeconds(10), progress[4]);
        }
        public void is_active_is_false_if_this_exercise_is_not_the_current_exercise()
        {
            var scheduler = new TestSchedulerService();
            var model1 = new ExerciseBuilder()
                .Build();
            var model2 = new ExerciseBuilder()
                .Build();
            var executionContext = new ExecutionContext();
            var sut = new ExerciseViewModelBuilder()
                .WithSchedulerService(scheduler)
                .WithExecutionContext(executionContext)
                .WithModel(model1)
                .Build();

            scheduler.AdvanceMinimal();
            Assert.False(sut.IsActive);

            executionContext.SetCurrentExercise(model2);
            scheduler.AdvanceMinimal();
            Assert.False(sut.IsActive);
        }
        public void skip_forwards_command_skips_to_the_next_exercise()
        {
            var scheduler = new TestSchedulerService();
            var action = new ActionMock();

            action
                .When(x => x.Duration)
                .Return(TimeSpan.FromSeconds(10));

            action
                .When(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                .Return<ExecutionContext>(
                    ec =>
                    {
                        ec.IsPaused = true;

                        return ec.WaitWhilePausedAsync();
                    });

            var exercise1 = new ExerciseBuilder()
                .WithName("Exercise 1")
                .WithBeforeExerciseAction(action)
                .Build();

            var exercise2 = new ExerciseBuilder()
                .WithName("Exercise 2")
                .WithBeforeExerciseAction(action)
                .Build();

            var sut = new ExerciseProgramViewModelBuilder()
                .WithModel(new ExerciseProgramBuilder()
                    .AddExercise(exercise1)
                    .AddExercise(exercise2))
                .WithSchedulerService(scheduler)
                .Build();

            var progress = sut
                .WhenAnyValue(x => x.ProgressTimeSpan)
                .CreateCollection();

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();

            sut.SkipForwardsCommand.Execute(null);
            scheduler.AdvanceMinimal();

            Assert.Equal(2, progress.Count);
            Assert.Equal(TimeSpan.Zero, progress[0]);
            Assert.Equal(TimeSpan.FromSeconds(10), progress[1]);
        }
        public void is_started_cycles_correctly_if_start_command_is_executed()
        {
            var scheduler = new TestSchedulerService();
            var sut = new ExerciseProgramViewModelBuilder()
                .WithSchedulerService(scheduler)
                .Build();
            scheduler.AdvanceMinimal();

            var isStarted = sut
                .WhenAnyValue(x => x.IsStarted)
                .CreateCollection();

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();

            Assert.Equal(3, isStarted.Count);
            Assert.False(isStarted[0]);
            Assert.True(isStarted[1]);
            Assert.False(isStarted[2]);
        }
        public void execution_context_is_cancelled_if_user_navigates_away()
        {
            var scheduler = new TestSchedulerService();
            var sut = new ExerciseProgramViewModelBuilder()
                .WithModel(new ExerciseProgramBuilder()
                    .AddExercise(new ExerciseBuilder()
                        .WithBeforeExerciseAction(
                            new WaitActionBuilder()
                                .WithDelayService(new DelayServiceBuilder().Build())
                                .WithDelay(TimeSpan.FromMinutes(1))
                                .Build())))
                .WithSchedulerService(scheduler)
                .Build();
            sut
                .HostScreen
                .Router
                .NavigationStack
                .Add(sut);

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();

            Assert.True(sut.IsStarted);

            sut
                .HostScreen
                .Router
                .NavigateBack
                .Execute(null);
            scheduler.AdvanceMinimal();

            Assert.False(sut.IsStarted);
        }
        public void programs_is_null_if_both_cache_and_cloud_are_empty()
        {
            var scheduler = new TestSchedulerService();

            var sut = new ExerciseProgramsViewModelBuilder()
                .WithSchedulerService(scheduler)
                .Build();

            scheduler.AdvanceMinimal();
            Assert.Null(sut.Programs);
        }