|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * The TRUE-identity fold counts only the keys the loop PROCESSED — objectui#9030. |
| 11 | + * |
| 12 | + * ## What was wrong |
| 13 | + * |
| 14 | + * `convertFiltersToAST` folds a filter that is nothing but TRUE-identity |
| 15 | + * combinators to `undefined` (objectui#8770), and folds a filter whose every key |
| 16 | + * the loop SKIPPED for a `null` / `undefined` value to `undefined` too |
| 17 | + * (objectui#9020). Each fold was keyed on its own count compared with |
| 18 | + * `Object.keys(filter).length`. |
| 19 | + * |
| 20 | + * But the loop's first statement skips the null/undefined keys without |
| 21 | + * incrementing anything the identity fold counts, while that denominator counted |
| 22 | + * them anyway. So a filter carrying one identity group beside one skipped key |
| 23 | + * read `1 === 2`, the fold declined, and the CALLER'S ORIGINAL OBJECT came back: |
| 24 | + * |
| 25 | + * ``` |
| 26 | + * { $and: [] } => undefined folds |
| 27 | + * { b: undefined } => undefined folds |
| 28 | + * { $and: [], b: undefined } => { $and: [], … } did NOT fold |
| 29 | + * ``` |
| 30 | + * |
| 31 | + * ⭐ Each key folds ALONE and the two together did not — behaviour decided by a |
| 32 | + * sibling, the hazard objectui#8555 named on this same function. Read the other |
| 33 | + * way round: adding an always-TRUE `$and: []` to a filter that folded could stop |
| 34 | + * it folding. |
| 35 | + * |
| 36 | + * ## Why BOTH skipped classes come along, and why that is not a new ruling |
| 37 | + * |
| 38 | + * The loop skips `null` and `undefined` with ONE statement, and objectui#9020 |
| 39 | + * already ruled what that skip means when it is all that is left: the key |
| 40 | + * contributes no condition, so the filter constrains nothing and says so. That |
| 41 | + * ruling is what this card carries into the mixed case; it does not re-decide |
| 42 | + * it, and it does not touch the skip — `{ a: null, s: 1 }` still lowers to |
| 43 | + * `['s', '=', 1]`, pinned in the controls below. |
| 44 | + * |
| 45 | + * ⚠️ The card that filed this proposed admitting `undefined` while keeping |
| 46 | + * `null` out, on the reasoning that a null-valued key rides the `$expand` route |
| 47 | + * as a real `a IS NULL` predicate. That reasoning was measured before |
| 48 | + * objectui#9020 landed and no longer holds: `{ a: null }` no longer reaches |
| 49 | + * either route as a predicate. Keeping `null` out would ALSO have meant reading |
| 50 | + * the two spellings apart for the first time anywhere in this file, and would |
| 51 | + * have left `{ $and: [], a: null }` as the one member of the family that still |
| 52 | + * 400s on the plain route — the very defect objectui#8770 exists to end. The |
| 53 | + * two-route measurement is in |
| 54 | + * `filter-identity-skipped-mix-two-routes-9030.test.ts`. |
| 55 | + * |
| 56 | + * ## Why the assertions are shaped the way they are |
| 57 | + * |
| 58 | + * Two halves, the shape taken from objectui#8770's own file, because either |
| 59 | + * alone passes on something worse than the bug: |
| 60 | + * |
| 61 | + * - a SHAPE assertion (`toBeUndefined`) alone passes on a converter that has |
| 62 | + * stopped emitting anything at all; |
| 63 | + * - a ROW-SET assertion alone cannot tell "honoured" from "dropped" — a filter |
| 64 | + * whose ruled answer is every row and an absent filter select the same rows. |
| 65 | + * |
| 66 | + * So §0 proves the fixture distinguishes every-row from a real subset before any |
| 67 | + * of it is read that way, the shape half is asserted against the spec's own door |
| 68 | + * (`isFilterAST`) rather than a literal, and the FALSE identity `{ $or: [] }` is |
| 69 | + * carried as a control that must keep selecting NO rows. |
| 70 | + */ |
| 71 | + |
| 72 | +import { describe, it, expect } from 'vitest'; |
| 73 | +import { isFilterAST } from '@objectstack/spec/data'; |
| 74 | +import { convertFiltersToAST, toFilterNode, mergeFilterNodes } from '../filter-converter'; |
| 75 | +import { ValueDataSource } from '../../adapters/ValueDataSource'; |
| 76 | + |
| 77 | +const ROWS = [ |
| 78 | + { id: '1', a: 'x', s: 1 }, |
| 79 | + { id: '2', a: null, s: 1 }, |
| 80 | + { id: '3', a: 'y', s: 2 }, |
| 81 | + { id: '4', a: 'z', s: 2 }, |
| 82 | +]; |
| 83 | +const ALL_IDS = ['1', '2', '3', '4']; |
| 84 | + |
| 85 | +async function selectedIds(filter: unknown): Promise<string[]> { |
| 86 | + const ds = new ValueDataSource({ items: ROWS as never }); |
| 87 | + const result = await ds.find('rows', (filter === undefined ? {} : { $filter: filter }) as never); |
| 88 | + return (result.data as Array<{ id: string }>).map((r) => String(r.id)); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * The census, enumerated rather than exemplified — a repair aimed at the card's |
| 93 | + * one literal would be re-touched immediately. |
| 94 | + * |
| 95 | + * The family is stated as a rule, not a list: a filter that produces NO |
| 96 | + * condition and whose every key was either a TRUE-identity group or a key the |
| 97 | + * loop skipped. The rows below walk that rule's degrees of freedom — which |
| 98 | + * identity spelling, which skipped spelling, key ORDER, more than one of each, |
| 99 | + * and an identity group that only becomes one because ITS child folded. |
| 100 | + */ |
| 101 | +const MIXED_FAMILY: ReadonlyArray<[string, Record<string, unknown>]> = [ |
| 102 | + ['{ $and: [], b: undefined }', { $and: [], b: undefined }], |
| 103 | + ['{ $and: [], a: null }', { $and: [], a: null }], |
| 104 | + ['{ $or: [{}], b: undefined }', { $or: [{}], b: undefined }], |
| 105 | + ['{ $and: [{}], a: null }', { $and: [{}], a: null }], |
| 106 | + ['{ b: undefined, $and: [] }', { b: undefined, $and: [] }], |
| 107 | + [ |
| 108 | + '{ $and: [], $or: [{}], a: null, b: undefined }', |
| 109 | + { $and: [], $or: [{}], a: null, b: undefined }, |
| 110 | + ], |
| 111 | + ['{ $and: [{ a: null }], b: undefined }', { $and: [{ a: null }], b: undefined }], |
| 112 | +]; |
| 113 | + |
| 114 | +// --------------------------------------------------------------------------- |
| 115 | +// 0. The harness has to be able to fail |
| 116 | +// --------------------------------------------------------------------------- |
| 117 | + |
| 118 | +describe('objectui#9030 — harness', () => { |
| 119 | + it('every row, the `a = null` subset and a real predicate are three answers here', async () => { |
| 120 | + expect(await selectedIds(undefined)).toEqual(ALL_IDS); |
| 121 | + // The predicate the object became on one route. It is NOT "every row". |
| 122 | + expect(await selectedIds({ a: null })).toEqual(['2']); |
| 123 | + // A filter that really constrains lands strictly between the two, so "every |
| 124 | + // row" below is a measurement and not this fixture's only answer. |
| 125 | + expect(await selectedIds(['s', '=', 1])).toEqual(['1', '2']); |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
| 129 | +// --------------------------------------------------------------------------- |
| 130 | +// 1. The family folds |
| 131 | +// --------------------------------------------------------------------------- |
| 132 | + |
| 133 | +describe('objectui#9030 — an identity group beside a skipped key folds', () => { |
| 134 | + it.each(MIXED_FAMILY)('%s lowers to "no constraint"', (_label, filter) => { |
| 135 | + expect(convertFiltersToAST(filter)).toBeUndefined(); |
| 136 | + }); |
| 137 | + |
| 138 | + it.each(MIXED_FAMILY)('%s is no longer the caller\'s own object', (_label, filter) => { |
| 139 | + // Stated as identity rather than shape: what came back WAS the input, so a |
| 140 | + // caller could not tell "lowered" from "handed back untouched". |
| 141 | + expect(convertFiltersToAST(filter)).not.toBe(filter); |
| 142 | + }); |
| 143 | + |
| 144 | + it('nothing unreadable is handed back', () => { |
| 145 | + // The property objectui#8770 and objectui#9020 each stated for their own |
| 146 | + // member: a value this function returns is either `undefined` — no filter — |
| 147 | + // or something the spec's own door accepts. A plain object is neither. |
| 148 | + for (const [, filter] of MIXED_FAMILY) { |
| 149 | + const out = convertFiltersToAST(filter); |
| 150 | + expect(out === undefined || isFilterAST(out)).toBe(true); |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + it('⭐ the property the counting broke — each key alone, and the two together', () => { |
| 155 | + // The defect in one assertion, and the reason this is a counting fix rather |
| 156 | + // than a new member: both halves already folded, so their conjunction had |
| 157 | + // no third answer available to it. |
| 158 | + expect(convertFiltersToAST({ $and: [] })).toBeUndefined(); |
| 159 | + expect(convertFiltersToAST({ b: undefined })).toBeUndefined(); |
| 160 | + expect(convertFiltersToAST({ $and: [], b: undefined })).toBeUndefined(); |
| 161 | + }); |
| 162 | +}); |
| 163 | + |
| 164 | +// --------------------------------------------------------------------------- |
| 165 | +// 2. The sinks |
| 166 | +// --------------------------------------------------------------------------- |
| 167 | + |
| 168 | +describe('objectui#9030 — the sinks', () => { |
| 169 | + it.each(MIXED_FAMILY)('%s is skipped outright by `toFilterNode`', (_label, filter) => { |
| 170 | + expect(toFilterNode(filter)).toBeUndefined(); |
| 171 | + }); |
| 172 | + |
| 173 | + it.each(MIXED_FAMILY)('%s contributes nothing to the merged `and`', async (_label, filter) => { |
| 174 | + // What `plugin-list`'s `buildEffectiveFilter` and `plugin-view`'s |
| 175 | + // `ObjectView` build. Before the fix the object landed in AST CHILD |
| 176 | + // position — `['and', { $and: [] }, ['s', '=', 1]]` — which `isFilterAST` |
| 177 | + // refuses, so a list whose filter narrowed nothing failed to load outright. |
| 178 | + const merged = mergeFilterNodes(filter, ['s', '=', 1]); |
| 179 | + expect(merged).toEqual(['s', '=', 1]); |
| 180 | + expect(isFilterAST(merged)).toBe(true); |
| 181 | + expect(await selectedIds(merged)).toEqual(['1', '2']); |
| 182 | + }); |
| 183 | + |
| 184 | + it.each(MIXED_FAMILY)('%s selects EVERY row through a real matcher', async (_label, filter) => { |
| 185 | + expect(await selectedIds(toFilterNode(filter))).toEqual(ALL_IDS); |
| 186 | + }); |
| 187 | +}); |
| 188 | + |
| 189 | +// --------------------------------------------------------------------------- |
| 190 | +// 3. Controls — everything that must NOT have moved |
| 191 | +// --------------------------------------------------------------------------- |
| 192 | + |
| 193 | +describe('objectui#9030 — controls', () => { |
| 194 | + it('⭐ the skip pin itself is untouched', async () => { |
| 195 | + // ⛔ The one repair this card was forbidden: making a null-valued key |
| 196 | + // meaningful. If this reddens, every caller's row set moved. |
| 197 | + const node = convertFiltersToAST({ a: null, s: 1 }); |
| 198 | + expect(node).toEqual(['s', '=', 1]); |
| 199 | + expect(await selectedIds(node)).toEqual(['1', '2']); |
| 200 | + }); |
| 201 | + |
| 202 | + it('⭐ a plain filter lowers to exactly the AST it always did', async () => { |
| 203 | + expect(convertFiltersToAST({ a: 'x' })).toEqual(['a', '=', 'x']); |
| 204 | + expect(convertFiltersToAST({ a: { $null: true } })).toEqual(['a', 'is_null', true]); |
| 205 | + expect(convertFiltersToAST({ a: 'x', s: 1 })).toEqual([ |
| 206 | + 'and', |
| 207 | + ['a', '=', 'x'], |
| 208 | + ['s', '=', 1], |
| 209 | + ]); |
| 210 | + expect(await selectedIds(convertFiltersToAST({ a: 'x' }))).toEqual(['1']); |
| 211 | + }); |
| 212 | + |
| 213 | + it('⭐ an identity group beside a key that DOES lower still lets the sibling carry it', async () => { |
| 214 | + // The fold must not start swallowing a live sibling now that its |
| 215 | + // denominator is smaller. |
| 216 | + const node = convertFiltersToAST({ $and: [], a: 'x' }); |
| 217 | + expect(node).toEqual(['a', '=', 'x']); |
| 218 | + expect(await selectedIds(node)).toEqual(['1']); |
| 219 | + }); |
| 220 | + |
| 221 | + it('⭐ the FALSE identity `{ $or: [] }` still selects NO row', async () => { |
| 222 | + // FALSE is not "no constraint", and the AST has no contradiction literal, so |
| 223 | + // the pre-existing leaf is still the emission. A fold that swept the |
| 224 | + // identities together would take this control with it. |
| 225 | + const node = convertFiltersToAST({ $or: [] }); |
| 226 | + expect(node).toEqual(['$or', '=', []]); |
| 227 | + expect(await selectedIds(node)).toEqual([]); |
| 228 | + // …and it survives a skipped key and a TRUE identity beside it. |
| 229 | + expect(convertFiltersToAST({ $or: [], a: null })).toEqual(['$or', '=', []]); |
| 230 | + expect(convertFiltersToAST({ $and: [], $or: [] })).toEqual(['$or', '=', []]); |
| 231 | + }); |
| 232 | + |
| 233 | + it('⛔ an empty operator map still returns the original object', () => { |
| 234 | + // NOT a member. Its key was PROCESSED — the loop entered it and the operator |
| 235 | + // loop ran zero times — so it is counted in the denominator and no arm |
| 236 | + // claims it. objectui#8770 measured this boundary and this card does not |
| 237 | + // move it. |
| 238 | + expect(convertFiltersToAST({ a: {} })).toEqual({ a: {} }); |
| 239 | + }); |
| 240 | + |
| 241 | + it('⛔ an empty operator map beside a SKIPPED key still returns the original object', () => { |
| 242 | + // ⭐ The sharpest boundary case, and the one that says what "processed" |
| 243 | + // means: subtracting the skipped key leaves one processed key that produced |
| 244 | + // no condition and is not an identity group, so neither arm fires. A repair |
| 245 | + // that had folded on "no conditions were produced" instead would swallow |
| 246 | + // this. |
| 247 | + const filter = { a: {}, b: undefined }; |
| 248 | + expect(convertFiltersToAST(filter)).toEqual(filter); |
| 249 | + expect(convertFiltersToAST({ $and: [], a: {} })).toEqual({ $and: [], a: {} }); |
| 250 | + }); |
| 251 | + |
| 252 | + it('⛔ an empty filter still returns the original object', () => { |
| 253 | + // `trueIdentityGroups > 0` and `skippedNullKeys > 0` both exclude it: there |
| 254 | + // is no key of either kind. `toFilterNode` already folds `{}` one level up. |
| 255 | + expect(convertFiltersToAST({})).toEqual({}); |
| 256 | + }); |
| 257 | + |
| 258 | + it('⭐ objectui#8770 and objectui#9020 keep their own answers', () => { |
| 259 | + // The two pure members, restated where a reader of this file will see them: |
| 260 | + // this card widened the denominator of one arm, it did not merge the arms. |
| 261 | + expect(convertFiltersToAST({ $and: [] })).toBeUndefined(); |
| 262 | + expect(convertFiltersToAST({ $or: [{}] })).toBeUndefined(); |
| 263 | + expect(convertFiltersToAST({ $and: [{}] })).toBeUndefined(); |
| 264 | + expect(convertFiltersToAST({ a: null })).toBeUndefined(); |
| 265 | + expect(convertFiltersToAST({ b: undefined })).toBeUndefined(); |
| 266 | + expect(convertFiltersToAST({ a: null, b: undefined })).toBeUndefined(); |
| 267 | + }); |
| 268 | +}); |
0 commit comments