Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor Twitter client test mocks #2234

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/client-twitter/__tests__/base.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { ClientBase } from '../src/base';
import { IAgentRuntime } from '@elizaos/core';
import { ActionTimelineType, IAgentRuntime } from '@elizaos/core';
import { TwitterConfig } from '../src/environment';

describe('Twitter Client Base', () => {
Expand Down Expand Up @@ -35,17 +35,24 @@ describe('Twitter Client Base', () => {

mockConfig = {
TWITTER_USERNAME: 'testuser',
TWITTER_PASSWORD: 'hashedpassword',
TWITTER_EMAIL: '[email protected]',
TWITTER_DRY_RUN: true,
TWITTER_SEARCH_ENABLE: false,
TWITTER_SPACES_ENABLE: false,
TWITTER_TARGET_USERS: [],
TWITTER_MAX_TWEETS_PER_DAY: 10,
TWITTER_MAX_TWEET_LENGTH: 280,
MAX_TWEET_LENGTH: 280,
TWITTER_2FA_SECRET: '',
TWITTER_RETRY_LIMIT: 5,
TWITTER_POLL_INTERVAL: 120,
POST_INTERVAL_MIN: 5,
POST_INTERVAL_MAX: 10,
ACTION_INTERVAL: 5,
ENABLE_ACTION_PROCESSING: true,
POST_IMMEDIATELY: false
POST_IMMEDIATELY: false,
MAX_ACTIONS_PROCESSING: 1,
ACTION_TIMELINE_TYPE: ActionTimelineType.ForYou
};
});

Expand All @@ -59,7 +66,7 @@ describe('Twitter Client Base', () => {

it('should initialize with correct tweet length limit', () => {
const client = new ClientBase(mockRuntime, mockConfig);
expect(client.twitterConfig.TWITTER_MAX_TWEET_LENGTH).toBe(280);
expect(client.twitterConfig.MAX_TWEET_LENGTH).toBe(280);
});

it('should initialize with correct post intervals', () => {
Expand Down
23 changes: 10 additions & 13 deletions packages/client-twitter/__tests__/environment.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { describe, it, expect, vi } from 'vitest';
import { validateTwitterConfig } from '../src/environment';
import { IAgentRuntime } from '@elizaos/core';
import { TestAgentRuntime, asIAgentRuntime } from './test-utils';

describe('Twitter Environment Configuration', () => {
const mockRuntime: IAgentRuntime = {
const mockRuntime = {
env: {
TWITTER_USERNAME: 'testuser123',
TWITTER_DRY_RUN: 'true',
TWITTER_SEARCH_ENABLE: 'false',
TWITTER_SPACES_ENABLE: 'false',
TWITTER_TARGET_USERS: 'user1,user2,user3',
TWITTER_MAX_TWEETS_PER_DAY: '10',
TWITTER_MAX_TWEET_LENGTH: '280',
TWITTER_POST_INTERVAL_MIN: '90',
TWITTER_POST_INTERVAL_MAX: '180',
TWITTER_ACTION_INTERVAL: '5',
Expand All @@ -32,10 +32,10 @@ describe('Twitter Environment Configuration', () => {
getSetting: function (key: string) {
return this.env[key] || null;
}
} as unknown as IAgentRuntime;
} as TestAgentRuntime;

it('should validate correct configuration', async () => {
const config = await validateTwitterConfig(mockRuntime);
const config = await validateTwitterConfig(mockRuntime as IAgentRuntime);
expect(config).toBeDefined();
expect(config.TWITTER_USERNAME).toBe('testuser123');
expect(config.TWITTER_DRY_RUN).toBe(true);
Expand All @@ -52,7 +52,6 @@ describe('Twitter Environment Configuration', () => {

it('should validate wildcard username', async () => {
const wildcardRuntime = {
...mockRuntime,
env: {
...mockRuntime.env,
TWITTER_USERNAME: '*'
Expand All @@ -63,15 +62,14 @@ describe('Twitter Environment Configuration', () => {
getSetting: function(key: string) {
return this.env[key] || null;
}
} as IAgentRuntime;
} as TestAgentRuntime;

const config = await validateTwitterConfig(wildcardRuntime);
const config = await validateTwitterConfig(asIAgentRuntime(wildcardRuntime));
expect(config.TWITTER_USERNAME).toBe('*');
});

it('should validate username with numbers and underscores', async () => {
const validRuntime = {
...mockRuntime,
env: {
...mockRuntime.env,
TWITTER_USERNAME: 'test_user_123'
Expand All @@ -82,15 +80,14 @@ describe('Twitter Environment Configuration', () => {
getSetting: function(key: string) {
return this.env[key] || null;
}
} as IAgentRuntime;
} as TestAgentRuntime;

const config = await validateTwitterConfig(validRuntime);
const config = await validateTwitterConfig(validRuntime as IAgentRuntime);
expect(config.TWITTER_USERNAME).toBe('test_user_123');
});

it('should handle empty target users', async () => {
const runtimeWithoutTargets = {
...mockRuntime,
env: {
...mockRuntime.env,
TWITTER_TARGET_USERS: ''
Expand All @@ -101,9 +98,9 @@ describe('Twitter Environment Configuration', () => {
getSetting: function(key: string) {
return this.env[key] || null;
}
} as IAgentRuntime;
} as TestAgentRuntime;

const config = await validateTwitterConfig(runtimeWithoutTargets);
const config = await validateTwitterConfig(runtimeWithoutTargets as IAgentRuntime);
expect(config.TWITTER_TARGET_USERS).toHaveLength(0);
});

Expand Down
12 changes: 9 additions & 3 deletions packages/client-twitter/__tests__/post.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect, vi } from 'vitest';
import { TwitterPostClient } from '../src/post';
import { ClientBase } from '../src/base';
import { IAgentRuntime } from '@elizaos/core';
import { IAgentRuntime, ActionTimelineType } from '@elizaos/core';
import { TwitterConfig } from '../src/environment';

describe('Twitter Post Client', () => {
Expand Down Expand Up @@ -45,18 +45,24 @@ describe('Twitter Post Client', () => {

mockConfig = {
TWITTER_USERNAME: 'testuser',
TWITTER_PASSWORD: 'hashedpassword',
TWITTER_EMAIL: '[email protected]',
TWITTER_DRY_RUN: true,
TWITTER_SEARCH_ENABLE: false,
TWITTER_SPACES_ENABLE: false,
TWITTER_TARGET_USERS: [],
TWITTER_MAX_TWEETS_PER_DAY: 10,
TWITTER_MAX_TWEET_LENGTH: 280,
MAX_TWEET_LENGTH: 280,
TWITTER_2FA_SECRET: '',
TWITTER_RETRY_LIMIT: 5,
TWITTER_POLL_INTERVAL: 120,
POST_INTERVAL_MIN: 5,
POST_INTERVAL_MAX: 10,
ACTION_INTERVAL: 5,
ENABLE_ACTION_PROCESSING: true,
POST_IMMEDIATELY: false,
MAX_TWEET_LENGTH: 280
MAX_ACTIONS_PROCESSING: 1,
ACTION_TIMELINE_TYPE: ActionTimelineType.ForYou
};

baseClient = new ClientBase(mockRuntime, mockConfig);
Expand Down
19 changes: 19 additions & 0 deletions packages/client-twitter/__tests__/test-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IAgentRuntime } from '@elizaos/core';

/**
* Runtime implementation for testing purposes.
* Provides environment configuration access without requiring full IAgentRuntime implementation.
*/
export type TestAgentRuntime = Partial<IAgentRuntime> & {
/** Environment variables for testing */
env: Record<string, string>;
/** Get environment variable value */
getEnv: (key: string) => string | null;
/** Get setting value (mirrors env for testing) */
getSetting: (key: string) => string | null;
};

/**
* Helper function to cast TestAgentRuntime to IAgentRuntime
*/
export const asIAgentRuntime = (runtime: TestAgentRuntime): IAgentRuntime => runtime as IAgentRuntime;
9 changes: 8 additions & 1 deletion packages/client-twitter/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const twitterEnvSchema = z.object({
.optional()
.default(''),
*/
TWITTER_MAX_TWEETS_PER_DAY: z.number().int().min(1).default(10),
POST_INTERVAL_MIN: z.number().int(),
POST_INTERVAL_MAX: z.number().int(),
ENABLE_ACTION_PROCESSING: z.boolean(),
Expand All @@ -74,7 +75,7 @@ export const twitterEnvSchema = z.object({
MAX_ACTIONS_PROCESSING: z.number().int(),
ACTION_TIMELINE_TYPE: z
.nativeEnum(ActionTimelineType)
.default(ActionTimelineType.ForYou),
.default(ActionTimelineType.ForYou)
});

export type TwitterConfig = z.infer<typeof twitterEnvSchema>;
Expand Down Expand Up @@ -223,6 +224,12 @@ export async function validateTwitterConfig(
ACTION_TIMELINE_TYPE:
runtime.getSetting("ACTION_TIMELINE_TYPE") ||
process.env.ACTION_TIMELINE_TYPE,

TWITTER_MAX_TWEETS_PER_DAY: safeParseInt(
runtime.getSetting("TWITTER_MAX_TWEETS_PER_DAY") ||
process.env.TWITTER_MAX_TWEETS_PER_DAY,
10
),
};

return twitterEnvSchema.parse(twitterConfig);
Expand Down
Loading