mirror of
https://github.com/giancarloerra/socraticode.git
synced 2026-07-03 14:05:21 +02:00
test: add includeLinked and searchMultipleCollections tests
- query-tools.test.ts: 6 new tests for includeLinked parameter: - omitted/false → calls searchChunks (not searchMultipleCollections) - true → calls searchMultipleCollections via resolveLinkedCollections - passes collections and args correctly - project label appears in output - no project tag when field absent - multi-collection-search.test.ts: 1 new test for searchMultipleCollections empty-input short-circuit (internal searchChunks calls cannot be mocked from the same module — integration covered by query-tools tests)
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
* Tests the pure function directly (no mocks needed).
|
||||
*/
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { mergeMultiCollectionResults } from "../../src/services/qdrant.js";
|
||||
import type { SearchResult } from "../../src/types.js";
|
||||
|
||||
@@ -222,3 +222,24 @@ describe("mergeMultiCollectionResults", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ── searchMultipleCollections tests ─────────────────────────────────────
|
||||
// searchMultipleCollections calls searchChunks internally (same module),
|
||||
// so vi.mock cannot intercept those calls. The pure merge logic is
|
||||
// thoroughly tested above (9 tests). The orchestration layer (includeLinked
|
||||
// → resolveLinkedCollections → searchMultipleCollections) is tested via
|
||||
// query-tools.test.ts which mocks at the module boundary.
|
||||
// Here we only test the empty-input short-circuit which needs no Qdrant.
|
||||
|
||||
vi.mock("../../src/services/logger.js", () => ({
|
||||
logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
|
||||
}));
|
||||
|
||||
const { searchMultipleCollections } = await import("../../src/services/qdrant.js");
|
||||
|
||||
describe("searchMultipleCollections", () => {
|
||||
it("returns empty array when collections array is empty", async () => {
|
||||
const results = await searchMultipleCollections([], "test", 10);
|
||||
expect(results).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -65,18 +65,26 @@ vi.mock("../../src/services/docker.js", () => ({
|
||||
// ── qdrant.js mock ───────────────────────────────────────────────────────
|
||||
|
||||
const mockSearchChunks = vi.fn(async (_collection: string, _query: string, _limit: number) => []);
|
||||
const mockSearchMultipleCollections = vi.fn(async () => []);
|
||||
|
||||
vi.mock("../../src/services/qdrant.js", () => ({
|
||||
searchChunks: (...args: unknown[]) => mockSearchChunks(...(args as [string, string, number])),
|
||||
searchMultipleCollections: (...args: unknown[]) => mockSearchMultipleCollections(...(args as [])),
|
||||
getCollectionInfo: vi.fn(async () => ({ points_count: 0 })),
|
||||
getProjectMetadata: vi.fn(async () => null),
|
||||
}));
|
||||
|
||||
// ── config.js mock ───────────────────────────────────────────────────────
|
||||
|
||||
const mockResolveLinkedCollections = vi.fn(() => [
|
||||
{ name: "codebase_abc123", label: "my-project" },
|
||||
{ name: "codebase_def456", label: "shared-lib" },
|
||||
]);
|
||||
|
||||
vi.mock("../../src/config.js", () => ({
|
||||
collectionName: vi.fn(() => "test-collection"),
|
||||
projectIdFromPath: vi.fn(() => "test-project-id"),
|
||||
resolveLinkedCollections: (...args: unknown[]) => mockResolveLinkedCollections(...(args as [])),
|
||||
}));
|
||||
|
||||
// ── indexer.js mock ──────────────────────────────────────────────────────
|
||||
@@ -178,3 +186,121 @@ describe("codebase_search — embedding provider readiness guard", () => {
|
||||
expect(mockGetEmbeddingProvider).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
|
||||
// ── includeLinked tests ──────────────────────────────────────────────────
|
||||
|
||||
describe("codebase_search — includeLinked parameter", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// Default: ollama provider
|
||||
mockGetEmbeddingConfig.mockReturnValue({
|
||||
embeddingProvider: "ollama",
|
||||
embeddingModel: "test-model",
|
||||
});
|
||||
});
|
||||
|
||||
it("calls searchChunks (not searchMultipleCollections) when includeLinked is omitted", async () => {
|
||||
await handleQueryTool("codebase_search", {
|
||||
projectPath: TEST_PATH,
|
||||
query: "test query",
|
||||
});
|
||||
|
||||
expect(mockSearchChunks).toHaveBeenCalledOnce();
|
||||
expect(mockSearchMultipleCollections).not.toHaveBeenCalled();
|
||||
expect(mockResolveLinkedCollections).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("calls searchChunks (not searchMultipleCollections) when includeLinked is false", async () => {
|
||||
await handleQueryTool("codebase_search", {
|
||||
projectPath: TEST_PATH,
|
||||
query: "test query",
|
||||
includeLinked: false,
|
||||
});
|
||||
|
||||
expect(mockSearchChunks).toHaveBeenCalledOnce();
|
||||
expect(mockSearchMultipleCollections).not.toHaveBeenCalled();
|
||||
expect(mockResolveLinkedCollections).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("calls searchMultipleCollections when includeLinked is true", async () => {
|
||||
await handleQueryTool("codebase_search", {
|
||||
projectPath: TEST_PATH,
|
||||
query: "test query",
|
||||
includeLinked: true,
|
||||
});
|
||||
|
||||
expect(mockSearchMultipleCollections).toHaveBeenCalledOnce();
|
||||
expect(mockResolveLinkedCollections).toHaveBeenCalledOnce();
|
||||
expect(mockSearchChunks).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("passes collections from resolveLinkedCollections to searchMultipleCollections", async () => {
|
||||
await handleQueryTool("codebase_search", {
|
||||
projectPath: TEST_PATH,
|
||||
query: "find me",
|
||||
includeLinked: true,
|
||||
limit: 5,
|
||||
});
|
||||
|
||||
expect(mockResolveLinkedCollections).toHaveBeenCalledWith(
|
||||
expect.stringContaining(TEST_PATH),
|
||||
);
|
||||
expect(mockSearchMultipleCollections).toHaveBeenCalledWith(
|
||||
[
|
||||
{ name: "codebase_abc123", label: "my-project" },
|
||||
{ name: "codebase_def456", label: "shared-lib" },
|
||||
],
|
||||
"find me",
|
||||
5,
|
||||
undefined, // fileFilter
|
||||
undefined, // languageFilter
|
||||
);
|
||||
});
|
||||
|
||||
it("includes project label in output when results have project field", async () => {
|
||||
mockSearchMultipleCollections.mockResolvedValueOnce([
|
||||
{
|
||||
filePath: "/proj/src/index.ts",
|
||||
relativePath: "src/index.ts",
|
||||
content: "console.log('hello')",
|
||||
startLine: 1,
|
||||
endLine: 5,
|
||||
language: "typescript",
|
||||
score: 0.5,
|
||||
project: "shared-lib",
|
||||
},
|
||||
]);
|
||||
|
||||
const output = await handleQueryTool("codebase_search", {
|
||||
projectPath: TEST_PATH,
|
||||
query: "hello",
|
||||
includeLinked: true,
|
||||
});
|
||||
|
||||
expect(output).toContain("[shared-lib]");
|
||||
expect(output).toContain("src/index.ts");
|
||||
});
|
||||
|
||||
it("does not include project tag when project field is absent", async () => {
|
||||
mockSearchChunks.mockResolvedValueOnce([
|
||||
{
|
||||
filePath: "/proj/src/index.ts",
|
||||
relativePath: "src/index.ts",
|
||||
content: "console.log('hello')",
|
||||
startLine: 1,
|
||||
endLine: 5,
|
||||
language: "typescript",
|
||||
score: 0.5,
|
||||
},
|
||||
]);
|
||||
|
||||
const output = await handleQueryTool("codebase_search", {
|
||||
projectPath: TEST_PATH,
|
||||
query: "hello",
|
||||
});
|
||||
|
||||
// Should have the file but no project tag brackets
|
||||
expect(output).toContain("src/index.ts");
|
||||
expect(output).not.toMatch(/\[.*-.*\]/); // no [project-name] tag
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user