Testing
Test conventions and practices for the-brain
Test Runner
bun test # All tests
bun test path/to/file # Single file
bun test --coverage # With coverage
bun test --watch # Watch modeConventions
- Tests live in
src/__tests__/alongside source files bun:testAPI:describe,test,expect- Use
process.env.HOMEoverride for test isolation - Integration tests use real filesystem under temp dirs
- No real API keys or paid tokens in tests
- When fixing a bug, write a regression test first
Coverage Target
80% line coverage across all packages.
Current: 85.86% line coverage, 740 tests, 0 failures across 54 files (2026-05-06).
Packages at 100%: backend-resolver, cleaner-default, context, storage-sqlite, storage-libsql, mcp-server/resources.
bun test --coverage # All
bun test --coverage packages/core # Core only
bun test --coverage packages/plugin-* # Plugins
bun test --coverage apps/cli # CLIKey Test Files
| File | What It Tests |
|---|---|
packages/core/src/__tests__/ | BrainDB, hooks, plugins, layers |
packages/plugin-spm-curator/src/__tests__/ | SPM evaluation, TF-IDF, promotion |
packages/plugin-graph-memory/src/__tests__/ | Graph nodes, corrections, context |
apps/cli/src/__tests__/ | Pipeline E2E, daemon, commands |
Test Harness
TestHarness provides an isolated test environment with in-memory DB, hooks, and plugins.
import { TestHarness } from "@the-brain/core";
const harness = new TestHarness({
projectName: "test-project", // Optional: isolated project
});
await harness.start();
// Inject interactions
await harness.injectInteraction({
prompt: "Write a React component",
response: "Here is a component...",
source: "test",
});
// Inject multiple
await harness.injectInteractions([
{ prompt: "Fix auth", response: "Done", source: "test" },
{ prompt: "Add tests", response: "Added", source: "test" },
]);
// Set memories directly
await harness.setMemories([
{ id: "m1", layer: MemoryLayer.DEEP, content: "...", timestamp: Date.now(), source: "test" },
]);
// Run SPM evaluation
const result = await harness.runSpemEvaluation();
// Run full consolidation pipeline
await harness.runConsolidation();
// Access DB + stats
const stats = await harness.db.getStats();
expect(stats.memories).toBe(3);
await harness.stop(); // Cleans up temp filesHarnessOptions
| Option | Default | Description |
|---|---|---|
projectName | random UUID | Isolated project name |
disableMlx | true | Skip MLX training calls |
enableWiki | false | Enable auto-wiki output |
HarnessState
interface HarnessState {
db: BrainDB;
hooks: ReturnType<typeof createHookSystem>;
pluginManager: PluginManager;
projectName: string;
tempDir: string;
}