Skip to content

Build your personal knowledge base with Trilium Notes

fix(server): cannot run server in e2e

Elian Doranc041c25e0f5d
diff --git a/apps/server/scripts/build.ts b/apps/server/scripts/build.ts
index 988744ca8b..0cf0f0c0d2 100644
--- a/apps/server/scripts/build.ts
+++ b/apps/server/scripts/build.ts
@@ -13,6 +13,11 @@ async function main() {
// alongside the server's own assets and read it via RESOURCE_DIR at
// runtime. See main.ts.
build.copy("/packages/trilium-core/src/assets/schema.sql", "assets/schema.sql");
+ // The integration test database fixture is loaded into memory when
+ // TRILIUM_INTEGRATION_TEST=memory is set (used by e2e tests against the
+ // packaged server). Same require.resolve issue as schema.sql in
+ // bundled environments — copy it into the bundle so it's reachable.
+ build.copy("/packages/trilium-core/src/test/fixtures/document.db", "assets/test/document.db");
build.triggerBuildAndCopyTo("packages/share-theme", "share-theme/assets/");
build.copy("/packages/share-theme/src/templates", "share-theme/templates/");
diff --git a/apps/server/src/core_assets.ts b/apps/server/src/core_assets.ts
index 6528a3f970..eabac3392a 100644
--- a/apps/server/src/core_assets.ts
+++ b/apps/server/src/core_assets.ts
@@ -22,3 +22,23 @@ export function loadCoreSchema(): string {
}
return fs.readFileSync(require.resolve("@triliumnext/core/src/assets/schema.sql"), "utf-8");
}
+
+/**
+ * Resolves the path to the integration test database fixture, with the same
+ * production-bundled vs. dev/test fallback as loadCoreSchema().
+ *
+ * Returns a real on-disk path so callers can either feed it into
+ * fs.readFileSync() (to load as a buffer for an in-memory connection) or
+ * pass it directly to better-sqlite3's `new Database(path)` constructor
+ * (for a separate file-backed read-only connection like share uses).
+ *
+ * Only meaningful when TRILIUM_INTEGRATION_TEST is set; production code
+ * paths that call this should be gated by that env var.
+ */
+export function getIntegrationTestDbPath(): string {
+ const productionPath = path.join(RESOURCE_DIR, "test", "document.db");
+ if (fs.existsSync(productionPath)) {
+ return productionPath;
+ }
+ return require.resolve("@triliumnext/core/src/test/fixtures/document.db");
+}
diff --git a/apps/server/src/main.ts b/apps/server/src/main.ts
index 65307f0be8..51011d4049 100644
--- a/apps/server/src/main.ts
+++ b/apps/server/src/main.ts
@@ -9,7 +9,7 @@ import { t } from "i18next";
import path from "path";
import ClsHookedExecutionContext from "./cls_provider.js";
-import { loadCoreSchema } from "./core_assets.js";
+import { getIntegrationTestDbPath, loadCoreSchema } from "./core_assets.js";
import NodejsCryptoProvider from "./crypto_provider.js";
import NodejsInAppHelpProvider from "./in_app_help_provider.js";
import ServerPlatformProvider from "./platform_provider.js";
@@ -30,11 +30,9 @@ async function startApplication() {
// Integration test mode: load the same fixture buffer used by the
// unit test setup so e2e tests get a known-good starting state
// (schema + demo content + known password) without touching disk.
- // The fixture path is marked external in the esbuild config, so
- // require.resolve here doesn't trigger a build-time warning.
- dbProvider.loadFromBuffer(fs.readFileSync(
- require.resolve("@triliumnext/core/src/test/fixtures/document.db")
- ));
+ // getIntegrationTestDbPath() handles the bundled-vs-source path
+ // resolution; see core_assets.ts.
+ dbProvider.loadFromBuffer(fs.readFileSync(getIntegrationTestDbPath()));
} else {
dbProvider.loadFromFile(DOCUMENT_PATH, config.General.readOnly);
}
diff --git a/apps/server/src/routes/api/database.ts b/apps/server/src/routes/api/database.ts
index 05beca3e67..08dc05121e 100644
--- a/apps/server/src/routes/api/database.ts
+++ b/apps/server/src/routes/api/database.ts
@@ -3,6 +3,7 @@ import { becca_loader, ValidationError } from "@triliumnext/core";
import type { Request } from "express";
import { readFileSync } from "fs";
+import { getIntegrationTestDbPath } from "../../core_assets.js";
import anonymizationService from "../../services/anonymization.js";
import backupService from "../../services/backup.js";
import consistencyChecksService from "../../services/consistency_checks.js";
@@ -34,7 +35,9 @@ async function rebuildIntegrationTestDatabase() {
// Reload the integration test database fixture into the in-memory SQL
// backend, then re-init schema-dependent state and the becca cache.
// Test-mode only — registered in routes.ts under the same env-var guard.
- const fixtureBytes = readFileSync(require.resolve("@triliumnext/core/src/test/fixtures/document.db"));
+ // getIntegrationTestDbPath() handles the bundled-vs-source path
+ // resolution; see core_assets.ts.
+ const fixtureBytes = readFileSync(getIntegrationTestDbPath());
sql.rebuildFromBuffer(fixtureBytes);
sql_init.initializeDb();
becca_loader.load();
diff --git a/apps/server/src/share/sql.ts b/apps/server/src/share/sql.ts
index e5d03e63b4..512ada71ab 100644
--- a/apps/server/src/share/sql.ts
+++ b/apps/server/src/share/sql.ts
@@ -1,6 +1,7 @@
"use strict";
import Database from "better-sqlite3";
+import { getIntegrationTestDbPath } from "../core_assets.js";
import dataDir from "../services/data_dir.js";
import sql_init from "../services/sql_init.js";
@@ -12,11 +13,10 @@ sql_init.dbReady.then(() => {
// database for isolation from the main read/write connection. In
// integration test mode `dataDir.DOCUMENT_PATH` doesn't contain a real
// database (the main connection is in-memory, loaded from a fixture
- // buffer), so we open the fixture file directly from its new location
- // in core. The share connection still gets its own independent
- // read-only handle, matching production semantics.
+ // buffer), so we open the fixture file directly. getIntegrationTestDbPath
+ // handles bundled-vs-source path resolution; see core_assets.ts.
const dbPath = process.env.TRILIUM_INTEGRATION_TEST
- ? require.resolve("@triliumnext/core/src/test/fixtures/document.db")
+ ? getIntegrationTestDbPath()
: dataDir.DOCUMENT_PATH;
dbConnection = new Database(dbPath, {