test(lists): improve test coverage and extract list ID parsing
- Extract extractListId to src/lib/extract-list-id.ts with proper tests - Add json-full/includeRaw test for list timeline - Add 404 retry tests for getOwnedLists and getListMemberships - Add tests for skipping invalid list entries and missing owner handling - Use shared TwitterClientPrivate type from fixtures 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
This commit is contained in:
committed by
Peter Steinberger
co-authored by
Claude Opus 4.5
parent
7818440d65
commit
59e827e153
@@ -3,22 +3,10 @@
|
||||
|
||||
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 { type TwitterClientPrivate, validCookies } from './twitter-client-fixtures.js';
|
||||
|
||||
const originalFetch = global.fetch;
|
||||
|
||||
type TwitterClientPrivate = TwitterClient & {
|
||||
getCurrentUser: () => Promise<{
|
||||
success: boolean;
|
||||
user?: { id: string; username: string; name: string };
|
||||
error?: string;
|
||||
}>;
|
||||
getListOwnershipsQueryIds: () => Promise<string[]>;
|
||||
getListMembershipsQueryIds: () => Promise<string[]>;
|
||||
getListTimelineQueryIds: () => Promise<string[]>;
|
||||
refreshQueryIds: () => Promise<void>;
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
global.fetch = originalFetch;
|
||||
vi.restoreAllMocks();
|
||||
@@ -239,6 +227,193 @@ describe('TwitterClient lists', () => {
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('Rate limit exceeded');
|
||||
});
|
||||
|
||||
it('retries on 404 error after refreshing query IDs', async () => {
|
||||
mockFetch
|
||||
.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
status: 404,
|
||||
text: async () => 'Not Found',
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
data: {
|
||||
user: {
|
||||
result: {
|
||||
timeline: {
|
||||
timeline: {
|
||||
instructions: [
|
||||
{
|
||||
entries: [
|
||||
{
|
||||
content: {
|
||||
itemContent: {
|
||||
list: {
|
||||
id_str: '333',
|
||||
name: 'Retry List',
|
||||
mode: 'Public',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const clientPrivate = client as unknown as TwitterClientPrivate;
|
||||
clientPrivate.getCurrentUser = async () => ({
|
||||
success: true,
|
||||
user: { id: '12345', username: 'testuser', name: 'Test User' },
|
||||
});
|
||||
clientPrivate.getListOwnershipsQueryIds = async () => ['test'];
|
||||
clientPrivate.refreshQueryIds = async () => {};
|
||||
|
||||
const result = await client.getOwnedLists(100);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.lists?.[0].id).toBe('333');
|
||||
expect(mockFetch).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('skips list entries with missing id_str or name', async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
data: {
|
||||
user: {
|
||||
result: {
|
||||
timeline: {
|
||||
timeline: {
|
||||
instructions: [
|
||||
{
|
||||
entries: [
|
||||
{
|
||||
content: {
|
||||
itemContent: {
|
||||
list: {
|
||||
id_str: '111',
|
||||
name: 'Valid List',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
content: {
|
||||
itemContent: {
|
||||
list: {
|
||||
// Missing id_str
|
||||
name: 'Invalid List 1',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
content: {
|
||||
itemContent: {
|
||||
list: {
|
||||
id_str: '222',
|
||||
// Missing name
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
content: {
|
||||
itemContent: {
|
||||
list: {
|
||||
id_str: '333',
|
||||
name: 'Another Valid List',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const clientPrivate = client as unknown as TwitterClientPrivate;
|
||||
clientPrivate.getCurrentUser = async () => ({
|
||||
success: true,
|
||||
user: { id: '12345', username: 'testuser', name: 'Test User' },
|
||||
});
|
||||
clientPrivate.getListOwnershipsQueryIds = async () => ['test'];
|
||||
|
||||
const result = await client.getOwnedLists(100);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.lists).toHaveLength(2);
|
||||
expect(result.lists?.[0].id).toBe('111');
|
||||
expect(result.lists?.[1].id).toBe('333');
|
||||
});
|
||||
|
||||
it('handles list with missing owner gracefully', async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
data: {
|
||||
user: {
|
||||
result: {
|
||||
timeline: {
|
||||
timeline: {
|
||||
instructions: [
|
||||
{
|
||||
entries: [
|
||||
{
|
||||
content: {
|
||||
itemContent: {
|
||||
list: {
|
||||
id_str: '444',
|
||||
name: 'List Without Owner',
|
||||
// No user_results
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const clientPrivate = client as unknown as TwitterClientPrivate;
|
||||
clientPrivate.getCurrentUser = async () => ({
|
||||
success: true,
|
||||
user: { id: '12345', username: 'testuser', name: 'Test User' },
|
||||
});
|
||||
clientPrivate.getListOwnershipsQueryIds = async () => ['test'];
|
||||
|
||||
const result = await client.getOwnedLists(100);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.lists).toHaveLength(1);
|
||||
expect(result.lists?.[0].owner).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getListMemberships', () => {
|
||||
@@ -298,6 +473,63 @@ describe('TwitterClient lists', () => {
|
||||
expect(result.lists?.[0].name).toBe('Member List');
|
||||
expect(result.lists?.[0].owner?.username).toBe('otheruser');
|
||||
});
|
||||
|
||||
it('retries on 404 error after refreshing query IDs', async () => {
|
||||
mockFetch
|
||||
.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
status: 404,
|
||||
text: async () => 'Not Found',
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
data: {
|
||||
user: {
|
||||
result: {
|
||||
timeline: {
|
||||
timeline: {
|
||||
instructions: [
|
||||
{
|
||||
entries: [
|
||||
{
|
||||
content: {
|
||||
itemContent: {
|
||||
list: {
|
||||
id_str: '6666',
|
||||
name: 'Retry Membership List',
|
||||
mode: 'Public',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const client = new TwitterClient({ cookies: validCookies });
|
||||
const clientPrivate = client as unknown as TwitterClientPrivate;
|
||||
clientPrivate.getCurrentUser = async () => ({
|
||||
success: true,
|
||||
user: { id: '12345', username: 'testuser', name: 'Test User' },
|
||||
});
|
||||
clientPrivate.getListMembershipsQueryIds = async () => ['test'];
|
||||
clientPrivate.refreshQueryIds = async () => {};
|
||||
|
||||
const result = await client.getListMemberships(100);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.lists?.[0].id).toBe('6666');
|
||||
expect(mockFetch).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getListTimeline', () => {
|
||||
|
||||
Reference in New Issue
Block a user