114 lines
4.2 KiB
TypeScript
114 lines
4.2 KiB
TypeScript
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: () => '',
|
|
printTweets: () => undefined,
|
|
} as unknown as CliContext;
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
describe('users commands', () => {
|
|
it('requires --all when --max-pages is provided', 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);
|
|
|
|
try {
|
|
await expect(program.parseAsync(['node', 'bird', 'following', '--max-pages', '2'])).rejects.toThrow('exit 1');
|
|
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('--max-pages requires --all.'));
|
|
} finally {
|
|
exitSpy.mockRestore();
|
|
errorSpy.mockRestore();
|
|
}
|
|
});
|
|
|
|
it('emits pagination JSON when --cursor is provided', async () => {
|
|
const program = new Command();
|
|
registerUserCommands(program, baseCtx);
|
|
vi.spyOn(TwitterClient.prototype, 'getCurrentUser').mockResolvedValue({
|
|
success: true,
|
|
user: { id: '123', username: 'tester', name: 'Tester' },
|
|
});
|
|
vi.spyOn(TwitterClient.prototype, 'getFollowing').mockResolvedValue({
|
|
success: true,
|
|
users: [{ id: '1', username: 'alpha', name: 'Alpha' }],
|
|
nextCursor: 'next-1',
|
|
});
|
|
|
|
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => undefined);
|
|
await program.parseAsync(['node', 'bird', 'following', '--cursor', 'prev', '--json']);
|
|
|
|
const payload = JSON.parse(String(logSpy.mock.calls[0]?.[0]));
|
|
expect(payload.users).toHaveLength(1);
|
|
expect(payload.nextCursor).toBe('next-1');
|
|
});
|
|
|
|
it('emits pagination JSON for --all results', async () => {
|
|
const program = new Command();
|
|
registerUserCommands(program, baseCtx);
|
|
vi.spyOn(TwitterClient.prototype, 'getCurrentUser').mockResolvedValue({
|
|
success: true,
|
|
user: { id: '123', username: 'tester', name: 'Tester' },
|
|
});
|
|
vi.spyOn(TwitterClient.prototype, 'getFollowing').mockResolvedValue({
|
|
success: true,
|
|
users: [{ id: '1', username: 'alpha', name: 'Alpha' }],
|
|
nextCursor: 'next-1',
|
|
});
|
|
|
|
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => undefined);
|
|
await program.parseAsync(['node', 'bird', 'following', '--all', '--max-pages', '1', '--json']);
|
|
|
|
const payload = JSON.parse(String(logSpy.mock.calls[0]?.[0]));
|
|
expect(payload.users).toHaveLength(1);
|
|
expect(payload.nextCursor).toBe('next-1');
|
|
});
|
|
|
|
it('stops pagination when cursor repeats or no new users are added', async () => {
|
|
vi.useFakeTimers();
|
|
const program = new Command();
|
|
registerUserCommands(program, baseCtx);
|
|
vi.spyOn(TwitterClient.prototype, 'getCurrentUser').mockResolvedValue({
|
|
success: true,
|
|
user: { id: '123', username: 'tester', name: 'Tester' },
|
|
});
|
|
const followingSpy = vi.spyOn(TwitterClient.prototype, 'getFollowing');
|
|
followingSpy
|
|
.mockResolvedValueOnce({
|
|
success: true,
|
|
users: [{ id: '1', username: 'alpha', name: 'Alpha' }],
|
|
nextCursor: 'cursor-1',
|
|
})
|
|
.mockResolvedValueOnce({
|
|
success: true,
|
|
users: [{ id: '1', username: 'alpha', name: 'Alpha' }],
|
|
nextCursor: 'cursor-1',
|
|
});
|
|
|
|
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => undefined);
|
|
const run = program.parseAsync(['node', 'bird', 'following', '--all', '--json']);
|
|
await vi.advanceTimersByTimeAsync(1000);
|
|
await run;
|
|
|
|
const payload = JSON.parse(String(logSpy.mock.calls[0]?.[0]));
|
|
expect(payload.users).toHaveLength(1);
|
|
expect(payload.nextCursor).toBeNull();
|
|
expect(followingSpy).toHaveBeenCalledTimes(2);
|
|
});
|
|
});
|