From eedf9b1644ba6ac45fc8d29b6d89097aa673a30b Mon Sep 17 00:00:00 2001 From: yuta Date: Wed, 24 Jun 2026 18:51:33 +0900 Subject: [PATCH] fix: adapt upstream commands to relay client --- src/commands/follow.ts | 32 +++--------- src/commands/users.ts | 66 ++----------------------- src/lib/twitter-client-follow.ts | 2 +- tests/commands.follow.test.ts | 6 +-- tests/commands.users.about.test.ts | 6 +-- tests/live/live-all.test.ts | 2 - tests/live/live.test.ts | 2 - tests/twitter-client.user-about.test.ts | 5 +- 8 files changed, 17 insertions(+), 104 deletions(-) diff --git a/src/commands/follow.ts b/src/commands/follow.ts index a5ae8b1..bdf86c2 100644 --- a/src/commands/follow.ts +++ b/src/commands/follow.ts @@ -42,19 +42,9 @@ export function registerFollowCommands(program: Command, ctx: CliContext): void .action(async (usernameOrId: string) => { const opts = program.opts(); const timeoutMs = ctx.resolveTimeoutFromOptions(opts); - - const { cookies, warnings } = await ctx.resolveCredentialsFromOptions(opts); - - for (const warning of warnings) { - console.error(`${ctx.p('warn')}${warning}`); - } - - if (!cookies.authToken || !cookies.ct0) { - console.error(`${ctx.p('err')}Missing required credentials`); - process.exit(1); - } - - const client = new TwitterClient({ cookies, timeoutMs }); + const relayBaseUrl = ctx.resolveRelayBaseUrlFromOptions(opts); + const profileName = ctx.resolveProfileNameFromOptions(opts); + const client = new TwitterClient({ relayBaseUrl, profileName, timeoutMs }); const resolved = await resolveUserId(client, usernameOrId, ctx); if (!resolved) { @@ -81,19 +71,9 @@ export function registerFollowCommands(program: Command, ctx: CliContext): void .action(async (usernameOrId: string) => { const opts = program.opts(); const timeoutMs = ctx.resolveTimeoutFromOptions(opts); - - const { cookies, warnings } = await ctx.resolveCredentialsFromOptions(opts); - - for (const warning of warnings) { - console.error(`${ctx.p('warn')}${warning}`); - } - - if (!cookies.authToken || !cookies.ct0) { - console.error(`${ctx.p('err')}Missing required credentials`); - process.exit(1); - } - - const client = new TwitterClient({ cookies, timeoutMs }); + const relayBaseUrl = ctx.resolveRelayBaseUrlFromOptions(opts); + const profileName = ctx.resolveProfileNameFromOptions(opts); + const client = new TwitterClient({ relayBaseUrl, profileName, timeoutMs }); const resolved = await resolveUserId(client, usernameOrId, ctx); if (!resolved) { diff --git a/src/commands/users.ts b/src/commands/users.ts index 0dad860..a86b1e6 100644 --- a/src/commands/users.ts +++ b/src/commands/users.ts @@ -317,18 +317,9 @@ export function registerUserCommands(program: Command, ctx: CliContext): void { process.exit(1); } - const { cookies, warnings } = await ctx.resolveCredentialsFromOptions(opts); - - for (const warning of warnings) { - console.error(`${ctx.p('warn')}${warning}`); - } - - if (!cookies.authToken || !cookies.ct0) { - console.error(`${ctx.p('err')}Missing required credentials`); - process.exit(1); - } - - const client = new TwitterClient({ cookies, timeoutMs }); + const relayBaseUrl = ctx.resolveRelayBaseUrlFromOptions(opts); + const profileName = ctx.resolveProfileNameFromOptions(opts); + const client = new TwitterClient({ relayBaseUrl, profileName, timeoutMs }); const result = await client.getUserAboutAccount(normalizedHandle); if (result.success && result.aboutProfile) { @@ -344,55 +335,4 @@ export function registerUserCommands(program: Command, ctx: CliContext): void { process.exit(1); } }); - - program - .command('about') - .description('Get account origin and location information for a user') - .argument('', 'Twitter username (with or without @)') - .option('--json', 'Output as JSON') - .action(async (username: string, cmdOpts: { json?: boolean }) => { - const opts = program.opts(); - const timeoutMs = ctx.resolveTimeoutFromOptions(opts); - - const { cookies, warnings } = await ctx.resolveCredentialsFromOptions(opts); - - for (const warning of warnings) { - console.error(`${ctx.p('warn')}${warning}`); - } - - if (!cookies.authToken || !cookies.ct0) { - console.error(`${ctx.p('err')}Missing required credentials`); - process.exit(1); - } - - const client = new TwitterClient({ cookies, timeoutMs }); - const result = await client.getUserAboutAccount(username); - - if (result.success && result.aboutProfile) { - if (cmdOpts.json) { - console.log(JSON.stringify(result.aboutProfile, null, 2)); - } else { - const profile = result.aboutProfile; - console.log(`${ctx.p('info')}Account information for @${username}:`); - if (profile.accountBasedIn) { - console.log(` Account based in: ${profile.accountBasedIn}`); - } - if (profile.createdCountryAccurate !== undefined) { - console.log(` Creation country accurate: ${profile.createdCountryAccurate ? 'Yes' : 'No'}`); - } - if (profile.locationAccurate !== undefined) { - console.log(` Location accurate: ${profile.locationAccurate ? 'Yes' : 'No'}`); - } - if (profile.source) { - console.log(`${ctx.l('source')}${profile.source}`); - } - if (profile.learnMoreUrl) { - console.log(` Learn more: ${profile.learnMoreUrl}`); - } - } - } else { - console.error(`${ctx.p('err')}Failed to fetch account information: ${result.error ?? 'Unknown error'}`); - process.exit(1); - } - }); } diff --git a/src/lib/twitter-client-follow.ts b/src/lib/twitter-client-follow.ts index 3f1ad89..b5e1251 100644 --- a/src/lib/twitter-client-follow.ts +++ b/src/lib/twitter-client-follow.ts @@ -66,7 +66,7 @@ export function withFollow> const response = await this.fetchWithTimeout(url, { method: 'POST', headers: { - ...this.getBaseHeaders(), + ...this.getHeaders(), 'content-type': 'application/x-www-form-urlencoded', }, body: params.toString(), diff --git a/tests/commands.follow.test.ts b/tests/commands.follow.test.ts index c19a505..827f09f 100644 --- a/tests/commands.follow.test.ts +++ b/tests/commands.follow.test.ts @@ -6,10 +6,8 @@ import { TwitterClient } from '../src/lib/twitter-client.js'; const baseCtx = { resolveTimeoutFromOptions: () => undefined, - resolveCredentialsFromOptions: async () => ({ - cookies: { authToken: 'auth', ct0: 'ct0', cookieHeader: 'auth=auth; ct0=ct0' }, - warnings: [], - }), + resolveRelayBaseUrlFromOptions: () => 'http://relay.test', + resolveProfileNameFromOptions: () => undefined, p: () => '', } as unknown as CliContext; diff --git a/tests/commands.users.about.test.ts b/tests/commands.users.about.test.ts index 51b40ac..8f4b0af 100644 --- a/tests/commands.users.about.test.ts +++ b/tests/commands.users.about.test.ts @@ -6,10 +6,8 @@ import { TwitterClient } from '../src/lib/twitter-client.js'; const baseCtx = { resolveTimeoutFromOptions: () => undefined, - resolveCredentialsFromOptions: async () => ({ - cookies: { authToken: 'auth', ct0: 'ct0', cookieHeader: 'auth=auth; ct0=ct0' }, - warnings: [], - }), + resolveRelayBaseUrlFromOptions: () => 'http://relay.test', + resolveProfileNameFromOptions: () => undefined, p: () => '', l: () => '', printTweets: () => undefined, diff --git a/tests/live/live-all.test.ts b/tests/live/live-all.test.ts index 8dfe9f2..242732a 100644 --- a/tests/live/live-all.test.ts +++ b/tests/live/live-all.test.ts @@ -2,8 +2,6 @@ import { spawn } from 'node:child_process'; import { existsSync } from 'node:fs'; import path from 'node:path'; import { beforeAll, describe, expect, it } from 'vitest'; -import { resolveCredentials } from '../../src/lib/cookies.js'; -import { TwitterClient } from '../../src/lib/twitter-client.js'; type RunResult = { exitCode: number; stdout: string; stderr: string; signal: NodeJS.Signals | null }; diff --git a/tests/live/live.test.ts b/tests/live/live.test.ts index b0c76d8..a755959 100644 --- a/tests/live/live.test.ts +++ b/tests/live/live.test.ts @@ -2,8 +2,6 @@ import { spawn } from 'node:child_process'; import { existsSync } from 'node:fs'; import path from 'node:path'; import { beforeAll, describe, expect, it } from 'vitest'; -import { resolveCredentials } from '../../src/lib/cookies.js'; -import { TwitterClient } from '../../src/lib/twitter-client.js'; type RunResult = { exitCode: number; stdout: string; stderr: string; signal: NodeJS.Signals | null }; diff --git a/tests/twitter-client.user-about.test.ts b/tests/twitter-client.user-about.test.ts index 33803a3..73d7e68 100644 --- a/tests/twitter-client.user-about.test.ts +++ b/tests/twitter-client.user-about.test.ts @@ -202,18 +202,19 @@ describe('TwitterClient getUserAboutAccount', () => { }), }); - const client = new TwitterClient({ cookies: validCookies }); + const client = new TwitterClient({ relayBaseUrl: 'http://relay.test', profileName: 'test-profile' }); await client.getUserAboutAccount('testuser'); expect(mockFetch).toHaveBeenCalledTimes(1); const [url, options] = mockFetch.mock.calls[0] as [string, RequestInit]; + expect(url).toContain('http://relay.test/i/api/graphql/'); expect(url).toContain('/AboutAccountQuery?'); expect(url).toContain('variables='); expect(url).toContain('testuser'); expect(options.method).toBe('GET'); expect(options.headers).toMatchObject({ - 'x-csrf-token': 'test_ct0_token', + 'x-profile-name': 'test-profile', }); }); });