fix(test): exercise mongoQuery wrong-driver guard, not assertConnection

The wrong-driver test's fake createConnector ignored the requested id
and always returned a mongodb connector, so the driver guard never
ran; the rejection came incidentally from assertConnection deep in the
connector. It also asserted KtxQueryError, but the guard throws
KtxExpectedError. Dispatch the fake by id and assert the guard's own
error type and message.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BdUJSmjqGoksdc45ZZJiVY
This commit is contained in:
Kevin Messiaen 2026-07-06 16:30:56 +07:00
parent 9853995930
commit 8aa4f8f74a
No known key found for this signature in database
GPG key ID: 19FF750A17315202

View file

@ -1,8 +1,9 @@
import { describe, expect, it, vi } from 'vitest';
import { KtxQueryError } from '../../src/errors.js';
import { KtxExpectedError } from '../../src/errors.js';
import { KtxMongoDbScanConnector } from '../../src/connectors/mongodb/connector.js';
import { createLocalProjectMcpContextPorts } from '../../src/context/mcp/local-project-ports.js';
import type { KtxLocalProject } from '../../src/context/project/project.js';
import type { KtxScanConnector } from '../../src/context/scan/types.js';
const MONGO_DOCS = [
{ _id: 'a1', city: 'Indianapolis' },
@ -38,7 +39,18 @@ function project(): KtxLocalProject {
} as unknown as KtxLocalProject;
}
function portsFor(createConnector: (id: string) => KtxMongoDbScanConnector) {
function warehouseConnector(): KtxScanConnector {
return {
id: 'postgres:warehouse',
driver: 'postgres',
capabilities: { structuralIntrospection: true } as KtxScanConnector['capabilities'],
introspect: vi.fn(),
listSchemas: vi.fn(async () => []),
listTables: vi.fn(async () => []),
} as unknown as KtxScanConnector;
}
function portsFor(createConnector: (id: string) => KtxScanConnector) {
return createLocalProjectMcpContextPorts(project(), {
embeddingService: null,
localScan: { createConnector } as never,
@ -58,10 +70,13 @@ describe('mongoQuery MCP port', () => {
expect(result.rowCount).toBe(2);
});
it('rejects a non-mongodb connection id as an expected query error', async () => {
const ports = portsFor(() => mongoConnector());
it('rejects a non-mongodb connection id with an expected error naming the wrong driver', async () => {
const ports = portsFor((id) => (id === 'mongo' ? mongoConnector() : warehouseConnector()));
await expect(
ports.mongoQuery!.execute({ connectionId: 'warehouse', collection: 'x', pipeline: [], limit: 10 }),
).rejects.toBeInstanceOf(KtxQueryError);
).rejects.toThrow(/is not a MongoDB connection/);
await expect(
ports.mongoQuery!.execute({ connectionId: 'warehouse', collection: 'x', pipeline: [], limit: 10 }),
).rejects.toBeInstanceOf(KtxExpectedError);
});
});