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
6.3 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

import { writePreference, type WritePreferenceInput } from "../../../src/core/brain/preference.ts";
import { buildSchemaReport } from "../../../src/core/brain/schema-report.ts";
import { writeSignal } from "../../../src/core/brain/signal.ts";
import {
  BRAIN_CONFIDENCE,
  BRAIN_PREFERENCE_STATUS,
  BRAIN_SIGNAL_SIGN,
} from "../../../src/core/brain/types.ts";

/** Schema tokens used by the cases below. */
const RESEARCH = "research";
const EXTERNAL = "external";
const DECISION = "decision";
const OBSERVATION = "observation";
const MILESTONE = "milestone";
const UNUSED_PAGE = "unused-page";
const UNUSED_EVENT = "unused-event";

let vault: string;

beforeEach(() => {
  vault = mkdtempSync(join(tmpdir(), "o2b-schema-report-"));
  mkdirSync(join(vault, "Brain", "preferences"), { recursive: true });
  mkdirSync(join(vault, "Brain", "retired"), { recursive: true });
  mkdirSync(join(vault, "Brain", "inbox"), { recursive: true });
  mkdirSync(join(vault, "Brain", "inbox", "processed"), { recursive: true });
  mkdirSync(join(vault, "Brain", "log"), { recursive: true });
});

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

function writeBrainConfig(body: string): void {
  writeFileSync(join(vault, "Brain", "_brain.yaml"), body, "utf8");
}

function basePref(
  slug: string,
  overrides: Partial<WritePreferenceInput> = {},
): WritePreferenceInput {
  return {
    slug,
    topic: "research",
    principle: `Principle for ${slug}`,
    created_at: "2026-05-30T12:00:00Z",
    unconfirmed_until: "2026-06-06T12:00:00Z",
    status: BRAIN_PREFERENCE_STATUS.confirmed,
    evidenced_by: [],
    confirmed_at: "2026-05-30T13:00:00Z",
    confidence: BRAIN_CONFIDENCE.low,
    pinned: false,
    ...overrides,
  };
}

describe("buildSchemaReport", () => {
  test("returns resolved vocabulary, usage counts, and lint findings", () => {
    writeBrainConfig(
      [
        "schema_version: 1",
        "schema:",
        "  preference_types: [research, decision]",
        "  signal_types: [observation]",
        "  page_types: [paper, unused-page]",
        "  log_event_kinds: [milestone, unused-event]",
      ].join("\n"),
    );
    writePreference(
      vault,
      basePref("research-pref", {
        schema_type: "research",
      }),
    );
    writeSignal(vault, {
      topic: "research",
      signal: BRAIN_SIGNAL_SIGN.positive,
      agent: "tester",
      principle: "unknown signal schema type",
      created_at: "2026-05-30T12:00:00Z",
      date: "2026-05-30",
      slug: "external",
      schema_type: "external",
    });
    mkdirSync(join(vault, "Papers"), { recursive: true });
    writeFileSync(
      join(vault, "Papers", "paper.md"),
      ["---", "title: Paper", "schema_type: paper", "---", "", "Body"].join("\n"),
      "utf8",
    );
    writeFileSync(
      join(vault, "Brain", "log", "2026-05-30.md"),
      [
        "# Brain log — 2026-05-30",
        "",
        "## 12:00:00 milestone",
        "- text: shipped schema report",
      ].join("\n"),
      "utf8",
    );

    const report = buildSchemaReport(vault);

    expect(report.vocabulary.preference_types).toEqual(["preference", "research", "decision"]);
    expect(report.vocabulary.signal_types).toEqual(["feedback", "observation"]);
    expect(report.usage.preference_types).toEqual([{ token: RESEARCH, count: 1 }]);
    expect(report.usage.signal_types).toEqual([{ token: EXTERNAL, count: 1 }]);
    expect(report.usage.page_types).toEqual([{ token: "paper", count: 1 }]);
    expect(report.usage.log_event_kinds).toEqual([{ token: MILESTONE, count: 1 }]);
    expect(report.findings).toContainEqual({
      kind: "unknown-token",
      category: "signal_types",
      token: EXTERNAL,
      path: "Brain/inbox/sig-2026-05-30-external.md",
    });
    expect(report.findings).toContainEqual({
      kind: "unused-declaration",
      category: "preference_types",
      token: DECISION,
    });
    expect(report.findings).toContainEqual({
      kind: "unused-declaration",
      category: "signal_types",
      token: OBSERVATION,
    });
    expect(report.findings).toContainEqual({
      kind: "unused-declaration",
      category: "page_types",
      token: UNUSED_PAGE,
    });
    expect(report.findings).toContainEqual({
      kind: "unused-declaration",
      category: "log_event_kinds",
      token: UNUSED_EVENT,
    });
  });

  /**
   * a-label-is-not-a-boundary, U12: one malformed artifact used to cost
   * the caller the whole report - the parse escaped `buildSchemaReport`
   * and every finding in the vault went with it.
   */
  test("a malformed artifact becomes a finding and the rest of the report survives", () => {
    writeBrainConfig(
      ["schema_version: 1", "schema:", "  preference_types: [research, decision]"].join("\n"),
    );
    writePreference(vault, basePref("research-pref", { schema_type: "research" }));
    // Missing `retired_at`: `parseRetired` raises with the absolute path.
    writeFileSync(
      join(vault, "Brain", "retired", "ret-broken.md"),
      ["---", "kind: brain-retired", "_status: retired", "id: ret-broken", "---", "", "x"].join(
        "\n",
      ),
      "utf8",
    );

    const report = buildSchemaReport(vault);

    expect(report.findings).toContainEqual({
      kind: "unreadable-artifact",
      category: "preference_types",
      path: "Brain/retired/ret-broken.md",
      detail: "preference missing field: retired_at",
    });
    // The readable half of the vault is still measured and still linted.
    expect(report.usage.preference_types).toEqual([{ token: RESEARCH, count: 1 }]);
    expect(report.findings).toContainEqual({
      kind: "unused-declaration",
      category: "preference_types",
      token: DECISION,
    });
  });

  test("default vault has built-in vocabulary and no findings", () => {
    writeBrainConfig("schema_version: 1\n");

    const report = buildSchemaReport(vault);

    expect(report.vocabulary.preference_types).toEqual(["preference"]);
    expect(report.usage.preference_types).toEqual([]);
    expect(report.findings).toEqual([]);
    expect(Object.isFrozen(report)).toBe(true);
    expect(Object.isFrozen(report.findings)).toBe(true);
  });
});