File size: 3,101 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// file deepcode ignore HardcodedNonCryptoSecret: No hardcoded secrets
const { getUserKey } = require('~/server/services/UserService');
const initializeClient = require('./initialize');
const { GoogleClient } = require('~/app');

jest.mock('~/server/services/UserService', () => ({
  checkUserKeyExpiry: jest.requireActual('~/server/services/UserService').checkUserKeyExpiry,
  getUserKey: jest.fn().mockImplementation(() => ({})),
}));

// Config is now passed via req.config, not getAppConfig

const app = { locals: {} };

describe('google/initializeClient', () => {
  afterEach(() => {
    jest.clearAllMocks();
  });

  test('should initialize GoogleClient with user-provided credentials', async () => {
    process.env.GOOGLE_KEY = 'user_provided';
    process.env.GOOGLE_REVERSE_PROXY = 'http://reverse.proxy';
    process.env.PROXY = 'http://proxy';

    const expiresAt = new Date(Date.now() + 60000).toISOString();

    const req = {
      body: { key: expiresAt },
      user: { id: '123' },
      app,
      config: {
        endpoints: {
          all: {},
          google: {},
        },
      },
    };
    const res = {};
    const endpointOption = { modelOptions: { model: 'default-model' } };

    const { client, credentials } = await initializeClient({ req, res, endpointOption });

    expect(getUserKey).toHaveBeenCalledWith({ userId: '123', name: 'google' });
    expect(client).toBeInstanceOf(GoogleClient);
    expect(client.options.reverseProxyUrl).toBe('http://reverse.proxy');
    expect(client.options.proxy).toBe('http://proxy');
    expect(credentials).toEqual({});
  });

  test('should initialize GoogleClient with service key credentials', async () => {
    process.env.GOOGLE_KEY = 'service_key';
    process.env.GOOGLE_REVERSE_PROXY = 'http://reverse.proxy';
    process.env.PROXY = 'http://proxy';

    const req = {
      body: { key: null },
      user: { id: '123' },
      app,
      config: {
        endpoints: {
          all: {},
          google: {},
        },
      },
    };
    const res = {};
    const endpointOption = { modelOptions: { model: 'default-model' } };

    const { client, credentials } = await initializeClient({ req, res, endpointOption });

    expect(client).toBeInstanceOf(GoogleClient);
    expect(client.options.reverseProxyUrl).toBe('http://reverse.proxy');
    expect(client.options.proxy).toBe('http://proxy');
    expect(credentials).toEqual({
      GOOGLE_SERVICE_KEY: {},
      GOOGLE_API_KEY: 'service_key',
    });
  });

  test('should handle expired user-provided key', async () => {
    process.env.GOOGLE_KEY = 'user_provided';

    const expiresAt = new Date(Date.now() - 10000).toISOString(); // Expired
    const req = {
      body: { key: expiresAt },
      user: { id: '123' },
      app,
      config: {
        endpoints: {
          all: {},
          google: {},
        },
      },
    };
    const res = {};
    const endpointOption = { modelOptions: { model: 'default-model' } };
    await expect(initializeClient({ req, res, endpointOption })).rejects.toThrow(
      /expired_user_key/,
    );
  });
});