// @vitest-environment jsdom import React from "react"; import { act } from "react"; import { createRoot } from "react-dom/client"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; // ── Mocks ───────────────────────────────────────────────────────────────────── vi.mock("next-intl", () => ({ useTranslations: () => (key: string) => key, })); // ── Import component at module level (after mocks) ──────────────────────────── const { default: MarkdownMessage } = await import( "../../../src/app/(dashboard)/dashboard/playground/components/MarkdownMessage" ); // ── Helpers ─────────────────────────────────────────────────────────────────── const containers: Array<{ root: ReturnType; el: HTMLDivElement }> = []; function renderMarkdown(content: string, className?: string): HTMLDivElement { const el = document.createElement("div"); document.body.appendChild(el); const root = createRoot(el); act(() => { root.render(); }); containers.push({ root, el }); return el; } async function waitForCondition(fn: () => boolean, timeoutMs = 3000): Promise { const start = Date.now(); while (!fn()) { if (Date.now() - start > timeoutMs) throw new Error("waitForCondition timed out"); await new Promise((r) => setTimeout(r, 20)); } } // ── Tests ───────────────────────────────────────────────────────────────────── describe("MarkdownMessage", () => { beforeEach(() => { (globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }) .IS_REACT_ACT_ENVIRONMENT = true; }); afterEach(() => { for (const { root, el } of containers.splice(0)) { act(() => root.unmount()); el.remove(); } document.body.innerHTML = ""; vi.unstubAllGlobals(); }); it("renders a code block as
", async () => {
    const el = renderMarkdown("```js\nconsole.log(1)\n```");
    // Wait for the component to render
    await waitForCondition(() => el.innerHTML.length > 0);

    const pre = el.querySelector("pre");
    const code = el.querySelector("code");
    expect(pre || code).toBeTruthy();
    expect(el.innerHTML).toContain("console.log");
  });

  it("renders a markdown table as ", async () => {
    const tableMarkdown = `
| Name | Age |
|------|-----|
| Alice | 30 |
| Bob | 25 |
`;
    const el = renderMarkdown(tableMarkdown);
    await waitForCondition(() => el.innerHTML.length > 0);

    const table = el.querySelector("table");
    expect(table).toBeTruthy();

    const cells = el.querySelectorAll("td");
    expect(cells.length).toBeGreaterThan(0);
  });

  it("renders a markdown list as