import { describe, expect, it } from "vitest";
import { subscriptionIsDue } from "../src/shared/subscriptions";
import { validateDefinitionCollection } from "../src/shared/site-definitions";

const definition = {
  id: "example",
  name: "Example",
  urlPatterns: ["https://example.test/*"],
  requiredSelectors: ["main"],
  post: { rootSelectors: ["article"], textSelectors: [".content"] }
};

describe("definition subscriptions", () => {
  it("validates versioned collections and rejects duplicate IDs", () => {
    const result = validateDefinitionCollection({ schemaVersion: 1, collectionId: "example-source", name: "Example", definitions: [definition, definition] });
    expect(result.valid).toBe(false);
  });

  it("is due when never updated or at least seven days old", () => {
    const now = Date.parse("2026-08-25T00:00:00Z");
    expect(subscriptionIsDue({ id: "x", name: "X", url: "https://example.test/x.json", enabled: true, priority: 0 }, now)).toBe(true);
    expect(subscriptionIsDue({ id: "x", name: "X", url: "https://example.test/x.json", enabled: true, priority: 0, lastSuccessfulUpdate: "2026-08-18T00:00:00Z" }, now)).toBe(true);
    expect(subscriptionIsDue({ id: "x", name: "X", url: "https://example.test/x.json", enabled: true, priority: 0, lastSuccessfulUpdate: "2026-08-24T00:00:00Z" }, now)).toBe(false);
  });
});
