okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8667a3dbf
main
1// The agent's keyless web search against a MOCKED, rate-limited DuckDuckGo: spaced requests, a session cache, a cooldown that doubles, 2// Bing as the second source, and one plain sentence (with the place to add a key) when every source refuses. 3import test from 'node:test' ; 4import assert from 'node:assert/strict' ; 5import { readFileSync } from 'node:fs' ; 6import { createDuckDuckGoSearch , BLOCKED_MESSAGE , LIMITED_SENTENCE , UNREACHABLE_MESSAGE } from '../server/web-search-ddg.mjs' ; 7import { bingTarget , parseBingResults , bingLooksBlocked } from '../server/web-search-bing.mjs' ; 8 9const BING = readFileSync ( new URL ( './fixtures/bing-results.html' , import . meta. url ), 'utf8' ); 10const DDG = `<div class="result"><h2 class="result__title"><a rel="nofollow" class="result__a" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Fddg.example%2Fa">DDG one</a></h2> 11<a class="result__snippet" href="x">First snippet</a></div> 12<div class="result"><h2 class="result__title"><a rel="nofollow" class="result__a" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Fddg.example%2Fb">DDG two</a></h2></div>` ; 13const ANOMALY = '<html><body><div class="anomaly-modal">Unfortunately, bots use DuckDuckGo too.</div></body></html>' ; 14 15/** A fake clock whose sleep moves time, and a fake web: `script(url, n)` decides each answer. */ 16function world ( script , { random= () => 0.5 } = {}) { 17let t = 5_000_000 ; 18const calls = []; 19const fetch = async ( url , init ) => { 20const at = t ; 21const call = { url :String ( url ), method :init ?. method ?? 'GET' , at, headers :init ?. headers ?? {} }; 22calls . push ( call ); 23const out = await script ( call , calls . length ); 24if ( out instanceof Error ) throw out ; 25return out ; 26}; 27const search = createDuckDuckGoSearch ({ fetch, now :() => t , sleep :async ms => { t += ms ; }, random}); 28return { search, calls, advance :ms => { t += ms ; }, clock :() => t , hostCalls :host => calls . filter ( c => c . url . includes ( host )) }; 29} 30const page = ( body , status = 200 , headers = {}) => new Response ( body , { status, headers}); 31const duck = url => / duckduckgo\.com / . test ( url ); 32 33test ( 'requests to one source are spaced 2.5 s or more apart, never in a burst' , async () => { 34const w = world (() => page ( DDG )); 35for ( const q of [ 'one' , 'two' , 'three' , 'four' ]) await w . search . search ({ query :q }); 36const at = w . calls . map ( c => c . at ); 37assert . equal ( at . length , 4 ); 38for ( let i = 1 ; i < at . length ; i ++ ) assert . ok ( at [ i ] - at [ i - 1 ] >= 2500 , `gap ${ i } : ${ at [ i ] - at [ i - 1 ]} ms` ); 39}); 40 41test ( 'the request looks like the page\'s own form post: browser headers, Referer, Origin, no hidden tricks' , async () => { 42const w = world (() => page ( DDG )); 43await w . search . search ({ query :'strata' }); 44const [ c ] = w . calls ; 45assert . equal ( c . method , 'POST' ); 46assert . match ( c . headers [ 'user-agent' ], / Mozilla\/5\.0 . * Chrome\/\d +/ ); 47assert . equal ( c . headers . referer , 'https://html.duckduckgo.com/' ); 48assert . equal ( c . headers . origin , 'https://html.duckduckgo.com' ); 49assert . equal ( c . headers [ 'sec-fetch-mode' ], 'navigate' ); 50assert . match ( c . headers [ 'accept-language' ], / en / ); 51}); 52 53test ( 'the same question is answered from the session cache, even while DuckDuckGo is refusing' , async () => { 54let refuse = false ; 55const w = world ( call => ( duck ( call . url ) ?( refuse ?page ( ANOMALY , 202 ) :page ( DDG )) :page ( BING ))); 56const a = await w . search . search ({ query :'Strata Engine' }); 57assert . equal ( a . sources . length , 2 ); 58refuse = true ; 59const b = await w . search . search ({ query :'strata engine' }); 60assert . deepEqual ( b . sources , a . sources ); 61assert . equal ( w . calls . length , 1 , 'no second request' ); 62w . advance ( 20 * 60_000 ); 63assert . equal (( await w . search . search ({ query :'STRATA ENGINE' })). sources . length , 2 , 'still cached after 20 minutes' ); 64assert . equal ( w . calls . length , 1 ); 65w . advance ( 11 * 60_000 ); 66await w . search . search ({ query :'strata engine' }); 67assert . ok ( w . calls . length > 1 , 'asked again after the cache is 30 minutes old' ); 68}); 69 70test ( 'DuckDuckGo refuses (HTTP 202): Bing answers, DuckDuckGo is left alone for a minute, and its lite page is not tried' , async () => { 71const w = world ( call => ( duck ( call . url ) ?page ( ANOMALY , 202 ) :page ( BING ))); 72const first = await w . search . search ({ query :'node release notes' , maxResults :3 }); 73assert . equal ( first . sources . length , 3 ); 74assert . equal ( first . sources [ 0 ]. url , 'https://github.com/Niko1221/Strata' ); 75assert . equal ( w . hostCalls ( 'html.duckduckgo.com' ). length , 1 ); 76assert . equal ( w . hostCalls ( 'lite.duckduckgo.com' ). length , 0 , 'a refusal is not followed by the lite page' ); 77assert . equal ( w . hostCalls ( 'bing.com' ). length , 1 ); 78// The next question goes straight to Bing: DuckDuckGo is cooling down. 79await w . search . search ({ query :'another question' }); 80assert . equal ( w . hostCalls ( 'duckduckgo.com' ). length , 1 , 'no request to DuckDuckGo during the cooldown' ); 81assert . equal ( w . hostCalls ( 'bing.com' ). length , 2 ); 82assert . ok ( w . search . status (). duckduckgo . blockedForMs > 0 ); 83// After the cooldown one probe goes out again. 84w . advance ( 61_000 ); 85await w . search . search ({ query :'a third question' }); 86assert . equal ( w . hostCalls ( 'html.duckduckgo.com' ). length , 2 ); 87}); 88 89test ( 'every source refuses: one plain sentence with the place to add a key; later searches make no request at all' , async () => { 90const w = world ( call => page ( duck ( call . url ) ?ANOMALY :'<html>captcha</html>' , duck ( call . url ) ?202 :429 )); 91const error = await w . search . search ({ query :'hello world' }). catch ( e => e ); 92assert . equal ( error . code , 'WEB_RATE_LIMITED' ); 93assert . equal ( error . message , BLOCKED_MESSAGE ); 94assert . ok ( error . message . startsWith ( LIMITED_SENTENCE )); 95assert . match ( LIMITED_SENTENCE , / ^Web search is limited right now\. Try again in a minute, or add a search key \(Exa, Perplexity or DeepSeek\) in Settings → Tools → Web search\.$ / ); 96assert . match ( error . message , / Do not retry this search / , 'the model is told to stop' ); 97const used = w . calls . length ; 98assert . equal ( used , 2 , 'one request per source, nothing more' ); 99for ( const q of [ 'again' , 'and again' , 'once more' ]) { 100const e = await w . search . search ({ query :q }). catch ( x => x ); 101assert . equal ( e . code , 'WEB_RATE_LIMITED' , q ); 102} 103assert . equal ( w . calls . length , used , 'a model that keeps asking does not extend the block' ); 104}); 105 106test ( 'the cooldown doubles each time a source refuses again, and a Retry-After header is honoured' , async () => { 107const w = world ( call => ( duck ( call . url ) ?page ( ANOMALY , 202 ) :page ( '<html>captcha</html>' , 429 ))); 108await w . search . search ({ query :'q1' }). catch (() => {}); 109const first = w . search . status (). duckduckgo . blockedForMs ; 110assert . ok ( first > 55_000 && first <= 60_000 , `first cooldown ${ first } ` ); 111w . advance ( 61_000 ); 112await w . search . search ({ query :'q2' }). catch (() => {}); 113const second = w . search . status (). duckduckgo . blockedForMs ; 114assert . ok ( second > 115_000 && second <= 120_000 , `second cooldown ${ second } ` ); 115w . advance ( 121_000 ); 116await w . search . search ({ query :'q3' }). catch (() => {}); 117assert . ok ( w . search . status (). duckduckgo . blockedForMs > 235_000 ); 118const r = world ( call => ( duck ( call . url ) ?page ( ANOMALY , 429 , { 'retry-after' :'300' }) :page ( '<html>captcha</html>' , 429 ))); 119await r . search . search ({ query :'x' }). catch (() => {}); 120assert . ok ( r . search . status (). duckduckgo . blockedForMs > 295_000 , 'Retry-After: 300' ); 121}); 122 123test ( 'a recovered source resets the cooldown' , async () => { 124let refuse = true ; 125const w = world ( call => ( duck ( call . url ) ?( refuse ?page ( ANOMALY , 202 ) :page ( DDG )) :page ( '<html>captcha</html>' , 429 ))); 126await w . search . search ({ query :'a' }). catch (() => {}); 127w . advance ( 61_000 ); 128refuse = false ; 129assert . equal (( await w . search . search ({ query :'b' })). sources [ 0 ]. url , 'https://ddg.example/a' ); 130assert . equal ( w . search . status (). duckduckgo . strikes , 0 ); 131assert . equal ( w . search . status (). duckduckgo . blockedForMs , 0 ); 132}); 133 134test ( 'a timeout or a server error gets one retry; two in a row moves on to the next source; nothing reachable says so' , async () => { 135let n = 0 ; 136const flaky = world ( call => ( duck ( call . url ) ?( ++ n === 1 ?page ( 'oops' , 503 ) :page ( DDG )) :page ( BING ))); 137assert . equal (( await flaky . search . search ({ query :'q' })). sources [ 0 ]. url , 'https://ddg.example/a' ); 138assert . equal ( flaky . hostCalls ( 'duckduckgo.com' ). length , 2 ); 139const down = world ( call => ( duck ( call . url ) ?new TypeError ( 'fetch failed' ) :page ( BING ))); 140const viaBing = await down . search . search ({ query :'q' }); 141assert . equal ( viaBing . sources . length , 5 , 'DuckDuckGo unreachable twice: Bing answers' ); 142const none = world (() => new TypeError ( 'fetch failed' )); 143const e = await none . search . search ({ query :'q' }). catch ( x => x ); 144assert . equal ( e . message , UNREACHABLE_MESSAGE ); 145assert . notEqual ( e . code , 'WEB_RATE_LIMITED' ); 146}); 147 148test ( 'a search with no hits is an answer; a cancelled search is cancelled' , async () => { 149const w = world (() => page ( '<html><body>No results found.</body></html>' )); 150assert . deepEqual ( await w . search . search ({ query :'zzzz qqqq' }), { sources :[], truncated :false }); 151const ctrl = new AbortController (); ctrl . abort (); 152const e = await w . search . search ({ query :'later' }, ctrl . signal ). catch ( x => x ); 153assert . equal ( e . code , 'WEB_ABORTED' ); 154}); 155 156test ( 'too many searches in a minute are held back by the app, not sent to the sources' , async () => { 157const w = world (() => page ( DDG )); 158let held = null ; 159for ( let i = 0 ; i < 12 && ! held ; i ++ ) held = await w . search . search ({ query :`question ${ i } ` }). then (() => null , e => e ); 160assert . ok ( held , 'the ninth search in a minute is held back' ); 161assert . equal ( held . code , 'WEB_RATE_LIMITED' ); 162assert . ok ( w . hostCalls ( 'duckduckgo.com' ). length <= 8 ); 163}); 164 165test ( 'Bing: the real result page is read (redirect links decoded, snippets kept); a challenge page is a refusal; ordinary pages are not' , () => { 166const rows = parseBingResults ( BING ); 167assert . equal ( rows . length , 5 ); 168assert . ok ( rows . every ( r => / ^https:\/\/ / . test ( r . url ) && ! r . url . includes ( 'bing.com' )), 'real addresses, not Bing redirects' ); 169assert . equal ( rows [ 1 ]. url , 'https://github.com/Niko1221/Strata/releases' ); 170assert . ok ( rows [ 0 ]. snippet . length > 20 ); 171assert . equal ( bingTarget ( 'https://www.bing.com/ck/a?!&&p=1&u=a1aHR0cHM6Ly9leGFtcGxlLmNvbS9hP3g9MQ&ntb=1' ), 'https://example.com/a?x=1' ); 172assert . equal ( bingTarget ( 'https://www.bing.com/images/search?q=x' ), null ); 173assert . equal ( bingTarget ( 'https://example.org/direct' ), 'https://example.org/direct' ); 174assert . equal ( bingLooksBlocked ( 429 , '' ), true ); 175assert . equal ( bingLooksBlocked ( 200 , '<html><form>captcha</form></html>' ), true ); 176assert . equal ( bingLooksBlocked ( 200 , BING ), false ); 177assert . deepEqual ( parseBingResults ( '<html>nothing here</html>' ), []); 178});