Skip to content

Commit 2bfa60e

Browse files
committed
feat(plugin-detail): default record page turns click-to-advance on, offering only state-machine-admitted moves
Claude-Session: https://claude.ai/code/session_01QSeKYpFBMr917PmVkG9up7 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 31fe289 commit 2bfa60e

4 files changed

Lines changed: 210 additions & 18 deletions

File tree

‎ui/packages/plugin-detail/src/renderers/__tests__/record-path.stageChange-28199.test.tsx‎

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
* a server refusal is shown in place; a reader who may not edit the status
1212
* field — and every path that does not opt in — keeps the readout.
1313
*
14+
* The last two groups pin objectstack#30075 (ruling: option A): the
15+
* synthesized default record page's path carries the opt-in, mounted here as
16+
* the JSON document `buildDefaultPageSchema` emits, through the real
17+
* `SchemaRenderer`; an authored `highlights` slot keeps its declared value;
18+
* and a stage the field's blocking `state_machine` cannot reach from the
19+
* stored value is never offered as a press.
20+
*
1421
* The subject is imported relatively and `@object-ui/*` resolves to source
1522
* through the root vitest alias table, so nothing here reads a `dist/`.
1623
*/
@@ -19,7 +26,7 @@ import * as React from 'react';
1926
import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest';
2027
import { render, cleanup, fireEvent, screen, waitFor, within } from '@testing-library/react';
2128
import '@testing-library/jest-dom';
22-
import { InlineEditProvider, RecordContextProvider, useInlineEdit } from '@object-ui/react';
29+
import { InlineEditProvider, RecordContextProvider, SchemaRenderer, useInlineEdit } from '@object-ui/react';
2330

2431
const { permsStub, perms } = vi.hoisted(() => {
2532
const perms: { isLoaded: boolean; writeDenied: string[] } = { isLoaded: true, writeDenied: [] };
@@ -49,6 +56,9 @@ vi.mock('@object-ui/permissions', async (importOriginal) => {
4956
});
5057

5158
import { RecordPathRenderer } from '../record-path';
59+
import { buildDefaultPageSchema } from '../../synth/buildDefaultPageSchema';
60+
// Registers `record:path` for the `SchemaRenderer` mounts below.
61+
import '../../index';
5262

5363
const STAGES = [
5464
{ value: 'draft', label: 'Draft' },
@@ -272,3 +282,151 @@ describe('record:path allowStageChange — who sees the readout', () => {
272282
await waitFor(() => noButtons(container));
273283
});
274284
});
285+
286+
/** The object as a host binds it: a stage picklist the default page derives its path from. */
287+
const QUOTE_DEF = {
288+
name: 'crm_quote',
289+
fields: {
290+
name: { type: 'text', label: 'Name' },
291+
status: {
292+
type: 'select',
293+
label: 'Status',
294+
options: STAGES.map(({ value, label }) => ({ value, label })),
295+
},
296+
amount: { type: 'currency', label: 'Amount' },
297+
},
298+
stageField: 'status',
299+
};
300+
301+
/** A blocking machine: from draft only in_review (or declined); never straight to accepted. */
302+
const MACHINE = {
303+
type: 'state_machine',
304+
name: 'quote_flow',
305+
field: 'status',
306+
message: 'A quote cannot skip review.',
307+
transitions: { draft: ['in_review', 'declined'], in_review: ['accepted', 'declined'] },
308+
};
309+
310+
/** Every `record:path` node among a synthesized page's top-level components. */
311+
const pathNodesOf = (page: any): any[] =>
312+
page.regions[0].components.filter((c: any) => c?.type === 'record:path');
313+
314+
function mountNode(node: unknown, opts: Omit<MountOptions, 'allowStageChange'> = {}) {
315+
const dataSource = opts.dataSource ?? makeDataSource();
316+
const refresh = opts.refresh ?? vi.fn(async () => {});
317+
const r = render(
318+
<RecordContextProvider
319+
objectName="crm_quote"
320+
recordId="q1"
321+
data={{ id: 'q1', status: 'draft', updated_at: VERSION }}
322+
objectSchema={opts.objectSchema ?? QUOTE_DEF}
323+
dataSource={dataSource as any}
324+
refresh={refresh}
325+
>
326+
<InlineEditProvider canEdit={opts.canEdit ?? true}>
327+
<SchemaRenderer schema={node as never} />
328+
</InlineEditProvider>
329+
</RecordContextProvider>,
330+
);
331+
return { ...r, dataSource, refresh };
332+
}
333+
334+
const buttonNames = (row: HTMLElement): (string | null)[] =>
335+
within(row).queryAllByRole('button').map((b) => b.getAttribute('aria-label'));
336+
337+
describe('#30075 the default record page turns click-to-advance on', () => {
338+
it('the synthesized page writes allowStageChange: true on its path node', () => {
339+
const [path] = pathNodesOf(buildDefaultPageSchema(QUOTE_DEF as never));
340+
expect(path.properties).toMatchObject({ statusField: 'status', allowStageChange: true });
341+
});
342+
343+
it('a reader who may edit the stage field gets a press, with a confirmation, through the record update', async () => {
344+
const [path] = pathNodesOf(buildDefaultPageSchema(QUOTE_DEF as never));
345+
const { container, dataSource, refresh } = mountNode(path);
346+
expect(rows(container)).toHaveLength(2);
347+
for (const row of rows(container)) {
348+
expect(buttonNames(row)).toEqual(['Move to In review', 'Move to Accepted', 'Move to Declined']);
349+
}
350+
fireEvent.click(within(rows(container)[0]).getByRole('button', { name: 'Move to In review' }));
351+
// Nothing is written before the reader confirms.
352+
const dialog = await screen.findByRole('alertdialog');
353+
expect(dataSource.update).not.toHaveBeenCalled();
354+
fireEvent.click(within(dialog).getByRole('button', { name: 'Move' }));
355+
await waitFor(() => expect(dataSource.update).toHaveBeenCalledTimes(1));
356+
expect(dataSource.update).toHaveBeenCalledWith('crm_quote', 'q1', { status: 'in_review' }, { ifMatch: VERSION });
357+
await waitFor(() => expect(refresh).toHaveBeenCalledTimes(1));
358+
});
359+
360+
it('a reader the page may not let edit this record keeps the read-only path', () => {
361+
const [path] = pathNodesOf(buildDefaultPageSchema(QUOTE_DEF as never));
362+
const { container } = mountNode(path, { canEdit: false });
363+
expect(rows(container)).toHaveLength(2);
364+
for (const row of rows(container)) expect(row.querySelectorAll('button,[tabindex]')).toHaveLength(0);
365+
});
366+
367+
it('a reader without the edit grant on the stage field keeps the read-only path', () => {
368+
perms.writeDenied = ['status'];
369+
const [path] = pathNodesOf(buildDefaultPageSchema(QUOTE_DEF as never));
370+
const { container } = mountNode(path);
371+
expect(rows(container)).toHaveLength(2);
372+
for (const row of rows(container)) expect(row.querySelectorAll('button,[tabindex]')).toHaveLength(0);
373+
});
374+
375+
it.each([true, false])('an authored highlights slot keeps its declared allowStageChange: %s', (declared) => {
376+
const authored = { type: 'record:path', properties: { statusField: 'status', stages: STAGES, allowStageChange: declared } };
377+
const page = buildDefaultPageSchema(QUOTE_DEF as never, { slots: { highlights: [authored] } } as never);
378+
const [path] = pathNodesOf(page);
379+
expect(path.properties.allowStageChange).toBe(declared);
380+
const { container } = mountNode(path);
381+
const names = buttonNames(rows(container)[0]);
382+
expect(names.length > 0).toBe(declared);
383+
});
384+
385+
it('an authored highlights slot that leaves the key out keeps the readout', () => {
386+
const authored = { type: 'record:path', properties: { statusField: 'status', stages: STAGES } };
387+
const page = buildDefaultPageSchema(QUOTE_DEF as never, { slots: { highlights: [authored] } } as never);
388+
const [path] = pathNodesOf(page);
389+
expect('allowStageChange' in path.properties).toBe(false);
390+
for (const row of rows(mountNode(path).container)) expect(buttonNames(row)).toEqual([]);
391+
});
392+
});
393+
394+
describe('#30075 the press offers only the moves the field\'s state machine admits', () => {
395+
const MACHINED = { ...QUOTE_DEF, validations: [MACHINE] };
396+
397+
it('a stage the machine cannot reach from the stored value is a readout, not a button', () => {
398+
const [path] = pathNodesOf(buildDefaultPageSchema(MACHINED as never));
399+
const { container } = mountNode(path, { objectSchema: MACHINED });
400+
for (const row of rows(container)) {
401+
expect(buttonNames(row)).toEqual(['Move to In review', 'Move to Declined']);
402+
// The unreachable stage is still drawn, as a plain listitem.
403+
expect(within(row).getByText('Accepted').closest('button')).toBeNull();
404+
}
405+
});
406+
407+
it('control: a warning-severity machine does not narrow the press (the server would not refuse)', () => {
408+
const warned = { ...QUOTE_DEF, validations: [{ ...MACHINE, severity: 'warning' }] };
409+
const [path] = pathNodesOf(buildDefaultPageSchema(warned as never));
410+
const { container } = mountNode(path, { objectSchema: warned });
411+
expect(buttonNames(rows(container)[0])).toEqual(['Move to In review', 'Move to Accepted', 'Move to Declined']);
412+
});
413+
414+
it('a move the server still refuses (here a required field elsewhere) is shown in place, nothing else changes', async () => {
415+
const refusal = Object.assign(new Error('VALIDATION_FAILED'), {
416+
code: 'VALIDATION_FAILED',
417+
fields: [{ field: 'amount', code: 'required', message: 'is required to send a quote for review' }],
418+
});
419+
const [path] = pathNodesOf(buildDefaultPageSchema(MACHINED as never));
420+
const { container, refresh } = mountNode(path, {
421+
objectSchema: MACHINED,
422+
dataSource: makeDataSource(async () => { throw refusal; }),
423+
});
424+
await pressAndConfirm(container, 'In review');
425+
const alert = await within(container).findByRole('alert');
426+
expect(alert).toHaveTextContent('Amount: is required to send a quote for review');
427+
expect(refresh).not.toHaveBeenCalled();
428+
for (const row of rows(container)) {
429+
expect(row.querySelector('[aria-current="step"]')).toHaveAttribute('aria-label', 'Draft, current stage');
430+
}
431+
});
432+
});

‎ui/packages/plugin-detail/src/renderers/record-path.tsx‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626
* mouseup → click) left the record's status untouched. Users spent clicks
2727
* on it before concluding it was decoration.
2828
*
29-
* Unless the author opts in with `allowStageChange: true`, nothing on the
30-
* path writes, so the pixels must not promise a press. Click-to-advance is
31-
* that opt-in (objectstack#28061, `./recordPathStageChange`): only then, and
32-
* only for a reader who may edit `statusField` on this record, does each stage
33-
* other than the current one carry a real `<button>` that asks to move the
34-
* record there. Everyone else — and every page that does not opt in — gets the
35-
* readout below, unchanged.
29+
* Unless the node carries `allowStageChange: true`, nothing on the path
30+
* writes, so the pixels must not promise a press. Click-to-advance is that
31+
* opt-in (objectstack#28061, `./recordPathStageChange`): only then, and only
32+
* for a reader who may edit `statusField` on this record, does each stage
33+
* other than the current one — and that the field's state machine can reach —
34+
* carry a real `<button>` that asks to move the record there. The synthesized
35+
* default record page writes the opt-in on its path (objectstack#30075); an
36+
* authored page keeps what it declared. Everyone else — and every node that
37+
* does not opt in — gets the readout below, unchanged.
3638
*
3739
* So the presentation below is a PROGRESS RAIL: a thin decorative indicator
3840
* per stage with the label as plain text beneath it, which is the same
@@ -594,9 +596,10 @@ const RecordPathBody: React.FC<RecordPathRendererProps> = ({
594596
/** The label's full text as a `title`, for a row whose labels can be cut. */
595597
labelTitle?: string;
596598
}) => {
597-
// A stage is a press only behind the opt-in, and never the stage the
598-
// record already stands on: there is nowhere to move to.
599-
const press = stageChange.pressable && o.state !== 'current';
599+
// A stage is a press only behind the opt-in, never the stage the record
600+
// already stands on (there is nowhere to move to), and never a stage the
601+
// field's blocking state machine cannot reach from the stored value.
602+
const press = stageChange.pressable && o.state !== 'current' && stageChange.admits(o.stage.value);
600603
const face = (
601604
<>
602605
<span aria-hidden="true" data-stage-rail="" className={railClass(o.state, o.terminal)} />

‎ui/packages/plugin-detail/src/renderers/recordPathStageChange.tsx‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,18 @@
2525
* `updated_at` as the `ifMatch` concurrency token, and re-reads the record.
2626
*
2727
* Which moves are legal is the SERVER's call: the object's `state_machine`
28-
* rule (ADR-0020), required fields and edit rights are judged on the write, and
29-
* nothing here evaluates them. A refusal is shown in place, under the rail —
30-
* the server's own sentences first, a fixed line only when it sent none.
28+
* rule (ADR-0020), required fields and edit rights are judged on the write. A
29+
* refusal is shown in place, under the rail — the server's own sentences
30+
* first, a fixed line only when it sent none.
31+
*
32+
* What the path OFFERS follows the same machine the record form, the grid's
33+
* inline editor and the board read (`stateMachineNextValues`): a stage the
34+
* field's blocking `state_machine` rule cannot reach from the STORED value is
35+
* drawn as a readout, not a button, so the press never promises a move the
36+
* server is bound to refuse. The default record page turns this block's press
37+
* on (objectstack#30075), which is why an always-refused button would reach
38+
* every app with a machine on its stage field. A field without such a rule
39+
* offers every stage, and the server still judges each write.
3140
*/
3241

3342
import React from 'react';
@@ -41,7 +50,7 @@ import {
4150
AlertDialogHeader,
4251
AlertDialogTitle,
4352
} from '@object-ui/components';
44-
import { resolveFieldAffordance } from '@object-ui/core';
53+
import { resolveFieldAffordance, stateMachineNextValues } from '@object-ui/core';
4554
import { usePermissions } from '@object-ui/permissions';
4655
import {
4756
declaredUserMessage,
@@ -113,6 +122,12 @@ const canUpdate = (ds: unknown): boolean => {
113122
export interface StageChange {
114123
/** Whether this reader may press a stage of this record's path. */
115124
pressable: boolean;
125+
/**
126+
* Whether the field's blocking `state_machine` admits a move from the
127+
* stored value to `value` — `true` for every value when no such rule
128+
* governs the field. Only meaningful while `pressable`.
129+
*/
130+
admits: (value: unknown) => boolean;
116131
/** A write is in flight. */
117132
busy: boolean;
118133
/** The last refusal's lines, until the next press. */
@@ -152,6 +167,10 @@ export function useStageChange(allowStageChange: boolean, statusField: string |
152167
&& !NON_EDITABLE_SYSTEM_FIELDS.has(statusField)
153168
&& resolveFieldAffordance('recordEdit', perms, objectName, statusField);
154169

170+
const stored = statusField ? (ctx?.data as Record<string, unknown> | undefined)?.[statusField] : undefined;
171+
const reachable = statusField ? stateMachineNextValues(ctx?.objectSchema, statusField, stored) : null;
172+
const admits = (value: unknown): boolean => reachable === null || reachable.has(String(value));
173+
155174
const request = React.useCallback((stage: { value: unknown; label: string }) => {
156175
setRefusal(null);
157176
setTarget(stage);
@@ -166,7 +185,7 @@ export function useStageChange(allowStageChange: boolean, statusField: string |
166185
const confirm = async () => {
167186
const stage = target;
168187
setTarget(null);
169-
if (!stage || !pressable) return;
188+
if (!stage || !pressable || !admits(stage.value)) return;
170189
setBusy(true);
171190
const updatedAt = (ctx?.data as Record<string, unknown> | undefined)?.updated_at;
172191
const ifMatch = typeof updatedAt === 'string' ? updatedAt : undefined;
@@ -202,5 +221,5 @@ export function useStageChange(allowStageChange: boolean, statusField: string |
202221
</AlertDialog>
203222
);
204223

205-
return { pressable, busy, refusal, request, dialog };
224+
return { pressable, admits, busy, refusal, request, dialog };
206225
}

‎ui/packages/plugin-detail/src/synth/buildDefaultPageSchema.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,18 @@ export function buildDefaultActions(
535535
*
536536
* Returns an array because the strip is conceptually one region with
537537
* two adjacent nodes.
538+
*
539+
* The default page's path turns click-to-advance on (`allowStageChange:
540+
* true`, maintainer ruling recorded on objectstack#30075, option A): moving a
541+
* record to its next stage is the record page's most frequent edit, and most
542+
* apps never author a page to opt in on. Nothing about WHO may press moves
543+
* here — `record:path`'s `useStageChange` still asks the page's edit session,
544+
* the field's read-only/computed markers and the caller's field edit grant,
545+
* offers only the moves the field's blocking `state_machine` admits, confirms
546+
* every press, and saves through the ordinary record update, so the server's
547+
* validation decides. A reader who may not edit the field keeps the readout.
548+
* An authored page — or an authored `highlights` slot — writes its own
549+
* `record:path` node and keeps whatever it declared; no new key is involved.
538550
*/
539551
export function buildDefaultHighlights(
540552
def: ObjectDefLike | undefined,
@@ -550,7 +562,7 @@ export function buildDefaultHighlights(
550562
out.push(componentNode('record:highlights', { fields: highlightFields }));
551563
}
552564
if (statusField && stages && stages.length > 0) {
553-
out.push(componentNode('record:path', { statusField, stages }));
565+
out.push(componentNode('record:path', { statusField, stages, allowStageChange: true }));
554566
}
555567
return out;
556568
}

0 commit comments

Comments
 (0)