|
| 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 | + * Merging the filter sources a view can carry. |
| 11 | + * |
| 12 | + * Three shapes are in circulation and all are legitimate — a spec |
| 13 | + * `ViewFilterRule[]`, an AST node, and a MongoDB-style object. Renderers merged |
| 14 | + * them by hand and got two things wrong, both silent: |
| 15 | + * |
| 16 | + * 1. They tested `source.length > 0` before using it. That is `undefined > 0` |
| 17 | + * for an object, so a `table.defaultFilters` (declared `Record<string, |
| 18 | + * any>`) was DROPPED and the view returned every record. |
| 19 | + * 2. They SPREAD a source into the `and` (`['and', ...rules]`). That is only |
| 20 | + * correct when the source is an array of nodes; for a `ViewFilterRule[]` |
| 21 | + * it puts bare rule objects where the AST expects nodes. `isFilterAST` |
| 22 | + * rejects that (a 400 since objectstack#4121) and `parseFilterAST` reads |
| 23 | + * the rule as a Mongo condition — filtering on columns literally named |
| 24 | + * `field`, `operator` and `value`. |
| 25 | + * |
| 26 | + * The emitted shape is asserted against the server's own `isFilterAST` rather |
| 27 | + * than a restated literal wherever the result is an AST. |
| 28 | + */ |
| 29 | + |
| 30 | +import { describe, it, expect } from 'vitest'; |
| 31 | +import { isFilterAST, parseFilterAST } from '@objectstack/spec/data'; |
| 32 | +import { toFilterNode, mergeFilterNodes } from '../filter-converter'; |
| 33 | + |
| 34 | +const RULES = [{ field: 'stage', operator: 'eq', value: 'won' }]; |
| 35 | +const TUPLE = ['owner', '=', 'me']; |
| 36 | + |
| 37 | +describe('toFilterNode', () => { |
| 38 | + it('passes a non-empty array source through unchanged', () => { |
| 39 | + expect(toFilterNode(RULES)).toEqual(RULES); |
| 40 | + expect(toFilterNode([TUPLE])).toEqual([TUPLE]); |
| 41 | + }); |
| 42 | + |
| 43 | + it('converts a MongoDB-style object into an AST node', () => { |
| 44 | + expect(toFilterNode({ status: 'active' })).toEqual(['status', '=', 'active']); |
| 45 | + }); |
| 46 | + |
| 47 | + it('treats absent and empty sources as nothing', () => { |
| 48 | + for (const empty of [undefined, null, [], {}, '', 0]) { |
| 49 | + expect(toFilterNode(empty)).toBeUndefined(); |
| 50 | + } |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('mergeFilterNodes', () => { |
| 55 | + it('returns undefined when every source is empty', () => { |
| 56 | + expect(mergeFilterNodes(undefined, [], {})).toBeUndefined(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('returns a lone source as-is rather than wrapping it in a pointless and', () => { |
| 60 | + expect(mergeFilterNodes([TUPLE], undefined)).toEqual([TUPLE]); |
| 61 | + }); |
| 62 | + |
| 63 | + it('wraps each source as its own child — never spreads it', () => { |
| 64 | + // The regression. Spreading would give ['and', {field…}, 'owner', '=', 'me']. |
| 65 | + expect(mergeFilterNodes(RULES, TUPLE)).toEqual(['and', RULES, TUPLE]); |
| 66 | + }); |
| 67 | + |
| 68 | + it('keeps an object source instead of dropping it', () => { |
| 69 | + // `table.defaultFilters` is declared `Record<string, any>`; the old |
| 70 | + // `.length > 0` guard read false and the whole filter disappeared. |
| 71 | + expect(mergeFilterNodes({ status: 'active' }, TUPLE)) |
| 72 | + .toEqual(['and', ['status', '=', 'active'], TUPLE]); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +describe('what reaches the server', () => { |
| 77 | + /** |
| 78 | + * `ViewFilterRule[]` is not itself AST — the adapter translates it on the way |
| 79 | + * out — so `isFilterAST` is only the right oracle for the all-AST cases. |
| 80 | + */ |
| 81 | + it('produces a node the server accepts when every source is AST', () => { |
| 82 | + expect(isFilterAST(mergeFilterNodes([TUPLE], ['amount', '>', 1]))).toBe(true); |
| 83 | + expect(isFilterAST(mergeFilterNodes({ status: 'active' }, ['amount', '>', 1]))).toBe(true); |
| 84 | + expect(isFilterAST(mergeFilterNodes({ status: 'active' }))).toBe(true); |
| 85 | + }); |
| 86 | + |
| 87 | + it('an object source survives the round trip to a real predicate', () => { |
| 88 | + const merged = mergeFilterNodes({ status: 'active' }, ['amount', '>', 1]); |
| 89 | + expect(parseFilterAST(merged)).toEqual({ |
| 90 | + $and: [{ status: 'active' }, { amount: { $gt: 1 } }], |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('the spread shape it replaces was NOT acceptable — pinning why', () => { |
| 95 | + // What `['and', ...rules, ...tuples]` produced. Kept as executable evidence |
| 96 | + // that the wrapping above is not a stylistic preference. |
| 97 | + const spread = ['and', ...RULES, TUPLE]; |
| 98 | + expect(isFilterAST(spread)).toBe(false); |
| 99 | + // Worse than rejected: read as a predicate over three columns that do not exist. |
| 100 | + expect(parseFilterAST(spread)).toEqual({ |
| 101 | + $and: [{ field: 'stage', operator: 'eq', value: 'won' }, { owner: 'me' }], |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments