import { describe, expect, it } from "vitest";
import { BUILTIN_DEFINITIONS, effectiveDefinitions } from "../src/content/definitions";
import { validateDefinitionConfiguration } from "../src/shared/site-definitions";

const customDefinition = {
  id: "example-social",
  name: "Example Social",
  urlPatterns: ["https://*.example.test/*"],
  requiredSelectors: [".feed"],
  post: { rootSelectors: ["article"], textSelectors: [".content"], idAttributes: ["data-id"] }
};

describe("site definitions", () => {
  it("accepts declarative custom definitions and deduplicates disabled IDs", () => {
    const result = validateDefinitionConfiguration({ customDefinitions: [customDefinition], disabledDefinitionIds: ["mastodon", "mastodon"] });
    expect(result.valid).toBe(true);
    if (result.valid) expect(result.value.disabledDefinitionIds).toEqual(["mastodon"]);
  });

  it("rejects executable or malformed definition fields", () => {
    const result = validateDefinitionConfiguration({ customDefinitions: [{ ...customDefinition, id: "Bad ID", post: { ...customDefinition.post, rootSelectors: ["["] } }], disabledDefinitionIds: [] });
    expect(result.valid).toBe(false);
  });

  it("lets a custom definition replace a bundled definition by ID", () => {
    const override = { ...BUILTIN_DEFINITIONS[0]!, name: "Custom Mastodon" };
    const definitions = effectiveDefinitions([override], []);
    expect(definitions.filter((definition) => definition.id === "mastodon")).toEqual([override]);
  });
});
