import FoldFormat import Models import Testing import simd @testable import Fold @MainActor private func cup(step: Int = 1) throws -> FoldSession { let model = try #require(try ModelLibrary.all().first { $0.fileTitle == "Cup" }) return try FoldSession(model: model, step: step) } @MainActor @Test func incrementingMovesToTheNextTenth() throws { let session = try cup() session.adjust(1) #expect(session.t == 0.1) session.adjust(1); session.adjust(1) #expect(session.t == 0.3) session.t = 0.37 session.adjust(1) #expect(session.t == 0.4) } @MainActor @Test func decrementingMovesToTheTenthBelowAndStopsAtZero() throws { let session = try cup() session.adjust(-1) #expect(session.t == 0) session.t = 0.37 session.adjust(-1) #expect(session.t == 0.3) } @MainActor @Test func incrementingToTheEndCompletesTheStep() throws { let session = try cup() for _ in 0..<9 { session.adjust(1) } #expect(session.step.index == 1 && session.t == 0.9) session.adjust(1) #expect(session.step.index == 2) #expect(session.t == 0) } @MainActor @Test func incrementingOnTheLastStepStaysAtTheEnd() throws { let session = try cup(step: 6) for _ in 0..<12 { session.adjust(1) } #expect(session.step.index == 6) #expect(session.t == 1) } @MainActor @Test func zoomIsClampedAndOrbitAndZoomLeaveTheFoldAlone() throws { let session = try cup() session.t = 0.5 session.zoom(by: 100) #expect(session.fieldOfView == FoldSession.fieldOfViewRange.lowerBound) session.zoom(by: 0.001) #expect(session.fieldOfView == FoldSession.fieldOfViewRange.upperBound) session.orbit(by: SIMD2(0.4, -0.7)) #expect(session.t == 0.5) #expect(session.step.index == 1) } @MainActor @Test func completingAStepResetsTheCameraToTheNextViewpoint() throws { let session = try cup() let start = session.camera session.orbit(by: SIMD2(0.4, -0.7)) session.zoom(by: 2) session.t = 1 session.completeStep() #expect(session.step.index == 2) #expect(session.eyeDirection == SIMD3(0, 0, 1)) #expect(session.camera.fieldOfView == start.fieldOfView) } /// The translation that takes a fold drag begun at `start` to `t` along the session's path, and /// 30 points past its end when `t` is 1. @MainActor private func translation(_ session: FoldSession, from start: Double = 0, to t: Double) -> SIMD2 { let m = session.mapping let past = t == 1 ? simd_normalize(m.point(at: 1) - m.point(at: 127.0 / 128)) * 30 : .zero return m.point(at: t) - m.point(at: start) + past } @MainActor private func cupInView(step: Int = 1) throws -> FoldSession { let session = try cup(step: step) session.viewportSize = SIMD2(400, 400) return session } @MainActor @Test func anOrbitDuringADragRestoresTAndTheDragDoesNotComplete() throws { let session = try cupInView() session.t = 0.2 session.beginFoldDrag() session.drag(translation: translation(session, from: 0.2, to: 1)) #expect(session.t == 1) session.beginOrbit() #expect(session.t == 0.2) #expect(session.isDragSuspended) session.endOrbit() #expect(session.isDragSuspended) session.endDrag(translation: translation(session, from: 0.2, to: 1)) #expect(session.step.index == 1) #expect(session.t == 0.2) #expect(!session.isDragSuspended) } @MainActor @Test func aDragThatStartsDuringAnOrbitIsSuspended() throws { let session = try cupInView() session.beginOrbit() session.beginFoldDrag() #expect(session.isDragSuspended) session.endOrbit() session.endDrag(translation: translation(session, to: 1)) #expect(session.step.index == 1) #expect(session.t == 0) } @MainActor @Test func aDragWithoutAnOrbitCompletesAtTheEnd() throws { let session = try cupInView() session.beginFoldDrag() session.drag(translation: translation(session, to: 1)) session.endDrag(translation: translation(session, to: 1)) #expect(session.step.index == 2) } @MainActor @Test func theReleasePositionIsAppliedBeforeTheLift() throws { // the last movement can arrive only with the lift let session = try cupInView() session.beginFoldDrag() session.drag(translation: translation(session, to: 0.5)) #expect(abs(session.t - 0.5) < 0.01) session.endDrag(translation: translation(session, to: 1)) #expect(session.step.index == 2) #expect(session.t == 0) } @MainActor @Test func aReleaseShortOfTheEndLeavesTWhereTheFingerLifted() throws { let session = try cupInView() session.beginFoldDrag() session.endDrag(translation: translation(session, to: 0.5)) #expect(session.step.index == 1) #expect(abs(session.t - 0.5) < 0.01) } @MainActor @Test func aTurnOverReleasedPastTheModelsWidthCompletes() throws { let session = try cupInView(step: 5) #expect(session.step.kind == .turnOver) session.beginReorientDrag() session.endDrag(translation: SIMD2(1000, 0)) #expect(session.step.index == 6) } @MainActor @Test func anOrbitAfterACancelledDragLeavesTAlone() throws { let session = try cupInView() session.t = 0.2 session.beginFoldDrag() session.drag(translation: translation(session, from: 0.2, to: 0.6)) session.cancelDrag() session.beginOrbit() #expect(abs(session.t - 0.6) < 0.01) #expect(!session.isDragSuspended) session.endOrbit() } @MainActor @Test func openingAStepStartsItFromTheFrameBeforeIt() throws { let session = try cup() session.orbit(by: SIMD2(0.4, -0.7)) session.zoom(by: 2) session.t = 0.6 session.open(step: 4) let fresh = try cup(step: 4) #expect(session.step.index == 4) #expect(session.t == 0) #expect(session.camera.eye == fresh.camera.eye) #expect(session.camera.up == fresh.camera.up) #expect(session.camera.fieldOfView == fresh.camera.fieldOfView) #expect(session.diagram == fresh.diagram) #expect(session.meshPositions == fresh.meshPositions) } @MainActor @Test func openingAnEarlierStepGoesBackToItsStart() throws { let session = try cup(step: 4) session.t = 1 session.completeStep() session.open(step: 2) #expect(session.step.index == 2) #expect(session.t == 0) #expect(session.meshPositions == (try cup(step: 2)).meshPositions) } @MainActor @Test func openingTheCurrentStepLeavesItAlone() throws { let session = try cup(step: 3) session.orbit(by: SIMD2(0.4, -0.7)) session.t = 0.4 let camera = session.camera session.open(step: 3) #expect(session.step.index == 3) #expect(session.t == 0.4) #expect(session.camera.eye == camera.eye) } @MainActor @Test func aDragBackTakesTheFoldBackToItsStart() throws { let session = try cupInView() session.beginFoldDrag() session.drag(translation: translation(session, to: 0.7)) session.drag(translation: translation(session, to: 0.3)) #expect(abs(session.t - 0.3) < 0.01) session.endDrag(translation: .zero) #expect(session.step.index == 1) #expect(session.t == 0) } @MainActor @Test func openingAStepFromTheStartOfAnotherShowsTheNewStep() throws { let session = try cup() session.open(step: 4) #expect(session.meshPositions == (try cup(step: 4)).meshPositions) } @MainActor @Test func openingAStepDuringADragLeavesTheNewStepAlone() throws { let session = try cupInView() session.beginFoldDrag() let end = translation(session, to: 1) session.drag(translation: translation(session, to: 0.5)) session.open(step: 4) session.drag(translation: end) session.endDrag(translation: end) #expect(session.step.index == 4) #expect(session.t == 0) } @MainActor @Test func anOrbitAfterOpeningAStepDuringADragLeavesTAlone() throws { let session = try cupInView() session.t = 0.2 session.beginFoldDrag() session.drag(translation: translation(session, from: 0.2, to: 0.6)) session.open(step: 4) session.beginOrbit() #expect(session.t == 0) #expect(!session.isDragSuspended) session.endOrbit() } /// The cup at its last step, finished by a drag to the end and a lift. @MainActor private func finishedCup() throws -> FoldSession { let session = try cupInView(step: 6) session.beginFoldDrag() session.endDrag(translation: translation(session, to: 1)) return session } @MainActor @Test func finishingTheLastStepFinishesAndLocksTheModel() throws { let session = try finishedCup() #expect(session.step.index == 6) #expect(session.t == 1) #expect(session.isFinished) #expect(session.isLocked) #expect(session.finishCount == 1) } @MainActor @Test func adjustingToTheEndOfTheLastStepFinishesTheModel() throws { let session = try cup(step: 6) for _ in 0..<9 { session.adjust(1) } #expect(!session.isFinished) session.adjust(1) #expect(session.isFinished && session.isLocked) } @MainActor @Test func dragsAndAdjustingAreIgnoredWhileLocked() throws { let session = try finishedCup() session.beginFoldDrag() session.drag(translation: translation(session, from: 1, to: 0.5)) #expect(session.t == 1) session.endDrag(translation: translation(session, from: 1, to: 0.5)) session.beginReorientDrag() session.endDrag(translation: SIMD2(-1000, 0)) session.adjust(-1) #expect(session.t == 1) #expect(session.isLocked) #expect(session.finishCount == 1) } @MainActor @Test func unlockingThenDraggingBackUnfoldsAndLeavesTheFinishedState() throws { let session = try finishedCup() session.toggleLock() #expect(!session.isLocked) #expect(session.isFinished) session.beginFoldDrag() session.drag(translation: translation(session, from: 1, to: 0.5)) #expect(abs(session.t - 0.5) < 0.01) session.endDrag(translation: translation(session, from: 1, to: 0.5)) #expect(abs(session.t - 0.5) < 0.01) #expect(!session.isFinished) #expect(!session.isLocked) } @MainActor @Test func foldingTheLastStepAgainFinishesAgain() throws { let session = try finishedCup() session.toggleLock() session.adjust(-1) #expect(session.t == 0.9) #expect(!session.isFinished) session.beginFoldDrag() session.endDrag(translation: translation(session, from: 0.9, to: 1)) #expect(session.isFinished && session.isLocked) #expect(session.finishCount == 2) } @MainActor @Test func liftingAtTheEndWhileFinishedAndUnlockedDoesNotFinishAgain() throws { let session = try finishedCup() session.toggleLock() session.beginFoldDrag() session.endDrag(translation: translation(session, from: 1, to: 1)) #expect(session.isFinished && !session.isLocked) #expect(session.finishCount == 1) } @MainActor @Test func openingAnotherStepLeavesTheFinishedState() throws { let session = try finishedCup() session.open(step: 2) #expect(session.step.index == 2) #expect(session.t == 0) #expect(!session.isFinished && !session.isLocked) session.toggleLock() #expect(!session.isLocked) } @MainActor @Test func theLockDoesNothingBeforeTheModelIsFinished() throws { let session = try cup(step: 6) session.t = 1 session.toggleLock() #expect(!session.isLocked && !session.isFinished) } @MainActor @Test func orbitAndZoomStillChangeTheCameraWhileLocked() throws { let session = try finishedCup() let camera = session.camera session.beginOrbit() session.orbit(by: SIMD2(0.4, -0.7)) session.zoom(by: 2) session.endOrbit() #expect(session.camera.eye != camera.eye) #expect(session.camera.fieldOfView != camera.fieldOfView) #expect(session.t == 1 && session.isLocked) } @MainActor @Test func anOrbitDuringADragBackLeavesTheModelFinished() throws { let session = try finishedCup() session.toggleLock() session.beginFoldDrag() session.drag(translation: translation(session, from: 1, to: 0.7)) session.beginOrbit() #expect(session.t == 1) session.endOrbit() session.endDrag(translation: translation(session, from: 1, to: 0.7)) #expect(session.t == 1) #expect(session.isFinished && !session.isLocked) #expect(session.finishCount == 1) } @MainActor @Test func lockingDuringADragBackReturnsTheModelToTheEnd() throws { let session = try finishedCup() session.toggleLock() session.beginFoldDrag() session.drag(translation: translation(session, from: 1, to: 0.7)) session.toggleLock() #expect(session.t == 1) #expect(session.isFinished && session.isLocked) session.endDrag(translation: translation(session, from: 1, to: 0.7)) #expect(session.t == 1 && session.isLocked) }