|
| 1 | +import fs from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { createClient } from '@libsql/client'; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 6 | + |
| 7 | +import { LibSQLStore } from './index'; |
| 8 | + |
| 9 | +type TestClient = ReturnType<typeof createClient>; |
| 10 | + |
| 11 | +const getClient = (store: LibSQLStore): TestClient => (store as unknown as { client: TestClient }).client; |
| 12 | + |
| 13 | +const executedSqlFrom = (spy: ReturnType<typeof vi.spyOn>): string[] => |
| 14 | + (spy.mock.calls as unknown as unknown[][]).map(call => { |
| 15 | + const arg = call[0]; |
| 16 | + return typeof arg === 'string' ? arg : (arg as { sql: string }).sql; |
| 17 | + }); |
| 18 | + |
| 19 | +describe('LibSQLStore.close()', () => { |
| 20 | + let tmpDir: string; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'libsql-close-test-')); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('checkpoints/truncates the WAL and closes the client for local file DBs', async () => { |
| 31 | + const dbPath = path.join(tmpDir, 'mastra.db'); |
| 32 | + const store = new LibSQLStore({ id: 'close-local', url: `file:${dbPath}` }); |
| 33 | + await store.init(); |
| 34 | + |
| 35 | + const client = getClient(store); |
| 36 | + const executeSpy = vi.spyOn(client, 'execute'); |
| 37 | + const closeSpy = vi.spyOn(client, 'close'); |
| 38 | + |
| 39 | + await store.close(); |
| 40 | + |
| 41 | + const executedSql = executedSqlFrom(executeSpy); |
| 42 | + expect(executedSql).toContain('PRAGMA wal_checkpoint(TRUNCATE);'); |
| 43 | + expect(executedSql).toContain('PRAGMA journal_mode=DELETE;'); |
| 44 | + expect(closeSpy).toHaveBeenCalledTimes(1); |
| 45 | + expect(client.closed).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it('is idempotent — a second close() is a no-op', async () => { |
| 49 | + const dbPath = path.join(tmpDir, 'mastra.db'); |
| 50 | + const store = new LibSQLStore({ id: 'close-idempotent', url: `file:${dbPath}` }); |
| 51 | + await store.init(); |
| 52 | + |
| 53 | + const client = getClient(store); |
| 54 | + |
| 55 | + await store.close(); |
| 56 | + |
| 57 | + const executeSpy = vi.spyOn(client, 'execute'); |
| 58 | + const closeSpy = vi.spyOn(client, 'close'); |
| 59 | + |
| 60 | + await expect(store.close()).resolves.toBeUndefined(); |
| 61 | + |
| 62 | + expect(executeSpy).not.toHaveBeenCalled(); |
| 63 | + expect(closeSpy).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('runs WAL cleanup for an injected client that points at a local file', async () => { |
| 67 | + const dbPath = path.join(tmpDir, 'injected.db'); |
| 68 | + const client = createClient({ url: `file:${dbPath}` }); |
| 69 | + const store = new LibSQLStore({ id: 'close-injected-local', client }); |
| 70 | + await store.init(); |
| 71 | + |
| 72 | + expect(client.protocol).toBe('file'); |
| 73 | + |
| 74 | + const executeSpy = vi.spyOn(client, 'execute'); |
| 75 | + const closeSpy = vi.spyOn(client, 'close'); |
| 76 | + |
| 77 | + await store.close(); |
| 78 | + |
| 79 | + expect(executedSqlFrom(executeSpy)).toContain('PRAGMA wal_checkpoint(TRUNCATE);'); |
| 80 | + expect(closeSpy).toHaveBeenCalledTimes(1); |
| 81 | + }); |
| 82 | + |
| 83 | + it('skips WAL pragmas for non-file (remote) clients', async () => { |
| 84 | + const client = createClient({ url: `file:${path.join(tmpDir, 'remote.db')}` }); |
| 85 | + // Pretend this is a remote connection without touching the underlying file logic. |
| 86 | + Object.defineProperty(client, 'protocol', { value: 'https', configurable: true }); |
| 87 | + |
| 88 | + const store = new LibSQLStore({ id: 'close-remote', client }); |
| 89 | + await store.init(); |
| 90 | + |
| 91 | + const executeSpy = vi.spyOn(client, 'execute'); |
| 92 | + const closeSpy = vi.spyOn(client, 'close'); |
| 93 | + |
| 94 | + await store.close(); |
| 95 | + |
| 96 | + const executedSql = executedSqlFrom(executeSpy); |
| 97 | + expect(executedSql).not.toContain('PRAGMA wal_checkpoint(TRUNCATE);'); |
| 98 | + expect(executedSql).not.toContain('PRAGMA journal_mode=DELETE;'); |
| 99 | + expect(closeSpy).toHaveBeenCalledTimes(1); |
| 100 | + }); |
| 101 | + |
| 102 | + it('still closes the client and warns when WAL checkpoint fails', async () => { |
| 103 | + const dbPath = path.join(tmpDir, 'wal-fail.db'); |
| 104 | + const store = new LibSQLStore({ id: 'close-wal-fail', url: `file:${dbPath}` }); |
| 105 | + await store.init(); |
| 106 | + |
| 107 | + const client = getClient(store); |
| 108 | + vi.spyOn(client, 'execute').mockRejectedValue(new Error('boom')); |
| 109 | + const closeSpy = vi.spyOn(client, 'close'); |
| 110 | + const warnSpy = vi.spyOn((store as unknown as { logger: { warn: (...args: unknown[]) => void } }).logger, 'warn'); |
| 111 | + |
| 112 | + await expect(store.close()).resolves.toBeUndefined(); |
| 113 | + |
| 114 | + expect(warnSpy).toHaveBeenCalled(); |
| 115 | + expect(closeSpy).toHaveBeenCalledTimes(1); |
| 116 | + }); |
| 117 | + |
| 118 | + it('removes WAL sidecar files so the storage dir can be deleted', async () => { |
| 119 | + const dbPath = path.join(tmpDir, 'sidecars.db'); |
| 120 | + const store = new LibSQLStore({ id: 'close-sidecars', url: `file:${dbPath}` }); |
| 121 | + await store.init(); |
| 122 | + |
| 123 | + // Force some writes so the WAL sidecar files exist. |
| 124 | + const client = getClient(store); |
| 125 | + await client.execute('CREATE TABLE IF NOT EXISTS t (id INTEGER PRIMARY KEY);'); |
| 126 | + await client.execute('INSERT INTO t (id) VALUES (1);'); |
| 127 | + |
| 128 | + await store.close(); |
| 129 | + |
| 130 | + expect(fs.existsSync(`${dbPath}-wal`)).toBe(false); |
| 131 | + expect(fs.existsSync(`${dbPath}-shm`)).toBe(false); |
| 132 | + }); |
| 133 | +}); |
0 commit comments