import { describe, expect, it } from "vitest";
import { getRuntimeCanonicalUrl, rebaseSameSiteSchemaUrls } from "../client/src/lib/seoUrls";
import { buildDefaultSchema } from "../client/src/components/SEOHead";

describe("runtime SEO URL helpers", () => {
  const deploymentOrigin = "https://homebuyernat-utzne5tk.manus.space";

  it("uses the current deployment origin while removing query and hash fragments from canonicals", () => {
    expect(
      getRuntimeCanonicalUrl(
        "https://homebuyers-nationwide.com/sell-as-is.html?utm_source=campaign#faq",
        deploymentOrigin
      )
    ).toBe(`${deploymentOrigin}/sell-as-is.html`);
  });

  it("keeps relative canonical paths on the active deployment origin", () => {
    expect(getRuntimeCanonicalUrl("/blog.html", deploymentOrigin)).toBe(`${deploymentOrigin}/blog.html`);
  });

  it("rebases same-site structured-data URLs without touching external resources", () => {
    const schema = {
      "@id": "https://homebuyers-nationwide.com/#organization",
      url: "https://homebuyers-nationwide.com/",
      publisher: { url: "https://homebuyers-nationwide.com/blog.html" },
      sameAs: ["https://www.facebook.com/originalhomebuyers"],
    };

    expect(rebaseSameSiteSchemaUrls(schema, deploymentOrigin)).toEqual({
      "@id": `${deploymentOrigin}/#organization`,
      url: `${deploymentOrigin}/`,
      publisher: { url: `${deploymentOrigin}/blog.html` },
      sameAs: ["https://www.facebook.com/originalhomebuyers"],
    });
  });

  it("creates portable organization markup without licensed-agent-only fields", () => {
    expect(buildDefaultSchema("Organization", deploymentOrigin)).toEqual({
      "@context": "https://schema.org",
      "@type": "Organization",
      name: "Original Home Buyers",
      "@id": `${deploymentOrigin}/#organization`,
      url: `${deploymentOrigin}/`,
      telephone: "+1-240-410-5636",
      email: "info@originalhomebuyers.com",
    });
  });

  it("does not produce unsupported default markup for unimplemented schema types", () => {
    expect(buildDefaultSchema("RealEstateAgent", deploymentOrigin)).toEqual({});
  });
});
