import { readFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import {
  parseLivePluginContentV1,
  validatePluginManifestV2,
} from "@thortiq/plugin-api";
import { assertSelfContainedSource } from "../scripts/validate.mjs";

const dir = dirname(fileURLToPath(new URL("../manifest.json", import.meta.url)));

async function content() {
  const manifest = JSON.parse(await readFile(join(dir, "manifest.json"), "utf8"));
  const javascript = await readFile(join(dir, "plugin.js"), "utf8");
  const css = await readFile(join(dir, "styles.css"), "utf8");
  return { manifest, javascript, css };
}

describe("Thortiq Example Live Customization live customization", () => {
  it("is a valid live-sandboxed manifest with self-contained source", async () => {
    const { manifest, javascript, css } = await content();
    expect(validatePluginManifestV2(manifest)).toMatchObject({ ok: true });
    expect(manifest.runtime).toBe("live-sandboxed");
    assertSelfContainedSource(javascript);
    expect(() => parseLivePluginContentV1(manifest.id, { manifest, javascript, css })).not.toThrow();
  });
});
