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
test(standalone): cover localFetch and the structured-clone bridge paths
Elian Doran0e33189f73f5
diff --git a/apps/standalone/src/lightweight/bridged_request_provider.spec.ts b/apps/standalone/src/lightweight/bridged_request_provider.spec.tsindex f9d1716cf1..dc3d35d7e2 100644--- a/apps/standalone/src/lightweight/bridged_request_provider.spec.ts+++ b/apps/standalone/src/lightweight/bridged_request_provider.spec.ts@@ -165,6 +165,22 @@ describe("BridgedRequestProvider.exec", () => {respond({ status: 200, body: JSON.stringify({ ok: true }) });await expect(promise).resolves.toEqual({ ok: true });});++ it("resolves via the structured-clone data field without re-parsing a body", async () => {+ const provider = new BridgedRequestProvider();+ const promise = provider.exec<{ ok: boolean }>({ method: "GET", url: "http://x" } as ExecOpts);+ // Main thread delivers the already-parsed object as `data` (no `body`) — the worker+ // must use it directly rather than JSON.parse-ing a string.+ respond({ status: 201, data: { ok: true } });+ await expect(promise).resolves.toEqual({ ok: true });+ });++ it("reads the error message from a structured-clone data object on a non-2xx response", async () => {+ const provider = new BridgedRequestProvider();+ const promise = provider.exec({ method: "GET", url: "http://x" } as ExecOpts);+ respond({ status: 500, data: { message: "boom via data" } });+ await expect(promise).rejects.toThrow("500 GET http://x: boom via data");+ });});describe("BridgedRequestProvider.getImage", () => {diff --git a/apps/standalone/src/local-bridge.spec.ts b/apps/standalone/src/local-bridge.spec.tsindex 7f57a24f23..cc07834622 100644--- a/apps/standalone/src/local-bridge.spec.ts+++ b/apps/standalone/src/local-bridge.spec.ts@@ -281,3 +281,53 @@ describe("pending request rejection", () => {expect(() => worker.onmessage?.({ data: { type: "HTTP_REQUEST", id: "1", request: {} } })).not.toThrow();});});++describe("localFetch", () => {+ it("posts a GET LOCAL_REQUEST with no body transfer and builds a Response from the reply", async () => {+ const bridge = await freshBridge();+ const promise = bridge.localFetch(new Request("http://x/api/notes"));++ const worker = lastWorker();+ await vi.waitFor(() => expect(worker.postMessage).toHaveBeenCalledWith(+ expect.objectContaining({ type: "LOCAL_REQUEST", request: expect.objectContaining({ method: "GET", body: null }) }),+ []+ ));++ const posted = worker.postMessage.mock.calls.at(-1)?.[0] as { id: string };+ const body = new TextEncoder().encode("hello").buffer;+ worker.onmessage?.({ data: { type: "LOCAL_RESPONSE", id: posted.id, response: { status: 201, headers: { "content-type": "text/plain" }, body } } });++ const res = await promise;+ expect(res.status).toBe(201);+ expect(res.headers.get("content-type")).toBe("text/plain");+ expect(await res.text()).toBe("hello");+ });++ it("transfers the body for non-GET requests and defaults a falsy status to 200", async () => {+ const bridge = await freshBridge();+ const promise = bridge.localFetch(new Request("http://x/api/notes", { method: "POST", body: "payload" }));++ const worker = lastWorker();+ await vi.waitFor(() => expect(worker.postMessage).toHaveBeenCalledWith(+ expect.objectContaining({ type: "LOCAL_REQUEST", request: expect.objectContaining({ method: "POST" }) }),+ [expect.any(ArrayBuffer)]+ ));++ const posted = worker.postMessage.mock.calls.at(-1)?.[0] as { id: string };+ worker.onmessage?.({ data: { type: "LOCAL_RESPONSE", id: posted.id, response: { status: 0, headers: {} } } });++ const res = await promise;+ expect(res.status).toBe(200);+ });+});++describe("isLocalApiRequest", () => {+ it("matches only the local API prefixes", async () => {+ const bridge = await freshBridge();+ for (const path of ["/bootstrap", "/api/notes", "/sync/changed", "/search/q"]) {+ expect(bridge.isLocalApiRequest(new URL(`http://x${path}`))).toBe(true);+ }+ expect(bridge.isLocalApiRequest(new URL("http://x/app.js"))).toBe(false);+ expect(bridge.isLocalApiRequest(new URL("http://x/"))).toBe(false);+ });+});