__tests__ / toolsProvider.test.ts
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { mkdtempSync, rmSync } from "fs";
import { tmpdir } from "os";
import { join } from "path";
import { closeAllConnections } from "../src/db";
import { toolsProvider } from "../src/toolsProvider";
import type { ToolsProviderController } from "@lmstudio/sdk";
describe("toolsProvider", () => {
let tempDir: string;
let dbPath: string;
beforeEach(() => {
tempDir = mkdtempSync(join(tmpdir(), "brain-tools-"));
dbPath = join(tempDir, "test.db");
});
afterEach(() => {
closeAllConnections();
rmSync(tempDir, { recursive: true, force: true });
});
function fakeCtl(): ToolsProviderController {
return {
getPluginConfig: () => ({ get: (_key: string) => dbPath }),
} as unknown as ToolsProviderController;
}
it("registers all nine tools with the expected names", async () => {
const tools = await toolsProvider(fakeCtl());
expect(tools.map(t => t.name)).toEqual([
"create_entities",
"create_relations",
"add_observations",
"delete_entities",
"delete_observations",
"delete_relations",
"read_graph",
"search_nodes",
"open_nodes",
]);
});
it("round-trips create -> search -> read through the tool implementations", async () => {
const tools = await toolsProvider(fakeCtl());
const byName = new Map(tools.map(t => [t.name, t]));
await (byName.get("create_entities") as any).implementation({
entities: [{ name: "Alice", entityType: "person", observations: ["likes tea"] }],
});
const searchResult: any = await (byName.get("search_nodes") as any).implementation({
query: "tea",
});
expect(searchResult.entities.map((e: any) => e.name)).toEqual(["Alice"]);
const graph: any = await (byName.get("read_graph") as any).implementation({});
expect(graph.entities).toHaveLength(1);
});
it("reports not-found names when deleting a mix of existing and missing entities", async () => {
const tools = await toolsProvider(fakeCtl());
const byName = new Map(tools.map(t => [t.name, t]));
await (byName.get("create_entities") as any).implementation({
entities: [{ name: "Alice", entityType: "person", observations: [] }],
});
const message = await (byName.get("delete_entities") as any).implementation({
entityNames: ["Alice", "Ghost"],
});
expect(message).toContain("Not found: Ghost");
});
});
__tests__ / toolsProvider.test.ts
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { mkdtempSync, rmSync } from "fs";
import { tmpdir } from "os";
import { join } from "path";
import { closeAllConnections } from "../src/db";
import { toolsProvider } from "../src/toolsProvider";
import type { ToolsProviderController } from "@lmstudio/sdk";
describe("toolsProvider", () => {
let tempDir: string;
let dbPath: string;
beforeEach(() => {
tempDir = mkdtempSync(join(tmpdir(), "brain-tools-"));
dbPath = join(tempDir, "test.db");
});
afterEach(() => {
closeAllConnections();
rmSync(tempDir, { recursive: true, force: true });
});
function fakeCtl(): ToolsProviderController {
return {
getPluginConfig: () => ({ get: (_key: string) => dbPath }),
} as unknown as ToolsProviderController;
}
it("registers all nine tools with the expected names", async () => {
const tools = await toolsProvider(fakeCtl());
expect(tools.map(t => t.name)).toEqual([
"create_entities",
"create_relations",
"add_observations",
"delete_entities",
"delete_observations",
"delete_relations",
"read_graph",
"search_nodes",
"open_nodes",
]);
});
it("round-trips create -> search -> read through the tool implementations", async () => {
const tools = await toolsProvider(fakeCtl());
const byName = new Map(tools.map(t => [t.name, t]));
await (byName.get("create_entities") as any).implementation({
entities: [{ name: "Alice", entityType: "person", observations: ["likes tea"] }],
});
const searchResult: any = await (byName.get("search_nodes") as any).implementation({
query: "tea",
});
expect(searchResult.entities.map((e: any) => e.name)).toEqual(["Alice"]);
const graph: any = await (byName.get("read_graph") as any).implementation({});
expect(graph.entities).toHaveLength(1);
});
it("reports not-found names when deleting a mix of existing and missing entities", async () => {
const tools = await toolsProvider(fakeCtl());
const byName = new Map(tools.map(t => [t.name, t]));
await (byName.get("create_entities") as any).implementation({
entities: [{ name: "Alice", entityType: "person", observations: [] }],
});
const message = await (byName.get("delete_entities") as any).implementation({
entityNames: ["Alice", "Ghost"],
});
expect(message).toContain("Not found: Ghost");
});
});