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
5.5 KB
Raw Blame
/**
 * CLI tests for `o2b search provider <add|list|show|remove>`.
 *
 * Forks the real `bun src/cli/main.ts` binary via `runCli`, asserting
 * exit codes, JSON shape, and the persisted registry file.
 */

import { afterEach, beforeEach, expect, setDefaultTimeout, test } from "bun:test";
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

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 config: string;

beforeEach(() => {
  tmp = mkdtempSync(join(tmpdir(), "o2b-provider-cli-"));
  vault = join(tmp, "vault");
  mkdirSync(vault, { recursive: true });
  // A legacy vault: it keeps its machine state. A vault with no `Brain/`
  // is store-only and keeps that state device-local instead.
  mkdirSync(join(vault, "Brain"), { recursive: true });
  config = join(tmp, "config.yaml");
  writeFileSync(config, `vault: "${vault}"\n`);
});

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

const env = () => ({ OPEN_SECOND_BRAIN_CONFIG: config });
const addArgs = [
  "search",
  "provider",
  "add",
  "nvidia-nim",
  "--base-url",
  "https://integrate.api.nvidia.com/v1",
  "--model",
  "nvidia/nv-embed-v1",
  "--env-key",
  "NIM_API_KEY",
];

test("provider add then list shows the registered profile", async () => {
  const add = await runCli(addArgs, { env: env() });
  expect(add.returncode).toBe(0);
  expect(existsSync(join(vault, "Brain", "search", "embedding-providers.json"))).toBe(true);

  const list = await runCli(["search", "provider", "list"], { env: env() });
  expect(list.returncode).toBe(0);
  expect(list.stdout).toContain("nvidia-nim");
});

test("provider list --json returns the profile array", async () => {
  await runCli(addArgs, { env: env() });
  const list = await runCli(["search", "provider", "list", "--json"], { env: env() });
  expect(list.returncode).toBe(0);
  const arr = JSON.parse(list.stdout);
  expect(Array.isArray(arr)).toBe(true);
  expect(arr[0]).toMatchObject({
    name: "nvidia-nim",
    baseUrl: "https://integrate.api.nvidia.com/v1",
    defaultModel: "nvidia/nv-embed-v1",
    envKey: "NIM_API_KEY",
  });
});

test("provider show prints a single profile", async () => {
  await runCli(addArgs, { env: env() });
  const show = await runCli(["search", "provider", "show", "nvidia-nim", "--json"], { env: env() });
  expect(show.returncode).toBe(0);
  expect(JSON.parse(show.stdout)).toMatchObject({ name: "nvidia-nim" });
});

test("provider remove deletes the profile", async () => {
  await runCli(addArgs, { env: env() });
  const rm = await runCli(["search", "provider", "remove", "nvidia-nim"], { env: env() });
  expect(rm.returncode).toBe(0);
  const list = await runCli(["search", "provider", "list", "--json"], { env: env() });
  expect(JSON.parse(list.stdout)).toEqual([]);
});

test("provider add with a reserved name fails with exit 2", async () => {
  const add = await runCli(
    [
      "search",
      "provider",
      "add",
      "local",
      "--base-url",
      "https://x/v1",
      "--model",
      "m",
      "--env-key",
      "K",
    ],
    { env: env() },
  );
  expect(add.returncode).toBe(2);
  expect(add.stderr).toContain("reserved");
});

test("provider show of an unknown name exits non-zero", async () => {
  const show = await runCli(["search", "provider", "show", "ghost"], { env: env() });
  expect(show.returncode).not.toBe(0);
});

test("provider presets lists the curated catalog without a vault", async () => {
  const presets = await runCli(["search", "provider", "presets", "--json"], { env: {} });
  expect(presets.returncode).toBe(0);
  const arr = JSON.parse(presets.stdout);
  expect(Array.isArray(arr)).toBe(true);
  expect(arr.length).toBeGreaterThan(0);
  expect(arr[0]).toHaveProperty("model");
  expect(arr[0]).toHaveProperty("dimension");
});

test("provider add without --model defaults to the recommended preset", async () => {
  const add = await runCli(
    ["search", "provider", "add", "defaulted", "--base-url", "https://x/v1", "--env-key", "K"],
    { env: env() },
  );
  expect(add.returncode).toBe(0);
  const show = await runCli(["search", "provider", "show", "defaulted", "--json"], { env: env() });
  const profile = JSON.parse(show.stdout);
  expect(profile.defaultModel).toBe("intfloat/multilingual-e5-small");
});

test("provider add with a custom --model persists it verbatim", async () => {
  await runCli(
    [
      "search",
      "provider",
      "add",
      "custom",
      "--base-url",
      "https://x/v1",
      "--model",
      "acme/custom-embed-9000",
      "--env-key",
      "K",
    ],
    { env: env() },
  );
  const show = await runCli(["search", "provider", "show", "custom", "--json"], { env: env() });
  expect(JSON.parse(show.stdout).defaultModel).toBe("acme/custom-embed-9000");
});

test("provider add accepts a comma-separated --env-key probe list", async () => {
  const add = await runCli(
    [
      "search",
      "provider",
      "add",
      "multi",
      "--base-url",
      "https://x/v1",
      "--model",
      "m",
      "--env-key",
      "PRIMARY_KEY,SECONDARY_KEY",
    ],
    { env: env() },
  );
  expect(add.returncode).toBe(0);
  const list = await runCli(["search", "provider", "list", "--json"], { env: env() });
  const arr = JSON.parse(list.stdout);
  expect(arr[0].envKey).toEqual(["PRIMARY_KEY", "SECONDARY_KEY"]);
});