|
73 | 73 | */ |
74 | 74 |
|
75 | 75 | import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 76 | +import { runInNewContext } from 'node:vm'; |
76 | 77 | import { FILTER_OPERATORS } from '@objectstack/spec/data'; |
77 | 78 | import { ValueDataSource } from '../ValueDataSource'; |
78 | 79 | import { toFilterNodeSafely } from '../../utils/filter-converter'; |
@@ -776,3 +777,233 @@ describe('a plain-object comparand under a `$` operator is refused as the AST ar |
776 | 777 | expect(lines[0]).toContain("carries the non-operator key 'profile'"); |
777 | 778 | }); |
778 | 779 | }); |
| 780 | + |
| 781 | +/** |
| 782 | + * `$null` / `$exists` take a BOOLEAN flag on this face too, as `$empty` does, |
| 783 | + * and the converter refuses the same flags before lowering, so the object form |
| 784 | + * and its lowered form cannot answer apart: neither one serves rows. |
| 785 | + * `nickname` is present on `a`, null on `b`, absent on `c`. |
| 786 | + */ |
| 787 | +describe('a `$null` / `$exists` flag that is not a boolean is refused on the `$` arm', () => { |
| 788 | + const NON_BOOLEAN_FLAGS: unknown[] = ['no', 'false', 1, 0, null, { owner: 'u1' }, {}]; |
| 789 | + |
| 790 | + async function refusalOf(filter: unknown): Promise<{ ids: string[]; lines: string[] }> { |
| 791 | + const warn = spyWarn(); |
| 792 | + const ids = await selectedIds(filter); |
| 793 | + const lines = warn.mock.calls.map((call) => String(call[0])); |
| 794 | + warn.mockRestore(); |
| 795 | + return { ids, lines }; |
| 796 | + } |
| 797 | + |
| 798 | + describe.each(['$null', '$exists'] as const)('%s', (operator) => { |
| 799 | + it.each(NON_BOOLEAN_FLAGS.map((flag) => [JSON.stringify(flag), flag]))( |
| 800 | + 'the flag %s excludes every row and logs the boolean-flag sentence once', |
| 801 | + async (_label, flag) => { |
| 802 | + const { ids, lines } = await refusalOf({ nickname: { [operator]: flag } }); |
| 803 | + expect(ids).toEqual([]); |
| 804 | + expect(lines).toHaveLength(1); |
| 805 | + expect(lines[0]).toContain( |
| 806 | + `filter operator '${operator}' on field 'nickname' takes a boolean flag (true or false)`, |
| 807 | + ); |
| 808 | + }, |
| 809 | + ); |
| 810 | + |
| 811 | + it('the converter refuses the same filter, so there is no lowered form to split from', () => { |
| 812 | + const lowered = toFilterNodeSafely({ nickname: { [operator]: 'no' } }); |
| 813 | + expect(lowered.ok).toBe(false); |
| 814 | + if (!lowered.ok) { |
| 815 | + expect(lowered.refusal).toMatchObject({ code: 'INVALID_FILTER', operator, field: 'nickname' }); |
| 816 | + } |
| 817 | + }); |
| 818 | + }); |
| 819 | + |
| 820 | + it.each([ |
| 821 | + [{ nickname: { $null: true } }, ['b', 'c']], |
| 822 | + [{ nickname: { $null: false } }, ['a']], |
| 823 | + [{ nickname: { $exists: true } }, ['a']], |
| 824 | + [{ nickname: { $exists: false } }, ['b', 'c']], |
| 825 | + ] as const)( |
| 826 | + 'CONTROL: %j answers as before, in the object form and in its lowered form, with nothing logged', |
| 827 | + async (filter, expected) => { |
| 828 | + const lowered = toFilterNodeSafely(filter); |
| 829 | + if (!lowered.ok) throw new Error(`the lowering refused ${JSON.stringify(filter)}`); |
| 830 | + const warn = spyWarn(); |
| 831 | + expect(await selectedIds(filter)).toEqual(expected); |
| 832 | + expect(await selectedIds(lowered.node)).toEqual(expected); |
| 833 | + expect(warn).not.toHaveBeenCalled(); |
| 834 | + }, |
| 835 | + ); |
| 836 | +}); |
| 837 | + |
| 838 | +/** |
| 839 | + * Three more object comparands this matcher cannot compare, refused on both |
| 840 | + * arms by the shared builder: a `$`-keyed plain object that is no `{ $field }` |
| 841 | + * reference (the `{ $now, addDays }` date anchor among them), a plain object |
| 842 | + * of another realm, and a class instance under an equality, membership or text |
| 843 | + * operator. The spec's comparand door refuses the last two at every operator; |
| 844 | + * it steps around a well-formed anchor, which the platform resolves in the |
| 845 | + * compared column's temporal form, and this face, which has no field types, |
| 846 | + * refuses it in words instead of comparing it as `[object Object]`. |
| 847 | + */ |
| 848 | +describe('a `$`-keyed object, a foreign-realm object and a class instance in comparand position', () => { |
| 849 | + const ROWS_2 = [ |
| 850 | + { id: 'a', owner: 'zeta', age: 30, due: '2099-01-01', floor: 25 }, |
| 851 | + { id: 'b', owner: 'Alpha', age: 25, due: '2000-01-01', floor: 25 }, |
| 852 | + { id: 'c', owner: null, age: 20, due: null, floor: 10 }, |
| 853 | + ]; |
| 854 | + const FLAG_OPERATORS = new Set(['$null', '$exists', '$empty']); |
| 855 | + const LIST_OPERATORS = new Set(['$in', '$nin', '$between']); |
| 856 | + const COMPARAND_OPERATORS = FILTER_OPERATORS.filter((op) => !FLAG_OPERATORS.has(op)); |
| 857 | + |
| 858 | + /** |
| 859 | + * How each comparand operator compares: by ORDER (a class instance is |
| 860 | + * compared through its `valueOf`), by VALUE (equality, membership, text: a |
| 861 | + * class instance is refused), or by its own text door (`$icontains`, which |
| 862 | + * refuses every non-string comparand already). |
| 863 | + */ |
| 864 | + const COMPARISON_KIND: Record<string, 'order' | 'value' | 'text-door'> = { |
| 865 | + $eq: 'value', $ne: 'value', $in: 'value', $nin: 'value', |
| 866 | + $contains: 'value', $notContains: 'value', $startsWith: 'value', $endsWith: 'value', |
| 867 | + $gt: 'order', $gte: 'order', $lt: 'order', $lte: 'order', $between: 'order', |
| 868 | + $icontains: 'text-door', |
| 869 | + }; |
| 870 | + |
| 871 | + async function refusalOf(filter: unknown): Promise<{ ids: string[]; lines: string[] }> { |
| 872 | + const warn = spyWarn(); |
| 873 | + const ids = await selectedIds(filter, ROWS_2); |
| 874 | + const lines = warn.mock.calls.map((call) => String(call[0])); |
| 875 | + warn.mockRestore(); |
| 876 | + return { ids, lines }; |
| 877 | + } |
| 878 | + |
| 879 | + /** The whole comparand, or a list member for a list operator. */ |
| 880 | + const placed = (operator: string, comparand: unknown): unknown => |
| 881 | + LIST_OPERATORS.has(operator) ? [comparand, 'zzz'] : comparand; |
| 882 | + |
| 883 | + it('the comparison-kind table covers the comparand operators exactly', () => { |
| 884 | + expect(Object.keys(COMPARISON_KIND).sort()).toEqual([...COMPARAND_OPERATORS].sort()); |
| 885 | + }); |
| 886 | + |
| 887 | + const ANCHOR = { $now: true, addDays: 7 }; |
| 888 | + const ANCHOR_REFUSAL = |
| 889 | + "filter comparand on field 'due' is a { $now, addDays } date anchor, which the in-memory " |
| 890 | + + 'matcher does not resolve'; |
| 891 | + |
| 892 | + it.each(COMPARAND_OPERATORS)( |
| 893 | + '`%s` refuses a `{ $now, addDays }` anchor by name, whole or as a list member', |
| 894 | + async (operator) => { |
| 895 | + const { ids, lines } = await refusalOf({ due: { [operator]: placed(operator, ANCHOR) } }); |
| 896 | + expect(ids).toEqual([]); |
| 897 | + expect(lines).toHaveLength(1); |
| 898 | + expect(lines[0]).toContain(ANCHOR_REFUSAL); |
| 899 | + }, |
| 900 | + ); |
| 901 | + |
| 902 | + it('the resolved anchor form (`$now` holding an instant) is refused in the same words', async () => { |
| 903 | + const { ids, lines } = await refusalOf({ due: { $lt: { $now: '2026-01-01T00:00:00Z', addDays: 7 } } }); |
| 904 | + expect(ids).toEqual([]); |
| 905 | + expect(lines[0]).toContain(ANCHOR_REFUSAL); |
| 906 | + }); |
| 907 | + |
| 908 | + it.each(COMPARAND_OPERATORS)( |
| 909 | + '`%s` refuses another `$`-keyed object that is no `{ $field }` reference', |
| 910 | + async (operator) => { |
| 911 | + const { ids, lines } = await refusalOf({ owner: { [operator]: placed(operator, { $foo: 1 }) } }); |
| 912 | + expect(ids).toEqual([]); |
| 913 | + expect(lines).toHaveLength(1); |
| 914 | + expect(lines[0]).toContain( |
| 915 | + "filter comparand on field 'owner' is an object carrying the key '$foo', which is neither " |
| 916 | + + 'a comparison value nor a { $field } reference', |
| 917 | + ); |
| 918 | + }, |
| 919 | + ); |
| 920 | + |
| 921 | + it.each(COMPARAND_OPERATORS)( |
| 922 | + '`%s` refuses a plain object of another realm, whole or as a list member', |
| 923 | + async (operator) => { |
| 924 | + const foreign = runInNewContext('({ id: "u1" })'); |
| 925 | + const { ids, lines } = await refusalOf({ owner: { [operator]: placed(operator, foreign) } }); |
| 926 | + expect(ids).toEqual([]); |
| 927 | + expect(lines).toHaveLength(1); |
| 928 | + expect(lines[0]).toContain("filter comparand on field 'owner' is a plain object from another realm"); |
| 929 | + }, |
| 930 | + ); |
| 931 | + |
| 932 | + it('a plain object of another realm as the WHOLE condition is refused in the converter’s words', async () => { |
| 933 | + const { ids, lines } = await refusalOf({ owner: runInNewContext('({ id: "u1" })') }); |
| 934 | + expect(ids).toEqual([]); |
| 935 | + expect(lines).toHaveLength(1); |
| 936 | + expect(lines[0]).toContain("The filter on field 'owner' carries a plain object from another realm"); |
| 937 | + }); |
| 938 | + |
| 939 | + /** A class instance: data, compared through its `valueOf` under an ordering. */ |
| 940 | + class Amount { |
| 941 | + readonly units: number; |
| 942 | + constructor(units: number) { |
| 943 | + this.units = units; |
| 944 | + } |
| 945 | + valueOf(): number { |
| 946 | + return this.units; |
| 947 | + } |
| 948 | + toString(): string { |
| 949 | + return `Amount(${this.units})`; |
| 950 | + } |
| 951 | + } |
| 952 | + const CLASS_REFUSAL = "filter comparand on field 'age' is a Amount instance, which the in-memory matcher compares only by order"; |
| 953 | + |
| 954 | + it.each(COMPARAND_OPERATORS.filter((op) => COMPARISON_KIND[op] === 'value'))( |
| 955 | + '`%s` refuses a class instance, whole or as a list member, by name', |
| 956 | + async (operator) => { |
| 957 | + const { ids, lines } = await refusalOf({ age: { [operator]: placed(operator, new Amount(25)) } }); |
| 958 | + expect(ids).toEqual([]); |
| 959 | + expect(lines).toHaveLength(1); |
| 960 | + expect(lines[0]).toContain(CLASS_REFUSAL); |
| 961 | + }, |
| 962 | + ); |
| 963 | + |
| 964 | + it('`$icontains` keeps its own text door for a class instance', async () => { |
| 965 | + const { ids, lines } = await refusalOf({ age: { $icontains: new Amount(25) } }); |
| 966 | + expect(ids).toEqual([]); |
| 967 | + expect(lines).toHaveLength(1); |
| 968 | + expect(lines[0]).not.toContain(CLASS_REFUSAL); |
| 969 | + expect(lines[0]).toContain('$icontains'); |
| 970 | + }); |
| 971 | + |
| 972 | + it('CONTROL: under `$between` a class instance endpoint compares through valueOf, with nothing logged', async () => { |
| 973 | + const warn = spyWarn(); |
| 974 | + expect(await selectedIds({ age: { $between: [new Amount(22), new Amount(28)] } }, ROWS_2)).toEqual(['b']); |
| 975 | + expect(warn).not.toHaveBeenCalled(); |
| 976 | + }); |
| 977 | + |
| 978 | + it('CONTROL: a scalar, a `Date` and a `{ $field }` reference are compared, with nothing logged', async () => { |
| 979 | + const warn = spyWarn(); |
| 980 | + expect(await selectedIds({ owner: { $ne: 'zeta' } }, ROWS_2)).toEqual(['b', 'c']); |
| 981 | + expect(await selectedIds({ due: { $lt: '2050-01-01' } }, ROWS_2)).toEqual(['b']); |
| 982 | + expect(await selectedIds({ age: { $eq: { $field: 'floor' } } }, ROWS_2)).toEqual(['b']); |
| 983 | + const dated = [{ id: 'x', at: new Date('2026-03-01T00:00:00Z') }, { id: 'y', at: new Date('2026-01-01T00:00:00Z') }]; |
| 984 | + expect(await selectedIds({ at: { $eq: new Date('2026-03-01T00:00:00Z') } }, dated)).toEqual(['x']); |
| 985 | + expect(await selectedIds({ at: { $nin: [new Date('2026-03-01T00:00:00Z')] } }, dated)).toEqual(['y']); |
| 986 | + expect(warn).not.toHaveBeenCalled(); |
| 987 | + }); |
| 988 | + |
| 989 | + it.each([ |
| 990 | + ['$lt anchor', { due: { $lt: ANCHOR } }], |
| 991 | + ['$in anchor member', { due: { $in: [ANCHOR] } }], |
| 992 | + ['$ne $-keyed', { owner: { $ne: { $foo: 1 } } }], |
| 993 | + ['$eq class instance', { age: { $eq: new Amount(25) } }], |
| 994 | + ['$nin class member', { age: { $nin: [new Amount(25)] } }], |
| 995 | + ['$notContains class instance', { age: { $notContains: new Amount(25) } }], |
| 996 | + ] as const)( |
| 997 | + '%s: the object form and its lowered AST form log one and the same sentence', |
| 998 | + async (_label, filter) => { |
| 999 | + const lowered = toFilterNodeSafely(filter); |
| 1000 | + if (!lowered.ok) throw new Error(`the lowering refused ${JSON.stringify(filter)}`); |
| 1001 | + const objectForm = await refusalOf(filter); |
| 1002 | + const astForm = await refusalOf(lowered.node); |
| 1003 | + expect(objectForm.ids).toEqual([]); |
| 1004 | + expect(astForm.ids).toEqual([]); |
| 1005 | + expect(objectForm.lines).toHaveLength(1); |
| 1006 | + expect(astForm.lines).toEqual(objectForm.lines); |
| 1007 | + }, |
| 1008 | + ); |
| 1009 | +}); |
0 commit comments