|
| 1 | +// Pins the WB-340 fix: when nodes load before the palette, they are validated |
| 2 | +// against an empty definition set and their errors are dropped. Once the palette |
| 3 | +// arrives, `refreshNodesErrorsIfNeeded` must re-run validation and repair the |
| 4 | +// stored nodes, while staying a no-op when nothing actually changed. |
| 5 | +import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest'; |
| 6 | + |
| 7 | +import { setCustomPaletteNodes } from '../../../data/palette'; |
| 8 | +import type { WorkflowBuilderNode } from '../../../node/node-data'; |
| 9 | +import { mockNodeDelay } from '../../../utils/validation/get-node-errors.mock'; |
| 10 | +import { resetWorkflowStore, useStore } from '../../store'; |
| 11 | +import { refreshNodesErrorsIfNeeded } from './actions'; |
| 12 | + |
| 13 | +// Mirrors the definition the consumer's palette would supply for `delay` nodes; |
| 14 | +// `description` is required, so a node missing it must surface a validation error. |
| 15 | +const delayDefinition = { |
| 16 | + type: 'delay', |
| 17 | + label: 'Delay', |
| 18 | + description: 'Pause the workflow', |
| 19 | + icon: 'Timer', |
| 20 | + defaultPropertiesData: mockNodeDelay.data.properties, |
| 21 | + schema: { |
| 22 | + type: 'object', |
| 23 | + properties: { |
| 24 | + label: { type: 'string' }, |
| 25 | + description: { type: 'string' }, |
| 26 | + status: { type: 'string' }, |
| 27 | + duration: { type: 'object' }, |
| 28 | + errors: { type: 'array' }, |
| 29 | + type: { type: 'string' }, |
| 30 | + }, |
| 31 | + required: ['label', 'description'], |
| 32 | + }, |
| 33 | + uischema: { type: 'VerticalLayout', elements: [] }, |
| 34 | +}; |
| 35 | + |
| 36 | +/** `mockNodeDelay` stripped of its required `description`, with stale (empty) errors. */ |
| 37 | +function invalidNode(): WorkflowBuilderNode { |
| 38 | + const { description: _description, ...properties } = mockNodeDelay.data.properties; |
| 39 | + return { |
| 40 | + ...mockNodeDelay, |
| 41 | + data: { ...mockNodeDelay.data, properties: { ...properties, errors: [] } }, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +describe('refreshNodesErrorsIfNeeded', () => { |
| 46 | + beforeAll(() => { |
| 47 | + setCustomPaletteNodes([delayDefinition as never]); |
| 48 | + }); |
| 49 | + |
| 50 | + afterAll(() => { |
| 51 | + setCustomPaletteNodes(null); |
| 52 | + }); |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + resetWorkflowStore(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('repairs errors that were skipped while the palette was unavailable', () => { |
| 59 | + // Simulates the race: the node was validated before the palette existed, so |
| 60 | + // its `errors` are empty despite being invalid. |
| 61 | + useStore.setState({ nodes: [invalidNode()] }); |
| 62 | + |
| 63 | + refreshNodesErrorsIfNeeded(); |
| 64 | + |
| 65 | + const errors = useStore.getState().nodes[0].data.properties.errors; |
| 66 | + expect(errors).toHaveLength(1); |
| 67 | + expect(errors).toEqual(expect.arrayContaining([expect.objectContaining({ keyword: 'required' })])); |
| 68 | + }); |
| 69 | + |
| 70 | + it('leaves the nodes array untouched when no errors change', () => { |
| 71 | + // `mockNodeDelay` is valid against the definition, so re-validation is a no-op. |
| 72 | + useStore.setState({ nodes: [mockNodeDelay] }); |
| 73 | + const before = useStore.getState().nodes; |
| 74 | + |
| 75 | + refreshNodesErrorsIfNeeded(); |
| 76 | + |
| 77 | + // Same reference: the deep-equal guard must skip the `setState` entirely. |
| 78 | + expect(useStore.getState().nodes).toBe(before); |
| 79 | + }); |
| 80 | + |
| 81 | + it('does nothing when the store has no nodes', () => { |
| 82 | + const before = useStore.getState().nodes; |
| 83 | + |
| 84 | + refreshNodesErrorsIfNeeded(); |
| 85 | + |
| 86 | + expect(useStore.getState().nodes).toBe(before); |
| 87 | + }); |
| 88 | +}); |
0 commit comments