/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { describe, it, expect, vi, beforeEach, afterAll } from 'vitest';
import { createContentGenerator, AuthType, createContentGeneratorConfig, } from './contentGenerator.js';
import { createCodeAssistContentGenerator } from '../code_assist/codeAssist.js';
import { GoogleGenAI } from '@google/genai';
vi.mock('../code_assist/codeAssist.js');
vi.mock('@google/genai');
const mockConfig = {
    getCliVersion: vi.fn().mockReturnValue('1.0.0'),
};
describe('createContentGenerator', () => {
    it('should create a CodeAssistContentGenerator', async () => {
        const mockGenerator = {};
        vi.mocked(createCodeAssistContentGenerator).mockResolvedValue(mockGenerator);
        const generator = await createContentGenerator({
            model: 'test-model',
            authType: AuthType.LOGIN_WITH_GOOGLE,
        }, mockConfig);
        expect(createCodeAssistContentGenerator).toHaveBeenCalled();
        expect(generator).toBe(mockGenerator);
    });
    it('should create a GoogleGenAI content generator', async () => {
        const mockGenerator = {
            models: {},
        };
        vi.mocked(GoogleGenAI).mockImplementation(() => mockGenerator);
        const generator = await createContentGenerator({
            model: 'test-model',
            apiKey: 'test-api-key',
            authType: AuthType.USE_GEMINI,
        }, mockConfig);
        expect(GoogleGenAI).toHaveBeenCalledWith({
            apiKey: 'test-api-key',
            vertexai: undefined,
            httpOptions: {
                headers: {
                    'User-Agent': expect.any(String),
                },
            },
        });
        expect(generator).toBe(mockGenerator.models);
    });
});
describe('createContentGeneratorConfig', () => {
    const originalEnv = process.env;
    const mockConfig = {
        getModel: vi.fn().mockReturnValue('gemini-pro'),
        setModel: vi.fn(),
        flashFallbackHandler: vi.fn(),
        getProxy: vi.fn(),
        getEnableOpenAILogging: vi.fn().mockReturnValue(false),
        getSamplingParams: vi.fn().mockReturnValue(undefined),
        getContentGeneratorTimeout: vi.fn().mockReturnValue(undefined),
        getContentGeneratorMaxRetries: vi.fn().mockReturnValue(undefined),
        getCliVersion: vi.fn().mockReturnValue('1.0.0'),
    };
    beforeEach(() => {
        // Reset modules to re-evaluate imports and environment variables
        vi.resetModules();
        // Restore process.env before each test
        process.env = { ...originalEnv };
        vi.clearAllMocks();
    });
    afterAll(() => {
        // Restore original process.env after all tests
        process.env = originalEnv;
    });
    it('should configure for Gemini using GEMINI_API_KEY when set', async () => {
        process.env.GEMINI_API_KEY = 'env-gemini-key';
        const config = await createContentGeneratorConfig(mockConfig, AuthType.USE_GEMINI);
        expect(config.apiKey).toBe('env-gemini-key');
        expect(config.vertexai).toBe(false);
    });
    it('should not configure for Gemini if GEMINI_API_KEY is empty', async () => {
        process.env.GEMINI_API_KEY = '';
        const config = await createContentGeneratorConfig(mockConfig, AuthType.USE_GEMINI);
        expect(config.apiKey).toBeUndefined();
        expect(config.vertexai).toBeUndefined();
    });
    it('should configure for Vertex AI using GOOGLE_API_KEY when set', async () => {
        process.env.GOOGLE_API_KEY = 'env-google-key';
        const config = await createContentGeneratorConfig(mockConfig, AuthType.USE_VERTEX_AI);
        expect(config.apiKey).toBe('env-google-key');
        expect(config.vertexai).toBe(true);
    });
    it('should configure for Vertex AI using GCP project and location when set', async () => {
        process.env.GOOGLE_CLOUD_PROJECT = 'env-gcp-project';
        process.env.GOOGLE_CLOUD_LOCATION = 'env-gcp-location';
        const config = await createContentGeneratorConfig(mockConfig, AuthType.USE_VERTEX_AI);
        expect(config.vertexai).toBe(true);
        expect(config.apiKey).toBeUndefined();
    });
    it('should not configure for Vertex AI if required env vars are empty', async () => {
        process.env.GOOGLE_API_KEY = '';
        process.env.GOOGLE_CLOUD_PROJECT = '';
        process.env.GOOGLE_CLOUD_LOCATION = '';
        const config = await createContentGeneratorConfig(mockConfig, AuthType.USE_VERTEX_AI);
        expect(config.apiKey).toBeUndefined();
        expect(config.vertexai).toBeUndefined();
    });
});
//# sourceMappingURL=contentGenerator.test.js.map