agentic_huge_data_base / wiki
页面 Mem0 · 14.2 测试·DeepWiki 中文全文译文

14.2 · 测试(Testing)

长期记忆与上下文管理 · 本章是 Mem0 DeepWiki 中文译文的独立章节页,保留原始链接、源码锚点、模块标签和章节层级。

项目Mem0 章节14.2 状态全文译文 模块测试、发布与运维、接口与服务契约、智能体运行时、工作流与编排
源码线索
  • mem0-ts/jest.integration.config.js
  • mem0-ts/src/client/tests/integration/batch.test.ts
  • mem0-ts/src/client/tests/integration/crud.test.ts
  • mem0-ts/src/client/tests/integration/global-setup.ts
  • mem0-ts/src/client/tests/integration/global-teardown.ts
  • mem0-ts/src/client/tests/integration/helpers.ts
  • mem0-ts/src/client/tests/integration/initialization.test.ts
  • mem0-ts/src/client/tests/integration/management.test.ts
  • mem0-ts/src/client/tests/integration/search.test.ts
  • mem0-ts/src/client/tests/memoryClient.crud.test.ts
模块标签
  • 测试、发布与运维
  • 接口与服务契约
  • 智能体运行时
  • 工作流与编排
  • 检索、召回与索引

中文译文

测试(中文译文)

原始 DeepWiki 页面:https://deepwiki.com/mem0ai/mem0/14.2-testing
翻译时间:2026-05-27T08:45:07.497Z
翻译模型:deepseek-chat
原文字符数:9078
项目:Mem0 (mem0)

---

测试

相关源文件

以下文件被用作生成此 Wiki 页面的上下文:

  • mem0-ts/jest.integration.config.js
  • mem0-ts/src/client/tests/integration/batch.test.ts
  • mem0-ts/src/client/tests/integration/crud.test.ts
  • mem0-ts/src/client/tests/integration/global-setup.ts
  • mem0-ts/src/client/tests/integration/global-teardown.ts
  • mem0-ts/src/client/tests/integration/helpers.ts
  • mem0-ts/src/client/tests/integration/initialization.test.ts
  • mem0-ts/src/client/tests/integration/management.test.ts
  • mem0-ts/src/client/tests/integration/search.test.ts
  • mem0-ts/src/client/tests/memoryClient.crud.test.ts
  • mem0-ts/src/client/tests/memoryClient.project.test.ts
  • mem0-ts/src/client/tests/memoryClient.search.test.ts
  • mem0-ts/src/oss/tests/dimension-autodetect.test.ts
  • mem0-ts/src/oss/tests/memory.add.test.ts
  • mem0-ts/src/oss/tests/memory.crud.test.ts
  • mem0-ts/src/oss/tests/memory.init.test.ts
  • mem0-ts/src/oss/tests/memory.validation.test.ts
  • mem0-ts/src/oss/tests/tsup-externals.test.ts
  • mem0/__init__.py
  • mem0/configs/prompts.py
  • mem0/memory/main.py
  • mem0/memory/storage.py
  • mem0/memory/utils.py
  • tests/configs/test_prompts.py
  • tests/memory/test_main.py
  • tests/test_chatty_llm_parsing.py
  • tests/test_main.py
  • tests/test_memory.py
  • tests/test_proxy.py

本文档涵盖了 Mem0 代码库中使用的测试基础设施、工具和流程。Mem0 的测试分为三个主要领域:核心 Python mem0 包、TypeScript SDK(mem0-ts)以及托管平台的集成测试。

测试框架与工具

Mem0 使用 pytest 作为 Python 的主要测试框架,使用 jest 作为 TypeScript 的测试框架,并辅以其他工具进行代码质量和覆盖率分析。

核心测试栈
工具用途配置
pytestPython 测试执行框架所有 Python 测试套件的默认运行器
jestTypeScript 测试执行框架用于 mem0-ts 单元测试和集成测试 mem0-ts/jest.integration.config.js
ruffPython 代码检查和格式化CI 检查和预提交钩子 .github/workflows/ci.yml:112-115
hatch环境管理多版本 Python 测试(3.10-3.12).github/workflows/ci.yml:83-99
codecov覆盖率报告集成用于覆盖率分析 .github/workflows/ci.yml:151-157

来源: .github/workflows/ci.yml:1-117mem0-ts/jest.integration.config.js

测试组织

仓库按语言和部署模型(OSS 与平台)对测试进行组织。

graph TB
    subgraph "仓库根目录"
        PythonTests["tests/"]
        TSTests["mem0-ts/src/"]
        Mem0Src["mem0/"]
    end

    subgraph "Python 测试套件"
        PythonTests --> P_Main["test_main.py"]
        PythonTests --> P_Memory["test_memory.py"]
        PythonTests --> P_Proxy["test_proxy.py"]
    end

    subgraph "TypeScript 测试套件"
        TSTests --> TS_OSS["oss/tests/"]
        TSTests --> TS_Client["client/tests/integration/"]
    end

    P_Main -.测试.-> Mem0Src
    TS_OSS -.测试.-> TSTests
    TS_Client -.测试.-> TSTests

    style PythonTests fill:#f9f9f9
    style TSTests fill:#f9f9f9

来源: tests/test_main.py:1-10mem0-ts/src/oss/tests/memory.add.test.ts:1-7mem0-ts/src/client/tests/integration/crud.test.ts:1-18

Python 测试的实现

模拟与夹具

Python 测试使用 pytest.fixtureunittest.mock 将内存逻辑与外部的大语言模型(LLM)、嵌入向量和向量存储提供者隔离开来。memory_instance 夹具 tests/test_main.py:18-32 使用模拟工厂设置了一个 Memory 类。

@pytest.fixture
def memory_instance():
    with (
        patch("mem0.utils.factory.EmbedderFactory") as mock_embedder,
        patch("mem0.memory.main.VectorStoreFactory") as mock_vector_store,
        patch("mem0.utils.factory.LlmFactory") as mock_llm,
        patch("mem0.memory.telemetry.capture_event"),
    ):
        mock_embedder.create.return_value = Mock()
        mock_vector_store.create.return_value = Mock()
        mock_llm.create.return_value = Mock()
        return Memory(MemoryConfig(version="v1.1"))

来源: tests/test_main.py:18-32tests/memory/test_main.py:10-27

数据流与代码实体映射

下图展示了在标准内存添加操作中,测试实体与它们所验证的核心系统类之间的映射关系。

sequenceDiagram
    participant T as test_main.py
    participant M as Memory (mem0.memory.main)
    participant VS as VectorStore (mem0.vector_stores)
    participant LLM as LLM (mem0.llms)

    T->>M: test_add(messages, user_id)
    M->>M: _add_to_vector_store()
    M->>LLM: generate_response() (v3 单次传递)
    LLM-->>M: {"memory": [{"text": "...", "attributed_to": "user"}]}
    M->>VS: search() (检查重复项)
    M->>VS: add() (持久化事实)
    M-->>T: assert result["results"]

来源: mem0/memory/main.py:103-170tests/test_main.py:55-67tests/memory/test_main.py:48-64

TypeScript 集成测试

TypeScript SDK 包含针对真实 Mem0 平台 API 运行的集成测试。这些测试由 MEM0_API_KEY 环境变量控制。

集成辅助函数

createTestClient 辅助函数 mem0-ts/src/client/tests/integration/helpers.ts:24-28 使用真实的 API 密钥初始化一个 MemoryClient。为了处理平台 API 的异步特性,测试使用 waitForMemories mem0-ts/src/client/tests/integration/helpers.ts:63-84 轮询 getAll 端点,直到内存被处理完毕。

集成测试生命周期
  1. 设置:抑制遥测噪音并初始化客户端 mem0-ts/src/client/tests/integration/crud.test.ts:29-32
  2. 执行:执行 CRUD 操作(添加、获取、更新、删除)。
  3. 清理:使用 cleanupTestUser 清除测试数据 mem0-ts/src/client/tests/integration/helpers.ts:190-204

来源: mem0-ts/src/client/tests/integration/helpers.ts:1-234mem0-ts/src/client/tests/integration/crud.test.ts:24-37

持续集成管线

CI 工作流架构
flowchart TB
    Trigger["GitHub 推送/拉取请求"] --> PathFilter["dorny/paths-filter"]

    PathFilter --> CheckChanges["check_changes 任务"]

    CheckChanges --> Mem0Changed{{"mem0_changed"}}
    CheckChanges --> TSChanged{{"ts_sdk_changed"}}

    Mem0Changed -->|"true"| BuildMem0["build_mem0 任务"]
    TSChanged -->|"true"| BuildTS["ts-sdk-ci 工作流"]

    subgraph "build_mem0 任务"
        BuildMem0 --> Matrix["Python 3.10, 3.11, 3.12"]
        Matrix --> Install["pip install .[test]"]
        Install --> Lint["make lint"]
        Lint --> Test["make test"]
    end

来源: .github/workflows/ci.yml:1-118.github/workflows/ts-sdk-ci.yml

专项校验

敏感数据脱敏

系统使用精确匹配 mem0/memory/main.py:69-88 和后缀匹配 mem0/memory/main.py:91-97 来识别敏感字段。_is_sensitive_field 函数 mem0/memory/main.py:171-184 确保在捕获遥测数据之前对这些字段进行脱敏处理。

参数校验

测试会验证顶级实体参数(如 user_idagent_id)会被拒绝,而应使用 filters 字典,以防止作用域歧义 mem0/memory/main.py:103-110

大语言模型(LLM)响应解析

TestAddToVectorStoreErrors 测试套件 tests/memory/test_main.py:30-64 确保内存系统能够优雅地处理大语言模型(LLM)在事实提取过程中返回的格式错误或空响应,通过返回空结果而不是崩溃来保证系统稳定性。

来源: mem0/memory/main.py:68-110mem0/memory/main.py:171-184tests/memory/test_main.py:48-64