CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
auth.test.ts67 linesDownload Raw Back to core
1/**2 * @license3 * Copyright 2025 Qwen Code4 * SPDX-License-Identifier: Apache-2.05 */6 7import { beforeEach, describe, expect, it, vi } from 'vitest';8import { performInitialAuth } from './auth.js';9 10const mockLogAuth = vi.fn();11vi.mock('@qwen-code/qwen-code-core', () => ({12  getErrorMessage: (e: unknown) => (e instanceof Error ? e.message : String(e)),13  logAuth: (...args: unknown[]) => mockLogAuth(...args),14  AuthEvent: vi.fn().mockImplementation((type, method, status, message?) => ({15    type,16    method,17    status,18    message,19  })),20}));21 22describe('performInitialAuth', () => {23  let mockConfig: {24    refreshAuth: ReturnType<typeof vi.fn>;25  };26 27  beforeEach(() => {28    vi.clearAllMocks();29    mockConfig = {30      refreshAuth: vi.fn(),31    };32  });33 34  it('should return null when authType is undefined', async () => {35    const result = await performInitialAuth(mockConfig as never, undefined);36 37    expect(result).toBeNull();38    expect(mockConfig.refreshAuth).not.toHaveBeenCalled();39    expect(mockLogAuth).not.toHaveBeenCalled();40  });41 42  it('should return null on successful authentication', async () => {43    mockConfig.refreshAuth.mockResolvedValue(undefined);44 45    const result = await performInitialAuth(46      mockConfig as never,47      'api_key' as never,48    );49 50    expect(result).toBeNull();51    expect(mockConfig.refreshAuth).toHaveBeenCalledWith('api_key', true);52    expect(mockLogAuth).toHaveBeenCalledTimes(1);53  });54 55  it('should return error message on authentication failure', async () => {56    mockConfig.refreshAuth.mockRejectedValue(new Error('Invalid API key'));57 58    const result = await performInitialAuth(59      mockConfig as never,60      'api_key' as never,61    );62 63    expect(result).toBe('Failed to login. Message: Invalid API key');64    expect(mockLogAuth).toHaveBeenCalledTimes(1);65  });66});67 
basant307/AI_Governance_Project · CoolFace