import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { describe, expect, it } from "vitest";
import { buildSitemap } from "./sitemap";

const projectFile = (...segments: string[]) =>
  readFileSync(resolve(process.cwd(), ...segments), "utf8");

const sellerResourceLinks = [
  "/blog.html",
  "/cash-sale-benefits.html",
  "/blog.html#selling-without-agent",
  "/seller-insights.html",
  "/sell-as-is.html",
  "/foreclosure-help.html",
  "/sell-inherited-property.html",
  "/sell-during-divorce.html",
];

describe("public seller-content discovery", () => {
  it("keeps every public seller-education route available in the sitemap", () => {
    const sitemap = buildSitemap("https://example.com");

    for (const href of sellerResourceLinks) {
      expect(sitemap).toContain(`https://example.com${href.split("#")[0]}`);
    }
  });

  it("exposes the full seller-resource set on the homepage", () => {
    const home = projectFile("client", "src", "pages", "Home.tsx");

    expect(home).toContain('id="seller-resources-title"');
    for (const href of sellerResourceLinks) {
      expect(home).toContain(`href="${href}"`);
    }
  });

  it("keeps seller education visible from shared content and location templates", () => {
    const contentPages = projectFile("client", "src", "pages", "ContentPages.tsx");
    const countyPages = projectFile("client", "src", "pages", "CountyAreaPage.tsx");

    for (const page of [contentPages, countyPages]) {
      expect(page).toContain('["Seller Education", "/blog.html"]');
      for (const href of sellerResourceLinks) {
        expect(page).toContain(`href="${href}"`);
      }
    }
  });

  it("keeps the standalone cash-sale guide linked from the existing blog article", () => {
    const contentPages = projectFile("client", "src", "pages", "ContentPages.tsx");

    expect(contentPages).toContain('href="/cash-sale-benefits.html"');
    expect(contentPages).toContain("Read the full Cash Sale Benefits Guide");
    expect(contentPages).toContain('id="cash-sale-benefits"');
    expect(contentPages).toContain('id="selling-without-agent"');
  });
});
