|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { resolveClass } from "./classify"; |
| 3 | + |
| 4 | +const CHROME_UA = |
| 5 | + "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Mobile Safari/537.36"; |
| 6 | +const HUMAN_CTX = { hasFingerprint: true, ephemeral: false }; |
| 7 | + |
| 8 | +describe("resolveClass", () => { |
| 9 | + it("classifies clean Chrome mobile as human", () => { |
| 10 | + const r = resolveClass({ ua: CHROME_UA }, HUMAN_CTX); |
| 11 | + expect(r.class).toBe("human"); |
| 12 | + expect(r.baseRisk).toBe(0); |
| 13 | + expect(r.verdict).toBe("human"); |
| 14 | + }); |
| 15 | + |
| 16 | + it("hard-bots missing UA", () => { |
| 17 | + const r = resolveClass({ ua: null }, HUMAN_CTX); |
| 18 | + expect(r.class).toBe("bot"); |
| 19 | + expect(r.verdict).toBe("no_ua"); |
| 20 | + }); |
| 21 | + |
| 22 | + it("hard-bots known crawler UA", () => { |
| 23 | + const r = resolveClass( |
| 24 | + { ua: "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" }, |
| 25 | + HUMAN_CTX, |
| 26 | + ); |
| 27 | + expect(r.class).toBe("bot"); |
| 28 | + expect(r.verdict).toBe("isbot_ua"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("hard-bots when BotID and BotD both fire", () => { |
| 32 | + const r = resolveClass({ ua: CHROME_UA, botIdIsBot: true, botdDetected: true }, HUMAN_CTX); |
| 33 | + expect(r.class).toBe("bot"); |
| 34 | + expect(r.verdict).toBe("botid+botd"); |
| 35 | + }); |
| 36 | + |
| 37 | + it("hard-bots lone detector without fingerprint", () => { |
| 38 | + const r = resolveClass( |
| 39 | + { ua: CHROME_UA, botdDetected: true }, |
| 40 | + { hasFingerprint: false, ephemeral: false }, |
| 41 | + ); |
| 42 | + expect(r.class).toBe("bot"); |
| 43 | + expect(r.verdict).toBe("botd_no_fp"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("overrides lone BotD when fingerprint present (Brave-style)", () => { |
| 47 | + const r = resolveClass({ ua: CHROME_UA, botdDetected: true }, HUMAN_CTX); |
| 48 | + expect(r.class).toBe("human"); |
| 49 | + expect(r.baseRisk).toBe(40); |
| 50 | + expect(r.verdict).toBe("botd_override:human"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("overrides lone BotID on privacy browser with fingerprint", () => { |
| 54 | + const r = resolveClass( |
| 55 | + { ua: CHROME_UA, botIdIsBot: true }, |
| 56 | + { ...HUMAN_CTX, privacyBrowser: true }, |
| 57 | + ); |
| 58 | + expect(r.class).toBe("human"); |
| 59 | + expect(r.baseRisk).toBe(45); |
| 60 | + expect(r.verdict).toBe("botid_override:human"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("does not override lone BotID without privacy browser hint", () => { |
| 64 | + const r = resolveClass({ ua: CHROME_UA, botIdIsBot: true }, HUMAN_CTX); |
| 65 | + expect(r.class).toBe("bot"); |
| 66 | + expect(r.verdict).toBe("botid_only"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("does not override BotD in incognito even with fingerprint", () => { |
| 70 | + const r = resolveClass( |
| 71 | + { ua: CHROME_UA, botdDetected: true }, |
| 72 | + { hasFingerprint: true, ephemeral: true }, |
| 73 | + ); |
| 74 | + expect(r.class).toBe("bot"); |
| 75 | + expect(r.verdict).toBe("botd_only"); |
| 76 | + }); |
| 77 | +}); |
0 commit comments