@@ -1,6 +1,6 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { TwitterClient } from '../src/lib/twitter-client.js';
|
||||
import { validCookies } from './twitter-client-fixtures.js';
|
||||
import { testRelayBaseUrl } from './twitter-client-fixtures.js';
|
||||
|
||||
const originalFetch = global.fetch;
|
||||
|
||||
@@ -28,7 +28,7 @@ describe('TwitterClient getCurrentUser', () => {
|
||||
}),
|
||||
});
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const client = new TwitterClient({ relayBaseUrl: testRelayBaseUrl });
|
||||
const result = await client.getCurrentUser();
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
@@ -44,7 +44,7 @@ describe('TwitterClient getCurrentUser', () => {
|
||||
text: async () => '{"language":"en"}',
|
||||
}));
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const client = new TwitterClient({ relayBaseUrl: testRelayBaseUrl });
|
||||
const result = await client.getCurrentUser();
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
@@ -58,32 +58,26 @@ describe('TwitterClient getCurrentUser', () => {
|
||||
text: async () => 'Unauthorized',
|
||||
}));
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const client = new TwitterClient({ relayBaseUrl: testRelayBaseUrl });
|
||||
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 () => {
|
||||
it('returns error 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>',
|
||||
});
|
||||
.mockResolvedValueOnce({ ok: false, status: 404, text: async () => 'not found' });
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const client = new TwitterClient({ relayBaseUrl: testRelayBaseUrl });
|
||||
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);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('HTTP 404');
|
||||
expect(mockFetch).toHaveBeenCalledTimes(4);
|
||||
});
|
||||
|
||||
it('skips an endpoint when JSON parsing fails', async () => {
|
||||
@@ -105,7 +99,7 @@ describe('TwitterClient getCurrentUser', () => {
|
||||
}),
|
||||
});
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const client = new TwitterClient({ relayBaseUrl: testRelayBaseUrl });
|
||||
const result = await client.getCurrentUser();
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
@@ -113,24 +107,26 @@ describe('TwitterClient getCurrentUser', () => {
|
||||
expect(mockFetch).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('continues on fetch errors and still succeeds via HTML fallback', async () => {
|
||||
it('continues on fetch errors and still succeeds via later API endpoint', async () => {
|
||||
mockFetch
|
||||
.mockRejectedValueOnce(new Error('boom'))
|
||||
.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' })
|
||||
.mockRejectedValueOnce(new Error('settings boom'))
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
text: async () => '<html>"screen_name":"fallback","user_id":"999"</html>',
|
||||
json: async () => ({
|
||||
user_id: '999',
|
||||
screen_name: 'fallback',
|
||||
name: 'Fallback User',
|
||||
}),
|
||||
});
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const client = new TwitterClient({ relayBaseUrl: testRelayBaseUrl });
|
||||
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(3);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user