import "@testing-library/jest-dom/vitest";
import { afterEach } from "vitest";
import { cleanup } from "@testing-library/react";

// crypto.randomUUID is used by the diagram store; jsdom may not provide it.
const existingCrypto = (globalThis as { crypto?: Partial<Crypto> }).crypto;
if (typeof existingCrypto?.randomUUID !== "function") {
  let counter = 0;
  const randomUUID = (): `${string}-${string}-${string}-${string}-${string}` =>
    `00000000-0000-4000-8000-${String(counter++).padStart(12, "0")}` as const;
  const cryptoStub = { ...(existingCrypto ?? {}), randomUUID } as Crypto;
  Object.defineProperty(globalThis, "crypto", { value: cryptoStub, configurable: true });
}

afterEach(() => {
  cleanup();
});
