Skip to content

Commit 6bc4007

Browse files
committed
wip(core): pins for flags and object comparands
Claude-Session: https://claude.ai/code/session_01L9qibvVRcFWKrfWeG5jzPb Co-authored-by: Claude <noreply@anthropic.com>
1 parent 397de17 commit 6bc4007

4 files changed

Lines changed: 400 additions & 7 deletions

File tree

‎ui/packages/core/src/adapters/__tests__/ValueDataSource.astFilterVocabulary.test.ts‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
*/
4949

5050
import { describe, it, expect, vi, afterEach } from 'vitest';
51+
import { runInNewContext } from 'node:vm';
5152
import { VIEW_FILTER_OPERATORS } from '@objectstack/spec/ui';
5253
import {
5354
canonicalAstOperator,
@@ -619,3 +620,84 @@ describe('the nested-relation node is refused by name in both dialects', () => {
619620
expect(warn).not.toHaveBeenCalled();
620621
});
621622
});
623+
624+
/**
625+
* The AST arm's null-ness operators carry no flag: direction comes from the
626+
* operator NAME, and the value slot is never read. That is the spec's own
627+
* reading, so this face needs no flag door of its own: `parseFilterAST`
628+
* hands `['f', 'is_null', <anything>]` back as `{ f: { $null: true } }`. The
629+
* flag door sits where a flag exists, on the `$` arm and in the converter,
630+
* which refuses a non-boolean before it can lower one.
631+
*/
632+
describe('the AST null-ness operators read no flag, as the spec reads them', () => {
633+
const ROWS = [
634+
{ id: 'has', ts: 't' },
635+
{ id: 'null', ts: null },
636+
{ id: 'missing' },
637+
];
638+
639+
it.each([['no'], [{ owner: 'u1' }], [false], [0]])(
640+
'`is_null` with %j in the value slot is the spec’s `$null: true`, with nothing logged',
641+
async (slot) => {
642+
expect(parseFilterAST(['ts', 'is_null', slot] as never)).toEqual({ ts: { $null: true } });
643+
expect(parseFilterAST(['ts', 'is_not_null', slot] as never)).toEqual({ ts: { $null: false } });
644+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
645+
expect(await selectedIds(['ts', 'is_null', slot], ROWS)).toEqual(['null', 'missing']);
646+
expect(await selectedIds(['ts', 'is_not_null', slot], ROWS)).toEqual(['has']);
647+
expect(warn).not.toHaveBeenCalled();
648+
},
649+
);
650+
});
651+
652+
/**
653+
* The shared comparand builder on the AST arm: a `{ $now, addDays }` anchor, a
654+
* `$`-keyed object that is no `{ $field }` reference and a plain object of
655+
* another realm are refused under every operator that reads the slot, and a
656+
* class instance under an equality, membership or text operator.
657+
*/
658+
describe('object comparands the AST arm cannot compare are refused by name', () => {
659+
class Amount {
660+
readonly units: number;
661+
constructor(units: number) {
662+
this.units = units;
663+
}
664+
valueOf(): number {
665+
return this.units;
666+
}
667+
}
668+
669+
it.each([
670+
['<', { $now: true, addDays: 7 }, 'is a { $now, addDays } date anchor'],
671+
['=', { $now: true, addDays: 0 }, 'is a { $now, addDays } date anchor'],
672+
['!=', { $foo: 1 }, "is an object carrying the key '$foo'"],
673+
['>', runInNewContext('({ id: "u1" })'), 'is a plain object from another realm'],
674+
['!=', runInNewContext('({ id: "u1" })'), 'is a plain object from another realm'],
675+
['=', new Amount(25), 'is a Amount instance'],
676+
['!=', new Amount(25), 'is a Amount instance'],
677+
['not_contains', new Amount(25), 'is a Amount instance'],
678+
['starts_with', new Amount(25), 'is a Amount instance'],
679+
] as const)('`%s` refuses %j', async (operator, comparand, words) => {
680+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
681+
expect(await selectedIds(['age', operator, comparand])).toEqual([]);
682+
expect(warn).toHaveBeenCalledTimes(1);
683+
expect(String(warn.mock.calls[0]?.[0])).toContain(`filter comparand on field 'age' ${words}`);
684+
});
685+
686+
it.each([
687+
['in', [new Amount(25), 30]],
688+
['nin', [new Amount(25)]],
689+
['in', [{ $now: true, addDays: 1 }]],
690+
['between', [runInNewContext('({})'), 30]],
691+
] as const)('`%s` refuses such an object as a list member', async (operator, list) => {
692+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
693+
expect(await selectedIds(['age', operator, list])).toEqual([]);
694+
expect(warn).toHaveBeenCalledTimes(1);
695+
expect(String(warn.mock.calls[0]?.[0])).toContain("filter comparand on field 'age' is ");
696+
});
697+
698+
it('CONTROL: a class instance under `between` compares through valueOf, with nothing logged', async () => {
699+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
700+
expect(await selectedIds(['age', 'between', [new Amount(22), new Amount(28)]])).toEqual(['b']);
701+
expect(warn).not.toHaveBeenCalled();
702+
});
703+
});

‎ui/packages/core/src/adapters/__tests__/ValueDataSource.dollarFilterVocabulary.test.ts‎

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
*/
7474

7575
import { describe, it, expect, vi, afterEach } from 'vitest';
76+
import { runInNewContext } from 'node:vm';
7677
import { FILTER_OPERATORS } from '@objectstack/spec/data';
7778
import { ValueDataSource } from '../ValueDataSource';
7879
import { toFilterNodeSafely } from '../../utils/filter-converter';
@@ -776,3 +777,233 @@ describe('a plain-object comparand under a `$` operator is refused as the AST ar
776777
expect(lines[0]).toContain("carries the non-operator key 'profile'");
777778
});
778779
});
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

Comments
 (0)