Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
core/start again: fix an issue
Adorian Doranbe738bbfd42f
diff --git a/packages/trilium-core/src/routes/api/setup.spec.ts b/packages/trilium-core/src/routes/api/setup.spec.tsindex 496d7ac883..cd28610f63 100644--- a/packages/trilium-core/src/routes/api/setup.spec.ts+++ b/packages/trilium-core/src/routes/api/setup.spec.ts@@ -164,7 +164,11 @@ describe("Setup API with a knowledge base behind the wizard (core)", () => {expect(createInitial).toHaveBeenCalled();});- it("erases it before syncing from a server, for the same reason", async () => {+ it("leaves the erasing to the sync itself, which alone knows when it is safe", async () => {+ // Deliberately not here: reaching a sync server can fail on a mistyped host, a refused+ // password or a version mismatch, and every one of those has to leave the knowledge base+ // where it was. `setupSyncFromSyncServer` erases once the server has answered — see the+ // service's own spec, which pins that ordering.vi.spyOn(setupService, "setupSyncFromSyncServer").mockResolvedValue({ result: "success" });const res = await api.post("/api/setup/sync-from-server", {@@ -172,7 +176,7 @@ describe("Setup API with a knowledge base behind the wizard (core)", () => {});expect(res.status).toBe(200);- expect(platform.removeDatabase).toHaveBeenCalledOnce();+ expect(platform.removeDatabase).not.toHaveBeenCalled();});it("refuses a pushed sync seed until the local user has cleared the way for it", async () => {diff --git a/packages/trilium-core/src/routes/api/setup.ts b/packages/trilium-core/src/routes/api/setup.tsindex 4b510b7ff6..133b8a7beb 100644--- a/packages/trilium-core/src/routes/api/setup.ts+++ b/packages/trilium-core/src/routes/api/setup.ts@@ -165,17 +165,19 @@ async function setupNewDocument(req: Request) {* The lock covers erasing what was there, fetching the seed and creating the schema, not the sync* that follows: an interrupted sync is resumed and retried across later requests, and a failed one* has to leave the user free to take another path instead.+ *+ * The erasure is not done here, unlike the path above it. Reaching a sync server can fail on a+ * mistyped host, a refused password or a version mismatch, and each of those has to leave the+ * knowledge base where it was — so it is done inside, once the server has answered and the next+ * step is the one that cannot be taken back.*/function setupSyncFromServer(req: Request): Promise<SetupSyncFromServerResponse> {const { syncServerHost, syncProxy, password, syncMaxBlobContentSize } = req.body;const maxBlobContentSize = Number.isFinite(syncMaxBlobContentSize) && syncMaxBlobContentSize > 0 ? syncMaxBlobContentSize : 0;- return withSetupLock("sync-from-server", async () => {- await discardExistingData();-- return setupService.setupSyncFromSyncServer(syncServerHost, syncProxy, password, maxBlobContentSize);- });+ return withSetupLock("sync-from-server", () =>+ setupService.setupSyncFromSyncServer(syncServerHost, syncProxy, password, maxBlobContentSize));}/**diff --git a/packages/trilium-core/src/services/setup.spec.ts b/packages/trilium-core/src/services/setup.spec.tsindex 39b901b0f4..41b5710df5 100644--- a/packages/trilium-core/src/services/setup.spec.ts+++ b/packages/trilium-core/src/services/setup.spec.ts@@ -6,6 +6,7 @@ import options from "./options.js";import { fakeRequestProvider } from "../test/request_provider.js";import { type ExecOpts, initRequest, type RequestProvider } from "./request.js";import setupService from "./setup.js";+import { enterSetupMode, initSetupPlatform, leaveSetupMode } from "./setup_mode.js";import sqlInit from "./sql_init.js";import syncService from "./sync.js";@@ -112,6 +113,77 @@ describe("setup service", () => {const result = await setupService.setupSyncFromSyncServer("http://srv", "", "pw");expect(result).toEqual({ result: "failure", error: "network down" });});++ describe("with a knowledge base still behind the wizard", () => {+ /** Records whether the erasure happened, and whether the schema was built before it. */+ function watchTheErasure() {+ const erased = { removeDatabase: false, beforeCreate: false };++ initSetupPlatform({+ writeMarker: async () => {},+ hasMarker: async () => false,+ removeMarker: async () => {},+ removeDatabase: async () => { erased.removeDatabase = true; }+ });+ enterSetupMode({ lang: "en" });++ vi.spyOn(sqlInit, "isDbInitialized").mockReturnValue(false);+ vi.spyOn(sqlInit, "createDatabaseForSync").mockImplementation(async () => {+ erased.beforeCreate = erased.removeDatabase;+ });+ vi.spyOn(syncService, "sync").mockResolvedValue({ success: true });++ return erased;+ }++ afterEach(() => leaveSetupMode());++ it("keeps it when the server cannot be reached", async () => {+ // A mistyped host, a proxy that is not there, a machine that is off. Every one of+ // them is something the user goes back and corrects — which they can only do if+ // what they were correcting it for is still there.+ const erased = watchTheErasure();+ execImpl = async () => {+ throw new Error("network down");+ };++ await expect(setupService.setupSyncFromSyncServer("http://srv", "", "pw"))+ .resolves.toMatchObject({ result: "failure" });+ expect(erased.removeDatabase).toBe(false);+ });++ it("keeps it when the password is refused", async () => {+ const erased = watchTheErasure();+ execImpl = async () => {+ throw new Error("Incorrect password");+ };++ await expect(setupService.setupSyncFromSyncServer("http://srv", "", "wrong"))+ .resolves.toMatchObject({ result: "failure" });+ expect(erased.removeDatabase).toBe(false);+ });++ it("keeps it when the server speaks a different sync version", async () => {+ const erased = watchTheErasure();+ execImpl = async () => ({ syncVersion: appInfo.syncVersion + 5, options: [] });++ await expect(setupService.setupSyncFromSyncServer("http://srv", "", "pw"))+ .resolves.toMatchObject({ result: "failure" });+ expect(erased.removeDatabase).toBe(false);+ });++ it("erases it only once the server has answered, and before the schema is built", async () => {+ // The schema is created by wiping whatever is in the way, so the erasure cannot come+ // after it either: there is exactly one place it belongs.+ const erased = watchTheErasure();+ execImpl = async () => ({ syncVersion: appInfo.syncVersion, options: [] });++ await expect(setupService.setupSyncFromSyncServer("http://srv", "", "pw"))+ .resolves.toEqual({ result: "success" });+ expect(erased.removeDatabase).toBe(true);+ expect(erased.beforeCreate).toBe(true);+ });+ });});describe("triggerSync", () => {diff --git a/packages/trilium-core/src/services/setup.ts b/packages/trilium-core/src/services/setup.tsindex a6361eb1bd..f95b213b09 100644--- a/packages/trilium-core/src/services/setup.ts+++ b/packages/trilium-core/src/services/setup.ts@@ -9,6 +9,7 @@ import { timeLimit } from "./utils/index.js";import becca from "../becca/becca.js";import type { SetupStatusResponse, SetupSyncFromServerResponse, SetupSyncSeedResponse } from "@triliumnext/commons";import request from "./request.js";+import { discardExistingData } from "./setup_existing.js";async function hasSyncServerSchemaAndSeed() {const response = await requestToSyncServer<SetupStatusResponse>("GET", "/api/setup/status");@@ -98,6 +99,12 @@ async function setupSyncFromSyncServer(syncServerHost: string, syncProxy: string};}+ // Not a step earlier. Everything above can fail on something the user can correct — a+ // mistyped host, a refused password, a server too old to talk to this one — and each of+ // those has to fail with the knowledge base still there to go back to. This is the first+ // move that cannot be taken back, and the erasure belongs immediately in front of it.+ await discardExistingData();+await sqlInit.createDatabaseForSync(resp.options, syncServerHost, syncProxy, syncMaxBlobContentSize);triggerSync();