Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

3.9 KB
Raw Blame
import { afterAll, describe, expect, test } from "bun:test";

const originalWindow = Object.getOwnPropertyDescriptor(globalThis, "window");

// Must be installed before importing modules that read `window` at load time.
globalThis.window = {
  location: { hostname: "notes.example.com", origin: "https://notes.example.com" },
  edgeeverDesktop: { isAvailable: true, apiBaseUrl: "https://notes.example.com" },
};

afterAll(() => {
  if (originalWindow) {
    Object.defineProperty(globalThis, "window", originalWindow);
  } else {
    delete globalThis.window;
  }
});

const {
  mapTiptapResourceUrls,
  createStagedResourceListItem,
  stageDesktopResource,
  toApiResourceUrl,
  toDesktopResourceDownloadUrl,
  toDesktopResourceUrl,
} = await import("./desktop-resources.ts");

describe("desktop resource URLs", () => {
  test("maps remote resource URLs to the native cache protocol", () => {
    expect(toDesktopResourceUrl("/api/v1/resources/resource-1/blob")).toBe("edgeever-resource://resource/resource-1");
    expect(toDesktopResourceUrl("https://cdn.example.com/image.png")).toBe("https://cdn.example.com/image.png");
  });

  test("adds the requested filename to native resource downloads", () => {
    expect(toDesktopResourceDownloadUrl(
      "edgeever-resource://resource/resource-1",
      "资料包.zip",
    )).toBe("edgeever-resource://resource/resource-1?download=%E8%B5%84%E6%96%99%E5%8C%85.zip");
    expect(toDesktopResourceDownloadUrl(
      "edgeever-staged://stage-1",
      "offline.zip",
    )).toBe("edgeever-staged://stage-1?download=offline.zip");
    expect(toDesktopResourceDownloadUrl("/api/v1/resources/resource-1/blob", "archive.zip"))
      .toBe("/api/v1/resources/resource-1/blob");
  });

  test("restores portable API URLs before a memo is saved", () => {
    expect(toApiResourceUrl("edgeever-resource://resource/resource-1")).toBe("/api/v1/resources/resource-1/blob");
    const mapped = mapTiptapResourceUrls({
      type: "doc",
      content: [{ type: "image", attrs: { src: "edgeever-resource://resource/resource-1" } }],
    }, toApiResourceUrl);
    expect(mapped.content?.[0]?.attrs?.src).toBe("/api/v1/resources/resource-1/blob");
  });

  test("restores PDF node URLs before a memo is saved", () => {
    const mapped = mapTiptapResourceUrls({
      type: "doc",
      content: [{ type: "pdfAttachment", attrs: { url: "edgeever-resource://resource/resource-pdf" } }],
    }, toApiResourceUrl);

    expect(mapped.content?.[0]?.attrs?.url).toBe("/api/v1/resources/resource-pdf/blob");
  });

  test("exposes staged offline attachments as local resources", () => {
    expect(createStagedResourceListItem({ id: "stage-1", memoId: "memo-1", name: "photo.png", type: "image/png", size: 42 }, "2026-01-01T00:00:00.000Z")).toMatchObject({
      id: "staged_stage-1",
      memoId: "memo-1",
      kind: "image",
      byteSize: 42,
      url: "edgeever-staged://stage-1",
      createdAt: "2026-01-01T00:00:00.000Z",
    });
  });

  test("stages offline attachments in bounded chunks", async () => {
    const parts = [];
    window.edgeeverDesktop.beginStagedResource = async (metadata) => {
      expect(metadata).toMatchObject({ memoId: "memo-1", name: "archive.bin", size: 18 });
      return { id: "stage-streamed", partSize: 8 };
    };
    window.edgeeverDesktop.appendStagedResource = async (_id, bytes) => {
      parts.push(Array.from(new Uint8Array(bytes)));
      return { receivedBytes: parts.reduce((total, part) => total + part.length, 0) };
    };
    window.edgeeverDesktop.completeStagedResource = async (id) => ({ id });
    window.edgeeverDesktop.abortStagedResource = async () => {};

    const staged = await stageDesktopResource(
      "memo-1",
      new File(["abcdefghijklmnopqr"], "archive.bin", { type: "application/octet-stream" }),
    );

    expect(staged).toEqual({ id: "stage-streamed" });
    expect(parts.map((part) => part.length)).toEqual([8, 8, 2]);
  });
});