File size: 1,434 Bytes
f0743f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const GoogleSearch = require('../GoogleSearch');

jest.mock('node-fetch');
jest.mock('@langchain/core/utils/env');

describe('GoogleSearch', () => {
  let originalEnv;
  const mockApiKey = 'mock_api';
  const mockSearchEngineId = 'mock_search_engine_id';

  beforeAll(() => {
    originalEnv = { ...process.env };
  });

  beforeEach(() => {
    jest.resetModules();
    process.env = {
      ...originalEnv,
      GOOGLE_SEARCH_API_KEY: mockApiKey,
      GOOGLE_CSE_ID: mockSearchEngineId,
    };
  });

  afterEach(() => {
    jest.clearAllMocks();
    process.env = originalEnv;
  });

  it('should use mockApiKey and mockSearchEngineId when environment variables are not set', () => {
    const instance = new GoogleSearch({
      GOOGLE_SEARCH_API_KEY: mockApiKey,
      GOOGLE_CSE_ID: mockSearchEngineId,
    });
    expect(instance.apiKey).toBe(mockApiKey);
    expect(instance.searchEngineId).toBe(mockSearchEngineId);
  });

  it('should throw an error if GOOGLE_SEARCH_API_KEY or GOOGLE_CSE_ID is missing', () => {
    delete process.env.GOOGLE_SEARCH_API_KEY;
    expect(() => new GoogleSearch()).toThrow(
      'Missing GOOGLE_SEARCH_API_KEY or GOOGLE_CSE_ID environment variable.',
    );

    process.env.GOOGLE_SEARCH_API_KEY = mockApiKey;
    delete process.env.GOOGLE_CSE_ID;
    expect(() => new GoogleSearch()).toThrow(
      'Missing GOOGLE_SEARCH_API_KEY or GOOGLE_CSE_ID environment variable.',
    );
  });
});