|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { parseWorkflowStateForPersistence } from '@/lib/workflows/persistence/save-normalized-state' |
| 6 | + |
| 7 | +/** |
| 8 | + * A checkpoint blob as the revert route builds it: JSONB-derived blocks and edges, plus a |
| 9 | + * real `Date` for `deployedAt`. |
| 10 | + */ |
| 11 | +function checkpointState(overrides?: Record<string, unknown>) { |
| 12 | + return { |
| 13 | + blocks: { |
| 14 | + 'block-1': { |
| 15 | + id: 'block-1', |
| 16 | + type: 'starter', |
| 17 | + name: 'Start', |
| 18 | + position: { x: 0, y: 0 }, |
| 19 | + subBlocks: {}, |
| 20 | + outputs: {}, |
| 21 | + enabled: true, |
| 22 | + }, |
| 23 | + }, |
| 24 | + edges: [], |
| 25 | + loops: {}, |
| 26 | + parallels: {}, |
| 27 | + isDeployed: false, |
| 28 | + lastSaved: 1_754_000_000_000, |
| 29 | + ...overrides, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +describe('parseWorkflowStateForPersistence', () => { |
| 34 | + /** |
| 35 | + * The revert used to reach this schema by POSTing the blob over HTTP, so every value |
| 36 | + * arrived JSON-serialized. In-process the blob keeps its runtime types. Both forms must |
| 37 | + * parse identically, or a checkpoint that reverted before would start failing. |
| 38 | + */ |
| 39 | + it('accepts a Date for deployedAt exactly as it accepted the serialized string', () => { |
| 40 | + const deployedAt = new Date('2026-01-02T03:04:05.678Z') |
| 41 | + |
| 42 | + const fromDate = parseWorkflowStateForPersistence(checkpointState({ deployedAt })) |
| 43 | + const overTheWire = parseWorkflowStateForPersistence( |
| 44 | + JSON.parse(JSON.stringify(checkpointState({ deployedAt }))) |
| 45 | + ) |
| 46 | + |
| 47 | + expect(fromDate.success).toBe(true) |
| 48 | + expect(overTheWire.success).toBe(true) |
| 49 | + expect(fromDate.data?.deployedAt).toEqual(deployedAt) |
| 50 | + expect(overTheWire.data?.deployedAt).toEqual(fromDate.data?.deployedAt) |
| 51 | + }) |
| 52 | + |
| 53 | + it('round-trips a JSONB-shaped blob without dropping blocks or edges', () => { |
| 54 | + const state = checkpointState() |
| 55 | + |
| 56 | + const parsed = parseWorkflowStateForPersistence(state) |
| 57 | + |
| 58 | + expect(parsed.success).toBe(true) |
| 59 | + expect(Object.keys(parsed.data?.blocks ?? {})).toEqual(['block-1']) |
| 60 | + expect(parsed.data?.lastSaved).toBe(1_754_000_000_000) |
| 61 | + }) |
| 62 | + |
| 63 | + it('accepts a null deployedAt, which the revert passes for a never-deployed checkpoint', () => { |
| 64 | + const parsed = parseWorkflowStateForPersistence(checkpointState({ deployedAt: null })) |
| 65 | + |
| 66 | + expect(parsed.success).toBe(true) |
| 67 | + expect(parsed.data?.deployedAt).toBeNull() |
| 68 | + }) |
| 69 | + |
| 70 | + /** The validation the removed HTTP hop used to provide: a malformed blob must not be written. */ |
| 71 | + it('rejects a blob whose blocks are malformed', () => { |
| 72 | + const parsed = parseWorkflowStateForPersistence({ |
| 73 | + blocks: { 'block-1': { id: 'block-1' } }, |
| 74 | + edges: [], |
| 75 | + }) |
| 76 | + |
| 77 | + expect(parsed.success).toBe(false) |
| 78 | + }) |
| 79 | + |
| 80 | + it('rejects a blob missing blocks entirely', () => { |
| 81 | + expect(parseWorkflowStateForPersistence({ edges: [] }).success).toBe(false) |
| 82 | + }) |
| 83 | +}) |
0 commit comments