File size: 4,690 Bytes
0ae3f27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
 * Integration tests — invoke CLI as subprocess to test end-to-end.
 */

import { describe, it, expect } from "vitest";
import { execSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

function run(
  args: string[],
  opts: { home?: string; env?: Record<string, string> } = {},
): { stdout: string; stderr: string; exitCode: number } {
  const env = { ...process.env };
  // Strip MEM0_ env vars
  for (const key of Object.keys(env)) {
    if (key.startsWith("MEM0_")) delete env[key];
  }
  if (opts.home) env.HOME = opts.home;
  if (opts.env) Object.assign(env, opts.env);

  try {
    const stdout = execSync(
      `npx tsx src/index.ts ${args.join(" ")}`,
      { cwd: path.join(__dirname, ".."), env, encoding: "utf-8", timeout: 15000 },
    );
    return { stdout, stderr: "", exitCode: 0 };
  } catch (e: any) {
    return {
      stdout: e.stdout ?? "",
      stderr: e.stderr ?? "",
      exitCode: e.status ?? 1,
    };
  }
}

describe("CLI Integration — help and version", () => {
  it("shows help with --help", () => {
    const result = run(["--help"]);
    expect(result.exitCode).toBe(0);
    expect(result.stdout).toContain("mem0");
    expect(result.stdout).toContain("add");
    expect(result.stdout).toContain("search");
  });

  it("help --json produces valid JSON", () => {
    const result = run(["help", "--json"]);
    expect(result.exitCode).toBe(0);
    const parsed = JSON.parse(result.stdout);
    // spec may have cli.name or top-level name
    const name = parsed.name ?? parsed.cli?.name;
    expect(name).toBe("mem0");
  });

  it("shows add help", () => {
    const result = run(["add", "--help"]);
    expect(result.exitCode).toBe(0);
    expect(result.stdout).toContain("user-id");
    expect(result.stdout).toContain("messages");
  });

  it("shows search help", () => {
    const result = run(["search", "--help"]);
    expect(result.exitCode).toBe(0);
    expect(result.stdout).toContain("top-k");
  });

  it("shows list help", () => {
    const result = run(["list", "--help"]);
    expect(result.exitCode).toBe(0);
    expect(result.stdout).toContain("page-size");
  });

  it("shows delete help with --all, --entity, --project", () => {
    const result = run(["delete", "--help"]);
    expect(result.exitCode).toBe(0);
    expect(result.stdout).toContain("--all");
    expect(result.stdout).toContain("--entity");
    expect(result.stdout).toContain("--project");
    expect(result.stdout).toContain("--force");
    expect(result.stdout.toLowerCase()).toContain("memory");
  });

  it("delete with no args errors", () => {
    const result = run(["delete"]);
    expect(result.exitCode).not.toBe(0);
    const combined = result.stdout + result.stderr;
    expect(combined).toContain("--all");
  });

  it("shows entity list help", () => {
    const result = run(["entity", "list", "--help"]);
    expect(result.exitCode).toBe(0);
    expect(result.stdout.toLowerCase()).toContain("entitytype");
  });

  it("shows entity delete help", () => {
    const result = run(["entity", "delete", "--help"]);
    expect(result.exitCode).toBe(0);
    expect(result.stdout).toContain("--user-id");
    expect(result.stdout).toContain("--force");
  });

  it("shows import help", () => {
    const result = run(["import", "--help"]);
    expect(result.exitCode).toBe(0);
  });

  it("add help has --graph flag", () => {
    const result = run(["add", "--help"]);
    expect(result.exitCode).toBe(0);
    expect(result.stdout).toContain("--graph");
  });

  it("search help has --graph flag", () => {
    const result = run(["search", "--help"]);
    expect(result.exitCode).toBe(0);
    expect(result.stdout).toContain("--graph");
  });

  it("list help has --graph flag", () => {
    const result = run(["list", "--help"]);
    expect(result.exitCode).toBe(0);
    expect(result.stdout).toContain("--graph");
  });
});

describe("CLI Integration — isolated (clean home)", () => {
  function cleanHome(): string {
    return fs.mkdtempSync(path.join(os.tmpdir(), "mem0-test-"));
  }

  it("add without API key errors", () => {
    const home = cleanHome();
    const result = run(["add", "test", "--user-id", "alice"], { home });
    expect(result.exitCode).not.toBe(0);
    const combined = result.stdout + result.stderr;
    expect(combined.toLowerCase()).toMatch(/api.key|error/i);
    fs.rmSync(home, { recursive: true, force: true });
  });

  it("config show works with clean home", () => {
    const home = cleanHome();
    const result = run(["config", "show"], { home });
    expect(result.exitCode).toBe(0);
    fs.rmSync(home, { recursive: true, force: true });
  });
});