|
| 1 | +import { LGraphNode } from '@comfyorg/litegraph' |
| 2 | +import { createPinia, setActivePinia } from 'pinia' |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +import { ExecutedWsMessage } from '@/schemas/apiSchema' |
| 6 | +import { app } from '@/scripts/app' |
| 7 | +import { useNodeOutputStore } from '@/stores/imagePreviewStore' |
| 8 | +import * as litegraphUtil from '@/utils/litegraphUtil' |
| 9 | + |
| 10 | +vi.mock('@/utils/litegraphUtil', () => ({ |
| 11 | + isVideoNode: vi.fn() |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/scripts/app', () => ({ |
| 15 | + app: { |
| 16 | + getPreviewFormatParam: vi.fn(() => '&format=test_webp') |
| 17 | + } |
| 18 | +})) |
| 19 | + |
| 20 | +const createMockNode = (overrides: Partial<LGraphNode> = {}): LGraphNode => |
| 21 | + ({ |
| 22 | + id: 1, |
| 23 | + type: 'TestNode', |
| 24 | + ...overrides |
| 25 | + }) as LGraphNode |
| 26 | + |
| 27 | +const createMockOutputs = ( |
| 28 | + images?: ExecutedWsMessage['output']['images'] |
| 29 | +): ExecutedWsMessage['output'] => ({ images }) |
| 30 | + |
| 31 | +describe('imagePreviewStore getPreviewParam', () => { |
| 32 | + beforeEach(() => { |
| 33 | + setActivePinia(createPinia()) |
| 34 | + vi.clearAllMocks() |
| 35 | + vi.mocked(litegraphUtil.isVideoNode).mockReturnValue(false) |
| 36 | + }) |
| 37 | + |
| 38 | + it('should return empty string if node.animatedImages is true', () => { |
| 39 | + const store = useNodeOutputStore() |
| 40 | + // @ts-expect-error `animatedImages` property is not typed |
| 41 | + const node = createMockNode({ animatedImages: true }) |
| 42 | + const outputs = createMockOutputs([{ filename: 'img.png' }]) |
| 43 | + expect(store.getPreviewParam(node, outputs)).toBe('') |
| 44 | + expect(vi.mocked(app).getPreviewFormatParam).not.toHaveBeenCalled() |
| 45 | + }) |
| 46 | + |
| 47 | + it('should return empty string if isVideoNode returns true', () => { |
| 48 | + const store = useNodeOutputStore() |
| 49 | + vi.mocked(litegraphUtil.isVideoNode).mockReturnValue(true) |
| 50 | + const node = createMockNode() |
| 51 | + const outputs = createMockOutputs([{ filename: 'img.png' }]) |
| 52 | + expect(store.getPreviewParam(node, outputs)).toBe('') |
| 53 | + expect(vi.mocked(app).getPreviewFormatParam).not.toHaveBeenCalled() |
| 54 | + }) |
| 55 | + |
| 56 | + it('should return empty string if outputs.images is undefined', () => { |
| 57 | + const store = useNodeOutputStore() |
| 58 | + const node = createMockNode() |
| 59 | + const outputs: ExecutedWsMessage['output'] = {} |
| 60 | + expect(store.getPreviewParam(node, outputs)).toBe('') |
| 61 | + expect(vi.mocked(app).getPreviewFormatParam).not.toHaveBeenCalled() |
| 62 | + }) |
| 63 | + |
| 64 | + it('should return empty string if outputs.images is empty', () => { |
| 65 | + const store = useNodeOutputStore() |
| 66 | + const node = createMockNode() |
| 67 | + const outputs = createMockOutputs([]) |
| 68 | + expect(store.getPreviewParam(node, outputs)).toBe('') |
| 69 | + expect(vi.mocked(app).getPreviewFormatParam).not.toHaveBeenCalled() |
| 70 | + }) |
| 71 | + |
| 72 | + it('should return empty string if outputs.images contains SVG images', () => { |
| 73 | + const store = useNodeOutputStore() |
| 74 | + const node = createMockNode() |
| 75 | + const outputs = createMockOutputs([{ filename: 'img.svg' }]) |
| 76 | + expect(store.getPreviewParam(node, outputs)).toBe('') |
| 77 | + expect(vi.mocked(app).getPreviewFormatParam).not.toHaveBeenCalled() |
| 78 | + }) |
| 79 | + |
| 80 | + it('should return format param for standard image outputs', () => { |
| 81 | + const store = useNodeOutputStore() |
| 82 | + const node = createMockNode() |
| 83 | + const outputs = createMockOutputs([{ filename: 'img.png' }]) |
| 84 | + expect(store.getPreviewParam(node, outputs)).toBe('&format=test_webp') |
| 85 | + expect(vi.mocked(app).getPreviewFormatParam).toHaveBeenCalledTimes(1) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should return format param for multiple standard images', () => { |
| 89 | + const store = useNodeOutputStore() |
| 90 | + const node = createMockNode() |
| 91 | + const outputs = createMockOutputs([ |
| 92 | + { filename: 'img1.png' }, |
| 93 | + { filename: 'img2.jpg' } |
| 94 | + ]) |
| 95 | + expect(store.getPreviewParam(node, outputs)).toBe('&format=test_webp') |
| 96 | + expect(vi.mocked(app).getPreviewFormatParam).toHaveBeenCalledTimes(1) |
| 97 | + }) |
| 98 | +}) |
0 commit comments