Skip to content
Merged
11 changes: 11 additions & 0 deletions .changeset/34122-condition-subjects-label-first.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@object-ui/app-shell': patch
'@object-ui/console': patch
---

fix(console): Studio condition editors list fields by their label

Clause-②: no (`@object-ui/app-shell` is `"private": true`; flipping the `labelSubjects` default reaches no published export)
Metadata-protocol: none

The subject list of every Studio condition editor now offers a field by its label, with its CEL path (`record.status`) beside it as secondary text: a hook's "Run only when", an action's "Visible when" and "Disabled when", a page block's "Visible when", and the generic condition field of the metadata forms. The flow Entry condition and the object Validations panel already did. A field with no label, or whose label only repeats its name, still reads as its path. The compiled condition is unchanged.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
* ever reaches the mounts that legitimately bind `user`.
*/

// The builder reaches a dynamic `import()` of each module below: its first load
// is paid here, at collection, never inside a case's clocked window
// (`check:test-source-alias`).
import '@objectstack/lint';
import '@testing-library/jest-dom/vitest';
import * as React from 'react';
import { describe, it, expect, vi, afterEach } from 'vitest';
Expand Down Expand Up @@ -286,3 +290,37 @@ describe('conditionSubjectsForMetadataType — the per-type derivation (objectui
}
});
});

/* ── Field subjects read by label (objectstack#34122) ────────────────────── */

describe('the generic condition mount names a field subject by its label (objectstack#34122)', () => {
/** The bound object's field catalog as the host loads it: one labelled field, one not. */
const context: WidgetContext = {
conditionScope: 'record',
objectFields: {
status: 'loaded',
data: [
{ name: 'status', label: 'Status', type: 'text' },
{ name: 'code', type: 'text' },
],
},
};

it('a field reads by its label, its spelling as the hint', async () => {
// `ux-conventions.labels-over-machine-names`: the widget passes no
// `labelSubjects`, so this is the builder's default as the mount gets it.
render(<Harness context={context} />);
const group = await screen.findByRole('group', { name: /Run only when/ });
await userEvent.click(within(group).getAllByRole('combobox')[0]);
const option = await screen.findByRole('option', { name: 'Status record.status' });
expect(option.querySelector('.font-mono')).toHaveTextContent(/^record\.status$/);
});

it('CONTROL: a field with no label reads as its spelling alone', async () => {
render(<Harness context={context} />);
const group = await screen.findByRole('group', { name: /Run only when/ });
await userEvent.click(within(group).getAllByRole('combobox')[0]);
const option = await screen.findByRole('option', { name: 'record.code' });
expect(option.querySelector('.font-mono')).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
* `CONTEXT_SUBJECTS`. An over-narrowing reddens here rather than shipping.
*/

// The builder reaches a dynamic `import()` of each module below: its first load
// is paid here, at collection, never inside a case's clocked window
// (`check:test-source-alias`).
import '@objectstack/lint';
import '@testing-library/jest-dom/vitest';
import * as React from 'react';
import { describe, it, expect, vi, afterEach } from 'vitest';
Expand Down Expand Up @@ -94,7 +98,9 @@ async function defaultSubjects(): Promise<string[]> {
/>,
);
await userEvent.click(container.querySelectorAll('[role="combobox"]')[0] as HTMLElement);
return (await screen.findAllByRole('option')).map((o) => o.textContent ?? '');
// A field subject reads by its label, its spelling the `.font-mono` hint
// (objectstack#34122); read the spelling, which is what the engine judges.
return (await screen.findAllByRole('option')).map((o) => (o.querySelector('.font-mono') ?? o).textContent ?? '');
}

/** The CONTEXT half of the default vocabulary — what is not a catalog field. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
* disabled by a predicate the spec prescribes.
*/

// The builder reaches a dynamic `import()` of each module below: its first load
// is paid here, at collection, never inside a case's clocked window
// (`check:test-source-alias`).
import '@objectstack/lint';
import '@testing-library/jest-dom/vitest';
import * as React from 'react';
import { describe, it, expect, vi, afterEach } from 'vitest';
Expand All @@ -78,9 +82,19 @@ import '@objectstack/formula';
// the real network. The verdict under test does not depend on the catalog: the
// engine reports a bare reference from the SCOPE, not from the field list
// (measured with `fields: []` and with `fields: undefined` — identical).
const state = vi.hoisted(() => ({
metadataClient: { get: vi.fn(async () => undefined), list: vi.fn(async () => [] as unknown[]), withPreviewDrafts() { return this; } },
}));
// `objects` is empty for every scope case; only the subject-label cases at the
// foot of this file serve an object definition through it.
const state = vi.hoisted(() => {
const objects: Record<string, unknown> = {};
return {
objects,
metadataClient: {
get: vi.fn(async (type: string, name: string) => (type === 'object' ? objects[name] : undefined)),
list: vi.fn(async () => [] as unknown[]),
withPreviewDrafts() { return this; },
},
};
});
vi.mock('../useMetadata', () => ({
useMetadataClient: () => state.metadataClient,
}));
Expand Down Expand Up @@ -379,3 +393,87 @@ describe('mounts that pass no `scope` are byte-for-byte unchanged (objectui#8167
await expectAccepted(box);
});
});

/* ── Field subjects read by label at every mount (objectstack#34122) ───── */

/**
* objectstack#34122 — each mount's subject dropdown names a field by its label,
* with the CEL spelling beside it as the hint
* (`ux-conventions.labels-over-machine-names`). No mount passes
* `labelSubjects`: the builder's default is on, so these cases pin the default
* as every mount receives it. A field with no label reads as its spelling.
*/
describe('every mount names a field subject by its label (objectstack#34122)', () => {
/** `invoice`, as the object-field catalog serves it: one labelled field, one not. */
const INVOICE = { name: 'invoice', fields: { status: { type: 'text', label: 'Status' }, code: { type: 'text' } } };

afterEach(() => {
delete state.objects.invoice;
});

/** The builder labelled `label`, with one row added; its subject dropdown opened. */
function openSubjects(label: string): void {
const root = builderLabelled(label);
fireEvent.click(within(root).getByRole('button', { name: 'Add condition' }));
fireEvent.keyDown(within(root).getAllByRole('combobox')[0], { key: 'ArrowDown' });
}

/** The labelled field reads `Status`, its spelling `record.status` the hint. */
async function expectLabelFirst(): Promise<void> {
const option = await screen.findByRole('option', { name: 'Status record.status' }, { timeout: 4000 });
expect(option.querySelector('.font-mono')).toHaveTextContent(/^record\.status$/);
}

/** CONTROL — the unlabelled field reads as its spelling alone, with no hint. */
async function expectSpellingOnly(): Promise<void> {
const option = await screen.findByRole('option', { name: 'record.code' }, { timeout: 4000 });
expect(option.querySelector('.font-mono')).toBeNull();
}

for (const label of ['Visible when', 'Disabled when'] as const) {
it(`an action's "${label}" reads a field by its label, its spelling as the hint`, async () => {
state.objects.invoice = INVOICE;
render(<ActionHarness />);
openSubjects(label);
await expectLabelFirst();
await expectSpellingOnly();
});
}

it("a hook's condition reads a field by its label, its spelling as the hint", async () => {
state.objects.invoice = INVOICE;
render(<HookHarness />);
openSubjects('Run only when (optional CEL)');
await expectLabelFirst();
await expectSpellingOnly();
});

it("a record page block's Visible when reads a field by its label, its spelling as the hint", async () => {
state.objects.invoice = INVOICE;
const draft = PageSchema.parse({
name: 'invoice_page',
label: 'Invoice',
type: 'record',
object: 'invoice',
template: 'default',
regions: [{ name: 'main', components: [{ type: 'text', id: 'b1' }] }],
}) as unknown as Record<string, unknown>;
render(
<PageBlockInspector
type="page"
name="invoice_page"
draft={draft}
selection={{ kind: 'block', id: 'regions[0].components[0]' }}
onPatch={() => {}}
onClearSelection={() => {}}
onSelectionChange={() => {}}
onBlockingIssuesChange={() => {}}
readOnly={false}
locale={'en-US' as never}
/>,
);
openSubjects('Visible when (CEL)');
await expectLabelFirst();
await expectSpellingOnly();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
* ordering comparison on a catalog field.
*/

// The builder reaches a dynamic `import()` of each module below: its first load
// is paid here, at collection, never inside a case's clocked window
// (`check:test-source-alias`).
import '@object-ui/core';
import * as React from 'react';
import { describe, it, expect, afterEach, beforeAll, vi } from 'vitest';
import { render, screen, cleanup, fireEvent } from '@testing-library/react';
Expand Down Expand Up @@ -102,7 +106,7 @@ describe('an ordering comparison on a field compiles behind a null guard (object
it('the card flow — Add condition, the optional number, greater than, 40 — writes the guarded shape', async () => {
const { container } = render(<Harness initial="" />);
fireEvent.click(screen.getByRole('button', { name: /Add condition/ }));
await pick(comboboxes(container)[0], 'record.estimated_hours');
await pick(comboboxes(container)[0], 'Estimated Hours record.estimated_hours');
await pick(comboboxes(container)[1], 'greater than');
fireEvent.change(screen.getByPlaceholderText('value'), { target: { value: '40' } });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
* under the placeholder "field / context", while the list-view filter in the
* same product showed labels. A field subject now reads by its label, with the
* spelling the CEL uses beside it; the compiled CEL is unchanged. A field with
* no label keeps its spelling, and the context subjects are as before. The
* label is the mount's choice (`labelSubjects`): a mount that lists spellings
* keeps them.
* no label keeps its spelling, and the context subjects are as before. Labels
* are the default (objectstack#34122); a mount that must list spellings passes
* `labelSubjects={false}`.
*/

// The builder reaches a dynamic `import()` of each module below: its first load
// is paid here, at collection, never inside a case's clocked window
// (`check:test-source-alias`).
import '@objectstack/formula';
import '@objectstack/lint';
import '@object-ui/core';
import '@testing-library/jest-dom/vitest';
import * as React from 'react';
import { describe, it, expect, afterEach, vi } from 'vitest';
Expand Down Expand Up @@ -84,7 +90,29 @@ describe('ConditionBuilder · field subjects read by label (objectstack#27521)',
expect(texts).toContain('Estimated Hours (before the change)previous.estimated_hours');
});

it('CONTROL — a mount that does not ask for labels lists the spellings', async () => {
it('a mount that passes no `labelSubjects` reads labels: the default is on (objectstack#34122)', async () => {
// Rendered here rather than through `mount`: handing `mount` an
// `undefined` would take ITS default (`true`) and pass that explicitly,
// so the builder's own default would never be read.
render(<ConditionBuilder value="" onCommit={vi.fn()} fields={FIELDS} />);
fireEvent.click(screen.getByRole('button', { name: t('engine.inspector.condition.add', 'en-US') }));
const texts = (await openSubjects()).map((o) => o.textContent);
expect(texts).toContain('Estimated Hoursrecord.estimated_hours');
expect(texts).toContain('record.code');
});

it('a label that only repeats the field name is no label: the field reads as its spelling (objectstack#34122)', async () => {
// The object-field catalog fills an unlabelled field's label with its name.
render(
<ConditionBuilder value="" onCommit={vi.fn()} fields={[{ name: 'code', label: 'code', type: 'text' }]} />,
);
fireEvent.click(screen.getByRole('button', { name: t('engine.inspector.condition.add', 'en-US') }));
const option = (await openSubjects()).find((o) => o.textContent?.includes('record.code'))!;
expect(option.textContent).toBe('record.code');
expect(option.querySelector('.font-mono')).toBeNull();
});

it('CONTROL — a mount that opts out of labels lists the spellings', async () => {
mount(false, false);
const texts = (await openSubjects()).map((o) => o.textContent);
expect(texts).toContain('record.estimated_hours');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
* `Builder` toggle would be on screen.
*/

// The builder reaches a dynamic `import()` of each module below: its first load
// is paid here, at collection, never inside a case's clocked window
// (`check:test-source-alias`).
import '@objectstack/formula';
import '@objectstack/lint';
import '@object-ui/core';
import * as React from 'react';
import { describe, it, expect, afterEach, beforeAll, vi } from 'vitest';
import { render, cleanup, screen, fireEvent } from '@testing-library/react';
Expand Down Expand Up @@ -212,12 +218,22 @@ beforeAll(() => {
}
});

/** Open the row's SUBJECT dropdown and read back every subject it offers. */
/**
* An option's CEL spelling. A field subject reads by its label with the
* spelling beside it as the hint (objectstack#34122), so the spelling is that
* hint; a subject with no label (`previous`, the context) is its spelling.
*/
function spellingOf(option: HTMLElement): string {
const hint = option.querySelector('.font-mono');
return (hint ?? option).textContent ?? '';
}

/** Open the row's SUBJECT dropdown and read back the spelling of every subject it offers. */
async function subjectOptions(container: HTMLElement): Promise<string[]> {
const trigger = container.querySelectorAll('[role="combobox"]')[0] as HTMLElement;
await userEvent.click(trigger);
const opts = await screen.findAllByRole('option');
return opts.map((o) => o.textContent ?? '');
return opts.map(spellingOf);
}

/** A mounted builder holding exactly one row, ready to have its subject read. */
Expand Down Expand Up @@ -283,7 +299,7 @@ describe('#6296 DEFAULT-PATH INVARIANCE — the record-scoped consumers are unto
const { container, onCommit } = mountOneRow();
const opts = await subjectOptions(container);
expect(opts).toContain('record.amount');
await userEvent.click(screen.getByRole('option', { name: 'record.amount' }));
await userEvent.click(screen.getByRole('option', { name: 'Amount record.amount' }));
expect(onCommit).toHaveBeenCalledWith("record.amount == 'x'");
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ function initFrom(value: string, valueOf: SubjectValueOf): { rows: Row[]; join:
return { rows: [], join: '&&', raw: !!value };
}

export function ConditionBuilder({ label, value, onCommit, objectName, fields: fieldsProp, disabled, onBlockingIssuesChange, subjects, scope, roots, scopeIssue, labelSubjects = false, verdictPath }: {
export function ConditionBuilder({ label, value, onCommit, objectName, fields: fieldsProp, disabled, onBlockingIssuesChange, subjects, scope, roots, scopeIssue, labelSubjects = true, verdictPath }: {
label?: string;
value: string;
onCommit: (cel: string) => void;
Expand Down Expand Up @@ -934,7 +934,10 @@ export function ConditionBuilder({ label, value, onCommit, objectName, fields: f
scopeIssue?: boolean;
/**
* The subject dropdown names a field by its label, with the CEL spelling
* beside it. Off by default, so a mount that lists spellings keeps them.
* beside it (`ux-conventions.labels-over-machine-names`); a field with no
* label reads as its spelling. On by default, because every mount's subjects
* are the fields of the object it edits. A mount whose spelling must stay
* primary passes `false` and says why at the mount.
*/
labelSubjects?: boolean;
/**
Expand Down Expand Up @@ -967,19 +970,24 @@ export function ConditionBuilder({ label, value, onCommit, objectName, fields: f
// With `labelSubjects`, a field reads by its label, its spelling beside it.
const named = (spelling: string, label: string | undefined) =>
labelSubjects && label ? { value: spelling, label, hint: spelling } : { value: spelling, label: spelling, hint: undefined };
const fieldOpts = visible.map((f) => named(`${fieldPrefix}${f.name}`, f.label));
// A label that only repeats the field's name is no label: the object-field
// catalog (`useObjectFields`) fills an unlabelled field's label with its
// name, and such a field reads as its spelling alone.
const labelOf = (f: { name: string; label?: string }) => (f.label && f.label !== f.name ? f.label : undefined);
const fieldOpts = visible.map((f) => named(`${fieldPrefix}${f.name}`, labelOf(f)));
// The whole-record `previous` comes first: `previous == null` is how an
// author branches on the create leg of a create-or-update trigger, and it
// reads as the head of the group rather than as one more field.
const previousOpts = includePrevious
? [
{ value: 'previous', label: 'previous', hint: undefined },
...visible.map((f) =>
named(
...visible.map((f) => {
const label = labelOf(f);
return named(
`previous.${f.name}`,
f.label ? tFormat('engine.inspector.condition.previousField', locale, { field: f.label }) : undefined,
),
),
label ? tFormat('engine.inspector.condition.previousField', locale, { field: label }) : undefined,
);
}),
]
: [];
const contextOpts = contextSubjects.map((c) => ({ value: c.value, label: c.label ?? c.value, hint: undefined }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
* ill typed, not only if its spelling moves.
*/

// The builder reaches a dynamic `import()` of each module below: its first load
// is paid here, at collection, never inside a case's clocked window
// (`check:test-source-alias`).
import '@objectstack/lint';
import '@object-ui/core';
import * as React from 'react';
import { describe, it, expect, afterEach, beforeAll, vi } from 'vitest';
import { render, screen, cleanup, fireEvent, waitFor } from '@testing-library/react';
Expand Down Expand Up @@ -294,7 +299,7 @@ describe('a new row starts on a typed operator, not `truthy`', () => {
fireEvent.click(screen.getByText('Add condition'));
expect(lastOpTrigger(container).textContent).toBe('equals');
await userEvent.click(comboboxes(container)[0]);
await userEvent.click(await screen.findByRole('option', { name: 'record.due_date' }));
await userEvent.click(await screen.findByRole('option', { name: 'Due date record.due_date' }));
const cel = committed();
expect(cel).toBe("record.due_date == ''");
expect(evaluate(cel, { due_date: null })).toEqual({ ok: true, value: false });
Expand Down
Loading