11package com.example.util.simpletimetracker.domain.complexRule.interactor
22
3+ import com.example.util.simpletimetracker.domain.base.ResultContainer
34import com.example.util.simpletimetracker.domain.complexRule.model.ComplexRule
45import com.example.util.simpletimetracker.domain.daysOfWeek.interactor.GetCurrentDayInteractor
56import com.example.util.simpletimetracker.domain.daysOfWeek.model.DayOfWeek
@@ -33,6 +34,7 @@ class ComplexRuleProcessActionInteractorTest {
3334
3435 @Test
3536 fun mergesSelectOnStart (): Unit = runBlocking {
37+ // Given
3638 val rule1 = assignTagRule(
3739 actionAssignTagValues = listOf (tag(10L , numericValue = null )),
3840 actionAssignTagValueOnStartIds = setOf (10L ),
@@ -41,54 +43,169 @@ class ComplexRuleProcessActionInteractorTest {
4143 actionAssignTagValues = listOf (tag(20L , numericValue = null )),
4244 actionAssignTagValueOnStartIds = setOf (20L ),
4345 )
44-
4546 `when `(complexRuleInteractor.getAll()).thenReturn(listOf (rule1, rule2))
4647
48+ // When
4749 val result = subject.processRules(
4850 timeStarted = 0L ,
4951 startingTypeId = startingTypeId,
5052 currentTypeIds = currentTypeIds,
5153 )
5254
55+ // Then
5356 assertEquals(setOf (10L , 20L ), result.tagIdsToSelectValueOnStart)
5457 }
5558
5659 @Test
5760 fun dropsSelectOnStartWhenNumericExists (): Unit = runBlocking {
61+ // Given
5862 val rule = assignTagRule(
5963 actionAssignTagValues = listOf (tag(30L , numericValue = 3.5 )),
6064 actionAssignTagValueOnStartIds = setOf (30L ),
6165 )
62-
6366 `when `(complexRuleInteractor.getAll()).thenReturn(listOf (rule))
6467
68+ // When
6569 val result = subject.processRules(
6670 timeStarted = 0L ,
6771 startingTypeId = startingTypeId,
6872 currentTypeIds = currentTypeIds,
6973 )
7074
75+ // Then
7176 assertEquals(emptySet<Long >(), result.tagIdsToSelectValueOnStart)
7277 }
7378
7479 @Test
7580 fun ignoresSelectOnStartWithoutMergedTag (): Unit = runBlocking {
81+ // Given
7682 val rule = assignTagRule(
7783 actionAssignTagValues = listOf (tag(40L , numericValue = null )),
7884 actionAssignTagValueOnStartIds = setOf (40L , 99L ),
7985 )
80-
8186 `when `(complexRuleInteractor.getAll()).thenReturn(listOf (rule))
8287
88+ // When
8389 val result = subject.processRules(
8490 timeStarted = 0L ,
8591 startingTypeId = startingTypeId,
8692 currentTypeIds = currentTypeIds,
8793 )
8894
95+ // Then
8996 assertEquals(setOf (40L ), result.tagIdsToSelectValueOnStart)
9097 }
9198
99+ @Test
100+ fun mergesAssignedTags (): Unit = runBlocking {
101+ // Given
102+ val rule1 = assignTagRule(
103+ actionAssignTagValues = listOf (tag(50L , numericValue = null )),
104+ actionAssignTagValueOnStartIds = emptySet(),
105+ )
106+ val rule2 = assignTagRule(
107+ actionAssignTagValues = listOf (tag(60L , numericValue = null )),
108+ actionAssignTagValueOnStartIds = emptySet(),
109+ )
110+ `when `(complexRuleInteractor.getAll()).thenReturn(listOf (rule1, rule2))
111+
112+ // When
113+ val result = subject.processRules(
114+ timeStarted = 0L ,
115+ startingTypeId = startingTypeId,
116+ currentTypeIds = currentTypeIds,
117+ )
118+
119+ // Then
120+ assertEquals(
121+ listOf (tag(50L , numericValue = null ), tag(60L , numericValue = null )),
122+ result.tags,
123+ )
124+ }
125+
126+ @Test
127+ fun numericValuesOverrideEarlierNull (): Unit = runBlocking {
128+ // Given
129+ val ruleWithNull = assignTagRule(
130+ actionAssignTagValues = listOf (tag(70L , numericValue = null )),
131+ actionAssignTagValueOnStartIds = emptySet(),
132+ )
133+ val ruleWithNumeric = assignTagRule(
134+ actionAssignTagValues = listOf (tag(70L , numericValue = 9.0 )),
135+ actionAssignTagValueOnStartIds = emptySet(),
136+ )
137+ val ruleWithLateNull = assignTagRule(
138+ actionAssignTagValues = listOf (tag(70L , numericValue = null )),
139+ actionAssignTagValueOnStartIds = emptySet(),
140+ )
141+ `when `(complexRuleInteractor.getAll()).thenReturn(listOf (ruleWithNull, ruleWithNumeric, ruleWithLateNull))
142+
143+ // When
144+ val result = subject.processRules(
145+ timeStarted = 0L ,
146+ startingTypeId = startingTypeId,
147+ currentTypeIds = currentTypeIds,
148+ )
149+
150+ // Then
151+ assertEquals(listOf (tag(70L , numericValue = 9.0 )), result.tags)
152+ }
153+
154+ @Test
155+ fun allowsMultitaskingWhenAllowRuleExistsEvenWithDisallow (): Unit = runBlocking {
156+ // Given
157+ val disallowRule = multitaskingRule(ComplexRule .Action .DisallowMultitasking )
158+ val allowRule = multitaskingRule(ComplexRule .Action .AllowMultitasking )
159+ `when `(complexRuleInteractor.getAll()).thenReturn(listOf (disallowRule, allowRule))
160+
161+ // When
162+ val result = subject.processRules(
163+ timeStarted = 0L ,
164+ startingTypeId = startingTypeId,
165+ currentTypeIds = currentTypeIds,
166+ )
167+
168+ // Then
169+ assertEquals(ResultContainer .Defined (true ), result.isMultitaskingAllowed)
170+ }
171+
172+ @Test
173+ fun disallowsMultitaskingWhenOnlyDisallowRulesRun (): Unit = runBlocking {
174+ // Given
175+ val rule = multitaskingRule(ComplexRule .Action .DisallowMultitasking )
176+ `when `(complexRuleInteractor.getAll()).thenReturn(listOf (rule))
177+
178+ // When
179+ val result = subject.processRules(
180+ timeStarted = 0L ,
181+ startingTypeId = startingTypeId,
182+ currentTypeIds = currentTypeIds,
183+ )
184+
185+ // Then
186+ assertEquals(ResultContainer .Defined (false ), result.isMultitaskingAllowed)
187+ }
188+
189+ @Test
190+ fun leavesMultitaskingUndefinedWhenNoAllowOrDisallowRules (): Unit = runBlocking {
191+ // Given
192+ val rule = assignTagRule(
193+ actionAssignTagValues = listOf (tag(80L , numericValue = null )),
194+ actionAssignTagValueOnStartIds = emptySet(),
195+ )
196+ `when `(complexRuleInteractor.getAll()).thenReturn(listOf (rule))
197+
198+ // When
199+ val result = subject.processRules(
200+ timeStarted = 0L ,
201+ startingTypeId = startingTypeId,
202+ currentTypeIds = currentTypeIds,
203+ )
204+
205+ // Then
206+ assertEquals(ResultContainer .Undefined , result.isMultitaskingAllowed)
207+ }
208+
92209 private fun assignTagRule (
93210 actionAssignTagValues : List <RecordBase .Tag >,
94211 actionAssignTagValueOnStartIds : Set <Long >,
@@ -112,4 +229,18 @@ class ComplexRuleProcessActionInteractorTest {
112229 ): RecordBase .Tag {
113230 return RecordBase .Tag (tagId = tagId, numericValue = numericValue)
114231 }
232+
233+ private fun multitaskingRule (action : ComplexRule .Action ): ComplexRule {
234+ return ComplexRule (
235+ id = 0L ,
236+ disabled = false ,
237+ action = action,
238+ actionDisallowOnlyPrevious = false ,
239+ actionAssignTagValues = emptyList(),
240+ actionAssignTagValueOnStartIds = emptySet(),
241+ conditionStartingTypeIds = setOf (startingTypeId),
242+ conditionCurrentTypeIds = currentTypeIds,
243+ conditionDaysOfWeek = setOf (currentDay),
244+ )
245+ }
115246}
0 commit comments