import { describe, expect, it } from "vitest";
import {
  RESOURCE_POSTS,
  RESOURCE_TABS,
  getPost,
  postsByTab,
} from "./resources-posts";

describe("RESOURCE_POSTS integrity", () => {
  it("has unique slugs", () => {
    const slugs = RESOURCE_POSTS.map((p) => p.slug);
    expect(new Set(slugs).size).toBe(slugs.length);
  });

  it("every post belongs to a declared tab", () => {
    const ids = new Set(RESOURCE_TABS.map((t) => t.id));
    for (const p of RESOURCE_POSTS) {
      expect(ids.has(p.tab)).toBe(true);
    }
  });

  it("every post has title, excerpt, date and read time", () => {
    for (const p of RESOURCE_POSTS) {
      expect(p.title.length).toBeGreaterThan(0);
      expect(p.excerpt.length).toBeGreaterThan(0);
      expect(Number.isNaN(Date.parse(p.date))).toBe(false);
      expect(p.readMins).toBeGreaterThan(0);
    }
  });

  it("has at most one featured post", () => {
    expect(RESOURCE_POSTS.filter((p) => p.featured).length).toBeLessThanOrEqual(1);
  });
});

describe("getPost / postsByTab", () => {
  it("finds posts by slug and misses unknown ones", () => {
    const first = RESOURCE_POSTS[0];
    expect(getPost(first.slug)?.title).toBe(first.title);
    expect(getPost("no-such-post")).toBeUndefined();
  });

  it("filters by tab without leakage", () => {
    for (const t of RESOURCE_TABS) {
      const list = postsByTab(t.id);
      expect(list.length).toBeGreaterThan(0);
      for (const p of list) expect(p.tab).toBe(t.id);
    }
  });

  it("covers every post exactly once across tabs", () => {
    const total = RESOURCE_TABS.reduce((n, t) => n + postsByTab(t.id).length, 0);
    expect(total).toBe(RESOURCE_POSTS.length);
  });
});
