summaryrefslogtreecommitdiff
path: root/src/LivingVillage.Kernel.Tests/RumorTests.fs
blob: 2cbb18908fd1605f6ff6ddadee3c7dd86e10eedd (plain)
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
namespace LivingVillage.Kernel.Tests

open Microsoft.VisualStudio.TestTools.UnitTesting
open LivingVillage.Kernel
open LivingVillage.Kernel.Sim

module private RumorHarness =

    let private needs =
        { Hunger = 100.0f
          Energy = 100.0f
          Social = 100.0f
          Money = 50.0f }

    let private personality =
        { Drive = 0.5f
          Aggression = 0.0f
          Extraversion = 0.8f
          Honesty = 0.5f
          Greed = 0.5f }

    let private npc id =
        { Id = NpcId id
          Pos = { X = float32 (id * 10); Y = 0.0f }
          Inventory = Map.empty
          Mind =
            { Needs = needs
              Personality = personality
              Action = Wander
              Target = actionTarget Wander
              ActionAge = minActionTicks
              EffectDone = true
              HungerFlagged = false
              Memory = [] } }

    let world tick count =
        { Tick = tick
          Time = float tick * dtSeconds
          Rng = Rng.ofSeed 11UL
          Avatar =
              { Pos = { X = 0.0f; Y = 0.0f }
                Mind =
                    { Needs = needs
                      Personality = personality
                      Action = Wander
                      Target = actionTarget Wander
                      ActionAge = 0L
                      EffectDone = false
                      HungerFlagged = false
                      Memory = [] } }
          NoHost = { Reserved = 0UL }
          Npcs = Array.init count npc
          Events = []
          Rumors = []
          Annals = [] }

    let readyForChat (world: World) : World =
        { world with
            Npcs =
                world.Npcs
                |> Array.map (fun npc ->
                    { npc with
                        Mind =
                            { npc.Mind with
                                Action = Wander
                                Target = actionTarget Wander
                                ActionAge = minActionTicks
                                EffectDone = true } }) }

    let chat narrator receiver world =
        match Sim.chat { Narrator = NpcId narrator; Receiver = NpcId receiver } world with
        | ChatSucceeded (_, next) -> next
        | ChatRejected (failure, _) -> Assert.Fail($"chat rejected: {failure}"); Unchecked.defaultof<World>

    let rumorId (RumorId id) = id

    let orderedRumors (world: World) =
        world.Rumors |> List.sortBy (fun rumor -> rumorId rumor.Id)

    let traceText (world: World) =
        orderedRumors world
        |> List.map (fun rumor ->
            let parent = rumor.Parent |> Option.map rumorId |> Option.map string |> Option.defaultValue "-"
            sprintf "%d|%d|%d|%d|%d|%s|%d"
                (rumorId rumor.Id)
                rumor.Tick
                (match rumor.Source with NpcId id -> id)
                (match rumor.Narrator with NpcId id -> id)
                (match rumor.Receiver with NpcId id -> id)
                parent
                rumor.Depth)
        |> String.concat "\n"

    let rumor id tick dayIndex =
        { Id = RumorId id
          Tick = tick
          DayIndex = dayIndex
          OriginTick = tick
          Source = NpcId 0
          Narrator = NpcId 0
          Receiver = NpcId 1
          Parent = None
          Depth = 0
          Strength = 1.0f }

    let withRumorBudget capacity retentionDays action =
        let previousCapacity = rumorCapacity
        let previousRetention = rumorRetentionDays
        rumorCapacity <- capacity
        rumorRetentionDays <- retentionDays
        try
            action ()
        finally
            rumorCapacity <- previousCapacity
            rumorRetentionDays <- previousRetention

[<TestClass>]
type RumorTests () =

    [<TestMethod>]
    member _.IdleChatsCreateThreeHopTraceableRumorPathWithinTwoDays () =
        let initial = RumorHarness.world 0L 4
        let first = RumorHarness.chat 0 1 initial
        let second = RumorHarness.chat 1 2 (RumorHarness.readyForChat { first with Tick = ticksPerDay })
        let third = RumorHarness.chat 2 3 (RumorHarness.readyForChat { second with Tick = 2L * ticksPerDay })
        let ordered = RumorHarness.orderedRumors third

        Assert.AreEqual<int>(3, third.Rumors.Length)
        Assert.AreEqual<int>(3, ordered.Length)
        let root = ordered.[0]
        let retelling = ordered.[1]
        let final = ordered.[2]
        Assert.AreEqual<NpcId>(NpcId 0, root.Source)
        Assert.AreEqual<NpcId>(NpcId 0, root.Narrator)
        Assert.AreEqual<NpcId>(NpcId 1, root.Receiver)
        Assert.IsTrue(root.Parent.IsNone)
        Assert.AreEqual<int>(0, root.Depth)
        Assert.AreEqual<RumorId>(root.Id, retelling.Parent |> Option.get)
        Assert.AreEqual<NpcId>(root.Source, retelling.Source)
        Assert.AreEqual<NpcId>(NpcId 1, retelling.Narrator)
        Assert.AreEqual<NpcId>(NpcId 2, retelling.Receiver)
        Assert.AreEqual<int>(1, retelling.Depth)
        Assert.AreEqual<RumorId>(retelling.Id, final.Parent |> Option.get)
        Assert.AreEqual<NpcId>(NpcId 2, final.Narrator)
        Assert.AreEqual<NpcId>(NpcId 3, final.Receiver)
        Assert.AreEqual<int>(2, final.Depth)

        let path = Sim.rumorPath third final.Id
        CollectionAssert.AreEqual([| root; retelling; final |], List.toArray path)

    [<TestMethod>]
    member _.DuplicateRetellingDoesNotCreateAnotherRumorNode () =
        let first = RumorHarness.chat 0 1 (RumorHarness.world 0L 3)
        let second = RumorHarness.chat 1 2 (RumorHarness.readyForChat first)
        let duplicate = RumorHarness.chat 1 2 (RumorHarness.readyForChat second)

        Assert.AreEqual<int>(2, second.Rumors.Length)
        Assert.AreEqual<int>(2, duplicate.Rumors.Length)
        Assert.AreEqual<string>(RumorHarness.traceText second, RumorHarness.traceText duplicate)

    [<TestMethod>]
    member _.StaleRumorIsNotUsedAsParentForAReTelling () =
        let first = RumorHarness.chat 0 1 (RumorHarness.world 0L 3)
        let stale =
            first
            |> RumorHarness.readyForChat
            |> fun world -> { world with Tick = rumorFreshnessTicks + 1L }
        let next = RumorHarness.chat 1 2 stale
        let latest = RumorHarness.orderedRumors next |> List.last

        Assert.IsTrue(latest.Parent.IsNone, "stale memory must not be reused as a parent")
        Assert.IsTrue(latest.Source = NpcId 1, "a stale rumor starts a new local source")
        Assert.IsTrue(Sim.rumorStrengthAt next.Tick (RumorHarness.orderedRumors next |> List.head) < rumorMinimumStrength)

    [<TestMethod>]
    member _.InvalidParticipantsLeaveWorldUnchanged () =
        let before = RumorHarness.world 10L 2
        let requests =
            [ { Narrator = NpcId 9; Receiver = NpcId 1 }
              { Narrator = NpcId 0; Receiver = NpcId 9 }
              { Narrator = NpcId 0; Receiver = NpcId 0 } ]

        for request in requests do
            match Sim.chat request before with
            | ChatRejected (_, unchanged) -> Assert.AreEqual<World>(before, unchanged)
            | ChatSucceeded _ -> Assert.Fail($"expected invalid chat to be rejected: {request}")

    [<TestMethod>]
    member _.SameSeedAndChatOperationsProduceIdenticalRumorTrace () =
        let run seed =
            let initial = { RumorHarness.world 0L 4 with Rng = Rng.ofSeed seed }
            let first = RumorHarness.chat 0 1 initial
            let second = RumorHarness.chat 1 2 (RumorHarness.readyForChat { first with Tick = ticksPerDay })
            let third = RumorHarness.chat 2 3 (RumorHarness.readyForChat { second with Tick = 2L * ticksPerDay })
            RumorHarness.traceText third

        Assert.AreEqual<string>(run 4242UL, run 4242UL)

    [<TestMethod>]
    member _.RumorTraceTextContainsStableMetadataAndReplayablePaths () =
        let initial = RumorHarness.world 0L 4
        let first = RumorHarness.chat 0 1 initial
        let second = RumorHarness.chat 1 2 (RumorHarness.readyForChat { first with Tick = ticksPerDay })
        let third = RumorHarness.chat 2 3 (RumorHarness.readyForChat { second with Tick = 2L * ticksPerDay })

        let expected =
            String.concat "\n"
                [ sprintf "rumor_trace tick=%d count=3" (2L * ticksPerDay)
                  "rumor id=0 tick=0 origin_tick=0 source=0 narrator=0 receiver=1 parent=- depth=0 strength=1.000000 path=0"
                  sprintf "rumor id=1 tick=%d origin_tick=0 source=0 narrator=1 receiver=2 parent=0 depth=1 strength=0.500000 path=0,1" ticksPerDay
                  sprintf "rumor id=2 tick=%d origin_tick=0 source=0 narrator=2 receiver=3 parent=1 depth=2 strength=0.250000 path=0,1,2" (2L * ticksPerDay) ]

        Assert.AreEqual<string>(expected, Sim.rumorTraceText third)

    [<TestMethod>]
    member _.TrimWithinCapacityAndFreshWindowIsZeroCopy () =
        RumorHarness.withRumorBudget 4 3L (fun () ->
            let rumors =
                [ RumorHarness.rumor 3L (3L * ticksPerDay) 3L
                  RumorHarness.rumor 2L (2L * ticksPerDay) 2L
                  RumorHarness.rumor 1L (1L * ticksPerDay) 1L ]
            let trimmed = Sim.trimRumors (3L * ticksPerDay) rumors
            Assert.IsTrue(System.Object.ReferenceEquals(rumors, trimmed), "within budget the list must be returned as-is")
            Assert.AreEqual<RumorEvent list>(rumors, trimmed))

    [<TestMethod>]
    member _.TrimCapsToNewestRumors () =
        RumorHarness.withRumorBudget 4 30L (fun () ->
            let rumors = List.init 6 (fun index -> RumorHarness.rumor (int64 (5 - index)) (10L * ticksPerDay) 10L)
            let trimmed = Sim.trimRumors (10L * ticksPerDay) rumors
            Assert.AreEqual<int>(4, trimmed.Length)
            let keptIds = trimmed |> List.map (fun rumor -> RumorHarness.rumorId rumor.Id)
            CollectionAssert.AreEqual([| 5L; 4L; 3L; 2L |], List.toArray keptIds))

    [<TestMethod>]
    member _.TrimDropsRumorsOlderThanRetentionWindow () =
        RumorHarness.withRumorBudget 100 3L (fun () ->
            let rumors =
                [ RumorHarness.rumor 3L (10L * ticksPerDay) 10L
                  RumorHarness.rumor 2L (9L * ticksPerDay) 9L
                  RumorHarness.rumor 1L (6L * ticksPerDay) 6L
                  RumorHarness.rumor 0L (5L * ticksPerDay) 5L ]
            let trimmed = Sim.trimRumors (10L * ticksPerDay) rumors
            let keptIds = trimmed |> List.map (fun rumor -> RumorHarness.rumorId rumor.Id)
            CollectionAssert.AreEqual([| 3L; 2L |], List.toArray keptIds)
            Assert.AreEqual<RumorEvent list>(trimmed, Sim.trimRumors (10L * ticksPerDay) trimmed))

    [<TestMethod>]
    member _.ChatKeepsWorkingSetWithinCapacityAndIdsStayMonotonic () =
        RumorHarness.withRumorBudget 1 30L (fun () ->
            let first = RumorHarness.chat 0 1 (RumorHarness.world 0L 4)
            let second = RumorHarness.chat 1 2 (RumorHarness.readyForChat first)
            Assert.AreEqual<int>(1, first.Rumors.Length)
            Assert.AreEqual<int>(1, second.Rumors.Length)
            let head = second.Rumors |> List.head
            Assert.AreEqual<int64>(1L, RumorHarness.rumorId head.Id)
            Assert.AreEqual<RumorId option>(Some(RumorId 0L), head.Parent))

    [<TestMethod>]
    member _.CappedRumorsWithinCapacitySaveByteIdenticallyToUncapped () =
        let run capacity =
            RumorHarness.withRumorBudget capacity 30L (fun () ->
                let first = RumorHarness.chat 0 1 (RumorHarness.world 0L 4)
                let second = RumorHarness.chat 1 2 (RumorHarness.readyForChat { first with Tick = ticksPerDay })
                WorldSave.save second)
        Assert.AreEqual<string>(run 8, run System.Int32.MaxValue)

    [<TestMethod>]
    member _.RumorDayIndexMirrorsTickAndSurvivesSaveRoundTrip () =
        let world = RumorHarness.chat 0 1 (RumorHarness.world 0L 3)
        let rumor = world.Rumors |> List.head
        Assert.AreEqual<int64>(rumor.Tick / ticksPerDay, rumor.DayIndex)
        match WorldSave.load (WorldSave.save world) with
        | Error failure -> Assert.Fail($"round trip should load: {failure}")
        | Ok restored ->
            Assert.AreEqual<RumorEvent list>(world.Rumors, restored.Rumors)
            Assert.AreEqual<int64>(rumor.DayIndex, (restored.Rumors |> List.head).DayIndex)