File size: 1,979 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
52
53
54
55
56
57
58
59
60
61
const DALLE3 = require('../DALLE3');
const { ProxyAgent } = require('undici');

jest.mock('tiktoken');
const processFileURL = jest.fn();

describe('DALLE3 Proxy Configuration', () => {
  let originalEnv;

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

  beforeEach(() => {
    jest.resetModules();
    process.env = { ...originalEnv };
  });

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

  it('should configure ProxyAgent in fetchOptions.dispatcher when PROXY env is set', () => {
    // Set proxy environment variable
    process.env.PROXY = 'http://proxy.example.com:8080';
    process.env.DALLE_API_KEY = 'test-api-key';

    // Create instance
    const dalleWithProxy = new DALLE3({ processFileURL });

    // Check that the openai client exists
    expect(dalleWithProxy.openai).toBeDefined();

    // Check that _options exists and has fetchOptions with a dispatcher
    expect(dalleWithProxy.openai._options).toBeDefined();
    expect(dalleWithProxy.openai._options.fetchOptions).toBeDefined();
    expect(dalleWithProxy.openai._options.fetchOptions.dispatcher).toBeDefined();
    expect(dalleWithProxy.openai._options.fetchOptions.dispatcher).toBeInstanceOf(ProxyAgent);
  });

  it('should not configure ProxyAgent when PROXY env is not set', () => {
    // Ensure PROXY is not set
    delete process.env.PROXY;
    process.env.DALLE_API_KEY = 'test-api-key';

    // Create instance
    const dalleWithoutProxy = new DALLE3({ processFileURL });

    // Check that the openai client exists
    expect(dalleWithoutProxy.openai).toBeDefined();

    // Check that _options exists but fetchOptions either doesn't exist or doesn't have a dispatcher
    expect(dalleWithoutProxy.openai._options).toBeDefined();

    // fetchOptions should either not exist or not have a dispatcher
    if (dalleWithoutProxy.openai._options.fetchOptions) {
      expect(dalleWithoutProxy.openai._options.fetchOptions.dispatcher).toBeUndefined();
    }
  });
});