在你完成 LangGraph agent 的原型开发后,下一个自然的步骤就是添加测试。本指南介绍了一些在编写单元测试时可以使用的实用模式。 请注意,本指南是 LangGraph 专用的,涵盖的是具有自定义结构的图相关场景。如果你刚刚入门,请查看使用 LangChain 内置 createAgent测试

前置条件

首先,请确保你已安装 vitest
$ npm install -D vitest

快速开始

由于许多 LangGraph agent 依赖状态,一个实用的模式是在每个使用图的测试之前创建图,然后在测试中使用新的 checkpointer 实例对其进行编译。 下面的示例展示了这一模式如何作用于一个简单的线性图,该图依次经过 node1node2。每个节点都会更新唯一的状态键 my_key
import { test, expect } from 'vitest';
import {
  StateGraph,
  StateSchema,
  START,
  END,
  MemorySaver,
} from '@langchain/langgraph';
import * as z from "zod";

const State = new StateSchema({
  my_key: z.string(),
});

const createGraph = () => {
  return new StateGraph(State)
    .addNode('node1', (state) => ({ my_key: 'hello from node1' }))
    .addNode('node2', (state) => ({ my_key: 'hello from node2' }))
    .addEdge(START, 'node1')
    .addEdge('node1', 'node2')
    .addEdge('node2', END);
};

test('basic agent execution', async () => {
  const uncompiledGraph = createGraph();
  const checkpointer = new MemorySaver();
  const compiledGraph = uncompiledGraph.compile({ checkpointer });
  const result = await compiledGraph.invoke(
    { my_key: 'initial_value' },
    { configurable: { thread_id: '1' } }
  );
  expect(result.my_key).toBe('hello from node2');
});

测试单个节点和边

编译后的 LangGraph agent 会通过 graph.nodes 暴露对每个单独节点的引用。你可以利用这一点来测试 agent 中的单个节点。请注意,这会绕过编译图时传入的任何 checkpointer:
import { test, expect } from 'vitest';
import {
  StateGraph,
  START,
  END,
  MemorySaver,
  StateSchema,
} from '@langchain/langgraph';
import * as z from "zod";

const State = new StateSchema({
  my_key: z.string(),
});

const createGraph = () => {
  return new StateGraph(State)
    .addNode('node1', (state) => ({ my_key: 'hello from node1' }))
    .addNode('node2', (state) => ({ my_key: 'hello from node2' }))
    .addEdge(START, 'node1')
    .addEdge('node1', 'node2')
    .addEdge('node2', END);
};

test('individual node execution', async () => {
  const uncompiledGraph = createGraph();
  // Will be ignored in this example
  const checkpointer = new MemorySaver();
  const compiledGraph = uncompiledGraph.compile({ checkpointer });
  // Only invoke node 1
  const result = await compiledGraph.nodes['node1'].invoke(
    { my_key: 'initial_value' },
  );
  expect(result.my_key).toBe('hello from node1');
});

部分执行

对于由更大图组成的 agent,你可能希望测试 agent 中的部分执行路径,而不是从头到尾测试整个流程。在某些情况下,从语义上看,将这些部分重构为子图可能更合理,这样你就可以像通常一样单独调用它们。 不过,如果你不想改变 agent 图的整体结构,可以使用 LangGraph 的持久化机制来模拟这样一种状态:agent 在目标部分开始之前暂停,并且会在目标部分结束时再次暂停。步骤如下:
  1. 使用 checkpointer 编译你的 agent(内存中的 checkpointer MemorySaver 足以用于测试)。
  2. 调用你的 agent 的 update_state 方法,并将 asNode 参数设置为你想开始测试的节点的前一个节点名称。
  3. 使用与更新状态时相同的 thread_id 调用你的 agent,并将 interruptBefore 参数设置为你想停止的节点名称。
下面是一个示例,它只执行线性图中的第二个和第三个节点:
import { test, expect } from 'vitest';
import {
  StateGraph,
  StateSchema,
  START,
  END,
  MemorySaver,
} from '@langchain/langgraph';
import * as z from "zod";

const State = new StateSchema({
  my_key: z.string(),
});

const createGraph = () => {
  return new StateGraph(State)
    .addNode('node1', (state) => ({ my_key: 'hello from node1' }))
    .addNode('node2', (state) => ({ my_key: 'hello from node2' }))
    .addNode('node3', (state) => ({ my_key: 'hello from node3' }))
    .addNode('node4', (state) => ({ my_key: 'hello from node4' }))
    .addEdge(START, 'node1')
    .addEdge('node1', 'node2')
    .addEdge('node2', 'node3')
    .addEdge('node3', 'node4')
    .addEdge('node4', END);
};

test('partial execution from node2 to node3', async () => {
  const uncompiledGraph = createGraph();
  const checkpointer = new MemorySaver();
  const compiledGraph = uncompiledGraph.compile({ checkpointer });
  await compiledGraph.updateState(
    { configurable: { thread_id: '1' } },
    // The state passed into node 2 - simulating the state at
    // the end of node 1
    { my_key: 'initial_value' },
    // Update saved state as if it came from node 1
    // Execution will resume at node 2
    'node1',
  );
  const result = await compiledGraph.invoke(
    // Resume execution by passing None
    null,
    {
      configurable: { thread_id: '1' },
      // Stop after node 3 so that node 4 doesn't run
      interruptAfter: ['node3']
    },
  );
  expect(result.my_key).toBe('hello from node3');
});