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.3 KB
Raw Blame
import { parseMdxDocument } from "@webstudio-is/content-engine/mdx";
import { describe, expect, test } from "vitest";
import { planMdxTemplateMigration } from "./mdx-template-migration";

const files = [
  {
    assetId: "article",
    revision: "revision-1",
    contentRef: "article.mdx-revision-1",
    source: `---\ntitle: Article\n---\n\n{/* keep */}\n\n<ws.element ws:name="Card">\n  <ws.element ws:name="Card" />\n</ws.element>\n\n<ws.element ws:name="Other" />`,
  },
];

describe("MDX template migration", () => {
  test("rejects duplicate, excessive, and oversized file selections", async () => {
    const input = {
      projectId: "project",
      migration: { type: "remove", name: "Card" } as const,
    };
    await expect(
      planMdxTemplateMigration({ ...input, files: [files[0], files[0]] })
    ).rejects.toThrow('Duplicate MDX Asset "article"');
    await expect(
      planMdxTemplateMigration({
        ...input,
        files: Array.from({ length: 101 }, (_, index) => ({
          ...files[0],
          assetId: `article-${index}`,
        })),
      })
    ).rejects.toThrow("limited to 100 files");
    await expect(
      planMdxTemplateMigration({
        ...input,
        files: [
          {
            ...files[0],
            source: "x".repeat(10 * 1024 * 1024 + 1),
          },
        ],
      })
    ).rejects.toThrow("exceeds the 10 MiB limit");
  });

  test("previews nested AST renames while preserving frontmatter and comments", async () => {
    const plan = await planMdxTemplateMigration({
      projectId: "project",
      migration: { type: "rename", from: "Card", to: "FeatureCard" },
      files,
    });

    expect(plan).toMatchObject({
      status: "confirmation-required",
      updateCount: 2,
      omissionCount: 0,
      changedFileCount: 1,
      files: [{ assetId: "article", changed: true, updateCount: 2 }],
    });
    const document = await parseMdxDocument({ source: plan.files[0].source });
    expect(document.frontmatter.properties).toEqual({ title: "Article" });
    expect(document.children[0]).toMatchObject({
      type: "comment",
      value: "/* keep */",
    });
    expect(document.children[1]).toMatchObject({
      type: "template",
      name: "FeatureCard",
      children: [{ type: "template", name: "FeatureCard" }],
    });
    expect(document.children[2]).toMatchObject({
      type: "template",
      name: "Other",
    });
  });

  test("renames JSX references and rejects incompatible names", async () => {
    const file = {
      ...files[0],
      source: "<Card><Card /></Card>",
    };
    const jsxPlan = await planMdxTemplateMigration({
      projectId: "project",
      migration: { type: "rename", from: "Card", to: "FeatureCard" },
      files: [file],
    });
    expect(jsxPlan.files[0]?.source).toBe(
      "<FeatureCard>\n  <FeatureCard />\n</FeatureCard>\n"
    );

    await expect(
      planMdxTemplateMigration({
        projectId: "project",
        migration: { type: "rename", from: "Card", to: "Feature Card" },
        files: [file],
      })
    ).rejects.toThrow("valid PascalCase MDX name");
  });

  test("migrates every reference because template names take precedence", async () => {
    const file = {
      ...files[0],
      source: `<Image src="hero.png" alt="Hero" />

<ws.element ws:name="Image" />`,
    };
    const renamePlan = await planMdxTemplateMigration({
      projectId: "project",
      migration: { type: "rename", from: "Image", to: "Photo" },
      files: [file],
    });
    const renamed = await parseMdxDocument({
      source: renamePlan.files[0]?.source ?? "",
    });

    expect(renamePlan).toMatchObject({ updateCount: 2, changedFileCount: 1 });
    expect(renamed.children).toMatchObject([
      { type: "template", name: "Photo", syntax: "jsx" },
      { type: "template", name: "Photo", syntax: "jsx" },
    ]);

    const removePlan = await planMdxTemplateMigration({
      projectId: "project",
      migration: { type: "remove", name: "Image" },
      files: [file],
    });
    const removed = await parseMdxDocument({
      source: removePlan.files[0]?.source ?? "",
    });

    expect(removePlan).toMatchObject({ omissionCount: 2, changedFileCount: 1 });
    expect(removed.children).toEqual([]);
  });

  test.each(["Image", "CodeText"])(
    "uses canonical JSX when renaming a template to %s",
    async (reservedName) => {
      const plan = await planMdxTemplateMigration({
        projectId: "project",
        migration: { type: "rename", from: "Card", to: reservedName },
        files: [{ ...files[0], source: "<Card />" }],
      });

      expect(plan.files[0]?.source).toBe(`<${reservedName} />\n`);
    }
  );

  test("removes nested matching references and reports invalid files", async () => {
    const plan = await planMdxTemplateMigration({
      projectId: "project",
      migration: { type: "remove", name: "Card" },
      files: [
        ...files,
        { ...files[0], assetId: "invalid", source: "<ws.element>" },
      ],
    });

    expect(plan).toMatchObject({
      updateCount: 0,
      omissionCount: 2,
      changedFileCount: 1,
      files: [
        { assetId: "article", omissionCount: 2, diagnostics: [] },
        {
          assetId: "invalid",
          changed: false,
          diagnostics: [{ code: "invalid-mdx" }],
        },
      ],
    });
    const document = await parseMdxDocument({ source: plan.files[0].source });
    expect(document.children).toEqual([
      expect.objectContaining({ type: "comment" }),
      expect.objectContaining({ type: "template", name: "Other" }),
    ]);
  });

  test("removes template wrappers without discarding authored children", async () => {
    const plan = await planMdxTemplateMigration({
      projectId: "project",
      migration: { type: "remove", name: "Card" },
      files: [
        {
          ...files[0],
          source: "<Card>Before <Other /> after</Card>",
        },
      ],
    });

    expect(plan).toMatchObject({ omissionCount: 1, changedFileCount: 1 });
    await expect(
      parseMdxDocument({ source: plan.files[0]?.source ?? "" })
    ).resolves.toMatchObject({
      children: [
        {
          type: "element",
          tag: "p",
          children: [{ type: "text", value: "Before" }],
        },
        { type: "template", name: "Other" },
        {
          type: "element",
          tag: "p",
          children: [{ type: "text", value: "after" }],
        },
      ],
    });
  });
});