namespace LivingVillage.Desktop.Tests open System open Microsoft.VisualStudio.TestTools.UnitTesting open LivingVillage.Kernel open LivingVillage.Kernel.Sim open LivingVillage.Desktop open LivingVillage.Desktop.VillagePresentation [] type SampleTests () = let areEqual (expected: 'T) (actual: 'T) = Assert.AreEqual<'T>(expected, actual) let freshWorld () = WorldBootstrap.initialWorld 42UL Sim.npcCount let freshView (world: World) = M5Interaction.refreshPrompt world M5Interaction.initial let driveUntil (maxFrames: int) (stop: SampleScript.SampleState -> bool) = let mutable world = freshWorld () let mutable view = freshView world let mutable state = SampleScript.initial let mutable frames = 0 while frames < maxFrames && not (stop state) do let nextWorld, nextView, nextState = SampleScript.runFrame world view state world <- nextWorld view <- nextView state <- nextState frames <- frames + 1 world, view, state, frames [] member _.SampleScriptStartsWalkingTowardHomeDoor () = let spawnWorld = freshWorld () let movedWorld = List.fold (fun w _ -> Sim.step { Input = { MoveX = -1.0f; MoveY = 0.0f } } w) spawnWorld [ 1..12 ] let view = freshView movedWorld let state, action = SampleScript.step movedWorld view SampleScript.initial areEqual SampleScript.ApproachDoor state.Stage match action with | SampleScript.MoveTo target -> areEqual SampleScript.doorWorldCenter target | other -> Assert.Fail(sprintf "expected MoveTo toward the door, got %A" other) [] member _.SampleScriptEntersTheHomeThroughTheDoor () = let _world, view, state, frames = driveUntil 2400 (fun s -> s.Stage = SampleScript.InspectInterior) Assert.IsTrue( frames < 2400, sprintf "home entry never confirmed; stage=%A frames=%d" state.Stage frames) areEqual (Inside (HomeId 1)) view.HomeMode [] member _.SampleScriptCompletesTheVillageTourWithADialogue () = let _world, view, state, frames = driveUntil 22000 (fun s -> s.Stage = SampleScript.SampleComplete) areEqual SampleScript.SampleComplete state.Stage Assert.IsFalse(state.TimedOut, sprintf "tour timed out; stage=%A frames=%d" state.Stage frames) Assert.IsTrue(state.DialogueTarget.IsSome, "no dialogue was opened during the tour") areEqual Outside view.HomeMode areEqual WorldPanel view.Panel