|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#26734 — a script action authored in Studio is stored with |
| 5 | + * `type: 'script'`, and the Type control shows only a `type` the draft holds. |
| 6 | + * |
| 7 | + * Measured before the fix: New → "Run a script" → a body left the draft as |
| 8 | + * `{ name, body }` with no `type`, while the Type control read Script. The |
| 9 | + * console reads the stored row, finds no `type`, and runs nothing ("is not |
| 10 | + * executable: no handler is registered under that name …"); the same action |
| 11 | + * with `type: 'script'` added through the API ran. |
| 12 | + * |
| 13 | + * The pins drive the real inspector in a host that merges each patch |
| 14 | + * shallowly, and parse the sent draft with the spec's `ActionSchema`. |
| 15 | + */ |
| 16 | + |
| 17 | +import '@testing-library/jest-dom/vitest'; |
| 18 | +import * as React from 'react'; |
| 19 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 20 | +import { render, screen, cleanup, fireEvent, waitFor } from '@testing-library/react'; |
| 21 | +import { ActionSchema } from '@objectstack/spec/ui'; |
| 22 | + |
| 23 | +const state = vi.hoisted(() => ({ |
| 24 | + metadataClient: { get: vi.fn(async () => undefined), list: vi.fn(async () => [] as unknown[]), withPreviewDrafts() { return this; } }, |
| 25 | +})); |
| 26 | +vi.mock('../useMetadata', () => ({ useMetadataClient: () => state.metadataClient })); |
| 27 | + |
| 28 | +import { ActionDefaultInspector } from './ActionDefaultInspector'; |
| 29 | + |
| 30 | +afterEach(cleanup); |
| 31 | + |
| 32 | +type Draft = Record<string, unknown>; |
| 33 | + |
| 34 | +function mountHost(initial: Draft) { |
| 35 | + const seen: { draft: Draft; writes: Draft[] } = { draft: initial, writes: [] }; |
| 36 | + function Host() { |
| 37 | + const [draft, setDraft] = React.useState<Draft>(initial); |
| 38 | + seen.draft = draft; |
| 39 | + return ( |
| 40 | + <ActionDefaultInspector |
| 41 | + type="action" |
| 42 | + name={String(initial.name)} |
| 43 | + locale={'en-US' as never} |
| 44 | + readOnly={false} |
| 45 | + draft={draft} |
| 46 | + onPatch={(patch) => { |
| 47 | + seen.writes.push(patch); |
| 48 | + setDraft((d) => ({ ...d, ...patch })); |
| 49 | + }} |
| 50 | + /> |
| 51 | + ); |
| 52 | + } |
| 53 | + render(<Host />); |
| 54 | + return seen; |
| 55 | +} |
| 56 | + |
| 57 | +const sent = (draft: Draft): Draft => JSON.parse(JSON.stringify(draft)); |
| 58 | + |
| 59 | +function refusals(draft: Draft): string[] { |
| 60 | + const parsed = ActionSchema.safeParse(sent(draft)); |
| 61 | + return parsed.success ? [] : parsed.error.issues.map((i) => `${i.path.join('.')}:${i.code}`); |
| 62 | +} |
| 63 | + |
| 64 | +async function pick(label: string, option: string) { |
| 65 | + fireEvent.keyDown(screen.getByRole('combobox', { name: label }), { key: 'ArrowDown' }); |
| 66 | + await waitFor(() => expect(screen.queryAllByRole('option').length).toBeGreaterThan(0)); |
| 67 | + fireEvent.click(screen.getByRole('option', { name: option })); |
| 68 | +} |
| 69 | + |
| 70 | +const typeControl = () => screen.getByRole('combobox', { name: 'Type' }); |
| 71 | +const scriptBody = () => screen.getByText('Script body', { selector: 'label' }).parentElement!.querySelector('textarea')!; |
| 72 | + |
| 73 | +const SCRIPT = 'Script — run an expression / sandboxed JS'; |
| 74 | +const RUN_A_SCRIPT = 'Run a script — sandboxed JS / expression body'; |
| 75 | + |
| 76 | +/** The skeleton Studio's New starts from (objectui#11820): `type` left to the spec. */ |
| 77 | +const NEW_UPDATE_ACTION: Draft = { |
| 78 | + name: 'invoice_action_2', |
| 79 | + label: 'New action', |
| 80 | + objectName: 'invoice', |
| 81 | + locations: ['record_header'], |
| 82 | + operation: 'update', |
| 83 | + patch: {}, |
| 84 | +}; |
| 85 | + |
| 86 | +describe('scriptTypePin — a script authored here is stored with its type (objectstack#26734)', () => { |
| 87 | + it('New → "Run a script" → a body: the draft carries `type: \'script\'` and parses', async () => { |
| 88 | + const seen = mountHost(NEW_UPDATE_ACTION); |
| 89 | + // The draft holds no `type`, so the Type control shows none. |
| 90 | + expect(typeControl()).not.toHaveTextContent(SCRIPT); |
| 91 | + |
| 92 | + await pick('What it does', RUN_A_SCRIPT); |
| 93 | + expect(sent(seen.draft).type).toBe('script'); |
| 94 | + expect(typeControl()).toHaveTextContent(SCRIPT); |
| 95 | + |
| 96 | + fireEvent.change(scriptBody(), { target: { value: 'true' } }); |
| 97 | + const stored = sent(seen.draft); |
| 98 | + expect(stored).toMatchObject({ type: 'script', body: { language: 'expression', source: 'true' } }); |
| 99 | + expect(stored).not.toHaveProperty('operation'); |
| 100 | + expect(stored).not.toHaveProperty('patch'); |
| 101 | + expect(refusals(seen.draft)).toEqual([]); |
| 102 | + }); |
| 103 | + |
| 104 | + it('a stored script with no `type` shows none in Type, and its first body edit stores it', () => { |
| 105 | + const seen = mountHost({ |
| 106 | + name: 'run_script', |
| 107 | + label: 'Run', |
| 108 | + objectName: 'invoice', |
| 109 | + locations: ['record_more'], |
| 110 | + body: { language: 'expression', source: 'true' }, |
| 111 | + }); |
| 112 | + expect(typeControl()).not.toHaveTextContent(SCRIPT); |
| 113 | + // The editor for what it will run stays on screen. |
| 114 | + expect(scriptBody()).toHaveValue('true'); |
| 115 | + |
| 116 | + fireEvent.change(scriptBody(), { target: { value: 'false' } }); |
| 117 | + expect(seen.writes).toStrictEqual([{ type: 'script', body: { language: 'expression', source: 'false' } }]); |
| 118 | + expect(typeControl()).toHaveTextContent(SCRIPT); |
| 119 | + expect(refusals(seen.draft)).toEqual([]); |
| 120 | + }); |
| 121 | + |
| 122 | + it('the first field value of an update action from New stores `type` too', () => { |
| 123 | + const seen = mountHost(NEW_UPDATE_ACTION); |
| 124 | + fireEvent.click(screen.getByRole('button', { name: 'Add field value' })); |
| 125 | + const field = screen.getByText('Field', { selector: 'label' }).parentElement!.querySelector('input')!; |
| 126 | + fireEvent.change(field, { target: { value: 'status' } }); |
| 127 | + expect(seen.writes).toStrictEqual([{ type: 'script', patch: { status: '' } }]); |
| 128 | + expect(refusals(seen.draft)).toEqual([]); |
| 129 | + }); |
| 130 | + |
| 131 | + it('why the panels still show the script editor: the spec gives a draft with no `type` the script route', () => { |
| 132 | + const parsed = ActionSchema.safeParse({ name: 'run_script', label: 'Run', body: { language: 'expression', source: 'true' } }); |
| 133 | + expect(parsed.success && parsed.data.type).toBe('script'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('control: an action that holds a `type` is written without one', async () => { |
| 137 | + const seen = mountHost({ |
| 138 | + name: 'invoice_sync', |
| 139 | + label: 'Sync', |
| 140 | + objectName: 'invoice', |
| 141 | + locations: ['record_more'], |
| 142 | + type: 'script', |
| 143 | + body: { language: 'expression', source: 'true' }, |
| 144 | + }); |
| 145 | + expect(typeControl()).toHaveTextContent(SCRIPT); |
| 146 | + fireEvent.change(scriptBody(), { target: { value: 'false' } }); |
| 147 | + await pick('Script language', 'Sandboxed JS (L2)'); |
| 148 | + for (const write of seen.writes) expect(write).not.toHaveProperty('type'); |
| 149 | + expect(refusals(seen.draft)).toEqual([]); |
| 150 | + }); |
| 151 | + |
| 152 | + it('control: an api action keeps its type, and Type shows it', () => { |
| 153 | + mountHost({ name: 'invoice_api', label: 'Call', type: 'api', target: '/api/v1/sync' }); |
| 154 | + expect(typeControl()).toHaveTextContent('API — call an endpoint'); |
| 155 | + }); |
| 156 | +}); |
0 commit comments