Files
dbmole-mcp/packages/esmole-mcp/test/unit/tools/search.test.ts
T

55 lines
2.0 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { runEsSearch } from '../../../src/tools/search.js'
const fakeManaged = (calls: unknown[]) =>
({
config: { name: 'c', readonly: false, type: 'elasticsearch' },
backend: {
request: async (method: string, path: string, opts?: { body?: unknown }) => {
calls.push({ method, path, body: opts?.body })
return { status: 200, body: { hits: { total: { value: 0 } }, aggregations: {} } }
},
dispose: async () => {}
}
}) as never
describe('runEsSearch', () => {
it('posts query + size and forwards _source projection', async () => {
const calls: { path: string; body: { query: unknown; size: number; _source: unknown } }[] =
[]
await runEsSearch(fakeManaged(calls), 'books', { match: { t: 'x' } }, undefined, 5, [
'title'
])
expect(calls[0].path).toBe('/books/_search')
expect(calls[0].body.query).toEqual({ match: { t: 'x' } })
expect(calls[0].body.size).toBe(5)
expect(calls[0].body._source).toEqual(['title'])
})
it('forces size 0 in aggs-only mode (aggs present, no query)', async () => {
const calls: { body: { size: number; aggs: unknown; query?: unknown } }[] = []
await runEsSearch(
fakeManaged(calls),
'books',
undefined,
{ byYear: { terms: { field: 'year' } } },
undefined,
undefined
)
expect(calls[0].body.size).toBe(0)
expect(calls[0].body.aggs).toEqual({ byYear: { terms: { field: 'year' } } })
expect(calls[0].body.query).toBeUndefined()
})
it('defaults size to 10 when query given without size', async () => {
const calls: { body: { size: number } }[] = []
await runEsSearch(
fakeManaged(calls),
'books',
{ match_all: {} },
undefined,
undefined,
undefined
)
expect(calls[0].body.size).toBe(10)
})
})