basant307/AI_Governance_Project
048
1/**2 * @license3 * Copyright 2025 Google LLC4 * SPDX-License-Identifier: Apache-2.05 */6 7import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';8import yargs from 'yargs';9import { authCommand, buildRemovalNotice, printRemovalNotice } from './auth.js';10 11describe('auth command', () => {12 const originalNoColor = process.env['NO_COLOR'];13 const originalIsTTY = process.stdout.isTTY;14 15 beforeEach(() => {16 process.env['NO_COLOR'] = '1';17 Object.defineProperty(process.stdout, 'isTTY', {18 value: false,19 configurable: true,20 });21 });22 23 afterEach(() => {24 if (originalNoColor === undefined) {25 delete process.env['NO_COLOR'];26 } else {27 process.env['NO_COLOR'] = originalNoColor;28 }29 Object.defineProperty(process.stdout, 'isTTY', {30 value: originalIsTTY,31 configurable: true,32 });33 vi.restoreAllMocks();34 });35 36 it('builds a removal notice with migration paths and no ANSI in non-TTY', () => {37 const notice = buildRemovalNotice();38 39 expect(notice).toContain('qwen auth has been removed');40 expect(notice).toContain('/auth');41 expect(notice).toContain('/doctor');42 expect(notice).toContain('BAILIAN_CODING_PLAN_API_KEY');43 expect(notice).toContain('https://coding.dashscope.aliyuncs.com/v1');44 expect(notice).toContain('https://coding-intl.dashscope.aliyuncs.com/v1');45 expect(notice).toContain('OPENROUTER_API_KEY');46 expect(notice).not.toContain('\x1b[');47 expect(notice).not.toContain('v0.15.8');48 });49 50 it('writes the notice before exiting', () => {51 const exit = vi.spyOn(process, 'exit').mockImplementation((() => {52 throw new Error('exit');53 }) as never);54 const write = vi.spyOn(process.stdout, 'write').mockImplementation(((55 _chunk: unknown,56 callback?: () => void,57 ) => {58 callback?.();59 return true;60 }) as never);61 62 expect(() => printRemovalNotice()).toThrow('exit');63 64 expect(write).toHaveBeenCalledWith(65 expect.any(String),66 expect.any(Function),67 );68 expect(exit).toHaveBeenCalledWith(0);69 });70 71 it.each([72 ['auth'],73 ['auth status'],74 ['auth api-key'],75 ['auth qwen-oauth'],76 ['auth openrouter --key test-key'],77 ['auth coding-plan --region china --key sk-sp-test'],78 ['auth --key test-key'],79 ])('routes `%s` to the removal notice', async (command) => {80 const exit = vi81 .spyOn(process, 'exit')82 .mockImplementation((() => undefined) as never);83 const write = vi.spyOn(process.stdout, 'write').mockImplementation(((84 _chunk: unknown,85 callback?: () => void,86 ) => {87 callback?.();88 return true;89 }) as never);90 91 await yargs(command.split(' '))92 .scriptName('qwen')93 .command(authCommand)94 .strict()95 .fail((message, error) => {96 throw error ?? new Error(message);97 })98 .parseAsync();99 100 expect(write).toHaveBeenCalledWith(101 expect.any(String),102 expect.any(Function),103 );104 expect(exit).toHaveBeenCalledWith(0);105 });106});107 