public async Task DailyGoalStreak_CalculatesCorrectly()
        {
            #region Day 1
            FakeClock.SetToIso("2020-03-01T11:00:00+08:00");

            var preferenceDataBase = new UserPreferenceData()
            {
                // +08:00
                DailyGoalResetTimeTimeZone = "Asia/Taipei",
                DailyGoalResetTimePartial  = "12:00:00",
                DailyGoalNewlyCreatedWordFamiliarityCount = 1,
            };

            await UserPreferencesController.Upsert(new()
            {
                Data = preferenceDataBase with
                {
                    DailyGoalNewlyCreatedWordFamiliarityCount = 1,
                },
            });

            (await DailyGoalStreak())
            .CurrentStreakDays
            .ShouldBe(0);

            await SetupWordFamiliarity(1, "sv", new() { "a" });

            (await DailyGoalStreak())
            .CurrentStreakDays
            .ShouldBe(1);
            #endregion


            #region Day 2
            FakeClock.AdvanceDays(1);
            await SetupWordFamiliarity(1, "sv", new() { "b" });

            (await DailyGoalStreak())
            .CurrentStreakDays
            .ShouldBe(2);

            await UserPreferencesController.Upsert(new()
            {
                Data = preferenceDataBase with
                {
                    DailyGoalNewlyCreatedWordFamiliarityCount = 2,
                },
            });

            (await DailyGoalStreak())
            .CurrentStreakDays
            .ShouldBe(1);

            await SetupWordFamiliarity(1, "sv", new() { "c" });

            (await DailyGoalStreak())
            .CurrentStreakDays
            .ShouldBe(2);

            await UserPreferencesController.Upsert(new()
            {
                Data = preferenceDataBase with
                {
                    DailyGoalNewlyCreatedWordFamiliarityCount = 1,
                },
            });

            (await DailyGoalStreak())
            .CurrentStreakDays
            .ShouldBe(2);


            using (User(2))
            {
                await UserPreferencesController.Upsert(new()
                {
                    Data = preferenceDataBase with
                    {
                        DailyGoalNewlyCreatedWordFamiliarityCount = 1,
                    },
                });

                (await DailyGoalStreak())
                .CurrentStreakDays
                .ShouldBe(0);

                await SetupWordFamiliarity(1, "sv", new() { "a" });

                (await DailyGoalStreak())
                .CurrentStreakDays
                .ShouldBe(1);
            }
            #endregion

            #region Day 3 - before daily goal reset
            FakeClock.AdvanceDays(1);

            (await DailyGoalStreak())
            .CurrentStreakDays
            .ShouldBe(2);
            #endregion

            #region Day 3 - after daily goal reset
            FakeClock.AdvanceHours(2);

            (await DailyGoalStreak())
            .CurrentStreakDays
            .ShouldBe(0);

            await SetupWordFamiliarity(1, "sv", new() { "d" });

            (await DailyGoalStreak())
            .CurrentStreakDays
            .ShouldBe(1);
            #endregion

            #region Day 4 - before daily goal reset
            FakeClock.AdvanceHours(-2);
            FakeClock.AdvanceDays(1);
            await SetupWordFamiliarity(1, "sv", new() { "e" });

            (await DailyGoalStreak())
            .CurrentStreakDays
            .ShouldBe(1);
            #endregion

            #region Day 5
            FakeClock.AdvanceDays(1);
            #endregion

            #region Day 6
            FakeClock.AdvanceDays(1);
            await SetupWordFamiliarity(1, "sv", new() { "f" });

            (await DailyGoalStreak())
            .CurrentStreakDays
            .ShouldBe(1);
            #endregion
        }
Beispiel #2
0
        public async Task UpsertBatch_DailyGoal_CountsCorrectly()
        {
            #region day 1
            FakeClock.SetToIso("2020-03-01T11:00:00+08:00");

            await UserPreferencesController.Upsert(new()
            {
                Data = new()
                {
                    // +08:00
                    DailyGoalResetTimeTimeZone = "Asia/Taipei",
                    DailyGoalResetTimePartial  = "12:00:00",
                    DailyGoalNewlyCreatedWordFamiliarityCount = 4,
                },
            });

            (await SetupWordFamiliarity(1, "sv", new() { "a" }))
            .DailyGoalStatus
            .ShouldSatisfyAllConditions(
                x => x.NewlyCreated.ShouldBe(1),
                x => x.NewlyCreatedGoal.ShouldBe(4),
                x => x.NewlyCreatedReached.ShouldBeFalse()
                );

            // Should yield the same result as what we got from Upsert's return.
            (await DailyGoalCheck())
            .ShouldSatisfyAllConditions(
                x => x.NewlyCreated.ShouldBe(1),
                x => x.NewlyCreatedGoal.ShouldBe(4),
                x => x.NewlyCreatedReached.ShouldBeFalse()
                );

            // Already learned word shouldn't count.
            (await SetupWordFamiliarity(3, "sv", new() { "a" }))
            .DailyGoalStatus
            .ShouldSatisfyAllConditions(
                x => x.NewlyCreated.ShouldBe(1),
                x => x.NewlyCreatedGoal.ShouldBe(4),
                x => x.NewlyCreatedReached.ShouldBeFalse()
                );

            (await SetupWordFamiliarity(2, "sv", new() { "b", "c" }))
            .DailyGoalStatus
            .ShouldSatisfyAllConditions(
                x => x.NewlyCreated.ShouldBe(3),
                x => x.NewlyCreatedGoal.ShouldBe(4),
                x => x.NewlyCreatedReached.ShouldBeFalse()
                );

            (await SetupWordFamiliarity(3, "sv", new() { "d" }))
            .DailyGoalStatus
            .ShouldSatisfyAllConditions(
                x => x.NewlyCreated.ShouldBe(4),
                x => x.NewlyCreatedGoal.ShouldBe(4),
                x => x.NewlyCreatedReached.ShouldBeTrue()
                );

            // Word "marked as new" (removed) shouldn't count.
            (await SetupWordFamiliarity(0, "sv", new() { "d" }))
            .DailyGoalStatus
            .ShouldSatisfyAllConditions(
                x => x.NewlyCreated.ShouldBe(3),
                x => x.NewlyCreatedGoal.ShouldBe(4),
                x => x.NewlyCreatedReached.ShouldBeFalse()
                );

            (await SetupWordFamiliarity(1, "sv", new() { "d", "e" }))
            .DailyGoalStatus
            .ShouldSatisfyAllConditions(
                x => x.NewlyCreated.ShouldBe(5),
                x => x.NewlyCreatedGoal.ShouldBe(4),
                x => x.NewlyCreatedReached.ShouldBeTrue()
                );
            #endregion


            #region day 2
            FakeClock.AdvanceDays(1);

            (await SetupWordFamiliarity(2, "sv", new() { "f" }))
            .DailyGoalStatus
            // Should reset state each day.
            .ShouldSatisfyAllConditions(
                x => x.NewlyCreated.ShouldBe(1),
                x => x.NewlyCreatedGoal.ShouldBe(4),
                x => x.NewlyCreatedReached.ShouldBeFalse()
                );
            #endregion
        }