feat: add config defaults, Firefox cookies, and Sweetistics whoami
This commit is contained in:
@@ -98,9 +98,11 @@ describe('cookies', () => {
|
||||
const result = await resolveCredentials({});
|
||||
|
||||
expect(result.warnings).toContain(
|
||||
'Missing auth_token - provide via --auth-token, AUTH_TOKEN env var, or login to x.com in Chrome',
|
||||
'Missing auth_token - provide via --auth-token, AUTH_TOKEN env var, or login to x.com in Chrome/Firefox',
|
||||
);
|
||||
expect(result.warnings).toContain(
|
||||
'Missing ct0 - provide via --ct0, CT0 env var, or login to x.com in Chrome/Firefox',
|
||||
);
|
||||
expect(result.warnings).toContain('Missing ct0 - provide via --ct0, CT0 env var, or login to x.com in Chrome');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -53,12 +53,10 @@ describe('SweetisticsClient', () => {
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
success: true,
|
||||
tweet: {
|
||||
id: '1',
|
||||
text: 'hi',
|
||||
author: { username: 'u', name: 'User' },
|
||||
},
|
||||
id: '1',
|
||||
text: 'hi',
|
||||
author: { username: 'u', name: 'User' },
|
||||
metrics: { likeCount: 5 },
|
||||
}),
|
||||
});
|
||||
globalThis.fetch = fetchMock as unknown as typeof fetch;
|
||||
@@ -69,17 +67,35 @@ describe('SweetisticsClient', () => {
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.tweet?.id).toBe('1');
|
||||
const [url, init] = fetchMock.mock.calls[0];
|
||||
expect(url).toBe('https://api.example.com/api/twitter/tweet/1');
|
||||
expect(url).toBe('https://api.example.com/api/tweets/1');
|
||||
expect((init as RequestInit).headers).toMatchObject({ authorization: 'Bearer sweet-test' });
|
||||
});
|
||||
|
||||
it('fetches replies', async () => {
|
||||
const conversationPayload = {
|
||||
tweetIds: ['1', '2'],
|
||||
tweets: {
|
||||
1: {
|
||||
id: '1',
|
||||
text: 'root',
|
||||
author: { username: 'root', name: 'Root' },
|
||||
conversationId: 'c1',
|
||||
},
|
||||
2: {
|
||||
id: '2',
|
||||
text: 'reply',
|
||||
author: { username: 'reply', name: 'Reply' },
|
||||
conversationId: 'c1',
|
||||
inReplyToStatusId: '1',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const fetchMock = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
success: true,
|
||||
tweets: [{ id: '2', text: 'reply', author: { username: 'r', name: 'Reply' } }],
|
||||
result: { data: conversationPayload },
|
||||
}),
|
||||
});
|
||||
globalThis.fetch = fetchMock as unknown as typeof fetch;
|
||||
@@ -90,17 +106,24 @@ describe('SweetisticsClient', () => {
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.tweets?.[0].id).toBe('2');
|
||||
const [url, init] = fetchMock.mock.calls[0];
|
||||
expect(url).toBe('https://api.example.com/api/twitter/tweet/1/replies');
|
||||
expect(url).toContain('/api/trpc/tweets.getConversation');
|
||||
expect((init as RequestInit).headers).toMatchObject({ authorization: 'Bearer sweet-test' });
|
||||
});
|
||||
|
||||
it('fetches thread', async () => {
|
||||
const conversationPayload = {
|
||||
tweetIds: ['1', '2'],
|
||||
tweets: {
|
||||
1: { id: '1', text: 'root', author: { username: 'root', name: 'Root' }, conversationId: 'c1' },
|
||||
2: { id: '2', text: 'reply', author: { username: 'reply', name: 'Reply' }, conversationId: 'c1' },
|
||||
},
|
||||
};
|
||||
|
||||
const fetchMock = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
success: true,
|
||||
tweets: [{ id: '1', text: 'root', author: { username: 'u', name: 'User' } }],
|
||||
result: { data: conversationPayload },
|
||||
}),
|
||||
});
|
||||
globalThis.fetch = fetchMock as unknown as typeof fetch;
|
||||
@@ -109,20 +132,32 @@ describe('SweetisticsClient', () => {
|
||||
const result = await client.thread('1');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.tweets?.length).toBe(1);
|
||||
expect(result.tweets?.length).toBe(2);
|
||||
const [url, init] = fetchMock.mock.calls[0];
|
||||
expect(url).toBe('https://api.example.com/api/twitter/tweet/1/thread');
|
||||
expect(url).toContain('/api/trpc/tweets.getConversation');
|
||||
expect((init as RequestInit).headers).toMatchObject({ authorization: 'Bearer sweet-test' });
|
||||
});
|
||||
|
||||
it('searches tweets', async () => {
|
||||
const searchPayload = {
|
||||
tweets: {
|
||||
items: [
|
||||
{
|
||||
id: '3',
|
||||
text: 'needle',
|
||||
authorUsername: 'n',
|
||||
authorName: 'Needle',
|
||||
createdAt: '2024-01-01',
|
||||
metrics: { replyCount: 1, retweetCount: 2, likeCount: 3 },
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const fetchMock = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
success: true,
|
||||
tweets: [{ id: '3', text: 'needle', author: { username: 'n', name: 'Needle' } }],
|
||||
}),
|
||||
json: async () => [{ result: { data: searchPayload } }],
|
||||
});
|
||||
globalThis.fetch = fetchMock as unknown as typeof fetch;
|
||||
|
||||
@@ -131,22 +166,76 @@ describe('SweetisticsClient', () => {
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.tweets?.[0].id).toBe('3');
|
||||
const [url] = fetchMock.mock.calls[0];
|
||||
expect(url).toBe('https://api.example.com/api/twitter/search?q=needle&count=5');
|
||||
const [, init] = fetchMock.mock.calls[0];
|
||||
const [url, init] = fetchMock.mock.calls[0];
|
||||
expect(url).toContain('/api/trpc/search.execute');
|
||||
expect((init as RequestInit).headers).toMatchObject({ authorization: 'Bearer sweet-test' });
|
||||
});
|
||||
|
||||
it('propagates Sweetistics error message', async () => {
|
||||
it('fetches current user', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: async () => ({ success: false, error: 'boom' }),
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => [{ result: { data: { json: { id: 'u1', username: 'tester', name: 'Test User' } } } }],
|
||||
});
|
||||
globalThis.fetch = fetchMock as unknown as typeof fetch;
|
||||
|
||||
const client = new SweetisticsClient({ baseUrl: 'https://api.example.com', apiKey: 'sweet-test' });
|
||||
const result = await client.read('1');
|
||||
const result = await client.getCurrentUser();
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.user?.id).toBe('u1');
|
||||
expect(result.user?.username).toBe('tester');
|
||||
const [url, init] = fetchMock.mock.calls[0];
|
||||
expect(url).toBe('https://api.example.com/api/trpc/user.getCurrent?batch=1');
|
||||
expect((init as RequestInit).headers).toMatchObject({ authorization: 'Bearer sweet-test' });
|
||||
});
|
||||
|
||||
it('falls back to GET when POST is not allowed', async () => {
|
||||
const fetchMock = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ ok: false, status: 405, json: async () => ({}) })
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({ result: { data: { json: { id: 'u2', username: 'fallback' } } } }),
|
||||
});
|
||||
|
||||
globalThis.fetch = fetchMock as unknown as typeof fetch;
|
||||
|
||||
const client = new SweetisticsClient({ baseUrl: 'https://api.example.com', apiKey: 'sweet-test' });
|
||||
const result = await client.getCurrentUser();
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.user?.id).toBe('u2');
|
||||
expect(fetchMock).toHaveBeenCalledTimes(2);
|
||||
expect(fetchMock.mock.calls[1][0]).toContain('input=null');
|
||||
});
|
||||
|
||||
it('propagates Sweetistics user errors', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => [{ result: { error: { message: 'bad key' } } }],
|
||||
});
|
||||
globalThis.fetch = fetchMock as unknown as typeof fetch;
|
||||
|
||||
const client = new SweetisticsClient({ baseUrl: 'https://api.example.com', apiKey: 'sweet-test' });
|
||||
const result = await client.getCurrentUser();
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('bad key');
|
||||
});
|
||||
|
||||
it('propagates Sweetistics error message', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
status: 500,
|
||||
json: async () => [{ error: { message: 'boom' } }],
|
||||
});
|
||||
globalThis.fetch = fetchMock as unknown as typeof fetch;
|
||||
|
||||
const client = new SweetisticsClient({ baseUrl: 'https://api.example.com', apiKey: 'sweet-test' });
|
||||
const result = await client.search('needle', 1);
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('boom');
|
||||
|
||||
@@ -296,4 +296,82 @@ describe('TwitterClient', () => {
|
||||
expect(result.error).toContain('HTTP 404');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCurrentUser', () => {
|
||||
let mockFetch: ReturnType<typeof vi.fn>;
|
||||
|
||||
beforeEach(() => {
|
||||
mockFetch = vi.fn();
|
||||
global.fetch = mockFetch as any;
|
||||
});
|
||||
|
||||
it('returns mapped user details when present', async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
user_id: '12345',
|
||||
screen_name: 'tester',
|
||||
name: 'Test User',
|
||||
}),
|
||||
});
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const result = await client.getCurrentUser();
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.user).toEqual({ id: '12345', username: 'tester', name: 'Test User' });
|
||||
expect(mockFetch).toHaveBeenCalledWith(expect.stringContaining('account/settings'), expect.any(Object));
|
||||
});
|
||||
|
||||
it('returns error when response lacks identifiers', async () => {
|
||||
mockFetch.mockImplementation(async () => ({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({ language: 'en' }),
|
||||
text: async () => '{"language":"en"}',
|
||||
}));
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const result = await client.getCurrentUser();
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('Could not');
|
||||
});
|
||||
|
||||
it('surfaces HTTP errors', async () => {
|
||||
mockFetch.mockImplementation(async () => ({
|
||||
ok: false,
|
||||
status: 401,
|
||||
text: async () => 'Unauthorized',
|
||||
}));
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const result = await client.getCurrentUser();
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('HTTP 401');
|
||||
});
|
||||
|
||||
it('uses HTML fallback when API endpoints 404', async () => {
|
||||
mockFetch
|
||||
.mockResolvedValueOnce({ ok: false, status: 404, text: async () => 'not found' })
|
||||
.mockResolvedValueOnce({ ok: false, status: 404, text: async () => 'not found' })
|
||||
.mockResolvedValueOnce({ ok: false, status: 404, text: async () => 'not found' })
|
||||
.mockResolvedValueOnce({ ok: false, status: 404, text: async () => 'not found' })
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
text: async () => '<html>"screen_name":"fallback","user_id":"999"</html>',
|
||||
});
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const result = await client.getCurrentUser();
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.user?.username).toBe('fallback');
|
||||
expect(result.user?.id).toBe('999');
|
||||
expect(mockFetch).toHaveBeenCalledTimes(5);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user