Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project : SPIDER — Universal Web Intelligence Engine7# File : tests/unit/core/test_canonical_robots.py8# Module : Tests9# Repository : https://git.spboucher.ai/spider10# Project Website : Not specified11# Created : 2026-12-0412# Last Modified : 2026-12-0413#14# Author : Simon-Pierre Boucher15# Position : Professor of Finance16# Institution : Université du Québec en Outaouais (UQO)17# Organization : SP Boucher18# Website : https://spboucher.ai19# Git : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Development : dev@spboucher.ai23# Support : support@spboucher.ai24# Security/Admin : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License : ISC33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041from datetime import UTC, datetime4243from spider.canonical import canonicalize, hamming, in_scope, simhash, trap_reason, url_shape44from spider.robots import Robots454647def test_canonicalize_basics():48 c = canonicalize49 assert c("HTTP://Example.COM:80/a/../b/./c?utm_source=x&b=2&a=1#frag") == "http://example.com/b/c?a=1&b=2"50 assert c("https://example.com") == "https://example.com/"51 assert c("https://example.com:443/x/") == "https://example.com/x/"52 assert c("https://example.com:8443/x") == "https://example.com:8443/x"53 assert c("/p?fbclid=1&gclid=2", "https://example.com/dir/") == "https://example.com/p"54 assert c("https://example.com/%7Euser/%e9t%C3%A9") == "https://example.com/~user/%E9t%C3%A9"55 assert c("https://exämple.com/") == "https://xn--exmple-cua.com/"56 for bad in ("javascript:void(0)", "mailto:a@b.c", "tel:123", "data:text/html,x", "ftp://x/", "", "#top"):57 assert c(bad) is None58 assert c("#top", "https://example.com/a") == "https://example.com/a"596061def test_semantic_parameters_are_kept():62 a = canonicalize("https://s.example/catalog?category=books")63 b = canonicalize("https://s.example/catalog?category=music")64 assert a != b65 assert canonicalize("https://s.example/p?ref=y&id=3") == "https://s.example/p?id=3&ref=y" # ref is not assumed tracking66 assert canonicalize("https://s.example/f?size=m&color=red") == canonicalize("https://s.example/f?color=red&size=m")67 assert canonicalize("https://s.example/f?t=1&t=2") != canonicalize("https://s.example/f?t=2&t=1")686970def test_scope():71 seeds = ["https://example.com"]72 kw = dict(same_origin_only=True, include_subdomains=False, allow_domains=[])73 assert in_scope("https://example.com/x", seeds, **kw)74 assert not in_scope("http://example.com/x", seeds, **kw)75 assert not in_scope("https://blog.example.com/x", seeds, **kw)76 assert in_scope("https://blog.example.com/x", seeds, **{**kw, "include_subdomains": True})77 assert not in_scope("https://evilexample.com/x", seeds, **{**kw, "include_subdomains": True})78 assert in_scope("https://cdn.other.net/x", seeds, **{**kw, "allow_domains": ["other.net"]})79 assert not in_scope("https://example.com.evil.net/", seeds, **{**kw, "include_subdomains": True})808182def test_traps_and_shapes():83 now = datetime(2026, 12, 4, tzinfo=UTC)84 assert url_shape("https://s.example/blog?page=3") == url_shape("https://s.example/blog?page=99")85 assert url_shape("https://s.example/trap/calendar/2026/12") == "s.example/trap/calendar/{n}/{n}"86 assert url_shape("https://s.example/f?color=red&size=m") == url_shape("https://s.example/f?size=l&color=blue")87 assert trap_reason("https://s.example/a/b/a/b/a/b", now) == "repeating_path_segment"88 assert trap_reason("https://s.example/trap/calendar/2031/05", now) == "calendar_out_of_range"89 assert trap_reason("https://s.example/trap/calendar/2026/11", now) is None90 assert trap_reason("https://s.example/blog/2019/05/post", now) is None919293def test_simhash_near_duplicates():94 a = "le chat mange la souris dans la cuisine pendant que le chien dort sur le tapis du salon " * 495 b = a.replace("cuisine", "cave", 1)96 c = "quarterly revenue grew by twelve percent driven by strong cloud demand across all regions " * 497 assert hamming(simhash(a), simhash(b)) < 8 < hamming(simhash(a), simhash(c))9899100ROBOTS = """101User-agent: *102Disallow: /private/103Disallow: /*.pdf$104Allow: /private/public-note105Crawl-delay: 2106107User-agent: SpiderBot108Disallow: /no-spider/109Allow: /110111Sitemap: https://s.example/sitemap.xml112"""113114115def test_robots_groups_and_longest_match():116 rb = Robots.parse(ROBOTS)117 assert rb.sitemaps == ["https://s.example/sitemap.xml"]118 # the specific group replaces the * group for SpiderBot (RFC 9309)119 assert not rb.allowed("https://s.example/no-spider/page", "SpiderBot/0.1 (+https://x)")120 assert rb.allowed("https://s.example/private/x", "SpiderBot")121 assert not rb.allowed("https://s.example/private/x", "OtherBot")122 assert rb.allowed("https://s.example/private/public-note", "OtherBot")123 assert not rb.allowed("https://s.example/doc/a.pdf", "OtherBot")124 assert rb.allowed("https://s.example/doc/a.pdf?x=1", "OtherBot")125 assert rb.crawl_delay("OtherBot") == 2 and rb.crawl_delay("SpiderBot") is None126127128def test_robots_status_rules():129 assert Robots.from_status(404).allowed("https://s.example/x", "SpiderBot")130 assert not Robots.from_status(503).allowed("https://s.example/x", "SpiderBot")131 assert not Robots.from_status(429).allowed("https://s.example/x", "SpiderBot")132 assert not Robots.from_status(0).allowed("https://s.example/x", "SpiderBot")133 assert Robots.parse("User-agent: *\nDisallow:\n").allowed("https://s.example/x", "SpiderBot")134 assert not Robots.parse("User-agent: *\nDisallow: /\n").allowed("https://s.example/x", "SpiderBot")135