|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#35650 — the action's full-page editor draws a script action's |
| 5 | + * refusal catalogue (`refusals`) and declared texts (`texts`) with their |
| 6 | + * served rows: the key-only record control and the row's help text, not the |
| 7 | + * raw JSON editor the bare schema gives a record. |
| 8 | + * |
| 9 | + * The REAL page with the production registrations: `action` registers a |
| 10 | + * preview and a default inspector, so the page takes its designer branch and |
| 11 | + * the no-selection panel is `ActionDefaultInspector`. The type row is built |
| 12 | + * from the spec the way the server builds it: `actionForm` as the form, and |
| 13 | + * `ActionSchema` converted with `z.toJSONSchema(…, { io: 'input' })` as the |
| 14 | + * schema. The only stand-ins are the metadata client and the network. |
| 15 | + */ |
| 16 | + |
| 17 | +import '@testing-library/jest-dom/vitest'; |
| 18 | +import * as React from 'react'; |
| 19 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 20 | +import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'; |
| 21 | +import { MemoryRouter } from 'react-router-dom'; |
| 22 | +import { z } from 'zod'; |
| 23 | +import { ActionSchema, actionForm } from '@objectstack/spec/ui'; |
| 24 | + |
| 25 | +// Module-scope imports of the engines the page reaches through dynamic |
| 26 | +// `import()` (the CEL authoring and the save-time lint): a cold first load |
| 27 | +// stays out of every `waitFor` (`check:test-source-alias`). |
| 28 | +import '@objectstack/formula'; |
| 29 | +import '@objectstack/lint'; |
| 30 | + |
| 31 | +/** The `action` row's `schema`, converted from the spec the way the server does. */ |
| 32 | +const ACTION_JSON_SCHEMA = z.toJSONSchema(ActionSchema as unknown as z.ZodType, { |
| 33 | + io: 'input', |
| 34 | + unrepresentable: 'any', |
| 35 | +}) as Record<string, unknown>; |
| 36 | + |
| 37 | +/** The served row of `key`, read off the form the server serves. */ |
| 38 | +function servedHelpText(key: string): string { |
| 39 | + for (const section of actionForm.sections ?? []) { |
| 40 | + for (const row of section.fields ?? []) { |
| 41 | + if (typeof row !== 'string' && row.field === key) return String(row.helpText); |
| 42 | + } |
| 43 | + } |
| 44 | + throw new Error(`actionForm declares no ${key} row`); |
| 45 | +} |
| 46 | + |
| 47 | +const STORED = { |
| 48 | + name: 'sign_contract', |
| 49 | + label: 'Sign contract', |
| 50 | + objectName: 'contract', |
| 51 | + type: 'script', |
| 52 | + locations: ['record_header'], |
| 53 | + body: { language: 'js', source: "throw { code: 'SIGN_IN_REQUIRED' };" }, |
| 54 | + refusals: { SIGN_IN_REQUIRED: { message: 'Sign in first.', status: 401 } }, |
| 55 | + texts: { email_subject: { message: 'Contract signed' } }, |
| 56 | +}; |
| 57 | + |
| 58 | +let stored: Record<string, unknown> = STORED; |
| 59 | + |
| 60 | +const mockClient = { |
| 61 | + list: vi.fn(async () => []), |
| 62 | + listDrafts: vi.fn(async () => []), |
| 63 | + layered: vi.fn(async () => ({ effective: stored, code: null, editable: true, deletable: true })), |
| 64 | + getDraft: vi.fn(async () => null), |
| 65 | + get: vi.fn(async () => stored), |
| 66 | + save: vi.fn(async (_type: string, _name: string, item: unknown) => { |
| 67 | + stored = item as Record<string, unknown>; |
| 68 | + return {}; |
| 69 | + }), |
| 70 | + saveDraft: vi.fn(async () => ({})), |
| 71 | + publish: vi.fn(async () => ({ success: true })), |
| 72 | + references: vi.fn(async () => []), |
| 73 | + history: vi.fn(async () => []), |
| 74 | + // The object-scoped field pickers read the bound object through the draft-preview client. |
| 75 | + withPreviewDrafts: vi.fn(), |
| 76 | +}; |
| 77 | +mockClient.withPreviewDrafts.mockImplementation(() => mockClient); |
| 78 | + |
| 79 | +vi.mock('./useMetadata', async (importOriginal) => ({ |
| 80 | + ...(await importOriginal<typeof import('./useMetadata')>()), |
| 81 | + useMetadataClient: () => mockClient, |
| 82 | + useMetadataTypes: () => ({ |
| 83 | + loading: false, |
| 84 | + error: null, |
| 85 | + entries: [ |
| 86 | + { |
| 87 | + type: 'action', |
| 88 | + name: 'action', |
| 89 | + label: 'Action', |
| 90 | + domain: 'ui', |
| 91 | + allowEnvironmentOverlay: true, |
| 92 | + allowRuntimeCreate: true, |
| 93 | + schema: ACTION_JSON_SCHEMA, |
| 94 | + form: actionForm, |
| 95 | + }, |
| 96 | + ], |
| 97 | + }), |
| 98 | +})); |
| 99 | +vi.mock('@monaco-editor/react', () => { |
| 100 | + const Editor = () => null; |
| 101 | + return { Editor, default: Editor, loader: { init: () => Promise.reject(new Error('offline')) } }; |
| 102 | +}); |
| 103 | +vi.mock('@object-ui/plugin-editor', () => ({ configureBundledMonaco: () => Promise.resolve() })); |
| 104 | +vi.stubGlobal( |
| 105 | + 'fetch', |
| 106 | + vi.fn(async () => new Response('null', { status: 404, headers: { 'content-type': 'application/json' } })), |
| 107 | +); |
| 108 | + |
| 109 | +import { MetadataResourceEditPage } from './ResourceEditPage'; |
| 110 | +import { t } from './i18n'; |
| 111 | +// The load-time registrations, exactly as the package entry runs them, so the |
| 112 | +// branch this file takes is the branch production takes. |
| 113 | +import './register-builtins'; |
| 114 | +import { registerBuiltinDesigners } from './register-builtin-designers'; |
| 115 | +registerBuiltinDesigners(); |
| 116 | + |
| 117 | +beforeEach(() => { |
| 118 | + stored = STORED; |
| 119 | + for (const fn of Object.values(mockClient)) (fn as unknown as { mockClear: () => void }).mockClear(); |
| 120 | + mockClient.withPreviewDrafts.mockImplementation(() => mockClient); |
| 121 | + window.localStorage.setItem('metadata-admin:autosave', '0'); |
| 122 | +}); |
| 123 | +afterEach(() => { |
| 124 | + cleanup(); |
| 125 | + window.localStorage.removeItem('metadata-admin:autosave'); |
| 126 | +}); |
| 127 | + |
| 128 | +async function openAction(): Promise<void> { |
| 129 | + render( |
| 130 | + <MemoryRouter initialEntries={['/metadata/action/sign_contract']}> |
| 131 | + <MetadataResourceEditPage type="action" name="sign_contract" /> |
| 132 | + </MemoryRouter>, |
| 133 | + ); |
| 134 | + // The designer branch opens editable: its panel is the inspector itself. |
| 135 | + await screen.findByText(t('engine.inspector.moreFields', 'en'), {}, { timeout: 8000 }); |
| 136 | + |
| 137 | +} |
| 138 | + |
| 139 | +describe('the action full-page editor draws refusals and texts from their served rows (objectstack#35650)', () => { |
| 140 | + it('draws each as the key-only record control with the served help text, and no raw JSON', async () => { |
| 141 | + await openAction(); |
| 142 | + // No editor on the panel holds either record as JSON text (the raw JSON face). |
| 143 | + const jsonHolders = Array.from(document.querySelectorAll('textarea')).filter( |
| 144 | + (el) => el.value.includes('"SIGN_IN_REQUIRED"') || el.value.includes('"email_subject"'), |
| 145 | + ); |
| 146 | + expect(jsonHolders).toEqual([]); |
| 147 | + // The served rows' help text, and the key-only control drawing each entry under its key. |
| 148 | + expect(screen.getByText(servedHelpText('refusals'))).toBeInTheDocument(); |
| 149 | + expect(screen.getByText(servedHelpText('texts'))).toBeInTheDocument(); |
| 150 | + const rows = screen.getByTestId('action-served-rows'); |
| 151 | + expect(within(rows).getByText('SIGN_IN_REQUIRED', { selector: 'span' })).toBeInTheDocument(); |
| 152 | + expect(within(rows).getByText('email_subject', { selector: 'span' })).toBeInTheDocument(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('an entry edited through the served row saves as a spec-valid action', async () => { |
| 156 | + await openAction(); |
| 157 | + const rows = screen.getByTestId('action-served-rows'); |
| 158 | + fireEvent.click(within(rows).getByText('email_subject', { selector: 'span' })); |
| 159 | + const entry = within(rows).getByText('email_subject', { selector: 'span' }).closest('.rounded-md') as HTMLElement; |
| 160 | + fireEvent.change(within(entry).getByDisplayValue('Contract signed'), { target: { value: 'Your contract is signed' } }); |
| 161 | + await waitFor(() => expect(screen.getByTestId('resource-save')).toBeEnabled(), { timeout: 4000 }); |
| 162 | + fireEvent.click(screen.getByTestId('resource-save')); |
| 163 | + await waitFor(() => expect(mockClient.save).toHaveBeenCalledTimes(1), { timeout: 6000 }); |
| 164 | + const body = JSON.parse(JSON.stringify(mockClient.save.mock.calls[0]![2])) as Record<string, unknown>; |
| 165 | + expect(body.texts).toEqual({ email_subject: { message: 'Your contract is signed' } }); |
| 166 | + expect(body.refusals).toEqual(STORED.refusals); |
| 167 | + const parsed = ActionSchema.safeParse(body); |
| 168 | + expect(parsed.success, JSON.stringify(parsed.error?.issues)).toBe(true); |
| 169 | + }); |
| 170 | +}); |
0 commit comments