Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Apr 21, 2024
1 parent 16c0403 commit 2bb95e0
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.intellij.ui.PopupHandler
import com.intellij.ui.ScrollPaneFactory
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.components.BorderLayoutPanel
import org.jetbrains.annotations.VisibleForTesting
import org.jetbrains.plugins.github.ui.HtmlInfoPanel
import java.awt.BorderLayout
import java.awt.Component
Expand All @@ -32,7 +33,8 @@ class JobsListPanel(
private val infoInNewLine: Boolean
) : BorderLayoutPanel(), Disposable {
private val topInfoPanel = HtmlInfoPanel()
private val jobsListModel = CollectionListModel<Job>()
@VisibleForTesting
internal val jobsListModel = CollectionListModel<Job>()

init {
Disposer.register(parentDisposable, this)
Expand Down
5 changes: 3 additions & 2 deletions src/test/kotlin/com/dsoftware/ghmanager/TestTools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ fun Job.withStep(
}

fun createJob(
id: Long = 21454796844, runId: Long = 7863783013, owner: String = "owner", repo: String = "repo"
id: Long = 21454796844, runId: Long = 7863783013, owner: String = "owner", repo: String = "repo",
name: String = "Job created for test",
): Job {
return Job(
id = id,
Expand All @@ -42,7 +43,7 @@ fun createJob(
createdAt = Clock.System.now(),
startedAt = Clock.System.now(),
completedAt = Clock.System.now(),
name = "Analyze (python)",
name = name,
steps = mutableListOf(),
checkRunUrl = "https://api.github.com/repos/$owner/$repo/check-runs/21454796844",
labels = arrayOf("ubuntu-latest"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ import com.dsoftware.ghmanager.api.model.WorkflowRunJobs
import com.dsoftware.ghmanager.api.model.WorkflowRuns
import com.dsoftware.ghmanager.api.model.WorkflowType
import com.dsoftware.ghmanager.api.model.WorkflowTypes
import com.dsoftware.ghmanager.createJob
import com.dsoftware.ghmanager.createWorkflowRun
import com.dsoftware.ghmanager.data.WorkflowDataContextService
import com.dsoftware.ghmanager.i18n.MessagesBundle.message
import com.dsoftware.ghmanager.ui.GhActionsMgrToolWindowContent
import com.dsoftware.ghmanager.ui.panels.JobListComponent
import com.dsoftware.ghmanager.ui.panels.JobsListPanel
import com.dsoftware.ghmanager.ui.panels.LogConsolePanel
import com.dsoftware.ghmanager.ui.panels.wfruns.WorkflowRunsListPanel
import com.intellij.openapi.components.service
import com.intellij.ui.OnePixelSplitter
import com.intellij.ui.components.JBPanelWithEmptyText
import com.intellij.ui.components.JBScrollPane
import io.mockk.MockKMatcherScope
import io.mockk.every
import io.mockk.impl.annotations.MockK
Expand All @@ -34,7 +40,6 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInfo
import org.junit.jupiter.api.extension.ExtendWith
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.JTextPane

Expand Down Expand Up @@ -100,7 +105,10 @@ class TestRepoTabControllerWorkflowRunsPanel : GhActionsMgrBaseTest() {
createWorkflowRun(id = 2, status = "completed"),
createWorkflowRun(id = 2, status = "queued"),
)
mockGithubApiRequestExecutor(workflowRunsList)
val jobsList = listOf(
createJob(id = 1, runId = 2, name = "job1"),
)
mockGithubApiRequestExecutor(workflowRunsList = workflowRunsList, jobs = jobsList)
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(projectRule.project)

// act
Expand Down Expand Up @@ -128,12 +136,91 @@ class TestRepoTabControllerWorkflowRunsPanel : GhActionsMgrBaseTest() {
jobsPanelEmptyText,
(selectedRunPanel.firstComponent.components[0] as JPanel).components[0]
)
val jobsPanel = ((selectedRunPanel.firstComponent.components[0] as JPanel).components[0] as HtmlInfoPanel)
Assertions.assertEquals("Loading...", jobsPanel)
val jobsPanel = (selectedRunPanel.firstComponent.components[0] as JobsListPanel)
Assertions.assertEquals(2, jobsPanel.componentCount)
val jobsInfoPanel = ((jobsPanel.components[0] as HtmlInfoPanel).components[0] as JTextPane)
Assertions.assertTrue(jobsInfoPanel.text.contains("1 completed (1 successful)"))
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(projectRule.project)
Assertions.assertEquals(jobsList.size, jobsPanel.jobsListModel.size)

val jobsListPanel = (jobsPanel.components[1] as JBScrollPane).viewport.view as JobListComponent
Assertions.assertEquals(jobsList.size, jobsListPanel.model.size)

// assert log is not loaded
val logPanelEmptyText = (selectedRunPanel.secondComponent.components[0] as JPanel).components[0] as JTextPane
Assertions.assertTrue(logPanelEmptyText.text.contains(message("panel.log.not-loading")))
}

@Test
fun `test selecting job shows log for job`() {
val workflowRunsList = listOf(
createWorkflowRun(id = 1, status = "in_progress"),
createWorkflowRun(id = 2, status = "completed"),
createWorkflowRun(id = 2, status = "queued"),
)
val jobsList = listOf(
createJob(id = 1, runId = 2, name = "job1"),
)
val log = "log for job1"
mockGithubApiRequestExecutor(workflowRunsList = workflowRunsList, jobs = jobsList, log = log)
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(projectRule.project)

// act
toolWindowContent.createContent()
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(projectRule.project)

// assert wf-runs loaded
val (workflowRunsListPanel, selectedRunPanel) = assertTabsAndPanels()
Assertions.assertEquals(workflowRunsList.size, workflowRunsListPanel.runListComponent.model.size)

val jobsPanelEmptyText = (selectedRunPanel.firstComponent.components[0] as JPanel).components[0] as JTextPane
Assertions.assertTrue(jobsPanelEmptyText.text.contains(message("panel.jobs.not-loading")))

// act workflow run selected => jobs listed
workflowRunsListPanel.runListComponent.setSelectedValue(
workflowRunsListPanel.runListComponent.model.getElementAt(0),
false
)
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(projectRule.project)

// assert workflow run selected => jobs listed
Assertions.assertEquals(
workflowRunsListPanel.runListComponent.selectedValue,
workflowRunsListPanel.runListComponent.model.getElementAt(0)
)
Assertions.assertNotEquals(
jobsPanelEmptyText,
(selectedRunPanel.firstComponent.components[0] as JPanel).components[0]
)
val jobsPanel = (selectedRunPanel.firstComponent.components[0] as JobsListPanel)
Assertions.assertEquals(2, jobsPanel.componentCount)
val jobsInfoPanel = ((jobsPanel.components[0] as HtmlInfoPanel).components[0] as JTextPane)
Assertions.assertTrue(jobsInfoPanel.text.contains("1 completed (1 successful)"))
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(projectRule.project)
Assertions.assertEquals(jobsList.size, jobsPanel.jobsListModel.size)

val jobsListPanel = (jobsPanel.components[1] as JBScrollPane).viewport.view as JobListComponent
Assertions.assertEquals(jobsList.size, jobsListPanel.model.size)

// assert log is not loaded
val logPanelEmptyText = (selectedRunPanel.secondComponent.components[0] as JPanel).components[0] as JTextPane
Assertions.assertTrue(logPanelEmptyText.text.contains(message("panel.log.not-loading")))

// act job selected => log loaded
jobsListPanel.setSelectedValue(jobsListPanel.model.getElementAt(0), false)
executeSomeCoroutineTasksAndDispatchAllInvocationEvents(projectRule.project)

Assertions.assertNotEquals(
logPanelEmptyText,
(selectedRunPanel.secondComponent.components[0] as JPanel).components[0]
)
val logPanel =
((selectedRunPanel.secondComponent.components[0] as JBPanelWithEmptyText).components[0] as LogConsolePanel)
Assertions.assertEquals(log, logPanel.editor.document.text)

}


@Test
fun `test repo without workflow-runs`() {
mockGithubApiRequestExecutor(emptyList())
Expand Down Expand Up @@ -190,6 +277,7 @@ class TestRepoTabControllerWorkflowRunsPanel : GhActionsMgrBaseTest() {
branches: Collection<String> = emptyList(),
workflowTypes: Collection<WorkflowType> = emptyList(),
jobs: Collection<Job> = emptyList(),
log: String = "",
) {
val collaboratorsResponse = GithubResponsePage(collaborators.map {
val user = mockk<GithubUserWithPermissions> {
Expand All @@ -205,6 +293,9 @@ class TestRepoTabControllerWorkflowRunsPanel : GhActionsMgrBaseTest() {
})
val workflowTypesResponse = WorkflowTypes(workflowTypes.size, workflowTypes.toList())
executorMock.apply {
every {
execute(any(), matchApiRequestUrl<String>("/logs")).hint(String::class)
} returns log
every {
execute(any(), matchApiRequestUrl<WorkflowRunJobs>("/jobs")).hint(WorkflowRunJobs::class)
} returns WorkflowRunJobs(jobs.size, jobs = jobs.toList())
Expand Down

0 comments on commit 2bb95e0

Please sign in to comment.