fun testUseCase4()

in src/integrationTests/kotlin/UseCase4Test.kt [108:319]


    fun testUseCase4() {
        runBlocking(Dispatchers.EDT) {
            openAndInitializePlugin(5)
            val modelService = project.service<ModelService>()

            // open the side panel
            val addBranchAction = AddBranchAction()
            val testEvent1 = createTestEvent(addBranchAction)
            addBranchAction.actionPerformed(testEvent1)

            val nav = BranchNavigationListener(project, modelService)

            val sidePanel =
                project.service<ActionService>()
                    .mainPanel.sidePanel
            val sidePanelPane = project.service<ActionService>().mainPanel.sidePanelPane
            assertThat(sidePanelPane.isVisible).isTrue()

            Awaitility.await()
                .pollInSameThread()
                .alias("open side panel and check if correct branches are displayed")
                .atMost(30000, TimeUnit.MILLISECONDS)
                .pollDelay(50, TimeUnit.MILLISECONDS)
                .until {
                    sidePanel.branches.equals(listOf("development", "main"))
                }

            val mainBranchPanel = sidePanel.sideBranchPanels[1]
            assertThat(mainBranchPanel.branchName).isEqualTo("main")

            // to select a branch to add to the view
            val mainBranchPanelListener = mainBranchPanel.mouseListeners[0]
            val mouseEvent = MouseEvent(mainBranchPanel, 444, 0L, 0, 2, 2, 1, false)

            mainBranchPanelListener.mouseClicked(mouseEvent)

            Awaitility.await()
                .atMost(15000, TimeUnit.MILLISECONDS)
                .pollDelay(50, TimeUnit.MILLISECONDS)
                .alias("adding a second branch to the view and refreshing")
                .pollInSameThread()
                .until {
                    modelService.graphInfo.addedBranch != null
                }

            // check if the correct information about the 2 branches is added to the model
            val addedBranch = modelService.graphInfo.addedBranch
            Awaitility.await()
                .pollInSameThread()
                .alias("add main to view")
                .atMost(15000, TimeUnit.MILLISECONDS)
                .pollDelay(50, TimeUnit.MILLISECONDS)
                .until {
                    addedBranch?.name == "main"
                }

            Awaitility.await()
                .pollInSameThread()
                .alias("correct commits on main branch")
                .atMost(15000, TimeUnit.MILLISECONDS)
                .pollDelay(50, TimeUnit.MILLISECONDS)
                .until {
                    val commitsOnMainBranch = addedBranch?.initialCommits?.map { it.commit.subject }
                    commitsOnMainBranch == listOf("third")
                }

            assertThat(modelService.graphInfo.mainBranch.name).isEqualTo(featureBranch)
            assertThat(modelService.graphInfo.mainBranch.initialCommits.map { it.commit.subject })
                .isEqualTo(listOf("new file", "testy", "it works", "whatever", "refactor"))

            mainBranchPanelListener.mouseClicked(mouseEvent)

            Awaitility.await()
                .atMost(15000, TimeUnit.MILLISECONDS)
                .pollDelay(50, TimeUnit.MILLISECONDS)
                .alias("removing a second branch from the view and refreshing")
                .pollInSameThread()
                .until {
                    modelService.graphInfo.addedBranch == null
                }

            val devBranchPanel = sidePanel.sideBranchPanels[0]
            assertThat(devBranchPanel.branchName).isEqualTo("development")

            // to select a branch to add to the view
            val devBranchPanelListener = devBranchPanel.mouseListeners[0]
            val mouseEvent2 = MouseEvent(devBranchPanel, 9, 0L, 0, 2, 2, 1, false)

            devBranchPanelListener.mouseClicked(mouseEvent2)

            Awaitility.await()
                .atMost(15000, TimeUnit.MILLISECONDS)
                .pollDelay(50, TimeUnit.MILLISECONDS)
                .alias("adding dev branch to the view and refreshing")
                .pollInSameThread()
                .until {
                    modelService.graphInfo.addedBranch != null
                }

            // check if the correct information about the 2 branches is added to the model
            val devBranch = modelService.graphInfo.addedBranch

            assertThat(devBranch?.name).isEqualTo("development")

            Awaitility.await()
                .atMost(15000, TimeUnit.MILLISECONDS)
                .pollDelay(50, TimeUnit.MILLISECONDS)
                .alias("check commits in dev branch are correct")
                .pollInSameThread()
                .until {
                    val commitsOnDevBranch = devBranch?.initialCommits?.map { it.commit.subject }
                    commitsOnDevBranch == listOf("my final commit", "i love testing", "code quality", "first", "initial")
                }

            assertThat(modelService.graphInfo.mainBranch.name).isEqualTo(featureBranch)
            assertThat(modelService.graphInfo.mainBranch.initialCommits.map { it.commit.subject })
                .isEqualTo(listOf("new file", "testy", "it works", "whatever", "refactor", "third", "second"))

            val changeBaseAction = RebaseAction()
            val changeBaseEvent = createTestEvent(changeBaseAction)
            changeBaseAction.update(changeBaseEvent)
            assertThat(changeBaseEvent.presentation.isEnabled).isTrue()

            // go to second branch with keyboard navigation
            nav.down()
            nav.right()
            nav.up()
            nav.up()

            val headOfSecondBranch = modelService.graphInfo.addedBranch?.selectedCommits?.get(0)
            modelService.graphInfo.addedBranch?.baseCommit = headOfSecondBranch
            project.service<ActionService>().takeNormalRebaseAction()

            var invokerCommands = project.service<RebaseInvoker>().commands.filterIsInstance<RebaseCommand>()
            assertThat(invokerCommands[0].commit).isEqualTo(headOfSecondBranch)

            // undoes the rebase action
            val undoAction = UndoAction()
            val undoEvent = createTestEvent(undoAction)
            undoAction.update(undoEvent)
            assertThat(undoEvent.presentation.isEnabled).isTrue()

            undoAction.actionPerformed(undoEvent)

            modelService.selectSingleCommit(
                modelService.graphInfo.addedBranch!!.currentCommits[1],
                modelService.graphInfo.addedBranch!!,
            )

            // redoes the rebase action
            val redoAction = RedoAction()
            val redoEvent = createTestEvent(redoAction)
            redoAction.update(redoEvent)
            assertThat(redoEvent.presentation.isEnabled).isTrue()

            redoAction.actionPerformed(redoEvent)
            invokerCommands = project.service<RebaseInvoker>().commands.filterIsInstance<RebaseCommand>()
            assertThat(invokerCommands[0].commit).isEqualTo(headOfSecondBranch)

            // resets all changes made to the branch
            val resetAction = ResetAction()
            val resetEvent = createTestEvent(resetAction)
            resetAction.update(resetEvent)
            assertThat(resetEvent.presentation.isEnabled).isTrue()
            resetAction.actionPerformed(resetEvent)

            assertThat(project.service<RebaseInvoker>().commands.isEmpty()).isTrue()

            assertThat(resetEvent.presentation.isEnabled).isTrue()
            assertThat(resetAction.actionUpdateThread).isEqualTo(ActionUpdateThread.EDT)
            resetAction.actionPerformed(resetEvent)

            // move the base of the branch to be the second to last commit on the second branch
            val secondChangeBaseEvent = createTestEvent(changeBaseAction)
            changeBaseAction.update(secondChangeBaseEvent)
            assertThat(secondChangeBaseEvent.presentation.isEnabled).isTrue()

            // go to second branch with keyboard navigation
            nav.down()
            nav.right()
            nav.up()
            val commitOnSecondBranch = modelService.graphInfo.addedBranch?.selectedCommits?.get(0)
            modelService.graphInfo.addedBranch?.baseCommit = commitOnSecondBranch
            project.service<ActionService>().takeNormalRebaseAction()

            invokerCommands = project.service<RebaseInvoker>().commands.filterIsInstance<RebaseCommand>()
            assertThat(invokerCommands[0].commit).isEqualTo(commitOnSecondBranch)

            nav.down()
            nav.left()
            nav.right()
            nav.right()
            nav.left()

            // starts the rebase process
            val rebaseAction = StartRebaseAction()
            val rebaseEvent = createTestEvent(rebaseAction)
            assertThat(rebaseAction.actionUpdateThread).isEqualTo(ActionUpdateThread.EDT)
            rebaseAction.actionPerformed(rebaseEvent)

            // asserts that the rebase action was done, moving it further away from the initial commit,
            // 3 commits to be exact
            Awaitility.await()
                .alias("rebase action being done")
                .pollInSameThread()
                .atMost(10000, TimeUnit.MILLISECONDS)
                .pollDelay(50, TimeUnit.MILLISECONDS)
                .until {
                    countCommitsSinceSpecificCommit(initialCommitOnMain) == 8
                }
        }
    }