Files
ComfyUI_frontend/src/utils/syncUtil.ts
Johnpaul Chiwetelu a64c561a5f Road to No explicit any: Group 8 (part 8) test files (#8496)
## Summary

This PR removes unsafe type assertions ("as unknown as Type") from test
files and improves type safety across the codebase.

### Key Changes

#### Type Safety Improvements
- Removed improper `as unknown as Type` patterns from test files in
Group 8 part 8
- Replaced with proper TypeScript patterns using Pinia store testing
patterns
- Fixed parameter shadowing issue in typeGuardUtil.test.ts (constructor
→ nodeConstructor)
- Fixed stale mock values in useConflictDetection.test.ts using getter
functions
- Refactored useManagerState tests to follow proper Pinia store testing
patterns with createTestingPinia

### Files Changed

Test files (Group 8 part 8 - utils and manager composables):
- src/utils/typeGuardUtil.test.ts - Fixed parameter shadowing
- src/utils/graphTraversalUtil.test.ts - Removed unsafe type assertions
- src/utils/litegraphUtil.test.ts - Improved type handling
- src/workbench/extensions/manager/composables/useManagerState.test.ts -
Complete rewrite using Pinia testing patterns
-
src/workbench/extensions/manager/composables/useConflictDetection.test.ts
- Fixed stale mock values with getters
- src/workbench/extensions/manager/composables/useManagerQueue.test.ts -
Type safety improvements
-
src/workbench/extensions/manager/composables/nodePack/useMissingNodes.test.ts
- Removed unsafe casts
-
src/workbench/extensions/manager/composables/nodePack/usePacksSelection.test.ts
- Type improvements
-
src/workbench/extensions/manager/composables/nodePack/usePacksStatus.test.ts
- Type improvements
- src/workbench/extensions/manager/utils/versionUtil.test.ts - Type
safety fixes

Source files (minor type fixes):
- src/utils/fuseUtil.ts - Type improvements
- src/utils/linkFixer.ts - Type safety fixes
- src/utils/syncUtil.ts - Type improvements
-
src/workbench/extensions/manager/composables/nodePack/useWorkflowPacks.ts
- Type fix
-
src/workbench/extensions/manager/composables/useConflictAcknowledgment.ts
- Type fix

### Testing
- All TypeScript type checking passes (`pnpm typecheck`)
- All affected test files pass (`pnpm test:unit`)
- Linting passes without errors (`pnpm lint`)
- Code formatting applied (`pnpm format`)

Part of the "Road to No Explicit Any" initiative, cleaning up type
casting issues from branch `fix/remove-any-types-part8`.

### Previous PRs in this series:
- Part 2: #7401
- Part 3: #7935
- Part 4: #7970
- Part 5: #8064
- Part 6: #8083
- Part 7: #8092
- Part 8 Group 1: #8253
- Part 8 Group 2: #8258
- Part 8 Group 3: #8304
- Part 8 Group 4: #8314
- Part 8 Group 5: #8329
- Part 8 Group 6: #8344
- Part 8 Group 7: #8459
- Part 8 Group 8: #8496 (this PR)

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-8496-Road-to-No-explicit-any-Group-8-part-8-test-files-2f86d73d365081f3afdcf8d01fba81e1)
by [Unito](https://www.unito.io)

---------

Co-authored-by: GitHub Action <action@github.com>
2026-01-30 22:25:10 +01:00

47 lines
1.5 KiB
TypeScript

import { api } from '@/scripts/api'
import type { UserDataFullInfo } from '@/schemas/apiSchema'
/**
* Sync entities from the API to the entityByPath map.
* @param dir The directory to sync from
* @param entityByPath The map to sync to
* @param createEntity A function to create an entity from a file
* @param updateEntity A function to update an entity from a file
* @param exclude A function to exclude an entity
*/
export async function syncEntities<T>(
dir: string,
entityByPath: Record<string, T>,
createEntity: (file: UserDataFullInfo & { path: string }) => T,
updateEntity: (entity: T, file: UserDataFullInfo & { path: string }) => void,
exclude: (file: T) => boolean = () => false
) {
const files = (await api.listUserDataFullInfo(dir)).map((file) => ({
...file,
path: dir ? `${dir}/${file.path}` : file.path
}))
for (const file of files) {
const existingEntity = entityByPath[file.path]
if (!existingEntity) {
// New entity, add it to the map
entityByPath[file.path] = createEntity(file)
} else if (exclude(existingEntity)) {
// Entity has been excluded, skip it
continue
} else {
// Entity has been modified, update its properties
updateEntity(existingEntity, file)
}
}
// Remove entities that no longer exist
for (const [path, entity] of Object.entries(entityByPath)) {
if (exclude(entity)) continue
if (!files.some((file) => file.path === path)) {
delete entityByPath[path]
}
}
}