fix: support built-in node:sqlite on Node 26 LTS to resolve native bindings failure
This commit is contained in:
parent
43f3891e9e
commit
26747f4d8b
5 changed files with 365 additions and 9 deletions
|
|
@ -19,6 +19,12 @@ import {
|
|||
getIstanbulMondayStart,
|
||||
isBeforeCurrentMondayInIstanbul
|
||||
} from '../src/utils/time';
|
||||
import {
|
||||
openDatabase,
|
||||
NodeSqliteDatabaseWrapper,
|
||||
NodeSqliteStatementWrapper,
|
||||
BetterSqlite3DatabaseWrapper
|
||||
} from '../src/services/sqlite-driver';
|
||||
import { Assignment, Lesson, ExamReportDetail } from '../src/types';
|
||||
|
||||
// Configure isolated temporary directory BEFORE any server/db singleton initialization
|
||||
|
|
@ -1220,6 +1226,99 @@ async function runAllTests() {
|
|||
}
|
||||
});
|
||||
|
||||
await test('SQLite Driver & Runtime Compatibility: seamlessly abstracts node:sqlite and better-sqlite3 with parameter sanitization, transactions, and pragmas', async () => {
|
||||
// 1. Test active database driver returned by openDatabase
|
||||
const activeDb = openDatabase(path.join(tempDir, 'active-driver.db'));
|
||||
assert.strictEqual(activeDb.open, true, 'Active driver must report open = true');
|
||||
activeDb.exec('CREATE TABLE test_tbl (id TEXT PRIMARY KEY, val TEXT);');
|
||||
activeDb.prepare('INSERT INTO test_tbl (id, val) VALUES (?, ?)').run('id1', 'val1');
|
||||
const row = activeDb.prepare('SELECT val FROM test_tbl WHERE id = ?').get('id1');
|
||||
assert.strictEqual(row?.val, 'val1');
|
||||
activeDb.close();
|
||||
assert.strictEqual(activeDb.open, false, 'Database must report open = false after close');
|
||||
|
||||
// 2. Test NodeSqliteDatabaseWrapper simulation to verify full parity on Node >= 22 (node:sqlite)
|
||||
const mockSqliteCalls: { exec: string[]; prepared: string[]; closed: boolean } = {
|
||||
exec: [],
|
||||
prepared: [],
|
||||
closed: false
|
||||
};
|
||||
|
||||
const mockRawDb = {
|
||||
exec(sql: string) {
|
||||
mockSqliteCalls.exec.push(sql);
|
||||
},
|
||||
prepare(sql: string) {
|
||||
mockSqliteCalls.prepared.push(sql);
|
||||
return {
|
||||
run(...args: any[]) {
|
||||
return { changes: 1, lastInsertRowid: 42 };
|
||||
},
|
||||
get(...args: any[]) {
|
||||
return { query: sql, args };
|
||||
},
|
||||
all(...args: any[]) {
|
||||
if (sql.includes('table_info')) {
|
||||
return [{ cid: 0, name: 'last_login_at', type: 'TEXT' }];
|
||||
}
|
||||
return [{ query: sql, args }];
|
||||
}
|
||||
};
|
||||
},
|
||||
close() {
|
||||
mockSqliteCalls.closed = true;
|
||||
}
|
||||
};
|
||||
|
||||
const nodeWrapper = new NodeSqliteDatabaseWrapper(mockRawDb);
|
||||
assert.strictEqual(nodeWrapper.driver, 'node:sqlite');
|
||||
assert.strictEqual(nodeWrapper.open, true);
|
||||
|
||||
// Verify pragma query vs command execution
|
||||
const pragmaCols = nodeWrapper.pragma('table_info(students)');
|
||||
assert.deepStrictEqual(pragmaCols, [{ cid: 0, name: 'last_login_at', type: 'TEXT' }]);
|
||||
|
||||
nodeWrapper.pragma('journal_mode = WAL');
|
||||
assert(mockSqliteCalls.exec.includes('PRAGMA journal_mode = WAL'), 'Pragma commands must run via exec');
|
||||
|
||||
// Verify named parameter mapping and sanitization for upserts
|
||||
const namedStmt = nodeWrapper.prepare('INSERT INTO assignments (id, student_id) VALUES (@id, @student_id)');
|
||||
const runRes = namedStmt.run({
|
||||
id: 'hw_1',
|
||||
student_id: 'std_99',
|
||||
extra_unrelated_field: 'should_be_stripped_for_node_sqlite'
|
||||
});
|
||||
assert.strictEqual(runRes.changes, 1);
|
||||
assert.strictEqual(runRes.lastInsertRowid, 42);
|
||||
|
||||
// Verify positional parameter sanitization (converting undefined to null)
|
||||
const posStmt = nodeWrapper.prepare('SELECT * FROM students WHERE student_id = ? AND last_synced = ?');
|
||||
const getRes = posStmt.get('std_1', undefined);
|
||||
assert.deepStrictEqual(getRes.args, ['std_1', null], 'Undefined positional arguments must be converted to null');
|
||||
|
||||
// Verify transaction behavior
|
||||
let txExecuted = false;
|
||||
const tx = nodeWrapper.transaction(() => {
|
||||
txExecuted = true;
|
||||
});
|
||||
tx();
|
||||
assert.strictEqual(txExecuted, true);
|
||||
assert(mockSqliteCalls.exec.includes('BEGIN IMMEDIATE'));
|
||||
assert(mockSqliteCalls.exec.includes('COMMIT'));
|
||||
|
||||
// Verify transaction rollback on error
|
||||
const failingTx = nodeWrapper.transaction(() => {
|
||||
throw new Error('Database transaction abort test');
|
||||
});
|
||||
assert.throws(() => failingTx(), /Database transaction abort test/);
|
||||
assert(mockSqliteCalls.exec.includes('ROLLBACK'));
|
||||
|
||||
// Verify close
|
||||
nodeWrapper.close();
|
||||
assert.strictEqual(nodeWrapper.open, false);
|
||||
assert.strictEqual(mockSqliteCalls.closed, true);
|
||||
});
|
||||
|
||||
// Optional Live Portal Test (via environment variables only)
|
||||
const testStudentId = process.env.TEST_STUDENT_ID;
|
||||
const testPassword = process.env.TEST_PASSWORD;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue