Files
bird/tests/twitter-client.lists.management.test.ts
T
yuta 45657a171e
CI / test (push) Successful in 10m5s
Publish npm package / publish (push) Successful in 9m53s
fix lists queries and authenticated list reading
2026-07-26 12:34:53 +09:00

172 lines
5.3 KiB
TypeScript

// ABOUTME: Tests for the authenticated user's Lists management timeline.
// ABOUTME: Covers nested list items, deduplication, and partial GraphQL errors.
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { TwitterClient } from '../src/lib/twitter-client.js';
import { type TwitterClientPrivate, testRelayBaseUrl } from './twitter-client-fixtures.js';
const originalFetch = global.fetch;
afterEach(() => {
global.fetch = originalFetch;
vi.restoreAllMocks();
});
describe('TwitterClient lists management', () => {
let mockFetch: ReturnType<typeof vi.fn>;
beforeEach(() => {
mockFetch = vi.fn();
global.fetch = mockFetch as unknown as typeof fetch;
});
it('gets and deduplicates the lists shown on the authenticated Lists page', async () => {
const list = {
id_str: '1234567890',
name: 'My List',
description: 'A useful list',
member_count: 10,
mode: 'Private',
created_at: 1_704_067_200_000,
user_results: {
result: {
rest_id: '12345',
core: { screen_name: 'testuser', name: 'Test User' },
},
},
};
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({
data: {
viewer: {
list_management_timeline: {
timeline: {
instructions: [
{
entries: [
{
content: {
clientEventInfo: { component: 'suggest_list_to_follow' },
items: [
{
item: {
itemContent: {
list: { id_str: '999', name: 'Suggested List' },
},
},
},
],
},
},
{
content: {
clientEventInfo: { component: 'suggest_owned_subscribed_list' },
items: [{ item: { itemContent: { list } } }, { item: { itemContent: { list } } }],
},
},
],
},
],
},
},
},
},
errors: [{ message: 'optional banner field failed' }],
}),
});
const client = new TwitterClient({ relayBaseUrl: testRelayBaseUrl });
const clientPrivate = client as unknown as TwitterClientPrivate;
clientPrivate.getListsManagementQueryIds = async () => ['test'];
const result = await client.getLists(100);
expect(result).toEqual({
success: true,
lists: [
{
id: '1234567890',
name: 'My List',
description: 'A useful list',
memberCount: 10,
subscriberCount: undefined,
isPrivate: true,
createdAt: '2024-01-01T00:00:00.000Z',
owner: {
id: '12345',
username: 'testuser',
name: 'Test User',
},
},
],
});
const [url] = mockFetch.mock.calls[0];
const requestUrl = new URL(url as string);
expect(requestUrl.pathname).toContain('/test/ListsManagementPageTimeline');
expect(JSON.parse(requestUrl.searchParams.get('variables') as string)).toEqual({ count: 100 });
});
it('returns an error when the management timeline is missing', async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ errors: [{ message: 'Not authorized' }] }),
});
const client = new TwitterClient({ relayBaseUrl: testRelayBaseUrl });
const clientPrivate = client as unknown as TwitterClientPrivate;
clientPrivate.getListsManagementQueryIds = async () => ['test'];
await expect(client.getLists()).resolves.toEqual({
success: false,
error: 'Not authorized',
});
});
it('ignores an invalid numeric creation date', async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({
data: {
viewer: {
list_management_timeline: {
timeline: {
instructions: [
{
entries: [
{
content: {
clientEventInfo: { component: 'suggest_owned_subscribed_list' },
items: [
{
item: {
itemContent: {
list: { id_str: '123', name: 'List', created_at: Number.NaN },
},
},
},
],
},
},
],
},
],
},
},
},
},
}),
});
const client = new TwitterClient({ relayBaseUrl: testRelayBaseUrl });
const clientPrivate = client as unknown as TwitterClientPrivate;
clientPrivate.getListsManagementQueryIds = async () => ['test'];
const result = await client.getLists();
expect(result.success && result.lists?.[0].createdAt).toBeUndefined();
});
});