|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `aggregate_records`: each aggregation entry carries its `filter` to the |
| 5 | + * engine and refuses an undeclared key by name (#28550). |
| 6 | + * |
| 7 | + * THE DEFECT. The entry was a plain `z.object({ function, field, alias })`, so |
| 8 | + * zod's `.strip` default deleted a per-aggregation `filter` before the handler |
| 9 | + * ran. `{ function: 'count', alias: 'n', filter: { status: 'active' } }` beside |
| 10 | + * an unfiltered `count` answered the same number twice (5 and 5 on the stock |
| 11 | + * showcase), while the REST query door answered 2 and 5 for the same body. An |
| 12 | + * unknown entry key (`bogusKey`) was dropped the same way. The top-level |
| 13 | + * closure (#16913) never reached inside the array. |
| 14 | + * |
| 15 | + * WHAT IS PINNED HERE, at the tool layer over a recording bridge: the entry's |
| 16 | + * `filter` reaches the bridge byte-for-byte (the bridge hands `aggregations` to |
| 17 | + * `engine.aggregate`, which applies it, so nothing in this package evaluates |
| 18 | + * it); a malformed filter gets the engine node's own refusal at the door; an |
| 19 | + * undeclared entry key is refused by name before the bridge is asked; and the |
| 20 | + * advertised JSON Schema declares both facts. The MCP-equals-REST answer over a |
| 21 | + * real engine, and the field-permission refusal, are pinned end to end in |
| 22 | + * `packages/qa/dogfood/test/showcase-mcp-aggregate-filter.dogfood.test.ts`. |
| 23 | + * |
| 24 | + * CONTROLS. Entries without a filter reach the bridge exactly as before, and |
| 25 | + * the top-level unknown-key posture is unchanged. |
| 26 | + */ |
| 27 | + |
| 28 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 29 | + |
| 30 | +import { MCPServerRuntime } from './mcp-server-runtime.js'; |
| 31 | +import type { McpDataBridge } from './mcp-http-tools.js'; |
| 32 | + |
| 33 | +interface RecordingBridge extends McpDataBridge { |
| 34 | + calls: any[]; |
| 35 | +} |
| 36 | + |
| 37 | +function makeBridge(): RecordingBridge { |
| 38 | + const calls: any[] = []; |
| 39 | + return { |
| 40 | + calls, |
| 41 | + async listObjects() { return [{ name: 'showcase_project' }]; }, |
| 42 | + async describeObject(name: string) { return { name, fields: [] }; }, |
| 43 | + async query(object: string) { return { object, records: [] }; }, |
| 44 | + async get() { return null; }, |
| 45 | + async create(object: string) { return { object, id: 'n1' }; }, |
| 46 | + async update(object: string, id: string) { return { object, id }; }, |
| 47 | + async remove(object: string, id: string) { return { object, id, success: true }; }, |
| 48 | + async aggregate(object: string, opts: any) { |
| 49 | + calls.push([object, opts]); |
| 50 | + return [{ n: 2, all: 5 }]; |
| 51 | + }, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +let nextId = 1; |
| 56 | + |
| 57 | +async function rpc(runtime: MCPServerRuntime, bridge: RecordingBridge, method: string, params?: unknown) { |
| 58 | + const body = { jsonrpc: '2.0', id: nextId++, method, ...(params === undefined ? {} : { params }) }; |
| 59 | + const req = new Request('http://localhost/api/v1/mcp', { |
| 60 | + method: 'POST', |
| 61 | + headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' }, |
| 62 | + body: JSON.stringify(body), |
| 63 | + }); |
| 64 | + const res = await runtime.handleHttpRequest(req, { bridge, parsedBody: body }); |
| 65 | + return (await res.json()) as any; |
| 66 | +} |
| 67 | + |
| 68 | +async function aggregate(runtime: MCPServerRuntime, bridge: RecordingBridge, args: Record<string, unknown>) { |
| 69 | + const json = await rpc(runtime, bridge, 'tools/call', { name: 'aggregate_records', arguments: args }); |
| 70 | + const text = json.result?.content?.map((c: any) => c.text).join('\n') ?? json.error?.message ?? ''; |
| 71 | + return { refused: json.result?.isError === true || Boolean(json.error), text: String(text) }; |
| 72 | +} |
| 73 | + |
| 74 | +describe('aggregate_records — the aggregation entry (#28550)', () => { |
| 75 | + let runtime: MCPServerRuntime; |
| 76 | + let bridge: RecordingBridge; |
| 77 | + |
| 78 | + beforeEach(() => { |
| 79 | + runtime = new MCPServerRuntime({ name: 't', version: '1.0.0' }); |
| 80 | + bridge = makeBridge(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('the reported call: an entry `filter` reaches the bridge unchanged, beside an unfiltered sibling', async () => { |
| 84 | + const aggregations = [ |
| 85 | + { function: 'count', alias: 'n', filter: { status: 'active' } }, |
| 86 | + { function: 'count', alias: 'all' }, |
| 87 | + ]; |
| 88 | + const { refused, text } = await aggregate(runtime, bridge, { objectName: 'showcase_project', aggregations }); |
| 89 | + expect(refused, text).toBe(false); |
| 90 | + expect(bridge.calls).toHaveLength(1); |
| 91 | + expect(bridge.calls[0][0]).toBe('showcase_project'); |
| 92 | + // Exactly what was sent: the filter on the first entry, none on the second. |
| 93 | + expect(bridge.calls[0][1].aggregations).toEqual(aggregations); |
| 94 | + }); |
| 95 | + |
| 96 | + it('an operator filter reaches the bridge as sent', async () => { |
| 97 | + const aggregations = [ |
| 98 | + { function: 'sum', field: 'budget', alias: 'big', filter: { budget: { $gte: 1000 }, status: { $in: ['active', 'planning'] } } }, |
| 99 | + ]; |
| 100 | + const { refused, text } = await aggregate(runtime, bridge, { |
| 101 | + objectName: 'showcase_project', |
| 102 | + aggregations, |
| 103 | + groupBy: ['status'], |
| 104 | + where: { owner: 'u1' }, |
| 105 | + }); |
| 106 | + expect(refused, text).toBe(false); |
| 107 | + expect(bridge.calls[0][1]).toMatchObject({ aggregations, groupBy: ['status'], where: { owner: 'u1' } }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('CONTROL — entries without a filter reach the bridge exactly as before', async () => { |
| 111 | + const aggregations = [ |
| 112 | + { function: 'count', alias: 'n' }, |
| 113 | + { function: 'sum', field: 'budget', alias: 'total' }, |
| 114 | + ]; |
| 115 | + const { refused, text } = await aggregate(runtime, bridge, { objectName: 'showcase_project', aggregations }); |
| 116 | + expect(refused, text).toBe(false); |
| 117 | + expect(bridge.calls[0][1].aggregations).toEqual(aggregations); |
| 118 | + }); |
| 119 | + |
| 120 | + it('an undeclared entry key is refused by name, and the bridge is never asked', async () => { |
| 121 | + const { refused, text } = await aggregate(runtime, bridge, { |
| 122 | + objectName: 'showcase_project', |
| 123 | + aggregations: [{ function: 'count', alias: 'n', bogusKey: 1 }], |
| 124 | + }); |
| 125 | + expect(refused).toBe(true); |
| 126 | + expect(text).toContain('bogusKey'); |
| 127 | + expect(bridge.calls).toHaveLength(0); |
| 128 | + }); |
| 129 | + |
| 130 | + it('an entry `where` is refused, and the refusal names `filter` as the spelling to send', async () => { |
| 131 | + const { refused, text } = await aggregate(runtime, bridge, { |
| 132 | + objectName: 'showcase_project', |
| 133 | + aggregations: [{ function: 'count', alias: 'n', where: { status: 'active' } }], |
| 134 | + }); |
| 135 | + expect(refused).toBe(true); |
| 136 | + expect(text).toContain('where'); |
| 137 | + expect(text).toContain('filter'); |
| 138 | + expect(bridge.calls).toHaveLength(0); |
| 139 | + }); |
| 140 | + |
| 141 | + it('the retired `distinct` entry key is refused, not dropped', async () => { |
| 142 | + const { refused, text } = await aggregate(runtime, bridge, { |
| 143 | + objectName: 'showcase_project', |
| 144 | + aggregations: [{ function: 'sum', field: 'budget', alias: 's', distinct: true }], |
| 145 | + }); |
| 146 | + expect(refused).toBe(true); |
| 147 | + expect(text).toContain('distinct'); |
| 148 | + expect(bridge.calls).toHaveLength(0); |
| 149 | + }); |
| 150 | + |
| 151 | + it('a filter that is not a filter object is refused at the door by the engine node\'s own shape', async () => { |
| 152 | + for (const filter of ["status = 'active'", ['status', '=', 'active'], 7]) { |
| 153 | + const { refused } = await aggregate(runtime, bridge, { |
| 154 | + objectName: 'showcase_project', |
| 155 | + aggregations: [{ function: 'count', alias: 'n', filter }], |
| 156 | + }); |
| 157 | + expect(refused, JSON.stringify(filter)).toBe(true); |
| 158 | + } |
| 159 | + expect(bridge.calls).toHaveLength(0); |
| 160 | + }); |
| 161 | + |
| 162 | + it('CONTROL — an undeclared TOP-LEVEL key is still refused, naming the declared spelling', async () => { |
| 163 | + const { refused, text } = await aggregate(runtime, bridge, { |
| 164 | + objectName: 'showcase_project', |
| 165 | + metrics: [{ function: 'count', alias: 'n' }], |
| 166 | + }); |
| 167 | + expect(refused).toBe(true); |
| 168 | + expect(text).toContain('metrics'); |
| 169 | + expect(text).toContain('aggregations'); |
| 170 | + expect(bridge.calls).toHaveLength(0); |
| 171 | + }); |
| 172 | + |
| 173 | + it('`tools/list` advertises the entry as closed, with `filter` declared', async () => { |
| 174 | + const json = await rpc(runtime, bridge, 'tools/list'); |
| 175 | + const tool = (json.result?.tools ?? []).find((t: any) => t.name === 'aggregate_records'); |
| 176 | + expect(tool).toBeDefined(); |
| 177 | + const entry = tool.inputSchema?.properties?.aggregations?.items; |
| 178 | + expect(entry?.additionalProperties).toBe(false); |
| 179 | + expect(Object.keys(entry?.properties ?? {}).sort()).toEqual(['alias', 'field', 'filter', 'function']); |
| 180 | + expect(entry?.required).toEqual(expect.arrayContaining(['function', 'alias'])); |
| 181 | + }); |
| 182 | +}); |
0 commit comments