Spaces:
Running

File size: 1,909 Bytes
64a58bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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");
  });
});