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
refactor: clean up some assumptions in tests
Elian Doran3a6decba7358
diff --git a/apps/client/src/services/debounce.spec.ts b/apps/client/src/services/debounce.spec.tsindex e2bf560a0b..5ec4469f7c 100644--- a/apps/client/src/services/debounce.spec.ts+++ b/apps/client/src/services/debounce.spec.ts@@ -116,6 +116,24 @@ describe("debounce", () => {expect(fn).toHaveBeenCalledTimes(1);});+ it("flush() does not re-fire after a leading-edge call when `immediate` is true", () => {+ const fn = vi.fn((x: string) => x.toUpperCase());+ const debounced = debounce(fn, 100, true) as Debounced;++ // Leading-edge call fires once and nulls `args` while the trailing timer stays live.+ expect(debounced("hi")).toBe("HI");+ expect(fn).toHaveBeenCalledTimes(1);++ // Flushing now must NOT re-invoke func: there is no pending trailing call,+ // and it must never call func with an empty arg list.+ debounced.flush();+ expect(fn).toHaveBeenCalledTimes(1);++ // The pending trailing timer was cleared by flush, so advancing does nothing.+ vi.advanceTimersByTime(100);+ expect(fn).toHaveBeenCalledTimes(1);+ });+it("reschedules in `later` when the system clock jumps backwards (last < 0)", () => {const fn = vi.fn();const debounced = debounce(fn, 100) as Debounced;diff --git a/apps/client/src/services/debounce.ts b/apps/client/src/services/debounce.tsindex 74d0f6a326..143b37ec0a 100644--- a/apps/client/src/services/debounce.ts+++ b/apps/client/src/services/debounce.ts@@ -59,9 +59,13 @@ function debounce<T>(func: (...args: any[]) => T, waitMs: number, immediate: boodebounced.flush = function () {if (timeout) {- /* v8 ignore next -- `|| []` is a defensive fallback: a live `timeout` implies `debounced` set `args` and no fire has nulled it yet */- result = func.apply(context, args || []);- context = args = null;+ // In immediate mode the leading-edge fire nulls `args` while leaving the+ // trailing timer live, so only invoke when there is actually a pending+ // trailing call to flush — otherwise this would re-fire `func` with no args.+ if (args) {+ result = func.apply(context, args);+ context = args = null;+ }clearTimeout(timeout);timeout = null;diff --git a/apps/server/src/services/image_provider.ts b/apps/server/src/services/image_provider.tsindex d4229ff53a..d2d267a64c 100644--- a/apps/server/src/services/image_provider.ts+++ b/apps/server/src/services/image_provider.ts@@ -93,10 +93,13 @@ export const serverImageProvider: ImageProvider = {if (!origImageFormat || !["jpg", "png"].includes(origImageFormat.ext)) {shouldShrink = false;- /* v8 ignore start -- defensive guard: file-type classifies animated PNG as- "apng" and animated GIF/WebP as non-jpg/png, so they are already excluded- above, making this branch effectively unreachable. */+ /* v8 ignore start -- rare defensive guard: spec-compliant animated images are+ already excluded above (file-type reports animated PNG as "apng" and animated+ GIF/WebP as gif/webp). Only a pathological PNG with 512+ chunks before its acTL+ chunk slips through (file-type bails to "png" at its chunk-scan limit while+ is-animated still flags it), so this guard correctly skips recompressing it. */} else if (isAnimated(Buffer.from(buffer))) {+ // Recompression of animated images would make them static.shouldShrink = false;}/* v8 ignore stop */diff --git a/apps/server/src/services/llm/tools/hierarchy_tools.ts b/apps/server/src/services/llm/tools/hierarchy_tools.tsindex f27277acb8..fd06b27863 100644--- a/apps/server/src/services/llm/tools/hierarchy_tools.ts+++ b/apps/server/src/services/llm/tools/hierarchy_tools.ts@@ -121,7 +121,10 @@ export const hierarchyTools = defineTools({// Use the first (primary) parent branch for the moveconst branches = note.getParentBranches();- /* v8 ignore next 3 -- defensive: a non-system note always has at least one parent branch */+ /* v8 ignore next 3 -- rare edge case, not worth simulating in a unit test:+ an orphaned note (its last parent branch removed but the note itself not+ yet deleted from Becca, e.g. during a sync race) can momentarily have zero+ parent branches. The guard returns a clean error instead of crashing on branches[0]. */if (branches.length === 0) {return { error: "Note has no parent branches" };}