basant307/AI_Governance_Project
045
1/**2 * @license3 * Copyright 2025 Qwen Code4 * SPDX-License-Identifier: Apache-2.05 */6 7import { beforeEach, describe, expect, it, vi } from 'vitest';8import { connectIdeForStartup, initializeApp } from './initializer.js';9 10const mockPerformInitialAuth = vi.fn();11const mockValidateTheme = vi.fn();12const mockInitializeI18n = vi.fn();13 14vi.mock('./auth.js', () => ({15 performInitialAuth: (...args: unknown[]) => mockPerformInitialAuth(...args),16}));17 18vi.mock('./theme.js', () => ({19 validateTheme: (...args: unknown[]) => mockValidateTheme(...args),20}));21 22vi.mock('../i18n/index.js', () => ({23 initializeI18n: (...args: unknown[]) => mockInitializeI18n(...args),24 resolveLanguageSetting: (settingsLang?: string) =>25 process.env['QWEN_CODE_LANG'] || settingsLang || 'auto',26}));27 28const mockConnect = vi.fn();29const mockGetInstance = vi.fn().mockResolvedValue({ connect: mockConnect });30const mockLogIdeConnection = vi.fn();31 32vi.mock('@qwen-code/qwen-code-core', async (importOriginal) => {33 const actual = await importOriginal<Record<string, unknown>>();34 return {35 ...actual,36 IdeClient: { getInstance: () => mockGetInstance() },37 IdeConnectionEvent: vi.fn().mockImplementation((type) => ({ type })),38 IdeConnectionType: { START: 'start' },39 logIdeConnection: (...args: unknown[]) => mockLogIdeConnection(...args),40 };41});42 43describe('initializeApp', () => {44 let mockConfig: {45 getModelsConfig: ReturnType<typeof vi.fn>;46 getIdeMode: ReturnType<typeof vi.fn>;47 getGeminiMdFileCount: ReturnType<typeof vi.fn>;48 };49 let mockSettings: {50 merged: Record<string, unknown>;51 setValue: ReturnType<typeof vi.fn>;52 };53 54 beforeEach(() => {55 vi.clearAllMocks();56 57 mockConfig = {58 getModelsConfig: vi.fn().mockReturnValue({59 getCurrentAuthType: vi.fn().mockReturnValue('api_key'),60 wasAuthTypeExplicitlyProvided: vi.fn().mockReturnValue(false),61 }),62 getIdeMode: vi.fn().mockReturnValue(false),63 getGeminiMdFileCount: vi.fn().mockReturnValue(0),64 };65 66 mockSettings = {67 merged: { general: { language: 'en' } },68 setValue: vi.fn(),69 };70 71 mockPerformInitialAuth.mockResolvedValue(null);72 mockValidateTheme.mockReturnValue(null);73 mockInitializeI18n.mockResolvedValue(undefined);74 });75 76 it('should initialize i18n with language from settings', async () => {77 await initializeApp(mockConfig as never, mockSettings as never);78 79 expect(mockInitializeI18n).toHaveBeenCalledWith('en');80 });81 82 it('should initialize i18n with QWEN_CODE_LANG env var if set', async () => {83 vi.stubEnv('QWEN_CODE_LANG', 'zh');84 85 await initializeApp(mockConfig as never, mockSettings as never);86 expect(mockInitializeI18n).toHaveBeenCalledWith('zh');87 88 vi.unstubAllEnvs();89 });90 91 it('should return no errors on successful initialization', async () => {92 const result = await initializeApp(93 mockConfig as never,94 mockSettings as never,95 );96 97 expect(result.authError).toBeNull();98 expect(result.themeError).toBeNull();99 expect(result.geminiMdFileCount).toBe(0);100 });101 102 it('should return authError when auth fails', async () => {103 mockPerformInitialAuth.mockResolvedValue('Auth failed');104 105 const result = await initializeApp(106 mockConfig as never,107 mockSettings as never,108 );109 110 expect(result.authError).toBe('Auth failed');111 expect(result.shouldOpenAuthDialog).toBe(true);112 // initializeApp does not clear the selected auth type on failure113 expect(mockSettings.setValue).not.toHaveBeenCalled();114 });115 116 it('should return themeError when theme validation fails', async () => {117 mockValidateTheme.mockReturnValue('Theme not found');118 119 const result = await initializeApp(120 mockConfig as never,121 mockSettings as never,122 );123 124 expect(result.themeError).toBe('Theme not found');125 });126 127 it('should set shouldOpenAuthDialog when auth was not explicitly provided', async () => {128 mockConfig129 .getModelsConfig()130 .wasAuthTypeExplicitlyProvided.mockReturnValue(false);131 132 const result = await initializeApp(133 mockConfig as never,134 mockSettings as never,135 );136 137 expect(result.shouldOpenAuthDialog).toBe(true);138 });139 140 it('should set shouldOpenAuthDialog when auth error occurs', async () => {141 mockConfig142 .getModelsConfig()143 .wasAuthTypeExplicitlyProvided.mockReturnValue(true);144 mockPerformInitialAuth.mockResolvedValue('Auth failed');145 146 const result = await initializeApp(147 mockConfig as never,148 mockSettings as never,149 );150 151 expect(result.shouldOpenAuthDialog).toBe(true);152 });153 154 it('should not open auth dialog when auth was explicitly provided and succeeds', async () => {155 mockConfig156 .getModelsConfig()157 .wasAuthTypeExplicitlyProvided.mockReturnValue(true);158 159 const result = await initializeApp(160 mockConfig as never,161 mockSettings as never,162 );163 164 expect(result.shouldOpenAuthDialog).toBe(false);165 });166 167 it('should connect to IDE by default when in IDE mode', async () => {168 mockConfig.getIdeMode.mockReturnValue(true);169 170 await initializeApp(mockConfig as never, mockSettings as never);171 172 expect(mockGetInstance).toHaveBeenCalled();173 expect(mockConnect).toHaveBeenCalled();174 expect(mockLogIdeConnection).toHaveBeenCalled();175 });176 177 it('should not connect to IDE when deferred', async () => {178 mockConfig.getIdeMode.mockReturnValue(true);179 180 await initializeApp(mockConfig as never, mockSettings as never, {181 deferIdeConnection: true,182 });183 184 expect(mockGetInstance).not.toHaveBeenCalled();185 expect(mockConnect).not.toHaveBeenCalled();186 expect(mockLogIdeConnection).not.toHaveBeenCalled();187 });188 189 it('should connect to IDE through startup helper', async () => {190 mockConfig.getIdeMode.mockReturnValue(true);191 192 await connectIdeForStartup(mockConfig as never);193 194 expect(mockGetInstance).toHaveBeenCalled();195 expect(mockConnect).toHaveBeenCalled();196 expect(mockLogIdeConnection).toHaveBeenCalled();197 });198 199 it('should not connect to IDE through startup helper when not in IDE mode', async () => {200 mockConfig.getIdeMode.mockReturnValue(false);201 202 await connectIdeForStartup(mockConfig as never);203 204 expect(mockGetInstance).not.toHaveBeenCalled();205 expect(mockConnect).not.toHaveBeenCalled();206 expect(mockLogIdeConnection).not.toHaveBeenCalled();207 });208 209 it('should not connect to IDE when not in IDE mode', async () => {210 mockConfig.getIdeMode.mockReturnValue(false);211 212 await initializeApp(mockConfig as never, mockSettings as never);213 214 expect(mockGetInstance).not.toHaveBeenCalled();215 });216 217 it('should default language to auto when no setting is provided', async () => {218 mockSettings.merged = {};219 220 await initializeApp(mockConfig as never, mockSettings as never);221 222 expect(mockInitializeI18n).toHaveBeenCalledWith('auto');223 });224});225 