Repository navigation
refactor: implement workspace selection logic and add tests for role-based workspace retrieval #8285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: implement workspace selection logic and add tests for role-based workspace retrieval #8285
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** 工作空间选择。 */ | ||
| import type { WorkspaceItem } from '@/api/types' | ||
| import { ROLE_TYPE } from '@/api/enums' | ||
|
|
||
| function hasWorkspaceManagerRole(workspace: WorkspaceItem) { | ||
| return workspace.role_info?.some(({ role_type }) => role_type === ROLE_TYPE.WORKSPACE_MANAGE) ?? false | ||
| } | ||
|
|
||
| /** 优先使用当前用户具有空间管理员角色的缓存工作空间,否则选择第一个可管理的工作空间。 */ | ||
| export function getInitialWorkspaceId(cachedWorkspaceId: string | null, workspaceList?: WorkspaceItem[]): string { | ||
| if (cachedWorkspaceId) { | ||
| const cachedWorkspace = workspaceList?.find(({ id }) => id === cachedWorkspaceId) | ||
| if (cachedWorkspace && hasWorkspaceManagerRole(cachedWorkspace)) { | ||
| return cachedWorkspaceId | ||
| } | ||
| } | ||
|
|
||
| return workspaceList?.find((workspace) => workspace.id && hasWorkspaceManagerRole(workspace))?.id ?? 'default' | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,12 +13,15 @@ import { ADMIN_STORAGE_KEY } from '@/constants/storage-key' | |
| import { useStore } from '@/stores' | ||
| import { MsgConfirm, MsgSuccess } from '@/utils/message' | ||
| import { getRoleName } from '@/utils/role-name' | ||
| import { getInitialWorkspaceId } from '@/utils/workspace-selection' | ||
| import perm from '@/permission/index.ts' | ||
| const { auth, user } = useStore() | ||
| const { t } = useI18n({ useScope: 'global' }) | ||
|
|
||
| /* 选择工作空间列表 */ | ||
| const selectedWorkspaceId = ref(localStorage.getItem(ADMIN_STORAGE_KEY.WORKSPACE_ID) || user.userInfo?.workspace_list?.[0]?.id || 'default') | ||
| const selectedWorkspaceId = ref( | ||
| getInitialWorkspaceId(localStorage.getItem(ADMIN_STORAGE_KEY.WORKSPACE_ID), user.userInfo?.workspace_list), | ||
| ) | ||
| const workspaceOptions = ref<WorkspaceItem[]>([]) | ||
| const loadingView = ref(false) | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change is reasonable—it centralizes workspace-ID initialization—but the main risk is the contract of Potential issues
Suggested helper shapeexport function getInitialWorkspaceId(
storedId: string | null,
workspaces?: WorkspaceItem[] | null,
): string {
const stored = storedId?.trim()
if (stored === 'default') return stored
// List not loaded yet: preserve stored ID instead of falling back.
if (workspaces == null) {
return stored || 'default'
}
if (stored && workspaces.some(w => String(w.id) === stored)) {
return stored
}
const firstId = workspaces[0]?.id
return firstId != null ? String(firstId) : 'default'
}If your workspace IDs are numbers and downstream APIs expect numbers, do not blindly convert everything to Minor suggestions
Overall, the diff is fine structurally; the critical review point is whether |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import assert from 'node:assert/strict' | ||
| import { readFileSync } from 'node:fs' | ||
| import { test } from 'node:test' | ||
| import { compileFunction } from 'node:vm' | ||
| import ts from 'typescript' | ||
|
|
||
| function loadWorkspaceSelection() { | ||
| const { outputText } = ts.transpileModule(readFileSync(new URL('../src/utils/workspace-selection.ts', import.meta.url), 'utf8'), { | ||
| compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 }, | ||
| }) | ||
| const exports = {} | ||
| compileFunction(outputText, ['require', 'exports'])((id) => { | ||
| assert.equal(id, '@/api/enums') | ||
| return { | ||
| ROLE_TYPE: { | ||
| ADMIN: 'ADMIN', | ||
| USER: 'USER', | ||
| WORKSPACE_MANAGE: 'WORKSPACE_MANAGE', | ||
| }, | ||
| } | ||
| }, exports) | ||
| return exports | ||
| } | ||
|
|
||
| const workspaceManagerRole = { | ||
| role_id: 'workspace-manager-role', | ||
| role_name: '空间管理员', | ||
| role_type: 'WORKSPACE_MANAGE', | ||
| } | ||
|
|
||
| const userRole = { | ||
| role_id: 'user-role', | ||
| role_name: '普通成员', | ||
| role_type: 'USER', | ||
| } | ||
|
|
||
| test('缓存工作空间包含空间管理员角色时沿用缓存 ID', () => { | ||
| const { getInitialWorkspaceId } = loadWorkspaceSelection() | ||
| const workspaces = [ | ||
| { id: 'workspace-a', name: '空间 A', role_info: [userRole] }, | ||
| { id: 'workspace-b', name: '空间 B', role_info: [workspaceManagerRole] }, | ||
| ] | ||
|
|
||
| assert.equal(getInitialWorkspaceId('workspace-b', workspaces), 'workspace-b') | ||
| }) | ||
|
|
||
| test('缓存工作空间不是管理员空间时选择第一个管理员空间', () => { | ||
| const { getInitialWorkspaceId } = loadWorkspaceSelection() | ||
| const workspaces = [ | ||
| { id: 'workspace-a', name: '空间 A', role_info: [userRole] }, | ||
| { id: 'workspace-b', name: '空间 B', role_info: [workspaceManagerRole] }, | ||
| ] | ||
|
|
||
| assert.equal(getInitialWorkspaceId('workspace-a', workspaces), 'workspace-b') | ||
| }) | ||
|
|
||
| test('缓存工作空间不在列表中时选择第一个管理员空间', () => { | ||
| const { getInitialWorkspaceId } = loadWorkspaceSelection() | ||
| const workspaces = [ | ||
| { id: 'workspace-a', name: '空间 A', role_info: [userRole] }, | ||
| { id: 'workspace-b', name: '空间 B', role_info: [userRole, workspaceManagerRole] }, | ||
| ] | ||
|
|
||
| assert.equal(getInitialWorkspaceId('missing-workspace', workspaces), 'workspace-b') | ||
| }) | ||
|
|
||
| test('没有管理员空间时回退到 default', () => { | ||
| const { getInitialWorkspaceId } = loadWorkspaceSelection() | ||
| const workspaces = [{ id: 'workspace-a', name: '空间 A', role_info: [userRole] }] | ||
|
|
||
| assert.equal(getInitialWorkspaceId('workspace-a', workspaces), 'default') | ||
| assert.equal(getInitialWorkspaceId(null, undefined), 'default') | ||
| }) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's a review of the diff: Potential Issues
Optimization Suggestions
SummaryLogic looks reasonable and the scenarios are sensible. Main risks: the over-strict single-module mock, re-transpiling per test, the duplicated |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, the logic is sound and there are no obvious syntax/type errors. Potential issues and suggestions:
Magic fallback
'default'null/undefinedif the caller should handle “no workspace” explicitly.'default'is guaranteed to exist, document that assumption.workspaceListmay beundefinedwhile loading'default'even when the list has not loaded yet. This can cause an incorrect initial workspace request.Cached ID validation
if (cachedWorkspaceId)accepts whitespace-only strings. If the value comes from storage, consider trimming/validating it first.string | null | undefinedif callers may passundefined.Predicate readability
workspace.id && hasWorkspaceManagerRole(workspace)works, but the predicate returnsstring | boolean.Boolean(workspace.id) && hasWorkspaceManagerRole(workspace)orworkspace.id !== '' && ...for clarity.Role check semantics
hasWorkspaceManagerRoleonly checksrole_type. Confirm whether disabled/deleted workspaces or role status also need filtering.role_inforepresents the current user’s roles, not all users’ roles.Return type assumption
string, soWorkspaceItem.idmust be a non-optionalstring. Verify the type definition.Minor optimization
findcalls are O(n) each, but this is negligible for typical workspace lists. No change needed unless the list is very large.Optional refactor: