import assert from "node:assert/strict";
import { readFile, readdir } from "node:fs/promises";
import test from "node:test";

const uiDirectory = new URL("../src/components/ui/", import.meta.url);
const appStylesheet = new URL("../src/styles/app.css", import.meta.url);

test("UI primitives do not depend on page, Host or domain modules", async () => {
  const files = (await readdir(uiDirectory)).filter(
    (name) => name.endsWith(".ts") || name.endsWith(".tsx"),
  );

  for (const file of files) {
    const source = await readFile(new URL(file, uiDirectory), "utf8");
    const imports = [...source.matchAll(/from\s+["']([^"']+)["']/g)].map((match) => match[1]);

    for (const specifier of imports) {
      assert.doesNotMatch(
        specifier,
        /(?:^|\/)(?:feedback|host|layout|pages|sdk|services)(?:\/|$)|\/lib\/(?:api|model|view-state)$/,
        `${file} must remain business-independent: ${specifier}`,
      );
    }
  }
});

test("global stylesheet does not own component variants", async () => {
  const source = await readFile(appStylesheet, "utf8");

  assert.doesNotMatch(
    source,
    /@layer\s+components/,
    "component classes and variants belong beside their Solid components",
  );
});
