Files
OmniRoute/tests/unit/db-adapters/cliSqlite.test.mjs
Arul Kumaran edbce2e35b feat: support Bun bundled SQLite runtime (#7878)
* feat: support Bun bundled SQLite runtime

* fix: harden Bun SQLite backups and params

* docs: keep Node as the only supported runtime; document bun:sqlite as best-effort compatibility path

Co-authored-by: Arul Kumaran <arul@luracast.com>

---------

Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com>
2026-07-21 16:30:26 -03:00

48 lines
1.7 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { backupSqliteFile, normalizeBunSqliteParams } from "../../../bin/cli/sqlite.mjs";
test("CLI Bun SQLite parameter normalization preserves binary values", () => {
const typedArray = new Uint8Array([1, 2, 3]);
const buffer = Buffer.from([4, 5, 6]);
assert.deepEqual(normalizeBunSqliteParams([typedArray]), [typedArray]);
assert.deepEqual(normalizeBunSqliteParams([buffer]), [buffer]);
});
test("CLI Bun SQLite parameter normalization expands named object keys", () => {
assert.deepEqual(normalizeBunSqliteParams([{ id: 7 }]), [{ "@id": 7, ":id": 7, $id: 7 }]);
});
test("CLI Bun SQLite backs up physical files without serializing them", async (t) => {
if (!process.versions.bun) {
t.skip("bun:sqlite is only available under Bun");
return;
}
const sourcePath = path.join(os.tmpdir(), `cli-bun-sqlite-source-${Date.now()}.sqlite`);
const destinationPath = path.join(os.tmpdir(), `cli-bun-sqlite-destination-${Date.now()}.sqlite`);
t.after(() => {
for (const filePath of [sourcePath, destinationPath]) {
try {
fs.unlinkSync(filePath);
} catch {}
}
});
const { Database } = await import("bun:sqlite");
const source = new Database(sourcePath);
source.exec("CREATE TABLE items (value TEXT); INSERT INTO items VALUES ('copied')");
source.close();
await backupSqliteFile(sourcePath, destinationPath);
const destination = new Database(destinationPath, { readonly: true });
assert.equal(destination.query("SELECT value FROM items").get().value, "copied");
destination.close();
});