add changes to docs and test

This commit is contained in:
Hande committed 2026-01-29 21:55:47 +01:00
1 parent c71eca5237
commit e8b4540c25
4 files changed
+75 -562

No files matched your search

-227
View File
@@ -1,227 +0,0 @@
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { CogneeClient } from "./cognee-client.js";
import { request } from "undici";
vi.mock("undici", async () => {
const actual = await vi.importActual<typeof import("undici")>("undici");
return {
...actual,
request: vi.fn(),
};
});
describe("CogneeClient", () => {
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
vi.restoreAllMocks();
});
describe("add", () => {
it("should add data successfully", async () => {
const mockResponse = {
statusCode: 200,
body: {
json: vi.fn().mockResolvedValue({
dataset_id: "test-dataset-id",
dataset_name: "test-dataset",
message: "Data added successfully",
}),
text: vi.fn(),
},
};
vi.mocked(request).mockResolvedValue(mockResponse as any);
const client = new CogneeClient({
baseUrl: "http://localhost:8000",
apiKey: "test-key",
});
const result = await client.add({
data: "Test data",
datasetName: "test-dataset",
});
expect(result).toEqual({
datasetId: "test-dataset-id",
datasetName: "test-dataset",
message: "Data added successfully",
});
expect(request).toHaveBeenCalledWith(
"http://localhost:8000/api/v1/add",
expect.objectContaining({
method: "POST",
headers: expect.objectContaining({
Authorization: "Bearer test-key",
"X-Api-Key": "test-key",
}),
}),
);
});
it("should handle errors", async () => {
const mockResponse = {
statusCode: 500,
body: {
text: vi.fn().mockResolvedValue("Internal server error"),
},
};
vi.mocked(request).mockResolvedValue(mockResponse as any);
const client = new CogneeClient();
await expect(
client.add({
data: "Test data",
datasetName: "test-dataset",
}),
).rejects.toThrow(
"Cognee add request failed: Cognee add failed with status 500: Internal server error",
);
});
});
describe("cognify", () => {
it("should run cognify successfully", async () => {
const mockResponse = {
statusCode: 200,
body: {
json: vi.fn().mockResolvedValue({
status: "success",
message: "Cognify completed",
}),
text: vi.fn(),
},
};
vi.mocked(request).mockResolvedValue(mockResponse as any);
const client = new CogneeClient({
baseUrl: "http://localhost:8000",
});
const result = await client.cognify({
datasetIds: ["dataset-1"],
});
expect(result).toEqual({
status: "success",
message: "Cognify completed",
});
});
});
describe("search", () => {
it("should search successfully", async () => {
const mockResponse = {
statusCode: 200,
body: {
json: vi.fn().mockResolvedValue({
results: [
{
id: "result-1",
text: "Test result",
score: 0.9,
metadata: { path: "test.md" },
},
],
query: "test query",
search_type: "GRAPH_COMPLETION",
}),
text: vi.fn(),
},
};
vi.mocked(request).mockResolvedValue(mockResponse as any);
const client = new CogneeClient();
const result = await client.search({
queryText: "test query",
searchType: "GRAPH_COMPLETION",
});
expect(result.results).toHaveLength(1);
expect(result.results[0]).toEqual({
id: "result-1",
text: "Test result",
score: 0.9,
metadata: { path: "test.md" },
});
expect(result.query).toBe("test query");
});
it("should use default search type", async () => {
const mockResponse = {
statusCode: 200,
body: {
json: vi.fn().mockResolvedValue({
results: [],
query: "test",
search_type: "GRAPH_COMPLETION",
}),
text: vi.fn(),
},
};
vi.mocked(request).mockResolvedValue(mockResponse as any);
const client = new CogneeClient();
await client.search({ queryText: "test" });
expect(request).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
body: expect.stringContaining('"searchType":"GRAPH_COMPLETION"'),
}),
);
});
});
describe("status", () => {
it("should get status successfully", async () => {
const mockResponse = {
statusCode: 200,
body: {
json: vi.fn().mockResolvedValue({ status: "healthy" }),
text: vi.fn(),
},
};
vi.mocked(request).mockResolvedValue(mockResponse as any);
const client = new CogneeClient();
const result = await client.status();
expect(result).toEqual({
status: "healthy",
});
});
});
describe("healthCheck", () => {
it("should return true when status is successful", async () => {
const mockResponse = {
statusCode: 200,
body: {
json: vi.fn().mockResolvedValue({ status: "healthy" }),
text: vi.fn(),
},
};
vi.mocked(request).mockResolvedValue(mockResponse as any);
const client = new CogneeClient();
const result = await client.healthCheck();
expect(result).toBe(true);
});
it("should return false when status fails", async () => {
vi.mocked(request).mockRejectedValue(new Error("Connection failed"));
const client = new CogneeClient();
const result = await client.healthCheck();
expect(result).toBe(false);
});
});
});
+58 -124
View File
@@ -1,27 +1,34 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { CogneeMemoryProvider } from "./cognee-provider.js";
import type { ClawdbotConfig } from "../config/config.js";
const searchMock = vi.fn();
vi.mock("./cognee-client.js", () => {
class CogneeClient {
healthCheck = vi.fn().mockResolvedValue(true);
add = vi.fn().mockResolvedValue({
datasetId: "test-dataset-id",
datasetName: "test-dataset",
message: "Success",
});
cognify = vi.fn().mockResolvedValue({
status: "success",
message: "Cognify completed",
});
search = vi.fn().mockResolvedValue({
search = searchMock;
}
return { CogneeClient };
});
describe("CogneeMemoryProvider", () => {
it("maps search results into memory snippets", async () => {
const mockConfig: ClawdbotConfig = {
agents: {
defaults: {
workspace: "/tmp/test-workspace",
},
},
};
searchMock.mockResolvedValue({
results: [
{
id: "result-1",
text: "Test result text",
text: "A".repeat(800),
score: 0.85,
metadata: {
path: "test.md",
path: "memory/test.md",
source: "memory",
},
},
@@ -29,124 +36,51 @@ vi.mock("./cognee-client.js", () => {
query: "test query",
searchType: "GRAPH_COMPLETION",
});
status = vi.fn().mockResolvedValue({
status: "healthy",
version: "1.0.0",
datasets: [
const provider = new CogneeMemoryProvider(mockConfig, "test-agent", ["memory"]);
const results = await provider.search("test query");
expect(results).toHaveLength(1);
expect(results[0]).toMatchObject({
path: "memory/test.md",
source: "memory",
score: 0.85,
});
expect(results[0].snippet.length).toBeGreaterThan(700);
expect(results[0].snippet.endsWith("...")).toBe(true);
});
it("defaults missing metadata to safe values", async () => {
searchMock.mockResolvedValue({
results: [
{
id: "test-dataset-id",
name: "clawdbot",
documentCount: 5,
id: "result-2",
text: "Short result",
score: 0.2,
},
],
query: "missing metadata",
searchType: "GRAPH_COMPLETION",
});
}
return { CogneeClient };
});
vi.mock("./internal.js", () => ({
listMemoryFiles: vi.fn().mockResolvedValue([]),
buildFileEntry: vi.fn(),
hashText: vi.fn().mockReturnValue("test-hash"),
}));
vi.mock("node:fs/promises", () => ({
default: {
readdir: vi.fn().mockResolvedValue([]),
readFile: vi.fn().mockResolvedValue("Test file content"),
stat: vi.fn().mockResolvedValue({ mtimeMs: 1234567890, size: 100 }),
},
}));
describe("CogneeMemoryProvider", () => {
const mockConfig: ClawdbotConfig = {
agents: {
defaults: {
workspace: "/tmp/test-workspace",
const mockConfig: ClawdbotConfig = {
agents: {
defaults: {
workspace: "/tmp/test-workspace",
},
},
},
};
};
const provider = new CogneeMemoryProvider(mockConfig, "test-agent", ["memory"]);
beforeEach(() => {
vi.clearAllMocks();
});
const results = await provider.search("missing metadata");
describe("constructor", () => {
it("should initialize with default configuration", () => {
const provider = new CogneeMemoryProvider(mockConfig, "test-agent", ["memory"]);
expect(provider).toBeDefined();
});
it("should initialize with custom configuration", () => {
const provider = new CogneeMemoryProvider(mockConfig, "test-agent", ["memory"], {
baseUrl: "http://custom:8000",
apiKey: "custom-key",
datasetName: "custom-dataset",
searchType: "chunks",
maxResults: 10,
});
expect(provider).toBeDefined();
});
});
describe("healthCheck", () => {
it("should perform health check", async () => {
const provider = new CogneeMemoryProvider(mockConfig, "test-agent", ["memory"]);
const healthy = await provider.healthCheck();
expect(healthy).toBe(true);
});
});
describe("search", () => {
it("should search and transform results", async () => {
const provider = new CogneeMemoryProvider(mockConfig, "test-agent", ["memory"]);
const results = await provider.search("test query");
expect(results).toHaveLength(1);
expect(results[0]).toMatchObject({
path: "test.md",
source: "memory",
score: 0.85,
snippet: "Test result text",
});
});
it("should respect maxResults setting", async () => {
const provider = new CogneeMemoryProvider(mockConfig, "test-agent", ["memory"], {
maxResults: 5,
});
const results = await provider.search("test query");
expect(results.length).toBeLessThanOrEqual(5);
});
});
describe("cognify", () => {
it("should run cognify", async () => {
const provider = new CogneeMemoryProvider(mockConfig, "test-agent", ["memory"]);
await expect(provider.cognify()).resolves.not.toThrow();
});
});
describe("getStatus", () => {
it("should return status information", async () => {
const provider = new CogneeMemoryProvider(mockConfig, "test-agent", ["memory"]);
const status = await provider.getStatus();
expect(status).toMatchObject({
connected: true,
datasetName: "clawdbot",
syncedFileCount: 0,
});
expect(results).toHaveLength(1);
expect(results[0]).toMatchObject({
path: "unknown",
source: "memory",
score: 0.2,
snippet: "Short result",
});
});
});