๐Ÿ“ฆ sleepyfran / duets

๐Ÿ“„ Simulation.Tests.fs ยท 314 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314module Duets.Simulation.Tests.Simulation

open System
open Aether
open Duets.Data.World
open FsUnit
open NUnit.Framework
open Duets
open Duets.Simulation.Time
open Test.Common
open Test.Common.Generators

open Duets.Entities
open Duets.Entities.Calendar.Shorthands
open Duets.Simulation
open Duets.Data.Careers

let state =
    { dummyState with
        Today = dummyTodayMiddleOfYear }

let stateInMorning =
    { state with
        Today = state.Today |> Calendar.Transform.changeDayMoment Morning }

let stateInMidnightOfNewYear =
    { state with
        Today =
            Spring 1<days> 2023<years>
            |> Calendar.Transform.changeDayMoment Midnight }

let unfinishedSong = Unfinished(dummySong, 10<quality>, 10<quality>)

let songStartedEffect = SongStarted(dummyBand, unfinishedSong)

let checkTimeIncrease timeIncrease effects =
    effects
    |> should
        contain
        (AdvanceTime.advanceDayMoment dummyTodayMiddleOfYear timeIncrease
         |> List.head)

[<Test>]
let ``tick does not try to apply moodlets (breaks) for game created effect``
    ()
    =
    let gameStartedEffect = GameCreated state

    (fun () -> Simulation.tickOne State.empty gameStartedEffect |> ignore)
    |> should not' (throw typeof<Exception>)

[<Test>]
let ``tick should apply the given effect`` () =
    Simulation.tickOne state songStartedEffect
    |> fst
    |> should contain songStartedEffect

[<Test>]
let ``tick should apply multiple given effects`` () =
    Simulation.tickMultiple state [ songStartedEffect; songStartedEffect ]
    |> fst
    |> List.filter (function
        | SongStarted _ -> true
        | _ -> false)
    |> List.length
    |> should equal 2

[<Test>]
let ``tick should gather and apply associated effects`` () =
    let effects =
        AdvanceTime.advanceDayMoment' state 1<dayMoments>
        |> Simulation.tickMultiple state
        |> fst

    effects
    |> List.item 0
    |> should
        equal
        (TimeAdvanced(
            { Year = 2021<years>
              Season = Season.Summer
              Day = 20<days>
              DayMoment = Morning }
        ))

    effects
    |> List.item 1
    |> should
        equal
        (CharacterAttributeChanged(
            dummyCharacter.Id,
            CharacterAttribute.Hunger,
            Diff(100, 85)
        ))

[<Test>]
let ``tick should stop the chain of effects when a BreakChain associated effect is raised``
    ()
    =
    let stateToHospitalize =
        [ CharacterAttributeChanged(
              dummyCharacter.Id,
              CharacterAttribute.Health,
              Diff(100, 1)
          )
          CharacterAttributeChanged(
              dummyCharacter.Id,
              CharacterAttribute.Hunger,
              Diff(100, 1)
          ) ]
        |> State.Root.applyEffects state

    let skippedEffect =
        CharacterAttributeChanged(
            dummyCharacter.Id,
            CharacterAttribute.Drunkenness,
            Diff(0, 10)
        )

    let result =
        [ yield! AdvanceTime.advanceDayMoment' state 1<dayMoments>
          (* Break chain should happen after advancing the moment *)
          skippedEffect ]
        |> Simulation.tickMultiple stateToHospitalize
        |> fst

    result |> should not' (contain skippedEffect)

[<Test>]
let ``tick should not apply the given effect more than once`` () =
    Simulation.tickOne state songStartedEffect
    |> fst
    |> List.filter (fun effect -> effect = songStartedEffect)
    |> should haveLength 1

let filterDailyUpdateEffects effect =
    match effect with
    | AlbumReleasedUpdate _ -> true
    | MoneyEarned _ -> true
    | ConcertUpdated _ -> true
    | _ -> false

[<Test>]
let ``tick should update album streams every day`` () =
    let state =
        dummyState
        |> addReleasedAlbum
            dummyBand.Id
            (Album.Released.fromUnreleased dummyUnreleasedAlbum dummyToday 1.0)

    AdvanceTime.advanceDayMoment' state 1<dayMoments>
    |> Simulation.tickMultiple state
    |> fst
    |> List.filter filterDailyUpdateEffects
    |> should haveLength 2

[<Test>]
let ``tick should update all scheduled concerts every day`` () =
    let state =
        State.generateOne
            { State.defaultOptions with
                FutureConcertsToGenerate = 10 }

    AdvanceTime.advanceDayMoment' state 1<dayMoments>
    |> Simulation.tickMultiple state
    |> fst
    |> List.filter filterDailyUpdateEffects
    |> should haveLength 10

[<Test>]
let ``tick should not update album streams or concerts if morning has passed``
    ()
    =
    Simulation.tickOne stateInMorning songStartedEffect
    |> fst
    |> List.filter filterDailyUpdateEffects
    |> should haveLength 0

let filterMarketUpdateEffects effect =
    match effect with
    | GenreMarketsUpdated _ -> true
    | _ -> false

[<Test>]
let ``tick should update markets every year in the early morning`` () =
    AdvanceTime.advanceDayMoment' stateInMidnightOfNewYear 1<dayMoments>
    |> Simulation.tickMultiple stateInMidnightOfNewYear
    |> fst
    |> List.filter filterMarketUpdateEffects
    |> should haveLength 1

    AdvanceTime.advanceDayMoment' dummyState 1<dayMoments>
    |> Simulation.tickMultiple dummyState
    |> fst
    |> List.filter filterMarketUpdateEffects
    |> should haveLength 0

[<Test>]
let ``tick should check for failed concerts in every time update`` () =
    let state =
        State.generateOne
            { State.defaultOptions with
                FutureConcertsToGenerate = 0 }
        |> State.Concerts.addScheduledConcert
            dummyBand
            (ScheduledConcert(dummyConcert, dummyToday))

    let dateAfterConcert =
        Simulation.Queries.Calendar.today state
        |> Calendar.Ops.addDays 31<days>
        |> Calendar.Transform.changeDayMoment Midnight

    let stateAfterConcert =
        TimeAdvanced dateAfterConcert |> State.Root.applyEffect state

    AdvanceTime.advanceDayMoment' stateAfterConcert 1<dayMoments>
    |> Simulation.tickMultiple stateAfterConcert
    |> fst
    |> List.filter (fun effect ->
        match effect with
        | ConcertCancelled _ -> true
        | _ -> false)
    |> should haveLength 1

[<Test>]
let ``tick should update turn time with the total minutes spent in the tick``
    ()
    =
    Simulation.tickOne state songStartedEffect
    |> snd
    |> Optic.get Lenses.State.turnMinutes_
    |> should equal 120<minute>

[<Test>]
let ``tick should advance day moment if the effects triggered enough time`` () =
    let effects, stateAfterTick =
        Simulation.tickMultiple state [ songStartedEffect; songStartedEffect ]

    effects
    |> List.find (function
        | TimeAdvanced _ -> true
        | _ -> false)
    |> should
        equal
        (TimeAdvanced(
            { Year = 2021<years>
              Season = Season.Summer
              Day = 20<days>
              DayMoment = Morning }
        ))

    stateAfterTick
    |> Optic.get Lenses.State.turnMinutes_
    |> should equal 0<minute>

[<Test>]
let ``tick should keep leftover time after advancing day moment if effects triggered enough time``
    ()
    =
    let effects, stateAfterTick =
        Simulation.tickMultiple
            state
            [ songStartedEffect; songStartedEffect; songStartedEffect ]

    effects
    |> List.find (function
        | TimeAdvanced _ -> true
        | _ -> false)
    |> should
        equal
        (TimeAdvanced(
            { Year = 2021<years>
              Season = Season.Summer
              Day = 20<days>
              DayMoment = Morning }
        ))

    stateAfterTick
    |> Optic.get Lenses.State.turnMinutes_
    |> should equal 120<minute>

[<Test>]
let ``ticks that include effects spanning multiple day moments get applied correctly``
    ()
    =
    let baristaManagerCareer = BaristaCareer.stages |> List.last

    let effects, stateAfterTick =
        CareerShiftPerformed(
            { Id = Barista
              CurrentStage = baristaManagerCareer
              Location = (Prague, "", Ids.Common.cafe) },
            4<dayMoments>,
            0.0m<dd>
        )
        |> Simulation.tickOne state

    effects
    |> List.find (function
        | TimeAdvanced _ -> true
        | _ -> false)
    |> should
        equal
        (TimeAdvanced(
            { Year = 2021<years>
              Season = Season.Summer
              Day = 20<days>
              DayMoment = Morning }
        ))

    stateAfterTick
    |> Optic.get Lenses.State.turnMinutes_
    |> should equal 0<minute>