fix(mongodb): keep collection results across tab switches

This commit is contained in:
onenewcode authored and GitHub committed 2026-09-10 12:46:18 +08:00
1 parent 1e9a3c0f67
commit 087351f73f
11 files changed
+826 -47

No files matched your search

@@ -1,5 +1,18 @@
import { describe, expect, it } from "vitest";
import { clearDocumentBrowserState, restoreDocumentBrowserState, saveDocumentBrowserState, type DocumentBrowserStateSnapshot } from "@/lib/tabs/documentBrowserStateCache";
import { beforeEach, describe, expect, it } from "vitest";
import {
beginClosingBrowserState,
clearDocumentBrowserState,
clearGridFsBrowserState,
MAX_DATA_ENTRIES,
MAX_RESTORED_DOCUMENT_ROWS,
resetBrowserStateCaches,
restoreDocumentBrowserState,
restoreGridFsBrowserState,
saveDocumentBrowserState,
saveGridFsBrowserState,
type DocumentBrowserDataSnapshot,
type DocumentBrowserStateSnapshot,
} from "@/lib/tabs/documentBrowserStateCache";
function snapshot(page: number): DocumentBrowserStateSnapshot {
return {
@@ -11,6 +24,25 @@ function snapshot(page: number): DocumentBrowserStateSnapshot {
};
}
function data(rowCount: number, signature = "sig"): DocumentBrowserDataSnapshot {
return {
signature,
documents: Array.from({ length: rowCount }, (_, index) => ({ _id: String(index) })),
copyDocuments: [],
copyDocumentsAvailable: false,
gridColumns: ["_id"],
gridColumnTypes: ["objectId"],
total: rowCount,
totalIsExact: true,
paginationTotal: rowCount,
selectedIdx: null,
};
}
beforeEach(() => {
resetBrowserStateCaches();
});
describe("documentBrowserStateCache", () => {
it("round-trips a snapshot for the same key", () => {
saveDocumentBrowserState("round-trip", snapshot(3));
@@ -56,4 +88,75 @@ describe("documentBrowserStateCache", () => {
expect(restoreDocumentBrowserState("clear-me")).toBeUndefined();
});
it("round-trips a row payload alongside the conditions", () => {
saveDocumentBrowserState("with-data", { ...snapshot(1), data: data(2) });
expect(restoreDocumentBrowserState("with-data")?.data?.documents).toHaveLength(2);
});
it("drops a stored payload on a conditions-only save", () => {
saveDocumentBrowserState("drop-data", { ...snapshot(1), data: data(2) });
saveDocumentBrowserState("drop-data", snapshot(1));
expect(restoreDocumentBrowserState("drop-data")?.data).toBeUndefined();
});
it("drops a payload larger than the row bound but keeps the conditions", () => {
saveDocumentBrowserState("too-big", { ...snapshot(2), data: data(MAX_RESTORED_DOCUMENT_ROWS + 1) });
const restored = restoreDocumentBrowserState("too-big");
expect(restored?.data).toBeUndefined();
expect(restored?.filterInput).toBe(snapshot(2).filterInput);
});
it("keeps payloads only for the most recently used tabs", () => {
for (let index = 0; index < MAX_DATA_ENTRIES + 2; index += 1) {
saveDocumentBrowserState(`payload-${index}`, { ...snapshot(index), data: data(1) });
}
expect(restoreDocumentBrowserState("payload-0")?.data).toBeUndefined();
expect(restoreDocumentBrowserState("payload-0")?.filterInput).toBe(snapshot(0).filterInput);
expect(restoreDocumentBrowserState(`payload-${MAX_DATA_ENTRIES + 1}`)?.data).toBeDefined();
});
it("blocks the unmount capture from resurrecting a closed tab", () => {
saveDocumentBrowserState("closing", { ...snapshot(1), data: data(1) });
beginClosingBrowserState("closing");
// DocumentBrowser unmounts after the store removed the tab and writes again.
saveDocumentBrowserState("closing", { ...snapshot(1), data: data(1) });
expect(restoreDocumentBrowserState("closing")).toBeUndefined();
});
});
describe("gridFsBrowserStateCache", () => {
it("round-trips conditions and rows", () => {
saveGridFsBrowserState("gridfs-a", { filterInput: "{}", sortInput: "", signature: "sig", rows: [{ name: "fs" }] });
const restored = restoreGridFsBrowserState<{ name: string }>("gridfs-a");
expect(restored?.rows).toEqual([{ name: "fs" }]);
expect(restored?.signature).toBe("sig");
});
it("drops an oversized listing but keeps the conditions", () => {
const rows = Array.from({ length: MAX_RESTORED_DOCUMENT_ROWS + 1 }, (_, index) => ({ name: String(index) }));
saveGridFsBrowserState("gridfs-big", { filterInput: "{}", sortInput: "", signature: "sig", rows });
const restored = restoreGridFsBrowserState<{ name: string }>("gridfs-big");
expect(restored?.rows).toBeUndefined();
expect(restored?.signature).toBeUndefined();
expect(restored?.filterInput).toBe("{}");
});
it("clears a key on demand and on close", () => {
saveGridFsBrowserState("gridfs-clear", { filterInput: "", sortInput: "" });
clearGridFsBrowserState("gridfs-clear");
expect(restoreGridFsBrowserState("gridfs-clear")).toBeUndefined();
saveGridFsBrowserState("gridfs-closing", { filterInput: "", sortInput: "" });
beginClosingBrowserState("gridfs-closing");
saveGridFsBrowserState("gridfs-closing", { filterInput: "", sortInput: "" });
expect(restoreGridFsBrowserState("gridfs-closing")).toBeUndefined();
});
});