Skip to content

Local-first 🧠 memory for Hermes Agent that lives in your Obsidian vault and remembers project context. Nightly 😴 dream passes turn repeat corrections into confirmed preferences with measurable confidence. Adapters ship for Claude Code, Codex, and OpenClaw, with an MCP server for anything else.

TypeScript
2.2 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, setDefaultTimeout, test } from "bun:test";
import { mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

import { bootstrapBrain } from "../../src/core/brain/init.ts";
import { runCli } from "../helpers/run-cli.ts";
import { CLI_SPAWN_BUDGET_MS } from "../helpers/cli-timeout.ts";

// Tests here spawn processes; see tests/helpers/cli-timeout.ts.
setDefaultTimeout(CLI_SPAWN_BUDGET_MS);

let tmp: string;
let vault: string;
let configPath: string;

beforeEach(() => {
  tmp = mkdtempSync(join(tmpdir(), "o2b-brain-session-hook-"));
  vault = join(tmp, "vault");
  configPath = join(tmp, "config.yaml");
  bootstrapBrain(vault);
  writeFileSync(configPath, `vault: ${vault}\nagent_name: tester\n`);
});

afterEach(() => {
  rmSync(tmp, { recursive: true, force: true });
});

const env = () => ({ OPEN_SECOND_BRAIN_CONFIG: configPath });

describe("o2b brain session-hook", () => {
  test("captures stdin hook payload as JSON", async () => {
    const payload = {
      hook_event_name: "UserPromptSubmit",
      session_id: "cli-session",
      prompt: "@osb feedback negative topic=cli-hook principle=capture-cli-hook",
    };

    const result = await runCli(["brain", "session-hook", "--json"], {
      env: env(),
      stdin: JSON.stringify(payload),
    });

    expect(result.returncode).toBe(0);
    const body = JSON.parse(result.stdout);
    expect(body.event).toBe("UserPromptSubmit");
    expect(body.signals_created).toBe(1);
    expect(
      readdirSync(join(vault, "Brain", "inbox")).filter((name) => name.endsWith(".md")),
    ).toHaveLength(1);
  });

  test("dry-run reports marker capture without writing", async () => {
    const result = await runCli(["brain", "session-hook", "--dry-run", "--json"], {
      env: env(),
      stdin: JSON.stringify({
        hook_event_name: "UserPromptSubmit",
        prompt: "@osb feedback positive topic=dry-run principle=no-write",
      }),
    });

    expect(result.returncode).toBe(0);
    expect(JSON.parse(result.stdout).signals_created).toBe(0);
    expect(
      readdirSync(join(vault, "Brain", "inbox")).filter((name) => name.endsWith(".md")),
    ).toHaveLength(0);
  });
});