From ce630daf4f57600033e9f3996027ddb7cf52cfc0 Mon Sep 17 00:00:00 2001 From: PabloWiedemann Date: Mon, 16 Nov 2026 11:45:48 -0800 Subject: [PATCH 1/4] feat(sidebar): workspace switcher with Platform's menu, links and avatar colors The sidebar workspace switcher now has the Developer Platform switcher's items, behavior, keyboard handling, links and avatar colors, built with Desktop's design system: a header with the avatar, name and plan and role, a workspace list beside the menu, Workspace settings, Manage plan & credits, the email and Log Out. Ported onto main from Comfy-Org/Comfy-Desktop#2026. The old Apps view needed the menu to tell it when to freeze the app page; main's app page host already hides itself when anything covers it, so that part is gone. --- e2e/workspace-switch-no-browser.test.ts | 16 +- locales/en.json | 20 +- locales/zh.json | 20 +- src/main/cloud/types.ts | 2 +- src/main/cloud/workspaces.test.ts | 26 ++ src/main/cloud/workspaces.ts | 8 +- src/main/devplatform/config.ts | 8 + .../ipc/registerDevPlatformHandlers.test.ts | 26 ++ .../lib/ipc/registerDevPlatformHandlers.ts | 21 +- src/preload/api.ts | 2 + .../src/components/WorkspaceAvatar.test.ts | 32 ++ .../src/components/WorkspaceAvatar.vue | 41 ++ src/renderer/src/panel/SidebarAccount.test.ts | 334 ++++++++++++++- src/renderer/src/panel/SidebarAccount.vue | 63 +-- .../src/panel/SidebarWorkspaceSwitcher.vue | 381 ++++++++++++++++++ src/renderer/src/preview/fakeBridge.ts | 1 + .../views/devplatform/DevPlatformAvatar.vue | 43 -- .../DevPlatformWorkspaceSelector.test.ts | 261 ------------ .../DevPlatformWorkspaceSelector.vue | 182 --------- src/types/ipc.ts | 3 + 20 files changed, 909 insertions(+), 581 deletions(-) create mode 100644 src/renderer/src/components/WorkspaceAvatar.test.ts create mode 100644 src/renderer/src/components/WorkspaceAvatar.vue create mode 100644 src/renderer/src/panel/SidebarWorkspaceSwitcher.vue delete mode 100644 src/renderer/src/views/devplatform/DevPlatformAvatar.vue delete mode 100644 src/renderer/src/views/devplatform/DevPlatformWorkspaceSelector.test.ts delete mode 100644 src/renderer/src/views/devplatform/DevPlatformWorkspaceSelector.vue diff --git a/e2e/workspace-switch-no-browser.test.ts b/e2e/workspace-switch-no-browser.test.ts index b7924b30b..788b74db9 100644 --- a/e2e/workspace-switch-no-browser.test.ts +++ b/e2e/workspace-switch-no-browser.test.ts @@ -11,7 +11,8 @@ import { captureHostWindow } from './support/windowCapture' const UNAVAILABLE_COPY = en.devPlatform.workspace.switchUnavailable const RETRYABLE_COPY = en.devPlatform.workspace.switchRetryable const FAILURE_CAPTION = '[data-testid="chooser-workspace-switch-failure"]' -const SELECTOR = '[data-testid="devplatform-workspace-selector"]' +const SELECTOR = '[data-testid="workspace-switcher-trigger"]' +const SWITCH = '[data-testid="workspace-switch-action"]' const REFRESH = '[data-testid="chooser-workspace-refresh"]' const SIGN_IN = '[data-testid="devplatform-account-signin"]' const SETTLE_MS = 1_500 @@ -62,7 +63,13 @@ async function expectActive(workspaceId: string): Promise { async function expectSelected(workspaceId: string): Promise { await expect - .poll(() => panel().textOf(`${SELECTOR} .workspace-selector__name`), { timeout: 15_000 }) + .poll( + () => + panel().evaluate( + `document.querySelector('${SELECTOR}')?.getAttribute('aria-label') ?? null` + ), + { timeout: 15_000 } + ) .toBe(`Workspace ${workspaceId}`) } @@ -71,9 +78,10 @@ async function expectBuildsFrom(workspaceId: string): Promise { } async function select(workspaceId: string): Promise { - const item = `[data-testid="devplatform-workspace-${workspaceId}"]` + const item = `[data-testid="workspace-option-${workspaceId}"]` await panel().waitForVisible(SELECTOR) - await panel().clickUntilVisible(SELECTOR, item) + await panel().clickUntilVisible(SELECTOR, SWITCH) + await panel().clickUntilVisible(SWITCH, item) expect(await panel().click(item), `selected ${workspaceId}`).toBe(true) } diff --git a/locales/en.json b/locales/en.json index 33d8b9069..a7ca13956 100644 --- a/locales/en.json +++ b/locales/en.json @@ -1547,16 +1547,12 @@ "cta": "Log in" }, "account": { - "signOut": "Sign out", - "signedInAs": "Signed in as {email}", "installationInProgressTitle": "Installation in progress", "installationInProgressMessage": "Signing out will cancel the installation. Do you want to cancel the installation and sign out?", "cancelInstallationAndSignOut": "Cancel Installation & Sign Out" }, "workspace": { - "personalLabel": "Personal", "switchLabel": "Workspace", - "currentFallback": "Current workspace", "switchUnavailable": "This workspace is unavailable.", "switchRetryable": "Couldn't switch workspace. Check your connection and try again.", "instanceCountLabel": "INSTANCES", @@ -1569,6 +1565,22 @@ "promoteFailedTitle": "Could not upload build", "promoteFailedMessage": "Could not create a draft in Comfy Developer Platform." }, + "workspaceMenu": { + "switchWorkspace": "Switch workspace", + "workspaceSettings": "Workspace settings", + "managePlanAndCredits": "Manage plan & credits", + "logOut": "Log Out", + "account": "Account", + "search": "Search workspaces", + "clearSearch": "Clear search", + "loadFailed": "Couldn't load your workspaces. Please try again later.", + "empty": "You don't have access to any workspaces yet.", + "noMatch": "No workspaces match your search.", + "role": { + "owner": "Owner", + "member": "Member" + } + }, "build": { "version": "Build v{version}", "loadError": "Couldn't load builds. Retry" diff --git a/locales/zh.json b/locales/zh.json index ebe69c26f..240ebe03f 100644 --- a/locales/zh.json +++ b/locales/zh.json @@ -1547,16 +1547,12 @@ "cta": "登录" }, "account": { - "signOut": "退出登录", - "signedInAs": "已登录为 {email}", "installationInProgressTitle": "安装正在进行中", "installationInProgressMessage": "退出登录将取消安装。是否取消安装并退出登录?", "cancelInstallationAndSignOut": "取消安装并退出登录" }, "workspace": { - "personalLabel": "个人", "switchLabel": "工作区", - "currentFallback": "当前工作区", "switchUnavailable": "此工作区不可用。", "switchRetryable": "无法切换工作区。请检查网络连接后重试。", "instanceCountLabel": "实例", @@ -1569,6 +1565,22 @@ "promoteFailedTitle": "无法上传构建", "promoteFailedMessage": "无法在 Comfy Developer Platform 中创建草稿。" }, + "workspaceMenu": { + "switchWorkspace": "切换工作区", + "workspaceSettings": "工作区设置", + "managePlanAndCredits": "管理套餐和积分", + "logOut": "退出登录", + "account": "账户", + "search": "搜索工作区", + "clearSearch": "清除搜索", + "loadFailed": "无法加载你的工作区。请稍后再试。", + "empty": "你还没有可访问的工作区。", + "noMatch": "没有匹配搜索的工作区。", + "role": { + "owner": "所有者", + "member": "成员" + } + }, "build": { "version": "构建 v{version}", "loadError": "无法加载构建,请重试" diff --git a/src/main/cloud/types.ts b/src/main/cloud/types.ts index ea8e97ec4..206751367 100644 --- a/src/main/cloud/types.ts +++ b/src/main/cloud/types.ts @@ -31,7 +31,7 @@ export interface Workspace { name: string type: string role: string - subscriptionTier?: string + subscriptionTier?: string | null createdAt?: string joinedAt?: string } diff --git a/src/main/cloud/workspaces.test.ts b/src/main/cloud/workspaces.test.ts index 7081f6b2d..bc232cd42 100644 --- a/src/main/cloud/workspaces.test.ts +++ b/src/main/cloud/workspaces.test.ts @@ -43,6 +43,32 @@ describe('listWorkspaces', () => { ]) }) + it('keeps a null plan apart from a missing one, so the avatar can tell Free from unknown', async () => { + stub(200, { + workspaces: [ + { id: 'w-1', name: 'Personal', type: 'personal', role: 'owner', subscription_tier: null }, + { id: 'w-2', name: 'Team', type: 'team', role: 'member' } + ] + }) + const ws = await listWorkspaces('tok', { apiBase: 'https://cloud/api' }) + expect(ws).toEqual([ + { id: 'w-1', name: 'Personal', type: 'personal', role: 'owner', subscriptionTier: null }, + { id: 'w-2', name: 'Team', type: 'team', role: 'member' } + ]) + }) + + it('drops a plan that is not text, so it reads as unknown', async () => { + stub(200, { + workspaces: [ + { id: 'w-1', name: 'One', type: 'team', role: 'owner', subscription_tier: 7 }, + { id: 'w-2', name: 'Two', type: 'team', role: 'owner', subscription_tier: { tier: 'PRO' } }, + { id: 'w-3', name: 'Three', type: 'team', role: 'owner', subscription_tier: ['PRO'] } + ] + }) + const ws = await listWorkspaces('tok', { apiBase: 'https://cloud/api' }) + expect(ws.map((workspace) => 'subscriptionTier' in workspace)).toEqual([false, false, false]) + }) + it.each([ { status: 404, body: {}, error: /HTTP 404/ }, { status: 401, body: {}, error: /authorized/i }, diff --git a/src/main/cloud/workspaces.ts b/src/main/cloud/workspaces.ts index 0b7e43acc..931ad5b21 100644 --- a/src/main/cloud/workspaces.ts +++ b/src/main/cloud/workspaces.ts @@ -8,7 +8,7 @@ interface WorkspaceRow { name: string type: string role: string - subscription_tier?: string + subscription_tier?: unknown created_at?: string joined_at?: string } @@ -32,6 +32,10 @@ interface WorkspaceMembersResponse { pagination?: { has_more?: boolean } } +function isPlanTier(value: unknown): value is string | null { + return typeof value === 'string' || value === null +} + function isWorkspaceRow(value: unknown): value is WorkspaceRow { if (!value || typeof value !== 'object') return false const row = value as Partial @@ -66,7 +70,7 @@ export async function listWorkspaces( name: w.name, type: w.type, role: w.role, - ...(w.subscription_tier ? { subscriptionTier: w.subscription_tier } : {}), + ...(isPlanTier(w.subscription_tier) ? { subscriptionTier: w.subscription_tier } : {}), ...(w.created_at ? { createdAt: w.created_at } : {}), ...(w.joined_at ? { joinedAt: w.joined_at } : {}) })) diff --git a/src/main/devplatform/config.ts b/src/main/devplatform/config.ts index 55736103d..cf48eb180 100644 --- a/src/main/devplatform/config.ts +++ b/src/main/devplatform/config.ts @@ -5,3 +5,11 @@ export const BUILDER_BASE_URL = process.env.COMFY_BUILDER_BASE_URL || PROD_BUILD export const PLATFORM_WEB_BASE_URL = process.env.COMFY_PLATFORM_WEB_BASE_URL || PROD_PLATFORM_WEB_BASE_URL + +export const PLATFORM_SETTINGS_PATHS = { + general: '/settings/general', + people: '/settings/people', + billing: '/settings/billing' +} as const + +export type PlatformSettingsPage = keyof typeof PLATFORM_SETTINGS_PATHS diff --git a/src/main/lib/ipc/registerDevPlatformHandlers.test.ts b/src/main/lib/ipc/registerDevPlatformHandlers.test.ts index 5fb65977f..f57b8048b 100644 --- a/src/main/lib/ipc/registerDevPlatformHandlers.test.ts +++ b/src/main/lib/ipc/registerDevPlatformHandlers.test.ts @@ -289,6 +289,32 @@ describe('registerDevPlatformHandlers', () => { ) }) + it.each([ + ['general', 'https://platform.comfy.org/settings/general?workspace=w1'], + ['people', 'https://platform.comfy.org/settings/people?workspace=w1'], + ['billing', 'https://platform.comfy.org/settings/billing?workspace=w1'] + ])('opens the Platform %s settings page for the workspace', async (page, url) => { + await handler('comfybuilder:openSettingsPage')({}, page, 'w1') + + expect(mocks.openExternal).toHaveBeenCalledExactlyOnceWith(url) + }) + + it('rejects a missing workspace instead of opening an unscoped settings page', async () => { + await expect(handler('comfybuilder:openSettingsPage')({}, 'billing', '')).rejects.toThrow( + 'A workspace is required to open Developer Platform settings.' + ) + + expect(mocks.openExternal).not.toHaveBeenCalled() + }) + + it('refuses a settings page Platform does not have', async () => { + await expect(handler('comfybuilder:openSettingsPage')({}, 'constructor', 'w1')).rejects.toThrow( + 'Unknown Developer Platform settings page.' + ) + + expect(mocks.openExternal).not.toHaveBeenCalled() + }) + it('rejects a missing workspace instead of opening an unscoped Builds page', async () => { await expect(handler('comfybuilder:openBuildsPage')({}, '')).rejects.toThrow( 'A workspace is required to open Builds.' diff --git a/src/main/lib/ipc/registerDevPlatformHandlers.ts b/src/main/lib/ipc/registerDevPlatformHandlers.ts index b97e5d634..7ba1cf959 100644 --- a/src/main/lib/ipc/registerDevPlatformHandlers.ts +++ b/src/main/lib/ipc/registerDevPlatformHandlers.ts @@ -7,7 +7,7 @@ import { getViewWorkspaceGrants } from '../viewWorkspaceGrants' import { openExternalUrl } from '../openExternal' import { openSystemModalAsync } from '../../popups/systemModal' import { normalizeSha256 } from '../../comfybuilder/integrity' -import { PLATFORM_WEB_BASE_URL } from '../../devplatform/config' +import { PLATFORM_SETTINGS_PATHS, PLATFORM_WEB_BASE_URL } from '../../devplatform/config' import { getBuilderClient, getCloudSession, @@ -59,6 +59,7 @@ export const DEVPLATFORM_CHANNELS = { switchWorkspace: 'comfybuilder:switchWorkspace', listBuilds: 'comfybuilder:listBuilds', openBuildsPage: 'comfybuilder:openBuildsPage', + openSettingsPage: 'comfybuilder:openSettingsPage', installBuild: 'comfybuilder:installBuild', promoteLocalInstance: 'comfybuilder:promoteLocalInstance' } as const @@ -228,6 +229,24 @@ export function registerDevPlatformHandlers(): void { } ) + ipcMain.handle( + DEVPLATFORM_CHANNELS.openSettingsPage, + async (_event, page: unknown, workspaceId: unknown): Promise => { + if (typeof page !== 'string' || !Object.hasOwn(PLATFORM_SETTINGS_PATHS, page)) { + throw new Error('Unknown Developer Platform settings page.') + } + const url = new URL( + PLATFORM_SETTINGS_PATHS[page as keyof typeof PLATFORM_SETTINGS_PATHS], + PLATFORM_WEB_BASE_URL + ) + if (typeof workspaceId !== 'string' || workspaceId.trim().length === 0) { + throw new Error('A workspace is required to open Developer Platform settings.') + } + url.searchParams.set('workspace', workspaceId) + await openExternalUrl(url.toString()) + } + ) + ipcMain.handle( DEVPLATFORM_CHANNELS.switchWorkspace, async (_event, workspaceId: string, intent?: WorkspaceSwitchIntent) => { diff --git a/src/preload/api.ts b/src/preload/api.ts index 7e726b16a..f2cf7a207 100644 --- a/src/preload/api.ts +++ b/src/preload/api.ts @@ -268,6 +268,8 @@ export function buildElectronApi(): ElectronApi { listBuilds: () => ipcRenderer.invoke('comfybuilder:listBuilds'), openBuildsPage: (workspaceId) => ipcRenderer.invoke('comfybuilder:openBuildsPage', workspaceId), + openSettingsPage: (page, workspaceId) => + ipcRenderer.invoke('comfybuilder:openSettingsPage', page, workspaceId), installBuild: (request) => ipcRenderer.invoke('comfybuilder:installBuild', request), promoteLocalInstance: (installationId) => ipcRenderer.invoke('comfybuilder:promoteLocalInstance', installationId) diff --git a/src/renderer/src/components/WorkspaceAvatar.test.ts b/src/renderer/src/components/WorkspaceAvatar.test.ts new file mode 100644 index 000000000..f11e191a0 --- /dev/null +++ b/src/renderer/src/components/WorkspaceAvatar.test.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from 'vitest' +import { mount } from '@vue/test-utils' + +import WorkspaceAvatar from './WorkspaceAvatar.vue' + +function initialOf(workspaceName: string): string { + return mount(WorkspaceAvatar, { props: { workspaceName, subscriptionTier: null } }).text() +} + +describe('WorkspaceAvatar', () => { + it('shows the first letter of the name in upper case', () => { + expect(initialOf('harbor')).toBe('H') + }) + + it('skips leading spaces', () => { + expect(initialOf(' harbor')).toBe('H') + }) + + it('keeps a whole emoji or accented letter', () => { + expect(initialOf('🇩🇪 Berlin')).toBe('🇩🇪') + expect(initialOf('👩‍💻 Studio')).toBe('👩‍💻') + expect(initialOf('école')).toBe('É') + }) + + it('keeps a letter whose upper case is two letters', () => { + expect(initialOf('ßtudio')).toBe('ß') + }) + + it('shows a question mark for a blank name', () => { + expect(initialOf(' ')).toBe('?') + }) +}) diff --git a/src/renderer/src/components/WorkspaceAvatar.vue b/src/renderer/src/components/WorkspaceAvatar.vue new file mode 100644 index 000000000..775195c86 --- /dev/null +++ b/src/renderer/src/components/WorkspaceAvatar.vue @@ -0,0 +1,41 @@ + + + diff --git a/src/renderer/src/panel/SidebarAccount.test.ts b/src/renderer/src/panel/SidebarAccount.test.ts index 5dccdc199..b67c2c613 100644 --- a/src/renderer/src/panel/SidebarAccount.test.ts +++ b/src/renderer/src/panel/SidebarAccount.test.ts @@ -1,10 +1,13 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' +import { userEvent } from 'vitest/browser' +import { nextTick } from 'vue' import { DOMWrapper, flushPromises, mount } from '@vue/test-utils' import { createPinia, setActivePinia } from 'pinia' +import { workspaceAvatarStyle } from '@comfyorg/design-system/workspaceAvatar' import SidebarAccount from './SidebarAccount.vue' import { useDashboardScopeStore } from '../stores/dashboardScopeStore' -import type { AuthStatus } from '../../../types/ipc' +import type { AuthStatus, Workspace } from '../../../types/ipc' const SIGNED_OUT: AuthStatus = { signedIn: false } const SIGNED_IN: AuthStatus = { @@ -15,29 +18,51 @@ const SIGNED_IN: AuthStatus = { workspaceType: 'team' } -function installApi(status: AuthStatus) { +const WORKSPACES: Workspace[] = [ + { + id: 'p1', + name: 'Personal Workspace', + type: 'personal', + role: 'owner', + subscriptionTier: null + }, + { id: 'w1', name: 'Silverside', type: 'team', role: 'owner', subscriptionTier: 'CREATOR' }, + { id: 'w2', name: 'Harbor', type: 'team', role: 'member', subscriptionTier: 'ENTERPRISE' }, + { id: 'legacy', name: 'Legacy', type: 'team', role: 'member' } +] + +function toComputedColor(color: string | undefined): string { + const probe = document.createElement('span') + probe.style.backgroundColor = color ?? '' + return probe.style.backgroundColor +} + +interface ApiOptions { + listWorkspaces?: () => Promise + savedWorkspaceId?: string +} + +function installApi(status: AuthStatus, options: ApiOptions = {}) { const comfybuilder = { getAuthStatus: vi.fn().mockResolvedValue(status), - onAuthChanged: vi.fn(() => () => {}), + onAuthChanged: vi.fn((_callback: (status: AuthStatus) => void) => () => {}), signIn: vi.fn().mockResolvedValue({ signedIn: true }), signOut: vi.fn().mockResolvedValue({ signedIn: false }), - listWorkspaces: vi.fn().mockResolvedValue([ - { id: 'w1', name: 'Silverside', type: 'team', role: 'owner' }, - { id: 'w2', name: 'Harbor', type: 'team', role: 'member' } - ]), - switchWorkspace: vi.fn() + listWorkspaces: vi.fn(options.listWorkspaces ?? (() => Promise.resolve(WORKSPACES))), + switchWorkspace: vi.fn(), + openSettingsPage: vi.fn().mockResolvedValue(undefined) } ;(window as unknown as { api: unknown }).api = { comfybuilder, - getSetting: vi.fn().mockResolvedValue(undefined), + getSetting: vi.fn().mockResolvedValue(options.savedWorkspaceId), setSetting: vi.fn().mockResolvedValue(undefined), onSettingsChanged: vi.fn(() => () => {}) } return comfybuilder } -async function mountAccount(status: AuthStatus, collapsed = false) { - const comfybuilder = installApi(status) +async function mountAccount(status: AuthStatus, collapsed = false, options: ApiOptions = {}) { + const comfybuilder = installApi(status, options) setActivePinia(createPinia()) const wrapper = mount(SidebarAccount, { attachTo: document.body, @@ -48,11 +73,19 @@ async function mountAccount(status: AuthStatus, collapsed = false) { } async function openMenu(wrapper: Awaited>['wrapper']) { - await wrapper.get('[data-testid="devplatform-workspace-selector"]').trigger('click') + await wrapper.get('[data-testid="workspace-switcher-trigger"]').trigger('click') await flushPromises() return new DOMWrapper(document.body) } +async function openWorkspaces(wrapper: Awaited>['wrapper']) { + const body = await openMenu(wrapper) + await body.get('[data-testid="workspace-switch-action"]').trigger('click') + await flushPromises() + await nextTick() + return body +} + beforeEach(() => { document.body.innerHTML = '' }) @@ -87,32 +120,293 @@ describe('SidebarAccount', () => { ) }) - it('shows the workspace and who is signed in', async () => { + it('shows the current workspace on the trigger', async () => { const { wrapper } = await mountAccount(SIGNED_IN) - const trigger = wrapper.get('[data-testid="devplatform-workspace-selector"]') - expect(trigger.text()).toContain('Silverside') - expect(trigger.text()).toContain('someone@comfy.org') + const trigger = wrapper.get('[data-testid="workspace-switcher-trigger"]') + expect(trigger.text()).toBe('SSilverside') + expect(trigger.attributes('aria-label')).toBe('Silverside') }) - it('switches the dashboard workspace from the menu', async () => { + it('lists the Platform menu items for the current workspace', async () => { const { wrapper } = await mountAccount(SIGNED_IN) const body = await openMenu(wrapper) - await body.get('[data-testid="devplatform-workspace-w2"]').trigger('click') + + expect(body.get('[data-testid="workspace-switch-action"]').text()).toContain('Silverside') + expect( + body + .findAll('[data-testid="workspace-menu-meta"] > span:not([aria-hidden])') + .map((part) => part.text()) + ).toEqual(['Creator', 'Owner']) + expect(body.get('[data-testid="workspace-settings"]').text()).toBe('Workspace settings') + expect(body.get('[data-testid="workspace-manage-credits"]').text()).toBe( + 'Manage plan & credits' + ) + expect(body.get('[data-testid="workspace-menu-user"]').text()).toBe('someone@comfy.org') + expect(body.get('[data-testid="workspace-logout"]').text()).toBe('Log Out') + expect(body.text()).not.toContain('Add workspace') + }) + + it('lists every workspace with its plan and checks the current one', async () => { + const { wrapper } = await mountAccount(SIGNED_IN) + + const body = await openWorkspaces(wrapper) + + const options = body.findAll('[data-testid^="workspace-option-"]') + expect(options.map((option) => option.text())).toEqual([ + 'PPersonal Workspace', + 'SSilversideCreator', + 'HHarborEnterprise', + 'LLegacy' + ]) + expect(options.map((option) => option.attributes('aria-current'))).toEqual([ + undefined, + 'true', + undefined, + undefined + ]) + expect(document.activeElement).toBe(options[1]?.element) + }) + + it('colors each avatar with the shared workspace avatar helper', async () => { + const { wrapper } = await mountAccount(SIGNED_IN) + + const body = await openWorkspaces(wrapper) + + const avatar = (testId: string) => + body.get(`[data-testid="${testId}"] [data-testid="workspace-avatar"]`).element as HTMLElement + expect(avatar('workspace-option-w2').style.backgroundColor).toBe( + toComputedColor(workspaceAvatarStyle('Harbor', 'ENTERPRISE').backgroundColor) + ) + expect(avatar('workspace-option-w1').style.backgroundColor).toBe( + toComputedColor(workspaceAvatarStyle('Silverside', 'CREATOR').backgroundColor) + ) + expect(avatar('workspace-option-p1').style.backgroundColor).toBe( + toComputedColor(workspaceAvatarStyle('Personal Workspace', null).backgroundColor) + ) + expect(avatar('workspace-option-legacy').style.backgroundColor).toBe('') + }) + + it('switches the dashboard workspace from the list', async () => { + const { wrapper } = await mountAccount(SIGNED_IN) + + const body = await openWorkspaces(wrapper) + await body.get('[data-testid="workspace-option-w2"]').trigger('click') await flushPromises() expect(useDashboardScopeStore().selectedWorkspaceId).toBe('w2') + expect(body.find('[data-testid="workspace-menu"]').exists()).toBe(false) }) - it('signs out from the menu', async () => { + it('closes the menu when the current workspace is picked again', async () => { + const { wrapper } = await mountAccount(SIGNED_IN) + + const body = await openWorkspaces(wrapper) + await body.get('[data-testid="workspace-option-w1"]').trigger('click') + await flushPromises() + + expect(useDashboardScopeStore().selectedWorkspaceId).toBe('w1') + expect(body.find('[data-testid="workspace-menu"]').exists()).toBe(false) + }) + + it('maps the personal workspace row to the personal dashboard scope', async () => { + const { wrapper } = await mountAccount(SIGNED_IN) + + const body = await openWorkspaces(wrapper) + await body.get('[data-testid="workspace-option-p1"]').trigger('click') + await flushPromises() + + expect(useDashboardScopeStore().selectedWorkspaceId).toBe('personal') + }) + + it('closes only the workspace list on Escape and returns focus to its header', async () => { + const { wrapper } = await mountAccount(SIGNED_IN) + + const body = await openWorkspaces(wrapper) + await userEvent.keyboard('{Escape}') + await flushPromises() + + expect(body.find('[data-testid="workspace-switcher-panel"]').exists()).toBe(false) + const header = body.get('[data-testid="workspace-switch-action"]') + expect(document.activeElement).toBe(header.element) + + await userEvent.keyboard('{Escape}') + await flushPromises() + + expect(body.find('[data-testid="workspace-menu"]').exists()).toBe(false) + }) + + it('opens the team settings page Platform links to, in the same workspace', async () => { const { wrapper, comfybuilder } = await mountAccount(SIGNED_IN) const body = await openMenu(wrapper) - await body.get('[data-testid="devplatform-account-signout"]').trigger('click') + await body.get('[data-testid="workspace-settings"]').trigger('click') + + expect(comfybuilder.openSettingsPage).toHaveBeenCalledExactlyOnceWith('general', 'w1') + }) + + it('opens People as the settings page of a personal workspace', async () => { + const { wrapper, comfybuilder } = await mountAccount(SIGNED_IN) + useDashboardScopeStore().selectWorkspace('personal') + await flushPromises() + + const body = await openMenu(wrapper) + await body.get('[data-testid="workspace-settings"]').trigger('click') + + expect(comfybuilder.openSettingsPage).toHaveBeenCalledExactlyOnceWith('people', 'p1') + }) + + it('opens Billing from Manage plan & credits', async () => { + const { wrapper, comfybuilder } = await mountAccount(SIGNED_IN) + + const body = await openMenu(wrapper) + await body.get('[data-testid="workspace-manage-credits"]').trigger('click') + await flushPromises() + + expect(comfybuilder.openSettingsPage).toHaveBeenCalledExactlyOnceWith('billing', 'w1') + expect(body.find('[data-testid="workspace-menu"]').exists()).toBe(false) + }) + + it('logs out from the menu', async () => { + const { wrapper, comfybuilder } = await mountAccount(SIGNED_IN) + + const body = await openMenu(wrapper) + await body.get('[data-testid="workspace-logout"]').trigger('click') await flushPromises() expect(comfybuilder.signOut).toHaveBeenCalledOnce() expect(wrapper.find('[data-testid="devplatform-account-signin"]').exists()).toBe(true) }) + + it('shows a workspace with no plan field on a neutral tile, as its list row does', async () => { + const { wrapper } = await mountAccount(SIGNED_IN) + useDashboardScopeStore().selectWorkspace('legacy') + await flushPromises() + + const avatar = wrapper.get( + '[data-testid="workspace-switcher-trigger"] [data-testid="workspace-avatar"]' + ).element as HTMLElement + expect(avatar.style.backgroundColor).toBe('') + }) + + it('shrinks to the workspace tile on the collapsed sidebar and names it in a tooltip', async () => { + const { wrapper } = await mountAccount(SIGNED_IN, true) + + const trigger = wrapper.get('[data-testid="workspace-switcher-trigger"]') + expect(trigger.text()).toBe('S') + expect(trigger.attributes('title')).toBe('Silverside') + }) + + it('closes its menu when the window loses focus', async () => { + const { wrapper } = await mountAccount(SIGNED_IN) + + await openMenu(wrapper) + window.dispatchEvent(new Event('blur')) + await flushPromises() + + expect(document.body.querySelector('[data-testid="workspace-menu"]')).toBeNull() + }) + + it('keeps the settings links off until it knows which workspace to open them in', async () => { + const { wrapper } = await mountAccount(SIGNED_IN, false, { + listWorkspaces: () => new Promise(() => {}), + savedWorkspaceId: 'w2' + }) + + const body = await openMenu(wrapper) + + expect(wrapper.get('[data-testid="workspace-switcher-trigger"]').text()).toBe('WWorkspace') + const settings = body.get('[data-testid="workspace-settings"]') + const credits = body.get('[data-testid="workspace-manage-credits"]') + expect(settings.attributes('disabled')).toBeDefined() + expect(credits.attributes('disabled')).toBeDefined() + }) + + it('takes the open menu away when the person signs out', async () => { + const { wrapper, comfybuilder } = await mountAccount(SIGNED_IN) + await openMenu(wrapper) + + const authChanged = comfybuilder.onAuthChanged.mock.calls[0]![0] as (status: AuthStatus) => void + authChanged(SIGNED_OUT) + await flushPromises() + + expect(wrapper.find('[data-testid="workspace-switcher-trigger"]').exists()).toBe(false) + expect(document.body.querySelector('[data-testid="workspace-menu"]')).toBeNull() + }) + + it('shows two loading rows while the workspaces load', async () => { + const { wrapper } = await mountAccount(SIGNED_IN, false, { + listWorkspaces: () => new Promise(() => {}) + }) + + const body = await openWorkspaces(wrapper) + + expect(body.findAll('[data-testid="workspace-skeleton"]')).toHaveLength(2) + expect(body.get('[data-testid="workspace-switcher-panel"]').attributes('aria-busy')).toBe( + 'true' + ) + }) + + it('says when the workspaces cannot load and tries again when the menu opens', async () => { + const { wrapper, comfybuilder } = await mountAccount(SIGNED_IN, false, { + listWorkspaces: () => Promise.reject(new Error('offline')) + }) + expect(comfybuilder.listWorkspaces).toHaveBeenCalledOnce() + + const body = await openWorkspaces(wrapper) + + expect(comfybuilder.listWorkspaces).toHaveBeenCalledTimes(2) + expect(body.get('[data-testid="workspace-load-error"]').text()).toBe( + "Couldn't load your workspaces. Please try again later." + ) + }) + + it('says so when there are no workspaces', async () => { + const { wrapper } = await mountAccount(SIGNED_IN, false, { + listWorkspaces: () => Promise.resolve([]) + }) + + const body = await openWorkspaces(wrapper) + + expect(body.get('[data-testid="workspace-switcher-empty"]').text()).toBe( + "You don't have access to any workspaces yet." + ) + }) + + it('offers search above six workspaces and forgets the query when the list closes', async () => { + const many: Workspace[] = Array.from({ length: 7 }, (_, index) => ({ + id: `t${index}`, + name: index === 0 ? 'Silverside' : `Team ${index}`, + type: 'team', + role: 'member' + })) + const { wrapper } = await mountAccount(SIGNED_IN, false, { + listWorkspaces: () => Promise.resolve(many) + }) + + let body = await openWorkspaces(wrapper) + const search = body.get('[data-testid="workspace-switcher-panel"] input') + await userEvent.fill(search.element, 'team 3') + await vi.waitFor(() => + expect(body.findAll('[data-testid^="workspace-option-"]')).toHaveLength(1) + ) + await userEvent.fill(search.element, 'nothing') + await vi.waitFor(() => + expect(body.get('[data-testid="workspace-switcher-empty"]').text()).toBe( + 'No workspaces match your search.' + ) + ) + + await userEvent.keyboard('{Escape}') + await flushPromises() + await body.get('[data-testid="workspace-switch-action"]').trigger('click') + await flushPromises() + body = new DOMWrapper(document.body) + + expect( + body.get('[data-testid="workspace-switcher-panel"] input').element.value + ).toBe('') + expect(body.findAll('[data-testid^="workspace-option-"]')).toHaveLength(7) + }) }) diff --git a/src/renderer/src/panel/SidebarAccount.vue b/src/renderer/src/panel/SidebarAccount.vue index 879e4e21c..7f89eb87f 100644 --- a/src/renderer/src/panel/SidebarAccount.vue +++ b/src/renderer/src/panel/SidebarAccount.vue @@ -1,12 +1,10 @@