File size: 1,698 Bytes
897170b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, expect, it, vi } from 'vitest';
import { searchWikipedia } from './web-search';

describe('Wikipedia search provider', () => {
  it('uses anonymous CORS search and returns plain-text results', async () => {
    const fetchMock = vi.fn(async () => new Response(JSON.stringify({
      query: { search: [{ title: 'WebGPU', snippet: '<span>GPU</span> API &amp; browser', timestamp: '2026-01-01T00:00:00Z' }] },
    }), { status: 200 })) as unknown as typeof fetch;

    const result = await searchWikipedia(' WebGPU ', 'en', 3, undefined, fetchMock);
    const [url, init] = vi.mocked(fetchMock).mock.calls[0] ?? [];
    expect(String(url)).toContain('en.wikipedia.org/w/api.php');
    expect(String(url)).toContain('list=search');
    expect(String(url)).toContain('origin=*');
    expect(init).toMatchObject({ credentials: 'omit', referrerPolicy: 'no-referrer' });
    expect(result).toEqual({
      provider: 'wikipedia',
      query: 'WebGPU',
      language: 'en',
      results: [{
        title: 'WebGPU',
        snippet: 'GPU API & browser',
        url: 'https://en.wikipedia.org/wiki/WebGPU',
        updatedAt: '2026-01-01T00:00:00Z',
      }],
    });
  });

  it('infers Russian and bounds the requested result count', async () => {
    const fetchMock = vi.fn(async () => new Response('{"query":{"search":[]}}', { status: 200 })) as unknown as typeof fetch;
    const result = await searchWikipedia('браузер', undefined, 99, undefined, fetchMock);
    const [url] = vi.mocked(fetchMock).mock.calls[0] ?? [];
    expect(result.language).toBe('ru');
    expect(String(url)).toContain('ru.wikipedia.org');
    expect(String(url)).toContain('srlimit=8');
  });
});