Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

6.4 KB
Raw Blame
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import {
  access,
  mkdir,
  mkdtemp,
  readFile,
  rm,
  writeFile,
} from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import type { Asset } from "@webstudio-is/sdk";
import {
  createLocalAssetDataReader,
  createLocalUpdateAssetContentInput,
  downloadAssetFiles,
  getLocalAssetPath,
  materializeAssetFile,
} from "./asset-files";

const originalCwd = process.cwd();
let tempDir: string;

const asset = {
  id: "asset-1",
  projectId: "project-1",
  name: "image.png",
  type: "image",
  createdAt: "2024-01-01T00:00:00.000Z",
  format: "png",
  size: 6,
  meta: { width: 1, height: 1 },
} satisfies Asset;

beforeEach(async () => {
  tempDir = await mkdtemp(join(tmpdir(), "webstudio-assets-"));
  process.chdir(tempDir);
});

afterEach(async () => {
  process.chdir(originalCwd);
  await rm(tempDir, { recursive: true, force: true });
  vi.restoreAllMocks();
});

test("rejects asset paths outside the asset directory", () => {
  expect(() => getLocalAssetPath("../image.png")).toThrow(
    "Asset path escapes .webstudio/assets"
  );
  expect(() => getLocalAssetPath("folder\\image.png")).toThrow(
    "Asset path escapes .webstudio/assets"
  );
});

test("creates a local asset data reader", async () => {
  await mkdir(".webstudio/assets", { recursive: true });
  await writeFile(".webstudio/assets/image.png", "asset", "utf8");

  const readAssetData = createLocalAssetDataReader(readFile);
  const data = await readAssetData({ name: "image.png" });

  expect(
    Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString()
  ).toBe("asset");
});

test("creates asset content readers from inline content or a file", async () => {
  await writeFile("content.json", '{"source":"file"}', "utf8");
  const fromContent = createLocalUpdateAssetContentInput({
    assetId: "asset",
    expectedName: "content_old.json",
    extension: "json",
    content: '{"source":"inline"}',
    readFile,
  });
  const fromPath = createLocalUpdateAssetContentInput({
    assetId: "asset",
    expectedName: "content_old.json",
    path: "content.json",
    readFile,
  });

  await expect(fromContent.readAssetData()).resolves.toBe(
    '{"source":"inline"}'
  );
  expect(fromContent.extension).toBe("json");
  await expect(fromPath.readAssetData()).resolves.toEqual(
    Buffer.from('{"source":"file"}')
  );
});

test("requires exactly one asset content source", () => {
  const base = {
    assetId: "asset",
    expectedName: "content_old.json",
    readFile,
  };

  expect(() => createLocalUpdateAssetContentInput(base)).toThrow(
    "requires exactly one"
  );
  expect(() =>
    createLocalUpdateAssetContentInput({
      ...base,
      path: "content.json",
      content: "content",
    })
  ).toThrow("requires exactly one");
});

test("materializes asset files from the synced asset cache before fetching", async () => {
  await mkdir(".webstudio/assets", { recursive: true });
  await writeFile(".webstudio/assets/image.png", "synced", "utf8");
  const fetch = vi.fn();
  globalThis.fetch = fetch;

  await materializeAssetFile({
    asset,
    origin: "https://example.com",
    targetAssetsDirectory: "public/assets",
  });

  await expect(readFile("public/assets/image.png", "utf8")).resolves.toBe(
    "synced"
  );
  expect(fetch).not.toHaveBeenCalled();
});

test("downloads asset files when they are missing from the synced asset cache", async () => {
  const fetch = vi.fn(async () => new Response("downloaded"));
  globalThis.fetch = fetch;

  await materializeAssetFile({
    asset,
    origin: "https://example.com",
    targetAssetsDirectory: "public/assets",
  });

  await expect(readFile("public/assets/image.png", "utf8")).resolves.toBe(
    "downloaded"
  );
  expect(fetch).toHaveBeenCalledWith(
    "https://example.com/cgi/image/image.png?format=raw"
  );
});

test("downloads no more than 10 asset files concurrently", async () => {
  let active = 0;
  let peak = 0;
  const releases: Array<() => void> = [];
  const fetch = vi.fn(async () => {
    active += 1;
    peak = Math.max(peak, active);
    await new Promise<void>((resolve) => releases.push(resolve));
    active -= 1;
    return new Response("article");
  });
  globalThis.fetch = fetch;
  const assets = Array.from({ length: 20 }, (_, index) => ({
    ...asset,
    id: `article-${index}`,
    name: `article-${index}.mdx`,
    type: "file" as const,
    format: "mdx",
    meta: {},
  }));

  const downloading = downloadAssetFiles({
    assets,
    origin: "https://example.com",
  });
  await vi.waitFor(() => expect(fetch).toHaveBeenCalledTimes(10));
  expect(peak).toBe(10);
  releases.splice(0).forEach((release) => release());
  await vi.waitFor(() => expect(fetch).toHaveBeenCalledTimes(20));
  expect(peak).toBe(10);
  releases.splice(0).forEach((release) => release());
  await downloading;
});

test("retries asset download once after a server error", async () => {
  const fetch = vi
    .fn()
    .mockResolvedValueOnce(new Response("failed", { status: 500 }))
    .mockResolvedValueOnce(new Response("downloaded"));
  globalThis.fetch = fetch;

  await materializeAssetFile({
    asset,
    origin: "https://example.com",
    targetAssetsDirectory: "public/assets",
  });

  await expect(readFile("public/assets/image.png", "utf8")).resolves.toBe(
    "downloaded"
  );
  expect(fetch).toHaveBeenCalledTimes(2);
});

test("removes temporary files when asset download fails", async () => {
  const fetch = vi.fn(
    async () =>
      new Response(
        new ReadableStream({
          pull(controller) {
            controller.error(new Error("download failed"));
          },
        })
      )
  );
  globalThis.fetch = fetch;

  await expect(
    materializeAssetFile({
      asset,
      origin: "https://example.com",
      targetAssetsDirectory: "public/assets",
    })
  ).rejects.toThrow("download failed");

  await expect(access("public/assets/image.png")).rejects.toThrow();
  await expect(access("public/assets/image.png.tmp")).rejects.toThrow();
});

test("fails clearly when asset download response has no body", async () => {
  const fetch = vi.fn(async () => new Response(null));
  globalThis.fetch = fetch;

  await expect(
    materializeAssetFile({
      asset,
      origin: "https://example.com",
      targetAssetsDirectory: "public/assets",
    })
  ).rejects.toThrow("response body is empty");

  await expect(access("public/assets/image.png")).rejects.toThrow();
  await expect(access("public/assets/image.png.tmp")).rejects.toThrow();
});