basant307/AI_Governance_Project
045
1/**2 * @license3 * Copyright 2025 Google LLC4 * SPDX-License-Identifier: Apache-2.05 */6 7import { describe, it, expect, vi, beforeEach } from 'vitest';8import { getStartupWarnings } from './startupWarnings.js';9import * as fs from 'node:fs/promises';10import { getErrorMessage } from '@qwen-code/qwen-code-core';11 12vi.mock('node:fs/promises', { spy: true });13vi.mock('@qwen-code/qwen-code-core', async (importOriginal) => {14 const actual =15 await importOriginal<typeof import('@qwen-code/qwen-code-core')>();16 return {17 ...actual,18 getErrorMessage: vi.fn(),19 };20});21 22describe('startupWarnings', () => {23 beforeEach(() => {24 vi.resetAllMocks();25 });26 27 it('should return warnings from the file and delete it', async () => {28 const mockWarnings = 'Warning 1\nWarning 2';29 vi.mocked(fs.access).mockResolvedValue();30 vi.mocked(fs.readFile).mockResolvedValue(mockWarnings);31 vi.mocked(fs.unlink).mockResolvedValue();32 33 const warnings = await getStartupWarnings();34 35 expect(fs.access).toHaveBeenCalled();36 expect(fs.readFile).toHaveBeenCalled();37 expect(fs.unlink).toHaveBeenCalled();38 expect(warnings).toEqual(['Warning 1', 'Warning 2']);39 });40 41 it('should return an empty array if the file does not exist', async () => {42 const error = new Error('File not found');43 (error as Error & { code: string }).code = 'ENOENT';44 vi.mocked(fs.access).mockRejectedValue(error);45 46 const warnings = await getStartupWarnings();47 48 expect(warnings).toEqual([]);49 });50 51 it('should return an error message if reading the file fails', async () => {52 const error = new Error('Permission denied');53 vi.mocked(fs.access).mockRejectedValue(error);54 vi.mocked(getErrorMessage).mockReturnValue('Permission denied');55 56 const warnings = await getStartupWarnings();57 58 expect(warnings).toEqual([59 'Error checking/reading warnings file: Permission denied',60 ]);61 });62 63 it('should return a warning if deleting the file fails', async () => {64 const mockWarnings = 'Warning 1';65 vi.mocked(fs.access).mockResolvedValue();66 vi.mocked(fs.readFile).mockResolvedValue(mockWarnings);67 vi.mocked(fs.unlink).mockRejectedValue(new Error('Permission denied'));68 69 const warnings = await getStartupWarnings();70 71 expect(warnings).toEqual([72 'Warning 1',73 'Warning: Could not delete temporary warnings file.',74 ]);75 });76});77 