|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#27513 — the builder's ordering comparison on a field compiles |
| 5 | + * behind a null guard. |
| 6 | + * |
| 7 | + * The defect, as the release walkthrough measured it: on a Validations tab, |
| 8 | + * "Reject the save when… `record.estimated_hours` greater than `40`" on an |
| 9 | + * optional Number field saved as `record.estimated_hours > 40`. CEL has no `>` |
| 10 | + * overload for `null`, so the server refused the rule at publish and held the |
| 11 | + * whole package back. The "Number can't be negative" preset already writes the |
| 12 | + * guarded shape (`record.x != null && record.x < 0`); the builder now writes it |
| 13 | + * too. |
| 14 | + * |
| 15 | + * The verdicts here are read off the CEL the builder EMITS, judged by the |
| 16 | + * server's own null-guard rule (`@objectstack/lint`'s |
| 17 | + * `findUnguardedNullableOperands`, the gate the publish runs) and evaluated by |
| 18 | + * the engine the server evaluates a validation rule with. ⛔ Neither is called |
| 19 | + * from the builder: it decides nothing about nullability and guards every |
| 20 | + * ordering comparison on a catalog field. |
| 21 | + */ |
| 22 | + |
| 23 | +import * as React from 'react'; |
| 24 | +import { describe, it, expect, afterEach, beforeAll, vi } from 'vitest'; |
| 25 | +import { render, screen, cleanup, fireEvent } from '@testing-library/react'; |
| 26 | +import userEvent from '@testing-library/user-event'; |
| 27 | +import { ExpressionEngine } from '@objectstack/formula'; |
| 28 | +import { findUnguardedNullableOperands } from '@objectstack/lint'; |
| 29 | + |
| 30 | +// objectui#4697 — ConditionBuilder calls useObjectFields(objectName) |
| 31 | +// unconditionally even when `fields` is supplied; keep that fetch off the network. |
| 32 | +const state = vi.hoisted(() => ({ |
| 33 | + metadataClient: { |
| 34 | + get: vi.fn(async (): Promise<unknown> => undefined), |
| 35 | + list: vi.fn(async () => [] as unknown[]), |
| 36 | + withPreviewDrafts() { return this; }, |
| 37 | + }, |
| 38 | +})); |
| 39 | +vi.mock('../useMetadata', () => ({ |
| 40 | + useMetadataClient: () => state.metadataClient, |
| 41 | +})); |
| 42 | + |
| 43 | +import { ConditionBuilder, RECORD_CONDITION_SUBJECTS } from './ConditionBuilder'; |
| 44 | + |
| 45 | +afterEach(cleanup); |
| 46 | + |
| 47 | +beforeAll(() => { |
| 48 | + // Radix Select probes pointer-capture APIs the test DOM lacks. |
| 49 | + for (const m of ['hasPointerCapture', 'setPointerCapture', 'releasePointerCapture'] as const) { |
| 50 | + if (!Element.prototype[m]) { |
| 51 | + // @ts-expect-error test shim |
| 52 | + Element.prototype[m] = m === 'hasPointerCapture' ? () => false : () => {}; |
| 53 | + } |
| 54 | + } |
| 55 | +}); |
| 56 | + |
| 57 | +/** The card's Work Order: an optional Number and a Picklist. */ |
| 58 | +const FIELDS = [ |
| 59 | + { name: 'estimated_hours', label: 'Estimated Hours', type: 'number' }, |
| 60 | + { name: 'status', label: 'Status', type: 'select' }, |
| 61 | +]; |
| 62 | +/** What the server's index calls nullable on that object: neither is required or defaulted. */ |
| 63 | +const NULLABLE = { nullableFields: new Set(['estimated_hours', 'status']) }; |
| 64 | + |
| 65 | +const GUARDED = 'record.estimated_hours != null && record.estimated_hours > 40'; |
| 66 | +const UNGUARDED = 'record.estimated_hours > 40'; |
| 67 | + |
| 68 | +/** Evaluate `source` exactly as objectql's validation-rule evaluator does. */ |
| 69 | +function evaluate(source: string, record: Record<string, unknown>) { |
| 70 | + const res = ExpressionEngine.evaluate<boolean>({ dialect: 'cel', source }, { record, previous: undefined }); |
| 71 | + return res.ok ? { ok: true, value: res.value } : { ok: false }; |
| 72 | +} |
| 73 | + |
| 74 | +/** The Validations tab's mount: record scope, the server host's subjects. */ |
| 75 | +function Harness({ initial }: { initial: string }) { |
| 76 | + const [v, setV] = React.useState(initial); |
| 77 | + return ( |
| 78 | + <div> |
| 79 | + <ConditionBuilder |
| 80 | + value={v} |
| 81 | + onCommit={setV} |
| 82 | + fields={FIELDS} |
| 83 | + scope="record" |
| 84 | + subjects={{ context: RECORD_CONDITION_SUBJECTS }} |
| 85 | + /> |
| 86 | + <pre data-testid="committed">{v}</pre> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +const committed = () => screen.getByTestId('committed').textContent ?? ''; |
| 92 | +const comboboxes = (c: HTMLElement) => Array.from(c.querySelectorAll('[role="combobox"]')) as HTMLElement[]; |
| 93 | +/** The raw editor's textarea, present only when the builder fell back to it. */ |
| 94 | +const rawEditor = (c: HTMLElement) => c.querySelector('textarea'); |
| 95 | + |
| 96 | +async function pick(trigger: HTMLElement, option: string) { |
| 97 | + await userEvent.click(trigger); |
| 98 | + await userEvent.click(await screen.findByRole('option', { name: option })); |
| 99 | +} |
| 100 | + |
| 101 | +describe('an ordering comparison on a field compiles behind a null guard (objectstack#27513)', () => { |
| 102 | + it('the card flow — Add condition, the optional number, greater than, 40 — writes the guarded shape', async () => { |
| 103 | + const { container } = render(<Harness initial="" />); |
| 104 | + fireEvent.click(screen.getByRole('button', { name: /Add condition/ })); |
| 105 | + await pick(comboboxes(container)[0], 'record.estimated_hours'); |
| 106 | + await pick(comboboxes(container)[1], 'greater than'); |
| 107 | + fireEvent.change(screen.getByPlaceholderText('value'), { target: { value: '40' } }); |
| 108 | + |
| 109 | + expect(committed()).toBe(GUARDED); |
| 110 | + // The server's null-guard gate accepts it, and the engine evaluates it on |
| 111 | + // a row with no hours instead of faulting. |
| 112 | + expect(findUnguardedNullableOperands(committed(), NULLABLE)).toEqual([]); |
| 113 | + expect(evaluate(committed(), { estimated_hours: null, status: 'new' })).toEqual({ ok: true, value: false }); |
| 114 | + expect(evaluate(committed(), { estimated_hours: 41, status: 'new' })).toEqual({ ok: true, value: true }); |
| 115 | + expect(evaluate(committed(), { estimated_hours: 40, status: 'new' })).toEqual({ ok: true, value: false }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('INSTRUMENT CONTROL — the emission this replaces is refused by the same gate and faults in the same engine', () => { |
| 119 | + expect(findUnguardedNullableOperands(UNGUARDED, NULLABLE)).toMatchObject([ |
| 120 | + { operand: 'record.estimated_hours', operator: '>' }, |
| 121 | + ]); |
| 122 | + expect(evaluate(UNGUARDED, { estimated_hours: null, status: 'new' })).toEqual({ ok: false }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('the guarded text reopens as the one row it compiles from', () => { |
| 126 | + const { container } = render(<Harness initial={GUARDED} />); |
| 127 | + expect(rawEditor(container)).toBeNull(); |
| 128 | + // One row: its subject and operator, and no join (two rows would read the |
| 129 | + // guard as an "is not empty" row of its own). |
| 130 | + expect(comboboxes(container)).toHaveLength(2); |
| 131 | + expect((screen.getByPlaceholderText('value') as HTMLInputElement).value).toBe('40'); |
| 132 | + expect(committed()).toBe(GUARDED); |
| 133 | + }); |
| 134 | + |
| 135 | + it('beside another row the guarded row is parenthesised, and the OR join round-trips', async () => { |
| 136 | + const { container } = render(<Harness initial="record.status == 'new' || record.estimated_hours == 40" />); |
| 137 | + await pick(comboboxes(container).at(-1)!, 'greater than'); |
| 138 | + const cel = committed(); |
| 139 | + expect(cel).toBe(`record.status == 'new' || (${GUARDED})`); |
| 140 | + expect(findUnguardedNullableOperands(cel, NULLABLE)).toEqual([]); |
| 141 | + |
| 142 | + cleanup(); |
| 143 | + const again = render(<Harness initial={cel} />); |
| 144 | + expect(rawEditor(again.container)).toBeNull(); |
| 145 | + expect(screen.getAllByPlaceholderText('value')).toHaveLength(2); |
| 146 | + }); |
| 147 | + |
| 148 | + it('a stored comparison written without the guard keeps its spelling until the author edits that row', () => { |
| 149 | + const { container } = render(<Harness initial={`${UNGUARDED} && record.status == 'new'`} />); |
| 150 | + // Adopted as rows, not rewritten: the builder writes nothing on mount. |
| 151 | + expect(rawEditor(container)).toBeNull(); |
| 152 | + expect(committed()).toBe(`${UNGUARDED} && record.status == 'new'`); |
| 153 | + |
| 154 | + // Editing the other row leaves this one's bytes alone. |
| 155 | + fireEvent.change(screen.getAllByPlaceholderText('value')[1], { target: { value: 'done' } }); |
| 156 | + expect(committed()).toBe(`${UNGUARDED} && record.status == 'done'`); |
| 157 | + |
| 158 | + // Editing this row writes it the way the builder writes. |
| 159 | + fireEvent.change(screen.getAllByPlaceholderText('value')[0], { target: { value: '50' } }); |
| 160 | + expect(committed()).toBe( |
| 161 | + "(record.estimated_hours != null && record.estimated_hours > 50) && record.status == 'done'", |
| 162 | + ); |
| 163 | + }); |
| 164 | + |
| 165 | + it('equality needs no guard: CEL compares against null without a fault', async () => { |
| 166 | + const { container } = render(<Harness initial="record.estimated_hours > 40" />); |
| 167 | + await pick(comboboxes(container).at(-1)!, 'equals'); |
| 168 | + expect(committed()).toBe('record.estimated_hours == 40'); |
| 169 | + expect(findUnguardedNullableOperands(committed(), NULLABLE)).toEqual([]); |
| 170 | + }); |
| 171 | + |
| 172 | + it('a subject that names no catalog field is not guarded', async () => { |
| 173 | + const { container } = render(<Harness initial="record.id == 5" />); |
| 174 | + await pick(comboboxes(container).at(-1)!, 'greater than'); |
| 175 | + expect(committed()).toBe('record.id > 5'); |
| 176 | + }); |
| 177 | +}); |
0 commit comments