feat: add official mem0 CLI (Python & TypeScript) (#4575)
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
"""Shared fixtures for mem0 CLI tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from mem0_cli.backend.base import Backend
|
||||
from mem0_cli.config import Mem0Config
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def isolate_config(tmp_path, monkeypatch):
|
||||
"""Redirect config to a temp directory so tests don't touch real config."""
|
||||
fake_config_dir = tmp_path / ".mem0"
|
||||
fake_config_file = fake_config_dir / "config.json"
|
||||
monkeypatch.setattr("mem0_cli.config.CONFIG_DIR", fake_config_dir)
|
||||
monkeypatch.setattr("mem0_cli.config.CONFIG_FILE", fake_config_file)
|
||||
# Also patch the commands that import config
|
||||
monkeypatch.setattr("mem0_cli.commands.config_cmd.CONFIG_DIR", fake_config_dir, raising=False)
|
||||
# Clear any MEM0 env vars
|
||||
for key in list(os.environ.keys()):
|
||||
if key.startswith("MEM0_"):
|
||||
monkeypatch.delenv(key, raising=False)
|
||||
return fake_config_dir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_backend():
|
||||
"""Return a mock backend with all methods stubbed."""
|
||||
backend = MagicMock(spec=Backend)
|
||||
|
||||
# Default return values
|
||||
backend.add.return_value = {
|
||||
"results": [
|
||||
{
|
||||
"id": "abc-123-def-456",
|
||||
"memory": "User prefers dark mode",
|
||||
"event": "ADD",
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
backend.search.return_value = [
|
||||
{
|
||||
"id": "abc-123-def-456",
|
||||
"memory": "User prefers dark mode",
|
||||
"score": 0.92,
|
||||
"created_at": "2026-02-15T10:30:00Z",
|
||||
"categories": ["preferences"],
|
||||
},
|
||||
{
|
||||
"id": "ghi-789-jkl-012",
|
||||
"memory": "User uses vim keybindings",
|
||||
"score": 0.78,
|
||||
"created_at": "2026-03-01T14:00:00Z",
|
||||
"categories": ["tools"],
|
||||
},
|
||||
]
|
||||
|
||||
backend.get.return_value = {
|
||||
"id": "abc-123-def-456",
|
||||
"memory": "User prefers dark mode",
|
||||
"created_at": "2026-02-15T10:30:00Z",
|
||||
"updated_at": "2026-02-20T08:00:00Z",
|
||||
"metadata": {"source": "onboarding"},
|
||||
"categories": ["preferences"],
|
||||
}
|
||||
|
||||
backend.list_memories.return_value = [
|
||||
{
|
||||
"id": "abc-123-def-456",
|
||||
"memory": "User prefers dark mode",
|
||||
"created_at": "2026-02-15T10:30:00Z",
|
||||
"categories": ["preferences"],
|
||||
},
|
||||
{
|
||||
"id": "ghi-789-jkl-012",
|
||||
"memory": "User uses vim keybindings",
|
||||
"created_at": "2026-03-01T14:00:00Z",
|
||||
"categories": ["tools"],
|
||||
},
|
||||
]
|
||||
|
||||
backend.update.return_value = {"id": "abc-123-def-456", "memory": "Updated memory"}
|
||||
backend.delete.return_value = {"status": "deleted"}
|
||||
backend.status.return_value = {
|
||||
"connected": True,
|
||||
"backend": "platform",
|
||||
"base_url": "https://api.mem0.ai",
|
||||
}
|
||||
backend.delete_entities.return_value = {"message": "Entity deleted"}
|
||||
backend.entities.return_value = [
|
||||
{"name": "alice", "count": 5},
|
||||
{"name": "bob", "count": 3},
|
||||
]
|
||||
|
||||
return backend
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_config():
|
||||
"""Return a sample config object."""
|
||||
config = Mem0Config()
|
||||
config.platform.api_key = "m0-test-key-12345678"
|
||||
config.platform.base_url = "https://api.mem0.ai"
|
||||
return config
|
||||
@@ -0,0 +1,54 @@
|
||||
"""Tests for branding and output helpers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from io import StringIO
|
||||
|
||||
from rich.console import Console
|
||||
|
||||
from mem0_cli.branding import print_banner, print_error, print_info, print_success, print_warning
|
||||
|
||||
|
||||
def _make_console() -> tuple[Console, StringIO]:
|
||||
buf = StringIO()
|
||||
return Console(file=buf, force_terminal=False, no_color=True, width=80), buf
|
||||
|
||||
|
||||
class TestBranding:
|
||||
def test_print_banner(self):
|
||||
console, buf = _make_console()
|
||||
print_banner(console)
|
||||
output = buf.getvalue()
|
||||
# Banner contains the mem0 ASCII art and tagline
|
||||
assert "Memory Layer" in output or "mem" in output.lower()
|
||||
|
||||
def test_print_success(self):
|
||||
console, buf = _make_console()
|
||||
print_success(console, "It worked!")
|
||||
output = buf.getvalue()
|
||||
assert "It worked!" in output
|
||||
|
||||
def test_print_error(self):
|
||||
console, buf = _make_console()
|
||||
print_error(console, "Something failed", hint="Try this fix")
|
||||
output = buf.getvalue()
|
||||
assert "Something failed" in output
|
||||
assert "Try this fix" in output
|
||||
|
||||
def test_print_error_no_hint(self):
|
||||
console, buf = _make_console()
|
||||
print_error(console, "Failed")
|
||||
output = buf.getvalue()
|
||||
assert "Failed" in output
|
||||
|
||||
def test_print_warning(self):
|
||||
console, buf = _make_console()
|
||||
print_warning(console, "Watch out")
|
||||
output = buf.getvalue()
|
||||
assert "Watch out" in output
|
||||
|
||||
def test_print_info(self):
|
||||
console, buf = _make_console()
|
||||
print_info(console, "FYI")
|
||||
output = buf.getvalue()
|
||||
assert "FYI" in output
|
||||
@@ -0,0 +1,238 @@
|
||||
"""Integration tests — invoke CLI as subprocess to test end-to-end.
|
||||
|
||||
These tests launch the CLI as a real subprocess, so they must manage
|
||||
environment isolation themselves (monkeypatch doesn't cross process
|
||||
boundaries).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def _run(
|
||||
args: list[str],
|
||||
env_override: dict | None = None,
|
||||
home_dir: str | None = None,
|
||||
) -> subprocess.CompletedProcess:
|
||||
"""Run mem0 CLI command and capture output.
|
||||
|
||||
Args:
|
||||
args: CLI arguments.
|
||||
env_override: Extra env vars to set.
|
||||
home_dir: If provided, set HOME to this path so the subprocess
|
||||
reads config from ``<home_dir>/.mem0/config.json`` instead
|
||||
of the user's real config. This is critical for tests that
|
||||
depend on a clean (no API key) or custom config state.
|
||||
"""
|
||||
env = os.environ.copy()
|
||||
# Strip all MEM0_ env vars so tests start clean
|
||||
for key in list(env.keys()):
|
||||
if key.startswith("MEM0_"):
|
||||
del env[key]
|
||||
if home_dir:
|
||||
env["HOME"] = home_dir
|
||||
if env_override:
|
||||
env.update(env_override)
|
||||
return subprocess.run(
|
||||
[sys.executable, "-m", "mem0_cli", *args],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env=env,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def clean_home(tmp_path):
|
||||
"""Return a temp directory to use as HOME, ensuring no ~/.mem0 exists."""
|
||||
return str(tmp_path)
|
||||
|
||||
|
||||
class TestCLIIntegration:
|
||||
"""Tests that only inspect help text / version — no config needed."""
|
||||
|
||||
def test_help(self):
|
||||
result = _run(["--help"])
|
||||
assert result.returncode == 0
|
||||
assert "mem0" in result.stdout
|
||||
assert "add" in result.stdout
|
||||
assert "search" in result.stdout
|
||||
|
||||
def test_version_flag(self):
|
||||
result = _run(["--version"])
|
||||
assert result.returncode == 0
|
||||
assert "0.1.0" in result.stdout
|
||||
|
||||
|
||||
def test_add_help(self):
|
||||
result = _run(["add", "--help"])
|
||||
assert result.returncode == 0
|
||||
assert "user-id" in result.stdout
|
||||
assert "messages" in result.stdout
|
||||
|
||||
def test_add_help_has_scope_panel(self):
|
||||
"""Verify rich_help_panel grouping shows in help output."""
|
||||
result = _run(["add", "--help"])
|
||||
assert result.returncode == 0
|
||||
assert "Scope" in result.stdout
|
||||
|
||||
def test_search_help(self):
|
||||
result = _run(["search", "--help"])
|
||||
assert result.returncode == 0
|
||||
assert "top-k" in result.stdout
|
||||
|
||||
def test_list_help(self):
|
||||
result = _run(["list", "--help"])
|
||||
assert result.returncode == 0
|
||||
assert "page-size" in result.stdout
|
||||
|
||||
def test_delete_help(self):
|
||||
result = _run(["delete", "--help"])
|
||||
assert result.returncode == 0
|
||||
assert "--all" in result.stdout
|
||||
assert "--entity" in result.stdout
|
||||
assert "--project" in result.stdout
|
||||
assert "--force" in result.stdout
|
||||
assert "--dry-run" in result.stdout
|
||||
|
||||
def test_entity_list_help(self):
|
||||
result = _run(["entity", "list", "--help"])
|
||||
assert result.returncode == 0
|
||||
assert "entity-type" in result.stdout.lower() or "entity_type" in result.stdout.lower()
|
||||
|
||||
def test_entity_delete_help(self):
|
||||
result = _run(["entity", "delete", "--help"])
|
||||
assert result.returncode == 0
|
||||
assert "--user-id" in result.stdout
|
||||
assert "--force" in result.stdout
|
||||
|
||||
def test_import_help(self):
|
||||
result = _run(["import", "--help"])
|
||||
assert result.returncode == 0
|
||||
|
||||
def test_no_args_shows_help(self):
|
||||
"""no_args_is_help=True makes Typer print help and exit with code 2."""
|
||||
result = _run([])
|
||||
# Typer returns exit code 2 for "no command given" — this is standard
|
||||
# Click/Typer behaviour and not an error.
|
||||
assert result.returncode in (0, 2)
|
||||
assert "Usage" in result.stdout
|
||||
|
||||
|
||||
class TestCLIIsolated:
|
||||
"""Tests that need a clean HOME to avoid reading the user's real config."""
|
||||
|
||||
def test_add_no_key_errors(self, clean_home):
|
||||
"""Without an API key, `mem0 add` must fail with a helpful message."""
|
||||
result = _run(
|
||||
["add", "test", "--user-id", "alice"],
|
||||
home_dir=clean_home,
|
||||
)
|
||||
assert result.returncode != 0
|
||||
combined = result.stderr + result.stdout
|
||||
assert "API key" in combined or "api" in combined.lower() or "Error" in combined
|
||||
|
||||
def test_search_no_key_errors(self, clean_home):
|
||||
"""Without an API key, `mem0 search` must fail."""
|
||||
result = _run(
|
||||
["search", "preferences", "--user-id", "alice"],
|
||||
home_dir=clean_home,
|
||||
)
|
||||
assert result.returncode != 0
|
||||
combined = result.stderr + result.stdout
|
||||
assert "API key" in combined or "Error" in combined
|
||||
|
||||
def test_list_no_key_errors(self, clean_home):
|
||||
"""Without an API key, `mem0 list` must fail."""
|
||||
result = _run(["list"], home_dir=clean_home)
|
||||
assert result.returncode != 0
|
||||
combined = result.stderr + result.stdout
|
||||
assert "API key" in combined or "Error" in combined
|
||||
|
||||
def test_delete_no_id_no_all_errors(self, clean_home):
|
||||
"""Delete without memory_id, --all, or --entity must fail."""
|
||||
result = _run(
|
||||
["delete", "--api-key", "m0-fake-key"],
|
||||
home_dir=clean_home,
|
||||
)
|
||||
assert result.returncode != 0
|
||||
combined = result.stderr + result.stdout
|
||||
assert "memory ID" in combined.lower() or "--all" in combined or "--entity" in combined or "Error" in combined
|
||||
|
||||
def test_config_show_clean(self, clean_home):
|
||||
"""config show with no config should still work."""
|
||||
result = _run(["config", "show"], home_dir=clean_home)
|
||||
assert result.returncode == 0
|
||||
assert "backend" in result.stdout.lower() or "platform" in result.stdout.lower()
|
||||
|
||||
def test_config_set_and_get_roundtrip(self, clean_home):
|
||||
"""config set then config get should return the set value."""
|
||||
_run(
|
||||
["config", "set", "defaults.user_id", "integration-test-user"],
|
||||
home_dir=clean_home,
|
||||
)
|
||||
result = _run(
|
||||
["config", "get", "defaults.user_id"],
|
||||
home_dir=clean_home,
|
||||
)
|
||||
assert result.returncode == 0
|
||||
assert "integration-test-user" in result.stdout
|
||||
|
||||
def test_import_nonexistent_file(self, clean_home):
|
||||
"""Importing a nonexistent file should fail gracefully."""
|
||||
result = _run(
|
||||
["import", "/nonexistent/file.json", "--api-key", "m0-fake"],
|
||||
home_dir=clean_home,
|
||||
)
|
||||
assert result.returncode != 0
|
||||
combined = result.stderr + result.stdout
|
||||
assert "Failed" in combined or "Error" in combined or "error" in combined
|
||||
|
||||
def test_add_no_content_errors(self, clean_home):
|
||||
"""add with no text/messages/file should fail."""
|
||||
result = _run(
|
||||
["add", "--user-id", "alice", "--api-key", "m0-fake"],
|
||||
home_dir=clean_home,
|
||||
)
|
||||
assert result.returncode != 0
|
||||
combined = result.stderr + result.stdout
|
||||
assert "No content" in combined or "Error" in combined
|
||||
|
||||
|
||||
class TestCLINewFeatures:
|
||||
"""Tests for MCP parity features: --graph, --limit, entities delete."""
|
||||
|
||||
def test_add_help_has_graph(self):
|
||||
result = _run(["add", "--help"])
|
||||
assert result.returncode == 0
|
||||
assert "--graph" in result.stdout
|
||||
|
||||
def test_search_help_has_graph_and_limit(self):
|
||||
result = _run(["search", "--help"])
|
||||
assert result.returncode == 0
|
||||
assert "--graph" in result.stdout
|
||||
assert "--limit" in result.stdout
|
||||
|
||||
def test_list_help_has_graph(self):
|
||||
result = _run(["list", "--help"])
|
||||
assert result.returncode == 0
|
||||
assert "--graph" in result.stdout
|
||||
|
||||
def test_delete_entity_via_delete_flag(self):
|
||||
"""delete --entity should appear in help output."""
|
||||
result = _run(["delete", "--help"])
|
||||
assert result.returncode == 0
|
||||
assert "--entity" in result.stdout
|
||||
|
||||
def test_entity_delete_has_scope_options(self):
|
||||
"""entity delete should expose scope options."""
|
||||
result = _run(["entity", "delete", "--help"])
|
||||
assert result.returncode == 0
|
||||
assert "--user-id" in result.stdout
|
||||
assert "--force" in result.stdout
|
||||
assert "--app-id" in result.stdout
|
||||
assert "--run-id" in result.stdout
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,240 @@
|
||||
"""Tests for configuration management."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
from mem0_cli.config import (
|
||||
Mem0Config,
|
||||
get_nested_value,
|
||||
load_config,
|
||||
redact_key,
|
||||
save_config,
|
||||
set_nested_value,
|
||||
)
|
||||
|
||||
|
||||
class TestRedactKey:
|
||||
def test_empty_key(self):
|
||||
assert redact_key("") == "(not set)"
|
||||
|
||||
def test_short_key(self):
|
||||
assert redact_key("abc") == "ab***"
|
||||
|
||||
def test_normal_key(self):
|
||||
result = redact_key("m0-abcdefgh12345678")
|
||||
assert result == "m0-a...5678"
|
||||
assert "abcdefgh" not in result
|
||||
|
||||
def test_exact_8_chars(self):
|
||||
# 8 chars is <= 8, so it gets the short redaction
|
||||
assert redact_key("12345678") == "12***"
|
||||
|
||||
|
||||
class TestConfig:
|
||||
def test_default_config(self):
|
||||
config = Mem0Config()
|
||||
assert config.platform.base_url == "https://api.mem0.ai"
|
||||
assert config.platform.api_key == ""
|
||||
|
||||
def test_save_and_load(self, isolate_config):
|
||||
config = Mem0Config()
|
||||
config.platform.api_key = "m0-test-key"
|
||||
|
||||
save_config(config)
|
||||
|
||||
loaded = load_config()
|
||||
assert loaded.platform.api_key == "m0-test-key"
|
||||
|
||||
def test_env_var_override(self, isolate_config, monkeypatch):
|
||||
config = Mem0Config()
|
||||
config.platform.api_key = "file-key"
|
||||
save_config(config)
|
||||
|
||||
monkeypatch.setenv("MEM0_API_KEY", "env-key")
|
||||
loaded = load_config()
|
||||
assert loaded.platform.api_key == "env-key"
|
||||
|
||||
def test_load_nonexistent_config(self, isolate_config):
|
||||
config = load_config()
|
||||
assert config.platform.api_key == ""
|
||||
|
||||
def test_config_file_permissions(self, isolate_config):
|
||||
config = Mem0Config()
|
||||
config.platform.api_key = "secret"
|
||||
save_config(config)
|
||||
|
||||
from mem0_cli.config import CONFIG_FILE
|
||||
|
||||
mode = os.stat(CONFIG_FILE).st_mode & 0o777
|
||||
assert mode == 0o600
|
||||
|
||||
def test_defaults_save_and_load(self, isolate_config):
|
||||
config = Mem0Config()
|
||||
config.defaults.user_id = "alice"
|
||||
config.defaults.agent_id = "support-bot"
|
||||
config.defaults.app_id = "my-app"
|
||||
config.defaults.run_id = "run-001"
|
||||
|
||||
save_config(config)
|
||||
loaded = load_config()
|
||||
|
||||
assert loaded.defaults.user_id == "alice"
|
||||
assert loaded.defaults.agent_id == "support-bot"
|
||||
assert loaded.defaults.app_id == "my-app"
|
||||
assert loaded.defaults.run_id == "run-001"
|
||||
|
||||
def test_defaults_env_var_override(self, isolate_config, monkeypatch):
|
||||
config = Mem0Config()
|
||||
config.defaults.user_id = "file-user"
|
||||
save_config(config)
|
||||
|
||||
monkeypatch.setenv("MEM0_USER_ID", "env-user")
|
||||
monkeypatch.setenv("MEM0_AGENT_ID", "env-agent")
|
||||
loaded = load_config()
|
||||
assert loaded.defaults.user_id == "env-user"
|
||||
assert loaded.defaults.agent_id == "env-agent"
|
||||
|
||||
def test_backward_compat_no_defaults_key(self, isolate_config):
|
||||
"""Old config files without 'defaults' key should load fine."""
|
||||
import json
|
||||
|
||||
from mem0_cli.config import CONFIG_FILE, ensure_config_dir
|
||||
|
||||
ensure_config_dir()
|
||||
# Write a config without the "defaults" key
|
||||
data = {
|
||||
"version": 1,
|
||||
"platform": {"api_key": "m0-test", "base_url": "https://api.mem0.ai"},
|
||||
}
|
||||
with open(CONFIG_FILE, "w") as f:
|
||||
json.dump(data, f)
|
||||
|
||||
loaded = load_config()
|
||||
assert loaded.platform.api_key == "m0-test"
|
||||
assert loaded.defaults.user_id == ""
|
||||
assert loaded.defaults.agent_id == ""
|
||||
|
||||
def test_default_config_has_empty_defaults(self):
|
||||
config = Mem0Config()
|
||||
assert config.defaults.user_id == ""
|
||||
assert config.defaults.agent_id == ""
|
||||
assert config.defaults.app_id == ""
|
||||
assert config.defaults.run_id == ""
|
||||
assert config.defaults.enable_graph is False
|
||||
|
||||
def test_enable_graph_save_and_load(self, isolate_config):
|
||||
config = Mem0Config()
|
||||
config.defaults.enable_graph = True
|
||||
save_config(config)
|
||||
loaded = load_config()
|
||||
assert loaded.defaults.enable_graph is True
|
||||
|
||||
def test_enable_graph_env_var_true(self, isolate_config, monkeypatch):
|
||||
monkeypatch.setenv("MEM0_ENABLE_GRAPH", "true")
|
||||
loaded = load_config()
|
||||
assert loaded.defaults.enable_graph is True
|
||||
|
||||
def test_enable_graph_env_var_false(self, isolate_config, monkeypatch):
|
||||
config = Mem0Config()
|
||||
config.defaults.enable_graph = True
|
||||
save_config(config)
|
||||
monkeypatch.setenv("MEM0_ENABLE_GRAPH", "false")
|
||||
loaded = load_config()
|
||||
assert loaded.defaults.enable_graph is False
|
||||
|
||||
def test_backward_compat_no_enable_graph_key(self, isolate_config):
|
||||
"""Old config files without 'enable_graph' key should default to False."""
|
||||
import json
|
||||
|
||||
from mem0_cli.config import CONFIG_FILE, ensure_config_dir
|
||||
|
||||
ensure_config_dir()
|
||||
data = {
|
||||
"version": 1,
|
||||
"defaults": {"user_id": "alice"},
|
||||
"platform": {"api_key": "m0-test", "base_url": "https://api.mem0.ai"},
|
||||
}
|
||||
with open(CONFIG_FILE, "w") as f:
|
||||
json.dump(data, f)
|
||||
|
||||
loaded = load_config()
|
||||
assert loaded.defaults.enable_graph is False
|
||||
assert loaded.defaults.user_id == "alice"
|
||||
|
||||
|
||||
class TestNestedAccess:
|
||||
def test_get_nested_value(self):
|
||||
config = Mem0Config()
|
||||
config.platform.api_key = "test-key"
|
||||
assert get_nested_value(config, "platform.api_key") == "test-key"
|
||||
|
||||
def test_get_nonexistent_key(self):
|
||||
config = Mem0Config()
|
||||
assert get_nested_value(config, "nonexistent.key") is None
|
||||
|
||||
def test_set_nested_value(self):
|
||||
config = Mem0Config()
|
||||
assert set_nested_value(config, "platform.api_key", "new-key")
|
||||
assert config.platform.api_key == "new-key"
|
||||
|
||||
def test_set_nonexistent_key(self):
|
||||
config = Mem0Config()
|
||||
assert set_nested_value(config, "nonexistent.key", "val") is False
|
||||
|
||||
def test_get_defaults_user_id(self):
|
||||
config = Mem0Config()
|
||||
config.defaults.user_id = "alice"
|
||||
assert get_nested_value(config, "defaults.user_id") == "alice"
|
||||
|
||||
def test_set_defaults_user_id(self):
|
||||
config = Mem0Config()
|
||||
assert set_nested_value(config, "defaults.user_id", "bob")
|
||||
assert config.defaults.user_id == "bob"
|
||||
|
||||
def test_set_defaults_enable_graph(self):
|
||||
config = Mem0Config()
|
||||
assert set_nested_value(config, "defaults.enable_graph", "true")
|
||||
assert config.defaults.enable_graph is True
|
||||
|
||||
|
||||
class TestResolveIds:
|
||||
def test_cli_flag_overrides_default(self):
|
||||
from mem0_cli.app import _resolve_ids
|
||||
|
||||
config = Mem0Config()
|
||||
config.defaults.user_id = "default-user"
|
||||
ids = _resolve_ids(
|
||||
config,
|
||||
user_id="cli-user",
|
||||
agent_id=None,
|
||||
)
|
||||
assert ids["user_id"] == "cli-user"
|
||||
|
||||
def test_default_used_when_flag_is_none(self):
|
||||
from mem0_cli.app import _resolve_ids
|
||||
|
||||
config = Mem0Config()
|
||||
config.defaults.user_id = "default-user"
|
||||
config.defaults.agent_id = "default-agent"
|
||||
ids = _resolve_ids(config, user_id=None, agent_id=None)
|
||||
assert ids["user_id"] == "default-user"
|
||||
assert ids["agent_id"] == "default-agent"
|
||||
|
||||
def test_none_when_neither_set(self):
|
||||
from mem0_cli.app import _resolve_ids
|
||||
|
||||
config = Mem0Config()
|
||||
ids = _resolve_ids(config, user_id=None, agent_id=None)
|
||||
assert ids["user_id"] is None
|
||||
assert ids["agent_id"] is None
|
||||
assert ids["app_id"] is None
|
||||
assert ids["run_id"] is None
|
||||
|
||||
def test_empty_string_treated_as_unset(self):
|
||||
from mem0_cli.app import _resolve_ids
|
||||
|
||||
config = Mem0Config()
|
||||
config.defaults.user_id = ""
|
||||
ids = _resolve_ids(config, user_id=None)
|
||||
assert ids["user_id"] is None
|
||||
@@ -0,0 +1,136 @@
|
||||
"""Tests for output formatting."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from io import StringIO
|
||||
|
||||
from rich.console import Console
|
||||
|
||||
from mem0_cli.output import (
|
||||
format_add_result,
|
||||
format_memories_table,
|
||||
format_memories_text,
|
||||
format_single_memory,
|
||||
)
|
||||
|
||||
|
||||
def _make_console() -> tuple[Console, StringIO]:
|
||||
buf = StringIO()
|
||||
return Console(file=buf, force_terminal=False, no_color=True, width=120, highlight=False), buf
|
||||
|
||||
|
||||
SAMPLE_MEMORIES = [
|
||||
{
|
||||
"id": "abc-123-def-456",
|
||||
"memory": "User prefers dark mode",
|
||||
"score": 0.92,
|
||||
"created_at": "2026-02-15T10:30:00Z",
|
||||
"categories": ["preferences"],
|
||||
},
|
||||
{
|
||||
"id": "ghi-789-jkl-012",
|
||||
"memory": "User uses vim keybindings",
|
||||
"score": 0.78,
|
||||
"created_at": "2026-03-01T14:00:00Z",
|
||||
"categories": ["tools"],
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
class TestTextFormat:
|
||||
def test_format_memories_text(self):
|
||||
console, buf = _make_console()
|
||||
format_memories_text(console, SAMPLE_MEMORIES)
|
||||
output = buf.getvalue()
|
||||
assert "Found 2 memories" in output
|
||||
assert "dark mode" in output
|
||||
assert "vim keybindings" in output
|
||||
assert "0.92" in output
|
||||
|
||||
def test_format_memories_text_empty(self):
|
||||
console, buf = _make_console()
|
||||
format_memories_text(console, [])
|
||||
output = buf.getvalue()
|
||||
assert "Found 0" in output
|
||||
|
||||
|
||||
class TestTableFormat:
|
||||
def test_format_memories_table(self):
|
||||
console, buf = _make_console()
|
||||
format_memories_table(console, SAMPLE_MEMORIES)
|
||||
output = buf.getvalue()
|
||||
assert "dark mode" in output
|
||||
assert "abc-123-" in output
|
||||
|
||||
def test_format_memories_table_empty(self):
|
||||
console, buf = _make_console()
|
||||
format_memories_table(console, [])
|
||||
output = buf.getvalue()
|
||||
# Should still render (empty table)
|
||||
assert "ID" in output
|
||||
|
||||
|
||||
class TestSingleMemory:
|
||||
def test_format_single_memory_text(self):
|
||||
console, buf = _make_console()
|
||||
mem = SAMPLE_MEMORIES[0]
|
||||
format_single_memory(console, mem, "text")
|
||||
output = buf.getvalue()
|
||||
assert "dark mode" in output
|
||||
assert "abc-123-def-456" in output
|
||||
|
||||
def test_format_single_memory_json(self):
|
||||
console, buf = _make_console()
|
||||
mem = SAMPLE_MEMORIES[0]
|
||||
format_single_memory(console, mem, "json")
|
||||
output = buf.getvalue()
|
||||
assert '"memory"' in output
|
||||
|
||||
|
||||
class TestAddResult:
|
||||
def test_format_add_result_text(self):
|
||||
console, buf = _make_console()
|
||||
result = {
|
||||
"results": [
|
||||
{"id": "abc-123-def-456", "memory": "User prefers dark mode", "event": "ADD"},
|
||||
]
|
||||
}
|
||||
format_add_result(console, result, "text")
|
||||
output = buf.getvalue()
|
||||
assert "dark mode" in output
|
||||
assert "Added" in output
|
||||
|
||||
def test_format_add_result_update_event(self):
|
||||
console, buf = _make_console()
|
||||
result = {
|
||||
"results": [
|
||||
{"id": "abc-123", "memory": "Updated pref", "event": "UPDATE"},
|
||||
]
|
||||
}
|
||||
format_add_result(console, result, "text")
|
||||
output = buf.getvalue()
|
||||
assert "Updated" in output
|
||||
|
||||
def test_format_add_result_noop(self):
|
||||
console, buf = _make_console()
|
||||
result = {
|
||||
"results": [
|
||||
{"id": "abc-123", "memory": "Same thing", "event": "NOOP"},
|
||||
]
|
||||
}
|
||||
format_add_result(console, result, "text")
|
||||
output = buf.getvalue()
|
||||
assert "No change" in output
|
||||
|
||||
def test_format_add_result_quiet(self):
|
||||
console, buf = _make_console()
|
||||
result = {"results": [{"id": "abc-123", "memory": "Quiet", "event": "ADD"}]}
|
||||
format_add_result(console, result, "quiet")
|
||||
output = buf.getvalue()
|
||||
assert output.strip() == ""
|
||||
|
||||
def test_format_add_result_empty(self):
|
||||
console, buf = _make_console()
|
||||
format_add_result(console, {"results": []}, "text")
|
||||
output = buf.getvalue()
|
||||
assert "No memories extracted" in output
|
||||
Reference in New Issue
Block a user