refactor: replace runtime isElectron() with build-time isDesktop constant (#8710)

## Summary

Replace all runtime `isElectron()` function calls with the build-time
`isDesktop` constant from `@/platform/distribution/types`, enabling
dead-code elimination in non-desktop builds.

## Changes

- **What**: Migrate 30 files from runtime `isElectron()` detection
(checking `window.electronAPI`) to the compile-time `isDesktop` constant
(driven by `__DISTRIBUTION__` Vite define). Remove `isElectron` from
`envUtil.ts`. Update `isNativeWindow()` to use `isDesktop`. Guard
`electronAPI()` calls behind `isDesktop` checks in stores. Update 7 test
files to use `vi.hoisted` + getter mock pattern for per-test `isDesktop`
toggling. Add `DISTRIBUTION=desktop` to `dev:electron` script.

## Review Focus

- The `electronDownloadStore.ts` now guards the top-level
`electronAPI()` call behind `isDesktop` to prevent crashes on
non-desktop builds.
- Test mocking pattern uses `vi.hoisted` with a getter to allow per-test
toggling of the `isDesktop` value.
- Pre-existing issues not addressed: `as ElectronAPI` cast in
`envUtil.ts`, `:class="[]"` in `BaseViewTemplate.vue`,
`@ts-expect-error` in `ModelLibrarySidebarTab.vue`.
- This subsumes PR #8627 and renders PR #6122 and PR #7374 obsolete.

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-8710-refactor-replace-runtime-isElectron-with-build-time-isDesktop-constant-3006d73d365081c08037f0e61c2f6c77)
by [Unito](https://www.unito.io)
This commit is contained in:
Christian Byrne
2026-02-07 19:47:05 -08:00
committed by GitHub
parent b7fef1c744
commit 6c8473e4e4
30 changed files with 165 additions and 142 deletions

View File

@@ -5,7 +5,8 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { SystemStats } from '@/schemas/apiSchema'
import { api } from '@/scripts/api'
import { useSystemStatsStore } from '@/stores/systemStatsStore'
import { isElectron } from '@/utils/envUtil'
const mockData = vi.hoisted(() => ({ isDesktop: false }))
// Mock the API
vi.mock('@/scripts/api', () => ({
@@ -14,13 +15,13 @@ vi.mock('@/scripts/api', () => ({
}
}))
// Mock the envUtil
vi.mock('@/utils/envUtil', () => ({
isElectron: vi.fn()
vi.mock('@/platform/distribution/types', () => ({
get isDesktop() {
return mockData.isDesktop
},
isCloud: false
}))
vi.mock('@/platform/distribution/types', () => ({ isCloud: false }))
describe('useSystemStatsStore', () => {
let store: ReturnType<typeof useSystemStatsStore>
@@ -161,9 +162,9 @@ describe('useSystemStatsStore', () => {
expect(store.getFormFactor()).toBe('other')
})
describe('desktop environment (Electron)', () => {
describe('desktop environment', () => {
beforeEach(() => {
vi.mocked(isElectron).mockReturnValue(true)
mockData.isDesktop = true
})
it('should return "desktop-windows" for Windows desktop', () => {
@@ -239,9 +240,9 @@ describe('useSystemStatsStore', () => {
})
})
describe('git environment (non-Electron)', () => {
describe('git environment (non-desktop)', () => {
beforeEach(() => {
vi.mocked(isElectron).mockReturnValue(false)
mockData.isDesktop = false
})
it('should return "git-windows" for Windows git', () => {
@@ -319,7 +320,7 @@ describe('useSystemStatsStore', () => {
describe('case insensitive OS detection', () => {
beforeEach(() => {
vi.mocked(isElectron).mockReturnValue(false)
mockData.isDesktop = false
})
it('should handle uppercase OS names', () => {