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
client/cleanup tool: update defaults
Adorian Doran828f717b6860
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.spec.tsxindex d188bcc466..df54c3fa16 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.spec.tsx@@ -30,6 +30,8 @@ const {const mocks = vi.hoisted(() => ({getInt: vi.fn<(name: string) => number | null>(() => -1),+ /** Reads the `revisionIgnoreNamedSnapshots` option, which the named-revisions field follows. */+ is: vi.fn<(name: string) => boolean>(() => false),storedOption: "{}",/** Occupied bytes handed to the run's before/after weighings, in that order. */weighed: [] as number[],@@ -49,6 +51,7 @@ vi.mock("../../../../../services/options", () => ({default: {get: () => mocks.storedOption,getInt: mocks.getInt,+ is: mocks.is,save: mocks.save}}));@@ -172,6 +175,11 @@ async function toggle(row: HTMLElement | undefined) {});}+/** Picks the rebuild, which never carries over from a previous run and so is always picked by hand. */+async function toggleCompacting() {+ await toggle(document.body.querySelector<HTMLElement>(".cleanup-item-compact") ?? undefined);+}+async function click(button: HTMLButtonElement | undefined) {await act(async () => {button?.dispatchEvent(new MouseEvent("click", { bubbles: true }));@@ -285,15 +293,16 @@ describe("showCleanupDialog", () => {// Picked on its own, it is the whole of the offer — and worth running, though it erases// nothing, so the button has to allow it.- await toggle(document.body.querySelector<HTMLElement>(".cleanup-item-compact") ?? undefined);+ await toggleCompacting();expect(textOf(".cleanup-chart-amount")).toBe(formatSize(FREE_PAGES));expect(cleanButton()?.disabled).toBe(false);});it("keeps a spinner up for the run, naming the rebuild while it holds the server", async () => {- mocks.storedOption = JSON.stringify({ unusedAttachments: true, compactDatabase: true });+ mocks.storedOption = JSON.stringify({ unusedAttachments: true });const { closed } = await openDialog();+ await toggleCompacting();await click(cleanButton());// The rebuild's own before/after is what a compacting run reports, not the content readings.await expect(closed).resolves.toBe(VACUUM_BEFORE - VACUUM_AFTER);@@ -316,9 +325,10 @@ describe("showCleanupDialog", () => {});it("reports the file's own reduction as the whole of what a compacting run reclaimed", async () => {- mocks.storedOption = JSON.stringify({ unusedAttachments: true, compactDatabase: true });+ mocks.storedOption = JSON.stringify({ unusedAttachments: true });const { closed } = await openDialog();+ await toggleCompacting();await click(cleanButton());// The rebuild returns the pages the erasures freed along with everything else, so its own@@ -346,8 +356,8 @@ describe("showCleanupDialog", () => {// An erasure-only run is over in seconds; there is nothing to warn about waiting for.expect(await confirmationHtml()).not.toContain("admonition");- mocks.storedOption = JSON.stringify({ unusedAttachments: true, compactDatabase: true });await openDialog();+ await toggleCompacting();await click(cleanButton());const warned = await confirmationHtml();diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.tsxindex efc9fb869f..c56a40167b 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.tsx@@ -25,7 +25,8 @@ import {hasWorkToDo,measureCompactionEstimate,readCleanupOptions,- runCleanup+ runCleanup,+ storedCleanupOptions} from "./cleanup_operation";import { useSpaceUsageFetch } from "./use_space_usage_fetch";@@ -69,7 +70,7 @@ function CleanupDialog({ onFinished }: { onFinished: (reclaimed: number | null)const update = (patch: Partial<CleanupToolOptions>) => {const next = { ...options, ...patch };setOptions(next);- void setStored(next);+ void setStored(storedCleanupOptions(next));};// Measured with the history erased outright: the whole the reading is "of", and fixed for asdiff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.spec.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.spec.tsindex 6f194a0b73..958278288f 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.spec.ts+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.spec.ts@@ -3,6 +3,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest";const mocks = vi.hoisted(() => ({getInt: vi.fn<(name: string) => number | null>(() => -1),+ /** Reads the `revisionIgnoreNamedSnapshots` option, which the named-revisions field follows. */+ is: vi.fn<(name: string) => boolean>(() => true),post: vi.fn(async (url: string, _body?: object) => (url === "database/vacuum-database" ? { sizeBefore: 9000, sizeAfter: 2000 } : undefined)),@@ -11,7 +13,7 @@ const mocks = vi.hoisted(() => ({get: vi.fn()}));-vi.mock("../../../../../services/options", () => ({ default: { getInt: mocks.getInt } }));+vi.mock("../../../../../services/options", () => ({ default: { getInt: mocks.getInt, is: mocks.is } }));// The run reaches for the long-timeout variants throughout: every request it makes is a// whole-database operation. The timeout itself is dropped here, leaving (url, data) as the calls// under test.@@ -34,7 +36,8 @@ import {type CleanupToolOptions,computeCleanupSizes,readCleanupOptions,- runCleanup+ runCleanup,+ storedCleanupOptions} from "./cleanup_operation";/** Only the fields the sizes are read from; the rest of the response is beside the point here. */@@ -57,6 +60,7 @@ const ALL_PICKED: CleanupToolOptions = {beforeEach(() => {mocks.getInt.mockReturnValue(-1);+ mocks.is.mockReturnValue(true);mocks.occupied = [ 5000, 3000 ];mocks.post.mockClear();mocks.get.mockClear();@@ -64,6 +68,8 @@ beforeEach(() => {describe("readCleanupOptions", () => {it("picks nothing at all when the setting has never been written", () => {+ mocks.is.mockReturnValue(false);+// The only safe reading of an absent answer, for an operation that deletes without recourse.for (const stored of [ undefined, null, {} ]) {expect(readCleanupOptions(stored)).toEqual({@@ -77,6 +83,22 @@ describe("readCleanupOptions", () => {}});+ it("follows the note revision setting on named revisions until it is answered here", () => {+ expect(readCleanupOptions({}).keepNamedSnapshots).toBe(true);++ // Once answered, the stored answer stands — including the one that matches no setting.+ expect(readCleanupOptions({ keepNamedSnapshots: false }).keepNamedSnapshots).toBe(false);++ mocks.is.mockReturnValue(false);+ expect(readCleanupOptions({}).keepNamedSnapshots).toBe(false);+ expect(readCleanupOptions({ keepNamedSnapshots: true }).keepNamedSnapshots).toBe(true);+ });++ it("never opens on compacting, however the last run left it", () => {+ // The most expensive step by far, and not one to run because a tick was left behind.+ expect(readCleanupOptions({ compactDatabase: true }).compactDatabase).toBe(false);+ });+it("opens on the configured revision limit, where that limit trims anything", () => {mocks.getInt.mockReturnValue(7);expect(readCleanupOptions({}).snapshotsToKeep).toBe(7);@@ -104,6 +126,18 @@ describe("readCleanupOptions", () => {});});+describe("storedCleanupOptions", () => {+ it("remembers every answer but the compacting step", () => {+ expect(storedCleanupOptions({ ...ALL_PICKED, compactDatabase: true })).toEqual({+ deletedEntities: true,+ unusedAttachments: true,+ revisionSnapshots: true,+ snapshotsToKeep: 3,+ keepNamedSnapshots: true+ });+ });+});+describe("computeCleanupSizes", () => {it("weighs the whole at the most aggressive trim, and the offer at the chosen one", () => {const sizes = computeCleanupSizes(usage(900, 100, 20), usage(300), ALL_PICKED);diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.tsindex 1bca20bc44..f452b0a482 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.ts+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.ts@@ -26,8 +26,9 @@ export type CleanupPhase = "erasing" | "compacting";/**- * What the cleanup tool is set to erase, persisted as JSON in the `cleanupToolOptions` option so the- * next run opens on the last answer rather than on a fresh set of defaults.+ * What the cleanup tool is set to erase. Persisted as JSON in the `cleanupToolOptions` option — all+ * but the compacting step, see {@link storedCleanupOptions} — so the next run opens on the last+ * answer rather than on a fresh set of defaults.*/export interface CleanupToolOptions {deletedEntities: boolean;@@ -69,11 +70,25 @@ export function readCleanupOptions(stored: Partial<CleanupToolOptions> | null |snapshotsToKeep: Number.isInteger(snapshotsToKeep) && Number(snapshotsToKeep) >= 0? Number(snapshotsToKeep): defaultSnapshotsToKeep(),- keepNamedSnapshots: stored?.keepNamedSnapshots === true,- compactDatabase: stored?.compactDatabase === true+ // Never answered here, the tool follows what the note revision settings say about named+ // revisions, rather than proposing a harsher trim than the one already configured.+ keepNamedSnapshots: stored?.keepNamedSnapshots ?? optionService.is("revisionIgnoreNamedSnapshots"),+ // Always starts unpicked, whatever the last run asked for: it is by far the most expensive+ // step, and one nobody should find themselves running because they forgot it was ticked.+ compactDatabase: false};}+/**+ * What is worth remembering from a run: everything but the compacting step, which is deliberately+ * asked for afresh each time (see {@link readCleanupOptions}) and so is never written back.+ */+export function storedCleanupOptions(options: CleanupToolOptions): Partial<CleanupToolOptions> {+ const { compactDatabase: _unremembered, ...remembered } = options;++ return remembered;+}+/*** Whether the settings amount to anything worth running. Compacting counts on its own: it frees no* *accounted* bytes — the figures are about content, and it moves none — but it is the only step