|
| 1 | +import { describe, it, beforeEach } from 'node:test'; |
| 2 | +import assert from 'node:assert'; |
| 3 | +import fs from 'fs'; |
| 4 | + |
| 5 | +// Mock parentPort BEFORE importing createWorkerEndpoint |
| 6 | +import Module from 'module'; |
| 7 | +const originalLoad = (Module as any)._load; |
| 8 | +(Module as any)._load = function (request: string, parent: any, isMain: boolean) { |
| 9 | + if (request === 'worker_threads') { |
| 10 | + return { |
| 11 | + parentPort: { |
| 12 | + postMessage: () => {}, |
| 13 | + on: () => {} |
| 14 | + } |
| 15 | + }; |
| 16 | + } |
| 17 | + return originalLoad(request, parent, isMain); |
| 18 | +}; |
| 19 | + |
| 20 | +// Now we can import the module to test |
| 21 | +import { createWorkerEndpoint } from '../../src/core/sqlite-db'; |
| 22 | + |
| 23 | +describe('Worker Endpoint', () => { |
| 24 | + let endpoint: ReturnType<typeof createWorkerEndpoint>; |
| 25 | + let wasmBinary: Buffer; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + endpoint = createWorkerEndpoint(); |
| 29 | + wasmBinary = fs.readFileSync('./node_modules/sql.js/dist/sql-wasm.wasm'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should throw Error if database is not initialized', async () => { |
| 33 | + const expectedError = new Error('No database initialized'); |
| 34 | + |
| 35 | + await assert.rejects(endpoint.runQuery('SELECT 1'), expectedError); |
| 36 | + await assert.rejects(endpoint.exportDatabase('test'), expectedError); |
| 37 | + await assert.rejects(endpoint.updateCell('table', 1, 'col', 'val'), expectedError); |
| 38 | + await assert.rejects(endpoint.insertRow('table', {}), expectedError); |
| 39 | + await assert.rejects(endpoint.insertRowBatch('table', []), expectedError); |
| 40 | + await assert.rejects(endpoint.deleteRows('table', [1]), expectedError); |
| 41 | + await assert.rejects(endpoint.deleteColumns('table', ['col']), expectedError); |
| 42 | + await assert.rejects(endpoint.findDependentIndexes('table', ['col']), expectedError); |
| 43 | + await assert.rejects(endpoint.createTable('table', []), expectedError); |
| 44 | + await assert.rejects(endpoint.updateCellBatch('table', []), expectedError); |
| 45 | + await assert.rejects(endpoint.addColumn('table', 'col', 'TEXT'), expectedError); |
| 46 | + await assert.rejects(endpoint.fetchTableData('table', { offset: 0, limit: 10 }), expectedError); |
| 47 | + await assert.rejects(endpoint.fetchTableCount('table', {}), expectedError); |
| 48 | + await assert.rejects(endpoint.fetchSchema(), expectedError); |
| 49 | + await assert.rejects(endpoint.getTableInfo('table'), expectedError); |
| 50 | + await assert.rejects(endpoint.getPragmas(), expectedError); |
| 51 | + await assert.rejects(endpoint.setPragma('journal_mode', 'WAL'), expectedError); |
| 52 | + await assert.rejects(endpoint.writeToFile('path'), expectedError); |
| 53 | + |
| 54 | + const pingResult = await endpoint.ping(); |
| 55 | + assert.strictEqual(pingResult, false); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should delegate operations after database is initialized', async () => { |
| 59 | + const initResult = await endpoint.initializeDatabase('test.db', { |
| 60 | + content: null, |
| 61 | + maxSize: 0, |
| 62 | + readOnlyMode: false, |
| 63 | + wasmBinary |
| 64 | + }); |
| 65 | + |
| 66 | + assert.strictEqual(initResult.isReadOnly, false); |
| 67 | + |
| 68 | + // Ping should work |
| 69 | + assert.strictEqual(await endpoint.ping(), true); |
| 70 | + |
| 71 | + // Run a query |
| 72 | + const queryResult = await endpoint.runQuery('SELECT 1 + 1 AS result'); |
| 73 | + assert.deepStrictEqual(queryResult[0].rows, [[2]]); |
| 74 | + |
| 75 | + // Create table and insert |
| 76 | + await endpoint.createTable('users', [ |
| 77 | + { name: 'id', type: 'INTEGER', primaryKey: true }, |
| 78 | + { name: 'name', type: 'TEXT' } |
| 79 | + ]); |
| 80 | + |
| 81 | + await endpoint.insertRow('users', { id: 1, name: 'Alice' }); |
| 82 | + |
| 83 | + const tableData = await endpoint.fetchTableData('users', { offset: 0, limit: 10 }); |
| 84 | + assert.deepStrictEqual(tableData.rows, [[1, 'Alice']]); |
| 85 | + |
| 86 | + // Export DB |
| 87 | + const data = await endpoint.exportDatabase('test'); |
| 88 | + assert.ok(data instanceof Uint8Array); |
| 89 | + assert.ok(data.length > 0); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should shutdown previous database when initializing a new one', async () => { |
| 93 | + await endpoint.initializeDatabase('test1.db', { |
| 94 | + content: null, |
| 95 | + maxSize: 0, |
| 96 | + readOnlyMode: false, |
| 97 | + wasmBinary |
| 98 | + }); |
| 99 | + |
| 100 | + await endpoint.createTable('test_table', [{ name: 'id', type: 'INTEGER' }]); |
| 101 | + |
| 102 | + // Initialize a new database, which should shutdown the previous one and create a clean state |
| 103 | + await endpoint.initializeDatabase('test2.db', { |
| 104 | + content: null, |
| 105 | + maxSize: 0, |
| 106 | + readOnlyMode: false, |
| 107 | + wasmBinary |
| 108 | + }); |
| 109 | + |
| 110 | + // The new database should not have the test_table |
| 111 | + const schema = await endpoint.fetchSchema(); |
| 112 | + const hasTestTable = schema.tables.some(t => t.identifier === 'test_table'); |
| 113 | + assert.strictEqual(hasTestTable, false); |
| 114 | + }); |
| 115 | +}); |
0 commit comments