checkpoint.test.ts
1.5 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
import { describe, it, expect, vi, beforeEach, afterEach, type Mock } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import { initializeSqliteSaver } from '../../src/utils/checkpoint';
import { SqliteSaver } from '@langchain/langgraph-checkpoint-sqlite';
// Mock fs
vi.mock('node:fs', () => ({
default: {
existsSync: vi.fn(),
mkdirSync: vi.fn(),
},
}));
// Mock SqliteSaver
vi.mock('@langchain/langgraph-checkpoint-sqlite', () => ({
SqliteSaver: {
fromConnString: vi.fn(),
},
}));
describe('initializeSqliteSaver', () => {
beforeEach(() => {
vi.resetAllMocks();
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should use path.dirname and create directory if it does not exist', () => {
const dbPath = '/path/to/db/file.db';
const dbDir = '/path/to/db'; // Expected dirname on POSIX
// Spy on path.dirname to ensure it is called
// Note: path module is frozen in some environments, so spyOn might fail.
// If so, we rely on the behavior correctness.
const dirnameSpy = vi.spyOn(path, 'dirname');
(fs.existsSync as Mock).mockReturnValue(false);
initializeSqliteSaver(dbPath);
expect(dirnameSpy).toHaveBeenCalledWith(dbPath);
expect(fs.existsSync).toHaveBeenCalledWith(dbDir);
expect(fs.mkdirSync).toHaveBeenCalledWith(dbDir, { recursive: true });
expect(SqliteSaver.fromConnString).toHaveBeenCalledWith(dbPath);
});
});