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