|
| 1 | +import type { NodeExecutionState } from 'features/nodes/types/invocation'; |
| 2 | +import { zNodeStatus } from 'features/nodes/types/invocation'; |
| 3 | +import type { S } from 'services/api/types'; |
| 4 | +import { describe, expect, it } from 'vitest'; |
| 5 | + |
| 6 | +import { |
| 7 | + getUpdatedNodeExecutionStateOnInvocationComplete, |
| 8 | + getUpdatedNodeExecutionStateOnInvocationProgress, |
| 9 | + getUpdatedNodeExecutionStateOnInvocationStarted, |
| 10 | +} from './nodeExecutionState'; |
| 11 | + |
| 12 | +const buildNodeExecutionState = (overrides: Partial<NodeExecutionState> = {}): NodeExecutionState => ({ |
| 13 | + nodeId: 'node-1', |
| 14 | + status: zNodeStatus.enum.PENDING, |
| 15 | + progress: null, |
| 16 | + progressImage: null, |
| 17 | + outputs: [], |
| 18 | + error: null, |
| 19 | + ...overrides, |
| 20 | +}); |
| 21 | + |
| 22 | +const buildInvocationStartedEvent = ( |
| 23 | + overrides: Partial<S['InvocationStartedEvent']> = {} |
| 24 | +): S['InvocationStartedEvent'] => |
| 25 | + ({ |
| 26 | + queue_id: 'default', |
| 27 | + item_id: 1, |
| 28 | + batch_id: 'batch-1', |
| 29 | + origin: 'workflows', |
| 30 | + destination: 'gallery', |
| 31 | + user_id: 'user-1', |
| 32 | + session_id: 'session-1', |
| 33 | + invocation_source_id: 'node-1', |
| 34 | + invocation: { |
| 35 | + id: 'prepared-node-1', |
| 36 | + type: 'add', |
| 37 | + }, |
| 38 | + ...overrides, |
| 39 | + }) as S['InvocationStartedEvent']; |
| 40 | + |
| 41 | +const buildInvocationProgressEvent = ( |
| 42 | + overrides: Partial<S['InvocationProgressEvent']> = {} |
| 43 | +): S['InvocationProgressEvent'] => |
| 44 | + ({ |
| 45 | + queue_id: 'default', |
| 46 | + item_id: 1, |
| 47 | + batch_id: 'batch-1', |
| 48 | + origin: 'workflows', |
| 49 | + destination: 'gallery', |
| 50 | + user_id: 'user-1', |
| 51 | + session_id: 'session-1', |
| 52 | + invocation_source_id: 'node-1', |
| 53 | + invocation: { |
| 54 | + id: 'prepared-node-1', |
| 55 | + type: 'add', |
| 56 | + }, |
| 57 | + percentage: 0.42, |
| 58 | + image: { |
| 59 | + dataURL: 'data:image/png;base64,abc', |
| 60 | + width: 64, |
| 61 | + height: 64, |
| 62 | + }, |
| 63 | + message: 'working', |
| 64 | + ...overrides, |
| 65 | + }) as S['InvocationProgressEvent']; |
| 66 | + |
| 67 | +const buildInvocationCompleteEvent = ( |
| 68 | + overrides: Partial<S['InvocationCompleteEvent']> = {} |
| 69 | +): S['InvocationCompleteEvent'] => |
| 70 | + ({ |
| 71 | + queue_id: 'default', |
| 72 | + item_id: 1, |
| 73 | + batch_id: 'batch-1', |
| 74 | + origin: 'workflows', |
| 75 | + destination: 'gallery', |
| 76 | + user_id: 'user-1', |
| 77 | + session_id: 'session-1', |
| 78 | + invocation_source_id: 'node-1', |
| 79 | + invocation: { |
| 80 | + id: 'prepared-node-1', |
| 81 | + type: 'add', |
| 82 | + }, |
| 83 | + result: { |
| 84 | + type: 'integer_output', |
| 85 | + value: 42, |
| 86 | + }, |
| 87 | + ...overrides, |
| 88 | + }) as S['InvocationCompleteEvent']; |
| 89 | + |
| 90 | +describe(getUpdatedNodeExecutionStateOnInvocationStarted.name, () => { |
| 91 | + it('marks the node in progress on invocation start', () => { |
| 92 | + const updated = getUpdatedNodeExecutionStateOnInvocationStarted( |
| 93 | + buildNodeExecutionState(), |
| 94 | + buildInvocationStartedEvent(), |
| 95 | + new Set<string>() |
| 96 | + ); |
| 97 | + |
| 98 | + expect(updated?.status).toBe(zNodeStatus.enum.IN_PROGRESS); |
| 99 | + }); |
| 100 | + |
| 101 | + it('ignores a late started event after that invocation already completed', () => { |
| 102 | + const event = buildInvocationStartedEvent(); |
| 103 | + const updated = getUpdatedNodeExecutionStateOnInvocationStarted( |
| 104 | + buildNodeExecutionState({ status: zNodeStatus.enum.COMPLETED, progress: 1 }), |
| 105 | + event, |
| 106 | + new Set([`${event.item_id}:${event.invocation.id}`]) |
| 107 | + ); |
| 108 | + |
| 109 | + expect(updated).toBeUndefined(); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +describe(getUpdatedNodeExecutionStateOnInvocationProgress.name, () => { |
| 114 | + it('marks the node in progress and preserves progress updates', () => { |
| 115 | + const event = buildInvocationProgressEvent(); |
| 116 | + const updated = getUpdatedNodeExecutionStateOnInvocationProgress( |
| 117 | + buildNodeExecutionState(), |
| 118 | + event, |
| 119 | + new Set<string>() |
| 120 | + ); |
| 121 | + |
| 122 | + expect(updated?.status).toBe(zNodeStatus.enum.IN_PROGRESS); |
| 123 | + expect(updated?.progress).toBe(event.percentage); |
| 124 | + expect(updated?.progressImage).toEqual(event.image); |
| 125 | + }); |
| 126 | + |
| 127 | + it('ignores a late progress event after that invocation already completed', () => { |
| 128 | + const event = buildInvocationProgressEvent(); |
| 129 | + const updated = getUpdatedNodeExecutionStateOnInvocationProgress( |
| 130 | + buildNodeExecutionState({ status: zNodeStatus.enum.COMPLETED, progress: 1 }), |
| 131 | + event, |
| 132 | + new Set([`${event.item_id}:${event.invocation.id}`]) |
| 133 | + ); |
| 134 | + |
| 135 | + expect(updated).toBeUndefined(); |
| 136 | + }); |
| 137 | +}); |
| 138 | + |
| 139 | +describe(getUpdatedNodeExecutionStateOnInvocationComplete.name, () => { |
| 140 | + it('records a completed invocation result once', () => { |
| 141 | + const event = buildInvocationCompleteEvent(); |
| 142 | + const completedInvocationKeys = new Set<string>(); |
| 143 | + |
| 144 | + const updated = getUpdatedNodeExecutionStateOnInvocationComplete( |
| 145 | + buildNodeExecutionState({ status: zNodeStatus.enum.IN_PROGRESS, progress: 0.5 }), |
| 146 | + event, |
| 147 | + completedInvocationKeys |
| 148 | + ); |
| 149 | + |
| 150 | + expect(updated?.status).toBe(zNodeStatus.enum.COMPLETED); |
| 151 | + expect(updated?.progress).toBe(1); |
| 152 | + expect(updated?.outputs).toEqual([event.result]); |
| 153 | + expect(completedInvocationKeys).toEqual(new Set([`${event.item_id}:${event.invocation.id}`])); |
| 154 | + }); |
| 155 | + |
| 156 | + it('ignores duplicate completion events for the same invocation', () => { |
| 157 | + const event = buildInvocationCompleteEvent(); |
| 158 | + const updated = getUpdatedNodeExecutionStateOnInvocationComplete( |
| 159 | + buildNodeExecutionState({ status: zNodeStatus.enum.COMPLETED, progress: 1, outputs: [event.result] }), |
| 160 | + event, |
| 161 | + new Set([`${event.item_id}:${event.invocation.id}`]) |
| 162 | + ); |
| 163 | + |
| 164 | + expect(updated).toBeUndefined(); |
| 165 | + }); |
| 166 | + |
| 167 | + it('allows the same prepared invocation id on a different queue item', () => { |
| 168 | + const firstEvent = buildInvocationCompleteEvent({ |
| 169 | + item_id: 1, |
| 170 | + result: { type: 'integer_output', value: 1 } as unknown as S['InvocationCompleteEvent']['result'], |
| 171 | + }); |
| 172 | + const secondEvent = buildInvocationCompleteEvent({ |
| 173 | + item_id: 2, |
| 174 | + result: { type: 'integer_output', value: 2 } as unknown as S['InvocationCompleteEvent']['result'], |
| 175 | + }); |
| 176 | + const completedInvocationKeys = new Set<string>(); |
| 177 | + |
| 178 | + const firstUpdate = getUpdatedNodeExecutionStateOnInvocationComplete( |
| 179 | + buildNodeExecutionState(), |
| 180 | + firstEvent, |
| 181 | + completedInvocationKeys |
| 182 | + ); |
| 183 | + const secondUpdate = getUpdatedNodeExecutionStateOnInvocationComplete( |
| 184 | + firstUpdate, |
| 185 | + secondEvent, |
| 186 | + completedInvocationKeys |
| 187 | + ); |
| 188 | + |
| 189 | + expect(secondUpdate?.outputs).toEqual([firstEvent.result, secondEvent.result]); |
| 190 | + }); |
| 191 | +}); |
0 commit comments