Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

9.6 KB
Raw Blame
import {
  crawlSitemapUrls,
  MAX_URLS_PER_SITEMAP,
} from "@/lib/sitemaps/import-tracked-sitemaps";
import { parseTrackedSitemaps } from "@/lib/sitemaps/site-visit-tracking";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { gzipSync } from "zlib";

vi.mock("server-only", () => ({}));

// Run the sitemap import through the Edge `safeFetch`, so the stubbed global
// `fetch` below handles both the DoH lookups and the sitemap requests.
vi.mock("@/lib/api/safe-fetch-node", async () => {
  const { safeFetch } = await import("@/lib/api/safe-fetch");
  return { safeFetchNode: safeFetch };
});

function toArrayBuffer(buf: Buffer): ArrayBuffer {
  const ab = new ArrayBuffer(buf.length);
  new Uint8Array(ab).set(buf);
  return ab;
}

function makeFetchResponse(
  body: Buffer,
  status = 200,
  headers?: Record<string, string>,
): Response {
  return {
    ok: status >= 200 && status < 300,
    status,
    headers: new Headers(headers),
    arrayBuffer: () => Promise.resolve(toArrayBuffer(body)),
    json: () => Promise.resolve(JSON.parse(body.toString("utf8"))),
  } as unknown as Response;
}

function dohResponseFor(ip: string, type: 1 | 28 = 1): Response {
  return makeFetchResponse(
    Buffer.from(JSON.stringify({ Status: 0, Answer: [{ data: ip, type }] })),
  );
}

const urlsetXml = (urls: string[]) =>
  `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  ${urls.map((u) => `<url><loc>${u}</loc></url>`).join("\n  ")}
</urlset>`;

const sitemapindexXml = (sitemaps: string[]) =>
  `<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  ${sitemaps.map((u) => `<sitemap><loc>${u}</loc></sitemap>`).join("\n  ")}
</sitemapindex>`;

describe("parseTrackedSitemaps", () => {
  it("returns empty array for invalid top-level input", () => {
    expect(parseTrackedSitemaps(null)).toEqual([]);
    expect(parseTrackedSitemaps(undefined)).toEqual([]);
    expect(parseTrackedSitemaps("string")).toEqual([]);
    expect(parseTrackedSitemaps(42)).toEqual([]);
    expect(parseTrackedSitemaps([])).toEqual([]);
    expect(parseTrackedSitemaps({})).toEqual([]);
  });

  it("filters out invalid items (null, primitives, objects without url)", () => {
    expect(
      parseTrackedSitemaps({
        trackedSitemaps: [null, undefined, 42, "string", {}],
      }),
    ).toEqual([]);
  });

  it("filters out items with empty or whitespace-only URL", () => {
    expect(
      parseTrackedSitemaps({
        trackedSitemaps: [{ url: "" }, { url: "   " }],
      }),
    ).toEqual([]);
  });

  it("trims URL whitespace", () => {
    const result = parseTrackedSitemaps({
      trackedSitemaps: [{ url: "  https://example.com/sitemap.xml  " }],
    });
    expect(result).toEqual([{ url: "https://example.com/sitemap.xml" }]);
  });

  it("preserves optional metadata fields when valid", () => {
    const trackedSitemaps = [
      {
        url: "https://example.com/sitemap.xml",
        lastCrawledAt: "2026-03-23T00:00:00.000Z",
        lastUrlCount: 42,
      },
    ];
    expect(parseTrackedSitemaps({ trackedSitemaps })).toEqual(trackedSitemaps);
  });

  it("omits lastCrawledAt when not a string", () => {
    expect(
      parseTrackedSitemaps({
        trackedSitemaps: [
          { url: "https://example.com/sitemap.xml", lastCrawledAt: 12345 },
        ],
      }),
    ).toEqual([{ url: "https://example.com/sitemap.xml" }]);
  });

  it("omits lastUrlCount when not a number", () => {
    expect(
      parseTrackedSitemaps({
        trackedSitemaps: [
          {
            url: "https://example.com/sitemap.xml",
            lastUrlCount: "not-a-number",
          },
        ],
      }),
    ).toEqual([{ url: "https://example.com/sitemap.xml" }]);
  });

  it("returns no sitemaps when the array contains a non-object entry (array-level guard)", () => {
    expect(
      parseTrackedSitemaps({
        trackedSitemaps: [null, { url: "https://a.com/sitemap.xml" }],
      }),
    ).toEqual([]);
  });

  it("skips invalid URL rows when every entry is a plain object", () => {
    expect(
      parseTrackedSitemaps({
        trackedSitemaps: [
          { url: "" },
          { url: "https://a.com/sitemap.xml" },
          { url: "not-a-url" },
          { url: "https://b.com/sitemap.xml", lastUrlCount: 10 },
        ],
      }),
    ).toEqual([
      { url: "https://a.com/sitemap.xml" },
      { url: "https://b.com/sitemap.xml", lastUrlCount: 10 },
    ]);
  });
});

describe("crawlSitemapUrls", () => {
  const mockContentFetch = vi.fn();
  const mockFetch = vi.fn((url: string | URL | Request) => {
    const href = typeof url === "string" ? url : url.toString();
    if (href.includes("dns.google/resolve")) {
      // Return a public IP so safeFetch lets the request through.
      return Promise.resolve(dohResponseFor("93.184.216.34"));
    }
    return mockContentFetch(href);
  });

  beforeEach(() => {
    vi.stubGlobal("fetch", mockFetch);
  });

  afterEach(() => {
    vi.unstubAllGlobals();
    vi.clearAllMocks();
  });

  describe("flat urlset", () => {
    it("returns all page URLs from a simple urlset", async () => {
      const pageUrls = [
        "https://example.com/page-1",
        "https://example.com/page-2",
      ];
      mockContentFetch.mockResolvedValue(
        makeFetchResponse(Buffer.from(urlsetXml(pageUrls))),
      );

      const { urls, hadErrors } = await crawlSitemapUrls(
        "https://example.com/sitemap.xml",
      );

      expect(urls).toEqual(expect.arrayContaining(pageUrls));
      expect(urls).toHaveLength(pageUrls.length);
      expect(hadErrors).toBe(false);
    });

    it("deduplicates URLs across multiple entries", async () => {
      mockContentFetch.mockResolvedValue(
        makeFetchResponse(
          Buffer.from(
            urlsetXml([
              "https://example.com/page-1",
              "https://example.com/page-1",
            ]),
          ),
        ),
      );

      const { urls } = await crawlSitemapUrls(
        "https://example.com/sitemap.xml",
      );

      expect(urls).toHaveLength(1);
    });
  });

  describe("sitemapindex", () => {
    it("does not fetch nested sitemaps from a sitemap index", async () => {
      mockContentFetch.mockResolvedValueOnce(
        makeFetchResponse(
          Buffer.from(
            sitemapindexXml([
              "https://example.com/sitemap-posts.xml",
              "https://example.com/sitemap-pages.xml",
            ]),
          ),
        ),
      );

      const { urls, hadErrors } = await crawlSitemapUrls(
        "https://example.com/sitemap.xml",
      );

      expect(urls).toEqual([]);
      expect(hadErrors).toBe(false);
      expect(mockContentFetch).toHaveBeenCalledTimes(1);
    });

    it("returns no page URLs when the index only references itself", async () => {
      mockContentFetch.mockResolvedValue(
        makeFetchResponse(
          Buffer.from(sitemapindexXml(["https://example.com/sitemap.xml"])),
        ),
      );

      const { urls } = await crawlSitemapUrls(
        "https://example.com/sitemap.xml",
      );

      expect(urls).toEqual([]);
      expect(mockContentFetch).toHaveBeenCalledTimes(1);
    });
  });

  describe("nested xml in urlset", () => {
    it("skips .xml loc entries instead of treating them as pages", async () => {
      mockContentFetch.mockResolvedValue(
        makeFetchResponse(
          Buffer.from(
            urlsetXml([
              "https://example.com/page-1",
              "https://example.com/nested.xml",
            ]),
          ),
        ),
      );

      const { urls } = await crawlSitemapUrls(
        "https://example.com/sitemap.xml",
      );

      expect(urls).toEqual(["https://example.com/page-1"]);
    });
  });

  describe("URL volume limits", () => {
    it(`throws when one sitemap has more than ${MAX_URLS_PER_SITEMAP} unique page URLs`, async () => {
      const many = Array.from({ length: MAX_URLS_PER_SITEMAP + 50 }, (_, i) =>
        i === 0 ? "https://example.com/first" : `https://example.com/p/${i}`,
      );
      mockContentFetch.mockResolvedValue(
        makeFetchResponse(Buffer.from(urlsetXml(many))),
      );

      await expect(
        crawlSitemapUrls("https://example.com/sitemap.xml"),
      ).rejects.toThrow(
        `Sitemap contains too many unique URLs: ${MAX_URLS_PER_SITEMAP + 50} (max ${MAX_URLS_PER_SITEMAP})`,
      );
    });
  });

  describe("gzip support", () => {
    it("decompresses a gzip-compressed sitemap response", async () => {
      const xml = urlsetXml(["https://example.com/page-1"]);
      const compressed = gzipSync(xml);
      mockContentFetch.mockResolvedValue(makeFetchResponse(compressed));

      const { urls } = await crawlSitemapUrls(
        "https://example.com/sitemap.xml.gz",
      );

      expect(urls).toContain("https://example.com/page-1");
    });

    it("handles a plain XML response without attempting decompression", async () => {
      const xml = urlsetXml(["https://example.com/page-1"]);
      mockContentFetch.mockResolvedValue(makeFetchResponse(Buffer.from(xml)));

      const { urls } = await crawlSitemapUrls(
        "https://example.com/sitemap.xml",
      );

      expect(urls).toContain("https://example.com/page-1");
    });
  });

  describe("non-2xx responses", () => {
    it("reports hadErrors instead of parsing the body on a non-2xx response", async () => {
      mockContentFetch.mockResolvedValue(
        makeFetchResponse(
          Buffer.from(urlsetXml(["https://example.com/page-1"])),
          500,
        ),
      );

      const { urls, hadErrors } = await crawlSitemapUrls(
        "https://example.com/sitemap.xml",
      );

      expect(urls).toEqual([]);
      expect(hadErrors).toBe(true);
    });
  });
});