forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanggraph-invoke-null-input.test.ts
More file actions
85 lines (69 loc) · 3.07 KB
/
langgraph-invoke-null-input.test.ts
File metadata and controls
85 lines (69 loc) · 3.07 KB
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
import { describe, expect, it, vi } from 'vitest';
import { instrumentLangGraph } from '../../src/tracing/langgraph';
/**
* Creates a minimal mock StateGraph that simulates LangGraph's StateGraph.
* The compile() method returns a mock CompiledGraph with an invoke() method.
*/
function createMockStateGraph(invokeResult: unknown = { messages: [] }) {
return {
compile: (options?: Record<string, unknown>) => {
return {
invoke: vi.fn().mockResolvedValue(invokeResult),
name: options?.name ?? 'test_graph',
builder: {
nodes: {},
},
};
},
};
}
describe('LangGraph invoke with null input (resume scenario)', () => {
it('should not throw TypeError when invoke is called with null as first argument', async () => {
const stateGraph = createMockStateGraph();
instrumentLangGraph(stateGraph, { recordInputs: true, recordOutputs: true });
const compiled = stateGraph.compile({ name: 'resume_agent' });
// Simulates graph.invoke(null, config) which is the standard pattern
// for resuming a LangGraph graph after a human-in-the-loop interrupt.
// Previously this would throw: TypeError: Cannot read properties of null (reading 'messages')
await expect(
compiled.invoke(null, {
configurable: { thread_id: 'thread-123' },
}),
).resolves.not.toThrow();
});
it('should not throw TypeError when invoke is called with undefined as first argument', async () => {
const stateGraph = createMockStateGraph();
instrumentLangGraph(stateGraph, { recordInputs: true, recordOutputs: true });
const compiled = stateGraph.compile({ name: 'resume_agent' });
await expect(
compiled.invoke(undefined, {
configurable: { thread_id: 'thread-123' },
}),
).resolves.not.toThrow();
});
it('should not throw when invoke is called with no arguments', async () => {
const stateGraph = createMockStateGraph();
instrumentLangGraph(stateGraph, { recordInputs: true, recordOutputs: true });
const compiled = stateGraph.compile({ name: 'resume_agent' });
await expect(compiled.invoke()).resolves.not.toThrow();
});
it('should still work correctly with a normal messages input', async () => {
const mockResult = {
messages: [
{ role: 'user', content: 'hello' },
{ role: 'assistant', content: 'Hi there!' },
],
};
const stateGraph = createMockStateGraph(mockResult);
instrumentLangGraph(stateGraph, { recordInputs: true, recordOutputs: true });
const compiled = stateGraph.compile({ name: 'chat_agent' });
const result = await compiled.invoke({ messages: [{ role: 'user', content: 'hello' }] });
expect(result).toEqual(mockResult);
});
it('should still work correctly with an empty object input', async () => {
const stateGraph = createMockStateGraph();
instrumentLangGraph(stateGraph, { recordInputs: true, recordOutputs: true });
const compiled = stateGraph.compile({ name: 'resume_agent' });
await expect(compiled.invoke({}, { configurable: { thread_id: 'thread-456' } })).resolves.not.toThrow();
});
});