|
| 1 | +/** |
| 2 | + * Sanity check to test the bridge between TypeScript and the Tauri commands. |
| 3 | + */ |
| 4 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest' |
| 5 | +import { mockIPC, clearMocks } from '@tauri-apps/api/mocks' |
| 6 | +import Database from './index' |
| 7 | + |
| 8 | +let lastCmd = '' |
| 9 | +let lastArgs: Record<string, unknown> = {} |
| 10 | + |
| 11 | +beforeEach(() => { |
| 12 | + mockIPC((cmd, args) => { |
| 13 | + lastCmd = cmd |
| 14 | + lastArgs = args as Record<string, unknown> |
| 15 | + if (cmd === 'plugin:sqlite|load') return (args as { db: string }).db |
| 16 | + if (cmd === 'plugin:sqlite|execute') return [1, 1] |
| 17 | + if (cmd === 'plugin:sqlite|execute_transaction') return [] |
| 18 | + if (cmd === 'plugin:sqlite|fetch_all') return [] |
| 19 | + if (cmd === 'plugin:sqlite|fetch_one') return null |
| 20 | + if (cmd === 'plugin:sqlite|close') return true |
| 21 | + if (cmd === 'plugin:sqlite|close_all') return undefined |
| 22 | + if (cmd === 'plugin:sqlite|remove') return true |
| 23 | + return undefined |
| 24 | + }) |
| 25 | +}) |
| 26 | + |
| 27 | +afterEach(() => clearMocks()) |
| 28 | + |
| 29 | +describe('Database commands', () => { |
| 30 | + it('load', async () => { |
| 31 | + await Database.load('test.db') |
| 32 | + expect(lastCmd).toBe('plugin:sqlite|load') |
| 33 | + expect(lastArgs.db).toBe('test.db') |
| 34 | + }) |
| 35 | + |
| 36 | + it('execute', async () => { |
| 37 | + await Database.get('t.db').execute('INSERT INTO t VALUES ($1)', [1]) |
| 38 | + expect(lastCmd).toBe('plugin:sqlite|execute') |
| 39 | + expect(lastArgs).toMatchObject({ db: 't.db', query: 'INSERT INTO t VALUES ($1)', values: [1] }) |
| 40 | + }) |
| 41 | + |
| 42 | + it('execute_transaction', async () => { |
| 43 | + await Database.get('t.db').executeTransaction([['DELETE FROM t']]) |
| 44 | + expect(lastCmd).toBe('plugin:sqlite|execute_transaction') |
| 45 | + expect(lastArgs.statements).toEqual([{ query: 'DELETE FROM t', values: [] }]) |
| 46 | + }) |
| 47 | + |
| 48 | + it('fetch_all', async () => { |
| 49 | + await Database.get('t.db').fetchAll('SELECT * FROM t') |
| 50 | + expect(lastCmd).toBe('plugin:sqlite|fetch_all') |
| 51 | + expect(lastArgs).toMatchObject({ db: 't.db', query: 'SELECT * FROM t' }) |
| 52 | + }) |
| 53 | + |
| 54 | + it('fetch_one', async () => { |
| 55 | + await Database.get('t.db').fetchOne('SELECT * FROM t WHERE id = $1', [1]) |
| 56 | + expect(lastCmd).toBe('plugin:sqlite|fetch_one') |
| 57 | + expect(lastArgs).toMatchObject({ db: 't.db', values: [1] }) |
| 58 | + }) |
| 59 | + |
| 60 | + it('close', async () => { |
| 61 | + await Database.get('t.db').close() |
| 62 | + expect(lastCmd).toBe('plugin:sqlite|close') |
| 63 | + expect(lastArgs.db).toBe('t.db') |
| 64 | + }) |
| 65 | + |
| 66 | + it('close_all', async () => { |
| 67 | + await Database.closeAll() |
| 68 | + expect(lastCmd).toBe('plugin:sqlite|close_all') |
| 69 | + }) |
| 70 | + |
| 71 | + it('remove', async () => { |
| 72 | + await Database.get('t.db').remove() |
| 73 | + expect(lastCmd).toBe('plugin:sqlite|remove') |
| 74 | + expect(lastArgs.db).toBe('t.db') |
| 75 | + }) |
| 76 | +}) |
0 commit comments