|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#26858 — on the Studio Automations page, the flow edge condition |
| 5 | + * shows what the pending draft's publish will refuse at that condition. |
| 6 | + * |
| 7 | + * The page reads the flow's draft itself (it is not the metadata edit page), |
| 8 | + * so the verdict the draft read serves since objectstack#26707 never reached |
| 9 | + * an inspector. The page now keeps that verdict, provides it to the inspector |
| 10 | + * (`DraftVerdictContext`), and reads it again after each draft save. |
| 11 | + * |
| 12 | + * The inspector is the real registered `FlowInspector`, which opens |
| 13 | + * `FlowEdgeInspector` for an edge; the canvas is a stand-in that selects an |
| 14 | + * edge and edits the flow on demand. The client is a server double whose draft |
| 15 | + * read judges a stored draft the way `expression-invalid` does: it refuses an |
| 16 | + * edge condition that does not parse, at `edges.N.condition`. |
| 17 | + */ |
| 18 | + |
| 19 | +import '@testing-library/jest-dom/vitest'; |
| 20 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 21 | +import { render, screen, fireEvent, cleanup, waitFor } from '@testing-library/react'; |
| 22 | +import { MemoryRouter } from 'react-router-dom'; |
| 23 | + |
| 24 | +const PKG = 'com.acme.app'; |
| 25 | +const BROKEN = 'record.name =='; |
| 26 | +const REFUSAL = 'Unexpected token: EOF — source: `record.name ==`'; |
| 27 | + |
| 28 | +const FLOW = { |
| 29 | + name: 'edge_cel', |
| 30 | + label: 'Edge CEL', |
| 31 | + type: 'autolaunched', |
| 32 | + status: 'active', |
| 33 | + nodes: [ |
| 34 | + { id: 'start', type: 'start', label: 'Start' }, |
| 35 | + { id: 'decide', type: 'decision', label: 'Decide' }, |
| 36 | + { id: 'end', type: 'end', label: 'End' }, |
| 37 | + ], |
| 38 | + edges: [ |
| 39 | + { id: 'e1', source: 'start', target: 'decide' }, |
| 40 | + { id: 'e2', source: 'decide', target: 'end', type: 'conditional', condition: BROKEN }, |
| 41 | + ], |
| 42 | +}; |
| 43 | + |
| 44 | +const server = vi.hoisted(() => ({ draft: null as Record<string, unknown> | null, draftReads: 0 })); |
| 45 | + |
| 46 | +/** The publish verdict on a stored draft: an edge condition that does not parse is refused at its path. */ |
| 47 | +function verdictOf(body: Record<string, unknown>) { |
| 48 | + const edges = (body.edges ?? []) as Array<{ condition?: string }>; |
| 49 | + const errors = edges.flatMap((e, i) => |
| 50 | + e.condition === BROKEN ? [{ path: `edges.${i}.condition`, message: REFUSAL, code: 'expression-invalid' }] : [], |
| 51 | + ); |
| 52 | + return errors.length ? { valid: false, errors } : { valid: true }; |
| 53 | +} |
| 54 | + |
| 55 | +const mockClient = vi.hoisted(() => ({ |
| 56 | + list: vi.fn(async () => [{ name: 'edge_cel', label: 'Edge CEL' }]), |
| 57 | + listDrafts: vi.fn(async () => []), |
| 58 | + listTypes: vi.fn(async () => ({ entries: [] })), |
| 59 | + get: vi.fn(async () => null), |
| 60 | + references: vi.fn(async () => []), |
| 61 | + layered: vi.fn(async () => ({ code: null, overlay: null, overlayScope: null, effective: null, editable: true })), |
| 62 | + getDraft: vi.fn(async (type: string, name: string) => { |
| 63 | + server.draftReads += 1; |
| 64 | + if (!server.draft) { |
| 65 | + throw Object.assign(new Error(`No pending draft exists for ${type}/${name}.`), { code: 'NO_DRAFT', status: 404 }); |
| 66 | + } |
| 67 | + return { type, name, item: { ...server.draft, _draft: true, _diagnostics: verdictOf(server.draft) } }; |
| 68 | + }), |
| 69 | + save: vi.fn(async (type: string, name: string, item: unknown) => { |
| 70 | + server.draft = JSON.parse(JSON.stringify(item)) as Record<string, unknown>; |
| 71 | + return { type, name, item }; |
| 72 | + }), |
| 73 | + publish: vi.fn(async () => ({ success: true })), |
| 74 | + reset: vi.fn(async () => ({})), |
| 75 | +})); |
| 76 | + |
| 77 | +vi.mock('../metadata-admin/useMetadata', async (importOriginal) => { |
| 78 | + const mod = await importOriginal<typeof import('../metadata-admin/useMetadata')>(); |
| 79 | + return { ...mod, useMetadataClient: () => mockClient, useMetadataTypes: () => ({ entries: [] }) }; |
| 80 | +}); |
| 81 | + |
| 82 | +vi.mock('./packages-io', async (importOriginal) => { |
| 83 | + const mod = await importOriginal<typeof import('./packages-io')>(); |
| 84 | + return { ...mod, fetchPackages: vi.fn(async () => []) }; |
| 85 | +}); |
| 86 | + |
| 87 | +vi.mock('@object-ui/react', async (importOriginal) => { |
| 88 | + const mod = await importOriginal<typeof import('@object-ui/react')>(); |
| 89 | + return { ...mod, useAdapter: () => dataSource }; |
| 90 | +}); |
| 91 | + |
| 92 | +vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } })); |
| 93 | + |
| 94 | +import { AutomationsPillar } from './StudioDesignSurface'; |
| 95 | +import { createEmptyDataSource } from './__tests__/emptyDataSource'; |
| 96 | +import { getMetadataPreview, registerMetadataPreview } from '../metadata-admin/preview-registry'; |
| 97 | +import { registerMetadataInspector } from '../metadata-admin/inspector-registry'; |
| 98 | +import { FlowInspector } from '../metadata-admin/inspectors/FlowInspector'; |
| 99 | + |
| 100 | +const dataSource = createEmptyDataSource(); |
| 101 | + |
| 102 | +vi.stubGlobal( |
| 103 | + 'fetch', |
| 104 | + vi.fn(async () => new Response('null', { status: 404, headers: { 'content-type': 'application/json' } })), |
| 105 | +); |
| 106 | + |
| 107 | +/** Canvas stand-in: selects the conditional edge, or rewrites its condition. */ |
| 108 | +function StubCanvas({ |
| 109 | + onSelectionChange, |
| 110 | + onPatch, |
| 111 | +}: { |
| 112 | + onSelectionChange?: (s: { kind: 'edge'; id: string; label?: string }) => void; |
| 113 | + onPatch?: (patch: Record<string, unknown>) => void; |
| 114 | +}) { |
| 115 | + return ( |
| 116 | + <> |
| 117 | + <button type="button" onClick={() => onSelectionChange?.({ kind: 'edge', id: 'e2', label: 'Decide → End' })}> |
| 118 | + select e2 |
| 119 | + </button> |
| 120 | + <button type="button" onClick={() => onSelectionChange?.({ kind: 'edge', id: 'e1', label: 'Start → Decide' })}> |
| 121 | + select e1 |
| 122 | + </button> |
| 123 | + <button |
| 124 | + type="button" |
| 125 | + onClick={() => |
| 126 | + onPatch?.({ edges: [FLOW.edges[0], { ...FLOW.edges[1], condition: "record.name == 'x'" }] }) |
| 127 | + } |
| 128 | + > |
| 129 | + fix e2 |
| 130 | + </button> |
| 131 | + </> |
| 132 | + ); |
| 133 | +} |
| 134 | + |
| 135 | +const realFlow = getMetadataPreview('flow'); |
| 136 | +registerMetadataInspector('flow', FlowInspector); |
| 137 | + |
| 138 | +beforeEach(() => { |
| 139 | + server.draft = JSON.parse(JSON.stringify(FLOW)); |
| 140 | + server.draftReads = 0; |
| 141 | + for (const fn of Object.values(mockClient)) (fn as unknown as { mockClear: () => void }).mockClear(); |
| 142 | + registerMetadataPreview('flow', StubCanvas as never); |
| 143 | +}); |
| 144 | + |
| 145 | +afterEach(() => { |
| 146 | + cleanup(); |
| 147 | + if (realFlow) registerMetadataPreview('flow', realFlow); |
| 148 | +}); |
| 149 | + |
| 150 | +function renderPillar() { |
| 151 | + render( |
| 152 | + <MemoryRouter initialEntries={[`/studio/${PKG}/automations`]}> |
| 153 | + <AutomationsPillar packageId={PKG} /> |
| 154 | + </MemoryRouter>, |
| 155 | + ); |
| 156 | +} |
| 157 | + |
| 158 | +describe('Automations page — the edge condition shows the draft\'s served verdict at its path (objectstack#26858)', () => { |
| 159 | + it('the refused edge shows the server message; the other edge shows none', async () => { |
| 160 | + renderPillar(); |
| 161 | + fireEvent.click(await screen.findByRole('button', { name: 'select e2' }, { timeout: 8000 })); |
| 162 | + expect(await screen.findByTestId('flow-edge-server-error', {}, { timeout: 8000 })).toHaveTextContent(REFUSAL); |
| 163 | + |
| 164 | + fireEvent.click(screen.getByRole('button', { name: 'select e1' })); |
| 165 | + await waitFor(() => expect(screen.queryByTestId('flow-edge-server-error')).toBeNull()); |
| 166 | + }); |
| 167 | + |
| 168 | + it('the verdict follows the draft: after the autosave of a fixed condition the refusal goes', async () => { |
| 169 | + renderPillar(); |
| 170 | + fireEvent.click(await screen.findByRole('button', { name: 'select e2' }, { timeout: 8000 })); |
| 171 | + await screen.findByTestId('flow-edge-server-error', {}, { timeout: 8000 }); |
| 172 | + const readsBefore = server.draftReads; |
| 173 | + |
| 174 | + fireEvent.click(screen.getByRole('button', { name: 'fix e2' })); |
| 175 | + |
| 176 | + await waitFor(() => expect(mockClient.save).toHaveBeenCalled(), { timeout: 6000 }); |
| 177 | + await waitFor(() => expect(server.draftReads).toBeGreaterThan(readsBefore), { timeout: 6000 }); |
| 178 | + await waitFor(() => expect(screen.queryByTestId('flow-edge-server-error')).toBeNull(), { timeout: 6000 }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments