Open History to see the last commit
1import XCTest
2@testable import ExpeditionCore
3
4final class ComputerTraversalTests: XCTestCase {
5 func testPhysicalAutopilotUsesARealRopeAndMakesProgress() {
6 let rim = DesktopWindow(id: UInt32.max, frame: .init(x: 0, y: 799, width: 1000, height: 1))
7 let terrain = TerrainSnapshot(windows: [rim], viewport: .init(x: 0, y: 0, width: 1000, height: 800))
8 var explorer = ExplorerInteraction(position: .init(x: 300, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 724)))
9 explorer.autonomous = true; explorer.updateTerrain(terrain); explorer.setKeyboardControl(true)
10 var usedRope = false
11 for _ in 0..<120 * 30 {
12 explorer.steerAgent(towardHand: .init(x: 600, y: 450)); explorer.advance(by: 1.0 / 120)
13 usedRope = usedRope || explorer.ropeSession != nil
14 XCTAssertTrue(explorer.position.x.isFinite && explorer.position.y.isFinite)
15 }
16 XCTAssertTrue(usedRope)
17 XCTAssertLessThan(abs(explorer.position.x - 580), 35)
18 XCTAssertLessThan(abs(explorer.position.y - 405), 35)
19 }
20 func testAutopilotCanCrossTheMenuBarAndReturnToTheDock() {
21 let terrain = TerrainSnapshot(windows: [DesktopWindow(id: UInt32.max, frame: .init(x: 0, y: 799, width: 1000, height: 1))], viewport: .init(x: 0, y: 0, width: 1000, height: 800))
22 var explorer = ExplorerInteraction(position: .init(x: 300, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 724)))
23 explorer.autonomous = true; explorer.updateTerrain(terrain); explorer.setKeyboardControl(true)
24 for target in [WorldPoint(x: 19, y: 788), .init(x: 260, y: 788), .init(x: 600, y: 35), .init(x: 460, y: 440)] {
25 for _ in 0..<120 * 35 { explorer.steerAgent(towardHand: target); explorer.advance(by: 1.0 / 120); explorer.updateTerrain(terrain) }
26 if target.y > 760 { XCTAssertTrue(explorer.agentCeilingHanging, "Menu-bar targets should finish with a planted top-edge grip") }
27 let localX = (target.x - explorer.position.x) * explorer.facing
28 let localY = target.y - explorer.position.y
29 let shoulderY = 46.0 - (explorer.canCrouch ? explorer.crouchBlend * 28 : 0)
30 XCTAssertLessThan(hypot(localX - 10, localY - shoulderY), 29, "target \(target), feet \(explorer.position), state \(explorer.state)")
31 }
32 }
33
34 func testTopEdgeGripLowersOntoConnectedRopeForAnOpenMenu() {
35 let terrain = TerrainSnapshot(windows: [DesktopWindow(id: UInt32.max, frame: .init(x: 0, y: 799, width: 1000, height: 1))], viewport: .init(x: 0, y: 0, width: 1000, height: 800))
36 var explorer = ExplorerInteraction(position: .init(x: 300, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 724)))
37 explorer.autonomous = true; explorer.updateTerrain(terrain); explorer.setKeyboardControl(true)
38 for _ in 0..<120 * 30 { explorer.steerAgent(towardHand: .init(x: 260, y: 788)); explorer.advance(by: 1.0 / 120) }
39 XCTAssertTrue(explorer.agentCeilingHanging)
40 let gripX = explorer.position.x
41 explorer.steerAgent(towardHand: .init(x: gripX + 20, y: 700))
42 XCTAssertFalse(explorer.agentCeilingHanging)
43 XCTAssertEqual(explorer.ropeSession?.anchor.windowID, UInt32.max)
44 XCTAssertEqual(explorer.ropeSession?.anchor.offset ?? -1, gripX, accuracy: 0.1)
45 XCTAssertEqual(explorer.ropeSession?.mode, .descending)
46 for _ in 0..<120 * 3 { explorer.steerAgent(towardHand: .init(x: gripX + 20, y: 700)); explorer.advance(by: 1.0 / 120) }
47 XCTAssertLessThan(explorer.position.y, 690)
48 XCTAssertNotNil(explorer.ropeSession)
49 }
50
51 func testMenuDescentCanPassAWindowRoofAndReachItsControls() {
52 let terrain = TerrainSnapshot(windows: [DesktopWindow(id: UInt32.max, frame: .init(x: 0, y: 799, width: 1000, height: 1)), DesktopWindow(id: 42, frame: .init(x: 200, y: 100, width: 600, height: 400))], viewport: .init(x: 0, y: 0, width: 1000, height: 800))
53 var explorer = ExplorerInteraction(position: .init(x: 300, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 724)))
54 explorer.autonomous = true; explorer.updateTerrain(terrain); explorer.setKeyboardControl(true)
55 for target in [WorldPoint(x: 260, y: 788), .init(x: 400, y: 350), .init(x: 600, y: 230)] {
56 for tick in 0..<120 * 45 {
57 explorer.steerAgent(towardHand: target); explorer.advance(by: 1.0 / 120)
58 if tick % 12 == 0 { explorer.updateTerrain(terrain) }
59 }
60 XCTAssertLessThan(abs(explorer.position.x - max(24, target.x - 20)), 35, "x for \(target), \(explorer.position), \(explorer.state)")
61 XCTAssertLessThan(abs(explorer.position.y - min(724, max(0, target.y - 45))), 35, "y for \(target), \(explorer.position), \(explorer.state)")
62 }
63 }
64
65 func testLongFloorTravelWalksBeforeGrapplingUpward() {
66 let terrain = TerrainSnapshot(windows: [DesktopWindow(id: UInt32.max, frame: .init(x: 0, y: 799, width: 1000, height: 1))], viewport: .init(x: 0, y: 0, width: 1000, height: 800))
67 var explorer = ExplorerInteraction(position: .init(x: 100, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 724)))
68 explorer.autonomous = true; explorer.updateTerrain(terrain); explorer.setKeyboardControl(true)
69 for _ in 0..<60 { explorer.steerAgent(towardHand: .init(x: 800, y: 500)); explorer.advance(by: 1.0 / 120) }
70 XCTAssertNil(explorer.ropeSession)
71 XCTAssertGreaterThan(explorer.position.x, 110)
72 XCTAssertTrue([ExplorerInteraction.State.walking, .running].contains(explorer.state))
73 }
74
75 func testLowerPointerDoesNotChangeTheCrouchPose() {
76 let terrain = TerrainSnapshot(windows: [], viewport: .init(x: 0, y: 0, width: 1000, height: 800))
77 var explorer = ExplorerInteraction(position: .init(x: 300, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 724)))
78 explorer.autonomous = true; explorer.updateTerrain(terrain); explorer.setKeyboardControl(true); explorer.setControlCrouch(true)
79 for _ in 0..<60 { explorer.advance(by: 1.0 / 120) }
80 let blend = explorer.crouchBlend, point = explorer.controlClickPoint
81 explorer.setControlPointerLowered(true)
82 XCTAssertEqual(explorer.crouchBlend, blend)
83 XCTAssertEqual(explorer.controlClickPoint.x, point.x)
84 XCTAssertEqual(explorer.controlClickPoint.y, point.y - 16, accuracy: 0.01)
85 explorer.setControlPointerLowered(false)
86 XCTAssertEqual(explorer.controlClickPoint, point)
87 }
88
89}