Files
ComfyUI_frontend/tests-ui/tests/composables/useExternalLink.test.ts
Benjamin Lu 41eb45754b Fix desktop menu docs links regression (#7181)
## Summary
- make `useExternalLink` rely on the global i18n locale so it can be
used safely outside setup
- restore `electronAdapter` to use the shared `useExternalLink` helper
for docs URLs and static links

## Motivation
Desktop menu items disappeared because a top-level call to
`useExternalLink` in `electronAdapter` triggered `useI18n` at
module-eval time, throwing and blocking extension registration. By
making the composable global-locale-only and using it in
`electronAdapter`, the module can load without setup context while
preserving link behavior.

## Testing
- pnpm typecheck
- pnpm lint:fix

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-7181-Fix-desktop-menu-docs-links-regression-2c06d73d36508157ae48cff078b9173e)
by [Unito](https://www.unito.io)
2025-12-08 22:20:07 -07:00

147 lines
4.7 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
// Mock the environment utilities
vi.mock('@/utils/envUtil', () => ({
isElectron: vi.fn(),
electronAPI: vi.fn()
}))
// Provide a minimal i18n instance for the composable
const i18n = vi.hoisted(() => ({
global: {
locale: {
value: 'en'
}
}
}))
vi.mock('@/i18n', () => ({
i18n
}))
// Import after mocking to get the mocked versions
import { useExternalLink } from '@/composables/useExternalLink'
import { electronAPI, isElectron } from '@/utils/envUtil'
describe('useExternalLink', () => {
beforeEach(() => {
vi.clearAllMocks()
// Reset to default state
i18n.global.locale.value = 'en'
vi.mocked(isElectron).mockReturnValue(false)
})
describe('staticUrls', () => {
it('should provide common external URLs', () => {
const { staticUrls } = useExternalLink()
// Static URLs
expect(staticUrls.discord).toBe('https://www.comfy.org/discord')
expect(staticUrls.github).toBe(
'https://github.com/comfyanonymous/ComfyUI'
)
expect(staticUrls.githubIssues).toBe(
'https://github.com/comfyanonymous/ComfyUI/issues'
)
expect(staticUrls.githubFrontend).toBe(
'https://github.com/Comfy-Org/ComfyUI_frontend'
)
expect(staticUrls.githubElectron).toBe(
'https://github.com/Comfy-Org/electron'
)
expect(staticUrls.forum).toBe('https://forum.comfy.org/')
expect(staticUrls.comfyOrg).toBe('https://www.comfy.org/')
})
})
describe('buildDocsUrl', () => {
it('should build basic docs URL without locale', () => {
i18n.global.locale.value = 'en'
const { buildDocsUrl } = useExternalLink()
const url = buildDocsUrl('/changelog')
expect(url).toBe('https://docs.comfy.org/changelog')
})
it('should build docs URL with Chinese (zh) locale when requested', () => {
i18n.global.locale.value = 'zh'
const { buildDocsUrl } = useExternalLink()
const url = buildDocsUrl('/changelog', { includeLocale: true })
expect(url).toBe('https://docs.comfy.org/zh-CN/changelog')
})
it('should build docs URL with Chinese (zh-TW) locale when requested', () => {
i18n.global.locale.value = 'zh-TW'
const { buildDocsUrl } = useExternalLink()
const url = buildDocsUrl('/changelog', { includeLocale: true })
expect(url).toBe('https://docs.comfy.org/zh-CN/changelog')
})
it('should not include locale for English when requested', () => {
i18n.global.locale.value = 'en'
const { buildDocsUrl } = useExternalLink()
const url = buildDocsUrl('/changelog', { includeLocale: true })
expect(url).toBe('https://docs.comfy.org/changelog')
})
it('should handle path without leading slash', () => {
const { buildDocsUrl } = useExternalLink()
const url = buildDocsUrl('changelog')
expect(url).toBe('https://docs.comfy.org/changelog')
})
it('should add platform suffix when requested', () => {
i18n.global.locale.value = 'en'
vi.mocked(isElectron).mockReturnValue(true)
vi.mocked(electronAPI).mockReturnValue({
getPlatform: () => 'darwin'
} as ReturnType<typeof electronAPI>)
const { buildDocsUrl } = useExternalLink()
const url = buildDocsUrl('/installation/desktop', { platform: true })
expect(url).toBe('https://docs.comfy.org/installation/desktop/macos')
})
it('should add platform suffix with trailing slash', () => {
i18n.global.locale.value = 'en'
vi.mocked(isElectron).mockReturnValue(true)
vi.mocked(electronAPI).mockReturnValue({
getPlatform: () => 'win32'
} as ReturnType<typeof electronAPI>)
const { buildDocsUrl } = useExternalLink()
const url = buildDocsUrl('/installation/desktop/', { platform: true })
expect(url).toBe('https://docs.comfy.org/installation/desktop/windows')
})
it('should combine locale and platform', () => {
i18n.global.locale.value = 'zh'
vi.mocked(isElectron).mockReturnValue(true)
vi.mocked(electronAPI).mockReturnValue({
getPlatform: () => 'darwin'
} as ReturnType<typeof electronAPI>)
const { buildDocsUrl } = useExternalLink()
const url = buildDocsUrl('/installation/desktop', {
includeLocale: true,
platform: true
})
expect(url).toBe(
'https://docs.comfy.org/zh-CN/installation/desktop/macos'
)
})
it('should not add platform when not desktop', () => {
i18n.global.locale.value = 'en'
vi.mocked(isElectron).mockReturnValue(false)
const { buildDocsUrl } = useExternalLink()
const url = buildDocsUrl('/installation/desktop', { platform: true })
expect(url).toBe('https://docs.comfy.org/installation/desktop')
})
})
})