|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The stdio data bridge asks the data door's internal-field gate — the |
| 5 | + * `protocol` service's `assertNoInternalFieldEvaluated` — before its engine |
| 6 | + * reads anything, on `query` (the call's `where` and `orderBy`) and on |
| 7 | + * `aggregate` (`where`, `groupBy`, `aggregations`), and a refusal it throws |
| 8 | + * reaches the caller as thrown: the tool renders its `code`, `status` and |
| 9 | + * sentence, and the engine is never asked. |
| 10 | + * |
| 11 | + * The gate itself is the protocol's and is pinned there |
| 12 | + * (`@objectstack/metadata-protocol`, the public-face cases beside the data |
| 13 | + * door's position enumeration), and the whole path on a real stack in |
| 14 | + * `@objectstack/dogfood`. This file pins the bridge's half: that it asks, with |
| 15 | + * what, before what — and that a host whose protocol has no such face (or that |
| 16 | + * registers no protocol) is served exactly as before. Refusals are asserted on |
| 17 | + * the envelope, never on a bare throw. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, it, expect, vi } from 'vitest'; |
| 21 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 22 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 23 | +import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js'; |
| 24 | +import type { ExecutionContext } from '@objectstack/spec/kernel'; |
| 25 | +import type { IDataEngine, IMetadataService } from '@objectstack/spec/contracts'; |
| 26 | +import { createStdioDataBridge, type StdioProtocolObjectReads } from './stdio-data-bridge.js'; |
| 27 | +import { registerObjectTools } from './mcp-http-tools.js'; |
| 28 | + |
| 29 | +const OBJECT = 'ifx_vault'; |
| 30 | +const FLAGGED = 'pin'; |
| 31 | +const DEF = { name: OBJECT, fields: { name: { type: 'text' }, [FLAGGED]: { type: 'text' } } }; |
| 32 | + |
| 33 | +/** The door's refusal as the protocol throws it (fields only; the sentence is the protocol's to pin). */ |
| 34 | +function doorRefusal(param: string) { |
| 35 | + return Object.assign(new Error(`Cannot evaluate '${OBJECT}' by '${FLAGGED}' (${param}): the query was not run.`), { |
| 36 | + code: 'INVALID_FIELD', status: 400, httpStatus: 400, object: OBJECT, field: FLAGGED, fields: [FLAGGED], param, |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +/** A stand-in for the protocol's gate: refuses any query whose JSON names the flagged field. */ |
| 41 | +function gate() { |
| 42 | + return vi.fn((_object: string, query: Record<string, unknown>) => { |
| 43 | + if (JSON.stringify(query).includes(`"${FLAGGED}"`)) throw doorRefusal('filter'); |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +function makeBridge(protocol?: StdioProtocolObjectReads) { |
| 48 | + const engine = { |
| 49 | + find: vi.fn(async () => [{ id: 'r1', name: 'alpha' }]), |
| 50 | + aggregate: vi.fn(async () => [{ n: 1 }]), |
| 51 | + }; |
| 52 | + const metadataService = { getObject: vi.fn(async (name: string) => (name === OBJECT ? DEF : undefined)) }; |
| 53 | + const bridge = createStdioDataBridge({ |
| 54 | + engine: engine as unknown as IDataEngine, |
| 55 | + metadataService: metadataService as unknown as IMetadataService, |
| 56 | + ...(protocol ? { protocol: () => protocol } : {}), |
| 57 | + resolvePrincipal: async () => ({ userId: 'u1', isSystem: false }) as unknown as ExecutionContext, |
| 58 | + }); |
| 59 | + return { bridge, engine }; |
| 60 | +} |
| 61 | + |
| 62 | +const refusalOf = async (run: () => Promise<unknown>) => { |
| 63 | + const err: any = await run().then(() => undefined, (e: unknown) => e); |
| 64 | + expect(err, 'the call resolved: the gate\'s refusal did not reach the caller').toBeDefined(); |
| 65 | + return { code: err.code, status: err.status, field: err.field, object: err.object }; |
| 66 | +}; |
| 67 | +const REFUSED = { code: 'INVALID_FIELD', status: 400, field: FLAGGED, object: OBJECT }; |
| 68 | + |
| 69 | +describe('the stdio bridge asks the protocol\'s internal-field gate before the engine', () => { |
| 70 | + it('query: the gate is asked with the call\'s where and orderBy, and its refusal stops the read', async () => { |
| 71 | + const assertNoInternalFieldEvaluated = gate(); |
| 72 | + const { bridge, engine } = makeBridge({ assertNoInternalFieldEvaluated }); |
| 73 | + const where = { [FLAGGED]: { $startsWith: 'x' } }; |
| 74 | + const orderBy = [{ field: 'name', order: 'asc' as const }]; |
| 75 | + expect(await refusalOf(() => bridge.query(OBJECT, { where, orderBy, limit: 5 }))).toEqual(REFUSED); |
| 76 | + expect(assertNoInternalFieldEvaluated).toHaveBeenCalledWith(OBJECT, { where, orderBy }); |
| 77 | + expect(engine.find).not.toHaveBeenCalled(); |
| 78 | + |
| 79 | + const sorted = [{ field: FLAGGED, order: 'desc' as const }]; |
| 80 | + expect(await refusalOf(() => bridge.query(OBJECT, { orderBy: sorted }))).toEqual(REFUSED); |
| 81 | + expect(engine.find).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('aggregate: the gate is asked with where, groupBy and aggregations, and its refusal stops the aggregate', async () => { |
| 85 | + const assertNoInternalFieldEvaluated = gate(); |
| 86 | + const { bridge, engine } = makeBridge({ assertNoInternalFieldEvaluated }); |
| 87 | + const bodies = [ |
| 88 | + { where: { [FLAGGED]: 'x' }, aggregations: [{ function: 'count' as const, alias: 'n' }] }, |
| 89 | + { groupBy: [FLAGGED], aggregations: [{ function: 'count' as const, alias: 'n' }] }, |
| 90 | + { aggregations: [{ function: 'max' as const, field: FLAGGED, alias: 'm' }] }, |
| 91 | + { aggregations: [{ function: 'count' as const, alias: 'n', filter: { [FLAGGED]: 'x' } }] }, |
| 92 | + ]; |
| 93 | + for (const body of bodies) { |
| 94 | + expect(await refusalOf(() => bridge.aggregate!(OBJECT, body))).toEqual(REFUSED); |
| 95 | + expect(assertNoInternalFieldEvaluated).toHaveBeenLastCalledWith(OBJECT, { |
| 96 | + where: (body as { where?: unknown }).where, |
| 97 | + groupBy: (body as { groupBy?: unknown }).groupBy, |
| 98 | + aggregations: body.aggregations, |
| 99 | + }); |
| 100 | + } |
| 101 | + expect(engine.aggregate).not.toHaveBeenCalled(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('CONTROL — a query the gate passes reaches the engine unchanged, on both verbs', async () => { |
| 105 | + const assertNoInternalFieldEvaluated = gate(); |
| 106 | + const { bridge, engine } = makeBridge({ assertNoInternalFieldEvaluated }); |
| 107 | + const where = { name: 'alpha' }; |
| 108 | + await bridge.query(OBJECT, { where, orderBy: [{ field: 'name', order: 'asc' }] }); |
| 109 | + expect(engine.find).toHaveBeenCalledTimes(1); |
| 110 | + expect((engine.find.mock.calls[0] as unknown[])[1]).toMatchObject({ where, orderBy: [{ field: 'name', order: 'asc' }] }); |
| 111 | + await bridge.aggregate!(OBJECT, { where, groupBy: ['name'], aggregations: [{ function: 'count', alias: 'n' }] }); |
| 112 | + expect(engine.aggregate).toHaveBeenCalledTimes(1); |
| 113 | + expect(assertNoInternalFieldEvaluated).toHaveBeenCalledTimes(2); |
| 114 | + }); |
| 115 | + |
| 116 | + it('CONTROL — no protocol, or one without the face: served as before (no door to agree with)', async () => { |
| 117 | + for (const protocol of [undefined, {} as StdioProtocolObjectReads]) { |
| 118 | + const { bridge, engine } = makeBridge(protocol); |
| 119 | + await bridge.query(OBJECT, { where: { [FLAGGED]: 'x' } }); |
| 120 | + await bridge.aggregate!(OBJECT, { where: { [FLAGGED]: 'x' }, aggregations: [{ function: 'count', alias: 'n' }] }); |
| 121 | + expect(engine.find).toHaveBeenCalledTimes(1); |
| 122 | + expect(engine.aggregate).toHaveBeenCalledTimes(1); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + it('query_records and aggregate_records render the gate\'s refusal as its envelope', async () => { |
| 127 | + const { bridge, engine } = makeBridge({ assertNoInternalFieldEvaluated: gate() }); |
| 128 | + const server = new McpServer({ name: 'pin', version: '0' }); |
| 129 | + registerObjectTools(server, bridge); |
| 130 | + const [clientSide, serverSide] = InMemoryTransport.createLinkedPair(); |
| 131 | + await server.connect(serverSide); |
| 132 | + const client = new Client({ name: 'pin', version: '0' }); |
| 133 | + await client.connect(clientSide); |
| 134 | + try { |
| 135 | + for (const [name, args] of [ |
| 136 | + ['query_records', { objectName: OBJECT, where: { [FLAGGED]: 'x' } }], |
| 137 | + ['aggregate_records', { objectName: OBJECT, groupBy: [FLAGGED], aggregations: [{ function: 'count', alias: 'n' }] }], |
| 138 | + ] as const) { |
| 139 | + const res = (await client.callTool({ name, arguments: args })) as { isError?: boolean; content: Array<{ text: string }> }; |
| 140 | + expect(res.isError, name).toBe(true); |
| 141 | + const { error } = JSON.parse(res.content[0]!.text) as { error: { code: unknown; status: unknown; message: string } }; |
| 142 | + expect({ code: error.code, status: error.status }).toEqual({ code: 'INVALID_FIELD', status: 400 }); |
| 143 | + expect(error.message).toBe(doorRefusal('filter').message); |
| 144 | + } |
| 145 | + expect(engine.find).not.toHaveBeenCalled(); |
| 146 | + expect(engine.aggregate).not.toHaveBeenCalled(); |
| 147 | + } finally { |
| 148 | + await client.close(); |
| 149 | + } |
| 150 | + }); |
| 151 | +}); |
0 commit comments