From 253791cc8f3a92dab9552444639969009d45329e Mon Sep 17 00:00:00 2001 From: Pablo Tovar Date: Mon, 12 Jan 2026 19:54:00 +0100 Subject: [PATCH] test: move about command coverage --- tests/commands.users.about.test.ts | 72 ++++++++++++++++++++++++++++++ tests/commands.users.test.ts | 51 --------------------- 2 files changed, 72 insertions(+), 51 deletions(-) create mode 100644 tests/commands.users.about.test.ts diff --git a/tests/commands.users.about.test.ts b/tests/commands.users.about.test.ts new file mode 100644 index 0000000..51b40ac --- /dev/null +++ b/tests/commands.users.about.test.ts @@ -0,0 +1,72 @@ +import { Command } from 'commander'; +import { afterEach, describe, expect, it, vi } from 'vitest'; +import type { CliContext } from '../src/cli/shared.js'; +import { registerUserCommands } from '../src/commands/users.js'; +import { TwitterClient } from '../src/lib/twitter-client.js'; + +const baseCtx = { + resolveTimeoutFromOptions: () => undefined, + resolveCredentialsFromOptions: async () => ({ + cookies: { authToken: 'auth', ct0: 'ct0', cookieHeader: 'auth=auth; ct0=ct0' }, + warnings: [], + }), + p: () => '', + l: () => '', + printTweets: () => undefined, +} as unknown as CliContext; + +afterEach(() => { + vi.restoreAllMocks(); +}); + +describe('users about command', () => { + it('prints account info JSON for about command', async () => { + const program = new Command(); + registerUserCommands(program, baseCtx); + vi.spyOn(TwitterClient.prototype, 'getUserAboutAccount').mockResolvedValue({ + success: true, + aboutProfile: { + accountBasedIn: 'Spain', + source: 'Spain App Store', + createdCountryAccurate: true, + locationAccurate: false, + learnMoreUrl: 'https://help.twitter.com/profile', + }, + }); + + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => undefined); + await program.parseAsync(['node', 'bird', 'about', 'pablotovar_', '--json']); + + const payload = JSON.parse(String(logSpy.mock.calls[0]?.[0])); + expect(payload).toEqual({ + accountBasedIn: 'Spain', + source: 'Spain App Store', + createdCountryAccurate: true, + locationAccurate: false, + learnMoreUrl: 'https://help.twitter.com/profile', + }); + }); + + it('exits on about command failure', async () => { + const program = new Command(); + registerUserCommands(program, baseCtx); + const exitSpy = vi.spyOn(process, 'exit').mockImplementation(((code?: number) => { + throw new Error(`exit ${code}`); + }) as never); + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined); + vi.spyOn(TwitterClient.prototype, 'getUserAboutAccount').mockResolvedValue({ + success: false, + error: 'Missing about_profile in response', + }); + + try { + await expect(program.parseAsync(['node', 'bird', 'about', 'pablotovar__'])).rejects.toThrow('exit 1'); + expect(errorSpy).toHaveBeenCalledWith( + expect.stringContaining('Failed to fetch account information: Missing about_profile in response'), + ); + } finally { + exitSpy.mockRestore(); + errorSpy.mockRestore(); + } + }); +}); diff --git a/tests/commands.users.test.ts b/tests/commands.users.test.ts index a17de69..bdf7271 100644 --- a/tests/commands.users.test.ts +++ b/tests/commands.users.test.ts @@ -9,7 +9,6 @@ const baseCtx = { resolveRelayBaseUrlFromOptions: () => 'https://relay.example', resolveProfileNameFromOptions: () => undefined, p: () => '', - l: () => '', printTweets: () => undefined, } as unknown as CliContext; @@ -210,54 +209,4 @@ describe('users commands', () => { expect(payload.nextCursor).toBeNull(); expect(followingSpy).toHaveBeenCalledTimes(2); }); - - it('prints account info JSON for about command', async () => { - const program = new Command(); - registerUserCommands(program, baseCtx); - vi.spyOn(TwitterClient.prototype, 'getUserAboutAccount').mockResolvedValue({ - success: true, - aboutProfile: { - accountBasedIn: 'Spain', - source: 'Spain App Store', - createdCountryAccurate: true, - locationAccurate: false, - learnMoreUrl: 'https://help.twitter.com/profile', - }, - }); - - const logSpy = vi.spyOn(console, 'log').mockImplementation(() => undefined); - await program.parseAsync(['node', 'bird', 'about', 'pablotovar_', '--json']); - - const payload = JSON.parse(String(logSpy.mock.calls[0]?.[0])); - expect(payload).toEqual({ - accountBasedIn: 'Spain', - source: 'Spain App Store', - createdCountryAccurate: true, - locationAccurate: false, - learnMoreUrl: 'https://help.twitter.com/profile', - }); - }); - - it('exits on about command failure', async () => { - const program = new Command(); - registerUserCommands(program, baseCtx); - const exitSpy = vi.spyOn(process, 'exit').mockImplementation(((code?: number) => { - throw new Error(`exit ${code}`); - }) as never); - const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined); - vi.spyOn(TwitterClient.prototype, 'getUserAboutAccount').mockResolvedValue({ - success: false, - error: 'Missing about_profile in response', - }); - - try { - await expect(program.parseAsync(['node', 'bird', 'about', 'pablotovar__'])).rejects.toThrow('exit 1'); - expect(errorSpy).toHaveBeenCalledWith( - expect.stringContaining('Failed to fetch account information: Missing about_profile in response'), - ); - } finally { - exitSpy.mockRestore(); - errorSpy.mockRestore(); - } - }); });