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
chore(core): integrate sync timeout migration
Elian Dorancae3d1480442
diff --git a/apps/server/src/services/data_dir.spec.ts b/apps/server/src/services/data_dir.spec.tsindex db9d47e991..223e17693d 100644--- a/apps/server/src/services/data_dir.spec.ts+++ b/apps/server/src/services/data_dir.spec.ts@@ -16,6 +16,11 @@ describe("data_dir.ts unit tests", async () => {pathJoinMock: vi.fn()};+ // Reset the module cache so the dynamic imports below get a fresh instance+ // of data_dir.ts with the mocked dependencies rather than the cached copy+ // loaded by spec/setup.ts.+ vi.resetModules();+// using doMock, to avoid hoisting, so that we can use the mockFn object// to collect all mocked Fnsvi.doMock("node:fs", () => {@@ -37,7 +42,7 @@ describe("data_dir.ts unit tests", async () => {};});- vi.doMock("path", () => {+ vi.doMock("node:path", () => {return {join: mockFn.pathJoinMock};diff --git a/apps/server/src/services/data_dir.ts b/apps/server/src/services/data_dir.tsindex 066bc89335..a3ed9f2bae 100644--- a/apps/server/src/services/data_dir.ts+++ b/apps/server/src/services/data_dir.ts@@ -6,9 +6,9 @@* - case D) as a fallback if the previous step fails, we'll use home dir*/-import fs from "fs";-import os from "os";-import { join as pathJoin } from "path";+import fs from "node:fs";+import os from "node:os";+import { join as pathJoin } from "node:path";const DIR_NAME = "trilium-data";const FOLDER_PERMISSIONS = 0o700;diff --git a/apps/server/src/services/sync_options.spec.ts b/apps/server/src/services/sync_options.spec.tsdeleted file mode 100644index 16d048b683..0000000000--- a/apps/server/src/services/sync_options.spec.ts+++ /dev/null@@ -1,48 +0,0 @@-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";--// Mock the dependencies before importing the module-vi.mock("./config.js", () => ({ default: { Sync: {} } }));-vi.mock("./options.js", () => ({ default: { getOption: vi.fn() } }));--import config from "./config.js";-import optionService from "./options.js";-import syncOptions from "./sync_options.js";--describe("syncOptions.getSyncTimeout", () => {- beforeEach(() => {- (config as any).Sync = {};- });-- afterEach(() => {- vi.clearAllMocks();- });-- it("converts database value from seconds to milliseconds", () => {- // TimeSelector stores value in seconds (displayed value × scale)- // Scale is UI-only, not used in backend calculation- vi.mocked(optionService.getOption).mockReturnValue("120"); // 120 seconds = 2 minutes- expect(syncOptions.getSyncTimeout()).toBe(120000);-- vi.mocked(optionService.getOption).mockReturnValue("30"); // 30 seconds- expect(syncOptions.getSyncTimeout()).toBe(30000);-- vi.mocked(optionService.getOption).mockReturnValue("3600"); // 3600 seconds = 1 hour- expect(syncOptions.getSyncTimeout()).toBe(3600000);- });-- it("treats config override as raw milliseconds for backward compatibility", () => {- (config as any).Sync = { syncServerTimeout: "60000" }; // 60 seconds in ms-- // Config value takes precedence, db value is ignored- vi.mocked(optionService.getOption).mockReturnValue("9999");- expect(syncOptions.getSyncTimeout()).toBe(60000);- });-- it("uses safe defaults for invalid values", () => {- vi.mocked(optionService.getOption).mockReturnValue("");- expect(syncOptions.getSyncTimeout()).toBe(120000); // default 120 seconds-- (config as any).Sync = { syncServerTimeout: "invalid" };- expect(syncOptions.getSyncTimeout()).toBe(120000); // fallback for invalid config- });-});diff --git a/packages/trilium-core/src/services/sync_options.spec.ts b/packages/trilium-core/src/services/sync_options.spec.tsnew file mode 100644index 0000000000..ca61914d88--- /dev/null+++ b/packages/trilium-core/src/services/sync_options.spec.ts@@ -0,0 +1,54 @@+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++describe("syncOptions.getSyncTimeout", () => {+ let getSyncTimeout: () => number;+ let getOptionMock: ReturnType<typeof vi.fn>;+ let mockSyncConfig: Record<string, string | undefined>;++ beforeEach(async () => {+ // Reset the module cache so the dynamic import below gets a fresh+ // instance of sync_options.ts with the mocked dependencies rather than+ // the cached copy loaded by the test runner's setupFiles.+ vi.resetModules();+ mockSyncConfig = {};+ getOptionMock = vi.fn();++ vi.doMock("./config.js", () => ({ default: { Sync: mockSyncConfig } }));+ vi.doMock("./options.js", () => ({ default: { getOption: getOptionMock } }));++ const mod = await import("./sync_options.js");+ getSyncTimeout = mod.default.getSyncTimeout;+ });++ afterEach(() => {+ vi.clearAllMocks();+ });++ it("converts database value from seconds to milliseconds", () => {+ // TimeSelector stores value in seconds (displayed value × scale)+ // Scale is UI-only, not used in backend calculation+ getOptionMock.mockReturnValue("120"); // 120 seconds = 2 minutes+ expect(getSyncTimeout()).toBe(120000);++ getOptionMock.mockReturnValue("30"); // 30 seconds+ expect(getSyncTimeout()).toBe(30000);++ getOptionMock.mockReturnValue("3600"); // 3600 seconds = 1 hour+ expect(getSyncTimeout()).toBe(3600000);+ });++ it("treats config override as raw milliseconds for backward compatibility", () => {+ mockSyncConfig.syncServerTimeout = "60000"; // 60 seconds in ms+ // Config value takes precedence, db value is ignored+ getOptionMock.mockReturnValue("9999");+ expect(getSyncTimeout()).toBe(60000);+ });++ it("uses safe defaults for invalid values", () => {+ getOptionMock.mockReturnValue("");+ expect(getSyncTimeout()).toBe(120000); // default 120 seconds++ mockSyncConfig.syncServerTimeout = "invalid";+ expect(getSyncTimeout()).toBe(120000); // fallback for invalid config+ });+});diff --git a/packages/trilium-core/src/services/sync_options.ts b/packages/trilium-core/src/services/sync_options.tsindex a94dbd12f1..48fac8cc1e 100644--- a/packages/trilium-core/src/services/sync_options.ts+++ b/packages/trilium-core/src/services/sync_options.ts@@ -29,6 +29,14 @@ export default {// and we need to override it with config from config.inireturn !!syncServerHost && syncServerHost !== "disabled";},- getSyncTimeout: () => parseInt(get("syncServerTimeout")) || 120000,+ getSyncTimeout: () => {+ // Config.ini values are in raw milliseconds (backward compat with old configs)+ if (config["Sync"] && config["Sync"]["syncServerTimeout"]) {+ return parseInt(config["Sync"]["syncServerTimeout"]) || 120000;+ }+ // Database values are stored in seconds — convert to milliseconds+ const seconds = parseInt(optionService.getOption("syncServerTimeout"));+ return (isNaN(seconds) || seconds <= 0) ? 120000 : seconds * 1000;+ },getSyncProxy: () => get("syncProxy")};