mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-07-03 13:48:49 +00:00
Compare commits
2 Commits
matt/be-22
...
codex/cove
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
98b31d1c71 | ||
|
|
31d1d8d84d |
85
src/platform/assets/composables/media/useAssetsApi.test.ts
Normal file
85
src/platform/assets/composables/media/useAssetsApi.test.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
|
||||
import { useAssetsApi } from './useAssetsApi'
|
||||
|
||||
const mockAssetsStore = vi.hoisted(() => ({
|
||||
inputAssets: [] as AssetItem[],
|
||||
historyAssets: [] as AssetItem[],
|
||||
inputLoading: false,
|
||||
historyLoading: false,
|
||||
inputError: null as string | null,
|
||||
historyError: null as string | null,
|
||||
hasMoreHistory: false,
|
||||
isLoadingMore: false,
|
||||
updateInputs: vi.fn(),
|
||||
updateHistory: vi.fn(),
|
||||
loadMoreHistory: vi.fn()
|
||||
}))
|
||||
|
||||
vi.mock('@/stores/assetsStore', () => ({
|
||||
useAssetsStore: () => mockAssetsStore
|
||||
}))
|
||||
|
||||
function createAsset(id: string): AssetItem {
|
||||
return {
|
||||
id,
|
||||
name: `${id}.png`,
|
||||
size: 1,
|
||||
created_at: '2026-01-01T00:00:00Z',
|
||||
tags: ['input']
|
||||
}
|
||||
}
|
||||
|
||||
describe('useAssetsApi', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockAssetsStore.inputAssets = [createAsset('input-1')]
|
||||
mockAssetsStore.historyAssets = [createAsset('history-1')]
|
||||
mockAssetsStore.inputLoading = true
|
||||
mockAssetsStore.historyLoading = false
|
||||
mockAssetsStore.inputError = 'input-error'
|
||||
mockAssetsStore.historyError = 'history-error'
|
||||
mockAssetsStore.hasMoreHistory = true
|
||||
mockAssetsStore.isLoadingMore = true
|
||||
})
|
||||
|
||||
it('uses input assets and refreshes inputs', async () => {
|
||||
const api = useAssetsApi('input')
|
||||
|
||||
expect(api.media.value).toEqual([createAsset('input-1')])
|
||||
expect(api.loading.value).toBe(true)
|
||||
expect(api.error.value).toBe('input-error')
|
||||
expect(api.hasMore.value).toBe(false)
|
||||
expect(api.isLoadingMore.value).toBe(false)
|
||||
|
||||
await expect(api.fetchMediaList()).resolves.toEqual([
|
||||
createAsset('input-1')
|
||||
])
|
||||
await expect(api.refresh()).resolves.toEqual([createAsset('input-1')])
|
||||
await api.loadMore()
|
||||
|
||||
expect(mockAssetsStore.updateInputs).toHaveBeenCalledTimes(2)
|
||||
expect(mockAssetsStore.updateHistory).not.toHaveBeenCalled()
|
||||
expect(mockAssetsStore.loadMoreHistory).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('uses output history and loads more history', async () => {
|
||||
const api = useAssetsApi('output')
|
||||
|
||||
expect(api.media.value).toEqual([createAsset('history-1')])
|
||||
expect(api.loading.value).toBe(false)
|
||||
expect(api.error.value).toBe('history-error')
|
||||
expect(api.hasMore.value).toBe(true)
|
||||
expect(api.isLoadingMore.value).toBe(true)
|
||||
|
||||
await expect(api.fetchMediaList()).resolves.toEqual([
|
||||
createAsset('history-1')
|
||||
])
|
||||
await api.loadMore()
|
||||
|
||||
expect(mockAssetsStore.updateHistory).toHaveBeenCalledOnce()
|
||||
expect(mockAssetsStore.updateInputs).not.toHaveBeenCalled()
|
||||
expect(mockAssetsStore.loadMoreHistory).toHaveBeenCalledOnce()
|
||||
})
|
||||
})
|
||||
@@ -8,6 +8,7 @@ import { useI18n } from 'vue-i18n'
|
||||
|
||||
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
|
||||
import { MediaAssetKey } from '@/platform/assets/schemas/mediaAssetSchema'
|
||||
import { api } from '@/scripts/api'
|
||||
import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
|
||||
import type { AssetMeta } from '@/platform/assets/schemas/mediaAssetSchema'
|
||||
import type * as outputAssetUtilModule from '../utils/outputAssetUtil'
|
||||
@@ -18,6 +19,12 @@ const mockIsCloud = vi.hoisted(() => ({ value: false }))
|
||||
|
||||
// Track the filename passed to createAnnotatedPath
|
||||
const capturedFilenames = vi.hoisted(() => ({ values: [] as string[] }))
|
||||
const capturedAnnotatedPaths = vi.hoisted(() => ({
|
||||
values: [] as Array<{
|
||||
item: { filename: string; subfolder?: string; type?: string }
|
||||
options: { rootFolder?: string }
|
||||
}>
|
||||
}))
|
||||
|
||||
const mockDownloadFile = vi.hoisted(() => vi.fn())
|
||||
vi.mock('@/base/common/downloadUtil', () => ({
|
||||
@@ -73,9 +80,10 @@ vi.mock('@/stores/modelToNodeStore', () => ({
|
||||
useModelToNodeStore: () => ({})
|
||||
}))
|
||||
|
||||
const mockCopyToClipboard = vi.hoisted(() => vi.fn())
|
||||
vi.mock('@/composables/useCopyToClipboard', () => ({
|
||||
useCopyToClipboard: () => ({
|
||||
copyToClipboard: vi.fn()
|
||||
copyToClipboard: mockCopyToClipboard
|
||||
})
|
||||
}))
|
||||
|
||||
@@ -93,45 +101,50 @@ vi.mock('@/platform/workflow/utils/workflowExtractionUtil', () => ({
|
||||
extractWorkflowFromAsset: mockExtractWorkflowFromAsset
|
||||
}))
|
||||
|
||||
const mockAddNodeOnGraph = vi.hoisted(() => vi.fn())
|
||||
const mockGetCanvasCenter = vi.hoisted(() => vi.fn())
|
||||
vi.mock('@/services/litegraphService', () => ({
|
||||
useLitegraphService: () => ({
|
||||
addNodeOnGraph: vi.fn().mockReturnValue(
|
||||
fromAny<LGraphNode, unknown>({
|
||||
widgets: [{ name: 'image', value: '', callback: vi.fn() }],
|
||||
graph: { setDirtyCanvas: vi.fn() }
|
||||
})
|
||||
),
|
||||
getCanvasCenter: vi.fn().mockReturnValue([100, 100])
|
||||
addNodeOnGraph: mockAddNodeOnGraph,
|
||||
getCanvasCenter: mockGetCanvasCenter
|
||||
})
|
||||
}))
|
||||
|
||||
const mockNodeDefsByName = vi.hoisted(() => ({
|
||||
value: {
|
||||
LoadImage: {
|
||||
name: 'LoadImage',
|
||||
display_name: 'Load Image'
|
||||
}
|
||||
} as Record<string, unknown>
|
||||
}))
|
||||
vi.mock('@/stores/nodeDefStore', () => ({
|
||||
useNodeDefStore: () => ({
|
||||
nodeDefsByName: {
|
||||
LoadImage: {
|
||||
name: 'LoadImage',
|
||||
display_name: 'Load Image'
|
||||
}
|
||||
}
|
||||
nodeDefsByName: mockNodeDefsByName.value
|
||||
})
|
||||
}))
|
||||
|
||||
vi.mock('@/utils/createAnnotatedPath', () => ({
|
||||
createAnnotatedPath: vi.fn((item: { filename: string }) => {
|
||||
capturedFilenames.values.push(item.filename)
|
||||
return item.filename
|
||||
})
|
||||
createAnnotatedPath: vi.fn(
|
||||
(
|
||||
item: { filename: string; subfolder?: string; type?: string },
|
||||
options: { rootFolder?: string }
|
||||
) => {
|
||||
capturedAnnotatedPaths.values.push({ item, options })
|
||||
capturedFilenames.values.push(item.filename)
|
||||
return item.filename
|
||||
}
|
||||
)
|
||||
}))
|
||||
|
||||
const mockDetectNodeTypeFromFilename = vi.hoisted(() => vi.fn())
|
||||
vi.mock('@/utils/loaderNodeUtil', () => ({
|
||||
detectNodeTypeFromFilename: vi.fn().mockReturnValue({
|
||||
nodeType: 'LoadImage',
|
||||
widgetName: 'image'
|
||||
})
|
||||
detectNodeTypeFromFilename: mockDetectNodeTypeFromFilename
|
||||
}))
|
||||
|
||||
const mockIsResultItemType = vi.hoisted(() => vi.fn())
|
||||
vi.mock('@/utils/typeGuardUtil', () => ({
|
||||
isResultItemType: vi.fn().mockReturnValue(true)
|
||||
isResultItemType: mockIsResultItemType
|
||||
}))
|
||||
|
||||
const mockGetAssetType = vi.hoisted(() => vi.fn())
|
||||
@@ -186,7 +199,9 @@ vi.mock('@/scripts/api', () => ({
|
||||
}
|
||||
}))
|
||||
|
||||
const mockAppGraph = vi.hoisted(() => ({ value: { _nodes: [] as unknown[] } }))
|
||||
const mockAppGraph = vi.hoisted(() => ({
|
||||
value: { _nodes: [] as unknown[] } as { _nodes: unknown[] } | null
|
||||
}))
|
||||
vi.mock('@/scripts/app', () => ({
|
||||
app: {
|
||||
get graph() {
|
||||
@@ -291,7 +306,43 @@ describe('useMediaAssetActions', () => {
|
||||
setActivePinia(createTestingPinia({ stubActions: false }))
|
||||
vi.clearAllMocks()
|
||||
capturedFilenames.values = []
|
||||
capturedAnnotatedPaths.values = []
|
||||
mockIsCloud.value = false
|
||||
mockAppGraph.value = { _nodes: [] }
|
||||
mockDownloadFile.mockReset()
|
||||
mockCopyToClipboard.mockReset()
|
||||
mockShowDialog.mockReset()
|
||||
mockAddNodeOnGraph.mockReset()
|
||||
mockAddNodeOnGraph.mockReturnValue(
|
||||
fromAny<LGraphNode, unknown>({
|
||||
widgets: [{ name: 'image', value: '', callback: vi.fn() }],
|
||||
graph: { setDirtyCanvas: vi.fn() }
|
||||
})
|
||||
)
|
||||
mockGetCanvasCenter.mockReset()
|
||||
mockGetCanvasCenter.mockReturnValue([100, 100])
|
||||
mockNodeDefsByName.value = {
|
||||
LoadImage: {
|
||||
name: 'LoadImage',
|
||||
display_name: 'Load Image'
|
||||
}
|
||||
}
|
||||
mockDetectNodeTypeFromFilename.mockReset()
|
||||
mockDetectNodeTypeFromFilename.mockReturnValue({
|
||||
nodeType: 'LoadImage',
|
||||
widgetName: 'image'
|
||||
})
|
||||
mockIsResultItemType.mockReset()
|
||||
mockIsResultItemType.mockReturnValue(true)
|
||||
mockExtractWorkflowFromAsset.mockReset()
|
||||
mockOpenWorkflowAction.mockReset()
|
||||
mockExportWorkflowAction.mockReset()
|
||||
mockCreateAssetExport.mockReset()
|
||||
mockCreateAssetExport.mockResolvedValue({
|
||||
task_id: 'test-task-id',
|
||||
status: 'pending'
|
||||
})
|
||||
mockDeleteAsset.mockReset()
|
||||
mockGetOutputAssetMetadata.mockReset()
|
||||
mockGetOutputAssetMetadata.mockReturnValue(null)
|
||||
mockGetAssetType.mockReset()
|
||||
@@ -299,7 +350,139 @@ describe('useMediaAssetActions', () => {
|
||||
mockResolveOutputAssetItems.mockResolvedValue([])
|
||||
})
|
||||
|
||||
describe('copyJobId', () => {
|
||||
it('does nothing when no asset is available', async () => {
|
||||
const { actions, unmount } = mountMediaActions()
|
||||
|
||||
await actions.copyJobId()
|
||||
|
||||
expect(mockCopyToClipboard).not.toHaveBeenCalled()
|
||||
expect(useToast().add).not.toHaveBeenCalled()
|
||||
|
||||
unmount()
|
||||
})
|
||||
|
||||
it('warns when the asset has no job id', async () => {
|
||||
mockGetAssetType.mockReturnValue('input')
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.copyJobId(createMockAsset())
|
||||
|
||||
expect(mockCopyToClipboard).not.toHaveBeenCalled()
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'warn' })
|
||||
)
|
||||
})
|
||||
|
||||
it('copies the metadata job id when present', async () => {
|
||||
mockGetOutputAssetMetadata.mockReturnValue({ jobId: 'job-from-meta' })
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.copyJobId(createMockAsset())
|
||||
|
||||
expect(mockCopyToClipboard).toHaveBeenCalledWith('job-from-meta')
|
||||
})
|
||||
|
||||
it('copies the output asset id when metadata omits the job id', async () => {
|
||||
mockGetAssetType.mockReturnValue('output')
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.copyJobId(createMockAsset({ id: 'history-id' }))
|
||||
|
||||
expect(mockCopyToClipboard).toHaveBeenCalledWith('history-id')
|
||||
})
|
||||
})
|
||||
|
||||
describe('addWorkflow', () => {
|
||||
it('does nothing when no asset is available', async () => {
|
||||
const { actions, unmount } = mountMediaActions()
|
||||
|
||||
await actions.addWorkflow()
|
||||
|
||||
expect(mockAddNodeOnGraph).not.toHaveBeenCalled()
|
||||
expect(useToast().add).not.toHaveBeenCalled()
|
||||
|
||||
unmount()
|
||||
})
|
||||
|
||||
it('uses the injected media asset when no explicit asset is provided', async () => {
|
||||
const mediaAsset = createMockMediaAsset({ name: 'context-image.png' })
|
||||
const { actions, unmount } = mountMediaActions(mediaAsset)
|
||||
|
||||
await actions.addWorkflow()
|
||||
|
||||
expect(capturedFilenames.values).toContain('context-image.png')
|
||||
|
||||
unmount()
|
||||
})
|
||||
|
||||
it('warns when the filename has no compatible loader node', async () => {
|
||||
mockDetectNodeTypeFromFilename.mockReturnValue({
|
||||
nodeType: undefined,
|
||||
widgetName: undefined
|
||||
})
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.addWorkflow(createMockAsset({ name: 'notes.txt' }))
|
||||
|
||||
expect(mockAddNodeOnGraph).not.toHaveBeenCalled()
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'warn' })
|
||||
)
|
||||
})
|
||||
|
||||
it('reports missing node definitions', async () => {
|
||||
mockNodeDefsByName.value = {}
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.addWorkflow(createMockAsset())
|
||||
|
||||
expect(mockAddNodeOnGraph).not.toHaveBeenCalled()
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'error' })
|
||||
)
|
||||
})
|
||||
|
||||
it('reports loader-node creation failure', async () => {
|
||||
mockAddNodeOnGraph.mockReturnValue(undefined)
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.addWorkflow(createMockAsset())
|
||||
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'error' })
|
||||
)
|
||||
})
|
||||
|
||||
it('still adds the node when the expected widget is absent', async () => {
|
||||
const setDirtyCanvas = vi.fn()
|
||||
mockAddNodeOnGraph.mockReturnValue(
|
||||
fromAny<LGraphNode, unknown>({
|
||||
widgets: [{ name: 'other', value: '' }],
|
||||
graph: { setDirtyCanvas }
|
||||
})
|
||||
)
|
||||
mockGetOutputAssetMetadata.mockReturnValue({ subfolder: 'nested' })
|
||||
mockGetAssetType.mockReturnValue('custom')
|
||||
mockIsResultItemType.mockReturnValue(false)
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.addWorkflow(createMockAsset({ name: 'asset.png' }))
|
||||
|
||||
expect(capturedAnnotatedPaths.values.at(-1)).toEqual({
|
||||
item: {
|
||||
filename: 'asset.png',
|
||||
subfolder: 'nested',
|
||||
type: undefined
|
||||
},
|
||||
options: { rootFolder: 'input' }
|
||||
})
|
||||
expect(setDirtyCanvas).toHaveBeenCalledWith(true, true)
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'success' })
|
||||
)
|
||||
})
|
||||
|
||||
describe('OSS mode (isCloud = false)', () => {
|
||||
beforeEach(() => {
|
||||
mockIsCloud.value = false
|
||||
@@ -366,6 +549,83 @@ describe('useMediaAssetActions', () => {
|
||||
})
|
||||
|
||||
describe('addMultipleToWorkflow', () => {
|
||||
it('does nothing for an empty selection', async () => {
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.addMultipleToWorkflow([])
|
||||
|
||||
expect(mockAddNodeOnGraph).not.toHaveBeenCalled()
|
||||
expect(useToast().add).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('shows a failure toast when none of the selected assets can be added', async () => {
|
||||
mockDetectNodeTypeFromFilename
|
||||
.mockReturnValueOnce({ nodeType: undefined, widgetName: undefined })
|
||||
.mockReturnValueOnce({ nodeType: 'MissingNode', widgetName: 'image' })
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.addMultipleToWorkflow([
|
||||
createMockAsset({ id: 'a', name: 'unsupported.txt' }),
|
||||
createMockAsset({ id: 'b', name: 'missing.png' })
|
||||
])
|
||||
|
||||
expect(mockAddNodeOnGraph).not.toHaveBeenCalled()
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'error' })
|
||||
)
|
||||
})
|
||||
|
||||
it('shows a partial warning when only some nodes are added', async () => {
|
||||
mockAddNodeOnGraph
|
||||
.mockReturnValueOnce(
|
||||
fromAny<LGraphNode, unknown>({
|
||||
widgets: [{ name: 'image', value: '', callback: vi.fn() }],
|
||||
graph: { setDirtyCanvas: vi.fn() }
|
||||
})
|
||||
)
|
||||
.mockReturnValueOnce(undefined)
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.addMultipleToWorkflow([
|
||||
createMockAsset({ id: 'a', name: 'a.png' }),
|
||||
createMockAsset({ id: 'b', name: 'b.png' })
|
||||
])
|
||||
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'warn' })
|
||||
)
|
||||
})
|
||||
|
||||
it('adds assets without a matching widget using untyped paths', async () => {
|
||||
const setDirtyCanvas = vi.fn()
|
||||
mockAddNodeOnGraph.mockReturnValue(
|
||||
fromAny<LGraphNode, unknown>({
|
||||
widgets: [{ name: 'other', value: '' }],
|
||||
graph: { setDirtyCanvas }
|
||||
})
|
||||
)
|
||||
mockGetAssetType.mockReturnValue('custom')
|
||||
mockIsResultItemType.mockReturnValue(false)
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.addMultipleToWorkflow([
|
||||
createMockAsset({ id: 'asset-1', name: 'asset-1.png' })
|
||||
])
|
||||
|
||||
expect(capturedAnnotatedPaths.values.at(-1)).toEqual({
|
||||
item: {
|
||||
filename: 'asset-1.png',
|
||||
subfolder: '',
|
||||
type: undefined
|
||||
},
|
||||
options: { rootFolder: undefined }
|
||||
})
|
||||
expect(setDirtyCanvas).toHaveBeenCalledWith(true, true)
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'success' })
|
||||
)
|
||||
})
|
||||
|
||||
describe('Cloud mode (isCloud = true)', () => {
|
||||
beforeEach(() => {
|
||||
mockIsCloud.value = true
|
||||
@@ -397,10 +657,56 @@ describe('useMediaAssetActions', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('openWorkflow', () => {
|
||||
beforeEach(() => {
|
||||
mockExtractWorkflowFromAsset.mockResolvedValue({
|
||||
workflow: { version: 0.4 },
|
||||
filename: 'workflow.json'
|
||||
})
|
||||
})
|
||||
|
||||
it('does nothing when no asset is available', async () => {
|
||||
const { actions, unmount } = mountMediaActions()
|
||||
|
||||
await actions.openWorkflow()
|
||||
|
||||
expect(mockExtractWorkflowFromAsset).not.toHaveBeenCalled()
|
||||
expect(mockOpenWorkflowAction).not.toHaveBeenCalled()
|
||||
|
||||
unmount()
|
||||
})
|
||||
|
||||
it('shows a success toast after opening the workflow', async () => {
|
||||
mockOpenWorkflowAction.mockResolvedValue({ success: true })
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.openWorkflow(createMockAsset())
|
||||
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'success' })
|
||||
)
|
||||
})
|
||||
|
||||
it('uses the fallback warning when opening returns no error message', async () => {
|
||||
mockOpenWorkflowAction.mockResolvedValue({ success: false })
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.openWorkflow(createMockAsset())
|
||||
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
severity: 'warn',
|
||||
detail: 'mediaAsset.noWorkflowDataFound'
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('exportWorkflow', () => {
|
||||
const successResult = { success: true } as const
|
||||
const cancelledResult = { success: false, cancelled: true } as const
|
||||
const failureResult = { success: false, error: 'boom' } as const
|
||||
const failureWithoutError = { success: false } as const
|
||||
const noWorkflowResult = {
|
||||
success: false,
|
||||
error: 'No workflow data available'
|
||||
@@ -455,6 +761,31 @@ describe('useMediaAssetActions', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('does nothing when no asset is available', async () => {
|
||||
const { actions, unmount } = mountMediaActions()
|
||||
|
||||
await actions.exportWorkflow()
|
||||
|
||||
expect(mockExtractWorkflowFromAsset).not.toHaveBeenCalled()
|
||||
expect(mockExportWorkflowAction).not.toHaveBeenCalled()
|
||||
|
||||
unmount()
|
||||
})
|
||||
|
||||
it('uses the fallback error when export fails without a message', async () => {
|
||||
mockExportWorkflowAction.mockResolvedValue(failureWithoutError)
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.exportWorkflow(createMockAsset())
|
||||
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
severity: 'error',
|
||||
detail: 'mediaAsset.failedToExportWorkflow'
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('shows no toast when every asset in a bulk export is cancelled', async () => {
|
||||
mockExportWorkflowAction.mockResolvedValue(cancelledResult)
|
||||
const actions = useMediaAssetActions()
|
||||
@@ -500,6 +831,118 @@ describe('useMediaAssetActions', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('openMultipleWorkflows', () => {
|
||||
beforeEach(() => {
|
||||
mockExtractWorkflowFromAsset.mockResolvedValue({
|
||||
workflow: { version: 0.4 },
|
||||
filename: 'workflow.json'
|
||||
})
|
||||
})
|
||||
|
||||
it('does nothing for an empty selection', async () => {
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.openMultipleWorkflows([])
|
||||
|
||||
expect(mockOpenWorkflowAction).not.toHaveBeenCalled()
|
||||
expect(useToast().add).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('shows success when every workflow opens', async () => {
|
||||
mockOpenWorkflowAction.mockResolvedValue({ success: true })
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.openMultipleWorkflows([
|
||||
createMockAsset({ id: 'a' }),
|
||||
createMockAsset({ id: 'b' })
|
||||
])
|
||||
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'success' })
|
||||
)
|
||||
})
|
||||
|
||||
it('shows a missing-workflow warning when none open', async () => {
|
||||
mockOpenWorkflowAction.mockResolvedValue({ success: false })
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.openMultipleWorkflows([
|
||||
createMockAsset({ id: 'a' }),
|
||||
createMockAsset({ id: 'b' })
|
||||
])
|
||||
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'warn' })
|
||||
)
|
||||
})
|
||||
|
||||
it('shows a partial warning when extraction throws for one asset', async () => {
|
||||
mockExtractWorkflowFromAsset
|
||||
.mockResolvedValueOnce({
|
||||
workflow: { version: 0.4 },
|
||||
filename: 'ok.json'
|
||||
})
|
||||
.mockRejectedValueOnce(new Error('missing workflow'))
|
||||
mockOpenWorkflowAction.mockResolvedValue({ success: true })
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.openMultipleWorkflows([
|
||||
createMockAsset({ id: 'a' }),
|
||||
createMockAsset({ id: 'b' })
|
||||
])
|
||||
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'warn' })
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('exportMultipleWorkflows', () => {
|
||||
beforeEach(() => {
|
||||
mockExtractWorkflowFromAsset.mockResolvedValue({
|
||||
workflow: { version: 0.4 },
|
||||
filename: 'workflow.json'
|
||||
})
|
||||
})
|
||||
|
||||
it('does nothing for an empty selection', async () => {
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.exportMultipleWorkflows([])
|
||||
|
||||
expect(mockExportWorkflowAction).not.toHaveBeenCalled()
|
||||
expect(useToast().add).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('shows no-workflows warning when every export fails', async () => {
|
||||
mockExportWorkflowAction.mockResolvedValue({
|
||||
success: false,
|
||||
error: 'boom'
|
||||
})
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.exportMultipleWorkflows([
|
||||
createMockAsset({ id: 'a' }),
|
||||
createMockAsset({ id: 'b' })
|
||||
])
|
||||
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'warn' })
|
||||
)
|
||||
})
|
||||
|
||||
it('counts extraction failures as failed exports', async () => {
|
||||
mockExtractWorkflowFromAsset.mockRejectedValue(new Error('missing'))
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.exportMultipleWorkflows([createMockAsset()])
|
||||
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'warn' })
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('downloadAssets', () => {
|
||||
it('downloads the injected media asset when called without explicit assets', () => {
|
||||
const mediaAsset = createMockMediaAsset({
|
||||
@@ -534,6 +977,36 @@ describe('useMediaAssetActions', () => {
|
||||
unmount()
|
||||
})
|
||||
|
||||
it('uses the asset URL when no preview URL is available', () => {
|
||||
mockGetAssetType.mockReturnValue('input')
|
||||
const asset = createMockAsset({
|
||||
name: 'raw image.png',
|
||||
preview_url: undefined,
|
||||
user_metadata: { subfolder: 'uploads' }
|
||||
})
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
actions.downloadAssets([asset])
|
||||
|
||||
expect(mockDownloadFile).toHaveBeenCalledWith(
|
||||
'http://localhost:8188/api/view?filename=raw+image.png&type=input&subfolder=uploads',
|
||||
'raw image.png'
|
||||
)
|
||||
})
|
||||
|
||||
it('shows an error toast when a direct download throws', () => {
|
||||
mockDownloadFile.mockImplementation(() => {
|
||||
throw new Error('download failed')
|
||||
})
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
actions.downloadAssets([createMockAsset()])
|
||||
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'error' })
|
||||
)
|
||||
})
|
||||
|
||||
it('keeps single explicit assets on the direct download path in cloud', () => {
|
||||
mockIsCloud.value = true
|
||||
mockGetOutputAssetMetadata.mockReturnValue({
|
||||
@@ -943,6 +1416,82 @@ describe('useMediaAssetActions', () => {
|
||||
})
|
||||
expect(payload.naming_strategy).toBe('preserve')
|
||||
})
|
||||
|
||||
it('should include asset ids for imported assets', async () => {
|
||||
mockGetAssetType.mockImplementation((asset: AssetItem) =>
|
||||
asset.tags?.includes('output') ? 'output' : 'input'
|
||||
)
|
||||
const asset1 = createMockAsset({ id: 'input-1', tags: ['input'] })
|
||||
const asset2 = createMockAsset({ id: 'input-2', tags: ['input'] })
|
||||
|
||||
const actions = useMediaAssetActions()
|
||||
actions.downloadAssets([asset1, asset2])
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(mockCreateAssetExport).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
const payload = mockCreateAssetExport.mock.calls[0][0]
|
||||
expect(payload.job_ids).toBeUndefined()
|
||||
expect(payload.asset_ids).toEqual(['input-1', 'input-2'])
|
||||
expect(payload.naming_strategy).toBe('preserve')
|
||||
})
|
||||
|
||||
it('should mix output job ids and imported asset ids', async () => {
|
||||
mockGetAssetType.mockImplementation((asset: AssetItem) =>
|
||||
asset.tags?.includes('output') ? 'output' : 'input'
|
||||
)
|
||||
const output = createMockAsset({
|
||||
id: 'history-id',
|
||||
name: 'output.png',
|
||||
tags: ['output']
|
||||
})
|
||||
const imported = createMockAsset({ id: 'input-id', tags: ['input'] })
|
||||
|
||||
const actions = useMediaAssetActions()
|
||||
actions.downloadAssets([output, imported])
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(mockCreateAssetExport).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
const payload = mockCreateAssetExport.mock.calls[0][0]
|
||||
expect(payload.job_ids).toEqual(['history-id'])
|
||||
expect(payload.asset_ids).toEqual(['input-id'])
|
||||
})
|
||||
|
||||
it('should only include a filtered output name once', async () => {
|
||||
const asset1 = createOutputAsset('a1', 'same.png', 'job1')
|
||||
const asset2 = createOutputAsset('a2', 'same.png', 'job1')
|
||||
|
||||
const actions = useMediaAssetActions()
|
||||
actions.downloadAssets([asset1, asset2])
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(mockCreateAssetExport).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
const payload = mockCreateAssetExport.mock.calls[0][0]
|
||||
expect(payload.job_asset_name_filters).toEqual({
|
||||
job1: ['same.png']
|
||||
})
|
||||
})
|
||||
|
||||
it('should show an error toast when ZIP export creation fails', async () => {
|
||||
mockCreateAssetExport.mockRejectedValueOnce(new Error('export failed'))
|
||||
const asset1 = createOutputAsset('a1', 'img1.png', 'job1')
|
||||
const asset2 = createOutputAsset('a2', 'img2.png', 'job2')
|
||||
|
||||
const actions = useMediaAssetActions()
|
||||
actions.downloadAssets([asset1, asset2])
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'error' })
|
||||
)
|
||||
})
|
||||
expect(mockTrackExport).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('downloadAssets - export toast file count', () => {
|
||||
@@ -1033,6 +1582,200 @@ describe('useMediaAssetActions', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('deleteAssets', () => {
|
||||
it('returns false for an empty selection', async () => {
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
const result = await actions.deleteAssets([])
|
||||
|
||||
expect(result).toBe(false)
|
||||
expect(mockShowDialog).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('returns false when the user cancels', async () => {
|
||||
mockShowDialog.mockImplementation(
|
||||
({ props }: { props: { onCancel: () => void } }) => {
|
||||
props.onCancel()
|
||||
}
|
||||
)
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
const result = await actions.deleteAssets(createMockAsset())
|
||||
|
||||
expect(result).toBe(false)
|
||||
expect(mockDeleteAsset).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects imported asset deletion outside cloud mode', async () => {
|
||||
mockIsCloud.value = false
|
||||
mockGetAssetType.mockReturnValue('input')
|
||||
mockShowDialog.mockImplementation(
|
||||
({ props }: { props: { onConfirm: () => Promise<void> } }) => {
|
||||
void props.onConfirm()
|
||||
}
|
||||
)
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.deleteAssets(createMockAsset({ tags: ['input'] }))
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'error' })
|
||||
)
|
||||
})
|
||||
expect(mockDeleteAsset).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects output deletion when no job id can be resolved', async () => {
|
||||
mockIsCloud.value = true
|
||||
mockGetAssetType.mockReturnValue('output')
|
||||
mockShowDialog.mockImplementation(
|
||||
({ props }: { props: { onConfirm: () => Promise<void> } }) => {
|
||||
void props.onConfirm()
|
||||
}
|
||||
)
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.deleteAssets(
|
||||
createMockAsset({ id: '', name: 'orphan.png', tags: ['output'] })
|
||||
)
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'error' })
|
||||
)
|
||||
})
|
||||
expect(api.deleteItem).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('updates output history and input listings for mixed successful deletion', async () => {
|
||||
mockIsCloud.value = true
|
||||
mockGetAssetType.mockImplementation((asset: AssetItem) =>
|
||||
asset.tags?.includes('output') ? 'output' : 'input'
|
||||
)
|
||||
mockShowDialog.mockImplementation(
|
||||
({ props }: { props: { onConfirm: () => Promise<void> } }) => {
|
||||
void props.onConfirm()
|
||||
}
|
||||
)
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.deleteAssets([
|
||||
createMockAsset({ id: 'history-1', tags: ['output'] }),
|
||||
createMockAsset({ id: 'input-1', tags: ['input'] })
|
||||
])
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(mockUpdateHistory).toHaveBeenCalled()
|
||||
})
|
||||
expect(mockUpdateInputs).toHaveBeenCalled()
|
||||
expect(api.deleteItem).toHaveBeenCalledWith('history', 'history-1')
|
||||
expect(mockDeleteAsset).toHaveBeenCalledWith('input-1')
|
||||
})
|
||||
|
||||
it('skips graph cleanup when there is no root graph', async () => {
|
||||
mockIsCloud.value = true
|
||||
mockGetAssetType.mockReturnValue('input')
|
||||
mockAppGraph.value = null
|
||||
mockShowDialog.mockImplementation(
|
||||
({ props }: { props: { onConfirm: () => Promise<void> } }) => {
|
||||
void props.onConfirm()
|
||||
}
|
||||
)
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.deleteAssets(createMockAsset({ tags: ['input'] }))
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(mockDeleteAsset).toHaveBeenCalled()
|
||||
})
|
||||
expect(mockClearNodePreviewCache).not.toHaveBeenCalled()
|
||||
expect(mockClearWidgetValues).not.toHaveBeenCalled()
|
||||
expect(mockCaptureCanvasState).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('uses temp widget-value variants when deleting temp assets', async () => {
|
||||
mockIsCloud.value = true
|
||||
mockGetAssetType.mockReturnValue('temp')
|
||||
mockShowDialog.mockImplementation(
|
||||
({ props }: { props: { onConfirm: () => Promise<void> } }) => {
|
||||
void props.onConfirm()
|
||||
}
|
||||
)
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.deleteAssets(
|
||||
createMockAsset({
|
||||
id: 'temp-1',
|
||||
name: 'preview.png',
|
||||
hash: 'preview-hash.png',
|
||||
tags: ['temp']
|
||||
})
|
||||
)
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(mockClearNodePreviewCache).toHaveBeenCalled()
|
||||
})
|
||||
const [, valuesArg] = mockClearNodePreviewCache.mock.calls[0]
|
||||
expect(valuesArg).toEqual(
|
||||
new Set(['preview.png [temp]', 'preview-hash.png'])
|
||||
)
|
||||
})
|
||||
|
||||
it('uses hash-only cleanup values when the asset name is empty', async () => {
|
||||
mockIsCloud.value = true
|
||||
mockGetAssetType.mockReturnValue('input')
|
||||
mockShowDialog.mockImplementation(
|
||||
({ props }: { props: { onConfirm: () => Promise<void> } }) => {
|
||||
void props.onConfirm()
|
||||
}
|
||||
)
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.deleteAssets(
|
||||
createMockAsset({
|
||||
id: 'hash-only',
|
||||
name: '',
|
||||
hash: 'only-hash.png',
|
||||
tags: ['input']
|
||||
})
|
||||
)
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(mockClearNodePreviewCache).toHaveBeenCalled()
|
||||
})
|
||||
const [, valuesArg] = mockClearNodePreviewCache.mock.calls[0]
|
||||
expect(valuesArg).toEqual(new Set(['only-hash.png']))
|
||||
})
|
||||
|
||||
it('shows a partial warning and cleans up only successfully deleted assets', async () => {
|
||||
mockIsCloud.value = true
|
||||
mockGetAssetType.mockReturnValue('input')
|
||||
mockDeleteAsset
|
||||
.mockResolvedValueOnce(undefined)
|
||||
.mockRejectedValueOnce(new Error('delete failed'))
|
||||
mockShowDialog.mockImplementation(
|
||||
({ props }: { props: { onConfirm: () => Promise<void> } }) => {
|
||||
void props.onConfirm()
|
||||
}
|
||||
)
|
||||
const actions = useMediaAssetActions()
|
||||
|
||||
await actions.deleteAssets([
|
||||
createMockAsset({ id: 'ok', name: 'ok.png', tags: ['input'] }),
|
||||
createMockAsset({ id: 'bad', name: 'bad.png', tags: ['input'] })
|
||||
])
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(useToast().add).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ severity: 'warn' })
|
||||
)
|
||||
})
|
||||
const [, valuesArg] = mockClearNodePreviewCache.mock.calls[0]
|
||||
expect(valuesArg).toEqual(new Set(['ok.png', 'ok.png [input]']))
|
||||
})
|
||||
})
|
||||
|
||||
describe('deleteAssets - model cache invalidation', () => {
|
||||
beforeEach(() => {
|
||||
mockIsCloud.value = true
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { createTestingPinia } from '@pinia/testing'
|
||||
import { fromPartial } from '@total-typescript/shoehorn'
|
||||
import { setActivePinia } from 'pinia'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createApp, nextTick, ref } from 'vue'
|
||||
@@ -14,6 +15,7 @@ vi.mock('@/platform/assets/services/assetService', () => ({
|
||||
assetService: {
|
||||
getAssetMetadata: vi.fn(),
|
||||
uploadAssetAsync: vi.fn(),
|
||||
uploadAssetFromBase64: vi.fn(),
|
||||
uploadAssetPreviewImage: vi.fn()
|
||||
}
|
||||
}))
|
||||
@@ -248,6 +250,81 @@ describe('useUploadModelWizard', () => {
|
||||
expect(wizard.selectedModelType.value).toBe('checkpoints')
|
||||
})
|
||||
|
||||
it('does not fetch metadata until the URL matches a supported source', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
|
||||
expect(wizard.canFetchMetadata.value).toBe(false)
|
||||
await wizard.fetchMetadata()
|
||||
|
||||
expect(assetService.getAssetMetadata).not.toHaveBeenCalled()
|
||||
expect(wizard.currentStep.value).toBe(1)
|
||||
})
|
||||
|
||||
it('decodes metadata filenames and selects a matching model type tag', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
vi.mocked(assetService.getAssetMetadata).mockResolvedValue({
|
||||
content_length: 100,
|
||||
final_url: 'https://huggingface.co/org/model',
|
||||
filename: '%E6%A8%A1%E5%9E%8B.safetensors',
|
||||
name: '%E5%90%8D%E7%A8%B1',
|
||||
tags: ['checkpoints'],
|
||||
preview_image: 'data:image/png;base64,abc'
|
||||
})
|
||||
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
wizard.wizardData.value.url = ' https://huggingface.co/org/model '
|
||||
|
||||
await wizard.fetchMetadata()
|
||||
|
||||
expect(wizard.currentStep.value).toBe(2)
|
||||
expect(wizard.wizardData.value.url).toBe('https://huggingface.co/org/model')
|
||||
expect(wizard.wizardData.value.name).toBe('模型.safetensors')
|
||||
expect(wizard.wizardData.value.previewImage).toBe(
|
||||
'data:image/png;base64,abc'
|
||||
)
|
||||
expect(wizard.selectedModelType.value).toBe('checkpoints')
|
||||
})
|
||||
|
||||
it('keeps metadata text when percent decoding fails', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
vi.mocked(assetService.getAssetMetadata).mockResolvedValue({
|
||||
content_length: 100,
|
||||
final_url: 'https://civitai.com/models/12345',
|
||||
filename: '%E0%A4%A',
|
||||
name: '%E0%A4%A',
|
||||
tags: []
|
||||
})
|
||||
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
wizard.wizardData.value.url = 'https://civitai.com/models/12345'
|
||||
|
||||
await wizard.fetchMetadata()
|
||||
|
||||
expect(wizard.currentStep.value).toBe(2)
|
||||
expect(wizard.wizardData.value.name).toBe('%E0%A4%A')
|
||||
expect(wizard.selectedModelType.value).toBeUndefined()
|
||||
})
|
||||
|
||||
it('uses the fallback metadata error for non-error rejections', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
vi.mocked(assetService.getAssetMetadata).mockRejectedValue('no metadata')
|
||||
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
wizard.wizardData.value.url = 'https://civitai.com/models/12345'
|
||||
|
||||
await wizard.fetchMetadata()
|
||||
|
||||
expect(wizard.currentStep.value).toBe(1)
|
||||
expect(wizard.uploadError.value).toBe(
|
||||
'Failed to retrieve metadata. Please check the link and try again.'
|
||||
)
|
||||
})
|
||||
|
||||
it('uploads with the required model type even if selection changes', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
@@ -279,6 +356,382 @@ describe('useUploadModelWizard', () => {
|
||||
expect(result?.modelType).toBe('checkpoints')
|
||||
})
|
||||
|
||||
it('clears upload errors and type mismatches when the URL changes', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
vi.mocked(assetService.uploadAssetAsync).mockResolvedValue({
|
||||
type: 'sync',
|
||||
asset: {
|
||||
id: 'asset-lora',
|
||||
name: 'model.safetensors',
|
||||
tags: ['models', 'loras']
|
||||
}
|
||||
})
|
||||
|
||||
const wizard = setupUploadModelWizard(
|
||||
ref([
|
||||
{ name: 'Checkpoint', value: 'checkpoints' },
|
||||
{ name: 'LoRA', value: 'loras' }
|
||||
]),
|
||||
{ requiredModelType: 'checkpoints' }
|
||||
)
|
||||
wizard.wizardData.value.url = 'https://civitai.com/models/12345'
|
||||
await wizard.uploadModel()
|
||||
|
||||
expect(wizard.uploadTypeMismatch.value).not.toBeNull()
|
||||
|
||||
wizard.wizardData.value.url = 'https://civitai.com/models/54321'
|
||||
await nextTick()
|
||||
|
||||
expect(wizard.uploadError.value).toBe('')
|
||||
expect(wizard.uploadTypeMismatch.value).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null while another upload is in progress', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
type UploadResult = Awaited<
|
||||
ReturnType<typeof assetService.uploadAssetAsync>
|
||||
>
|
||||
let resolveUpload!: (value: UploadResult) => void
|
||||
vi.mocked(assetService.uploadAssetAsync).mockReturnValue(
|
||||
new Promise<UploadResult>((resolve) => {
|
||||
resolveUpload = resolve
|
||||
})
|
||||
)
|
||||
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
wizard.wizardData.value.url = 'https://civitai.com/models/12345'
|
||||
wizard.selectedModelType.value = 'checkpoints'
|
||||
|
||||
const firstUpload = wizard.uploadModel()
|
||||
await nextTick()
|
||||
|
||||
await expect(wizard.uploadModel()).resolves.toBeNull()
|
||||
|
||||
resolveUpload({
|
||||
type: 'sync',
|
||||
asset: {
|
||||
id: 'asset-1',
|
||||
name: 'model.safetensors',
|
||||
tags: ['models', 'checkpoints']
|
||||
}
|
||||
})
|
||||
|
||||
await expect(firstUpload).resolves.toEqual(
|
||||
expect.objectContaining({ status: 'success' })
|
||||
)
|
||||
})
|
||||
|
||||
it('returns null when no model type is selected', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
wizard.wizardData.value.url = 'https://civitai.com/models/12345'
|
||||
|
||||
const result = await wizard.uploadModel()
|
||||
|
||||
expect(result).toBeNull()
|
||||
expect(assetService.uploadAssetAsync).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('reports an upload error when no valid source is detected', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
wizard.wizardData.value.url = 'https://example.com/model'
|
||||
wizard.selectedModelType.value = 'checkpoints'
|
||||
|
||||
const result = await wizard.uploadModel()
|
||||
|
||||
expect(result).toBeNull()
|
||||
expect(assetService.uploadAssetAsync).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('uploads preview images and passes the preview id to the model upload', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
vi.mocked(assetService.uploadAssetFromBase64).mockResolvedValue(
|
||||
fromPartial({ id: 'preview-1' })
|
||||
)
|
||||
vi.mocked(assetService.uploadAssetAsync).mockResolvedValue({
|
||||
type: 'sync',
|
||||
asset: {
|
||||
id: 'asset-1',
|
||||
name: 'model.safetensors',
|
||||
tags: ['models', 'checkpoints']
|
||||
}
|
||||
})
|
||||
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
wizard.wizardData.value.url = 'https://civitai.com/models/12345'
|
||||
wizard.wizardData.value.metadata = {
|
||||
content_length: 100,
|
||||
final_url: 'https://civitai.com/models/12345',
|
||||
filename: 'model.safetensors'
|
||||
}
|
||||
wizard.wizardData.value.previewImage = 'data:image/jpeg;base64,abc'
|
||||
wizard.selectedModelType.value = 'checkpoints'
|
||||
|
||||
await wizard.uploadModel()
|
||||
|
||||
expect(assetService.uploadAssetFromBase64).toHaveBeenCalledWith({
|
||||
data: 'data:image/jpeg;base64,abc',
|
||||
name: 'model_preview.jpg',
|
||||
tags: ['preview']
|
||||
})
|
||||
expect(assetService.uploadAssetAsync).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ preview_id: 'preview-1' })
|
||||
)
|
||||
})
|
||||
|
||||
it('continues model upload when preview upload fails', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
vi.mocked(assetService.uploadAssetFromBase64).mockRejectedValue(
|
||||
new Error('preview failed')
|
||||
)
|
||||
vi.mocked(assetService.uploadAssetAsync).mockResolvedValue({
|
||||
type: 'sync',
|
||||
asset: {
|
||||
id: 'asset-1',
|
||||
name: 'model.safetensors',
|
||||
tags: ['models', 'checkpoints']
|
||||
}
|
||||
})
|
||||
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
wizard.wizardData.value.url = 'https://civitai.com/models/12345'
|
||||
wizard.wizardData.value.metadata = {
|
||||
content_length: 100,
|
||||
final_url: 'https://civitai.com/models/12345',
|
||||
name: 'model'
|
||||
}
|
||||
wizard.wizardData.value.previewImage = 'data:image/webp;base64,abc'
|
||||
wizard.selectedModelType.value = 'checkpoints'
|
||||
|
||||
await wizard.uploadModel()
|
||||
|
||||
expect(assetService.uploadAssetAsync).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ preview_id: undefined })
|
||||
)
|
||||
expect(wizard.uploadStatus.value).toBe('success')
|
||||
})
|
||||
|
||||
it('treats an already completed async upload as success', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
vi.mocked(assetService.uploadAssetAsync).mockResolvedValue({
|
||||
type: 'async',
|
||||
task: {
|
||||
task_id: 'task-complete',
|
||||
status: 'completed',
|
||||
message: 'Download complete'
|
||||
}
|
||||
})
|
||||
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
wizard.wizardData.value.url = 'https://civitai.com/models/12345'
|
||||
wizard.wizardData.value.metadata = {
|
||||
content_length: 100,
|
||||
final_url: 'https://civitai.com/models/12345',
|
||||
filename: 'queued.safetensors'
|
||||
}
|
||||
wizard.selectedModelType.value = 'checkpoints'
|
||||
|
||||
const result = await wizard.uploadModel()
|
||||
|
||||
expect(result).toEqual({
|
||||
filename: 'queued.safetensors',
|
||||
modelType: 'checkpoints',
|
||||
status: 'success'
|
||||
})
|
||||
expect(wizard.uploadStatus.value).toBe('success')
|
||||
})
|
||||
|
||||
it('cleans up an immediately resolved async watcher', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
const { useAssetDownloadStore } =
|
||||
await import('@/stores/assetDownloadStore')
|
||||
const assetDownloadStore = useAssetDownloadStore()
|
||||
assetDownloadStore.trackDownload(
|
||||
'task-ready',
|
||||
'checkpoints',
|
||||
'ready.safetensors'
|
||||
)
|
||||
const { api } = await import('@/scripts/api')
|
||||
const handler = vi
|
||||
.mocked(api.addEventListener)
|
||||
.mock.calls.find((c) => c[0] === 'asset_download')?.[1] as
|
||||
| ((e: CustomEvent) => void)
|
||||
| undefined
|
||||
expect(handler).toBeDefined()
|
||||
handler!(
|
||||
new CustomEvent('asset_download', {
|
||||
detail: {
|
||||
task_id: 'task-ready',
|
||||
asset_id: 'asset-ready',
|
||||
asset_name: 'ready.safetensors',
|
||||
bytes_total: 100,
|
||||
bytes_downloaded: 100,
|
||||
progress: 100,
|
||||
status: 'completed' as const
|
||||
}
|
||||
})
|
||||
)
|
||||
vi.mocked(assetService.uploadAssetAsync).mockResolvedValue({
|
||||
type: 'async',
|
||||
task: {
|
||||
task_id: 'task-ready',
|
||||
status: 'created',
|
||||
message: 'Download queued'
|
||||
}
|
||||
})
|
||||
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
wizard.wizardData.value.url = 'https://civitai.com/models/12345'
|
||||
wizard.selectedModelType.value = 'checkpoints'
|
||||
|
||||
await wizard.uploadModel()
|
||||
await nextTick()
|
||||
|
||||
expect(wizard.uploadStatus.value).toBe('success')
|
||||
})
|
||||
|
||||
it('uses the default failed-download message when no error is available', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
vi.mocked(assetService.uploadAssetAsync).mockResolvedValue({
|
||||
type: 'async',
|
||||
task: {
|
||||
task_id: 'task-fallback-fail',
|
||||
status: 'created',
|
||||
message: 'Download queued'
|
||||
}
|
||||
})
|
||||
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
wizard.wizardData.value.url = 'https://civitai.com/models/12345'
|
||||
wizard.selectedModelType.value = 'checkpoints'
|
||||
|
||||
await wizard.uploadModel()
|
||||
|
||||
const { api } = await import('@/scripts/api')
|
||||
const handler = vi
|
||||
.mocked(api.addEventListener)
|
||||
.mock.calls.find((c) => c[0] === 'asset_download')?.[1] as
|
||||
| ((e: CustomEvent) => void)
|
||||
| undefined
|
||||
expect(handler).toBeDefined()
|
||||
handler!(
|
||||
new CustomEvent('asset_download', {
|
||||
detail: {
|
||||
task_id: 'task-fallback-fail',
|
||||
asset_id: '',
|
||||
asset_name: '',
|
||||
bytes_total: 1000,
|
||||
bytes_downloaded: 500,
|
||||
progress: 50,
|
||||
status: 'failed' as const
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
await nextTick()
|
||||
|
||||
expect(wizard.uploadStatus.value).toBe('error')
|
||||
expect(wizard.uploadError.value).toBe('assetBrowser.downloadFailed')
|
||||
})
|
||||
|
||||
it('uses fallback labels for unknown mismatch types', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
vi.mocked(assetService.uploadAssetAsync).mockResolvedValue({
|
||||
type: 'sync',
|
||||
asset: {
|
||||
id: 'asset-unknown',
|
||||
name: 'model.safetensors',
|
||||
tags: ['models']
|
||||
}
|
||||
})
|
||||
|
||||
const wizard = setupUploadModelWizard(modelTypes, {
|
||||
requiredModelType: 'unknown-required'
|
||||
})
|
||||
wizard.wizardData.value.url = 'https://civitai.com/models/12345'
|
||||
|
||||
const result = await wizard.uploadModel()
|
||||
|
||||
expect(result).toBeNull()
|
||||
expect(wizard.uploadTypeMismatch.value).toEqual({
|
||||
importedModelType: undefined,
|
||||
importedModelTypeLabel: undefined,
|
||||
requiredModelType: 'unknown-required',
|
||||
requiredModelTypeLabel: 'unknown-required'
|
||||
})
|
||||
})
|
||||
|
||||
it('uses a generic upload error for non-error upload failures', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
vi.mocked(assetService.uploadAssetAsync).mockRejectedValue('failed')
|
||||
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
wizard.wizardData.value.url = 'https://civitai.com/models/12345'
|
||||
wizard.selectedModelType.value = 'checkpoints'
|
||||
|
||||
const result = await wizard.uploadModel()
|
||||
|
||||
expect(result).toBeNull()
|
||||
expect(wizard.uploadStatus.value).toBe('error')
|
||||
expect(wizard.uploadError.value).toBe('Failed to upload model')
|
||||
})
|
||||
|
||||
it('navigates backward only after the first step', () => {
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
|
||||
wizard.goToPreviousStep()
|
||||
expect(wizard.currentStep.value).toBe(1)
|
||||
|
||||
wizard.currentStep.value = 3
|
||||
wizard.goToPreviousStep()
|
||||
|
||||
expect(wizard.currentStep.value).toBe(2)
|
||||
})
|
||||
|
||||
it('resets wizard state and cancels pending async status watching', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
vi.mocked(assetService.uploadAssetAsync).mockResolvedValue({
|
||||
type: 'async',
|
||||
task: {
|
||||
task_id: 'task-reset',
|
||||
status: 'created',
|
||||
message: 'Download queued'
|
||||
}
|
||||
})
|
||||
|
||||
const wizard = setupUploadModelWizard(modelTypes)
|
||||
wizard.wizardData.value.url = 'https://civitai.com/models/12345'
|
||||
wizard.wizardData.value.name = 'Model'
|
||||
wizard.wizardData.value.tags = ['checkpoints']
|
||||
wizard.selectedModelType.value = 'checkpoints'
|
||||
|
||||
await wizard.uploadModel()
|
||||
wizard.resetWizard()
|
||||
|
||||
expect(wizard.currentStep.value).toBe(1)
|
||||
expect(wizard.uploadStatus.value).toBeUndefined()
|
||||
expect(wizard.uploadError.value).toBe('')
|
||||
expect(wizard.wizardData.value).toEqual({
|
||||
url: '',
|
||||
name: '',
|
||||
tags: []
|
||||
})
|
||||
expect(wizard.selectedModelType.value).toBeUndefined()
|
||||
})
|
||||
|
||||
it('returns the synced asset filename for sync imports', async () => {
|
||||
const { assetService } =
|
||||
await import('@/platform/assets/services/assetService')
|
||||
|
||||
@@ -12,6 +12,7 @@ import { api } from '@/scripts/api'
|
||||
|
||||
const mockDistributionState = vi.hoisted(() => ({ isCloud: false }))
|
||||
const mockSettingStoreGet = vi.hoisted(() => vi.fn(() => false))
|
||||
const mockGetCategoryForNodeType = vi.hoisted(() => vi.fn())
|
||||
|
||||
vi.mock('@/platform/distribution/types', () => ({
|
||||
get isCloud() {
|
||||
@@ -33,7 +34,7 @@ vi.mock('@/stores/modelToNodeStore', () => {
|
||||
return {
|
||||
useModelToNodeStore: vi.fn(() => ({
|
||||
getRegisteredNodeTypes: () => registeredNodeTypes,
|
||||
getCategoryForNodeType: vi.fn()
|
||||
getCategoryForNodeType: mockGetCategoryForNodeType
|
||||
}))
|
||||
}
|
||||
})
|
||||
@@ -172,6 +173,28 @@ describe(assetService.getAssetMetadata, () => {
|
||||
).rejects.toThrow('File too large')
|
||||
})
|
||||
|
||||
it('falls back to the unknown localized message for unrecognized error codes', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(
|
||||
buildResponse({ code: 'NOT_A_REAL_CODE' }, { ok: false, status: 400 })
|
||||
)
|
||||
|
||||
await expect(
|
||||
assetService.getAssetMetadata('https://example.com/model.safetensors')
|
||||
).rejects.toThrow('Unknown error')
|
||||
})
|
||||
|
||||
it('falls back to unknown when error JSON cannot be parsed', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
status: 400,
|
||||
json: vi.fn().mockRejectedValue(new Error('bad json'))
|
||||
} as unknown as Response)
|
||||
|
||||
await expect(
|
||||
assetService.getAssetMetadata('https://example.com/model.safetensors')
|
||||
).rejects.toThrow('Unknown error')
|
||||
})
|
||||
|
||||
it('throws a localized message when validation reports is_valid=false', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(
|
||||
buildResponse({
|
||||
@@ -189,6 +212,20 @@ describe(assetService.getAssetMetadata, () => {
|
||||
).rejects.toThrow('Unsafe virus scan')
|
||||
})
|
||||
|
||||
it('falls back to unknown when validation errors are absent', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(
|
||||
buildResponse({
|
||||
content_length: 100,
|
||||
final_url: 'https://example.com/model.safetensors',
|
||||
validation: { is_valid: false }
|
||||
})
|
||||
)
|
||||
|
||||
await expect(
|
||||
assetService.getAssetMetadata('https://example.com/model.safetensors')
|
||||
).rejects.toThrow('Unknown error')
|
||||
})
|
||||
|
||||
it('encodes the URL in the query string', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(
|
||||
buildResponse({
|
||||
@@ -208,12 +245,115 @@ describe(assetService.getAssetMetadata, () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe(assetService.getAssetsForNodeType, () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockGetCategoryForNodeType.mockReset()
|
||||
})
|
||||
|
||||
it('returns an empty list for invalid node types without fetching', async () => {
|
||||
await expect(assetService.getAssetsForNodeType('')).resolves.toEqual([])
|
||||
|
||||
expect(fetchApiMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('returns an empty list when the node type has no asset category', async () => {
|
||||
mockGetCategoryForNodeType.mockReturnValue(undefined)
|
||||
|
||||
await expect(
|
||||
assetService.getAssetsForNodeType('UnknownNode')
|
||||
).resolves.toEqual([])
|
||||
|
||||
expect(fetchApiMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('fetches category assets with default pagination', async () => {
|
||||
mockGetCategoryForNodeType.mockReturnValue('checkpoints')
|
||||
const assets = [
|
||||
validAsset({ id: 'ckpt-1', tags: ['models', 'checkpoints'] })
|
||||
]
|
||||
fetchApiMock.mockResolvedValueOnce(buildAssetListResponse(assets))
|
||||
|
||||
await expect(
|
||||
assetService.getAssetsForNodeType('CheckpointLoaderSimple')
|
||||
).resolves.toEqual(assets)
|
||||
|
||||
const requestedUrl = fetchApiMock.mock.calls[0]?.[0] as string
|
||||
const params = new URL(requestedUrl, 'http://localhost').searchParams
|
||||
expect(params.get('include_tags')).toBe('models,checkpoints')
|
||||
expect(params.get('limit')).toBe('500')
|
||||
expect(params.has('offset')).toBe(false)
|
||||
})
|
||||
|
||||
it('passes positive offsets for category asset pagination', async () => {
|
||||
mockGetCategoryForNodeType.mockReturnValue('loras')
|
||||
fetchApiMock.mockResolvedValueOnce(buildAssetListResponse([]))
|
||||
|
||||
await assetService.getAssetsForNodeType('LoraLoader', {
|
||||
limit: 25,
|
||||
offset: 50
|
||||
})
|
||||
|
||||
const requestedUrl = fetchApiMock.mock.calls[0]?.[0] as string
|
||||
const params = new URL(requestedUrl, 'http://localhost').searchParams
|
||||
expect(params.get('include_tags')).toBe('models,loras')
|
||||
expect(params.get('limit')).toBe('25')
|
||||
expect(params.get('offset')).toBe('50')
|
||||
})
|
||||
})
|
||||
|
||||
describe(assetService.getAssetDetails, () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('throws when the details response is not ok', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(
|
||||
buildResponse({}, { ok: false, status: 404 })
|
||||
)
|
||||
|
||||
await expect(assetService.getAssetDetails('missing')).rejects.toThrow(
|
||||
'Unable to load asset details for missing: Server returned 404'
|
||||
)
|
||||
})
|
||||
|
||||
it('throws when the details response is invalid', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(buildResponse({ id: 'asset-1' }))
|
||||
|
||||
await expect(assetService.getAssetDetails('asset-1')).rejects.toThrow(
|
||||
/Invalid asset response/
|
||||
)
|
||||
})
|
||||
|
||||
it('returns validated asset details', async () => {
|
||||
const asset = validAsset({ id: 'asset-details' })
|
||||
fetchApiMock.mockResolvedValueOnce(buildResponse(asset))
|
||||
|
||||
await expect(
|
||||
assetService.getAssetDetails('asset-details')
|
||||
).resolves.toEqual(asset)
|
||||
})
|
||||
})
|
||||
|
||||
describe(assetService.uploadAssetFromUrl, () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
assetService.invalidateInputAssetsIncludingPublic()
|
||||
})
|
||||
|
||||
it('throws when URL upload returns a non-ok response', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(
|
||||
buildResponse(null, { ok: false, status: 500 })
|
||||
)
|
||||
|
||||
await expect(
|
||||
assetService.uploadAssetFromUrl({
|
||||
url: 'https://example.com/input.png',
|
||||
name: 'input.png'
|
||||
})
|
||||
).rejects.toThrow('Failed to upload asset')
|
||||
})
|
||||
|
||||
it('does not invalidate cached input assets when the upload response is invalid', async () => {
|
||||
const staleAssets = [validAsset({ id: 'stale-input', tags: ['input'] })]
|
||||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
@@ -294,6 +434,61 @@ describe(assetService.uploadAssetFromBase64, () => {
|
||||
expect(fetchApiMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('throws when base64 upload returns a non-ok response', async () => {
|
||||
const fetchSpy = vi
|
||||
.spyOn(globalThis, 'fetch')
|
||||
.mockResolvedValueOnce(new Response('hello'))
|
||||
try {
|
||||
fetchApiMock.mockResolvedValueOnce(
|
||||
buildResponse(null, { ok: false, status: 507 })
|
||||
)
|
||||
|
||||
await expect(
|
||||
assetService.uploadAssetFromBase64({
|
||||
data: 'data:text/plain;base64,aGVsbG8=',
|
||||
name: 'input.txt'
|
||||
})
|
||||
).rejects.toThrow('Failed to upload asset from base64: 507')
|
||||
} finally {
|
||||
fetchSpy.mockRestore()
|
||||
}
|
||||
})
|
||||
|
||||
it('posts base64 uploads with tags and user metadata', async () => {
|
||||
const uploadedAsset = {
|
||||
...validAsset({ id: 'uploaded-input', tags: ['input'] }),
|
||||
created_new: false
|
||||
}
|
||||
const fetchSpy = vi
|
||||
.spyOn(globalThis, 'fetch')
|
||||
.mockResolvedValueOnce(new Response('hello'))
|
||||
try {
|
||||
fetchApiMock.mockResolvedValueOnce(buildResponse(uploadedAsset))
|
||||
|
||||
const result = await assetService.uploadAssetFromBase64({
|
||||
data: 'data:text/plain;base64,aGVsbG8=',
|
||||
name: 'input.txt',
|
||||
tags: ['input', 'mask'],
|
||||
user_metadata: { source: 'paste' }
|
||||
})
|
||||
|
||||
expect(result).toEqual(uploadedAsset)
|
||||
const request = fetchApiMock.mock.calls[0]?.[1]
|
||||
expect(request).toEqual(expect.objectContaining({ method: 'POST' }))
|
||||
expect(request?.body).toBeInstanceOf(FormData)
|
||||
const formData = request?.body
|
||||
if (!(formData instanceof FormData)) {
|
||||
throw new Error('Expected base64 upload body to be FormData')
|
||||
}
|
||||
expect(formData.get('tags')).toBe(JSON.stringify(['input', 'mask']))
|
||||
expect(formData.get('user_metadata')).toBe(
|
||||
JSON.stringify({ source: 'paste' })
|
||||
)
|
||||
} finally {
|
||||
fetchSpy.mockRestore()
|
||||
}
|
||||
})
|
||||
|
||||
it('does not invalidate cached input assets when the upload response is invalid', async () => {
|
||||
const staleAssets = [validAsset({ id: 'stale-input', tags: ['input'] })]
|
||||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
@@ -355,6 +550,7 @@ describe(assetService.uploadAssetFromBase64, () => {
|
||||
describe(assetService.uploadAssetAsync, () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
assetService.invalidateInputAssetsIncludingPublic()
|
||||
})
|
||||
|
||||
it('returns an async result when the server responds 202', async () => {
|
||||
@@ -389,6 +585,64 @@ describe(assetService.uploadAssetAsync, () => {
|
||||
asset: expect.objectContaining({ id: 'asset-2' })
|
||||
})
|
||||
})
|
||||
|
||||
it('throws when the async upload response is not ok', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(
|
||||
buildResponse(null, { ok: false, status: 502 })
|
||||
)
|
||||
|
||||
await expect(
|
||||
assetService.uploadAssetAsync({
|
||||
source_url: 'https://example.com/model.safetensors'
|
||||
})
|
||||
).rejects.toThrow('Failed to upload asset')
|
||||
})
|
||||
|
||||
it('throws when an async upload task response is invalid', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(
|
||||
buildResponse({ task_id: 'task-1', status: 'waiting' }, { status: 202 })
|
||||
)
|
||||
|
||||
await expect(
|
||||
assetService.uploadAssetAsync({
|
||||
source_url: 'https://example.com/model.safetensors'
|
||||
})
|
||||
).rejects.toThrow('Failed to parse async upload response')
|
||||
})
|
||||
|
||||
it('throws when a sync upload asset response is invalid', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(buildResponse({ id: 'asset-2' }))
|
||||
|
||||
await expect(
|
||||
assetService.uploadAssetAsync({
|
||||
source_url: 'https://example.com/model.safetensors'
|
||||
})
|
||||
).rejects.toThrow('Failed to parse sync upload response')
|
||||
})
|
||||
|
||||
it('invalidates cached input assets for completed async input uploads', async () => {
|
||||
const staleAssets = [validAsset({ id: 'stale-input', tags: ['input'] })]
|
||||
const freshAssets = [validAsset({ id: 'fresh-input', tags: ['input'] })]
|
||||
fetchApiMock
|
||||
.mockResolvedValueOnce(buildAssetListResponse(staleAssets))
|
||||
.mockResolvedValueOnce(
|
||||
buildResponse(
|
||||
{ task_id: 'task-1', status: 'completed' },
|
||||
{ ok: true, status: 202 }
|
||||
)
|
||||
)
|
||||
.mockResolvedValueOnce(buildAssetListResponse(freshAssets))
|
||||
|
||||
await assetService.getInputAssetsIncludingPublic()
|
||||
await assetService.uploadAssetAsync({
|
||||
source_url: 'https://example.com/input.png',
|
||||
tags: ['input']
|
||||
})
|
||||
const refreshed = await assetService.getInputAssetsIncludingPublic()
|
||||
|
||||
expect(refreshed).toEqual(freshAssets)
|
||||
expect(fetchApiMock).toHaveBeenCalledTimes(3)
|
||||
})
|
||||
})
|
||||
|
||||
describe(assetService.deleteAsset, () => {
|
||||
@@ -416,6 +670,94 @@ describe(assetService.deleteAsset, () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe(assetService.addAssetTags, () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
assetService.invalidateInputAssetsIncludingPublic()
|
||||
})
|
||||
|
||||
it('posts tags and returns the parsed tag operation result', async () => {
|
||||
const result = { total_tags: ['input', 'mask'], added: ['mask'] }
|
||||
fetchApiMock.mockResolvedValueOnce(buildResponse(result))
|
||||
|
||||
await expect(
|
||||
assetService.addAssetTags('asset-1', ['mask'])
|
||||
).resolves.toEqual(result)
|
||||
|
||||
expect(fetchApiMock).toHaveBeenCalledWith(
|
||||
'/assets/asset-1/tags',
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ tags: ['mask'] })
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('throws when adding tags fails', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(
|
||||
buildResponse(null, { ok: false, status: 403 })
|
||||
)
|
||||
|
||||
await expect(
|
||||
assetService.addAssetTags('asset-1', ['mask'])
|
||||
).rejects.toThrow(
|
||||
'Unable to add tags to asset asset-1: Server returned 403'
|
||||
)
|
||||
})
|
||||
|
||||
it('throws when the add-tags response is invalid', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(buildResponse({ added: ['mask'] }))
|
||||
|
||||
await expect(
|
||||
assetService.addAssetTags('asset-1', ['mask'])
|
||||
).rejects.toThrow()
|
||||
})
|
||||
})
|
||||
|
||||
describe(assetService.removeAssetTags, () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
assetService.invalidateInputAssetsIncludingPublic()
|
||||
})
|
||||
|
||||
it('deletes tags and returns the parsed tag operation result', async () => {
|
||||
const result = { total_tags: ['input'], removed: ['mask'] }
|
||||
fetchApiMock.mockResolvedValueOnce(buildResponse(result))
|
||||
|
||||
await expect(
|
||||
assetService.removeAssetTags('asset-1', ['mask'])
|
||||
).resolves.toEqual(result)
|
||||
|
||||
expect(fetchApiMock).toHaveBeenCalledWith(
|
||||
'/assets/asset-1/tags',
|
||||
expect.objectContaining({
|
||||
method: 'DELETE',
|
||||
body: JSON.stringify({ tags: ['mask'] })
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('throws when removing tags fails', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(
|
||||
buildResponse(null, { ok: false, status: 404 })
|
||||
)
|
||||
|
||||
await expect(
|
||||
assetService.removeAssetTags('asset-1', ['mask'])
|
||||
).rejects.toThrow(
|
||||
'Unable to remove tags from asset asset-1: Server returned 404'
|
||||
)
|
||||
})
|
||||
|
||||
it('throws when the remove-tags response is invalid', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(buildResponse({ removed: ['mask'] }))
|
||||
|
||||
await expect(
|
||||
assetService.removeAssetTags('asset-1', ['mask'])
|
||||
).rejects.toThrow()
|
||||
})
|
||||
})
|
||||
|
||||
describe(assetService.getAssetModelFolders, () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
@@ -481,6 +823,16 @@ describe(assetService.updateAsset, () => {
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('throws when the update response is not ok', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(
|
||||
buildResponse(null, { ok: false, status: 409 })
|
||||
)
|
||||
|
||||
await expect(
|
||||
assetService.updateAsset('asset-1', { name: 'renamed.safetensors' })
|
||||
).rejects.toThrow('Unable to update asset asset-1: Server returned 409')
|
||||
})
|
||||
})
|
||||
|
||||
describe(assetService.getAssetsByTag, () => {
|
||||
@@ -515,6 +867,21 @@ describe(assetService.getAssetsByTag, () => {
|
||||
expect(params.get('include_tags')).toBe('input')
|
||||
expect(params.get('exclude_tags')).toBe(MISSING_TAG)
|
||||
})
|
||||
|
||||
it('forwards explicit public filtering and offset pagination', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(buildAssetListResponse([]))
|
||||
|
||||
await assetService.getAssetsByTag('input', false, {
|
||||
limit: 30,
|
||||
offset: 60
|
||||
})
|
||||
|
||||
const requestedUrl = fetchApiMock.mock.calls[0]?.[0] as string
|
||||
const params = new URL(requestedUrl, 'http://localhost').searchParams
|
||||
expect(params.get('include_public')).toBe('false')
|
||||
expect(params.get('limit')).toBe('30')
|
||||
expect(params.get('offset')).toBe('60')
|
||||
})
|
||||
})
|
||||
|
||||
describe(assetService.getAllAssetsByTag, () => {
|
||||
@@ -562,6 +929,31 @@ describe(assetService.getAllAssetsByTag, () => {
|
||||
expect(secondParams.has('offset')).toBe(false)
|
||||
})
|
||||
|
||||
it('uses the default page size when limit is not positive', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(buildAssetListResponse([]))
|
||||
|
||||
await expect(
|
||||
assetService.getAllAssetsByTag('input', true, { limit: 0 })
|
||||
).resolves.toEqual([])
|
||||
|
||||
const requestedUrl = fetchApiMock.mock.calls[0]?.[0] as string
|
||||
const params = new URL(requestedUrl, 'http://localhost').searchParams
|
||||
expect(params.get('limit')).toBe('500')
|
||||
})
|
||||
|
||||
it('throws before fetching when the pagination signal is already aborted', async () => {
|
||||
const controller = new AbortController()
|
||||
controller.abort()
|
||||
|
||||
await expect(
|
||||
assetService.getAllAssetsByTag('input', true, {
|
||||
signal: controller.signal
|
||||
})
|
||||
).rejects.toMatchObject({ name: 'AbortError' })
|
||||
|
||||
expect(fetchApiMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('honors has_more when walking tagged asset pages', async () => {
|
||||
fetchApiMock
|
||||
.mockResolvedValueOnce(
|
||||
@@ -703,6 +1095,75 @@ describe(assetService.getAllAssetsByTag, () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe(assetService.createAssetExport, () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('posts export options and returns the export task', async () => {
|
||||
const task = { task_id: 'export-1', status: 'created', message: 'queued' }
|
||||
fetchApiMock.mockResolvedValueOnce(buildResponse(task))
|
||||
|
||||
await expect(
|
||||
assetService.createAssetExport({
|
||||
asset_ids: ['asset-1'],
|
||||
include_previews: true
|
||||
})
|
||||
).resolves.toEqual(task)
|
||||
|
||||
expect(fetchApiMock).toHaveBeenCalledWith(
|
||||
'/assets/export',
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
asset_ids: ['asset-1'],
|
||||
include_previews: true
|
||||
})
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('throws when creating an export fails', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(
|
||||
buildResponse(null, { ok: false, status: 503 })
|
||||
)
|
||||
|
||||
await expect(
|
||||
assetService.createAssetExport({ asset_ids: ['asset-1'] })
|
||||
).rejects.toThrow('Failed to create asset export: 503')
|
||||
})
|
||||
})
|
||||
|
||||
describe(assetService.getExportDownloadUrl, () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('returns the export download URL', async () => {
|
||||
const download = {
|
||||
url: 'https://example.com/export.zip',
|
||||
expires_at: '2026-07-01T00:00:00Z'
|
||||
}
|
||||
fetchApiMock.mockResolvedValueOnce(buildResponse(download))
|
||||
|
||||
await expect(
|
||||
assetService.getExportDownloadUrl('export.zip')
|
||||
).resolves.toEqual(download)
|
||||
|
||||
expect(fetchApiMock).toHaveBeenCalledWith('/assets/exports/export.zip')
|
||||
})
|
||||
|
||||
it('throws when export download URL lookup fails', async () => {
|
||||
fetchApiMock.mockResolvedValueOnce(
|
||||
buildResponse(null, { ok: false, status: 404 })
|
||||
)
|
||||
|
||||
await expect(
|
||||
assetService.getExportDownloadUrl('missing.zip')
|
||||
).rejects.toThrow('Failed to get export download URL: 404')
|
||||
})
|
||||
})
|
||||
|
||||
describe(assetService.getInputAssetsIncludingPublic, () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
@@ -729,6 +1190,17 @@ describe(assetService.getInputAssetsIncludingPublic, () => {
|
||||
expect(params.get('limit')).toBe('500')
|
||||
})
|
||||
|
||||
it('throws before starting a shared request when the caller signal is already aborted', async () => {
|
||||
const controller = new AbortController()
|
||||
controller.abort()
|
||||
|
||||
await expect(
|
||||
assetService.getInputAssetsIncludingPublic(controller.signal)
|
||||
).rejects.toMatchObject({ name: 'AbortError' })
|
||||
|
||||
expect(fetchApiMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('fetches fresh input assets after explicit invalidation', async () => {
|
||||
const staleAssets = [validAsset({ id: 'stale-input', tags: ['input'] })]
|
||||
const freshAssets = [validAsset({ id: 'fresh-input', tags: ['input'] })]
|
||||
|
||||
37
src/platform/assets/utils/assetTypeUtil.test.ts
Normal file
37
src/platform/assets/utils/assetTypeUtil.test.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
|
||||
import { getAssetType } from '@/platform/assets/utils/assetTypeUtil'
|
||||
|
||||
function asset(overrides: Partial<AssetItem> = {}): AssetItem {
|
||||
return {
|
||||
id: 'asset-1',
|
||||
name: 'image.png',
|
||||
preview_url: '',
|
||||
tags: [],
|
||||
created_at: '',
|
||||
updated_at: '',
|
||||
size: 0,
|
||||
mime_type: 'image/png',
|
||||
user_metadata: {},
|
||||
...overrides
|
||||
} as AssetItem
|
||||
}
|
||||
|
||||
describe('getAssetType', () => {
|
||||
it('prefers the preview URL type over tags', () => {
|
||||
expect(
|
||||
getAssetType(
|
||||
asset({
|
||||
preview_url: '/api/view?filename=image.png&type=temp',
|
||||
tags: ['output']
|
||||
})
|
||||
)
|
||||
).toBe('temp')
|
||||
})
|
||||
|
||||
it('falls back to tags and then the supplied default type', () => {
|
||||
expect(getAssetType(asset({ tags: ['input'] }))).toBe('input')
|
||||
expect(getAssetType(asset(), 'input')).toBe('input')
|
||||
})
|
||||
})
|
||||
62
src/platform/assets/utils/assetUrlUtil.test.ts
Normal file
62
src/platform/assets/utils/assetUrlUtil.test.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
|
||||
import { getAssetUrl } from '@/platform/assets/utils/assetUrlUtil'
|
||||
|
||||
const { apiURL } = vi.hoisted(() => ({
|
||||
apiURL: vi.fn((path: string) => `https://comfy.local${path}`)
|
||||
}))
|
||||
|
||||
vi.mock('@/scripts/api', () => ({
|
||||
api: { apiURL }
|
||||
}))
|
||||
|
||||
function asset(overrides: Partial<AssetItem> = {}): AssetItem {
|
||||
return {
|
||||
id: 'asset-1',
|
||||
name: 'folder image.png',
|
||||
preview_url: '',
|
||||
tags: ['output'],
|
||||
created_at: '',
|
||||
updated_at: '',
|
||||
size: 0,
|
||||
mime_type: 'image/png',
|
||||
user_metadata: {},
|
||||
...overrides
|
||||
} as AssetItem
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
apiURL.mockClear()
|
||||
})
|
||||
|
||||
describe('getAssetUrl', () => {
|
||||
it('builds encoded view URLs with type and subfolder', () => {
|
||||
const url = getAssetUrl(
|
||||
asset({
|
||||
user_metadata: { subfolder: 'nested/path' }
|
||||
})
|
||||
)
|
||||
|
||||
expect(apiURL).toHaveBeenCalledWith(
|
||||
'/view?filename=folder+image.png&type=output&subfolder=nested%2Fpath'
|
||||
)
|
||||
expect(url).toBe(
|
||||
'https://comfy.local/view?filename=folder+image.png&type=output&subfolder=nested%2Fpath'
|
||||
)
|
||||
})
|
||||
|
||||
it('uses preview URL type and omits empty subfolders', () => {
|
||||
getAssetUrl(
|
||||
asset({
|
||||
preview_url: '/api/view?filename=image.png&type=temp',
|
||||
tags: ['output'],
|
||||
user_metadata: { subfolder: '' }
|
||||
})
|
||||
)
|
||||
|
||||
expect(apiURL).toHaveBeenCalledWith(
|
||||
'/view?filename=folder+image.png&type=temp'
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,5 @@
|
||||
import { createTestingPinia } from '@pinia/testing'
|
||||
import { fromPartial } from '@total-typescript/shoehorn'
|
||||
import { setActivePinia } from 'pinia'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
@@ -28,6 +29,8 @@ interface HostAssetWidget extends IBaseWidget<
|
||||
node: LGraphNode
|
||||
}
|
||||
|
||||
type AssetWidget = IBaseWidget<string | undefined, 'asset', IWidgetAssetOptions>
|
||||
|
||||
type OnWidgetChanged = NonNullable<LGraphNode['onWidgetChanged']>
|
||||
|
||||
function checkpointAsset(name: string): AssetItem {
|
||||
@@ -166,4 +169,118 @@ describe('createAssetWidget', () => {
|
||||
)
|
||||
expect(captureCanvasState).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('falls back to widget name and empty current value for cloned widgets', async () => {
|
||||
const { node } = createAssetWidgetNode()
|
||||
const sourceWidget = createAssetWidget({
|
||||
node,
|
||||
widgetName: 'lora_name',
|
||||
nodeTypeForBrowser: 'LoraLoader'
|
||||
})
|
||||
assertAssetOptions(sourceWidget.options)
|
||||
const clonedWidget: AssetWidget = {
|
||||
type: 'asset',
|
||||
name: 'lora_name',
|
||||
value: undefined,
|
||||
options: sourceWidget.options,
|
||||
y: 0
|
||||
}
|
||||
|
||||
await sourceWidget.options.openModal(clonedWidget)
|
||||
|
||||
expect(firstShowOptions()).toMatchObject({
|
||||
nodeType: 'LoraLoader',
|
||||
inputName: 'lora_name',
|
||||
currentValue: ''
|
||||
})
|
||||
})
|
||||
|
||||
it('rejects malformed asset selections', async () => {
|
||||
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
const { node } = createAssetWidgetNode()
|
||||
const widget = createAssetWidget({
|
||||
node,
|
||||
widgetName: 'ckpt_name',
|
||||
nodeTypeForBrowser: 'CheckpointLoaderSimple',
|
||||
defaultValue: 'fake_model.safetensors'
|
||||
})
|
||||
assertAssetOptions(widget.options)
|
||||
|
||||
await widget.options.openModal(widget)
|
||||
firstShowOptions().onAssetSelected?.(
|
||||
fromPartial({ id: 'asset-without-name' })
|
||||
)
|
||||
|
||||
expect(widget.value).toBe('fake_model.safetensors')
|
||||
expect(captureCanvasState).not.toHaveBeenCalled()
|
||||
consoleError.mockRestore()
|
||||
})
|
||||
|
||||
it('rejects invalid asset filenames', async () => {
|
||||
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
const { node } = createAssetWidgetNode()
|
||||
const widget = createAssetWidget({
|
||||
node,
|
||||
widgetName: 'ckpt_name',
|
||||
nodeTypeForBrowser: 'CheckpointLoaderSimple',
|
||||
defaultValue: 'fake_model.safetensors'
|
||||
})
|
||||
assertAssetOptions(widget.options)
|
||||
|
||||
await widget.options.openModal(widget)
|
||||
firstShowOptions().onAssetSelected?.(checkpointAsset('../bad.safetensors'))
|
||||
|
||||
expect(widget.value).toBe('fake_model.safetensors')
|
||||
expect(captureCanvasState).not.toHaveBeenCalled()
|
||||
consoleError.mockRestore()
|
||||
})
|
||||
|
||||
it('updates ownerless cloned widgets without node callbacks', async () => {
|
||||
const { node, onWidgetChanged } = createAssetWidgetNode()
|
||||
const sourceWidget = createAssetWidget({
|
||||
node,
|
||||
widgetName: 'ckpt_name',
|
||||
nodeTypeForBrowser: 'CheckpointLoaderSimple',
|
||||
defaultValue: 'fake_model.safetensors'
|
||||
})
|
||||
assertAssetOptions(sourceWidget.options)
|
||||
const callback = vi.fn<NonNullable<IBaseWidget['callback']>>()
|
||||
const clonedWidget: AssetWidget = {
|
||||
type: 'asset',
|
||||
name: 'ckpt_name',
|
||||
value: 'fake_model.safetensors',
|
||||
callback,
|
||||
options: sourceWidget.options,
|
||||
y: 0
|
||||
}
|
||||
|
||||
await sourceWidget.options.openModal(clonedWidget)
|
||||
firstShowOptions().onAssetSelected?.(
|
||||
checkpointAsset('real_model.safetensors')
|
||||
)
|
||||
|
||||
expect(clonedWidget.value).toBe('real_model.safetensors')
|
||||
expect(callback).toHaveBeenCalledWith('real_model.safetensors')
|
||||
expect(onWidgetChanged).not.toHaveBeenCalled()
|
||||
expect(captureCanvasState).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('does not capture canvas state when the selection is unchanged', async () => {
|
||||
const { node } = createAssetWidgetNode()
|
||||
const widget = createAssetWidget({
|
||||
node,
|
||||
widgetName: 'ckpt_name',
|
||||
nodeTypeForBrowser: 'CheckpointLoaderSimple',
|
||||
defaultValue: 'fake_model.safetensors'
|
||||
})
|
||||
assertAssetOptions(widget.options)
|
||||
|
||||
await widget.options.openModal(widget)
|
||||
firstShowOptions().onAssetSelected?.(
|
||||
checkpointAsset('fake_model.safetensors')
|
||||
)
|
||||
|
||||
expect(widget.value).toBe('fake_model.safetensors')
|
||||
expect(captureCanvasState).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
286
src/services/comfyRegistryService.test.ts
Normal file
286
src/services/comfyRegistryService.test.ts
Normal file
@@ -0,0 +1,286 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
interface AxiosLikeError extends Error {
|
||||
isAxiosError: true
|
||||
response?: {
|
||||
status: number
|
||||
data?: {
|
||||
message?: string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const mockClient = vi.hoisted(() => ({
|
||||
get: vi.fn(),
|
||||
post: vi.fn()
|
||||
}))
|
||||
|
||||
const mockAxios = vi.hoisted(() => ({
|
||||
create: vi.fn(() => mockClient),
|
||||
isAxiosError: vi.fn(
|
||||
(error: unknown): error is AxiosLikeError =>
|
||||
typeof error === 'object' &&
|
||||
error !== null &&
|
||||
'isAxiosError' in error &&
|
||||
error.isAxiosError === true
|
||||
)
|
||||
}))
|
||||
|
||||
vi.mock('axios', () => ({
|
||||
default: mockAxios
|
||||
}))
|
||||
|
||||
import { useComfyRegistryService } from './comfyRegistryService'
|
||||
|
||||
function response<T>(data: T) {
|
||||
return { data }
|
||||
}
|
||||
|
||||
function axiosError(
|
||||
message: string,
|
||||
responseData?: AxiosLikeError['response']
|
||||
): AxiosLikeError {
|
||||
const error = new Error(message) as AxiosLikeError
|
||||
error.isAxiosError = true
|
||||
if (responseData) error.response = responseData
|
||||
return error
|
||||
}
|
||||
|
||||
describe('useComfyRegistryService', () => {
|
||||
beforeEach(() => {
|
||||
mockClient.get.mockReset()
|
||||
mockClient.post.mockReset()
|
||||
mockAxios.isAxiosError.mockClear()
|
||||
})
|
||||
|
||||
it('configures the registry axios client with repeated query params', () => {
|
||||
expect(mockAxios.create).toHaveBeenCalledWith({
|
||||
baseURL: 'https://api.comfy.org',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
paramsSerializer: {
|
||||
indexes: null
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('returns response data and clears loading state for successful requests', async () => {
|
||||
mockClient.get.mockResolvedValueOnce(response({ nodes: [] }))
|
||||
const service = useComfyRegistryService()
|
||||
|
||||
const result = await service.search({ search: 'manager' })
|
||||
|
||||
expect(result).toEqual({ nodes: [] })
|
||||
expect(mockClient.get).toHaveBeenCalledWith('/nodes/search', {
|
||||
params: { search: 'manager' },
|
||||
signal: undefined
|
||||
})
|
||||
expect(service.error.value).toBeNull()
|
||||
expect(service.isLoading.value).toBe(false)
|
||||
})
|
||||
|
||||
it('skips node definition requests when pack id or version is missing', async () => {
|
||||
const service = useComfyRegistryService()
|
||||
|
||||
await expect(
|
||||
service.getNodeDefs({ packId: '', version: '1.0.0' })
|
||||
).resolves.toBeNull()
|
||||
await expect(
|
||||
service.getNodeDefs({ packId: 'pack', version: '' })
|
||||
).resolves.toBeNull()
|
||||
expect(mockClient.get).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('passes query params and abort signals through node definition requests', async () => {
|
||||
const signal = new AbortController().signal
|
||||
mockClient.get.mockResolvedValueOnce(response([{ name: 'KSampler' }]))
|
||||
const service = useComfyRegistryService()
|
||||
|
||||
const result = await service.getNodeDefs(
|
||||
{ packId: 'pack', version: '1.0.0', page: 2 },
|
||||
signal
|
||||
)
|
||||
|
||||
expect(result).toEqual([{ name: 'KSampler' }])
|
||||
expect(mockClient.get).toHaveBeenCalledWith(
|
||||
'/nodes/pack/versions/1.0.0/comfy-nodes',
|
||||
{
|
||||
params: { page: 2 },
|
||||
signal
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it('routes publisher, pack, and review methods to their registry endpoints', async () => {
|
||||
mockClient.get
|
||||
.mockResolvedValueOnce(response({ id: 'publisher' }))
|
||||
.mockResolvedValueOnce(response([{ id: 'pack' }]))
|
||||
.mockResolvedValueOnce(response([{ version: '1.0.0' }]))
|
||||
.mockResolvedValueOnce(response({ id: 'version' }))
|
||||
.mockResolvedValueOnce(response({ id: 'pack' }))
|
||||
.mockResolvedValueOnce(response({ id: 'pack' }))
|
||||
.mockResolvedValueOnce(response({ id: 'pack' }))
|
||||
mockClient.post
|
||||
.mockResolvedValueOnce(response({ id: 'reviewed' }))
|
||||
.mockResolvedValueOnce(response({ node_versions: [] }))
|
||||
const service = useComfyRegistryService()
|
||||
const signal = new AbortController().signal
|
||||
|
||||
await expect(
|
||||
service.getPublisherById('publisher', signal)
|
||||
).resolves.toEqual({ id: 'publisher' })
|
||||
await expect(
|
||||
service.listPacksForPublisher('publisher', true, signal)
|
||||
).resolves.toEqual([{ id: 'pack' }])
|
||||
await expect(
|
||||
service.getPackVersions(
|
||||
'pack',
|
||||
{ statuses: ['NodeVersionStatusActive'] },
|
||||
signal
|
||||
)
|
||||
).resolves.toEqual([{ version: '1.0.0' }])
|
||||
await expect(
|
||||
service.getPackByVersion('pack', 'version', signal)
|
||||
).resolves.toEqual({ id: 'version' })
|
||||
await expect(service.getPackById('pack', signal)).resolves.toEqual({
|
||||
id: 'pack'
|
||||
})
|
||||
await expect(
|
||||
service.inferPackFromNodeName('KSampler', signal)
|
||||
).resolves.toEqual({ id: 'pack' })
|
||||
await expect(service.listAllPacks({ page: 1 }, signal)).resolves.toEqual({
|
||||
id: 'pack'
|
||||
})
|
||||
await expect(service.postPackReview('pack', 5, signal)).resolves.toEqual({
|
||||
id: 'reviewed'
|
||||
})
|
||||
await expect(
|
||||
service.getBulkNodeVersions(
|
||||
[{ node_id: 'pack', version: '1.0.0' }],
|
||||
signal
|
||||
)
|
||||
).resolves.toEqual({ node_versions: [] })
|
||||
|
||||
expect(mockClient.get).toHaveBeenNthCalledWith(1, '/publishers/publisher', {
|
||||
signal
|
||||
})
|
||||
expect(mockClient.get).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'/publishers/publisher/nodes',
|
||||
{
|
||||
params: { include_banned: true },
|
||||
signal
|
||||
}
|
||||
)
|
||||
expect(mockClient.get).toHaveBeenNthCalledWith(3, '/nodes/pack/versions', {
|
||||
params: { statuses: ['NodeVersionStatusActive'] },
|
||||
signal
|
||||
})
|
||||
expect(mockClient.get).toHaveBeenNthCalledWith(
|
||||
4,
|
||||
'/nodes/pack/versions/version',
|
||||
{ signal }
|
||||
)
|
||||
expect(mockClient.get).toHaveBeenNthCalledWith(5, '/nodes/pack', {
|
||||
signal
|
||||
})
|
||||
expect(mockClient.get).toHaveBeenNthCalledWith(
|
||||
6,
|
||||
'/comfy-nodes/KSampler/node',
|
||||
{ signal }
|
||||
)
|
||||
expect(mockClient.get).toHaveBeenNthCalledWith(7, '/nodes', {
|
||||
params: { page: 1 },
|
||||
signal
|
||||
})
|
||||
expect(mockClient.post).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'/nodes/pack/reviews',
|
||||
null,
|
||||
{ params: { star: 5 }, signal }
|
||||
)
|
||||
expect(mockClient.post).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'/bulk/nodes/versions',
|
||||
{ node_versions: [{ node_id: 'pack', version: '1.0.0' }] },
|
||||
{ signal }
|
||||
)
|
||||
})
|
||||
|
||||
it('omits include_banned when listing publisher packs without banned packs', async () => {
|
||||
mockClient.get.mockResolvedValueOnce(response([]))
|
||||
const service = useComfyRegistryService()
|
||||
|
||||
await service.listPacksForPublisher('publisher', false)
|
||||
|
||||
expect(mockClient.get).toHaveBeenCalledWith('/publishers/publisher/nodes', {
|
||||
params: undefined,
|
||||
signal: undefined
|
||||
})
|
||||
})
|
||||
|
||||
it.for([
|
||||
{ status: 400, expected: 'Bad request: Invalid input' },
|
||||
{ status: 401, expected: 'Unauthorized: Authentication required' },
|
||||
{ status: 403, expected: 'Forbidden: Access denied' },
|
||||
{ status: 404, expected: 'Not found: Resource not found' },
|
||||
{ status: 409, expected: 'Conflict: Resource conflict' },
|
||||
{ status: 500, expected: 'Server error: Internal server error' },
|
||||
{ status: 418, expected: 'Failed to perform search: teapot' }
|
||||
])(
|
||||
'normalizes axios response status $status',
|
||||
async ({ status, expected }) => {
|
||||
mockClient.get.mockRejectedValueOnce(
|
||||
axiosError('Request failed', {
|
||||
status,
|
||||
data: status === 418 ? { message: 'teapot' } : {}
|
||||
})
|
||||
)
|
||||
const service = useComfyRegistryService()
|
||||
|
||||
await expect(service.search()).resolves.toBeNull()
|
||||
|
||||
expect(service.error.value).toBe(expected)
|
||||
expect(service.isLoading.value).toBe(false)
|
||||
}
|
||||
)
|
||||
|
||||
it('uses route-specific errors before generic status messages', async () => {
|
||||
mockClient.get.mockRejectedValueOnce(
|
||||
axiosError('Request failed', {
|
||||
status: 404,
|
||||
data: { message: 'ignored' }
|
||||
})
|
||||
)
|
||||
const service = useComfyRegistryService()
|
||||
|
||||
await expect(service.getPackById('missing')).resolves.toBeNull()
|
||||
|
||||
expect(service.error.value).toBe(
|
||||
'Pack not found: The pack with ID missing does not exist'
|
||||
)
|
||||
})
|
||||
|
||||
it('normalizes network, thrown Error, unknown, and abort failures', async () => {
|
||||
const service = useComfyRegistryService()
|
||||
|
||||
mockClient.get.mockRejectedValueOnce(axiosError('Network down'))
|
||||
await expect(service.search()).resolves.toBeNull()
|
||||
expect(service.error.value).toBe('Failed to perform search: Network down')
|
||||
|
||||
mockClient.get.mockRejectedValueOnce(new Error('boom'))
|
||||
await expect(service.search()).resolves.toBeNull()
|
||||
expect(service.error.value).toBe('Failed to perform search: boom')
|
||||
|
||||
mockClient.get.mockRejectedValueOnce('bad')
|
||||
await expect(service.search()).resolves.toBeNull()
|
||||
expect(service.error.value).toBe(
|
||||
'Failed to perform search: Unknown error occurred'
|
||||
)
|
||||
|
||||
mockClient.get.mockRejectedValueOnce(new DOMException('', 'AbortError'))
|
||||
await expect(service.search()).resolves.toBeNull()
|
||||
expect(service.error.value).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -234,6 +234,54 @@ describe('useRegistrySearchGateway', () => {
|
||||
const gateway = useRegistrySearchGateway()
|
||||
expect(gateway).toBeDefined()
|
||||
})
|
||||
|
||||
it('waits for the circuit breaker timeout before retrying a failed provider', async () => {
|
||||
vi.useFakeTimers()
|
||||
vi.setSystemTime(new Date('2024-01-01T00:00:00Z'))
|
||||
|
||||
vi.mocked(useAlgoliaSearchProvider).mockImplementation(() => {
|
||||
throw new Error('Algolia init failed')
|
||||
})
|
||||
|
||||
const registryResult = {
|
||||
nodePacks: [{ id: 'registry-1', name: 'Registry Pack' }],
|
||||
querySuggestions: []
|
||||
}
|
||||
|
||||
const mockRegistryProvider = {
|
||||
searchPacks: vi.fn().mockRejectedValue(new Error('Registry failed')),
|
||||
clearSearchCache: vi.fn(),
|
||||
getSortValue: vi.fn(),
|
||||
getSortableFields: vi.fn().mockReturnValue([])
|
||||
}
|
||||
|
||||
vi.mocked(useComfyRegistrySearchProvider).mockReturnValue(
|
||||
mockRegistryProvider
|
||||
)
|
||||
|
||||
const gateway = useRegistrySearchGateway()
|
||||
|
||||
for (let attempt = 0; attempt < 3; attempt++) {
|
||||
await expect(
|
||||
gateway.searchPacks('test', { pageSize: 10, pageNumber: 0 })
|
||||
).rejects.toThrow('All search providers failed')
|
||||
}
|
||||
|
||||
expect(mockRegistryProvider.searchPacks).toHaveBeenCalledTimes(3)
|
||||
|
||||
await expect(
|
||||
gateway.searchPacks('test', { pageSize: 10, pageNumber: 0 })
|
||||
).rejects.toThrow('All search providers failed')
|
||||
expect(mockRegistryProvider.searchPacks).toHaveBeenCalledTimes(3)
|
||||
|
||||
vi.setSystemTime(new Date('2024-01-01T00:01:01Z'))
|
||||
mockRegistryProvider.searchPacks.mockResolvedValueOnce(registryResult)
|
||||
|
||||
await expect(
|
||||
gateway.searchPacks('test', { pageSize: 10, pageNumber: 0 })
|
||||
).resolves.toBe(registryResult)
|
||||
expect(mockRegistryProvider.searchPacks).toHaveBeenCalledTimes(4)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Cache management', () => {
|
||||
|
||||
@@ -126,6 +126,19 @@ describe('useAssetDownloadStore', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps the first placeholder when the same task is tracked twice', () => {
|
||||
const store = useAssetDownloadStore()
|
||||
|
||||
store.trackDownload('task-123', 'checkpoints', 'first.safetensors')
|
||||
store.trackDownload('task-123', 'loras', 'second.safetensors')
|
||||
|
||||
expect(store.downloadList).toHaveLength(1)
|
||||
expect(store.downloadList[0]).toMatchObject({
|
||||
modelType: 'checkpoints',
|
||||
assetName: 'first.safetensors'
|
||||
})
|
||||
})
|
||||
|
||||
it('handles out-of-order messages where completed arrives before progress', () => {
|
||||
const store = useAssetDownloadStore()
|
||||
|
||||
@@ -179,6 +192,19 @@ describe('useAssetDownloadStore', () => {
|
||||
expect(store.finishedDownloads[0].status).toBe('completed')
|
||||
})
|
||||
|
||||
it('skips polling when active downloads have fresh progress', async () => {
|
||||
const store = useAssetDownloadStore()
|
||||
|
||||
dispatch(createDownloadMessage({ status: 'running' }))
|
||||
await vi.advanceTimersByTimeAsync(9_999)
|
||||
dispatch(createDownloadMessage({ status: 'running', progress: 75 }))
|
||||
await vi.advanceTimersByTimeAsync(1)
|
||||
|
||||
expect(taskService.getTask).not.toHaveBeenCalled()
|
||||
expect(store.activeDownloads).toHaveLength(1)
|
||||
expect(store.activeDownloads[0].progress).toBe(75)
|
||||
})
|
||||
|
||||
it('polls and marks failed downloads', async () => {
|
||||
const store = useAssetDownloadStore()
|
||||
|
||||
@@ -311,5 +337,22 @@ describe('useAssetDownloadStore', () => {
|
||||
expect(store.sessionDownloadCount).toBe(0)
|
||||
expect(store.isDownloadedThisSession('asset-456')).toBe(false)
|
||||
})
|
||||
|
||||
it('does not acknowledge unrelated completed downloads', () => {
|
||||
const store = useAssetDownloadStore()
|
||||
|
||||
dispatch(
|
||||
createDownloadMessage({
|
||||
status: 'completed',
|
||||
progress: 100,
|
||||
asset_id: 'asset-456'
|
||||
})
|
||||
)
|
||||
|
||||
store.acknowledgeAsset('other-asset')
|
||||
|
||||
expect(store.sessionDownloadCount).toBe(1)
|
||||
expect(store.isDownloadedThisSession('asset-456')).toBe(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { createTestingPinia } from '@pinia/testing'
|
||||
import { fromAny } from '@total-typescript/shoehorn'
|
||||
import { setActivePinia } from 'pinia'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { nextTick, watch } from 'vue'
|
||||
@@ -11,6 +12,7 @@ import type {
|
||||
} from '@/platform/assets/schemas/assetSchema'
|
||||
import type { JobListItem } from '@/platform/remote/comfyui/jobs/jobTypes'
|
||||
import { assetService } from '@/platform/assets/services/assetService'
|
||||
import { useAssetDownloadStore } from '@/stores/assetDownloadStore'
|
||||
|
||||
// Mock the api module
|
||||
vi.mock('@/scripts/api', () => ({
|
||||
@@ -96,6 +98,10 @@ const mockOutputOverrides = vi.hoisted(() => ({
|
||||
value: null as MockOutput[] | null
|
||||
}))
|
||||
|
||||
const mockAssetMapperOptions = vi.hoisted(() => ({
|
||||
omitCreatedAtForIds: new Set<string>()
|
||||
}))
|
||||
|
||||
// Mock TaskItemImpl
|
||||
const PREVIEWABLE_MEDIA_TYPES = new Set(['images', 'video', 'audio'])
|
||||
|
||||
@@ -169,11 +175,14 @@ vi.mock('@/platform/assets/composables/media/assetMappers', () => ({
|
||||
})),
|
||||
mapTaskOutputToAssetItem: vi.fn((task, output) => {
|
||||
const index = parseInt(task.jobId.split('_')[1]) || 0
|
||||
const createdAt = new Date(Date.now() - index * 1000).toISOString()
|
||||
return {
|
||||
id: task.jobId,
|
||||
name: output.filename,
|
||||
size: 0,
|
||||
created_at: new Date(Date.now() - index * 1000).toISOString(),
|
||||
...(!mockAssetMapperOptions.omitCreatedAtForIds.has(task.jobId) && {
|
||||
created_at: createdAt
|
||||
}),
|
||||
tags: ['output'],
|
||||
preview_url: output.url,
|
||||
user_metadata: {}
|
||||
@@ -205,6 +214,7 @@ describe('assetsStore - Refactored (Option A)', () => {
|
||||
setActivePinia(createTestingPinia({ stubActions: false }))
|
||||
store = useAssetsStore()
|
||||
vi.clearAllMocks()
|
||||
mockAssetMapperOptions.omitCreatedAtForIds.clear()
|
||||
})
|
||||
|
||||
describe('Initial Load', () => {
|
||||
@@ -272,6 +282,17 @@ describe('assetsStore - Refactored (Option A)', () => {
|
||||
'prompt_2'
|
||||
])
|
||||
})
|
||||
|
||||
it('should skip unfinished jobs and completed jobs without previews', async () => {
|
||||
vi.mocked(api.getHistory).mockResolvedValue([
|
||||
{ ...createMockJobItem(0), status: 'in_progress' },
|
||||
{ ...createMockJobItem(1), preview_output: undefined }
|
||||
])
|
||||
|
||||
await store.updateHistory()
|
||||
|
||||
expect(store.historyAssets).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('Pagination', () => {
|
||||
@@ -328,6 +349,46 @@ describe('assetsStore - Refactored (Option A)', () => {
|
||||
expect(uniqueAssetIds.size).toBe(store.historyAssets.length)
|
||||
})
|
||||
|
||||
it('should insert newer paginated items in sorted order', async () => {
|
||||
vi.mocked(api.getHistory).mockResolvedValueOnce(
|
||||
Array.from({ length: 200 }, (_, i) => createMockJobItem(i))
|
||||
)
|
||||
await store.updateHistory()
|
||||
|
||||
vi.mocked(api.getHistory).mockResolvedValueOnce([createMockJobItem(-1)])
|
||||
await store.loadMoreHistory()
|
||||
|
||||
expect(store.historyAssets[0].id).toBe('prompt_-1')
|
||||
})
|
||||
|
||||
it('sorts paginated items when the incoming asset has no timestamp', async () => {
|
||||
vi.mocked(api.getHistory).mockResolvedValueOnce(
|
||||
Array.from({ length: 200 }, (_, i) => createMockJobItem(i))
|
||||
)
|
||||
await store.updateHistory()
|
||||
mockAssetMapperOptions.omitCreatedAtForIds.add('prompt_200')
|
||||
vi.mocked(api.getHistory).mockResolvedValueOnce([createMockJobItem(200)])
|
||||
|
||||
await store.loadMoreHistory()
|
||||
|
||||
expect(store.historyAssets.at(-1)?.id).toBe('prompt_200')
|
||||
})
|
||||
|
||||
it('sorts paginated items when an existing asset has no timestamp', async () => {
|
||||
for (let i = 0; i < 200; i++) {
|
||||
mockAssetMapperOptions.omitCreatedAtForIds.add(`prompt_${i}`)
|
||||
}
|
||||
vi.mocked(api.getHistory).mockResolvedValueOnce(
|
||||
Array.from({ length: 200 }, (_, i) => createMockJobItem(i))
|
||||
)
|
||||
await store.updateHistory()
|
||||
vi.mocked(api.getHistory).mockResolvedValueOnce([createMockJobItem(-1)])
|
||||
|
||||
await store.loadMoreHistory()
|
||||
|
||||
expect(store.historyAssets[0].id).toBe('prompt_-1')
|
||||
})
|
||||
|
||||
it('should stop loading when no more items', async () => {
|
||||
// First batch - less than BATCH_SIZE
|
||||
const firstBatch = Array.from({ length: 50 }, (_, i) =>
|
||||
@@ -494,6 +555,29 @@ describe('assetsStore - Refactored (Option A)', () => {
|
||||
expect(store.historyLoading).toBe(false)
|
||||
expect(store.historyError).toBe(error)
|
||||
})
|
||||
|
||||
it('should preserve existing history when refresh fails', async () => {
|
||||
vi.mocked(api.getHistory).mockResolvedValueOnce([createMockJobItem(0)])
|
||||
await store.updateHistory()
|
||||
|
||||
const error = new Error('API error')
|
||||
vi.mocked(api.getHistory).mockRejectedValueOnce(error)
|
||||
|
||||
await store.updateHistory()
|
||||
|
||||
expect(store.historyAssets).toHaveLength(1)
|
||||
expect(store.historyError).toBe(error)
|
||||
})
|
||||
|
||||
it('should keep empty history when loadMore fails before any load', async () => {
|
||||
const error = new Error('API error')
|
||||
vi.mocked(api.getHistory).mockRejectedValueOnce(error)
|
||||
|
||||
await store.loadMoreHistory()
|
||||
|
||||
expect(store.historyAssets).toEqual([])
|
||||
expect(store.historyError).toBe(error)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Memory Management', () => {
|
||||
@@ -924,6 +1008,43 @@ describe('assetsStore - Model Assets Cache (Cloud)', () => {
|
||||
vi.mocked(assetService.getAssetsForNodeType)
|
||||
).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('ignores a model response after the category is invalidated', async () => {
|
||||
const store = useAssetsStore()
|
||||
let resolveFetch!: (assets: AssetItem[]) => void
|
||||
vi.mocked(assetService.getAssetsForNodeType).mockReturnValueOnce(
|
||||
new Promise((resolve) => {
|
||||
resolveFetch = resolve
|
||||
})
|
||||
)
|
||||
|
||||
const request = store.updateModelsForNodeType('CheckpointLoaderSimple')
|
||||
store.invalidateCategory('checkpoints')
|
||||
resolveFetch([createMockAsset('stale-response')])
|
||||
await request
|
||||
|
||||
expect(store.getAssets('CheckpointLoaderSimple')).toEqual([])
|
||||
})
|
||||
|
||||
it('ignores a model rejection after the category is invalidated', async () => {
|
||||
const store = useAssetsStore()
|
||||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
let rejectFetch!: (error: Error) => void
|
||||
vi.mocked(assetService.getAssetsForNodeType).mockReturnValueOnce(
|
||||
new Promise((_resolve, reject) => {
|
||||
rejectFetch = reject
|
||||
})
|
||||
)
|
||||
|
||||
const request = store.updateModelsForNodeType('CheckpointLoaderSimple')
|
||||
store.invalidateCategory('checkpoints')
|
||||
rejectFetch(new Error('stale rejection'))
|
||||
await request
|
||||
|
||||
expect(store.getError('CheckpointLoaderSimple')).toBeUndefined()
|
||||
expect(consoleSpy).not.toHaveBeenCalled()
|
||||
consoleSpy.mockRestore()
|
||||
})
|
||||
})
|
||||
|
||||
describe('shallowReactive state reactivity', () => {
|
||||
@@ -966,6 +1087,10 @@ describe('assetsStore - Model Assets Cache (Cloud)', () => {
|
||||
it('should return empty array for unknown node types', () => {
|
||||
const store = useAssetsStore()
|
||||
expect(store.getAssets('UnknownNodeType')).toEqual([])
|
||||
expect(store.isModelLoading('UnknownNodeType')).toBe(false)
|
||||
expect(store.getError('UnknownNodeType')).toBeUndefined()
|
||||
expect(store.hasMore('UnknownNodeType')).toBe(false)
|
||||
expect(store.hasAssetKey('UnknownNodeType')).toBe(false)
|
||||
})
|
||||
|
||||
it('should not fetch for unknown node types', async () => {
|
||||
@@ -975,6 +1100,63 @@ describe('assetsStore - Model Assets Cache (Cloud)', () => {
|
||||
vi.mocked(assetService.getAssetsForNodeType)
|
||||
).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should refresh an already loaded category', async () => {
|
||||
const store = useAssetsStore()
|
||||
const nodeType = 'CheckpointLoaderSimple'
|
||||
|
||||
vi.mocked(assetService.getAssetsForNodeType).mockResolvedValueOnce([
|
||||
createMockAsset('first')
|
||||
])
|
||||
await store.updateModelsForNodeType(nodeType)
|
||||
|
||||
vi.mocked(assetService.getAssetsForNodeType).mockResolvedValueOnce([
|
||||
createMockAsset('second')
|
||||
])
|
||||
await store.updateModelsForNodeType(nodeType)
|
||||
|
||||
expect(store.getAssets(nodeType).map((asset) => asset.id)).toEqual([
|
||||
'second'
|
||||
])
|
||||
})
|
||||
|
||||
it('reports hasMore for a loaded category', async () => {
|
||||
const store = useAssetsStore()
|
||||
const nodeType = 'CheckpointLoaderSimple'
|
||||
|
||||
expect(store.hasMore(nodeType)).toBe(false)
|
||||
vi.mocked(assetService.getAssetsForNodeType).mockResolvedValueOnce([
|
||||
createMockAsset('only-page')
|
||||
])
|
||||
|
||||
await store.updateModelsForNodeType(nodeType)
|
||||
|
||||
expect(store.hasMore(nodeType)).toBe(false)
|
||||
})
|
||||
|
||||
it('should record model loading errors', async () => {
|
||||
const store = useAssetsStore()
|
||||
const error = new Error('model fetch failed')
|
||||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
vi.mocked(assetService.getAssetsForNodeType).mockRejectedValueOnce(error)
|
||||
|
||||
await store.updateModelsForNodeType('CheckpointLoaderSimple')
|
||||
|
||||
expect(store.getError('CheckpointLoaderSimple')).toBe(error)
|
||||
expect(store.isModelLoading('CheckpointLoaderSimple')).toBe(false)
|
||||
consoleSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('should wrap non-error model loading failures', async () => {
|
||||
const store = useAssetsStore()
|
||||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
vi.mocked(assetService.getAssetsForNodeType).mockRejectedValueOnce('boom')
|
||||
|
||||
await store.updateModelsForNodeType('CheckpointLoaderSimple')
|
||||
|
||||
expect(store.getError('CheckpointLoaderSimple')?.message).toBe('boom')
|
||||
consoleSpy.mockRestore()
|
||||
})
|
||||
})
|
||||
|
||||
describe('invalidateCategory', () => {
|
||||
@@ -1129,7 +1311,140 @@ describe('assetsStore - Model Assets Cache (Cloud)', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('completed download refresh', () => {
|
||||
it('refreshes provider and tag caches for the completed model type', async () => {
|
||||
const store = useAssetsStore()
|
||||
const downloadStore = useAssetDownloadStore()
|
||||
vi.mocked(assetService.getAssetsForNodeType).mockResolvedValue([])
|
||||
vi.mocked(assetService.getAssetsByTag).mockResolvedValue([])
|
||||
|
||||
downloadStore.lastCompletedDownload = {
|
||||
taskId: 'task-1',
|
||||
modelType: 'checkpoints',
|
||||
timestamp: 1
|
||||
}
|
||||
|
||||
await vi.waitFor(() =>
|
||||
expect(assetService.getAssetsByTag).toHaveBeenCalledWith(
|
||||
'models',
|
||||
true,
|
||||
expect.objectContaining({ limit: 500, offset: 0 })
|
||||
)
|
||||
)
|
||||
|
||||
expect(assetService.getAssetsForNodeType).toHaveBeenCalledWith(
|
||||
'CheckpointLoaderSimple',
|
||||
expect.objectContaining({ limit: 500, offset: 0 })
|
||||
)
|
||||
expect(assetService.getAssetsForNodeType).toHaveBeenCalledTimes(1)
|
||||
expect(assetService.getAssetsByTag).toHaveBeenCalledWith(
|
||||
'checkpoints',
|
||||
true,
|
||||
expect.objectContaining({ limit: 500, offset: 0 })
|
||||
)
|
||||
expect(store.hasCategory('tag:models')).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('updateAssetMetadata optimistic cache', () => {
|
||||
it('still writes metadata when a cache key is unresolved', async () => {
|
||||
const store = useAssetsStore()
|
||||
const original = {
|
||||
...createMockAsset('opt-unknown'),
|
||||
user_metadata: { note: 'before' } as Record<string, unknown>
|
||||
}
|
||||
vi.mocked(assetService.updateAsset).mockResolvedValueOnce({
|
||||
...original,
|
||||
user_metadata: { note: 'after' }
|
||||
})
|
||||
|
||||
await store.updateAssetMetadata(
|
||||
original,
|
||||
{ note: 'after' },
|
||||
'UnknownNodeType'
|
||||
)
|
||||
|
||||
expect(vi.mocked(assetService.updateAsset)).toHaveBeenCalledWith(
|
||||
'opt-unknown',
|
||||
{ user_metadata: { note: 'after' } }
|
||||
)
|
||||
})
|
||||
|
||||
it('still updates the server when the asset is not cached', async () => {
|
||||
const store = useAssetsStore()
|
||||
const original = {
|
||||
...createMockAsset('opt-missing'),
|
||||
user_metadata: { note: 'before' } as Record<string, unknown>
|
||||
}
|
||||
vi.mocked(assetService.updateAsset).mockResolvedValueOnce({
|
||||
...original,
|
||||
user_metadata: { note: 'server' }
|
||||
})
|
||||
|
||||
await store.updateAssetMetadata(original, { note: 'after' })
|
||||
|
||||
expect(vi.mocked(assetService.updateAsset)).toHaveBeenCalledWith(
|
||||
'opt-missing',
|
||||
{ user_metadata: { note: 'after' } }
|
||||
)
|
||||
})
|
||||
|
||||
it('still updates the server when a resolved cache key has not loaded yet', async () => {
|
||||
const store = useAssetsStore()
|
||||
const original = {
|
||||
...createMockAsset('opt-unloaded'),
|
||||
user_metadata: { note: 'before' } as Record<string, unknown>
|
||||
}
|
||||
vi.mocked(assetService.updateAsset).mockResolvedValueOnce({
|
||||
...original,
|
||||
user_metadata: { note: 'server' }
|
||||
})
|
||||
|
||||
await store.updateAssetMetadata(
|
||||
original,
|
||||
{ note: 'after' },
|
||||
'CheckpointLoaderSimple'
|
||||
)
|
||||
|
||||
expect(vi.mocked(assetService.updateAsset)).toHaveBeenCalledWith(
|
||||
'opt-unloaded',
|
||||
{ user_metadata: { note: 'after' } }
|
||||
)
|
||||
})
|
||||
|
||||
it('leaves unrelated cached assets alone during optimistic metadata update', async () => {
|
||||
const store = useAssetsStore()
|
||||
const cached = {
|
||||
...createMockAsset('opt-cached'),
|
||||
user_metadata: { note: 'cached' } as Record<string, unknown>
|
||||
}
|
||||
const missing = {
|
||||
...createMockAsset('opt-missing-from-cache'),
|
||||
user_metadata: { note: 'before' } as Record<string, unknown>
|
||||
}
|
||||
|
||||
vi.mocked(assetService.getAssetsForNodeType).mockResolvedValueOnce([
|
||||
cached
|
||||
])
|
||||
await store.updateModelsForNodeType('CheckpointLoaderSimple')
|
||||
vi.mocked(assetService.updateAsset).mockResolvedValueOnce({
|
||||
...missing,
|
||||
user_metadata: { note: 'server' }
|
||||
})
|
||||
|
||||
await store.updateAssetMetadata(
|
||||
missing,
|
||||
{ note: 'after' },
|
||||
'CheckpointLoaderSimple'
|
||||
)
|
||||
|
||||
expect(
|
||||
store.getAssets('CheckpointLoaderSimple')[0].user_metadata
|
||||
).toEqual({
|
||||
note: 'cached'
|
||||
})
|
||||
})
|
||||
|
||||
it('reflects the server response in the cache after a successful update', async () => {
|
||||
const store = useAssetsStore()
|
||||
const original = {
|
||||
@@ -1237,6 +1552,31 @@ describe('assetsStore - Model Assets Cache (Cloud)', () => {
|
||||
'featured'
|
||||
])
|
||||
})
|
||||
|
||||
it('calls only the remove endpoint when there are no tags to add', async () => {
|
||||
const store = useAssetsStore()
|
||||
const asset = createMockAsset('tags-remove-only', ['models', 'archived'])
|
||||
|
||||
vi.mocked(assetService.getAssetsForNodeType).mockResolvedValueOnce([
|
||||
asset
|
||||
])
|
||||
await store.updateModelsForNodeType('CheckpointLoaderSimple')
|
||||
|
||||
vi.mocked(assetService.removeAssetTags).mockResolvedValueOnce({
|
||||
total_tags: ['models']
|
||||
})
|
||||
|
||||
await store.updateAssetTags(asset, ['models'], 'CheckpointLoaderSimple')
|
||||
|
||||
expect(vi.mocked(assetService.removeAssetTags)).toHaveBeenCalledWith(
|
||||
'tags-remove-only',
|
||||
['archived']
|
||||
)
|
||||
expect(vi.mocked(assetService.addAssetTags)).not.toHaveBeenCalled()
|
||||
expect(store.getAssets('CheckpointLoaderSimple')[0].tags).toEqual([
|
||||
'models'
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('updateAssetTags partial-failure compensation', () => {
|
||||
@@ -1351,6 +1691,36 @@ describe('assetsStore - Model Assets Cache (Cloud)', () => {
|
||||
expect(store.hasCategory('tag:models')).toBe(false)
|
||||
})
|
||||
|
||||
it('keeps unrelated tag caches when compensation fails with a cache key', async () => {
|
||||
const store = useAssetsStore()
|
||||
const asset = createMockAsset('tags-target-fail', ['models', 'loras'])
|
||||
const otherAsset = createMockAsset('tags-other', ['models'])
|
||||
|
||||
vi.mocked(assetService.getAssetsForNodeType).mockResolvedValueOnce([
|
||||
asset
|
||||
])
|
||||
await store.updateModelsForNodeType('LoraLoader')
|
||||
vi.mocked(assetService.getAssetsByTag).mockResolvedValueOnce([otherAsset])
|
||||
await store.updateModelsForTag('models')
|
||||
|
||||
vi.mocked(assetService.removeAssetTags).mockResolvedValueOnce({
|
||||
removed: ['loras'],
|
||||
total_tags: ['models']
|
||||
})
|
||||
vi.mocked(assetService.addAssetTags)
|
||||
.mockRejectedValueOnce(new Error('500 add failed'))
|
||||
.mockRejectedValueOnce(new Error('503 compensation failed'))
|
||||
|
||||
await store.updateAssetTags(
|
||||
asset,
|
||||
['models', 'checkpoints'],
|
||||
'LoraLoader'
|
||||
)
|
||||
|
||||
expect(store.hasCategory('loras')).toBe(false)
|
||||
expect(store.hasCategory('tag:models')).toBe(true)
|
||||
})
|
||||
|
||||
it('does not attempt compensation when only the add was attempted', async () => {
|
||||
const store = useAssetsStore()
|
||||
const asset = createMockAsset('tags-add-only-fail', ['models'])
|
||||
@@ -1483,9 +1853,78 @@ describe('assetsStore - Deletion State and Input Mapping', () => {
|
||||
const store = useAssetsStore()
|
||||
expect(store.getInputName('unknown.png')).toBe('unknown.png')
|
||||
})
|
||||
|
||||
it('ignores input assets without hashes', async () => {
|
||||
mockIsCloud.value = true
|
||||
try {
|
||||
setActivePinia(createTestingPinia({ stubActions: false }))
|
||||
const store = useAssetsStore()
|
||||
|
||||
vi.mocked(assetService.getAssetsByTag).mockResolvedValueOnce([
|
||||
{
|
||||
id: 'input-1',
|
||||
name: 'plain.png',
|
||||
tags: ['input']
|
||||
}
|
||||
])
|
||||
await store.updateInputs()
|
||||
|
||||
expect(store.getInputName('plain.png')).toBe('plain.png')
|
||||
} finally {
|
||||
mockIsCloud.value = false
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('updateInputs cloud routing', () => {
|
||||
it('reads input files from the internal API when isCloud is false', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue(
|
||||
fromAny<Response, unknown>({
|
||||
ok: true,
|
||||
json: async () => ['input-a.png', 'input-b.png']
|
||||
})
|
||||
)
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
try {
|
||||
const store = useAssetsStore()
|
||||
|
||||
await store.updateInputs()
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledWith(
|
||||
'http://localhost:3000/files/input',
|
||||
{ headers: { 'Comfy-User': 'test-user' } }
|
||||
)
|
||||
expect(store.inputAssets.map((asset) => asset.name)).toEqual([
|
||||
'input-a.png',
|
||||
'input-b.png'
|
||||
])
|
||||
} finally {
|
||||
vi.unstubAllGlobals()
|
||||
}
|
||||
})
|
||||
|
||||
it('records internal input API failures', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue(
|
||||
fromAny<Response, unknown>({
|
||||
ok: false
|
||||
})
|
||||
)
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
try {
|
||||
const consoleSpy = vi
|
||||
.spyOn(console, 'error')
|
||||
.mockImplementation(() => {})
|
||||
const store = useAssetsStore()
|
||||
|
||||
await store.updateInputs()
|
||||
|
||||
expect(store.inputError).toBeInstanceOf(Error)
|
||||
consoleSpy.mockRestore()
|
||||
} finally {
|
||||
vi.unstubAllGlobals()
|
||||
}
|
||||
})
|
||||
|
||||
it('reads from assetService.getAssetsByTag with limit 100 when isCloud is true', async () => {
|
||||
mockIsCloud.value = true
|
||||
try {
|
||||
@@ -1586,6 +2025,18 @@ describe('assetsStore - Flat Output Assets (cloud-only)', () => {
|
||||
expect(store.flatOutputHasMore).toBe(false)
|
||||
})
|
||||
|
||||
it('does not load more flat outputs when there are no more pages', async () => {
|
||||
vi.mocked(assetService.getAssetsPageByTag).mockResolvedValueOnce(
|
||||
makePage([makeAsset('a1', 'one.png')])
|
||||
)
|
||||
|
||||
const store = useAssetsStore()
|
||||
await store.updateFlatOutputs()
|
||||
await store.loadMoreFlatOutputs()
|
||||
|
||||
expect(assetService.getAssetsPageByTag).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('threads the minted cursor into after on loadMore and omits offset', async () => {
|
||||
vi.mocked(assetService.getAssetsPageByTag)
|
||||
.mockResolvedValueOnce(
|
||||
@@ -1800,4 +2251,26 @@ describe('assetsStore - Flat Output Assets (cloud-only)', () => {
|
||||
|
||||
expect(store.flatOutputAssets.map((x) => x.id)).toEqual(['shared-1'])
|
||||
})
|
||||
|
||||
it('ignores concurrent load more calls while one is active', async () => {
|
||||
vi.mocked(assetService.getAssetsPageByTag).mockResolvedValueOnce(
|
||||
makePage([makeAsset('a1', 'f1.png')], { hasMore: true })
|
||||
)
|
||||
const store = useAssetsStore()
|
||||
await store.updateFlatOutputs()
|
||||
|
||||
let resolvePage!: (page: AssetResponse) => void
|
||||
vi.mocked(assetService.getAssetsPageByTag).mockReturnValueOnce(
|
||||
new Promise<AssetResponse>((resolve) => {
|
||||
resolvePage = resolve
|
||||
})
|
||||
)
|
||||
|
||||
const first = store.loadMoreFlatOutputs()
|
||||
const second = store.loadMoreFlatOutputs()
|
||||
resolvePage(makePage([makeAsset('a2', 'f2.png')]))
|
||||
await Promise.all([first, second])
|
||||
|
||||
expect(assetService.getAssetsPageByTag).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { createTestingPinia } from '@pinia/testing'
|
||||
import { fromAny } from '@total-typescript/shoehorn'
|
||||
import { setActivePinia } from 'pinia'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { ref } from 'vue'
|
||||
@@ -177,9 +178,10 @@ describe('useComfyRegistryStore', () => {
|
||||
|
||||
it('should return null when fetching a pack with null ID', async () => {
|
||||
const store = useComfyRegistryStore()
|
||||
vi.spyOn(store.getPackById, 'call').mockResolvedValueOnce(null)
|
||||
|
||||
const result = await store.getPackById.call(null!)
|
||||
const result = await store.getPackById.call(
|
||||
fromAny<Parameters<typeof store.getPackById.call>[0], unknown>(null)
|
||||
)
|
||||
|
||||
expect(result).toBeNull()
|
||||
expect(mockRegistryService.getPackById).not.toHaveBeenCalled()
|
||||
@@ -206,6 +208,56 @@ describe('useComfyRegistryStore', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('should reuse cached packs by ID', async () => {
|
||||
const store = useComfyRegistryStore()
|
||||
|
||||
await store.getPacksByIds.call(['test-pack-id'])
|
||||
const result = await store.getPacksByIds.call(['test-pack-id'])
|
||||
|
||||
expect(result).toEqual([mockNodePack])
|
||||
expect(mockRegistryService.listAllPacks).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('should ignore missing packs by ID', async () => {
|
||||
mockRegistryService.listAllPacks.mockResolvedValueOnce({
|
||||
nodes: [fromAny<components['schemas']['Node'], unknown>({ name: 'bad' })],
|
||||
total: 1,
|
||||
page: 1,
|
||||
limit: 10
|
||||
})
|
||||
const store = useComfyRegistryStore()
|
||||
|
||||
const result = await store.getPacksByIds.call(['unknown-pack-id'])
|
||||
|
||||
expect(result).toEqual([])
|
||||
})
|
||||
|
||||
it('should handle empty pack lookup responses', async () => {
|
||||
mockRegistryService.listAllPacks.mockResolvedValueOnce(null)
|
||||
const store = useComfyRegistryStore()
|
||||
|
||||
const result = await store.getPacksByIds.call(['unknown-pack-id'])
|
||||
|
||||
expect(result).toEqual([])
|
||||
})
|
||||
|
||||
it('should filter undefined pack IDs before lookup', async () => {
|
||||
const store = useComfyRegistryStore()
|
||||
|
||||
const result = await store.getPacksByIds.call(
|
||||
fromAny<components['schemas']['Node']['id'][], unknown>([
|
||||
'test-pack-id',
|
||||
undefined
|
||||
])
|
||||
)
|
||||
|
||||
expect(result).toEqual([mockNodePack])
|
||||
expect(mockRegistryService.listAllPacks).toHaveBeenCalledWith(
|
||||
{ node_id: ['test-pack-id'] },
|
||||
expect.any(Object)
|
||||
)
|
||||
})
|
||||
|
||||
describe('inferPackFromNodeName', () => {
|
||||
it('should fetch a pack by comfy node name', async () => {
|
||||
const store = useComfyRegistryStore()
|
||||
|
||||
@@ -137,6 +137,88 @@ describe('useModelStore', () => {
|
||||
expect(model.resolution).toBe('')
|
||||
})
|
||||
|
||||
it('keeps the default model metadata when the server returns null', async () => {
|
||||
enableMocks()
|
||||
vi.mocked(api.viewMetadata).mockResolvedValueOnce(null)
|
||||
store = useModelStore()
|
||||
await store.loadModelFolders()
|
||||
const folderStore = await store.getLoadedModelFolder('checkpoints')
|
||||
const model = folderStore!.models['0/sdxl.safetensors']
|
||||
|
||||
await model.load()
|
||||
|
||||
expect(model.title).toBe('sdxl')
|
||||
expect(model.has_loaded_metadata).toBe(false)
|
||||
})
|
||||
|
||||
it('loads model metadata once', async () => {
|
||||
enableMocks()
|
||||
store = useModelStore()
|
||||
await store.loadModelFolders()
|
||||
const folderStore = await store.getLoadedModelFolder('checkpoints')
|
||||
const model = folderStore!.models['0/sdxl.safetensors']
|
||||
|
||||
await model.load()
|
||||
await model.load()
|
||||
|
||||
expect(api.viewMetadata).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('keeps the default title when the first metadata key is empty', async () => {
|
||||
enableMocks()
|
||||
vi.mocked(api.viewMetadata).mockResolvedValueOnce({
|
||||
'modelspec.title': '',
|
||||
display_name: 'Fallback title'
|
||||
})
|
||||
store = useModelStore()
|
||||
await store.loadModelFolders()
|
||||
const folderStore = await store.getLoadedModelFolder('checkpoints')
|
||||
const model = folderStore!.models['0/sdxl.safetensors']
|
||||
|
||||
await model.load()
|
||||
|
||||
expect(model.title).toBe('sdxl')
|
||||
})
|
||||
|
||||
it('returns null for unknown loaded model folders', async () => {
|
||||
enableMocks()
|
||||
store = useModelStore()
|
||||
await store.loadModelFolders()
|
||||
|
||||
await expect(store.getLoadedModelFolder('missing')).resolves.toBeNull()
|
||||
})
|
||||
|
||||
it('should read metadata from suffixed keys and ignore null values', async () => {
|
||||
enableMocks()
|
||||
vi.mocked(api.viewMetadata).mockResolvedValueOnce({
|
||||
'custom.modelspec.title': 'Namespaced title',
|
||||
'custom.modelspec.author': null,
|
||||
'custom.modelspec.tags': null
|
||||
})
|
||||
store = useModelStore()
|
||||
await store.loadModelFolders()
|
||||
const folderStore = await store.getLoadedModelFolder('checkpoints')
|
||||
const model = folderStore!.models['0/sdxl.safetensors']
|
||||
|
||||
await model.load()
|
||||
|
||||
expect(model.title).toBe('Namespaced title')
|
||||
expect(model.author).toBe('')
|
||||
expect(model.tags).toEqual([''])
|
||||
})
|
||||
|
||||
it('should keep extensions for non-safetensors files', async () => {
|
||||
enableMocks()
|
||||
vi.mocked(api.getModels).mockResolvedValueOnce([
|
||||
{ name: 'notes.txt', pathIndex: 0 }
|
||||
])
|
||||
store = useModelStore()
|
||||
await store.loadModelFolders()
|
||||
const folderStore = await store.getLoadedModelFolder('checkpoints')
|
||||
|
||||
expect(folderStore!.models['0/notes.txt'].title).toBe('notes.txt')
|
||||
})
|
||||
|
||||
it('should cache model information', async () => {
|
||||
enableMocks()
|
||||
store = useModelStore()
|
||||
@@ -209,6 +291,23 @@ describe('useModelStore', () => {
|
||||
expect(api.getModelFolders).toHaveBeenCalledTimes(2)
|
||||
expect(api.getModels).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('does not reload previously loaded folders that disappear', async () => {
|
||||
enableMocks()
|
||||
store = useModelStore()
|
||||
await store.loadModelFolders()
|
||||
await store.getLoadedModelFolder('checkpoints')
|
||||
vi.mocked(api.getModelFolders).mockResolvedValueOnce([
|
||||
{ name: 'vae', folders: ['/path/to/vae'] }
|
||||
])
|
||||
|
||||
await store.refresh()
|
||||
|
||||
expect(store.modelFolders.map((folder) => folder.directory)).toEqual([
|
||||
'vae'
|
||||
])
|
||||
expect(api.getModels).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('API switching functionality', () => {
|
||||
|
||||
@@ -138,6 +138,22 @@ describe('useModelToNodeStore', () => {
|
||||
expect(provider?.key).toBe('ckpt_name')
|
||||
})
|
||||
|
||||
it('omits providers whose node definition is unavailable from reverse lookup', () => {
|
||||
const modelToNodeStore = useModelToNodeStore()
|
||||
modelToNodeStore.modelToNodeMap = {
|
||||
missing: [
|
||||
new ModelNodeProvider(
|
||||
undefined as unknown as ComfyNodeDefImpl,
|
||||
'model'
|
||||
)
|
||||
]
|
||||
}
|
||||
|
||||
expect(modelToNodeStore.getRegisteredNodeTypes()).not.toHaveProperty(
|
||||
'undefined'
|
||||
)
|
||||
})
|
||||
|
||||
it('should return undefined for unregistered model type', () => {
|
||||
const modelToNodeStore = useModelToNodeStore()
|
||||
modelToNodeStore.registerDefaults()
|
||||
@@ -577,6 +593,22 @@ describe('useModelToNodeStore', () => {
|
||||
expect(modelToNodeStore.getCategoryForNodeType('')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('skips providers without node definitions during category lookup', () => {
|
||||
const modelToNodeStore = useModelToNodeStore()
|
||||
modelToNodeStore.modelToNodeMap = {
|
||||
missing: [
|
||||
new ModelNodeProvider(
|
||||
undefined as unknown as ComfyNodeDefImpl,
|
||||
'model'
|
||||
)
|
||||
]
|
||||
}
|
||||
|
||||
expect(
|
||||
modelToNodeStore.getCategoryForNodeType('MissingNode')
|
||||
).toBeUndefined()
|
||||
})
|
||||
|
||||
it('maps the IC-LoRA Loader Model Only node to loras so its lora_name dropdown uses the cloud asset browser (FE-838)', () => {
|
||||
const modelToNodeStore = useModelToNodeStore()
|
||||
modelToNodeStore.registerDefaults()
|
||||
|
||||
@@ -1,16 +1,24 @@
|
||||
import { createTestingPinia } from '@pinia/testing'
|
||||
import { fromAny } from '@total-typescript/shoehorn'
|
||||
import axios from 'axios'
|
||||
import { setActivePinia } from 'pinia'
|
||||
import { beforeEach, describe, expect, it } from 'vitest'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import { promoteValueWidgetViaSubgraphInput } from '@/core/graph/subgraph/promotionUtils'
|
||||
import { LGraphNode } from '@/lib/litegraph/src/litegraph'
|
||||
import { LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
import type { LGraph, SubgraphNode } from '@/lib/litegraph/src/litegraph'
|
||||
import {
|
||||
createTestSubgraph,
|
||||
createTestSubgraphNode
|
||||
} from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers'
|
||||
import type { ComfyNodeDef } from '@/schemas/nodeDefSchema'
|
||||
import { useNodeDefStore } from '@/stores/nodeDefStore'
|
||||
import {
|
||||
ComfyNodeDefImpl,
|
||||
buildNodeDefTree,
|
||||
createDummyFolderNodeDef,
|
||||
useNodeDefStore,
|
||||
useNodeFrequencyStore
|
||||
} from '@/stores/nodeDefStore'
|
||||
import type { NodeDefFilter } from '@/stores/nodeDefStore'
|
||||
|
||||
describe('useNodeDefStore', () => {
|
||||
@@ -21,6 +29,10 @@ describe('useNodeDefStore', () => {
|
||||
store = useNodeDefStore()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
const createMockNodeDef = (
|
||||
overrides: Partial<ComfyNodeDef> = {}
|
||||
): ComfyNodeDef => ({
|
||||
@@ -39,7 +51,112 @@ describe('useNodeDefStore', () => {
|
||||
...overrides
|
||||
})
|
||||
|
||||
describe('ComfyNodeDefImpl', () => {
|
||||
it('migrates defaultInput options and applies constructor fallbacks', () => {
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
const nodeDef = createMockNodeDef({
|
||||
category: '_for_testing/coverage',
|
||||
deprecated: undefined,
|
||||
dev_only: undefined,
|
||||
experimental: undefined,
|
||||
help: undefined,
|
||||
input: {
|
||||
required: { prompt: ['STRING', { defaultInput: true }] },
|
||||
optional: { seed_override: ['INT', { defaultInput: true }] }
|
||||
}
|
||||
})
|
||||
|
||||
const impl = new ComfyNodeDefImpl(nodeDef)
|
||||
|
||||
expect(warn).toHaveBeenCalledTimes(2)
|
||||
expect(impl.help).toBe('')
|
||||
expect(impl.experimental).toBe(true)
|
||||
expect(impl.dev_only).toBe(false)
|
||||
expect(impl.inputs.seed_override.forceInput).toBe(true)
|
||||
})
|
||||
|
||||
it('derives empty-category node paths and lifecycle badges', () => {
|
||||
const deprecated = new ComfyNodeDefImpl(
|
||||
createMockNodeDef({ category: '', deprecated: undefined })
|
||||
)
|
||||
const beta = new ComfyNodeDefImpl(
|
||||
createMockNodeDef({ experimental: true })
|
||||
)
|
||||
const dev = new ComfyNodeDefImpl(createMockNodeDef({ dev_only: true }))
|
||||
const normal = new ComfyNodeDefImpl(createMockNodeDef())
|
||||
|
||||
expect(deprecated.nodePath).toBe('TestNode')
|
||||
expect(deprecated.isDummyFolder).toBe(false)
|
||||
expect(deprecated.nodeLifeCycleBadgeText).toBe('[DEPR]')
|
||||
expect(beta.nodeLifeCycleBadgeText).toBe('[BETA]')
|
||||
expect(dev.nodeLifeCycleBadgeText).toBe('[DEV]')
|
||||
expect(normal.nodeLifeCycleBadgeText).toBe('')
|
||||
})
|
||||
|
||||
it('defaults missing legacy input and output fields', () => {
|
||||
const nodeDef = new ComfyNodeDefImpl(
|
||||
fromAny<ComfyNodeDef, unknown>({
|
||||
name: 'FallbackNode',
|
||||
display_name: 'Fallback Node',
|
||||
category: 'test',
|
||||
python_module: 'test_module',
|
||||
description: 'Test node',
|
||||
output_node: false
|
||||
})
|
||||
)
|
||||
|
||||
expect(nodeDef.input).toEqual({})
|
||||
expect(nodeDef.output).toEqual([])
|
||||
})
|
||||
|
||||
it('post-processes search scores with node frequency', async () => {
|
||||
vi.spyOn(axios, 'get').mockResolvedValue({ data: { TestNode: 7 } })
|
||||
const frequencyStore = useNodeFrequencyStore()
|
||||
await frequencyStore.loadNodeFrequencies()
|
||||
const nodeDef = new ComfyNodeDefImpl(createMockNodeDef())
|
||||
|
||||
expect(nodeDef.postProcessSearchScores([10, 4, 2])).toEqual([
|
||||
10, -7, 4, 2
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('tree helpers', () => {
|
||||
it('builds node definition trees from default and custom paths', () => {
|
||||
const nodeDef = new ComfyNodeDefImpl(
|
||||
createMockNodeDef({ name: 'TreeNode', category: 'root/branch' })
|
||||
)
|
||||
|
||||
expect(buildNodeDefTree([nodeDef]).children?.[0].label).toBe('root')
|
||||
expect(
|
||||
buildNodeDefTree([nodeDef], {
|
||||
pathExtractor: (node) => ['custom', node.name]
|
||||
}).children?.[0].label
|
||||
).toBe('custom')
|
||||
})
|
||||
|
||||
it('normalizes dummy folder paths', () => {
|
||||
expect(createDummyFolderNodeDef('folder/').category).toBe('folder')
|
||||
expect(createDummyFolderNodeDef('folder').category).toBe('folder')
|
||||
})
|
||||
})
|
||||
|
||||
describe('filter registry', () => {
|
||||
it('updates LiteGraph skip state for registered dev-only nodes', () => {
|
||||
const registeredNodeTypes = LiteGraph.registered_node_types
|
||||
LiteGraph.registered_node_types = fromAny({
|
||||
DevNode: { nodeData: { dev_only: true }, skip_list: false },
|
||||
NormalNode: { nodeData: {}, skip_list: false }
|
||||
})
|
||||
|
||||
setActivePinia(createTestingPinia({ stubActions: false }))
|
||||
useNodeDefStore()
|
||||
|
||||
expect(LiteGraph.registered_node_types.DevNode.skip_list).toBe(true)
|
||||
expect(LiteGraph.registered_node_types.NormalNode.skip_list).toBe(false)
|
||||
LiteGraph.registered_node_types = registeredNodeTypes
|
||||
})
|
||||
|
||||
it('should register a new filter', () => {
|
||||
const filter: NodeDefFilter = {
|
||||
id: 'test.filter',
|
||||
@@ -287,6 +404,26 @@ describe('useNodeDefStore', () => {
|
||||
})
|
||||
|
||||
describe('allNodeDefsByName', () => {
|
||||
it('keeps existing ComfyNodeDefImpl instances during updates', () => {
|
||||
const nodeDef = new ComfyNodeDefImpl(
|
||||
createMockNodeDef({ name: 'ExistingImpl' })
|
||||
)
|
||||
|
||||
store.updateNodeDefs([nodeDef])
|
||||
|
||||
expect(store.nodeDefsByName.ExistingImpl.name).toBe('ExistingImpl')
|
||||
expect(store.nodeDefsByDisplayName['Test Node'].name).toBe('ExistingImpl')
|
||||
})
|
||||
|
||||
it('adds one node definition to the name and display-name indexes', () => {
|
||||
store.addNodeDef(
|
||||
createMockNodeDef({ name: 'AddedNode', display_name: 'Added Node' })
|
||||
)
|
||||
|
||||
expect(store.nodeDefsByName.AddedNode.name).toBe('AddedNode')
|
||||
expect(store.nodeDefsByDisplayName['Added Node'].name).toBe('AddedNode')
|
||||
})
|
||||
|
||||
it('should include all node defs by name', () => {
|
||||
const node1 = createMockNodeDef({ name: 'Node1' })
|
||||
const node2 = createMockNodeDef({ name: 'Node2' })
|
||||
@@ -336,6 +473,39 @@ describe('useNodeDefStore', () => {
|
||||
expect(store.allNodeDefsByName).toHaveProperty('Normal')
|
||||
expect(store.allNodeDefsByName).toHaveProperty('Deprecated')
|
||||
})
|
||||
|
||||
it('derives unique input and output data types', () => {
|
||||
store.updateNodeDefs([
|
||||
createMockNodeDef({
|
||||
input: {
|
||||
required: { image: ['IMAGE', {}] },
|
||||
optional: { mask: ['MASK', {}] }
|
||||
},
|
||||
output: ['IMAGE', 'LATENT'],
|
||||
output_is_list: [false, false],
|
||||
output_name: ['image', 'latent']
|
||||
})
|
||||
])
|
||||
|
||||
expect([...store.nodeDataTypes].sort()).toEqual([
|
||||
'IMAGE',
|
||||
'LATENT',
|
||||
'MASK'
|
||||
])
|
||||
})
|
||||
|
||||
it('looks up node definitions from graph nodes and returns null for misses', () => {
|
||||
store.updateNodeDefs([createMockNodeDef({ name: 'KnownNode' })])
|
||||
|
||||
expect(
|
||||
store.fromLGraphNode(new LGraphNode('KnownNode', 'KnownNode'))?.name
|
||||
).toBe('KnownNode')
|
||||
expect(store.fromLGraphNode(new LGraphNode('', ''))).toBeNull()
|
||||
expect(
|
||||
store.getInputSpecForWidget(new LGraphNode('Missing', 'Missing'), 'x')
|
||||
).toBeUndefined()
|
||||
expect(store.nodeSearchService).toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('subgraph widget input specs', () => {
|
||||
@@ -389,6 +559,94 @@ describe('useNodeDefStore', () => {
|
||||
expect(spec?.type).toBe('STRING')
|
||||
expect(spec?.default).toBeUndefined()
|
||||
})
|
||||
|
||||
it('returns undefined for missing promoted subgraph inputs', () => {
|
||||
const host = setupPromotedPrompt(
|
||||
createMockNodeDef({
|
||||
name: 'PromptNode',
|
||||
input: { required: { prompt: ['STRING', {}] } }
|
||||
})
|
||||
)
|
||||
|
||||
expect(store.getInputSpecForWidget(host, 'missing')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('returns undefined when a subgraph input is not promoted', () => {
|
||||
const subgraph = createTestSubgraph()
|
||||
const host = createTestSubgraphNode(subgraph)
|
||||
host.addInput('raw', 'STRING')
|
||||
|
||||
expect(store.getInputSpecForWidget(host, 'raw')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('returns undefined when a promoted source no longer resolves', () => {
|
||||
const host = setupPromotedPrompt(
|
||||
createMockNodeDef({
|
||||
name: 'PromptNode',
|
||||
input: { required: { prompt: ['STRING', {}] } }
|
||||
})
|
||||
)
|
||||
host.subgraph.nodes[0].widgets = []
|
||||
|
||||
expect(store.getInputSpecForWidget(host, 'prompt')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('returns undefined when concrete promoted widget resolution fails', async () => {
|
||||
const resolver =
|
||||
await import('@/core/graph/subgraph/resolveConcretePromotedWidget')
|
||||
vi.spyOn(resolver, 'resolveConcretePromotedWidget').mockReturnValue(
|
||||
fromAny({ status: 'failure', failure: 'missing-widget' })
|
||||
)
|
||||
const host = setupPromotedPrompt(
|
||||
createMockNodeDef({
|
||||
name: 'PromptNode',
|
||||
input: { required: { prompt: ['STRING', {}] } }
|
||||
})
|
||||
)
|
||||
|
||||
expect(store.getInputSpecForWidget(host, 'prompt')).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('node frequency store', () => {
|
||||
it('loads frequencies once and exposes top matching node definitions', async () => {
|
||||
const get = vi.spyOn(axios, 'get').mockResolvedValue({
|
||||
data: { RankedNode: 10, MissingNode: 3 }
|
||||
})
|
||||
store.updateNodeDefs([createMockNodeDef({ name: 'RankedNode' })])
|
||||
const frequencyStore = useNodeFrequencyStore()
|
||||
|
||||
await frequencyStore.loadNodeFrequencies()
|
||||
await frequencyStore.loadNodeFrequencies()
|
||||
|
||||
expect(get).toHaveBeenCalledTimes(1)
|
||||
expect(frequencyStore.isLoaded).toBe(true)
|
||||
expect(frequencyStore.getNodeFrequencyByName('RankedNode')).toBe(10)
|
||||
expect(
|
||||
frequencyStore.getNodeFrequency(
|
||||
new ComfyNodeDefImpl(createMockNodeDef({ name: 'RankedNode' }))
|
||||
)
|
||||
).toBe(10)
|
||||
expect(frequencyStore.getNodeFrequencyByName('Unknown')).toBe(0)
|
||||
expect(frequencyStore.topNodeDefs.map((nodeDef) => nodeDef.name)).toEqual(
|
||||
['RankedNode']
|
||||
)
|
||||
})
|
||||
|
||||
it('leaves frequency state unloaded when loading fails', async () => {
|
||||
const error = new Error('boom')
|
||||
vi.spyOn(axios, 'get').mockRejectedValue(error)
|
||||
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
const frequencyStore = useNodeFrequencyStore()
|
||||
|
||||
await frequencyStore.loadNodeFrequencies()
|
||||
|
||||
expect(frequencyStore.isLoaded).toBe(false)
|
||||
expect(errorSpy).toHaveBeenCalledWith(
|
||||
'Error loading node frequencies:',
|
||||
error
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('performance', () => {
|
||||
|
||||
@@ -3,15 +3,41 @@ import { fromAny } from '@total-typescript/shoehorn'
|
||||
import { setActivePinia } from 'pinia'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
|
||||
import type { LGraphNode, SubgraphNode } from '@/lib/litegraph/src/litegraph'
|
||||
import { LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||||
import type { ExecutedWsMessage } from '@/schemas/apiSchema'
|
||||
import { app } from '@/scripts/app'
|
||||
import { useNodeOutputStore } from '@/stores/nodeOutputStore'
|
||||
import { createNodeExecutionId } from '@/types/nodeIdentification'
|
||||
import {
|
||||
createNodeExecutionId,
|
||||
createNodeLocatorId
|
||||
} from '@/types/nodeIdentification'
|
||||
import type { NodeExecutionId, NodeLocatorId } from '@/types/nodeIdentification'
|
||||
import { toNodeId } from '@/types/nodeId'
|
||||
import * as litegraphUtil from '@/utils/litegraphUtil'
|
||||
|
||||
const {
|
||||
mockApiURL,
|
||||
mockExecutionIdToNodeLocatorId,
|
||||
mockNodeIdToNodeLocatorId,
|
||||
mockNodeToNodeLocatorId,
|
||||
mockReleaseSharedObjectUrl,
|
||||
mockRetainSharedObjectUrl
|
||||
} = vi.hoisted(() => ({
|
||||
mockApiURL: vi.fn((path: string) => `api${path}`),
|
||||
mockExecutionIdToNodeLocatorId: vi.fn(
|
||||
(_rootGraph: unknown, id: NodeExecutionId) => id as unknown as NodeLocatorId
|
||||
),
|
||||
mockNodeIdToNodeLocatorId: vi.fn(
|
||||
(id: string | number) => String(id) as NodeLocatorId
|
||||
),
|
||||
mockNodeToNodeLocatorId: vi.fn(
|
||||
(node: { id: string | number }) => String(node.id) as NodeLocatorId
|
||||
),
|
||||
mockReleaseSharedObjectUrl: vi.fn(),
|
||||
mockRetainSharedObjectUrl: vi.fn()
|
||||
}))
|
||||
|
||||
const mockResolveNode = vi.fn()
|
||||
|
||||
vi.mock('@/utils/litegraphUtil', () => ({
|
||||
@@ -20,11 +46,25 @@ vi.mock('@/utils/litegraphUtil', () => ({
|
||||
resolveNode: (...args: unknown[]) => mockResolveNode(...args)
|
||||
}))
|
||||
|
||||
vi.mock('@/scripts/api', () => ({
|
||||
api: {
|
||||
apiURL: (...args: Parameters<typeof mockApiURL>) => mockApiURL(...args)
|
||||
}
|
||||
}))
|
||||
|
||||
vi.mock('@/utils/objectUrlUtil', () => ({
|
||||
releaseSharedObjectUrl: (...args: [string | undefined]) =>
|
||||
mockReleaseSharedObjectUrl(...args),
|
||||
retainSharedObjectUrl: (...args: [string | undefined]) =>
|
||||
mockRetainSharedObjectUrl(...args)
|
||||
}))
|
||||
|
||||
const mockGetNodeById = vi.fn()
|
||||
|
||||
vi.mock('@/scripts/app', () => ({
|
||||
app: {
|
||||
getPreviewFormatParam: vi.fn(() => '&format=test_webp'),
|
||||
getRandParam: vi.fn(() => '&rand=1'),
|
||||
rootGraph: {
|
||||
getNodeById: (...args: unknown[]) => mockGetNodeById(...args)
|
||||
},
|
||||
@@ -49,13 +89,31 @@ const createMockOutputs = (
|
||||
): ExecutedWsMessage['output'] => ({ images })
|
||||
|
||||
vi.mock('@/utils/graphTraversalUtil', () => ({
|
||||
executionIdToNodeLocatorId: vi.fn((_rootGraph: unknown, id: string) => id)
|
||||
executionIdToNodeLocatorId: (
|
||||
...args: Parameters<typeof mockExecutionIdToNodeLocatorId>
|
||||
) => mockExecutionIdToNodeLocatorId(...args)
|
||||
}))
|
||||
|
||||
beforeEach(() => {
|
||||
mockExecutionIdToNodeLocatorId.mockImplementation(
|
||||
(_rootGraph: unknown, id: NodeExecutionId) => id as unknown as NodeLocatorId
|
||||
)
|
||||
mockNodeIdToNodeLocatorId.mockImplementation(
|
||||
(id: string | number) => String(id) as NodeLocatorId
|
||||
)
|
||||
mockNodeToNodeLocatorId.mockImplementation(
|
||||
(node: { id: string | number }) => String(node.id) as NodeLocatorId
|
||||
)
|
||||
})
|
||||
|
||||
vi.mock('@/platform/workflow/management/stores/workflowStore', () => ({
|
||||
useWorkflowStore: vi.fn(() => ({
|
||||
nodeIdToNodeLocatorId: vi.fn((id: string | number) => String(id)),
|
||||
nodeToNodeLocatorId: vi.fn((node: { id: number }) => String(node.id))
|
||||
nodeIdToNodeLocatorId: (
|
||||
...args: Parameters<typeof mockNodeIdToNodeLocatorId>
|
||||
) => mockNodeIdToNodeLocatorId(...args),
|
||||
nodeToNodeLocatorId: (
|
||||
...args: Parameters<typeof mockNodeToNodeLocatorId>
|
||||
) => mockNodeToNodeLocatorId(...args)
|
||||
}))
|
||||
}))
|
||||
|
||||
@@ -780,6 +838,19 @@ describe('nodeOutputStore setNodeOutputs (widget path)', () => {
|
||||
expect(store.nodeOutputs['5']?.images?.[0]?.type).toBe('input')
|
||||
})
|
||||
|
||||
it('ignores widget outputs when no locator can be resolved', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const node = createMockNode({ id: 5 })
|
||||
mockNodeToNodeLocatorId.mockReturnValueOnce(
|
||||
fromAny<NodeLocatorId, undefined>(undefined)
|
||||
)
|
||||
|
||||
store.setNodeOutputs(node, 'test.png')
|
||||
|
||||
expect(store.nodeOutputs).toEqual({})
|
||||
expect(app.nodeOutputs).toEqual({})
|
||||
})
|
||||
|
||||
it('should skip empty array of filenames after createOutputs', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const node = createMockNode({ id: 5 })
|
||||
@@ -789,6 +860,470 @@ describe('nodeOutputStore setNodeOutputs (widget path)', () => {
|
||||
expect(store.nodeOutputs['5']).toBeUndefined()
|
||||
expect(app.nodeOutputs['5']).toBeUndefined()
|
||||
})
|
||||
|
||||
it('stores direct result items without wrapping them as image outputs', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const node = createMockNode({ id: 5 })
|
||||
|
||||
store.setNodeOutputs(node, { filename: 'direct.png', type: 'temp' })
|
||||
|
||||
expect(store.nodeOutputs['5']).toEqual({
|
||||
filename: 'direct.png',
|
||||
type: 'temp'
|
||||
})
|
||||
})
|
||||
|
||||
it('marks animated webp and png filenames when requested', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const node = createMockNode({ id: 5 })
|
||||
|
||||
store.setNodeOutputs(node, ['clip.webp', 'still.jpg', 'mask.png'], {
|
||||
folder: 'output',
|
||||
isAnimated: true
|
||||
})
|
||||
|
||||
expect(store.nodeOutputs['5']?.animated).toEqual([true, false, true])
|
||||
expect(store.nodeOutputs['5']?.images?.map((image) => image.type)).toEqual([
|
||||
'output',
|
||||
'output',
|
||||
'output'
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('nodeOutputStore image URLs', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createTestingPinia({ stubActions: false }))
|
||||
vi.clearAllMocks()
|
||||
vi.mocked(litegraphUtil.isAnimatedOutput).mockReturnValue(false)
|
||||
vi.mocked(litegraphUtil.isVideoNode).mockReturnValue(false)
|
||||
app.nodeOutputs = {}
|
||||
app.nodePreviewImages = {}
|
||||
})
|
||||
|
||||
it('returns stored preview URLs before output URLs', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const node = createMockNode({ id: 5 })
|
||||
|
||||
store.setNodePreviewsByLocatorId(createNodeLocatorId(null, toNodeId(5)), [
|
||||
'blob:preview'
|
||||
])
|
||||
|
||||
expect(store.getNodeImageUrls(node)).toEqual(['blob:preview'])
|
||||
expect(mockApiURL).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('builds view URLs from output images', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const node = createMockNode({ id: 5 })
|
||||
app.nodeOutputs['5'] = createMockOutputs(
|
||||
fromAny([{ filename: 'a.png', subfolder: 'x', type: 'temp' }, null])
|
||||
)
|
||||
|
||||
expect(store.getNodeImageUrls(node)).toEqual([
|
||||
'api/view?filename=a.png&subfolder=x&type=temp&format=test_webp&rand=1'
|
||||
])
|
||||
})
|
||||
|
||||
it('returns undefined when a node has neither previews nor outputs', () => {
|
||||
const store = useNodeOutputStore()
|
||||
|
||||
expect(store.getNodeImageUrls(createMockNode({ id: 5 }))).toBeUndefined()
|
||||
})
|
||||
|
||||
it('returns execution previews before execution output URLs', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const node = createMockNode({ id: 5 })
|
||||
const executionId = createNodeExecutionId([toNodeId(5)])
|
||||
|
||||
store.setNodePreviewsByExecutionId(executionId, ['blob:preview'])
|
||||
|
||||
expect(store.getNodeImageUrlsByExecutionId(executionId, node)).toEqual([
|
||||
'blob:preview'
|
||||
])
|
||||
expect(store.latestPreview).toEqual(['blob:preview'])
|
||||
expect(mockApiURL).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('falls back to execution output URLs when no preview exists', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const node = createMockNode({ id: 5 })
|
||||
const executionId = createNodeExecutionId([toNodeId(5)])
|
||||
|
||||
store.setNodeOutputsByExecutionId(
|
||||
executionId,
|
||||
createMockOutputs([{ filename: 'result.png', type: 'temp' }])
|
||||
)
|
||||
|
||||
expect(store.getNodeImageUrlsByExecutionId(executionId, node)).toEqual([
|
||||
'api/view?filename=result.png&type=temp&format=test_webp&rand=1'
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('nodeOutputStore locator misses', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createTestingPinia({ stubActions: false }))
|
||||
vi.clearAllMocks()
|
||||
app.nodeOutputs = {}
|
||||
app.nodePreviewImages = {}
|
||||
})
|
||||
|
||||
it('keeps execution operations inert when no locator can be resolved', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const executionId = createNodeExecutionId([toNodeId(5)])
|
||||
mockExecutionIdToNodeLocatorId.mockReturnValue(
|
||||
fromAny<NodeLocatorId, undefined>(undefined)
|
||||
)
|
||||
|
||||
store.setNodeOutputsByExecutionId(
|
||||
executionId,
|
||||
createMockOutputs([{ filename: 'result.png' }])
|
||||
)
|
||||
store.setNodePreviewsByExecutionId(executionId, ['blob:preview'])
|
||||
store.revokePreviewsByExecutionId(executionId)
|
||||
|
||||
expect(store.getNodeOutputByExecutionId(executionId)).toBeUndefined()
|
||||
expect(store.getNodePreviewImagesByExecutionId(executionId)).toBeUndefined()
|
||||
expect(store.nodeOutputs).toEqual({})
|
||||
expect(store.nodePreviewImages).toEqual({})
|
||||
})
|
||||
})
|
||||
|
||||
describe('nodeOutputStore merge branches', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createTestingPinia({ stubActions: false }))
|
||||
vi.clearAllMocks()
|
||||
app.nodeOutputs = {}
|
||||
app.nodePreviewImages = {}
|
||||
})
|
||||
|
||||
it('sets outputs when merge is requested without existing output', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const executionId = createNodeExecutionId([toNodeId(5)])
|
||||
const output = createMockOutputs([{ filename: 'first.png' }])
|
||||
|
||||
store.setNodeOutputsByExecutionId(executionId, output, { merge: true })
|
||||
|
||||
expect(store.nodeOutputs[executionId]).toEqual(output)
|
||||
})
|
||||
|
||||
it('ignores null outputs', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const executionId = createNodeExecutionId([toNodeId(5)])
|
||||
|
||||
store.setNodeOutputsByExecutionId(
|
||||
executionId,
|
||||
fromAny<ExecutedWsMessage['output'], unknown>(null)
|
||||
)
|
||||
|
||||
expect(store.nodeOutputs[executionId]).toBeUndefined()
|
||||
})
|
||||
|
||||
it('overwrites non-array fields during merge', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const executionId = createNodeExecutionId([toNodeId(5)])
|
||||
const firstOutput: ExecutedWsMessage['output'] = {
|
||||
images: [{ filename: 'first.png' }],
|
||||
text: 'old'
|
||||
}
|
||||
|
||||
store.setNodeOutputsByExecutionId(executionId, firstOutput)
|
||||
store.setNodeOutputsByExecutionId(
|
||||
executionId,
|
||||
{ text: ['new'] },
|
||||
{ merge: true }
|
||||
)
|
||||
|
||||
expect(store.nodeOutputs[executionId]?.images).toEqual([
|
||||
{ filename: 'first.png' }
|
||||
])
|
||||
expect(store.nodeOutputs[executionId]?.text).toEqual(['new'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('nodeOutputStore previews and removal', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createTestingPinia({ stubActions: false }))
|
||||
vi.clearAllMocks()
|
||||
app.nodeOutputs = {}
|
||||
app.nodePreviewImages = {}
|
||||
})
|
||||
|
||||
it('releases old previews and retains new previews on replacement', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const locatorId = createNodeLocatorId(null, toNodeId(5))
|
||||
|
||||
store.setNodePreviewsByLocatorId(locatorId, ['blob:first'])
|
||||
store.setNodePreviewsByLocatorId(locatorId, ['blob:second'])
|
||||
|
||||
expect(mockReleaseSharedObjectUrl).toHaveBeenCalledWith('blob:first')
|
||||
expect(mockRetainSharedObjectUrl).toHaveBeenCalledWith('blob:second')
|
||||
expect(store.nodePreviewImages[locatorId]).toEqual(['blob:second'])
|
||||
})
|
||||
|
||||
it('starts with an empty preview map when legacy previews are missing', () => {
|
||||
app.nodePreviewImages = fromAny(undefined)
|
||||
|
||||
const store = useNodeOutputStore()
|
||||
|
||||
expect(store.nodePreviewImages).toEqual({})
|
||||
})
|
||||
|
||||
it('cancels scheduled revocation when a newer preview arrives', async () => {
|
||||
vi.useFakeTimers()
|
||||
const store = useNodeOutputStore()
|
||||
const executionId = createNodeExecutionId([toNodeId(5)])
|
||||
|
||||
store.setNodePreviewsByExecutionId(executionId, ['blob:first'])
|
||||
store.revokePreviewsByExecutionId(executionId)
|
||||
store.setNodePreviewsByExecutionId(executionId, ['blob:second'])
|
||||
await vi.advanceTimersByTimeAsync(400)
|
||||
vi.useRealTimers()
|
||||
|
||||
expect(store.nodePreviewImages[executionId]).toEqual(['blob:second'])
|
||||
expect(mockReleaseSharedObjectUrl).not.toHaveBeenCalledWith('blob:second')
|
||||
})
|
||||
|
||||
it('revokes locator previews and clears preview state', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const locatorId = createNodeLocatorId(null, toNodeId(5))
|
||||
|
||||
store.setNodePreviewsByLocatorId(locatorId, ['blob:first'])
|
||||
store.revokePreviewsByLocatorId(locatorId)
|
||||
|
||||
expect(mockReleaseSharedObjectUrl).toHaveBeenCalledWith('blob:first')
|
||||
expect(store.nodePreviewImages[locatorId]).toBeUndefined()
|
||||
expect(app.nodePreviewImages[locatorId]).toBeUndefined()
|
||||
})
|
||||
|
||||
it('leaves state unchanged when revoking a locator with no previews', () => {
|
||||
const store = useNodeOutputStore()
|
||||
|
||||
store.revokePreviewsByLocatorId(createNodeLocatorId(null, toNodeId(5)))
|
||||
|
||||
expect(mockReleaseSharedObjectUrl).not.toHaveBeenCalled()
|
||||
expect(store.nodePreviewImages).toEqual({})
|
||||
})
|
||||
|
||||
it('skips non-iterable preview entries when revoking all previews', () => {
|
||||
const store = useNodeOutputStore()
|
||||
app.nodePreviewImages = fromAny({
|
||||
'5': {},
|
||||
'6': ['blob:preview']
|
||||
})
|
||||
|
||||
store.revokeAllPreviews()
|
||||
|
||||
expect(mockReleaseSharedObjectUrl).toHaveBeenCalledTimes(1)
|
||||
expect(mockReleaseSharedObjectUrl).toHaveBeenCalledWith('blob:preview')
|
||||
expect(store.nodePreviewImages).toEqual({})
|
||||
})
|
||||
|
||||
it('revokes subgraph previews for the parent node and child nodes', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const subgraphId = '11111111-1111-1111-1111-111111111111'
|
||||
const parentLocatorId = createNodeLocatorId(null, toNodeId(9))
|
||||
const childLocatorId = createNodeLocatorId(subgraphId, toNodeId(10))
|
||||
const subgraphNode = fromAny<SubgraphNode, unknown>({
|
||||
id: toNodeId(9),
|
||||
graph: { isRootGraph: true },
|
||||
subgraph: {
|
||||
id: subgraphId,
|
||||
nodes: [createMockNode({ id: 10 })]
|
||||
}
|
||||
})
|
||||
|
||||
store.setNodePreviewsByLocatorId(parentLocatorId, ['blob:parent'])
|
||||
store.setNodePreviewsByLocatorId(childLocatorId, ['blob:child'])
|
||||
store.revokeSubgraphPreviews(subgraphNode)
|
||||
|
||||
expect(store.nodePreviewImages[parentLocatorId]).toBeUndefined()
|
||||
expect(store.nodePreviewImages[childLocatorId]).toBeUndefined()
|
||||
expect(mockReleaseSharedObjectUrl).toHaveBeenCalledWith('blob:parent')
|
||||
expect(mockReleaseSharedObjectUrl).toHaveBeenCalledWith('blob:child')
|
||||
})
|
||||
|
||||
it('uses the parent graph id for non-root subgraph preview revocation', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const graphId = '22222222-2222-2222-2222-222222222222'
|
||||
const subgraphId = '33333333-3333-3333-3333-333333333333'
|
||||
const parentLocatorId = createNodeLocatorId(graphId, toNodeId(9))
|
||||
const subgraphNode = fromAny<SubgraphNode, unknown>({
|
||||
id: toNodeId(9),
|
||||
graph: { id: graphId, isRootGraph: false },
|
||||
subgraph: { id: subgraphId, nodes: [] }
|
||||
})
|
||||
|
||||
store.setNodePreviewsByLocatorId(parentLocatorId, ['blob:parent'])
|
||||
store.revokeSubgraphPreviews(subgraphNode)
|
||||
|
||||
expect(store.nodePreviewImages[parentLocatorId]).toBeUndefined()
|
||||
})
|
||||
|
||||
it('leaves previews alone when a subgraph node has no parent graph', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const locatorId = createNodeLocatorId(null, toNodeId(9))
|
||||
const subgraphNode = fromAny<SubgraphNode, unknown>({
|
||||
graph: undefined,
|
||||
subgraph: { nodes: [] }
|
||||
})
|
||||
|
||||
store.setNodePreviewsByLocatorId(locatorId, ['blob:parent'])
|
||||
store.revokeSubgraphPreviews(subgraphNode)
|
||||
|
||||
expect(store.nodePreviewImages[locatorId]).toEqual(['blob:parent'])
|
||||
})
|
||||
|
||||
it('removes outputs and previews for a node id', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const executionId = createNodeExecutionId([toNodeId(5)])
|
||||
|
||||
store.setNodeOutputsByExecutionId(
|
||||
executionId,
|
||||
createMockOutputs([{ filename: 'result.png' }])
|
||||
)
|
||||
store.setNodePreviewsByExecutionId(executionId, ['blob:preview'])
|
||||
|
||||
expect(store.removeNodeOutputs(toNodeId(5))).toBe(true)
|
||||
expect(store.nodeOutputs[executionId]).toBeUndefined()
|
||||
expect(store.nodePreviewImages[executionId]).toBeUndefined()
|
||||
expect(mockReleaseSharedObjectUrl).toHaveBeenCalledWith('blob:preview')
|
||||
})
|
||||
|
||||
it('returns false when removing outputs for a node with no outputs', () => {
|
||||
const store = useNodeOutputStore()
|
||||
|
||||
expect(store.removeNodeOutputsForNode(createMockNode({ id: 9 }))).toBe(
|
||||
false
|
||||
)
|
||||
})
|
||||
|
||||
it('returns false when a node id cannot resolve to a locator', () => {
|
||||
const store = useNodeOutputStore()
|
||||
mockNodeIdToNodeLocatorId.mockReturnValueOnce(
|
||||
fromAny<NodeLocatorId, undefined>(undefined)
|
||||
)
|
||||
|
||||
expect(store.removeNodeOutputs(toNodeId(9))).toBe(false)
|
||||
})
|
||||
|
||||
it('removes preview state even when preview entries are not iterable', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const executionId = createNodeExecutionId([toNodeId(5)])
|
||||
|
||||
store.setNodeOutputsByExecutionId(
|
||||
executionId,
|
||||
createMockOutputs([{ filename: 'result.png' }])
|
||||
)
|
||||
app.nodePreviewImages[executionId] = fromAny({})
|
||||
store.nodePreviewImages[executionId] = fromAny({})
|
||||
|
||||
expect(store.removeNodeOutputs(toNodeId(5))).toBe(true)
|
||||
expect(store.nodePreviewImages[executionId]).toBeUndefined()
|
||||
expect(mockReleaseSharedObjectUrl).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('nodeOutputStore output refresh', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createTestingPinia({ stubActions: false }))
|
||||
vi.clearAllMocks()
|
||||
app.nodeOutputs = {}
|
||||
app.nodePreviewImages = {}
|
||||
})
|
||||
|
||||
it('updates stored output images from legacy node images', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const node = createMockNode({
|
||||
id: 5,
|
||||
images: [{ filename: 'new.png', type: 'temp' }]
|
||||
})
|
||||
|
||||
store.setNodeOutputsByExecutionId(
|
||||
createNodeExecutionId([toNodeId(5)]),
|
||||
createMockOutputs([{ filename: 'old.png', type: 'temp' }])
|
||||
)
|
||||
store.updateNodeImages(node)
|
||||
|
||||
expect(store.nodeOutputs['5']?.images).toEqual([
|
||||
{ filename: 'new.png', type: 'temp' }
|
||||
])
|
||||
})
|
||||
|
||||
it('ignores legacy image updates when the node has no images', () => {
|
||||
const store = useNodeOutputStore()
|
||||
|
||||
store.updateNodeImages(createMockNode({ id: 5 }))
|
||||
|
||||
expect(store.nodeOutputs).toEqual({})
|
||||
})
|
||||
|
||||
it('ignores legacy image updates when no locator exists', () => {
|
||||
const store = useNodeOutputStore()
|
||||
mockNodeIdToNodeLocatorId.mockReturnValueOnce(
|
||||
fromAny<NodeLocatorId, undefined>(undefined)
|
||||
)
|
||||
|
||||
store.updateNodeImages(
|
||||
createMockNode({ id: 5, images: [{ filename: 'new.png' }] })
|
||||
)
|
||||
|
||||
expect(store.nodeOutputs).toEqual({})
|
||||
})
|
||||
|
||||
it('ignores legacy image updates when no output exists', () => {
|
||||
const store = useNodeOutputStore()
|
||||
|
||||
store.updateNodeImages(
|
||||
createMockNode({ id: 5, images: [{ filename: 'new.png' }] })
|
||||
)
|
||||
|
||||
expect(store.nodeOutputs).toEqual({})
|
||||
})
|
||||
|
||||
it('copies app outputs into reactive state during refresh', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const node = createMockNode({ id: 5 })
|
||||
const output = createMockOutputs([{ filename: 'result.png' }])
|
||||
app.nodeOutputs['5'] = output
|
||||
|
||||
store.refreshNodeOutputs(node)
|
||||
|
||||
expect(store.nodeOutputs['5']).toEqual(output)
|
||||
expect(store.nodeOutputs['5']).not.toBe(output)
|
||||
})
|
||||
|
||||
it('does not refresh when a node has no locator', () => {
|
||||
const store = useNodeOutputStore()
|
||||
mockNodeToNodeLocatorId.mockReturnValueOnce(
|
||||
fromAny<NodeLocatorId, undefined>(undefined)
|
||||
)
|
||||
|
||||
store.refreshNodeOutputs(createMockNode({ id: 5 }))
|
||||
|
||||
expect(store.nodeOutputs).toEqual({})
|
||||
})
|
||||
|
||||
it('does not refresh when app has no output for the node', () => {
|
||||
const store = useNodeOutputStore()
|
||||
|
||||
store.refreshNodeOutputs(createMockNode({ id: 5 }))
|
||||
|
||||
expect(store.nodeOutputs).toEqual({})
|
||||
})
|
||||
|
||||
it('keeps unresolved restore output ids as their original ids', () => {
|
||||
const store = useNodeOutputStore()
|
||||
const output = createMockOutputs([{ filename: 'saved.png' }])
|
||||
mockExecutionIdToNodeLocatorId.mockReturnValueOnce(
|
||||
fromAny<NodeLocatorId, undefined>(undefined)
|
||||
)
|
||||
|
||||
store.restoreOutputs({ missing: output })
|
||||
|
||||
expect(store.nodeOutputs.missing).toEqual(output)
|
||||
})
|
||||
})
|
||||
|
||||
describe('nodeOutputStore syncLegacyNodeImgs', () => {
|
||||
@@ -894,4 +1429,20 @@ describe('nodeOutputStore syncLegacyNodeImgs', () => {
|
||||
expect(mockNode.imgs).toEqual([mockImg])
|
||||
expect(mockNode.imageIndex).toBe(0)
|
||||
})
|
||||
|
||||
it('copies output images onto the legacy node', () => {
|
||||
LiteGraph.vueNodesMode = true
|
||||
const store = useNodeOutputStore()
|
||||
const mockNode = createMockNode({ id: 1 })
|
||||
const mockImg = document.createElement('img')
|
||||
mockResolveNode.mockReturnValue(mockNode)
|
||||
|
||||
store.setNodeOutputsByExecutionId(
|
||||
createNodeExecutionId([toNodeId(1)]),
|
||||
createMockOutputs([{ filename: 'result.png', type: 'temp' }])
|
||||
)
|
||||
store.syncLegacyNodeImgs(toNodeId(1), mockImg)
|
||||
|
||||
expect(mockNode.images).toEqual([{ filename: 'result.png', type: 'temp' }])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -95,6 +95,22 @@ describe(usePreviewExposureStore, () => {
|
||||
|
||||
expect(store.getExposures(rootGraphA, hostA)).toEqual([])
|
||||
})
|
||||
|
||||
it('clears only the requested host when other hosts remain', () => {
|
||||
store.addExposure(rootGraphA, hostA, {
|
||||
sourceNodeId: '42',
|
||||
sourcePreviewName: 'preview'
|
||||
})
|
||||
store.addExposure(rootGraphA, hostB, {
|
||||
sourceNodeId: '43',
|
||||
sourcePreviewName: 'preview'
|
||||
})
|
||||
|
||||
store.setExposures(rootGraphA, hostA, [])
|
||||
|
||||
expect(store.getExposures(rootGraphA, hostA)).toEqual([])
|
||||
expect(store.getExposures(rootGraphA, hostB)).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('removeExposure', () => {
|
||||
@@ -122,6 +138,12 @@ describe(usePreviewExposureStore, () => {
|
||||
store.removeExposure(rootGraphA, hostA, 'does-not-exist')
|
||||
expect(store.getExposures(rootGraphA, hostA)).toEqual(before)
|
||||
})
|
||||
|
||||
it('is a no-op for an unknown host', () => {
|
||||
store.removeExposure(rootGraphA, 'missing-host', 'preview')
|
||||
|
||||
expect(store.getExposures(rootGraphA, 'missing-host')).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('getExposuresAsPromotionShape', () => {
|
||||
|
||||
Reference in New Issue
Block a user