fix lists queries and authenticated list reading
CI / test (push) Successful in 10m5s
Publish npm package / publish (push) Successful in 9m53s

This commit is contained in:
2026-07-26 12:34:53 +09:00
parent b8400469ca
commit 45657a171e
20 changed files with 497 additions and 296 deletions
+35
View File
@@ -0,0 +1,35 @@
import { Command } from 'commander';
import { describe, expect, it, vi } from 'vitest';
import type { CliContext } from '../src/cli/shared.js';
import { registerListsCommand } from '../src/commands/lists.js';
import { TwitterClient } from '../src/lib/twitter-client.js';
describe('lists command', () => {
it('uses the authenticated Lists page by default', async () => {
const program = new Command();
const ctx = {
resolveTimeoutFromOptions: () => undefined,
resolveRelayBaseUrlFromOptions: () => 'https://relay.example',
resolveProfileNameFromOptions: () => undefined,
p: () => '',
colors: { muted: (value: string) => value, accent: (value: string) => value },
getOutput: () => 'plain',
} as unknown as CliContext;
const listsSpy = vi.spyOn(TwitterClient.prototype, 'getLists').mockResolvedValue({
success: true,
lists: [],
});
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => undefined);
try {
registerListsCommand(program, ctx);
await program.parseAsync(['node', 'bird', 'lists', '--count', '25']);
expect(listsSpy).toHaveBeenCalledWith(25);
expect(logSpy).toHaveBeenCalledWith('No lists found.');
} finally {
listsSpy.mockRestore();
logSpy.mockRestore();
}
});
});
+23 -12
View File
@@ -29,9 +29,6 @@ describe('runtime-features', () => {
likes: {
likes_only: true,
},
lists: {
responsive_web_text_conversations_enabled: true,
},
},
});
@@ -44,9 +41,6 @@ describe('runtime-features', () => {
const likes = buildLikesFeatures();
expect(likes.global_flag).toBe(true);
expect(likes.likes_only).toBe(true);
const lists = buildListsFeatures();
expect(lists.responsive_web_text_conversations_enabled).toBe(true);
});
it('refresh merges defaults with existing cache', async () => {
@@ -119,11 +113,28 @@ describe('runtime-features', () => {
it('includes required lists API feature flags', () => {
const features = buildListsFeatures();
expect(features.responsive_web_graphql_exclude_directive_enabled).toBe(true);
expect(features.blue_business_profile_image_shape_enabled).toBe(true);
expect(features.responsive_web_text_conversations_enabled).toBe(false);
expect(features.tweetypie_unmention_optimization_enabled).toBe(true);
expect(features.vibe_api_enabled).toBe(true);
expect(features.interactive_text_enabled).toBe(true);
expect(features.responsive_web_graphql_exclude_directive_enabled).toBeUndefined();
expect(features.responsive_web_grok_annotations_enabled).toBe(true);
expect(features.rweb_cashtags_enabled).toBe(true);
expect(features.responsive_web_grok_show_grok_translated_post).toBe(true);
expect(features.longform_notetweets_inline_media_enabled).toBe(false);
});
it('keeps catalog-locked list features independent from runtime overrides', () => {
process.env.BIRD_FEATURES_JSON = JSON.stringify({
global: {
responsive_web_grok_annotations_enabled: false,
},
sets: {
lists: {
rweb_cashtags_enabled: false,
},
},
});
clearFeatureOverridesCache();
const features = buildListsFeatures();
expect(features.responsive_web_grok_annotations_enabled).toBe(true);
expect(features.rweb_cashtags_enabled).toBe(true);
});
});
+1
View File
@@ -9,6 +9,7 @@ export type TwitterClientPrivate = TwitterClient & {
error?: string;
}>;
getLikesQueryIds: () => Promise<string[]>;
getListsManagementQueryIds: () => Promise<string[]>;
getListTimelineQueryIds: () => Promise<string[]>;
getListOwnershipsQueryIds: () => Promise<string[]>;
getListMembershipsQueryIds: () => Promise<string[]>;
@@ -0,0 +1,171 @@
// 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();
});
});
@@ -75,6 +75,9 @@ describe('TwitterClient lists memberships', () => {
expect(result.lists?.[0].id).toBe('5555');
expect(result.lists?.[0].name).toBe('Member List');
expect(result.lists?.[0].owner?.username).toBe('otheruser');
const [url] = mockFetch.mock.calls[0];
const variables = JSON.parse(new URL(url as string).searchParams.get('variables') as string);
expect(variables).toEqual({ userId: '12345', count: 100 });
});
it('retries on 404 error after refreshing query IDs', async () => {
+13 -7
View File
@@ -116,13 +116,19 @@ describe('TwitterClient lists owned', () => {
await client.getOwnedLists(1);
const [url] = mockFetch.mock.calls[0];
const parsedFeatures = JSON.parse(new URL(url as string).searchParams.get('features') as string);
expect(parsedFeatures.responsive_web_graphql_exclude_directive_enabled).toBe(true);
expect(parsedFeatures.blue_business_profile_image_shape_enabled).toBe(true);
expect(parsedFeatures.responsive_web_text_conversations_enabled).toBe(false);
expect(parsedFeatures.tweetypie_unmention_optimization_enabled).toBe(true);
expect(parsedFeatures.vibe_api_enabled).toBe(true);
expect(parsedFeatures.interactive_text_enabled).toBe(true);
const requestUrl = new URL(url as string);
const parsedVariables = JSON.parse(requestUrl.searchParams.get('variables') as string);
const parsedFeatures = JSON.parse(requestUrl.searchParams.get('features') as string);
expect(parsedVariables).toEqual({
userId: '12345',
isListMemberTargetUserId: '12345',
count: 1,
});
expect(parsedFeatures.responsive_web_graphql_exclude_directive_enabled).toBeUndefined();
expect(parsedFeatures.responsive_web_grok_annotations_enabled).toBe(true);
expect(parsedFeatures.rweb_cashtags_enabled).toBe(true);
expect(parsedFeatures.responsive_web_grok_show_grok_translated_post).toBe(true);
expect(parsedFeatures.longform_notetweets_inline_media_enabled).toBe(false);
});
it('handles private lists correctly', async () => {
@@ -66,6 +66,7 @@ describe('TwitterClient lists timeline', () => {
},
},
},
errors: [{ message: 'optional media field failed' }],
}),
});