import { expect, test } from "bun:test";
import { HMR_CLIENT_JS, collectScripts } from "../src/runtime.ts";

test("accent controls load the shared theme runtime", () => {
  expect(collectScripts('<button data-wire-accent-set="rose">Rose</button>')).toContain(
    "/__wrnexus/theme.js",
  );
});

test("accent clear controls load the shared theme runtime", () => {
  expect(collectScripts("<button data-wire-accent-clear>Reset</button>")).toContain(
    "/__wrnexus/theme.js",
  );
});

test("HMR preserves the active browser theme and accent", () => {
  expect(HMR_CLIENT_JS).not.toContain('["data-theme", "data-accent", "lang"]');

  expect(HMR_CLIENT_JS).not.toContain("document.documentElement.setAttribute(name, value)");

  expect(HMR_CLIENT_JS).toMatch(/var nextLang\s*=\s*doc\.documentElement\.getAttribute\("lang"\)/);

  expect(HMR_CLIENT_JS).toMatch(/typeof\s+window\.wireTheme\.bind\s*===\s*"function"/);

  expect(HMR_CLIENT_JS).toMatch(/window\.wireTheme\.bind\(document\)/);

  expect(HMR_CLIENT_JS).toContain('new CustomEvent("wrnexus:hmr-updated"');
});

test("HMR synchronizes local WRN styles with the current document nonce", () => {
  expect(HMR_CLIENT_JS).toContain("style[data-wrnexus-style-id]");
  expect(HMR_CLIENT_JS).toContain("function currentDocumentNonce()");
  expect(HMR_CLIENT_JS).toContain('currentStyle.setAttribute("nonce", nonce)');
  expect(HMR_CLIENT_JS).toContain("document.head.appendChild(currentStyle)");
  expect(HMR_CLIENT_JS).toContain("syncWrnStyles(doc)");
});
