import { describe, it, expect } from "vitest";
import path from "node:path";
import os from "node:os";
import fs from "node:fs";
import { findAllManifests, buildEventGraph, findOrphanConsumers } from "./conformance.mjs";

function makeRepo(features) {
  const root = fs.mkdtempSync(path.join(os.tmpdir(), "conformance-"));
  for (const [name, useCases] of Object.entries(features)) {
    const dir = path.join(root, "packages", name, "src");
    fs.mkdirSync(dir, { recursive: true });
    const useCasesStr = Object.entries(useCases)
      .map(([ucName, uc]) =>
        `    ${ucName}: { mutates: ${uc.mutates ?? false}, audits: [], publishes: [${(uc.publishes ?? []).map((p) => `"${p}"`).join(", ")}], consumes: [${(uc.consumes ?? []).map((c) => `"${c}"`).join(", ")}] },`,
      )
      .join("\n");
    fs.writeFileSync(
      path.join(dir, "feature.manifest.ts"),
      `export const ${name}Manifest = defineFeature({
  name: "${name}",
  requiredCores: [],
  useCases: {
${useCasesStr}
  },
  realtimeChannels: [],
  jobs: [],
} as const);`,
    );
  }
  return root;
}

describe("conformance script", () => {
  describe("findAllManifests", () => {
    it("returns one entry per feature with a manifest", () => {
      const root = makeRepo({
        auth: { signIn: {} },
        blog: { getArticles: {} },
      });
      const ms = findAllManifests(root);
      expect(ms.map((m) => m.feature).sort()).toEqual(["auth", "blog"]);
    });

    it("skips packages without a manifest", () => {
      const root = fs.mkdtempSync(path.join(os.tmpdir(), "conformance-empty-"));
      fs.mkdirSync(path.join(root, "packages", "no-manifest", "src"), { recursive: true });
      expect(findAllManifests(root)).toEqual([]);
    });
  });

  describe("buildEventGraph + findOrphanConsumers", () => {
    it("finds zero orphans when consumers and publishers line up", () => {
      const root = makeRepo({
        auth: { signUp: { mutates: true, publishes: ["auth.signed-up"] } },
        marketing: { onAuthSignedUp: { consumes: ["auth.signed-up"] } },
      });
      const manifests = findAllManifests(root);
      const graph = buildEventGraph(manifests);
      expect(findOrphanConsumers(graph)).toEqual([]);
    });

    it("flags orphan consumers", () => {
      const root = makeRepo({
        marketing: { onAuthSignedUp: { consumes: ["auth.signed-up"] } },
      });
      const manifests = findAllManifests(root);
      const graph = buildEventGraph(manifests);
      const orphans = findOrphanConsumers(graph);
      expect(orphans).toHaveLength(1);
      expect(orphans[0].event).toBe("auth.signed-up");
      expect(orphans[0].consumers).toEqual([{ feature: "marketing", useCase: "onAuthSignedUp" }]);
    });

    it("treats publish-only events as fine (no consumers is not an orphan)", () => {
      const root = makeRepo({
        auth: { signUp: { mutates: true, publishes: ["auth.signed-up"] } },
      });
      const manifests = findAllManifests(root);
      const graph = buildEventGraph(manifests);
      expect(findOrphanConsumers(graph)).toEqual([]);
    });
  });
});
