From 30839c8613f851a369c6f8bb18d29b555ff9def9 Mon Sep 17 00:00:00 2001 From: Asher Date: Thu, 3 Oct 2024 17:58:22 -0800 Subject: [PATCH] Fix selection when workspace name collides This can happen if multiple users are using the same name for their workspaces. Turns out we can just match the workspace ID. It hardly seems worth a test to see that a == is working, so I did not bother to update it. --- .../gateway/models/WorkspaceAgentListModel.kt | 3 +- .../views/steps/CoderWorkspacesStepView.kt | 20 ++++--- .../steps/CoderWorkspacesStepViewTest.kt | 55 ------------------- 3 files changed, 13 insertions(+), 65 deletions(-) delete mode 100644 src/test/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepViewTest.kt diff --git a/src/main/kotlin/com/coder/gateway/models/WorkspaceAgentListModel.kt b/src/main/kotlin/com/coder/gateway/models/WorkspaceAgentListModel.kt index 3c7abada..f7b94da1 100644 --- a/src/main/kotlin/com/coder/gateway/models/WorkspaceAgentListModel.kt +++ b/src/main/kotlin/com/coder/gateway/models/WorkspaceAgentListModel.kt @@ -16,6 +16,7 @@ data class WorkspaceAgentListModel( var icon: Icon? = null, // The combined status of the workspace and agent to display on the row. val status: WorkspaceAndAgentStatus = WorkspaceAndAgentStatus.from(workspace, agent), - // The combined `workspace.agent` name to display on the row. + // The combined `workspace.agent` name to display on the row. Users can have workspaces with the same name, so it + // must not be used as a unique identifier. val name: String = if (agent != null) "${workspace.name}.${agent.name}" else workspace.name, ) diff --git a/src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt b/src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt index 71cb48b0..3873ef32 100644 --- a/src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt +++ b/src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt @@ -989,17 +989,19 @@ class WorkspacesTable : TableView(WorkspacesTableModel( } } - fun getNewSelection(oldSelection: WorkspaceAgentListModel?): Int { + /** + * If a row becomes unselected because the workspace turned on, find the + * first agent row and select that. + * + * If a row becomes unselected because the workspace turned off, find the + * workspace row and select that. + */ + private fun getNewSelection(oldSelection: WorkspaceAgentListModel?): Int { if (oldSelection == null) { return -1 } - val index = listTableModel.items.indexOfFirst { it.name == oldSelection.name } - if (index > -1) { - return index - } - // If there is no matching agent, try matching on just the workspace. - // It is possible it turned off so it no longer has agents displaying; - // in this case we want to keep it highlighted. - return listTableModel.items.indexOfFirst { it.workspace.name == oldSelection.workspace.name } + // Both cases are handled by just looking for the ID, since we only ever + // show agents or a workspace but never both. + return listTableModel.items.indexOfFirst { it.workspace.id == oldSelection.workspace.id } } } diff --git a/src/test/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepViewTest.kt b/src/test/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepViewTest.kt deleted file mode 100644 index 6d5cc559..00000000 --- a/src/test/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepViewTest.kt +++ /dev/null @@ -1,55 +0,0 @@ -package com.coder.gateway.views.steps - -import com.coder.gateway.sdk.DataGen -import kotlin.test.Test -import kotlin.test.assertEquals - -internal class CoderWorkspacesStepViewTest { - @Test - fun getsNewSelection() { - val table = WorkspacesTable() - table.listTableModel.items = - listOf( - // An off workspace. - DataGen.agentList("ws1"), - // On workspaces. - DataGen.agentList("ws2", "agent1"), - DataGen.agentList("ws2", "agent2"), - DataGen.agentList("ws3", "agent3"), - // Another off workspace. - DataGen.agentList("ws4"), - // In practice we do not list both agents and workspaces - // together but here test that anyway with an agent first and - // then with a workspace first. - DataGen.agentList("ws5", "agent2"), - DataGen.agentList("ws5"), - DataGen.agentList("ws6"), - DataGen.agentList("ws6", "agent3"), - ).flatten() - - val tests = - listOf( - Pair(null, -1), // No selection. - Pair(DataGen.agentList("gone", "gone"), -1), // No workspace that matches. - Pair(DataGen.agentList("ws1"), 0), // Workspace exact match. - Pair(DataGen.agentList("ws1", "gone"), 0), // Agent gone, select workspace. - Pair(DataGen.agentList("ws2"), 1), // Workspace gone, select first agent. - Pair(DataGen.agentList("ws2", "agent1"), 1), // Agent exact match. - Pair(DataGen.agentList("ws2", "agent2"), 2), // Agent exact match. - Pair(DataGen.agentList("ws3"), 3), // Workspace gone, select first agent. - Pair(DataGen.agentList("ws3", "agent3"), 3), // Agent exact match. - Pair(DataGen.agentList("ws4", "gone"), 4), // Agent gone, select workspace. - Pair(DataGen.agentList("ws4"), 4), // Workspace exact match. - Pair(DataGen.agentList("ws5", "agent2"), 5), // Agent exact match. - Pair(DataGen.agentList("ws5", "gone"), 5), // Agent gone, another agent comes first. - Pair(DataGen.agentList("ws5"), 6), // Workspace exact match. - Pair(DataGen.agentList("ws6"), 7), // Workspace exact match. - Pair(DataGen.agentList("ws6", "gone"), 7), // Agent gone, workspace comes first. - Pair(DataGen.agentList("ws6", "agent3"), 8), // Agent exact match. - ) - - tests.forEach { - assertEquals(it.second, table.getNewSelection(it.first?.first())) - } - } -}