Files
ComfyUI_frontend/tests-ui/tests/utils/versionUtil.test.ts
Jin Yi 5c1e00ff8e Refactor conflict detection system and move to manager extension (#5436)
## Refactor conflict detection system and move to manager extension

### Description

This PR refactors the conflict detection system, moving it from the
global composables to the manager extension folder for better code
organization. Additionally, it improves test type safety and adds
comprehensive test coverage for utility functions.

### Main Changes

#### 📦 Code Organization
- **Moved conflict detection to manager extension** - Relocated all
conflict detection related composables, stores, and utilities from
global scope to `/workbench/extensions/manager/` for better modularity
(https://github.com/Comfy-Org/ComfyUI_frontend/pull/5722)
- **Moved from** `src/composables/useConflictDetection.ts` **to**
`src/workbench/extensions/manager/composables/useConflictDetection.ts`
- Moved related stores and composables to maintain cohesive module
structure

#### ♻️ Refactoring
- **Extracted utility functions** - Split conflict detection logic into
separate utility modules:
  - `conflictUtils.ts` - Conflict consolidation and summary generation
- `systemCompatibility.ts` - OS and accelerator compatibility checking
  - `versionUtil.ts` - Version compatibility checking
- **Removed duplicate state management** - Cleaned up redundant state
and unused functions
- **Improved naming conventions** - Renamed functions for better clarity
- **Removed unused system environment code** - Cleaned up deprecated
code

#### 🔧 Test Improvements
- **Fixed TypeScript errors** in all test files - removed all `any` type
usage
- **Added comprehensive test coverage**:
  - `conflictUtils.test.ts` - 299 lines of tests for conflict utilities
- `systemCompatibility.test.ts` - 270 lines of tests for compatibility
checking
  - `versionUtil.test.ts` - 342 lines of tests for version utilities
- **Updated mock objects** to match actual implementations
- **Aligned with backend changes** - Updated SystemStats structure to
include `pytorch_version`, `embedded_python`,
`required_frontend_version`

#### 🐛 Bug Fixes
- **Fixed OS detection bug** - Resolved issue where 'darwin' was
incorrectly matched as 'Windows' due to containing 'win' substring
- **Fixed import paths** - Updated all import paths after moving to
manager extension
- **Fixed unused exports** - Removed all unused function exports
- **Fixed lint errors** - Resolved all ESLint and Prettier issues

### File Structure Changes

```
Before:
src/
├── composables/
│   └── useConflictDetection.ts (1374 lines)
└── types/

After:
src/
├── utils/
│   ├── conflictUtils.ts (114 lines)
│   ├── systemCompatibility.ts (125 lines)
│   └── versionUtil.ts (enhanced)
└── workbench/extensions/manager/
    ├── composables/
    │   ├── useConflictDetection.ts (758 lines)
    │   └── [other composables]
    └── stores/
        └── conflictDetectionStore.ts
```

### Testing

All tests pass successfully:
-  **155 test files passed**
-  **2209 tests passed**
-  19 skipped (intentionally skipped subgraph-related tests)

### Impact

- **Better code organization** - Manager-specific code is now properly
isolated
- **Improved maintainability** - Smaller, focused utility functions are
easier to test and maintain
- **Enhanced type safety** - No more `any` types in tests
- **Comprehensive test coverage** - All utility functions are thoroughly
tested

### Commits in this PR
1. OS detection bug fix and refactor
2. Remove unused system environment code
3. Improve function naming
4. Refactor conflict detection
5. Remove duplicate state and unused functions
6. Fix unused function exports
7. Move manager features to workbench extension folder
8. Fix import paths
9. Rename systemCompatibility file
10. Improve test type safety
11. Apply ESLint and Prettier fixes

## Screenshots (if applicable)

[screen-capture.webm](https://github.com/user-attachments/assets/b4595604-3761-4d98-ae8e-5693cd0c95bd)


┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-5436-Manager-refactor-conflict-detect-2686d73d36508186ba06f57dae3656e5)
by [Unito](https://www.unito.io)

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-09-26 12:21:05 +09:00

347 lines
9.3 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import {
checkVersionCompatibility,
getFrontendVersion
} from '@/workbench/extensions/manager/utils/versionUtil'
// Mock config module
vi.mock('@/config', () => ({
default: {
app_version: '1.24.0-1'
}
}))
describe('versionUtil', () => {
describe('checkVersionCompatibility', () => {
it('should return null when current version is undefined', () => {
const result = checkVersionCompatibility(
'comfyui_version',
undefined,
'>=1.0.0'
)
expect(result).toBeNull()
})
it('should return null when current version is null', () => {
const result = checkVersionCompatibility(
'comfyui_version',
null as any,
'>=1.0.0'
)
expect(result).toBeNull()
})
it('should return null when current version is empty string', () => {
const result = checkVersionCompatibility('comfyui_version', '', '>=1.0.0')
expect(result).toBeNull()
})
it('should return null when supported version is undefined', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'1.0.0',
undefined
)
expect(result).toBeNull()
})
it('should return null when supported version is null', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'1.0.0',
null as any
)
expect(result).toBeNull()
})
it('should return null when supported version is empty string', () => {
const result = checkVersionCompatibility('comfyui_version', '1.0.0', '')
expect(result).toBeNull()
})
it('should return null when supported version is whitespace only', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'1.0.0',
' '
)
expect(result).toBeNull()
})
describe('version compatibility checks', () => {
it('should return null when version satisfies >= requirement', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'2.0.0',
'>=1.0.0'
)
expect(result).toBeNull()
})
it('should return null when version exactly matches requirement', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'1.0.0',
'1.0.0'
)
expect(result).toBeNull()
})
it('should return null when version satisfies ^ requirement', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'1.2.3',
'^1.0.0'
)
expect(result).toBeNull()
})
it('should return null when version satisfies ~ requirement', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'1.0.5',
'~1.0.0'
)
expect(result).toBeNull()
})
it('should return null when version satisfies range requirement', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'1.5.0',
'1.0.0 - 2.0.0'
)
expect(result).toBeNull()
})
it('should return conflict when version does not satisfy >= requirement', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'0.9.0',
'>=1.0.0'
)
expect(result).toEqual({
type: 'comfyui_version',
current_value: '0.9.0',
required_value: '>=1.0.0'
})
})
it('should return conflict when version does not satisfy ^ requirement', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'2.0.0',
'^1.0.0'
)
expect(result).toEqual({
type: 'comfyui_version',
current_value: '2.0.0',
required_value: '^1.0.0'
})
})
it('should return conflict when version is outside range', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'3.0.0',
'1.0.0 - 2.0.0'
)
expect(result).toEqual({
type: 'comfyui_version',
current_value: '3.0.0',
required_value: '1.0.0 - 2.0.0'
})
})
})
describe('version cleaning', () => {
it('should handle versions with v prefix', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'v1.0.0',
'>=1.0.0'
)
expect(result).toBeNull()
})
it('should handle versions with pre-release tags', () => {
// Pre-release versions have specific semver rules
// 1.0.0-alpha satisfies >=1.0.0-alpha but not >=1.0.0
const result = checkVersionCompatibility(
'comfyui_version',
'1.0.0-alpha',
'>=1.0.0-alpha'
)
expect(result).toBeNull()
// This should fail because pre-release < stable
const result2 = checkVersionCompatibility(
'comfyui_version',
'1.0.0-alpha',
'>=1.0.0'
)
expect(result2).toEqual({
type: 'comfyui_version',
current_value: '1.0.0-alpha',
required_value: '>=1.0.0'
})
})
it('should handle versions with build metadata', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'1.0.0+build123',
'>=1.0.0'
)
expect(result).toBeNull()
})
it('should handle malformed versions gracefully', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'not-a-version',
'>=1.0.0'
)
expect(result).toEqual({
type: 'comfyui_version',
current_value: 'not-a-version',
required_value: '>=1.0.0'
})
})
})
describe('different conflict types', () => {
it('should handle comfyui_version type', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'0.5.0',
'>=1.0.0'
)
expect(result?.type).toBe('comfyui_version')
})
it('should handle frontend_version type', () => {
const result = checkVersionCompatibility(
'frontend_version',
'0.5.0',
'>=1.0.0'
)
expect(result?.type).toBe('frontend_version')
})
})
describe('complex version ranges', () => {
it('should handle OR conditions with ||', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'1.5.0',
'>=1.0.0 <2.0.0 || >=3.0.0'
)
expect(result).toBeNull()
})
it('should handle multiple constraints', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'1.5.0',
'>=1.0.0 <2.0.0'
)
expect(result).toBeNull()
})
it('should return conflict when no constraints are met', () => {
const result = checkVersionCompatibility(
'comfyui_version',
'2.5.0',
'>=1.0.0 <2.0.0 || >=3.0.0 <4.0.0'
)
expect(result).toEqual({
type: 'comfyui_version',
current_value: '2.5.0',
required_value: '>=1.0.0 <2.0.0 || >=3.0.0 <4.0.0'
})
})
})
})
describe('getFrontendVersion', () => {
it('should return app_version from config when available', () => {
const version = getFrontendVersion()
expect(version).toBe('1.24.0-1')
})
it('should fallback to VITE_APP_VERSION when app_version is not available', async () => {
// Save original environment
const originalEnv = import.meta.env.VITE_APP_VERSION
// Mock config without app_version
vi.doMock('@/config', () => ({
default: {}
}))
// Set VITE_APP_VERSION
import.meta.env.VITE_APP_VERSION = '2.0.0'
// Clear module cache to force re-import
vi.resetModules()
// Import fresh module
const versionUtil = await import(
'@/workbench/extensions/manager/utils/versionUtil'
)
const version = versionUtil.getFrontendVersion()
expect(version).toBe('2.0.0')
// Restore original env
import.meta.env.VITE_APP_VERSION = originalEnv
// Reset mocks for next test
vi.resetModules()
vi.doMock('@/config', () => ({
default: {
app_version: '1.24.0-1'
}
}))
})
it('should return undefined when no version is available', async () => {
// Save original environment
const originalEnv = import.meta.env.VITE_APP_VERSION
// Mock config without app_version
vi.doMock('@/config', () => ({
default: {}
}))
// Clear VITE_APP_VERSION
delete import.meta.env.VITE_APP_VERSION
// Clear module cache to force re-import
vi.resetModules()
// Import fresh module
const versionUtil = await import(
'@/workbench/extensions/manager/utils/versionUtil'
)
const version = versionUtil.getFrontendVersion()
expect(version).toBeUndefined()
// Restore original env
if (originalEnv !== undefined) {
import.meta.env.VITE_APP_VERSION = originalEnv
}
// Reset mocks for next test
vi.resetModules()
vi.doMock('@/config', () => ({
default: {
app_version: '1.24.0-1'
}
}))
})
})
})