refactor: split twitter client

This commit is contained in:
Peter Steinberger
2026-01-01 13:58:08 +01:00
parent 51bf79d9ed
commit 7a4782ddc4
15 changed files with 2716 additions and 2663 deletions
+3 -7
View File
@@ -1,5 +1,6 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { TwitterClient } from '../src/lib/twitter-client.js';
import { extractArticleText } from '../src/lib/twitter-client-utils.js';
const validCookies = {
authToken: 'test_auth_token',
@@ -17,7 +18,6 @@ type ResponseLike = {
type TwitterClientUploadPrivate = TwitterClient & {
sleep: (ms: number) => Promise<void>;
extractArticleText: (result: unknown) => string | undefined;
};
const makeResponse = (overrides: Partial<ResponseLike> = {}): ResponseLike => ({
@@ -188,12 +188,10 @@ describe('TwitterClient upload coverage', () => {
describe('article extraction edge cases', () => {
it('logs article payloads when debug flag is set', () => {
const client = new TwitterClient({ cookies: validCookies });
process.env.BIRD_DEBUG_ARTICLE = '1';
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const clientPrivate = client as unknown as TwitterClientUploadPrivate;
const result = clientPrivate.extractArticleText({
const result = extractArticleText({
rest_id: '1',
article: {
title: 'Title',
@@ -207,9 +205,7 @@ describe('TwitterClient upload coverage', () => {
});
it('drops duplicate body when it matches the title', () => {
const client = new TwitterClient({ cookies: validCookies });
const clientPrivate = client as unknown as TwitterClientUploadPrivate;
const result = clientPrivate.extractArticleText({
const result = extractArticleText({
rest_id: '1',
article: {
title: 'Same',
+5 -12
View File
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { type TweetData, TwitterClient } from '../src/lib/twitter-client.js';
import { mapTweetResult } from '../src/lib/twitter-client-utils.js';
describe('TwitterClient', () => {
const originalFetch = global.fetch;
@@ -992,9 +993,7 @@ describe('TwitterClient', () => {
root.quoted_status_result = { result: quoted };
const client = new TwitterClient({ cookies: validCookies });
const mapped = (
client as unknown as { mapTweetResult: (result: unknown) => TweetData | undefined }
).mapTweetResult(root);
const mapped = mapTweetResult(root, (client as { quoteDepth: number }).quoteDepth);
expect(mapped?.quotedTweet?.id).toBe('2');
expect(mapped?.quotedTweet?.quotedTweet).toBeUndefined();
@@ -1006,9 +1005,7 @@ describe('TwitterClient', () => {
root.quoted_status_result = { result: quoted };
const client = new TwitterClient({ cookies: validCookies, quoteDepth: 0 });
const mapped = (
client as unknown as { mapTweetResult: (result: unknown) => TweetData | undefined }
).mapTweetResult(root);
const mapped = mapTweetResult(root, (client as { quoteDepth: number }).quoteDepth);
expect(mapped?.quotedTweet).toBeUndefined();
});
@@ -1021,9 +1018,7 @@ describe('TwitterClient', () => {
root.quoted_status_result = { result: quoted1 };
const client = new TwitterClient({ cookies: validCookies, quoteDepth: 2 });
const mapped = (
client as unknown as { mapTweetResult: (result: unknown) => TweetData | undefined }
).mapTweetResult(root);
const mapped = mapTweetResult(root, (client as { quoteDepth: number }).quoteDepth);
expect(mapped?.quotedTweet?.id).toBe('2');
expect(mapped?.quotedTweet?.quotedTweet?.id).toBe('3');
@@ -1041,9 +1036,7 @@ describe('TwitterClient', () => {
};
const client = new TwitterClient({ cookies: validCookies });
const mapped = (
client as unknown as { mapTweetResult: (result: unknown) => TweetData | undefined }
).mapTweetResult(root);
const mapped = mapTweetResult(root, (client as { quoteDepth: number }).quoteDepth);
expect(mapped?.quotedTweet?.id).toBe('2');
});