summaryrefslogtreecommitdiff
path: root/src/LivingVillage.Kernel.Tests/DailyTaskTests.fs
blob: e784d833b966dd7c8a37b0bdac69717d414dad15 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
namespace LivingVillage.Kernel.Tests

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

module private DailyTaskHarness =

    let taskOf (template: Occupation.TaskTemplateId) (state: Occupation.TaskState) : Occupation.DailyTask =
        { TemplateId = template
          TargetNpc = None
          TargetTile = None
          OfferedTick = 0L
          DueTick = Sim.ticksPerDay - 1L
          State = state }

    let accepted (template: Occupation.TaskTemplateId) : Occupation.DailyTask =
        Occupation.acceptTask (taskOf template Occupation.Offered)

    let worldWithAnnal () : World =
        let world = Sim.initialWorldN 7UL 2
        match Sim.trade { Buyer = NpcId 0; Seller = NpcId 1; Item = Food; Quantity = 1 } world with
        | TradeSucceeded next -> next
        | TradeRejected (failure, _) -> Assert.Fail($"trade rejected: {failure}"); Unchecked.defaultof<World>

    let taskForDay (day: int64) : Occupation.DailyTask =
        (Occupation.dailyTaskOf 42UL 7UL day Occupation.Fisher).Value |> Occupation.acceptTask

    let withTempSave (action: string -> unit) =
        let id = Guid.NewGuid().ToString("N")
        let path = Path.Combine(Path.GetTempPath(), $"living-village-p11-{id}.save")
        try
            action path
        finally
            if File.Exists path then File.Delete path

[<TestClass>]
type DailyTaskTests () =

    // ---- §3 生成:hash(seed ^ occupationSeed, dayIndex) mod |pool|(splitmix 同源)----

    [<TestMethod>]
    member _.TaskHashFollowsSplitmixOverSeedXorOccupationSeed () =
        Assert.AreEqual<uint64>(0xF7E9F3F88CC04AD6UL, Occupation.taskHash 42UL 7UL 0L)
        Assert.AreEqual<uint64>(0xFB624BD98F2BE6C4UL, Occupation.taskHash 42UL 7UL 1L)
        Assert.AreEqual<uint64>(0x87FC3F1DAC740225UL, Occupation.taskHash 42UL 7UL 2L)
        Assert.AreEqual<uint64>(0xDA258F1151868B40UL, Occupation.taskHash 42UL 7UL 3L)
        Assert.AreEqual<uint64>(0x02D114B7ABDD7A1FUL, Occupation.taskHash 4242UL 99UL 0L)

    [<TestMethod>]
    member _.FarmerAndFisherPoolsAreThreeTemplatesEach () =
        Assert.AreEqual(
            [ Occupation.DeliverGrain; Occupation.HelpWork; Occupation.TillSoil ],
            Occupation.taskPoolOf Occupation.Farmer)
        Assert.AreEqual(
            [ Occupation.NightCatch; Occupation.SellFish; Occupation.MarketInquiry ],
            Occupation.taskPoolOf Occupation.Fisher)
        Assert.AreEqual<int>(0, Occupation.taskPoolOf Occupation.Peddler |> List.length)
        Assert.AreEqual<int>(0, Occupation.taskPoolOf Occupation.Scholar |> List.length)

    [<TestMethod>]
    member _.GeneratedTasksStayInPoolAndRepeatDeterministically () =
        for seed, occ in [ (42UL, 7UL); (7UL, 42UL); (123456789UL, 555UL) ] do
            for kind in [ Occupation.Farmer; Occupation.Fisher ] do
                let pool = Occupation.taskPoolOf kind
                for day in 0L .. 299L do
                    let task = (Occupation.dailyTaskOf seed occ day kind).Value
                    Assert.IsTrue(pool |> List.contains task.TemplateId, $"day {day} escaped the pool")
                    let again = (Occupation.dailyTaskOf seed occ day kind).Value
                    Assert.AreEqual<Occupation.DailyTask>(task, again)
                let distinct =
                    [ 0L .. 299L ]
                    |> List.map (fun day -> (Occupation.dailyTaskOf seed occ day kind).Value.TemplateId)
                    |> Set.ofList
                Assert.IsTrue(distinct.Count >= 2, "300 days must not pin a single template")

    [<TestMethod>]
    member _.PeddlerAndScholarHaveNoDailyTaskYet () =
        Assert.AreEqual<Occupation.DailyTask option>(None, Occupation.dailyTaskOf 42UL 7UL 0L Occupation.Peddler)
        Assert.AreEqual<Occupation.DailyTask option>(None, Occupation.dailyTaskOf 42UL 7UL 0L Occupation.Scholar)

    [<TestMethod>]
    member _.TaskWindowCoversExactlyOneDayStartingOffered () =
        let task = (Occupation.dailyTaskOf 42UL 7UL 3L Occupation.Farmer).Value
        Assert.AreEqual<int64>(3L * Sim.ticksPerDay, task.OfferedTick)
        Assert.AreEqual<int64>(4L * Sim.ticksPerDay - 1L, task.DueTick)
        Assert.AreEqual<Occupation.TaskState>(Occupation.Offered, task.State)
        Assert.AreEqual<int64>(3L, Occupation.dayIndexOf (3L * Sim.ticksPerDay))
        Assert.AreEqual<int64>(3L, Occupation.dayIndexOf (4L * Sim.ticksPerDay - 1L))

    [<TestMethod>]
    member _.TaskTemplateChineseNamesMatchDesignTable () =
        Assert.AreEqual<string>("送粮", Occupation.taskNameOf Occupation.DeliverGrain)
        Assert.AreEqual<string>("帮工", Occupation.taskNameOf Occupation.HelpWork)
        Assert.AreEqual<string>("翻土", Occupation.taskNameOf Occupation.TillSoil)
        Assert.AreEqual<string>("夜捕", Occupation.taskNameOf Occupation.NightCatch)
        Assert.AreEqual<string>("卖渔", Occupation.taskNameOf Occupation.SellFish)
        Assert.AreEqual<string>("询价", Occupation.taskNameOf Occupation.MarketInquiry)

    // ---- §3 状态机 Offered|Active|Done|Failed ----

    [<TestMethod>]
    member _.AcceptMovesOfferedToActiveOnly () =
        let offered = DailyTaskHarness.taskOf Occupation.HelpWork Occupation.Offered
        let active = Occupation.acceptTask offered
        Assert.AreEqual<Occupation.TaskState>(Occupation.Active, active.State)
        Assert.AreEqual<Occupation.TaskState>(Occupation.Active, (Occupation.acceptTask active).State)
        let finished = { active with State = Occupation.Done }
        Assert.AreEqual<Occupation.TaskState>(Occupation.Done, (Occupation.acceptTask finished).State)
        let failed = { active with State = Occupation.Failed }
        Assert.AreEqual<Occupation.TaskState>(Occupation.Failed, (Occupation.acceptTask failed).State)

    [<TestMethod>]
    member _.SellFishCompletesOnlyOnPlayerFishSales () =
        let active = DailyTaskHarness.accepted Occupation.SellFish
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Done,
            (Occupation.applySignal (Occupation.Traded(playerId, Fish, 1)) active).State)
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Active,
            (Occupation.applySignal (Occupation.Traded(playerId, Spice, 1)) active).State)
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Active,
            (Occupation.applySignal (Occupation.Traded(NpcId 2, Fish, 4)) active).State)
        let offered = DailyTaskHarness.taskOf Occupation.SellFish Occupation.Offered
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Offered,
            (Occupation.applySignal (Occupation.Traded(playerId, Fish, 1)) offered).State)

    [<TestMethod>]
    member _.DeliverGrainCompletesOnlyOnPlayerFoodGiveaways () =
        let active = DailyTaskHarness.accepted Occupation.DeliverGrain
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Done,
            (Occupation.applySignal (Occupation.Traded(playerId, Food, 3)) active).State)
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Active,
            (Occupation.applySignal (Occupation.Traded(playerId, Fish, 1)) active).State)
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Active,
            (Occupation.applySignal (Occupation.Traded(NpcId 5, Food, 1)) active).State)

    [<TestMethod>]
    member _.HelpWorkAndTillSoilRespectOptionalTargets () =
        let wildcard = DailyTaskHarness.accepted Occupation.HelpWork
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Done,
            (Occupation.applySignal (Occupation.Dialogued(NpcId 4)) wildcard).State)
        let targeted =
            { DailyTaskHarness.accepted Occupation.HelpWork with TargetNpc = Some(NpcId 2) }
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Done,
            (Occupation.applySignal (Occupation.Dialogued(NpcId 2)) targeted).State)
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Active,
            (Occupation.applySignal (Occupation.Dialogued(NpcId 4)) targeted).State)
        let untilled = DailyTaskHarness.accepted Occupation.TillSoil
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Done,
            (Occupation.applySignal (Occupation.ArrivedAt(1, 1)) untilled).State)
        let plotTilling =
            { DailyTaskHarness.accepted Occupation.TillSoil with TargetTile = Some(52, 38) }
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Done,
            (Occupation.applySignal (Occupation.ArrivedAt(52, 38)) plotTilling).State)
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Active,
            (Occupation.applySignal (Occupation.ArrivedAt(10, 10)) plotTilling).State)

    [<TestMethod>]
    member _.NightCatchAndMarketInquiryCompleteOnTheirSignals () =
        let active = DailyTaskHarness.accepted Occupation.NightCatch
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Done,
            (Occupation.applySignal Occupation.NightAtWater active).State)
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Active,
            (Occupation.applySignal (Occupation.Dialogued(NpcId 0)) active).State)
        let inquiring = DailyTaskHarness.accepted Occupation.MarketInquiry
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Done,
            (Occupation.applySignal (Occupation.Dialogued(NpcId 3)) inquiring).State)
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Active,
            (Occupation.applySignal Occupation.NightAtWater inquiring).State)

    [<TestMethod>]
    member _.DoneAndFailedAreTerminalStates () =
        let finished = { DailyTaskHarness.accepted Occupation.SellFish with State = Occupation.Done }
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Done,
            (Occupation.applySignal (Occupation.Traded(playerId, Fish, 1)) finished).State)
        let failed = { DailyTaskHarness.accepted Occupation.SellFish with State = Occupation.Failed }
        Assert.AreEqual<Occupation.TaskState>(
            Occupation.Failed,
            (Occupation.applySignal (Occupation.Traded(playerId, Fish, 1)) failed).State)

    [<TestMethod>]
    member _.OverdueTasksFailAtDayBoundaryButNotBefore () =
        let active = DailyTaskHarness.accepted Occupation.SellFish
        Assert.AreEqual<Occupation.TaskState>(Occupation.Active, (Occupation.expireAt active.DueTick active).State)
        Assert.AreEqual<Occupation.TaskState>(Occupation.Failed, (Occupation.expireAt (active.DueTick + 1L) active).State)
        let offered = DailyTaskHarness.taskOf Occupation.SellFish Occupation.Offered
        Assert.AreEqual<Occupation.TaskState>(Occupation.Failed, (Occupation.expireAt (offered.DueTick + 1L) offered).State)
        let finished = { active with State = Occupation.Done }
        Assert.AreEqual<Occupation.TaskState>(Occupation.Done, (Occupation.expireAt (finished.DueTick + 1L) finished).State)

    // ---- §2-A 存档 v3:annals 之后追加 today task 尾段 ----

    [<TestMethod>]
    member _.SaveV3AppendsTaskTailAfterAnnalsAndRoundTrips () =
        let world = DailyTaskHarness.worldWithAnnal ()
        let state =
            { (Occupation.stateOf Occupation.Fisher) with
                TaskToken = 5
                StoryStage = 2
                Today = Some(DailyTaskHarness.taskForDay 2L) }
        DailyTaskHarness.withTempSave (fun path ->
            WorldSave.saveToFileWith (Some state) path world
            let text = File.ReadAllText path
            Assert.IsTrue(text.StartsWith("LV_WORLD_SAVE_V3"), "v3 header expected")
            let tokens = text.Split('|')
            Assert.IsTrue(tokens |> Array.exists (fun token -> token = "task-today"), "task tail marker expected")
            Assert.AreEqual<string>("tile-none", Array.last tokens)
            let markerIndex = tokens |> Array.findIndex (fun token -> token = "task-today")
            let lastAnnalKindIndex =
                tokens |> Array.findIndexBack (fun token -> token = "trade")
            Assert.IsTrue(markerIndex > lastAnnalKindIndex, "task tail must follow the annals segment")
            match WorldSave.loadFromFileWith path with
            | Ok (restored, Some restoredState) ->
                Assert.AreEqual<Occupation.State>(state, restoredState)
                Assert.AreEqual<string>(WorldSave.save world, WorldSave.save restored)
                Assert.AreEqual<string>(text, WorldSave.saveWith (Some restoredState) restored)
            | Ok (_, None) -> Assert.Fail("v3 with task tail must restore the occupation")
            | Error message -> Assert.Fail($"v3 task round trip failed: {message}"))

    [<TestMethod>]
    member _.SaveV3WithoutTaskKeepsOldTailFreeLayout () =
        let world = DailyTaskHarness.worldWithAnnal ()
        let state = Occupation.stateOf Occupation.Peddler
        DailyTaskHarness.withTempSave (fun path ->
            WorldSave.saveToFileWith (Some state) path world
            let text = File.ReadAllText path
            Assert.IsTrue(text.StartsWith("LV_WORLD_SAVE_V3"), "v3 header expected")
            Assert.IsFalse(text.Contains("task-today"), "no-task v3 must not carry a task tail")
            match WorldSave.loadFromFileWith path with
            | Ok (restored, Some restoredState) ->
                Assert.AreEqual<Occupation.DailyTask option>(None, restoredState.Today)
                Assert.AreEqual<string>(text, WorldSave.saveWith (Some restoredState) restored)
            | Ok (_, None) -> Assert.Fail("v3 with kind must restore the occupation")
            | Error message -> Assert.Fail($"v3 no-task read failed: {message}"))

    [<TestMethod>]
    member _.V1AndV2SavesCarryNoTaskTail () =
        let world = Sim.initialWorld 42UL
        DailyTaskHarness.withTempSave (fun path ->
            WorldSave.saveToFile path world
            let v2Text = File.ReadAllText path
            Assert.IsTrue(v2Text.StartsWith("LV_WORLD_SAVE_V2"), "default save must stay v2")
            Assert.IsFalse(v2Text.Contains("task-today"))
            match WorldSave.loadFromFileWith path with
            | Ok (restored, occupation) ->
                Assert.AreEqual<Occupation.State option>(None, occupation)
                Assert.AreEqual<string>(v2Text, WorldSave.save restored)
            | Error message -> Assert.Fail($"v2 read failed: {message}")

            let firstPipe = v2Text.IndexOf('|')
            let secondPipe = v2Text.IndexOf('|', firstPipe + 1)
            let thirdPipe = v2Text.IndexOf('|', secondPipe + 1)
            let v1Text = "LV_WORLD_SAVE_V1" + v2Text.Substring(thirdPipe)
            File.WriteAllText(path, v1Text)
            match WorldSave.loadFromFileWith path with
            | Ok (_, occupation) -> Assert.AreEqual<Occupation.State option>(None, occupation)
            | Error message -> Assert.Fail($"v1 read failed: {message}")

            File.WriteAllText(path, WorldSave.saveWith (Some (Occupation.stateOf Occupation.Fisher)) world + "|task-today|bogus|offered|0|0|npc-none|tile-none")
            match WorldSave.loadFromFile path with
            | Ok _ -> Assert.Fail("unknown task template must be rejected")
            | Error _ -> ())

    [<TestMethod>]
    member _.TrailingGarbageAfterTaskTailIsRejected () =
        let world = DailyTaskHarness.worldWithAnnal ()
        let state = { (Occupation.stateOf Occupation.Farmer) with Today = Some(DailyTaskHarness.taskForDay 4L) }
        DailyTaskHarness.withTempSave (fun path ->
            let text = WorldSave.saveWith (Some state) world
            File.WriteAllText(path, text + "|junk")
            match WorldSave.loadFromFile path with
            | Ok _ -> Assert.Fail("trailing garbage after the task tail must be rejected")
            | Error _ -> ())

    [<TestMethod>]
    member _.TaskCarryingSavesAreByteIdenticalAcrossRuns () =
        let build () =
            let world = DailyTaskHarness.worldWithAnnal ()
            let state = { (Occupation.stateOf Occupation.Farmer) with Today = Some(DailyTaskHarness.taskForDay 9L) }
            WorldSave.saveWith (Some state) world
        Assert.AreEqual<string>(build (), build ())