Skip to content

Build your personal knowledge base with Trilium Notes

test(client): fix specs stale after getBlob and blob-stub changes

Elian Doran506a7fb20fbc
diff --git a/apps/client/src/services/froca.spec.ts b/apps/client/src/services/froca.spec.ts
index 937f840f9b..b7a5eb9412 100644
--- a/apps/client/src/services/froca.spec.ts
+++ b/apps/client/src/services/froca.spec.ts
@@ -509,13 +509,13 @@ describe("getAttachment / getAttachmentsForNote / processAttachmentRows", () =>
describe("getBlob", () => {
it("fetches and caches the blob promise, and serves a second call from cache", async () => {
- server.get = vi.fn(async () => ({ blobId: "blob-1", content: "hi", contentLength: 2 })) as typeof server.get;
+ server.getWithSilentNotFound = vi.fn(async () => ({ blobId: "blob-1", content: "hi", contentLength: 2 })) as typeof server.getWithSilentNotFound;
const blob1 = await froca.getBlob("notes", "blob-note");
const blob2 = await froca.getBlob("notes", "blob-note");
- expect(server.get).toHaveBeenCalledTimes(1);
- expect(server.get).toHaveBeenCalledWith("notes/blob-note/blob");
+ expect(server.getWithSilentNotFound).toHaveBeenCalledTimes(1);
+ expect(server.getWithSilentNotFound).toHaveBeenCalledWith("notes/blob-note/blob");
expect(blob1).not.toBeNull();
expect(blob2).toBe(blob1);
});
@@ -523,7 +523,7 @@ describe("getBlob", () => {
it("clears the cached blob promise after the cleanup timeout fires", async () => {
vi.useFakeTimers();
try {
- server.get = vi.fn(async () => ({ blobId: "blob-2", content: "x", contentLength: 1 })) as typeof server.get;
+ server.getWithSilentNotFound = vi.fn(async () => ({ blobId: "blob-2", content: "x", contentLength: 1 })) as typeof server.getWithSilentNotFound;
await froca.getBlob("notes", "blob-cleanup");
// The .then() that schedules the cleanup setTimeout runs on a microtask.
@@ -536,9 +536,9 @@ describe("getBlob", () => {
});
it("returns null and logs when the blob request fails", async () => {
- server.get = vi.fn(async () => {
+ server.getWithSilentNotFound = vi.fn(async () => {
throw new Error("blob boom");
- }) as typeof server.get;
+ }) as typeof server.getWithSilentNotFound;
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
const blob = await froca.getBlob("notes", "blob-fail");
diff --git a/apps/client/src/test/setup.ts b/apps/client/src/test/setup.ts
index 89ad69d622..bc5520b9a9 100644
--- a/apps/client/src/test/setup.ts
+++ b/apps/client/src/test/setup.ts
@@ -30,27 +30,33 @@ function mockWebsocket() {
}
function mockServer() {
- return {
- default: {
- async get(url: string) {
- if (url === "options") {
- return {};
- }
+ async function get(url: string) {
+ if (url === "options") {
+ return {};
+ }
- if (url === "keyboard-actions") {
- return [];
- }
+ if (url === "keyboard-actions") {
+ return [];
+ }
- if (url === "tree") {
- return {
- branches: [],
- notes: [],
- attributes: []
- };
- }
+ if (url === "tree") {
+ return {
+ branches: [],
+ notes: [],
+ attributes: []
+ };
+ }
+
+ console.warn(`Unsupported GET to mocked server: ${url}`);
+ }
+
+ return {
+ default: {
+ get,
- console.warn(`Unsupported GET to mocked server: ${url}`);
- },
+ // Froca's blob and attachment loads go through this variant; it only differs from `get`
+ // in how it reports 404s, which the mock never produces, so share the same routing.
+ getWithSilentNotFound: get,
async post(url: string, data: object) {
if (url === "tree/load") {
diff --git a/apps/client/src/widgets/NoteDetail.spec.ts b/apps/client/src/widgets/NoteDetail.spec.ts
index bd5d39e996..5dc84ef815 100644
--- a/apps/client/src/widgets/NoteDetail.spec.ts
+++ b/apps/client/src/widgets/NoteDetail.spec.ts
@@ -1,7 +1,8 @@
-import { describe, expect, it, vi } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import NoteContext from "../components/note_context";
import type FNote from "../entities/fnote";
+import froca from "../services/froca";
// NoteDetail pulls in note_tree, which loads jquery.fancytree at import time and needs a `jQuery`
// global the test env doesn't provide. The pure function under test never touches the tree, so stub it.
@@ -9,6 +10,8 @@ vi.mock("./note_tree", () => ({ default: class {} }));
import { getExtendedWidgetType, isContextInActiveTab } from "./NoteDetail";
+const FAKE_NOTE_ID = "blob-stub-note";
+
/**
* Builds a tab as a list of note contexts: the first is the main context (the tab itself),
* the rest are its splits. Every context resolves its main context to the first one — the
@@ -51,8 +54,17 @@ describe("isContextInActiveTab", () => {
});
describe("getExtendedWidgetType blob-stub routing", () => {
+ afterEach(() => {
+ delete froca.notes[FAKE_NOTE_ID];
+ });
+
+ /**
+ * Builds a note and registers it in the froca cache: `getExtendedWidgetType` only fetches a blob
+ * for notes still present there, so an unregistered note would skip the stub check entirely.
+ */
function fakeNote(overrides: Record<string, unknown> = {}): FNote {
- return {
+ const note = {
+ noteId: FAKE_NOTE_ID,
type: "text",
isProtected: false,
isTriliumSqlite: () => false,
@@ -61,6 +73,8 @@ describe("getExtendedWidgetType blob-stub routing", () => {
getBlob: async () => ({ isStubbed: false }),
...overrides
} as unknown as FNote;
+ froca.notes[FAKE_NOTE_ID] = note;
+ return note;
}
function fakeContext(overrides: Record<string, unknown> = {}): NoteContext {