|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #27356 row 19 (maintainer-ruled A1) — Delete flow in the Automations |
| 5 | + * pillar's ⋯ menu, on a writable package's open flow: an in-app confirm |
| 6 | + * naming the flow (objectui#13333), the draft's discard and the flow's delete |
| 7 | + * at the package (`client.reset`), then the empty state over a re-read rail. |
| 8 | + * A refusal keeps the flow open; a read-only package draws no menu. The |
| 9 | + * harness is `AutomationsPillar.showMeFocus-26730`'s, the server doubled at |
| 10 | + * the metadata client. |
| 11 | + */ |
| 12 | + |
| 13 | +import '@testing-library/jest-dom/vitest'; |
| 14 | +import * as React from 'react'; |
| 15 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 16 | +import { render, screen, fireEvent, cleanup, waitFor, within } from '@testing-library/react'; |
| 17 | +import { toast } from 'sonner'; |
| 18 | +import { MemoryRouter } from 'react-router-dom'; |
| 19 | + |
| 20 | +const PKG = 'com.acme.app'; |
| 21 | + |
| 22 | +const FLOW = { |
| 23 | + name: 'approval', |
| 24 | + label: 'Approval', |
| 25 | + type: 'autolaunched', |
| 26 | + status: 'active', |
| 27 | + nodes: [{ id: 'start', type: 'start', label: 'Start' }, { id: 'end', type: 'end', label: 'End' }], |
| 28 | + edges: [{ id: 'e1', source: 'start', target: 'end' }], |
| 29 | +}; |
| 30 | + |
| 31 | +const server = vi.hoisted(() => ({ draft: null as Record<string, unknown> | null, deleted: false, refuse: null as Error | null })); |
| 32 | + |
| 33 | +const mockClient = vi.hoisted(() => ({ |
| 34 | + // A second flow stays on the rail: the pillar must not open it after the delete. |
| 35 | + list: vi.fn(async () => [...(server.deleted ? [] : [{ name: 'approval', label: 'Approval' }]), { name: 'escalate', label: 'Escalate' }]), |
| 36 | + listDrafts: vi.fn(async () => []), |
| 37 | + listTypes: vi.fn(async () => ({ entries: [] })), |
| 38 | + get: vi.fn(async () => null), |
| 39 | + references: vi.fn(async () => []), |
| 40 | + layered: vi.fn(async (_t: string, name: string) => ({ code: null, overlay: null, effective: { ...FLOW, name, label: name === 'approval' ? 'Approval' : 'Escalate' }, lock: 'none' })), |
| 41 | + getDraft: vi.fn(async (type: string, name: string) => { |
| 42 | + if (server.draft) return { type, name, item: JSON.parse(JSON.stringify(server.draft)) }; |
| 43 | + throw Object.assign(new Error(`No pending draft exists for ${type}/${name}.`), { code: 'NO_DRAFT', status: 404 }); |
| 44 | + }), |
| 45 | + save: vi.fn(async (type: string, name: string, item: unknown) => ({ type, name, item })), |
| 46 | + reset: vi.fn(async (_type: string, _name: string, options: { state?: string } = {}) => { |
| 47 | + if (options.state === 'draft') server.draft = null; |
| 48 | + else if (server.refuse) throw server.refuse; |
| 49 | + else server.deleted = true; |
| 50 | + return {}; |
| 51 | + }), |
| 52 | +})); |
| 53 | + |
| 54 | +vi.mock('../metadata-admin/useMetadata', async (importOriginal) => { |
| 55 | + const mod = await importOriginal<typeof import('../metadata-admin/useMetadata')>(); |
| 56 | + return { ...mod, useMetadataClient: () => mockClient, useMetadataTypes: () => ({ entries: [] }) }; |
| 57 | +}); |
| 58 | + |
| 59 | +vi.mock('./packages-io', async (importOriginal) => { |
| 60 | + const mod = await importOriginal<typeof import('./packages-io')>(); |
| 61 | + return { ...mod, fetchPackages: vi.fn(async () => []) }; |
| 62 | +}); |
| 63 | + |
| 64 | +vi.mock('@object-ui/react', async (importOriginal) => { |
| 65 | + const mod = await importOriginal<typeof import('@object-ui/react')>(); |
| 66 | + return { ...mod, useAdapter: () => dataSource }; |
| 67 | +}); |
| 68 | + |
| 69 | +vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } })); |
| 70 | + |
| 71 | +import { AutomationsPillar } from './StudioDesignSurface'; |
| 72 | +import { createEmptyDataSource } from './__tests__/emptyDataSource'; |
| 73 | + |
| 74 | +const dataSource = createEmptyDataSource(); |
| 75 | + |
| 76 | +// The pillar's `/automation/_status` probe: "absent", its documented fallback. |
| 77 | +vi.stubGlobal('fetch', vi.fn(async () => new Response('null', { status: 404, headers: { 'content-type': 'application/json' } }))); |
| 78 | + |
| 79 | +beforeEach(() => { |
| 80 | + server.draft = null; |
| 81 | + server.deleted = false; |
| 82 | + server.refuse = null; |
| 83 | + for (const fn of Object.values(mockClient)) (fn as unknown as { mockClear: () => void }).mockClear(); |
| 84 | + vi.mocked(toast.success).mockClear(); |
| 85 | +}); |
| 86 | + |
| 87 | +afterEach(cleanup); |
| 88 | + |
| 89 | +function mount(readOnly = false) { |
| 90 | + render( |
| 91 | + <MemoryRouter initialEntries={[`/studio/${PKG}/automations`]}> |
| 92 | + <AutomationsPillar packageId={PKG} readOnly={readOnly} /> |
| 93 | + </MemoryRouter>, |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +/** Open the ⋯ menu and pick Delete flow: the confirm dialog. */ |
| 98 | +async function openConfirm(): Promise<HTMLElement> { |
| 99 | + fireEvent.pointerDown(await screen.findByTestId('auto-item-more', undefined, { timeout: 8000 }), { button: 0, ctrlKey: false }); |
| 100 | + fireEvent.click(await screen.findByTestId('auto-item-delete')); |
| 101 | + return screen.findByTestId('auto-delete-confirm-dialog'); |
| 102 | +} |
| 103 | +const deleteFromMenu = async () => fireEvent.click(within(await openConfirm()).getByTestId('auto-delete-confirm-action')); |
| 104 | + |
| 105 | +describe('Automations — Delete flow (#27356 row 19)', () => { |
| 106 | + it('asks naming the flow, discards its draft, deletes it at the package, and leaves the pillar on its empty state', async () => { |
| 107 | + server.draft = { ...FLOW, label: 'Approval' }; |
| 108 | + mount(); |
| 109 | + expect(await screen.findByTestId('auto-canvas-caption', undefined, { timeout: 8000 })).toHaveTextContent('Approval'); |
| 110 | + const listReads = mockClient.list.mock.calls.length; |
| 111 | + |
| 112 | + const dialog = await openConfirm(); |
| 113 | + expect(dialog).toHaveTextContent('"Approval"'); |
| 114 | + expect(dialog).toHaveTextContent('flow/approval'); |
| 115 | + expect(mockClient.reset).not.toHaveBeenCalled(); |
| 116 | + fireEvent.click(within(dialog).getByTestId('auto-delete-confirm-action')); |
| 117 | + |
| 118 | + await waitFor(() => expect(mockClient.reset).toHaveBeenCalledTimes(2)); |
| 119 | + expect(mockClient.reset.mock.calls).toEqual([ |
| 120 | + ['flow', 'approval', { state: 'draft', packageId: PKG }], |
| 121 | + ['flow', 'approval', { packageId: PKG }], |
| 122 | + ]); |
| 123 | + await waitFor(() => expect(mockClient.list.mock.calls.length).toBeGreaterThan(listReads)); |
| 124 | + await waitFor(() => expect(screen.queryByText('Approval')).toBeNull()); |
| 125 | + expect(screen.getByText('Escalate')).toBeInTheDocument(); |
| 126 | + expect(mockClient.layered.mock.calls.map(([, name]) => name)).not.toContain('escalate'); |
| 127 | + expect(screen.queryByTestId('auto-canvas-caption')).toBeNull(); |
| 128 | + expect(screen.queryByTestId('auto-item-more')).toBeNull(); |
| 129 | + expect(toast.success).toHaveBeenCalledWith('Deleted flow "Approval"'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('a flow with no pending draft is deleted with the one call', async () => { |
| 133 | + mount(); |
| 134 | + await deleteFromMenu(); |
| 135 | + await waitFor(() => expect(screen.queryByTestId('auto-canvas-caption')).toBeNull()); |
| 136 | + expect(mockClient.reset.mock.calls).toEqual([['flow', 'approval', { packageId: PKG }]]); |
| 137 | + }); |
| 138 | + |
| 139 | + it('a refused delete shows the door\'s sentence and keeps the flow open', async () => { |
| 140 | + server.refuse = Object.assign(new Error('flow/approval is referenced by 2 triggers.'), { status: 409, code: 'RESOURCE_CONFLICT' }); |
| 141 | + mount(); |
| 142 | + await deleteFromMenu(); |
| 143 | + expect(await screen.findByTestId('studio-refusal')).toHaveTextContent('flow/approval is referenced by 2 triggers.'); |
| 144 | + expect(screen.getByTestId('auto-canvas-caption')).toHaveTextContent('Approval'); |
| 145 | + expect(toast.success).not.toHaveBeenCalled(); |
| 146 | + }); |
| 147 | + |
| 148 | + it('a read-only package draws no ⋯ menu', async () => { |
| 149 | + mount(true); |
| 150 | + expect(await screen.findByTestId('auto-canvas-caption', undefined, { timeout: 8000 })).toHaveTextContent('Approval'); |
| 151 | + expect(screen.queryByTestId('auto-item-more')).toBeNull(); |
| 152 | + }); |
| 153 | +}); |
0 commit comments