// // Copyright 2026 DXOS.org // import * as Effect from 'effect/Effect'; import * as SqlClient from 'effect/sql/SqlClient'; import { beforeAll, describe, test } from 'vitest'; import { Aggregate, Filter, Query } from '@dxos/echo'; import { type QueryAST } from '@dxos/echo-protocol'; import { TestSchema } from '@dxos/echo/testing'; import * as RuntimeProvider from '@dxos/effect/RuntimeProvider'; import { IndexEngine } from '@dxos/index-core'; import { invariant } from '@dxos/invariant'; import { DXN, EID, EntityId, SpaceId } from '@dxos/keys'; import { QueryReactivity } from '@dxos/protocols/buf/dxos/echo/query_pb'; import { AutomergeHost } from '../automerge/index.ts'; import { QueryExecutor } from '../query/query-executor.ts'; import { createTestSqliteRuntime } from '../testing/index.ts'; import { type InvalidationHint, canonicalTypename, hintFromIndexingResult, mergeHints } from './invalidation-hint.ts'; import { SpaceStateManager } from './space-state-manager.ts'; // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- const makeHint = (partial: Partial): InvalidationHint => partial; const makeSpaceSet = (...ids: SpaceId[]) => new Set(ids); const makeTypeSet = (...types: string[]) => new Set(types); const makeObjectSet = (...ids: EntityId[]) => new Set(ids); const SPACE_ID = SpaceId.make('B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO'); // Versioned type DXNs as recorded on stored objects / raw IndexingResults. const PERSON_DXN = 'dxn:com.example.type.person:0.1.0'; const ORG_DXN = 'dxn:com.example.type.organization:0.1.0'; // Canonical (version-less) typenames as carried by an InvalidationHint and a query scope. const PERSON_TYPENAME = 'com.example.type.person'; const ORG_TYPENAME = 'com.example.type.organization'; // Stable queue EID mirroring query-planner.test.ts. const QUEUE_SPACE_ID = SpaceId.make('B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO'); const QUEUE_ID = EntityId.make('01JJRA86VK4H1TEB6QQVSWXP0E'); const QUEUE_DXN = EID.make({ spaceId: QUEUE_SPACE_ID, entityId: QUEUE_ID }); const withSpace = (q: Query.Any): Query.Any => q.from([{ _tag: 'space' as const, spaceId: SPACE_ID }]); /** Never run, so a `never`-typed placeholder satisfies every dependency's `RuntimeProvider`. */ const testRuntime = Effect.never; /** * Real but never-opened QueryExecutor dependencies, shared across the fixtures below. The index * engine holds a client, and resolving one is asynchronous, so the fixture is built in `beforeAll` * — no query in this file reaches the engine. */ let testDeps: { indexEngine: IndexEngine; runtime: typeof testRuntime; automergeHost: AutomergeHost; spaceStateManager: SpaceStateManager; sql: SqlClient.SqlClient; }; beforeAll(async () => { const { runtime, dispose } = createTestSqliteRuntime(); const sql = await RuntimeProvider.runPromise(runtime)(SqlClient.SqlClient); testDeps = { indexEngine: new IndexEngine(sql), runtime: testRuntime, automergeHost: new AutomergeHost({ runtime: testRuntime }), spaceStateManager: new SpaceStateManager({ runtime: testRuntime }), sql, }; return () => dispose(); }); /** Creates a QueryExecutor whose plan and cached scopes come only from `query`, via extractScopes(). */ const makeExecutor = (query: { ast: QueryAST.Query }): QueryExecutor => new QueryExecutor({ ...testDeps, queryId: 'test', query: query.ast, reactivity: QueryReactivity.REACTIVE, }); // --------------------------------------------------------------------------- // hintFromIndexingResult // --------------------------------------------------------------------------- describe('hintFromIndexingResult', () => { test('returns undefined when nothing was indexed', ({ expect }) => { const result = hintFromIndexingResult({ updated: 0, done: true, drained: true, spaces: new Set(), queues: new Set(), documents: new Set(), types: new Set(), objects: new Set(), }); expect(result).toBeUndefined(); }); test('converts populated result to hint with non-empty dimensions', ({ expect }) => { const spaceId = SpaceId.random(); const objectId = EntityId.random(); const result = hintFromIndexingResult({ updated: 1, done: true, drained: true, spaces: new Set([spaceId]), queues: new Set(), documents: new Set(['doc-1']), types: new Set([PERSON_DXN]), objects: new Set([objectId]), }); invariant(result); expect(result.spaceIds?.has(spaceId)).toBe(true); // Versioned object type is canonicalized to the bare typename. expect(result.typenames?.has(PERSON_TYPENAME)).toBe(true); expect(result.typenames?.has(PERSON_DXN)).toBe(false); expect(result.objectIds?.has(objectId)).toBe(true); // Empty queues → undefined (no queue constraint) expect(result.queueIds).toBeUndefined(); }); // Regression for DX-966: stored object types arrive versioned; the hint must reduce them to the // bare typename so it can match a version-less type filter scope. test('canonicalizes versioned object types', ({ expect }) => { const result = hintFromIndexingResult({ updated: 2, done: true, drained: true, spaces: new Set([SpaceId.random()]), queues: new Set(), documents: new Set(), types: new Set([PERSON_DXN, ORG_DXN]), objects: new Set(), }); invariant(result); expect(result.typenames).toEqual(new Set([PERSON_TYPENAME, ORG_TYPENAME])); }); }); describe('canonicalTypename', () => { test('strips schema version', ({ expect }) => { expect(canonicalTypename(PERSON_DXN)).toBe(PERSON_TYPENAME); expect(canonicalTypename(`dxn:${PERSON_TYPENAME}`)).toBe(PERSON_TYPENAME); }); test('passes through schema-as-object (EchoURI) and other non-type URIs unchanged', ({ expect }) => { // Dynamic schemas reference the schema object by EchoURI rather than a typename DXN. const echoUri = EID.make({ spaceId: QUEUE_SPACE_ID, entityId: QUEUE_ID }); expect(canonicalTypename(echoUri)).toBe(echoUri); // Arbitrary non-DXN strings are returned verbatim. expect(canonicalTypename('plain-string')).toBe('plain-string'); }); }); // --------------------------------------------------------------------------- // mergeHints — per-tick merger // --------------------------------------------------------------------------- describe('mergeHints', () => { test('unions constrained dimensions from both hints', ({ expect }) => { const s1 = SpaceId.random(); const s2 = SpaceId.random(); const merged = mergeHints( makeHint({ spaceIds: makeSpaceSet(s1), typenames: makeTypeSet('TypeA') }), makeHint({ spaceIds: makeSpaceSet(s2), typenames: makeTypeSet('TypeB') }), ); expect(merged.spaceIds?.has(s1)).toBe(true); expect(merged.spaceIds?.has(s2)).toBe(true); expect(merged.typenames?.has('TypeA')).toBe(true); expect(merged.typenames?.has('TypeB')).toBe(true); }); test('drops a dimension to unconstrained when either contributor is unconstrained', ({ expect }) => { const s = SpaceId.random(); // b has no spaceIds → unconstrained on that dimension const merged = mergeHints( makeHint({ spaceIds: makeSpaceSet(s), typenames: makeTypeSet('TypeA') }), makeHint({ typenames: makeTypeSet('TypeB') }), ); // Space is dropped because b is unconstrained on it. expect(merged.spaceIds).toBeUndefined(); // Types are still unioned. expect(merged.typenames?.has('TypeA')).toBe(true); expect(merged.typenames?.has('TypeB')).toBe(true); }); test('fully unconstrained result when one side has no constraints at all', ({ expect }) => { const merged = mergeHints( makeHint({}), // fully unconstrained makeHint({ spaceIds: makeSpaceSet(SpaceId.random()), typenames: makeTypeSet('TypeA') }), ); expect(merged.spaceIds).toBeUndefined(); expect(merged.typenames).toBeUndefined(); }); }); // --------------------------------------------------------------------------- // QueryExecutor.matchesHint — typed queries // --------------------------------------------------------------------------- describe('QueryExecutor.matchesHint — typed query', () => { test('matches when hint typenames overlaps', ({ expect }) => { const executor = makeExecutor(withSpace(Query.select(Filter.type(TestSchema.Person)))); expect(executor.matchesHint(makeHint({ typenames: makeTypeSet(PERSON_TYPENAME) }))).toBe(true); }); test('does NOT match when hint typenames are disjoint', ({ expect }) => { const executor = makeExecutor(withSpace(Query.select(Filter.type(TestSchema.Person)))); const hint = makeHint({ spaceIds: makeSpaceSet(SPACE_ID), typenames: makeTypeSet(ORG_TYPENAME), // different type }); expect(executor.matchesHint(hint)).toBe(false); }); test('matches when hint has no typenames constraint (unconstrained → always match)', ({ expect }) => { const executor = makeExecutor(withSpace(Query.select(Filter.type(TestSchema.Person)))); expect(executor.matchesHint(makeHint({ spaceIds: makeSpaceSet(SPACE_ID) }))).toBe(true); }); test('does NOT match when hint spaceIds exclude this query space', ({ expect }) => { const executor = makeExecutor(withSpace(Query.select(Filter.type(TestSchema.Person)))); const hint = makeHint({ spaceIds: makeSpaceSet(SpaceId.random()), typenames: makeTypeSet(PERSON_TYPENAME), }); expect(executor.matchesHint(hint)).toBe(false); }); test('Filter.or(typeA, typeB) matches hint with only typeA', ({ expect }) => { const executor = makeExecutor( withSpace(Query.select(Filter.or(Filter.type(TestSchema.Organization), Filter.type(TestSchema.Person)))), ); expect( executor.matchesHint(makeHint({ spaceIds: makeSpaceSet(SPACE_ID), typenames: makeTypeSet(PERSON_TYPENAME) })), ).toBe(true); }); test('Filter.or(typeA, typeB) does NOT match hint with unrelated type', ({ expect }) => { const executor = makeExecutor( withSpace(Query.select(Filter.or(Filter.type(TestSchema.Organization), Filter.type(TestSchema.Person)))), ); expect( executor.matchesHint( makeHint({ spaceIds: makeSpaceSet(SPACE_ID), typenames: makeTypeSet('com.example.unrelated') }), ), ).toBe(false); }); // Regression for DX-966. The Composer navtree lists objects per type via a version-less typename // DXN (`Filter.type(DXN.make(typename))`). It must match the canonical hint produced for a stored // (versioned) object so the reactive query is re-run on insert/delete. Both scope and hint are // reduced to the same canonical typename, so the comparison stays a plain set overlap — see // `canonicalTypename` and `hintFromIndexingResult` for the canonicalization at each boundary. test('version-less typename scope matches the canonical hint typename', ({ expect }) => { const executor = makeExecutor(withSpace(Query.select(Filter.type(DXN.make(PERSON_TYPENAME))))); expect(executor.matchesHint(makeHint({ typenames: makeTypeSet(PERSON_TYPENAME) }))).toBe(true); }); }); // --------------------------------------------------------------------------- // QueryExecutor.matchesHint — aggregate queries // --------------------------------------------------------------------------- // An aggregate clause on top of a simple type selector stays "simple" for hint purposes — like // OrderStep/LimitStep, it reorders/annotates the working set but doesn't change which typenames, // spaces, or object ids the query is scoped to. Any property edit on a matching object still // triggers re-execution (via the typename/space hint match below); the executor's `changed` diff // then decides whether the edit actually moved the object between groups and needs to reach the // client — that end-to-end behavior is covered by the reactive aggregate tests in // `echo-client-e2e/src/query.test.ts`. describe('QueryExecutor.matchesHint — aggregate query', () => { test('a grouped query with a simple type selector still matches on typename/space (isSimple stays true)', ({ expect, }) => { const executor = makeExecutor( withSpace(Query.select(Filter.type(TestSchema.Person)).aggregate({ age: Aggregate.group('age') })), ); expect( executor.matchesHint(makeHint({ spaceIds: makeSpaceSet(SPACE_ID), typenames: makeTypeSet(PERSON_TYPENAME) })), ).toBe(true); }); test('a grouped query does NOT match a disjoint type/space hint', ({ expect }) => { const executor = makeExecutor( withSpace(Query.select(Filter.type(TestSchema.Person)).aggregate({ age: Aggregate.group('age') })), ); const hint = makeHint({ spaceIds: makeSpaceSet(SpaceId.random()), typenames: makeTypeSet(ORG_TYPENAME), }); expect(executor.matchesHint(hint)).toBe(false); }); test('aggregate combined with a traversal still forces conservative (isSimple=false) matching', ({ expect }) => { const executor = makeExecutor( withSpace( Query.select(Filter.id(EntityId.random())) .referencedBy() .aggregate({ age: Aggregate.group('age') }), ), ); const hint = makeHint({ spaceIds: makeSpaceSet(SpaceId.random()), typenames: makeTypeSet(ORG_TYPENAME), }); // Traversal forces isSimple=false regardless of the trailing aggregate, so it always re-runs. expect(executor.matchesHint(hint)).toBe(true); }); }); // --------------------------------------------------------------------------- // QueryExecutor.matchesHint — complex / non-simple queries // --------------------------------------------------------------------------- describe('QueryExecutor.matchesHint — non-simple queries always match', () => { test('Filter.not(Filter.or(typeA, typeB)) query always matches (inverted TypeSelector → isSimple=false)', ({ expect, }) => { const executor = makeExecutor( withSpace( Query.select(Filter.not(Filter.or(Filter.type(TestSchema.Organization), Filter.type(TestSchema.Person)))), ), ); // Even a completely disjoint hint should match for non-simple queries. const hint = makeHint({ spaceIds: makeSpaceSet(SpaceId.random()), typenames: makeTypeSet('dxn:totally.unrelated:0.1.0'), }); expect(executor.matchesHint(hint)).toBe(true); }); test('Union query (Query.all) always matches (UnionStep → isSimple=false)', ({ expect }) => { const executor = new QueryExecutor({ ...testDeps, queryId: 'test', query: Query.all( withSpace(Query.select(Filter.type(TestSchema.Person))), withSpace(Query.select(Filter.type(TestSchema.Organization))), ).ast, reactivity: QueryReactivity.REACTIVE, }); const hint = makeHint({ spaceIds: makeSpaceSet(SpaceId.random()), typenames: makeTypeSet('dxn:unrelated:0.1.0'), }); expect(executor.matchesHint(hint)).toBe(true); }); }); // --------------------------------------------------------------------------- // QueryExecutor.matchesHint — nested Filter.in(projection) (subquery semi-join) // // The subquery's result depends on its own scope/typenames, which may differ entirely from the // parent query's — a hint scoped to the parent's dimensions could miss a change that alters the // subquery's result set. `extractScopes` marks such a query non-simple (like `child-of`, // `text-search`, unions) so it conservatively re-runs on every invalidation, regardless of // whether the parent and subquery target the same or different types/spaces. // --------------------------------------------------------------------------- describe('QueryExecutor.matchesHint — nested Filter.in(projection)', () => { test('a query with a nested subquery semi-join always matches (isSimple=false)', ({ expect }) => { const subquery = Query.select(Filter.type(TestSchema.Organization)); const executor = makeExecutor( withSpace(Query.select(Filter.type(TestSchema.Person, { name: Filter.in(subquery.project('name')) }))), ); // Even a fully disjoint hint (different type, different space) must re-run this query. const hint = makeHint({ spaceIds: makeSpaceSet(SpaceId.random()), typenames: makeTypeSet('com.example.unrelated'), }); expect(executor.matchesHint(hint)).toBe(true); }); test('negative control: the same query without the subquery predicate stays simple (hint-selective)', ({ expect, }) => { const executor = makeExecutor(withSpace(Query.select(Filter.type(TestSchema.Person, { name: 'Alice' })))); const disjointHint = makeHint({ spaceIds: makeSpaceSet(SpaceId.random()), typenames: makeTypeSet(ORG_TYPENAME), }); expect(executor.matchesHint(disjointHint)).toBe(false); expect(executor.matchesHint(makeHint({ typenames: makeTypeSet(PERSON_TYPENAME) }))).toBe(true); }); }); // --------------------------------------------------------------------------- // QueryExecutor.matchesHint — id-rooted relation traversal // // The shape: // Query.select(Filter.id(primary.id)).targetOf(SomeRelation).source() // i.e. it is rooted at a FIXED object id and traverses inbound relations. When a new source + // relation are persisted, the primary object is NOT re-indexed, so the indexing hint mentions // only the new relation/source (their ids + the relation typename) — not the primary's id. // The user's hypothesis: such a query is skipped by hint matching because its only constrained // dimension (objectIds = {primary.id}) does not overlap the hint. // --------------------------------------------------------------------------- describe('QueryExecutor.matchesHint — id-rooted relation traversal', () => { const PRIMARY_ID = EntityId.random(); const NEW_CHAT_ID = EntityId.random(); const NEW_RELATION_ID = EntityId.random(); const idRootedTraversal = () => makeExecutor(withSpace(Query.select(Filter.id(PRIMARY_ID)).targetOf(TestSchema.HasManager).source())); // The decisive case: a relation is added targeting PRIMARY_ID. The hint carries the new // relation/source object ids and the relation typename, but NOT PRIMARY_ID. If the query is // treated as "simple" (objectIds = {PRIMARY_ID}), this hint is disjoint and the query is skipped — // reproducing the reported "history dropdown doesn't update until reload" bug. test('re-runs when a relation targeting the root is added (hint omits the root id)', ({ expect }) => { const hint = makeHint({ spaceIds: makeSpaceSet(SPACE_ID), objectIds: makeObjectSet(NEW_CHAT_ID, NEW_RELATION_ID), }); expect(idRootedTraversal().matchesHint(hint)).toBe(true); }); // A type-keyed hint for the newly added objects must also re-run the traversal. test('re-runs for a type-only hint that omits the root id', ({ expect }) => { const hint = makeHint({ spaceIds: makeSpaceSet(SPACE_ID), typenames: makeTypeSet(PERSON_TYPENAME), }); expect(idRootedTraversal().matchesHint(hint)).toBe(true); }); // Sanity: traversal queries are conservative — even a fully disjoint hint re-runs them. test('re-runs for a fully disjoint hint (traversal → isSimple=false → always match)', ({ expect }) => { const hint = makeHint({ spaceIds: makeSpaceSet(SpaceId.random()), typenames: makeTypeSet('com.example.unrelated'), objectIds: makeObjectSet(EntityId.random()), }); expect(idRootedTraversal().matchesHint(hint)).toBe(true); }); }); // --------------------------------------------------------------------------- // QueryExecutor.matchesHint — id-rooted typed relation query // // `Query.select(Filter.id(root)).targetOf(Relation)` returns the relations themselves, so its result changes only // when the root changes, a relation of that type changes, or an endpoint of a returned relation changes. Edits to // other objects in the space (typing in a document's text, say) must not re-run it. // --------------------------------------------------------------------------- describe('QueryExecutor.matchesHint — id-rooted typed relation query', () => { const ROOT_ID = EntityId.random(); const HAS_MANAGER_TYPENAME = 'com.example.type.hasManager'; const relationsOfRoot = () => makeExecutor(withSpace(Query.select(Filter.id(ROOT_ID)).targetOf(TestSchema.HasManager))); test('skips a write to an unrelated object in the same space', ({ expect }) => { const hint = makeHint({ spaceIds: makeSpaceSet(SPACE_ID), typenames: makeTypeSet(PERSON_TYPENAME), objectIds: makeObjectSet(EntityId.random()), }); expect(relationsOfRoot().matchesHint(hint)).toBe(false); }); test('re-runs when a relation of the traversed type changes (hint omits the root id)', ({ expect }) => { const hint = makeHint({ spaceIds: makeSpaceSet(SPACE_ID), typenames: makeTypeSet(HAS_MANAGER_TYPENAME), objectIds: makeObjectSet(EntityId.random()), }); expect(relationsOfRoot().matchesHint(hint)).toBe(true); }); test('re-runs when the root changes', ({ expect }) => { const hint = makeHint({ spaceIds: makeSpaceSet(SPACE_ID), typenames: makeTypeSet(PERSON_TYPENAME), objectIds: makeObjectSet(ROOT_ID), }); expect(relationsOfRoot().matchesHint(hint)).toBe(true); }); test('re-runs for a hint that does not name its types', ({ expect }) => { const hint = makeHint({ spaceIds: makeSpaceSet(SPACE_ID), objectIds: makeObjectSet(EntityId.random()) }); expect(relationsOfRoot().matchesHint(hint)).toBe(true); }); test('skips a write in another space', ({ expect }) => { const hint = makeHint({ spaceIds: makeSpaceSet(SpaceId.random()), typenames: makeTypeSet(HAS_MANAGER_TYPENAME) }); expect(relationsOfRoot().matchesHint(hint)).toBe(false); }); test('stays conservative when the traversal continues past the relations', ({ expect }) => { const executor = makeExecutor(withSpace(Query.select(Filter.id(ROOT_ID)).targetOf(TestSchema.HasManager).source())); const hint = makeHint({ spaceIds: makeSpaceSet(SPACE_ID), typenames: makeTypeSet('com.example.unrelated') }); expect(executor.matchesHint(hint)).toBe(true); }); }); // --------------------------------------------------------------------------- // QueryExecutor.matchesHint — queue-scope derives spaceId // --------------------------------------------------------------------------- describe('QueryExecutor.matchesHint — queue scope derives spaceId', () => { test('queue-only scope derives spaceId and matches space-scoped hint', ({ expect }) => { const executor = new QueryExecutor({ ...testDeps, queryId: 'test', query: Query.select(Filter.type(TestSchema.Task)).from([{ _tag: 'feed' as const, feedUri: QUEUE_DXN }]).ast, reactivity: QueryReactivity.REACTIVE, }); // Hint carries the space derived from the queue DXN → should match. const matchingHint = makeHint({ spaceIds: makeSpaceSet(QUEUE_SPACE_ID) }); expect(executor.matchesHint(matchingHint)).toBe(true); // Hint from a different space → should not match. const nonMatchingHint = makeHint({ spaceIds: makeSpaceSet(SpaceId.random()) }); expect(executor.matchesHint(nonMatchingHint)).toBe(false); }); test('queue-only scope matches queue-scoped hint with the right queueId', ({ expect }) => { const executor = new QueryExecutor({ ...testDeps, queryId: 'test', query: Query.select(Filter.type(TestSchema.Task)).from([{ _tag: 'feed' as const, feedUri: QUEUE_DXN }]).ast, reactivity: QueryReactivity.REACTIVE, }); // Hint constrained to the exact queue → match. const matchingHint = makeHint({ queueIds: makeObjectSet(QUEUE_ID) }); expect(executor.matchesHint(matchingHint)).toBe(true); // Hint with a different queue → no match. const nonMatchingHint = makeHint({ queueIds: makeObjectSet(EntityId.random()) }); expect(executor.matchesHint(nonMatchingHint)).toBe(false); }); }); // --------------------------------------------------------------------------- // QueryExecutor.matchesHint — compiled (sql) plan // --------------------------------------------------------------------------- // The compiled path folds the plan into one `SqlStep`; scope analysis must still see the steps it // absorbed, or every compiled query matches every hint and re-runs on every write. describe('QueryExecutor.matchesHint — compiled (sql) plan', () => { const makeSqlExecutor = (query: { ast: QueryAST.Query }): QueryExecutor => new QueryExecutor({ ...testDeps, queryId: 'test', query: query.ast, reactivity: QueryReactivity.REACTIVE, executor: 'sql', }); test('space query does NOT match when hint typenames are disjoint', ({ expect }) => { const executor = makeSqlExecutor(withSpace(Query.select(Filter.type(TestSchema.Person)))); expect(executor.compiled).toBe(true); const disjoint = makeHint({ spaceIds: makeSpaceSet(SPACE_ID), typenames: makeTypeSet(ORG_TYPENAME) }); expect(executor.matchesHint(disjoint)).toBe(false); expect(executor.matchesHint(makeHint({ typenames: makeTypeSet(PERSON_TYPENAME) }))).toBe(true); }); test('feed query does NOT match a space write of an unrelated type', ({ expect }) => { const executor = makeSqlExecutor( Query.select(Filter.type(TestSchema.Task)).from([{ _tag: 'feed' as const, feedUri: QUEUE_DXN }]), ); expect(executor.compiled).toBe(true); const spaceWrite = makeHint({ spaceIds: makeSpaceSet(QUEUE_SPACE_ID), typenames: makeTypeSet(ORG_TYPENAME) }); expect(executor.matchesHint(spaceWrite)).toBe(false); expect(executor.matchesHint(makeHint({ queueIds: makeObjectSet(QUEUE_ID) }))).toBe(true); }); });