import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { describe, expect, it } from "vitest";

const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const packageJsonPath = path.join(rootDir, "package.json");
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));

describe("package metadata", () => {
  it("ships first-party type declarations for the public exports", () => {
    expect(packageJson.types).toBe("./lib/index.d.ts");
    expect(packageJson.exports["."]).toEqual({
      types: "./lib/index.d.ts",
      default: "./lib/index.mjs",
    });
    expect(packageJson.exports["./setup"]).toEqual({
      types: "./lib/setup/index.d.ts",
      default: "./lib/setup/index.mjs",
    });
    expect(packageJson.exports["./runtime"]).toEqual({
      types: "./lib/runtime/index.d.ts",
      default: "./lib/runtime/index.mjs",
    });
    expect(packageJson.exports["./discovery"]).toEqual({
      types: "./lib/discovery/index.d.ts",
      default: "./lib/discovery/index.mjs",
    });
    expect(packageJson.exports["./known-failures"]).toEqual({
      types: "./lib/known-failures/index.d.ts",
      default: "./lib/known-failures/index.mjs",
    });
    expect(fs.existsSync(path.join(rootDir, "lib", "index.d.ts"))).toBe(true);
    expect(fs.existsSync(path.join(rootDir, "lib", "setup", "index.d.ts"))).toBe(true);
    expect(fs.existsSync(path.join(rootDir, "lib", "runtime", "index.d.ts"))).toBe(true);
    expect(fs.existsSync(path.join(rootDir, "lib", "discovery", "index.d.ts"))).toBe(true);
    expect(fs.existsSync(path.join(rootDir, "lib", "known-failures", "index.d.ts"))).toBe(true);
  });
});
