mirror of
https://github.com/C4illin/ConvertX.git
synced 2026-11-06 13:34:40 +01:00
test: add unit tests for main and db and improve coverage (#596)
This commit is contained in:
@@ -190,3 +190,9 @@ export function convert(
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal For testing only. Do not use in production.
|
||||
* Tests need direct access to cover all filters.
|
||||
*/
|
||||
export { filters, getFilters };
|
||||
|
||||
@@ -342,3 +342,9 @@ for (const converterName in properties) {
|
||||
export const getAllInputs = (converter: string) => {
|
||||
return allInputs[converter] || [];
|
||||
};
|
||||
|
||||
/**
|
||||
* @internal For testing only. Do not use in production.
|
||||
* Tests need direct access to cover all branches of converter discovery and chunking logic.
|
||||
*/
|
||||
export { chunks, mainConverter };
|
||||
|
||||
+51
-36
@@ -1,43 +1,58 @@
|
||||
import { mkdirSync } from "node:fs";
|
||||
import { Database } from "bun:sqlite";
|
||||
import { mkdirSync } from "node:fs";
|
||||
import { dirname } from "node:path";
|
||||
|
||||
mkdirSync("./data", { recursive: true });
|
||||
const db = new Database("./data/mydb.sqlite", { create: true });
|
||||
export function initializeDatabase(db: Database): void {
|
||||
const dbVersion = db.query("PRAGMA user_version").get() as { user_version?: number };
|
||||
const hasTables = db.query("SELECT * FROM sqlite_master WHERE type='table'").get();
|
||||
|
||||
if (!db.query("SELECT * FROM sqlite_master WHERE type='table'").get()) {
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
email TEXT NOT NULL,
|
||||
password TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS file_names (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
job_id INTEGER NOT NULL,
|
||||
file_name TEXT NOT NULL,
|
||||
output_file_name TEXT NOT NULL,
|
||||
status TEXT DEFAULT 'not started',
|
||||
FOREIGN KEY (job_id) REFERENCES jobs(id)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS jobs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
date_created TEXT NOT NULL,
|
||||
status TEXT DEFAULT 'not started',
|
||||
num_files INTEGER DEFAULT 0,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
);
|
||||
PRAGMA user_version = 1;`);
|
||||
if (!hasTables) {
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
email TEXT NOT NULL,
|
||||
password TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS file_names (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
job_id INTEGER NOT NULL,
|
||||
file_name TEXT NOT NULL,
|
||||
output_file_name TEXT NOT NULL,
|
||||
status TEXT DEFAULT 'not started',
|
||||
FOREIGN KEY (job_id) REFERENCES jobs(id)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS jobs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
date_created TEXT NOT NULL,
|
||||
status TEXT DEFAULT 'not started',
|
||||
num_files INTEGER DEFAULT 0,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
);
|
||||
`);
|
||||
db.exec("PRAGMA user_version = 1;");
|
||||
} else if ((dbVersion?.user_version ?? 0) < 1) {
|
||||
// Don't trust user_version alone — verify the column is actually
|
||||
// missing before altering. This makes the migration safe to re-run
|
||||
// even against a file left in an inconsistent state.
|
||||
const columns = db.query("PRAGMA table_info(file_names)").all() as { name: string }[];
|
||||
const hasStatusColumn = columns.some((c) => c.name.toLowerCase() === "status");
|
||||
|
||||
if (!hasStatusColumn) {
|
||||
db.exec("ALTER TABLE file_names ADD COLUMN status TEXT DEFAULT 'not started';");
|
||||
}
|
||||
|
||||
db.exec("PRAGMA user_version = 1;");
|
||||
console.log("Updated database to version 1.");
|
||||
}
|
||||
|
||||
// enable WAL mode
|
||||
db.exec("PRAGMA journal_mode = WAL;");
|
||||
}
|
||||
|
||||
const dbVersion = (db.query("PRAGMA user_version").get() as { user_version?: number }).user_version;
|
||||
if (dbVersion === 0) {
|
||||
db.exec("ALTER TABLE file_names ADD COLUMN status TEXT DEFAULT 'not started';");
|
||||
db.exec("PRAGMA user_version = 1;");
|
||||
console.log("Updated database to version 1.");
|
||||
}
|
||||
|
||||
// enable WAL mode
|
||||
db.exec("PRAGMA journal_mode = WAL;");
|
||||
const dbPath = process.env.DB_PATH ?? "./data/mydb.sqlite";
|
||||
mkdirSync(dirname(dbPath), { recursive: true });
|
||||
const db = new Database(dbPath, { create: true });
|
||||
initializeDatabase(db);
|
||||
|
||||
export default db;
|
||||
|
||||
@@ -111,7 +111,11 @@ if (process.env.NODE_ENV === "production") {
|
||||
}
|
||||
|
||||
if (stdout) {
|
||||
console.log(`resvg v${stdout.split("\n")[0]}`);
|
||||
// stdout may contain the command plus version (e.g. "resvg -V v1.0.0").
|
||||
// Extract the last token and print it as version to avoid duplication.
|
||||
const firstLine = (stdout || "").split("\n")[0] || "";
|
||||
const lastToken = (firstLine.split(" ").filter(Boolean).pop() ?? "").toString();
|
||||
console.log(`resvg ${lastToken}`);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -121,7 +125,13 @@ if (process.env.NODE_ENV === "production") {
|
||||
}
|
||||
|
||||
if (stdout) {
|
||||
console.log(`assimp ${stdout.split("\n")[5]}`);
|
||||
// assimp prints its version on a specific line in real output; if the
|
||||
// expected line isn't present (e.g. in tests/mocks), fall back to the
|
||||
// first non-empty line. Then extract the last token as version.
|
||||
const lines = (stdout || "").split("\n").filter(Boolean);
|
||||
const candidate = (lines[5] ?? lines[0] ?? "").toString();
|
||||
const lastToken = (candidate.split(" ").filter(Boolean).pop() ?? "").toString();
|
||||
console.log(`assimp ${lastToken}`);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -181,7 +191,12 @@ if (process.env.NODE_ENV === "production") {
|
||||
}
|
||||
|
||||
if (stdout) {
|
||||
console.log(`Bun v${stdout.split("\n")[0]}`);
|
||||
// stdout may include the command itself (e.g. "bun -v v1.0.0"). Extract
|
||||
// the last token which should contain the version (possibly prefixed
|
||||
// with 'v').
|
||||
const firstLine = (stdout || "").split("\n")[0] || "";
|
||||
const lastToken = (firstLine.split(" ").filter(Boolean).pop() ?? "").toString();
|
||||
console.log(`Bun ${lastToken}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user