// author: simon-pierre boucher import { describe, expect, it } from "vitest"; import { crawlDelay, isAllowed, parseRobots } from "./robots.js"; describe("parseRobots / isAllowed", () => { it("allows everything when robots is empty", () => { const r = parseRobots(""); expect(isAllowed(r, "/anything")).toBe(true); }); it("honors a wildcard Disallow", () => { const r = parseRobots("User-agent: *\nDisallow: /private/"); expect(isAllowed(r, "/private/x")).toBe(false); expect(isAllowed(r, "/public/x")).toBe(true); }); it("applies longest-match-wins between Allow and Disallow", () => { const r = parseRobots("User-agent: *\nDisallow: /a/\nAllow: /a/b"); expect(isAllowed(r, "/a/b/c")).toBe(true); expect(isAllowed(r, "/a/x")).toBe(false); }); it("supports * and $ wildcards", () => { const r = parseRobots("User-agent: *\nDisallow: /*.pdf$"); expect(isAllowed(r, "/docs/file.pdf")).toBe(false); expect(isAllowed(r, "/docs/file.pdf?x=1")).toBe(true); expect(isAllowed(r, "/docs/file.html")).toBe(true); }); it("selects the most specific matching user-agent group", () => { const r = parseRobots( "User-agent: *\nDisallow: /\n\nUser-agent: Tendril\nAllow: /\nDisallow: /secret", ); expect(isAllowed(r, "/page", "Tendril/1.0")).toBe(true); expect(isAllowed(r, "/secret", "Tendril/1.0")).toBe(false); expect(isAllowed(r, "/page", "SomeOtherBot")).toBe(false); }); it("treats an empty Disallow as allow-all", () => { const r = parseRobots("User-agent: *\nDisallow:"); expect(isAllowed(r, "/anything")).toBe(true); }); it("groups consecutive user-agent lines together", () => { const r = parseRobots("User-agent: A\nUser-agent: Tendril\nDisallow: /x"); expect(isAllowed(r, "/x", "Tendril")).toBe(false); }); it("collects Sitemap directives and Crawl-delay", () => { const r = parseRobots("Sitemap: https://e.com/sitemap.xml\nUser-agent: *\nCrawl-delay: 5\nDisallow: /y"); expect(r.sitemaps).toEqual(["https://e.com/sitemap.xml"]); expect(crawlDelay(r)).toBe(5); }); });