Skip to content

Build your personal knowledge base with Trilium Notes

e2e: use different mechanism that doesn't bundle the test database

Elian Doran87ca7aefd0a6
diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.yml
index a24dbb57d2..90e593d47a 100644
--- a/.github/workflows/playwright.yml
+++ b/.github/workflows/playwright.yml
@@ -86,6 +86,9 @@ jobs:
os: linux
arch: ${{ matrix.arch }}
+ - name: Copy test fixture database
+ run: cp packages/trilium-core/src/test/fixtures/document.db apps/server/spec/db/document.db
+
- name: Unpack and start the server
run: |
version=$(node --eval "console.log(require('./package.json').version)")
diff --git a/apps/server/playwright.config.ts b/apps/server/playwright.config.ts
index f3a73101e1..b5c9495de3 100644
--- a/apps/server/playwright.config.ts
+++ b/apps/server/playwright.config.ts
@@ -1,8 +1,21 @@
+import { cpSync } from "fs";
+import { join } from "path";
+
import { createBaseConfig } from "../../packages/trilium-e2e/src/base-config";
const port = process.env["TRILIUM_PORT"] ?? "8082";
const baseURL = process.env["BASE_URL"] || `http://127.0.0.1:${port}`;
+// Copy the test fixture database into the spec/db data directory so the
+// server finds it as a regular file-backed database. In CI the workflow
+// handles this, but for local runs we do it here.
+if (!process.env.TRILIUM_DOCKER) {
+ cpSync(
+ join(__dirname, "../../packages/trilium-core/src/test/fixtures/document.db"),
+ join(__dirname, "spec/db/document.db")
+ );
+}
+
export default createBaseConfig({
appDir: __dirname,
localTestDir: "e2e",
@@ -15,7 +28,7 @@ export default createBaseConfig({
env: {
TRILIUM_DATA_DIR: "spec/db",
TRILIUM_PORT: port,
- TRILIUM_INTEGRATION_TEST: "memory"
+ TRILIUM_INTEGRATION_TEST: "memory",
},
timeout: 5 * 60 * 1000
} : undefined,
diff --git a/apps/server/src/core_assets.ts b/apps/server/src/core_assets.ts
index eabac3392a..6528a3f970 100644
--- a/apps/server/src/core_assets.ts
+++ b/apps/server/src/core_assets.ts
@@ -22,23 +22,3 @@ 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 f4aa0c1151..c0d1f8b207 100644
--- a/apps/server/src/main.ts
+++ b/apps/server/src/main.ts
@@ -10,7 +10,7 @@ import path from "path";
import ServerBackupService from "./backup_provider.js";
import ClsHookedExecutionContext from "./cls_provider.js";
-import { getIntegrationTestDbPath, loadCoreSchema } from "./core_assets.js";
+import { loadCoreSchema } from "./core_assets.js";
import NodejsCryptoProvider from "./crypto_provider.js";
import NodejsInAppHelpProvider from "./in_app_help_provider.js";
import ServerLogService from "./log_provider.js";
@@ -29,12 +29,10 @@ async function startApplication() {
const dbProvider = new BetterSqlite3Provider();
if (process.env.TRILIUM_INTEGRATION_TEST === "memory") {
- // 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.
- // getIntegrationTestDbPath() handles the bundled-vs-source path
- // resolution; see core_assets.ts.
- dbProvider.loadFromBuffer(fs.readFileSync(getIntegrationTestDbPath()));
+ // Load the database file into memory so mutations don't persist to
+ // disk between test runs. The fixture is provided externally (e.g.
+ // copied into the data dir by CI or the Playwright config).
+ dbProvider.loadFromBuffer(fs.readFileSync(DOCUMENT_PATH));
} 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 a5f810daf2..d349b6fafa 100644
--- a/apps/server/src/routes/api/database.ts
+++ b/apps/server/src/routes/api/database.ts
@@ -4,7 +4,6 @@ import type { Request, Response } from "express";
import fs, { readFileSync } from "fs";
import path from "path";
-import { getIntegrationTestDbPath } from "../../core_assets.js";
import anonymizationService from "../../services/anonymization.js";
import consistencyChecksService from "../../services/consistency_checks.js";
import dataDir from "../../services/data_dir.js";
@@ -33,12 +32,7 @@ function findAndFixConsistencyIssues() {
}
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.
- // getIntegrationTestDbPath() handles the bundled-vs-source path
- // resolution; see core_assets.ts.
- const fixtureBytes = readFileSync(getIntegrationTestDbPath());
+ const fixtureBytes = readFileSync(dataDir.DOCUMENT_PATH);
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 512ada71ab..5a127e791f 100644
--- a/apps/server/src/share/sql.ts
+++ b/apps/server/src/share/sql.ts
@@ -1,7 +1,6 @@
"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";
@@ -9,17 +8,7 @@ let dbConnection!: Database.Database;
let dbConnectionReady = false;
sql_init.dbReady.then(() => {
- // The share module opens its own read-only connection to the on-disk
- // 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. getIntegrationTestDbPath
- // handles bundled-vs-source path resolution; see core_assets.ts.
- const dbPath = process.env.TRILIUM_INTEGRATION_TEST
- ? getIntegrationTestDbPath()
- : dataDir.DOCUMENT_PATH;
-
- dbConnection = new Database(dbPath, {
+ dbConnection = new Database(dataDir.DOCUMENT_PATH, {
readonly: true,
nativeBinding: process.env.BETTERSQLITE3_NATIVE_PATH || undefined
});