|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #4386 — the `callData('query')` ObjectQL fallback serves the caller's query |
| 5 | + * instead of dropping it. |
| 6 | + * |
| 7 | + * Regression: when the protocol service is unavailable (lean assemblies, MCP |
| 8 | + * multi-env with a raw driver), the fallback passed only `{ context }` to |
| 9 | + * `ql.find` — no `where`, no `orderBy`, no `limit` — and answered with an |
| 10 | + * ordinary-looking `{ records, total }` of the ENTIRE table. The sibling |
| 11 | + * `get`/`update`/`delete` fallbacks all built a proper `where`; `query` was |
| 12 | + * the only verb whose fallback forgot the request. |
| 13 | + * |
| 14 | + * The fallback now forwards the canonical QueryAST keys both possible |
| 15 | + * recipients execute (engine option bag / raw-driver QueryAST are aligned by |
| 16 | + * design), and REFUSES (501) anything it cannot reproduce without the |
| 17 | + * protocol layer — wire spellings needing fold/lowering (`sort`, `select`), |
| 18 | + * and capabilities a raw driver would silently drop (`search`, `expand`). |
| 19 | + * A fallback that cannot reproduce the query's semantics must not pretend to. |
| 20 | + */ |
| 21 | + |
| 22 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 23 | +import { callData, type ActionExecutionDeps } from './action-execution.js'; |
| 24 | + |
| 25 | +const EC = { userId: 'u1', isSystem: false, positions: [], permissions: [] } as any; |
| 26 | + |
| 27 | +function makeHarness(opts: { withProtocol?: boolean } = {}) { |
| 28 | + const finds: any[] = []; |
| 29 | + const findData: any[] = []; |
| 30 | + const ql = { |
| 31 | + find: async (_o: string, bag: any) => { finds.push(bag); return [{ id: 'r1' }, { id: 'r2' }]; }, |
| 32 | + }; |
| 33 | + const protocol = opts.withProtocol |
| 34 | + ? { findData: async (req: any) => { findData.push(req); return { object: req.object, records: [], total: 0, hasMore: false }; } } |
| 35 | + : undefined; |
| 36 | + const services: Record<string, any> = { |
| 37 | + metadata: { getObject: async () => ({ name: 'task', fields: {} }) }, |
| 38 | + objectql: ql, |
| 39 | + ...(protocol ? { protocol } : {}), |
| 40 | + }; |
| 41 | + const deps: ActionExecutionDeps = { |
| 42 | + resolveService: (async (name: string) => services[name]) as any, |
| 43 | + getObjectQL: async () => ql, |
| 44 | + }; |
| 45 | + return { deps, finds, findData }; |
| 46 | +} |
| 47 | + |
| 48 | +describe("callData('query') fallback serves the query it was given (#4386)", () => { |
| 49 | + let h: ReturnType<typeof makeHarness>; |
| 50 | + |
| 51 | + beforeEach(() => { h = makeHarness(); }); |
| 52 | + |
| 53 | + it('forwards where/orderBy/limit/offset/fields to ql.find, with the server context', async () => { |
| 54 | + const query = { |
| 55 | + where: { status: 'open' }, |
| 56 | + orderBy: [{ field: 'created_at', order: 'desc' }], |
| 57 | + limit: 5, |
| 58 | + offset: 10, |
| 59 | + fields: ['id', 'title'], |
| 60 | + }; |
| 61 | + const out = await callData(h.deps, 'query', { object: 'task', query }, undefined, undefined, EC); |
| 62 | + expect(h.finds).toHaveLength(1); |
| 63 | + expect(h.finds[0]).toMatchObject({ ...query, context: EC }); |
| 64 | + expect(out.records).toHaveLength(2); |
| 65 | + }); |
| 66 | + |
| 67 | + it('extracts query fields from bare params when params.query is absent — same source as the protocol path', async () => { |
| 68 | + await callData(h.deps, 'query', { object: 'task', where: { status: 'open' }, limit: 3 }, undefined, undefined, EC); |
| 69 | + expect(h.finds[0]).toMatchObject({ where: { status: 'open' }, limit: 3 }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('a caller-supplied context is dropped, never honoured — server-derived only, matching findData', async () => { |
| 73 | + await callData(h.deps, 'query', { object: 'task', query: { where: { a: 1 }, context: { isSystem: true } } }, undefined, undefined, EC); |
| 74 | + expect(h.finds[0].context).toBe(EC); |
| 75 | + }); |
| 76 | + |
| 77 | + it.each(['sort', 'select', 'skip', 'populate', 'search', 'expand', '$filter'])( |
| 78 | + 'refuses %s with 501 instead of part-serving — nothing reaches ql.find', |
| 79 | + async (key) => { |
| 80 | + await expect( |
| 81 | + callData(h.deps, 'query', { object: 'task', query: { where: { a: 1 }, [key]: 'x' } }, undefined, undefined, EC), |
| 82 | + ).rejects.toMatchObject({ statusCode: 501 }); |
| 83 | + expect(h.finds).toHaveLength(0); |
| 84 | + }, |
| 85 | + ); |
| 86 | + |
| 87 | + it('names the unservable keys and the served set in the refusal', async () => { |
| 88 | + await expect( |
| 89 | + callData(h.deps, 'query', { object: 'task', query: { sort: '-x', select: 'id' } }, undefined, undefined, EC), |
| 90 | + ).rejects.toMatchObject({ message: expect.stringMatching(/'sort', 'select'.*where, fields, orderBy, limit, offset/s) }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('an empty query still lists (the protocol path lists too) — no refusal, no predicate', async () => { |
| 94 | + const out = await callData(h.deps, 'query', { object: 'task' }, undefined, undefined, EC); |
| 95 | + expect(h.finds[0]).toMatchObject({ context: EC }); |
| 96 | + expect(out.total).toBe(2); |
| 97 | + }); |
| 98 | + |
| 99 | + it('null-valued keys are withdrawals, not unservable', async () => { |
| 100 | + await callData(h.deps, 'query', { object: 'task', query: { sort: null, where: { a: 1 } } }, undefined, undefined, EC); |
| 101 | + expect(h.finds[0]).toMatchObject({ where: { a: 1 } }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('with the protocol service present the fallback never runs — findData gets the query verbatim, wire spellings included', async () => { |
| 105 | + const withP = makeHarness({ withProtocol: true }); |
| 106 | + await callData(withP.deps, 'query', { object: 'task', query: { sort: '-title', top: 5 } }, undefined, undefined, EC); |
| 107 | + expect(withP.findData).toHaveLength(1); |
| 108 | + expect(withP.findData[0].query).toEqual({ sort: '-title', top: 5 }); |
| 109 | + expect(withP.finds).toHaveLength(0); |
| 110 | + }); |
| 111 | +}); |
0 commit comments