mirror of
https://github.com/langchain-ai/agents-from-scratch-ts.git
synced 2026-07-01 21:34:00 -04:00
3.5 KiB
3.5 KiB
LangGraph Email Agent Testing Suite
This is a TypeScript testing framework for evaluating LangGraph agents, particularly email processing assistants with human-in-the-loop (HITL) and memory capabilities.
Overview
The testing suite mirrors the Python-based testing framework but adapted for TypeScript and Jest. It includes:
- Response Quality Testing: Tests the agent's ability to generate quality email responses.
- Tool Call Testing: Verifies the agent makes the expected tool calls.
- HITL Workflow Testing: Tests the human-in-the-loop workflow with accept/edit/reject operations.
- Memory Testing: Verifies memory persistence and preference learning across conversations.
Installation
# Install dependencies
npm install
Running Tests
# Run from the project root directory
# Run all tests
pnpm test
# Run specific test suites
pnpm test:hitl # Run HITL workflow tests
pnpm test:memory # Run memory functionality tests
pnpm test:base # Run basic email assistant tests
# Run with specific module
AGENT_MODULE=email_assistant_hitl_memory pnpm test
Test Files
test_response.test.ts: Tests email response quality and tool calling accuracyhitl_testing.test.ts: Tests human-in-the-loop workflowsmemory_testing.test.ts: Tests memory persistence and learning
ESM Module Setup
This project uses ESM modules which required specific Jest configuration:
- The setup file is in
.mjsformat to support ESM modules - Jest is configured to use the proper ESM preset
- Global types are declared in a separate
.d.tsfile
Mock Assistant Implementation
The testing suite uses a configurable mock assistant approach:
createMockAssistant()intest-utils.tscreates a mock assistant with customizable responses- Thread-specific responses can be provided via
mockResponsesandmockStatesparameters - This approach allows tests to be tailored for different test scenarios without complex mocks
Memory Testing
Memory tests use a specialized TestInMemoryStore that:
- Simulates memory storage and retrieval
- Tracks memory changes
- Provides methods for displaying memory content
Environment Variables
Create a .env file in the root directory with:
OPENAI_API_KEY=your-api-key
# Optional: For tracing
LANGCHAIN_API_KEY=your-langsmith-api-key
LANGCHAIN_TRACING_V2=true
LANGCHAIN_CALLBACKS_BACKGROUND=true
Maintenance and Extension
Adding New Tests
To add new tests:
- Use the existing test pattern for consistency
- Leverage the configurability of
createMockAssistant()for custom scenarios - Add test-specific mock responses and states as needed
Updating Mocks
To update the mock assistant behavior:
- Modify the
createMockAssistant()function intest-utils.ts - Add new mock states for specific thread scenarios
- Extend the function to handle new interrupt types or responses
Debugging Tests
For test debugging:
- Look at the console logs for the streams, interrupts, and memory content
- Check assertions for the expected vs actual values
- Verify the mock responses and states match the expected agent behavior
Extending Test Utilities
To add new utility functions:
- Add the function to
test-utils.ts - Document the purpose and usage
- Export the function for use in test files
Notes
- Tests may take longer to run due to LLM calls
- Default timeout is set to 2 minutes for LLM-based tests
- The mock assistant approach allows for faster tests without actual LLM calls