Spaces:
Running

MiniSearch / server /pageBotChecks.test.ts
system's picture
system HF Staff
Sync from felladrin/MiniSearch@1dcb31a (part 2)
64a58bb verified
Raw History Blame Contribute Delete
1.91 kB
import { describe, expect, it } from "vitest";
import {
hasBotCheckMarkers,
isScripted,
LINK_PREVIEW_USER_AGENTS,
REFUSAL_STATUSES,
} from "./pageBotChecks";
describe("REFUSAL_STATUSES", () => {
it("names the statuses a bot wall answers with", () => {
expect([...REFUSAL_STATUSES].sort((a, b) => a - b)).toEqual([
202, 401, 403, 405, 429,
]);
});
it("leaves out the statuses that are not walls", () => {
expect(REFUSAL_STATUSES.has(404)).toBe(false);
expect(REFUSAL_STATUSES.has(500)).toBe(false);
expect(REFUSAL_STATUSES.has(302)).toBe(false);
});
});
describe("hasBotCheckMarkers", () => {
it("recognizes the Anubis and Akamai challenge pages", () => {
expect(hasBotCheckMarkers('<div id="anubis_version">0</div>')).toBe(true);
expect(hasBotCheckMarkers('<div id="sec-if-cpt-container"></div>')).toBe(
true,
);
});
it("says no to a page that carries neither marker", () => {
expect(hasBotCheckMarkers("<html><body><p>Hello</p></body></html>")).toBe(
false,
);
});
});
describe("isScripted", () => {
it("reads a script tag in any spelling", () => {
expect(isScripted("<script>x()</script>")).toBe(true);
expect(isScripted('<SCRIPT src="/a.js"></SCRIPT>')).toBe(true);
expect(isScripted("<script\ndebugger</script>")).toBe(true);
expect(isScripted("<script/>")).toBe(true);
});
it("says no to prose that only mentions scripts", () => {
expect(isScripted("<p>The script ran.</p>")).toBe(false);
expect(isScripted("<p>javascript: is a language</p>")).toBe(false);
});
});
describe("LINK_PREVIEW_USER_AGENTS", () => {
it("carries the Slack and the Facebook/X preview identities", () => {
expect(LINK_PREVIEW_USER_AGENTS).toHaveLength(2);
expect(LINK_PREVIEW_USER_AGENTS[0]).toContain("Slackbot");
expect(LINK_PREVIEW_USER_AGENTS[1]).toContain("facebookexternalhit");
});
});