// // Copyright 2025 DXOS.org // import { describe, expect, test } from 'vitest'; import { Filter, Ref, Type } from '@dxos/echo'; import { EntityStructure } from '@dxos/echo-protocol'; import { TestSchema } from '@dxos/echo/testing'; import { DXN, EID, EntityId, SpaceId } from '@dxos/keys'; import { type MatchedDoc, filterMatchDoc } from './filter-match.ts'; describe('filterMatch', () => { test('everything', () => { expect(filterMatchDoc(Filter.everything().ast, OBJECT_1)).to.be.true; expect(filterMatchDoc(Filter.everything().ast, OBJECT_2)).to.be.true; }); test('nothing', () => { expect(filterMatchDoc(Filter.nothing().ast, OBJECT_1)).to.be.false; expect(filterMatchDoc(Filter.nothing().ast, OBJECT_2)).to.be.false; }); test('properties', () => { expect(filterMatchDoc(Filter.type(TestSchema.Expando, { title: 'test' }).ast, OBJECT_1)).to.be.true; expect(filterMatchDoc(Filter.type(TestSchema.Expando, { value: 100 }).ast, OBJECT_1)).to.be.true; expect(filterMatchDoc(Filter.type(TestSchema.Expando, { complete: false }).ast, OBJECT_1)).to.be.false; expect(filterMatchDoc(Filter.type(TestSchema.Expando, { missing: undefined }).ast, OBJECT_1)).to.be.true; expect(filterMatchDoc(Filter.type(TestSchema.Expando, { properties: { subject: 'test' } }).ast, OBJECT_1)).to.be .true; expect(filterMatchDoc(Filter.type(TestSchema.Expando, { array: Filter.contains('two') }).ast, OBJECT_1)).to.be.true; }); test('and', () => { const filter1 = Filter.type(TestSchema.Expando, { title: 'test' }); const filter2 = Filter.type(TestSchema.Expando, { value: 100 }); const filter3 = Filter.type(TestSchema.Expando, { complete: true }); expect(filterMatchDoc(Filter.and(filter1, filter2, filter3).ast, OBJECT_1)).to.be.true; }); test('or', () => { const filter1 = Filter.type(TestSchema.Expando, { value: 200 }); const filter2 = Filter.type(TestSchema.Expando, { title: 'test' }); const filter3 = Filter.type(TestSchema.Expando, { complete: false }); expect(filterMatchDoc(Filter.or(filter1, filter2, filter3).ast, OBJECT_1)).to.be.true; }); test('not', () => { const filter1 = Filter.type(TestSchema.Expando, { title: 'test' }); const filter2 = Filter.not(filter1); expect(filterMatchDoc(filter1.ast, OBJECT_1)).to.be.true; expect(filterMatchDoc(filter2.ast, OBJECT_1)).to.be.false; }); test('contains', () => { expect( filterMatchDoc(Filter.type(TestSchema.Expando, { properties: { label: Filter.contains('test') } }).ast, OBJECT_1), ).to.be.true; expect( filterMatchDoc( Filter.type(TestSchema.Expando, { fields: Filter.contains({ label: 'label', value: 'test' }) }).ast, OBJECT_1, ), ).to.be.true; }); test('complex', () => { const filter1 = Filter.type(TestSchema.Expando, { title: 'bad' }); const filter2 = Filter.type(TestSchema.Expando, { value: 0 }); expect(filterMatchDoc(Filter.not(Filter.or(filter1, filter2)).ast, OBJECT_1)).to.be.true; }); test('ids', () => { const filter = Filter.id(OBJECT_1.id); expect(filterMatchDoc(filter.ast, OBJECT_1)).to.be.true; expect(filterMatchDoc(filter.ast, OBJECT_2)).to.be.false; }); test('refs', () => { const filter = Filter.type(TestSchema.Expando, { parent: Ref.fromURI(EID.make({ entityId: OBJECT_1.id })) }); expect(filterMatchDoc(filter.ast, OBJECT_1)).to.be.false; expect(filterMatchDoc(filter.ast, OBJECT_2)).to.be.false; expect(filterMatchDoc(filter.ast, OBJECT_3)).to.be.true; }); test('standalone timestamp filter throws invariant (must be handled at index level)', () => { expect(() => filterMatchDoc(Filter.updated({ after: Date.now() - 1000 }).ast, OBJECT_1)).to.throw(); expect(() => filterMatchDoc(Filter.created({ before: Date.now() + 1000 }).ast, OBJECT_1)).to.throw(); }); test('and(type, timestamp) throws on timestamp part', () => { const filter = Filter.and(Filter.type(TestSchema.Expando), Filter.updated({ after: Date.now() - 1000 })); expect(() => filterMatchDoc(filter.ast, OBJECT_1)).to.throw(); }); }); const OBJECT_1: MatchedDoc = { id: EntityId.make('01JVS9YYT5VMVJW0GGTM1YHCCH'), spaceId: SpaceId.make('B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO'), doc: EntityStructure.makeObject({ type: DXN.make(Type.getTypename(TestSchema.Expando), '0.1.0'), data: { title: 'test', value: 100, complete: true, array: ['one', 'two', 'three'], properties: { label: ['test'], subject: 'test' }, fields: [{ label: 'label', value: 'test' }], }, }), }; const OBJECT_2: MatchedDoc = { id: EntityId.make('01JT5TD6K9FFJ3VNM5FGMS5C0Q'), spaceId: SpaceId.make('B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO'), doc: EntityStructure.makeObject({ type: DXN.make(Type.getTypename(TestSchema.Expando), '0.1.0'), data: { title: 'test', value: 100, complete: true }, deleted: true, }), }; const OBJECT_3: MatchedDoc = { id: EntityId.make('01JT5TD6K9FFJ3VNM5FGMS5C0Q'), spaceId: SpaceId.make('B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO'), doc: EntityStructure.makeObject({ type: DXN.make(Type.getTypename(TestSchema.Expando), '0.1.0'), data: { title: 'test', value: 100, complete: true, parent: { '/': EID.make({ entityId: OBJECT_1.id }) } }, }), };