|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#26730 — the Automations strip's "Show me" ends on the input. |
| 5 | + * |
| 6 | + * Measured before this card: "Show me" selected the step the refusal names |
| 7 | + * and stopped there, so the focus stayed on the button and the author still |
| 8 | + * had to find the input. It now ends on the input the sentence names: the |
| 9 | + * step's Title for `nodes.2.config.title`, and the step's first input when |
| 10 | + * the sentence names the step alone. |
| 11 | + * |
| 12 | + * The harness is `AutomationsPillar.authorRefusal-11785.test.tsx`'s: the real |
| 13 | + * registered `FlowPreview` and `FlowInspector`, and a server double whose |
| 14 | + * `save` refuses, driven through the header switch. |
| 15 | + */ |
| 16 | + |
| 17 | +import '@testing-library/jest-dom/vitest'; |
| 18 | +import * as React from 'react'; |
| 19 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 20 | +import { render, screen, fireEvent, cleanup, waitFor, within } from '@testing-library/react'; |
| 21 | +import { MemoryRouter } from 'react-router-dom'; |
| 22 | + |
| 23 | +const PKG = 'com.acme.app'; |
| 24 | + |
| 25 | +const FLOW = { |
| 26 | + name: 'approval', |
| 27 | + label: 'Approval', |
| 28 | + type: 'autolaunched', |
| 29 | + status: 'active', |
| 30 | + nodes: [ |
| 31 | + { id: 'start', type: 'start', label: 'Start' }, |
| 32 | + { id: 'route', type: 'decision', label: 'Route' }, |
| 33 | + { id: 'tell', type: 'notify', label: 'Notify approver', config: { recipients: ['owner'] } }, |
| 34 | + { id: 'end', type: 'end', label: 'End' }, |
| 35 | + ], |
| 36 | + edges: [ |
| 37 | + { id: 'e1', source: 'start', target: 'route' }, |
| 38 | + { id: 'e2', source: 'route', target: 'tell' }, |
| 39 | + { id: 'e3', source: 'tell', target: 'end' }, |
| 40 | + ], |
| 41 | +}; |
| 42 | + |
| 43 | +const MISSING = 'Invalid input: expected string, received undefined'; |
| 44 | + |
| 45 | +const server = vi.hoisted(() => ({ |
| 46 | + /** The issues the next draft save is refused with. */ |
| 47 | + issues: [] as Array<{ path: string; message: string }>, |
| 48 | +})); |
| 49 | + |
| 50 | +const mockClient = vi.hoisted(() => ({ |
| 51 | + list: vi.fn(async () => [{ name: 'approval', label: 'Approval' }]), |
| 52 | + listDrafts: vi.fn(async () => []), |
| 53 | + listTypes: vi.fn(async () => ({ entries: [] })), |
| 54 | + get: vi.fn(async () => null), |
| 55 | + references: vi.fn(async () => []), |
| 56 | + layered: vi.fn(async () => ({ |
| 57 | + code: null, |
| 58 | + overlay: null, |
| 59 | + overlayScope: null, |
| 60 | + effective: JSON.parse(JSON.stringify(FLOW)), |
| 61 | + editable: true, |
| 62 | + deletable: true, |
| 63 | + resettable: false, |
| 64 | + lock: 'none', |
| 65 | + })), |
| 66 | + getDraft: vi.fn(async (type: string, name: string) => { |
| 67 | + throw Object.assign(new Error(`No pending draft exists for ${type}/${name}.`), { code: 'NO_DRAFT', status: 404 }); |
| 68 | + }), |
| 69 | + save: vi.fn(async () => { |
| 70 | + // The client's parsed `MetadataError`: a headline, a status, a code and |
| 71 | + // the structured issues the server returned. |
| 72 | + throw Object.assign(new Error(`flow/approval failed spec validation (${server.issues.length})`), { |
| 73 | + status: 422, |
| 74 | + code: 'INVALID_METADATA', |
| 75 | + issues: server.issues, |
| 76 | + }); |
| 77 | + }), |
| 78 | + publish: vi.fn(async () => ({ success: true })), |
| 79 | + reset: vi.fn(async () => ({})), |
| 80 | +})); |
| 81 | + |
| 82 | +vi.mock('../metadata-admin/useMetadata', async (importOriginal) => { |
| 83 | + const mod = await importOriginal<typeof import('../metadata-admin/useMetadata')>(); |
| 84 | + return { ...mod, useMetadataClient: () => mockClient, useMetadataTypes: () => ({ entries: [] }) }; |
| 85 | +}); |
| 86 | + |
| 87 | +vi.mock('./packages-io', async (importOriginal) => { |
| 88 | + const mod = await importOriginal<typeof import('./packages-io')>(); |
| 89 | + return { ...mod, fetchPackages: vi.fn(async () => []) }; |
| 90 | +}); |
| 91 | + |
| 92 | +vi.mock('@object-ui/react', async (importOriginal) => { |
| 93 | + const mod = await importOriginal<typeof import('@object-ui/react')>(); |
| 94 | + return { ...mod, useAdapter: () => dataSource }; |
| 95 | +}); |
| 96 | + |
| 97 | +vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } })); |
| 98 | + |
| 99 | +import { AutomationsPillar } from './StudioDesignSurface'; |
| 100 | +import { createEmptyDataSource } from './__tests__/emptyDataSource'; |
| 101 | +import { registerMetadataPreview } from '../metadata-admin/preview-registry'; |
| 102 | +import { registerMetadataInspector } from '../metadata-admin/inspector-registry'; |
| 103 | +import { FlowPreview } from '../metadata-admin/previews/FlowPreview'; |
| 104 | +import { FlowInspector } from '../metadata-admin/inspectors/FlowInspector'; |
| 105 | +import { t } from '../metadata-admin/i18n'; |
| 106 | + |
| 107 | +const dataSource = createEmptyDataSource(); |
| 108 | + |
| 109 | +// The pillar's `/automation/_status` probe and the inspector's catalog reads go |
| 110 | +// through the global `fetch`; "absent" keeps each on its documented fallback. |
| 111 | +vi.stubGlobal( |
| 112 | + 'fetch', |
| 113 | + vi.fn(async () => new Response('null', { status: 404, headers: { 'content-type': 'application/json' } })), |
| 114 | +); |
| 115 | + |
| 116 | +registerMetadataPreview('flow', FlowPreview); |
| 117 | +registerMetadataInspector('flow', FlowInspector); |
| 118 | + |
| 119 | +beforeEach(() => { |
| 120 | + server.issues = []; |
| 121 | + for (const fn of Object.values(mockClient)) (fn as unknown as { mockClear: () => void }).mockClear(); |
| 122 | +}); |
| 123 | + |
| 124 | +afterEach(cleanup); |
| 125 | + |
| 126 | +/** Open the flow, then flip the header switch: a save that the server refuses. */ |
| 127 | +async function refusedSave(issues: Array<{ path: string; message: string }>): Promise<HTMLElement> { |
| 128 | + server.issues = issues; |
| 129 | + render( |
| 130 | + <MemoryRouter initialEntries={[`/studio/${PKG}/automations`]}> |
| 131 | + <AutomationsPillar packageId={PKG} /> |
| 132 | + </MemoryRouter>, |
| 133 | + ); |
| 134 | + const toggle = await screen.findByRole('switch', undefined, { timeout: 8000 }); |
| 135 | + await waitFor(() => expect(toggle).not.toBeDisabled()); |
| 136 | + fireEvent.click(toggle); |
| 137 | + await waitFor(() => expect(mockClient.save).toHaveBeenCalledTimes(1)); |
| 138 | + return screen.findByTestId('studio-refusal'); |
| 139 | +} |
| 140 | + |
| 141 | +describe('Automations page — "Show me" puts the focus on the input the refusal names (objectstack#26730)', () => { |
| 142 | + it('`nodes.2.config.title`: the focus lands on the step\'s Title', async () => { |
| 143 | + const strip = await refusedSave([{ path: 'nodes.2.config.title', message: MISSING }]); |
| 144 | + const show = within(strip).getByRole('button', { name: t('engine.studio.refusal.show', 'en') }); |
| 145 | + show.focus(); |
| 146 | + fireEvent.click(show); |
| 147 | + |
| 148 | + const rail = screen.getByTestId('auto-config-aside'); |
| 149 | + // The Title box, by the placeholder the notify table gives it (its label |
| 150 | + // heads the row rather than naming the box). |
| 151 | + const title = await within(rail).findByPlaceholderText('Your request was approved', undefined, { timeout: 8000 }); |
| 152 | + await waitFor(() => expect(document.activeElement).toBe(title)); |
| 153 | + expect(within(rail).getByLabelText('ID')).toHaveValue('tell'); |
| 154 | + }); |
| 155 | + |
| 156 | + it('a sentence naming the step alone: the focus lands in that step\'s inspector', async () => { |
| 157 | + const strip = await refusedSave([{ path: 'nodes.2', message: MISSING }]); |
| 158 | + fireEvent.click(within(strip).getByRole('button', { name: t('engine.studio.refusal.show', 'en') })); |
| 159 | + |
| 160 | + const rail = screen.getByTestId('auto-config-aside'); |
| 161 | + await within(rail).findByLabelText('ID', undefined, { timeout: 8000 }); |
| 162 | + await waitFor(() => expect(rail.contains(document.activeElement)).toBe(true)); |
| 163 | + }); |
| 164 | +}); |
0 commit comments