|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#26728 — "Expose to AI agents" writes the spec's nested `ai` |
| 5 | + * block, and unticking leaves a draft the spec accepts. |
| 6 | + * |
| 7 | + * The control used to write a flat `aiExposed` (and its description a flat |
| 8 | + * `aiDescription`). The spec's `ActionSchema` refuses both by name, so one tick |
| 9 | + * refused every later save of the object, and unticking wrote `aiExposed: |
| 10 | + * false`, still refused. The pins drive the real inspector inside a host that |
| 11 | + * merges each patch shallowly (as `ObjectActionsPanel` applies `onPatch`), send |
| 12 | + * the held draft through a JSON round trip (the body the save sends), and parse |
| 13 | + * it with the spec's own `ActionSchema`: |
| 14 | + * |
| 15 | + * - a tick writes `ai.exposed`, and the spec's only refusal is the |
| 16 | + * description it requires beside it — the input the inspector opens; |
| 17 | + * - a description of the spec's minimum length parses, and reads back; |
| 18 | + * - unticking parses, with or without a description typed; |
| 19 | + * - an older draft carrying the flat keys parses after the control's next |
| 20 | + * write, which clears them; |
| 21 | + * - the other `ai` keys (edited under "More fields") are kept. |
| 22 | + * |
| 23 | + * Parity: `AI_DESCRIPTION_MIN` is the spec's own minimum, measured both sides. |
| 24 | + */ |
| 25 | + |
| 26 | +import '@testing-library/jest-dom/vitest'; |
| 27 | +import * as React from 'react'; |
| 28 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 29 | +import { render, screen, cleanup, fireEvent } from '@testing-library/react'; |
| 30 | +import { ActionSchema } from '@objectstack/spec/ui'; |
| 31 | + |
| 32 | +// objectui#4697: the inspector reads the shared client on mount; stub it so |
| 33 | +// nothing reaches the network, as the sibling inspector suites do. |
| 34 | +const state = vi.hoisted(() => ({ |
| 35 | + metadataClient: { get: vi.fn(async () => undefined), list: vi.fn(async () => [] as unknown[]), withPreviewDrafts() { return this; } }, |
| 36 | +})); |
| 37 | +vi.mock('../useMetadata', () => ({ useMetadataClient: () => state.metadataClient })); |
| 38 | + |
| 39 | +import { ActionDefaultInspector, AI_DESCRIPTION_MIN } from './ActionDefaultInspector'; |
| 40 | + |
| 41 | +afterEach(cleanup); |
| 42 | + |
| 43 | +type Draft = Record<string, unknown>; |
| 44 | + |
| 45 | +const SCRIPT_ACTION: Draft = { |
| 46 | + name: 'invoice_send', |
| 47 | + label: 'Send', |
| 48 | + objectName: 'invoice', |
| 49 | + locations: ['record_more'], |
| 50 | + type: 'script', |
| 51 | + body: { language: 'expression', source: 'true' }, |
| 52 | +}; |
| 53 | + |
| 54 | +const DESCRIPTION = 'Send the invoice to its customer once it has been approved by finance.'; |
| 55 | + |
| 56 | +function mountHost(initial: Draft) { |
| 57 | + const seen: { draft: Draft; writes: Draft[] } = { draft: initial, writes: [] }; |
| 58 | + function Host() { |
| 59 | + const [draft, setDraft] = React.useState<Draft>(initial); |
| 60 | + seen.draft = draft; |
| 61 | + return ( |
| 62 | + <ActionDefaultInspector |
| 63 | + type="action" |
| 64 | + name={String(initial.name)} |
| 65 | + locale={'en-US' as never} |
| 66 | + readOnly={false} |
| 67 | + draft={draft} |
| 68 | + onPatch={(patch) => { |
| 69 | + seen.writes.push(patch); |
| 70 | + setDraft((d) => ({ ...d, ...patch })); |
| 71 | + }} |
| 72 | + /> |
| 73 | + ); |
| 74 | + } |
| 75 | + render(<Host />); |
| 76 | + return seen; |
| 77 | +} |
| 78 | + |
| 79 | +const sent = (draft: Draft): Draft => JSON.parse(JSON.stringify(draft)); |
| 80 | + |
| 81 | +function refusals(draft: Draft): string[] { |
| 82 | + const parsed = ActionSchema.safeParse(sent(draft)); |
| 83 | + return parsed.success ? [] : parsed.error.issues.map((i) => `${i.path.join('.')}:${i.code}`); |
| 84 | +} |
| 85 | + |
| 86 | +const exposeBox = () => screen.getByRole('checkbox', { name: 'Expose to AI agents' }); |
| 87 | +const typeDescription = (text: string) => |
| 88 | + fireEvent.change(screen.getByPlaceholderText('When and why an agent should call this action…'), { target: { value: text } }); |
| 89 | + |
| 90 | +describe('aiBlockPin — "Expose to AI agents" writes the nested ai block (objectstack#26728)', () => { |
| 91 | + it('a tick writes ai.exposed, never a flat key; the only refusal is the description it requires', () => { |
| 92 | + const seen = mountHost(SCRIPT_ACTION); |
| 93 | + expect(refusals(seen.draft)).toEqual([]); |
| 94 | + |
| 95 | + fireEvent.click(exposeBox()); |
| 96 | + expect(seen.writes.at(-1)).toStrictEqual({ ai: { exposed: true } }); |
| 97 | + expect(sent(seen.draft)).not.toHaveProperty('aiExposed'); |
| 98 | + // Before the fix: `:unrecognized_keys` at the action, on every later save. |
| 99 | + expect(refusals(seen.draft)).toEqual(['ai.description:custom']); |
| 100 | + expect(exposeBox()).toBeChecked(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('a description of the spec minimum parses, and reads back into the box', () => { |
| 104 | + const seen = mountHost(SCRIPT_ACTION); |
| 105 | + fireEvent.click(exposeBox()); |
| 106 | + typeDescription('short'); |
| 107 | + expect(refusals(seen.draft)).toEqual(['ai.description:too_small']); |
| 108 | + |
| 109 | + typeDescription(DESCRIPTION); |
| 110 | + expect(sent(seen.draft).ai).toStrictEqual({ exposed: true, description: DESCRIPTION }); |
| 111 | + expect(refusals(seen.draft)).toEqual([]); |
| 112 | + expect(screen.getByPlaceholderText('When and why an agent should call this action…')).toHaveValue(DESCRIPTION); |
| 113 | + }); |
| 114 | + |
| 115 | + it('unticking parses: a finished description is kept, one the spec would refuse goes with the box', () => { |
| 116 | + const seen = mountHost(SCRIPT_ACTION); |
| 117 | + fireEvent.click(exposeBox()); |
| 118 | + typeDescription(DESCRIPTION); |
| 119 | + fireEvent.click(exposeBox()); |
| 120 | + expect(sent(seen.draft).ai).toStrictEqual({ exposed: false, description: DESCRIPTION }); |
| 121 | + expect(refusals(seen.draft)).toEqual([]); |
| 122 | + |
| 123 | + fireEvent.click(exposeBox()); |
| 124 | + typeDescription('short'); |
| 125 | + fireEvent.click(exposeBox()); |
| 126 | + expect(sent(seen.draft).ai).toStrictEqual({ exposed: false }); |
| 127 | + expect(refusals(seen.draft)).toEqual([]); |
| 128 | + }); |
| 129 | + |
| 130 | + it('an older draft carrying the flat keys parses after the control\'s next write', () => { |
| 131 | + // The shape the control wrote before this fix: refused at the action. |
| 132 | + const seen = mountHost({ ...SCRIPT_ACTION, aiExposed: true, aiDescription: 'x' }); |
| 133 | + expect(refusals(seen.draft)).toEqual([':unrecognized_keys']); |
| 134 | + // The flat keys are never read: the box is unticked. |
| 135 | + expect(exposeBox()).not.toBeChecked(); |
| 136 | + |
| 137 | + fireEvent.click(exposeBox()); |
| 138 | + fireEvent.click(exposeBox()); |
| 139 | + expect(sent(seen.draft)).not.toHaveProperty('aiExposed'); |
| 140 | + expect(sent(seen.draft)).not.toHaveProperty('aiDescription'); |
| 141 | + expect(refusals(seen.draft)).toEqual([]); |
| 142 | + }); |
| 143 | + |
| 144 | + it('control: the other ai keys are kept through a tick and an untick', () => { |
| 145 | + const seen = mountHost({ ...SCRIPT_ACTION, ai: { category: 'data', requiresConfirmation: true } }); |
| 146 | + fireEvent.click(exposeBox()); |
| 147 | + typeDescription(DESCRIPTION); |
| 148 | + expect(sent(seen.draft).ai).toStrictEqual({ category: 'data', requiresConfirmation: true, exposed: true, description: DESCRIPTION }); |
| 149 | + expect(refusals(seen.draft)).toEqual([]); |
| 150 | + }); |
| 151 | +}); |
| 152 | + |
| 153 | +describe('aiBlockPin — AI_DESCRIPTION_MIN is the spec minimum (objectstack#26728)', () => { |
| 154 | + const withDescription = (length: number): Draft => ({ ...SCRIPT_ACTION, ai: { exposed: true, description: 'x'.repeat(length) } }); |
| 155 | + |
| 156 | + it('the spec accepts a description of that length and refuses one shorter', () => { |
| 157 | + expect(refusals(withDescription(AI_DESCRIPTION_MIN))).toEqual([]); |
| 158 | + expect(refusals(withDescription(AI_DESCRIPTION_MIN - 1))).toEqual(['ai.description:too_small']); |
| 159 | + }); |
| 160 | +}); |
0 commit comments