import { describe, expect, it } from "vitest";
import { createMemoryScanSource } from "./memory-source.js";
import { extractSfcUcfsFromSourceWithDiagnostics } from "./sfc-ucf-source.js";

describe("extractSfcUcfsFromSourceWithDiagnostics", () => {
  it("extracts Vue and Svelte UCFs from a ScanSource", async () => {
    const result = await extractSfcUcfsFromSourceWithDiagnostics({
      source: createMemoryScanSource({
        "src/BaseButton.vue": [
          "<template><button class=\"btn\"><slot /></button></template>",
          "<script setup lang=\"ts\">",
          "defineProps<{ variant?: 'primary' | 'secondary' }>();",
          "</script>",
        ].join("\n"),
        "src/Card.svelte": [
          "<script lang=\"ts\">export let tone: 'info' | 'danger' = 'info';</script>",
          "<article><slot name=\"body\" /></article>",
        ].join("\n"),
      }),
      files: [{ relPath: "src/BaseButton.vue" }, { relPath: "src/Card.svelte" }],
      sourceCommit: "sha",
    });

    expect(result.diagnostics).toEqual([]);
    expect(result.ucfs.map((ucf) => [ucf.framework, ucf.componentName])).toEqual([
      ["vue", "BaseButton"],
      ["svelte", "Card"],
    ]);
  });

  it("reports invalid, unsupported, and missing source files", async () => {
    const result = await extractSfcUcfsFromSourceWithDiagnostics({
      source: createMemoryScanSource({}),
      files: [
        { relPath: "../Bad.vue" },
        { relPath: "src/Button.tsx" },
        { relPath: "src/Missing.vue" },
      ],
      sourceCommit: "sha",
    });

    expect(result.ucfs).toEqual([]);
    expect(result.diagnostics.map((diagnostic) => diagnostic.code)).toEqual([
      "invalid_path",
      "unsupported_file",
      "missing_file",
    ]);
  });
});
