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..585e05044 100644 --- a/locales/en.json +++ b/locales/en.json @@ -1547,8 +1547,6 @@ "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" @@ -1556,7 +1554,6 @@ "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 +1566,26 @@ "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", + "plan": { + "FREE": "Free", + "STANDARD": "Standard", + "CREATOR": "Creator", + "PRO": "Pro", + "FOUNDERS_EDITION": "Founders", + "TEAM": "Team", + "ENTERPRISE": "Enterprise" + }, + "role": { + "owner": "Owner", + "admin": "Admin", + "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..be8b8d689 100644 --- a/locales/zh.json +++ b/locales/zh.json @@ -1547,8 +1547,6 @@ "cta": "登录" }, "account": { - "signOut": "退出登录", - "signedInAs": "已登录为 {email}", "installationInProgressTitle": "安装正在进行中", "installationInProgressMessage": "退出登录将取消安装。是否取消安装并退出登录?", "cancelInstallationAndSignOut": "取消安装并退出登录" @@ -1556,7 +1554,6 @@ "workspace": { "personalLabel": "个人", "switchLabel": "工作区", - "currentFallback": "当前工作区", "switchUnavailable": "此工作区不可用。", "switchRetryable": "无法切换工作区。请检查网络连接后重试。", "instanceCountLabel": "实例", @@ -1569,6 +1566,26 @@ "promoteFailedTitle": "无法上传构建", "promoteFailedMessage": "无法在 Comfy Developer Platform 中创建草稿。" }, + "workspaceMenu": { + "switchWorkspace": "切换工作区", + "workspaceSettings": "工作区设置", + "managePlanAndCredits": "管理套餐和积分", + "logOut": "退出登录", + "plan": { + "FREE": "免费版", + "STANDARD": "标准版", + "CREATOR": "创作者版", + "PRO": "专业版", + "FOUNDERS_EDITION": "创始版", + "TEAM": "团队版", + "ENTERPRISE": "企业版" + }, + "role": { + "owner": "所有者", + "admin": "管理员", + "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..e607363e1 100644 --- a/src/main/cloud/workspaces.test.ts +++ b/src/main/cloud/workspaces.test.ts @@ -43,6 +43,38 @@ 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 an empty or non-text plan, 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'] }, + { id: 'w-4', name: 'Four', type: 'team', role: 'owner', subscription_tier: '' } + ] + }) + const ws = await listWorkspaces('tok', { apiBase: 'https://cloud/api' }) + expect(ws.map((workspace) => 'subscriptionTier' in workspace)).toEqual([ + false, + 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..86fbb6295 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 value === null || (typeof value === 'string' && value !== '') +} + 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..9331962a6 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,321 @@ 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-checked'))).toEqual([ + 'false', + 'true', + 'false', + 'false' + ]) + }) + + it('shows a plan it has no name for as the plan code', async () => { + const { wrapper } = await mountAccount(SIGNED_IN, false, { + listWorkspaces: () => + Promise.resolve([ + { id: 'w1', name: 'Silverside', type: 'team', role: 'owner', subscriptionTier: 'GALAXY' } + ]) + }) + + const body = await openWorkspaces(wrapper) + + expect(body.get('[data-testid="workspace-option-w1"]').text()).toBe('SSilversideGALAXY') + }) + + 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('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('opens and closes the workspace list from the keyboard', async () => { + const { wrapper } = await mountAccount(SIGNED_IN) + const body = new DOMWrapper(document.body) + + ;(wrapper.get('[data-testid="workspace-switcher-trigger"]').element as HTMLElement).focus() + await userEvent.keyboard('{Enter}') + await flushPromises() + const header = body.get('[data-testid="workspace-switch-action"]') + expect(document.activeElement).toBe(header.element) + + await userEvent.keyboard('{ArrowRight}') + await vi.waitFor(() => + expect(body.find('[data-testid="workspace-switcher-panel"]').exists()).toBe(true) + ) + + await userEvent.keyboard('{ArrowLeft}') + await vi.waitFor(() => + expect(body.find('[data-testid="workspace-switcher-panel"]').exists()).toBe(false) + ) + 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="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 People for a legacy personal workspace the server reports as a team', async () => { + const { wrapper, comfybuilder } = await mountAccount(SIGNED_IN, false, { + listWorkspaces: () => + Promise.resolve([ + { id: 'p1', name: 'Personal Workspace', type: 'team', role: 'owner' }, + { id: 'w1', name: 'Silverside', type: 'team', role: 'owner' } + ]) + }) + 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('shows one Personal row when a team is also named Personal Workspace', async () => { + const { wrapper } = await mountAccount(SIGNED_IN, false, { + listWorkspaces: () => + Promise.resolve([ + { id: 'p1', name: 'Personal Workspace', type: 'personal', role: 'owner' }, + { id: 'w9', name: 'Personal Workspace', type: 'team', role: 'owner' }, + { id: 'w1', name: 'Silverside', type: 'team', role: 'owner' } + ]) + }) + + const body = await openWorkspaces(wrapper) + + expect( + body + .findAll('[data-testid^="workspace-option-"]') + .map((option) => option.attributes('data-testid')) + ).toEqual(['workspace-option-p1', 'workspace-option-w1']) + }) + + 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('signs out from the menu', async () => { + it('logs out from the menu', 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-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('aria-disabled')).toBe('true') + expect(credits.attributes('aria-disabled')).toBe('true') + }) + + 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('says it is loading while the workspaces load', async () => { + const { wrapper } = await mountAccount(SIGNED_IN, false, { + listWorkspaces: () => new Promise(() => {}) + }) + + const body = await openWorkspaces(wrapper) + + expect(body.get('[data-testid="workspace-switcher-panel"] [role="status"]').text()).toBe( + 'Loading…' + ) + }) + + 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 workspaces. Retry" + ) + + await body.get('[data-testid="workspace-load-error"]').trigger('click') + await flushPromises() + + expect(comfybuilder.listWorkspaces).toHaveBeenCalledTimes(3) + }) + + it('still offers Personal when the workspaces cannot load', async () => { + const { wrapper } = await mountAccount(SIGNED_IN, false, { + listWorkspaces: () => Promise.reject(new Error('offline')) + }) + + const body = await openWorkspaces(wrapper) + const personal = body.get('[data-testid="workspace-option-personal"]') + expect(personal.text()).toBe('PPersonal') + await personal.trigger('click') + await flushPromises() + + expect(useDashboardScopeStore().selectedWorkspaceId).toBe('personal') + }) }) 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 @@