Improve tweet read reliability and test coverage
This commit is contained in:
@@ -2,12 +2,18 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { TwitterClient } from '../src/lib/twitter-client.js';
|
||||
|
||||
describe('TwitterClient', () => {
|
||||
const originalFetch = global.fetch;
|
||||
const validCookies = {
|
||||
authToken: 'test_auth_token',
|
||||
ct0: 'test_ct0_token',
|
||||
source: 'test',
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
global.fetch = originalFetch;
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('constructor', () => {
|
||||
it('should throw if authToken is missing', () => {
|
||||
expect(
|
||||
@@ -41,10 +47,6 @@ describe('TwitterClient', () => {
|
||||
global.fetch = mockFetch;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('should post a tweet successfully', async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
@@ -127,10 +129,6 @@ describe('TwitterClient', () => {
|
||||
global.fetch = mockFetch;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('should post a reply with correct reply_to_tweet_id', async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
@@ -159,4 +157,53 @@ describe('TwitterClient', () => {
|
||||
expect(body.variables.tweet_text).toBe('This is a reply');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTweet', () => {
|
||||
let mockFetch: ReturnType<typeof vi.fn>;
|
||||
|
||||
beforeEach(() => {
|
||||
mockFetch = vi.fn();
|
||||
global.fetch = mockFetch;
|
||||
});
|
||||
|
||||
it('should return tweet data from root tweetResult', async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
data: {
|
||||
tweetResult: {
|
||||
result: {
|
||||
rest_id: '12345',
|
||||
legacy: {
|
||||
full_text: 'Root tweet text',
|
||||
created_at: 'Mon Jan 01 00:00:00 +0000 2024',
|
||||
reply_count: 1,
|
||||
retweet_count: 2,
|
||||
favorite_count: 3,
|
||||
},
|
||||
core: {
|
||||
user_results: {
|
||||
result: {
|
||||
legacy: {
|
||||
screen_name: 'user',
|
||||
name: 'User Name',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const result = await client.getTweet('12345');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.tweet?.id).toBe('12345');
|
||||
expect(result.tweet?.text).toBe('Root tweet text');
|
||||
expect(result.tweet?.author.username).toBe('user');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user