user.test.js
1 import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; 2 import { getRegistry } from '@jackwener/opencli/registry'; 3 import './user.js'; 4 const command = getRegistry().get('gitee/user'); 5 function createPage(snapshot) { 6 return { 7 goto: vi.fn().mockResolvedValue(undefined), 8 wait: vi.fn().mockResolvedValue(undefined), 9 evaluate: vi.fn().mockResolvedValue(snapshot), 10 }; 11 } 12 describe('gitee user', () => { 13 beforeEach(() => { 14 vi.stubGlobal('fetch', vi.fn()); 15 }); 16 afterEach(() => { 17 vi.unstubAllGlobals(); 18 vi.restoreAllMocks(); 19 }); 20 it('registers the gitee user command', () => { 21 expect(command).toMatchObject({ 22 site: 'gitee', 23 name: 'user', 24 }); 25 }); 26 it('does not mislabel contribution totals as Gitee Index when the real index is unavailable', async () => { 27 const page = createPage({ 28 notFound: false, 29 blocked: false, 30 nickname: 'Alice', 31 followers: '12', 32 publicRepos: '7', 33 giteeIndex: '', 34 contributionTotal: 321, 35 }); 36 vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ 37 login: 'alice', 38 name: 'Alice', 39 followers: 12, 40 public_repos: 7, 41 }), { status: 200 }))); 42 const rows = await command.func(page, { username: 'alice' }); 43 expect(rows).toContainEqual({ field: 'Gitee Index', value: '-' }); 44 }); 45 it('uses an API-provided Gitee Index when available', async () => { 46 const page = createPage({ 47 notFound: false, 48 blocked: false, 49 nickname: '', 50 followers: '', 51 publicRepos: '', 52 giteeIndex: '', 53 }); 54 vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ 55 login: 'alice', 56 followers: 9, 57 public_repos: 3, 58 gitee_index: 88, 59 }), { status: 200 }))); 60 const rows = await command.func(page, { username: 'alice' }); 61 expect(rows).toContainEqual({ field: 'Gitee Index', value: '88' }); 62 }); 63 });