Skip to content

Build your personal knowledge base with Trilium Notes

test(standalone): get most tests to pass

Elian Doran21302e414241
diff --git a/apps/client-standalone/src/lightweight/sql_provider.ts b/apps/client-standalone/src/lightweight/sql_provider.ts
index 4b22833de4..972b58db0d 100644
--- a/apps/client-standalone/src/lightweight/sql_provider.ts
+++ b/apps/client-standalone/src/lightweight/sql_provider.ts
@@ -435,9 +435,18 @@ export default class BrowserSqlProvider implements DatabaseProvider {
loadFromBuffer(buffer: Uint8Array): void {
this.ensureSqlite3();
- // SQLite WASM can deserialize a database from a byte array
- const p = this.sqlite3!.wasm.allocFromTypedArray(buffer);
+ // SQLite WASM's allocFromTypedArray rejects Node's Buffer (and other
+ // non-Uint8Array typed arrays) with "expecting 8/16/32/64". Normalize
+ // to a plain Uint8Array view over the same memory so callers can pass
+ // anything readFileSync returns.
+ const view = buffer instanceof Uint8Array && buffer.constructor === Uint8Array
+ ? buffer
+ : new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
+ const p = this.sqlite3!.wasm.allocFromTypedArray(view);
try {
+ // Cached statements reference the previous DB and become invalid
+ // once we swap connections. Drop them so callers re-prepare.
+ this.clearStatementCache();
this.db = new this.sqlite3!.oo1.DB({ filename: ":memory:", flags: "c" });
this.opfsDbPath = undefined; // Not using OPFS
@@ -445,8 +454,8 @@ export default class BrowserSqlProvider implements DatabaseProvider {
this.db.pointer!,
"main",
p,
- buffer.byteLength,
- buffer.byteLength,
+ view.byteLength,
+ view.byteLength,
this.sqlite3!.capi.SQLITE_DESERIALIZE_FREEONCLOSE |
this.sqlite3!.capi.SQLITE_DESERIALIZE_RESIZEABLE
);
@@ -563,8 +572,7 @@ export default class BrowserSqlProvider implements DatabaseProvider {
this.db!.exec(query);
}
- close(): void {
- // Clean up all cached statements first
+ private clearStatementCache(): void {
for (const statement of this.statementCache.values()) {
try {
statement.finalize();
@@ -574,6 +582,10 @@ export default class BrowserSqlProvider implements DatabaseProvider {
}
}
this.statementCache.clear();
+ }
+
+ close(): void {
+ this.clearStatementCache();
if (this.db) {
this.db.close();
diff --git a/apps/client-standalone/src/test_setup.ts b/apps/client-standalone/src/test_setup.ts
index 9e59c9552e..da79bcde96 100644
--- a/apps/client-standalone/src/test_setup.ts
+++ b/apps/client-standalone/src/test_setup.ts
@@ -1,8 +1,10 @@
+import { createRequire } from "node:module";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { initializeCore } from "@triliumnext/core";
import schemaSql from "@triliumnext/core/src/assets/schema.sql?raw";
+import serverEnTranslations from "../../server/src/assets/translations/en/server.json";
import { beforeAll } from "vitest";
import BrowserExecutionContext from "./lightweight/cls_provider.js";
@@ -63,16 +65,20 @@ WebAssembly.instantiateStreaming = (async (source, importObject) => {
// =============================================================================
// Core initialization for standalone-flavored tests
// =============================================================================
-// Mirror what apps/client-standalone/src/local-server-worker.ts does at
-// startup, but without messaging / requests / OPFS / demo archives. We just
-// need core to be initialized so that pure-becca / pure-search tests can run.
+// Mirror what apps/server/spec/setup.ts does: load the pre-seeded integration
+// fixture DB into an in-memory sqlite-wasm instance, then initialize core
+// against it with the standalone (browser) providers. Each vitest worker gets
+// a fresh copy because tests run in forks (per the default pool).
+
+const require = createRequire(import.meta.url);
+const fixtureDb = readFileSync(
+ require.resolve("@triliumnext/core/src/test/fixtures/document.db")
+);
beforeAll(async () => {
const sqlProvider = new BrowserSqlProvider();
await sqlProvider.initWasm();
- sqlProvider.loadFromMemory();
- // Apply the schema so search/becca tests that touch SQL find real tables.
- sqlProvider.exec(schemaSql);
+ sqlProvider.loadFromBuffer(fixtureDb);
await initializeCore({
executionContext: new BrowserExecutionContext(),
@@ -81,8 +87,21 @@ beforeAll(async () => {
zipExportProviderFactory: (
await import("./lightweight/zip_export_provider_factory.js")
).standaloneZipExportProviderFactory,
- // Stub translations: pure-becca tests don't need real i18n strings.
- translations: async () => undefined,
+ // i18next must be wired up — keyboard_actions.ts and other modules
+ // call `t()` and throw if translations are missing. Inline the
+ // en/server.json resources via vite's JSON import so we don't need a
+ // backend in tests.
+ translations: async (i18nextInstance, locale) => {
+ await i18nextInstance.init({
+ lng: locale,
+ fallbackLng: "en",
+ ns: "server",
+ defaultNS: "server",
+ resources: {
+ en: { server: serverEnTranslations }
+ }
+ });
+ },
platform: new StandalonePlatformProvider(""),
schema: schemaSql,
dbConfig: {