|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `Is null` must not ERASE the stored dataset filter (objectui#9363). |
| 5 | + * |
| 6 | + * ## The mechanism, and why it is data loss rather than a wrong result set |
| 7 | + * |
| 8 | + * `groupToCondition` returns `undefined` when no row survives serialization, |
| 9 | + * and the Studio dataset inspector commits on EVERY change — the filter popover |
| 10 | + * mounts the shared `FilterBuilder` and hands each emitted group straight to |
| 11 | + * `onCommit(groupToCondition(g))`, which lands as `onPatch({ filter })` and is |
| 12 | + * spread over the draft. So an author with a working `dataset.filter` who opens |
| 13 | + * the inspector and switches the single condition's operator to **Is null** |
| 14 | + * commits `undefined`: the persisted key is destroyed, nothing errors, and the |
| 15 | + * panel still shows the condition. |
| 16 | + * |
| 17 | + * The inspector passes no `extraOperators`, so `isNull` / `isNotNull` are |
| 18 | + * ordinary entries in the default menu — not opt-in ones. The parity block at |
| 19 | + * the bottom of this file reads that offering from the builder's own exported |
| 20 | + * bucket function rather than restating it. |
| 21 | + * |
| 22 | + * ## The distinction this file exists to keep |
| 23 | + * |
| 24 | + * The `continue` these two operators fell into is a DELIBERATE decision for |
| 25 | + * operators the bridge does not map: dropping beats emitting a filter that |
| 26 | + * means something else. That behaviour is kept, and pinned below, because a |
| 27 | + * repair that made the fallback stop dropping everything it cannot map would |
| 28 | + * emit wrong filters — a worse defect than this one. |
| 29 | + * |
| 30 | + * `isNull` / `isNotNull` are a different case and that is the whole repair: the |
| 31 | + * dialect CAN express them (the spec's own `$null`, asserted below against |
| 32 | + * `FILTER_OPERATORS` rather than assumed), the builder draws them as COMPLETE |
| 33 | + * rows with no value input, and this inspector offers them. The drop was an |
| 34 | + * unhandled operator falling into the fallback's path, not the fallback doing |
| 35 | + * its job. Both readings are pinned so they stay distinguishable. |
| 36 | + * |
| 37 | + * ## Red-first |
| 38 | + * |
| 39 | + * Before the repair, the two `$null` expectations fail with `undefined` while |
| 40 | + * the `equals` control in the same run passes — a table of all-`undefined` |
| 41 | + * answers and a dead function are otherwise indistinguishable. |
| 42 | + */ |
| 43 | +import { describe, it, expect } from 'vitest'; |
| 44 | +import { FILTER_OPERATORS, FieldOperatorsSchema } from '@objectstack/spec/data'; |
| 45 | +import { |
| 46 | + FILTER_BUILDER_OPERATORS, |
| 47 | + VALUELESS_FILTER_BUILDER_OPERATORS, |
| 48 | + operatorsForFieldType, |
| 49 | +} from '@object-ui/components'; |
| 50 | +import { groupToCondition, conditionToGroup } from './datasetFilterCondition'; |
| 51 | +import type { BuilderGroup } from './datasetFilterCondition'; |
| 52 | + |
| 53 | +/** One condition row, as the builder emits it. */ |
| 54 | +const row = (operator: string, value: unknown = ''): BuilderGroup => ({ |
| 55 | + id: 'g', |
| 56 | + logic: 'and', |
| 57 | + conditions: [{ id: 'c1', field: 'closed_at', operator, value }], |
| 58 | +}); |
| 59 | + |
| 60 | +describe('groupToCondition — the null predicates this inspector offers (objectui#9363)', () => { |
| 61 | + it('CONTROL: a mapped operator still serializes, so an empty answer below is about that operator', () => { |
| 62 | + expect(groupToCondition(row('equals', 'acme'))).toEqual({ closed_at: { $eq: 'acme' } }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('isNull serializes to the dialect\'s null predicate instead of vanishing', () => { |
| 66 | + expect( |
| 67 | + groupToCondition(row('isNull')), |
| 68 | + 'an `Is null` row serialized to nothing; committing that ERASES dataset.filter', |
| 69 | + ).toEqual({ closed_at: { $null: true } }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('isNotNull serializes to the same predicate negated', () => { |
| 73 | + expect(groupToCondition(row('isNotNull'))).toEqual({ closed_at: { $null: false } }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('keeps a null row alongside a complete one instead of dropping either', () => { |
| 77 | + expect(groupToCondition({ |
| 78 | + id: 'g', |
| 79 | + logic: 'and', |
| 80 | + conditions: [ |
| 81 | + { id: 'c1', field: 'stage', operator: 'equals', value: 'won' }, |
| 82 | + { id: 'c2', field: 'closed_at', operator: 'isNull', value: '' }, |
| 83 | + ], |
| 84 | + })).toEqual({ $and: [{ stage: { $eq: 'won' } }, { closed_at: { $null: true } }] }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('THE DEFECT: switching the only row of a stored filter to Is null no longer commits `undefined`', () => { |
| 88 | + // The exact author gesture: a dataset that already has a filter, opened in |
| 89 | + // the inspector, one operator change. `undefined` here is not "unchanged" — |
| 90 | + // it is what the host spreads over the draft as `{ filter: undefined }`, |
| 91 | + // the same patch shape `objectChangePatch` uses to CLEAR the filter. |
| 92 | + const stored = { stage: { $eq: 'won' } }; |
| 93 | + const { group, representable } = conditionToGroup(stored); |
| 94 | + expect(representable).toBe(true); |
| 95 | + const edited: BuilderGroup = { |
| 96 | + ...group, |
| 97 | + conditions: [{ ...group.conditions[0], operator: 'isNull', value: '' }], |
| 98 | + }; |
| 99 | + expect( |
| 100 | + groupToCondition(edited), |
| 101 | + 'the commit for this gesture was `undefined`, which erases the stored filter', |
| 102 | + ).toEqual({ stage: { $null: true } }); |
| 103 | + }); |
| 104 | + |
| 105 | + it('leaves the $exists pair exactly as it was', () => { |
| 106 | + expect(groupToCondition(row('isEmpty'))).toEqual({ closed_at: { $exists: false } }); |
| 107 | + expect(groupToCondition(row('isNotEmpty'))).toEqual({ closed_at: { $exists: true } }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('still drops an operator it does not map, rather than emitting a wrong filter', () => { |
| 111 | + // Deliberate, and kept: see this file's header. These four are OFFERED by |
| 112 | + // the menu and dropped, which erases the same way — tracked as its own |
| 113 | + // finding, not widened here on the way past. |
| 114 | + expect(groupToCondition(row('notContains', 'a'))).toBeUndefined(); |
| 115 | + expect(groupToCondition(row('between', [1, 5]))).toBeUndefined(); |
| 116 | + expect(groupToCondition(row('startsWith', 'a'))).toBeUndefined(); |
| 117 | + expect(groupToCondition(row('endsWith', 'a'))).toBeUndefined(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('an empty group is still `undefined` — that is the author CLEARING the filter', () => { |
| 121 | + expect(groupToCondition({ id: 'g', logic: 'and', conditions: [] })).toBeUndefined(); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +describe('the emitted token is the spec\'s, not a local invention (objectui#9363)', () => { |
| 126 | + it('`$null` is a member of the spec\'s filter operator vocabulary', () => { |
| 127 | + expect(FILTER_OPERATORS).toContain('$null'); |
| 128 | + // Negative control: membership is a real reading, not a list that contains |
| 129 | + // everything. A plausible-looking spelling this bridge could have invented |
| 130 | + // is NOT in it. |
| 131 | + expect(FILTER_OPERATORS).not.toContain('$isNull'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('the spec\'s field-operator door accepts the boolean comparand and refuses a wrong one', () => { |
| 135 | + expect(FieldOperatorsSchema.safeParse({ $null: true }).success).toBe(true); |
| 136 | + expect(FieldOperatorsSchema.safeParse({ $null: false }).success).toBe(true); |
| 137 | + // Negative control: this door judges the VALUE, so a string comparand is |
| 138 | + // refused — without this leg the assertion above would pass for a schema |
| 139 | + // that accepts anything. |
| 140 | + expect(FieldOperatorsSchema.safeParse({ $null: 'yes' }).success).toBe(false); |
| 141 | + }); |
| 142 | +}); |
| 143 | + |
| 144 | +describe('conditionToGroup — the read half round-trips the new shape (objectui#9363)', () => { |
| 145 | + it('reads a stored $null back as the operator the author picked', () => { |
| 146 | + expect(conditionToGroup({ closed_at: { $null: true } })).toEqual({ |
| 147 | + group: { id: 'g', logic: 'and', conditions: [{ id: 'c0', field: 'closed_at', operator: 'isNull', value: '' }] }, |
| 148 | + representable: true, |
| 149 | + }); |
| 150 | + expect(conditionToGroup({ closed_at: { $null: false } }).group.conditions[0].operator) |
| 151 | + .toBe('isNotNull'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('round-trips condition → group → condition', () => { |
| 155 | + for (const c of [{ closed_at: { $null: true } }, { closed_at: { $null: false } }]) { |
| 156 | + const { group, representable } = conditionToGroup(c); |
| 157 | + expect(representable, `${JSON.stringify(c)} fell back to the source editor`).toBe(true); |
| 158 | + expect(groupToCondition(group)).toEqual(c); |
| 159 | + } |
| 160 | + }); |
| 161 | +}); |
| 162 | + |
| 163 | +/** |
| 164 | + * Offered ⇄ expressible parity for THIS inspector. |
| 165 | + * |
| 166 | + * The direction that broke: every guard in the repo sweeps spec → objectui, |
| 167 | + * asking whether an operator an author may DECLARE can be rendered. None asks |
| 168 | + * whether an operator this dropdown OFFERS can be stored by the consumer that |
| 169 | + * mounted it — and that is the direction where an unmapped operator becomes |
| 170 | + * silent data loss rather than a rendering gap. |
| 171 | + * |
| 172 | + * The offering is read from the builder's own bucket function with NO |
| 173 | + * `extraOperators`, which is exactly what `DatasetFilterField` passes, so a |
| 174 | + * future opt-in granted at that call site has to come through here. |
| 175 | + */ |
| 176 | +const PROBE_FIELD_TYPES: ReadonlyArray<string | undefined> = [ |
| 177 | + undefined, 'text', 'a_type_this_builder_has_never_heard_of', 'number', 'currency', |
| 178 | + 'percent', 'rating', 'boolean', 'date', 'datetime', 'time', 'select', 'status', |
| 179 | + 'lookup', 'master_detail', 'user', |
| 180 | +]; |
| 181 | + |
| 182 | +function offeredAcrossBuckets(extra: readonly string[]): string[] { |
| 183 | + const ids = new Set<string>(); |
| 184 | + for (const type of PROBE_FIELD_TYPES) for (const op of operatorsForFieldType(type, extra)) ids.add(op.value); |
| 185 | + return [...ids].sort(); |
| 186 | +} |
| 187 | + |
| 188 | +/** What the dataset inspector's filter popover offers. */ |
| 189 | +const OFFERED = offeredAcrossBuckets([]); |
| 190 | + |
| 191 | +/** |
| 192 | + * Offered, and deliberately NOT expressible by this bridge today. |
| 193 | + * |
| 194 | + * Each one drops on commit, and a drop of the last surviving row erases the |
| 195 | + * stored filter — the same mechanism objectui#9363 fixed for the null pair. |
| 196 | + * They are listed rather than fixed here because mapping them is a separate |
| 197 | + * decision per operator (`between` needs a both-bounds-present rule before it |
| 198 | + * can be emitted at all), and a blanket "stop dropping" would emit filters that |
| 199 | + * mean something else. Mapping one is what makes this list shrink — and this |
| 200 | + * assertion go red until it is updated. |
| 201 | + */ |
| 202 | +const DECLARED_UNEXPRESSIBLE = ['between', 'endsWith', 'notContains', 'startsWith']; |
| 203 | + |
| 204 | +/** A value that keeps a row from being dropped as INCOMPLETE, per operator. */ |
| 205 | +function probeValue(operator: string): unknown { |
| 206 | + if (VALUELESS_FILTER_BUILDER_OPERATORS.has(operator)) return ''; |
| 207 | + if (operator === 'in' || operator === 'notIn') return ['a', 'b']; |
| 208 | + if (operator === 'between') return [1, 5]; |
| 209 | + return 'x'; |
| 210 | +} |
| 211 | + |
| 212 | +describe('every operator this inspector OFFERS is either expressible or declared (objectui#9363)', () => { |
| 213 | + it('the probe types cover every bucket, so the offering below is the whole dropdown', () => { |
| 214 | + // Granting every id as an opt-in yields the full drawable vocabulary only |
| 215 | + // if the probe list reaches every bucket. Without this, a bucket added |
| 216 | + // later would silently shrink what the partition below is asserted over. |
| 217 | + expect(offeredAcrossBuckets(FILTER_BUILDER_OPERATORS)).toEqual([...FILTER_BUILDER_OPERATORS].sort()); |
| 218 | + }); |
| 219 | + |
| 220 | + it('partitions the offering exactly — no operator is silently unhandled', () => { |
| 221 | + const expressible: string[] = []; |
| 222 | + const dropped: string[] = []; |
| 223 | + for (const operator of OFFERED) { |
| 224 | + (groupToCondition(row(operator, probeValue(operator))) === undefined ? dropped : expressible) |
| 225 | + .push(operator); |
| 226 | + } |
| 227 | + expect(dropped.sort()).toEqual(DECLARED_UNEXPRESSIBLE); |
| 228 | + // The other half of the equality: every remaining offered id serializes. |
| 229 | + expect(expressible.sort()).toEqual(OFFERED.filter((o) => !DECLARED_UNEXPRESSIBLE.includes(o))); |
| 230 | + // And the null pair is on the expressible side — the card's defect, stated |
| 231 | + // as a fact about the offering rather than about two literals. |
| 232 | + expect(expressible).toContain('isNull'); |
| 233 | + expect(expressible).toContain('isNotNull'); |
| 234 | + }); |
| 235 | +}); |
0 commit comments