Skip to content

Commit

Permalink
working test route
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotBraem committed Feb 1, 2025
1 parent ff0ecf1 commit 790ab71
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
63 changes: 57 additions & 6 deletions backend/src/__tests__/mocks/twitter-service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ export class MockTwitterService extends TwitterService {
private mockTweets: Tweet[] = [];
private mockUserIds: Map<string, string> = new Map();
private tweetIdCounter: bigint = BigInt(Date.now());
private testLastCheckedTweetId: string | null = null;

constructor() {
// Pass config with the bot's username so mentions are found
super({
username: "curatedotfun",
username: "test_bot",
password: "mock_pass",
email: "[email protected]",
});
Expand All @@ -22,9 +23,9 @@ export class MockTwitterService extends TwitterService {
getCookies: async () => [],
setCookies: async () => {},
fetchSearchTweets: async (query: string, count: number, mode: SearchMode) => {
// Filter tweets that match the query (mentions @curatedotfun)
// Filter tweets that match the query (mentions @test_bot)
const matchingTweets = this.mockTweets.filter(tweet =>
tweet.text?.includes("@curatedotfun")
tweet.text?.includes("@test_bot")
);

// Sort by ID descending (newest first) to match Twitter search behavior
Expand All @@ -45,7 +46,7 @@ export class MockTwitterService extends TwitterService {
sendTweet: async (message: string, replyToId?: string) => {
const newTweet = this.addMockTweet({
text: message,
username: "curatedotfun",
username: "test_bot",
inReplyToStatusId: replyToId,
});
return {
Expand Down Expand Up @@ -110,9 +111,59 @@ export class MockTwitterService extends TwitterService {
async getUserIdByScreenName(screenName: string): Promise<string> {
return this.mockUserIds.get(screenName) || `mock-user-id-${screenName}`;
}

async fetchAllNewMentions(): Promise<Tweet[]> {
// Let the parent TwitterService handle the processing logic
return super.fetchAllNewMentions();
const BATCH_SIZE = 200;

// Get the last tweet ID we processed
const lastCheckedId = this.testLastCheckedTweetId
? BigInt(this.testLastCheckedTweetId)
: null;

// Get latest tweets first (up to batch size), excluding already checked tweets
const latestTweets = [...this.mockTweets]
.sort((a, b) => {
const aId = BigInt(a.id);
const bId = BigInt(b.id);
return bId > aId ? -1 : bId < aId ? 1 : 0; // Descending order (newest first)
})
.filter(tweet => {
const tweetId = BigInt(tweet.id);
return !lastCheckedId || tweetId > lastCheckedId;
})
.slice(0, BATCH_SIZE);

if (latestTweets.length === 0) {
logger.info("No tweets found");
return [];
}

// Filter for mentions
const newMentions = latestTweets.filter(tweet =>
tweet.text?.includes("@test_bot") ||
tweet.mentions?.some(m => m.username === "test_bot")
);

// Sort chronologically (oldest to newest) to match real service
newMentions.sort((a, b) => {
const aId = BigInt(a.id);
const bId = BigInt(b.id);
return aId > bId ? 1 : aId < bId ? -1 : 0;
});

// Update last checked ID if we found new tweets
if (latestTweets.length > 0) {
// Use the first tweet from latestTweets since it's the newest (they're in descending order)
const highestId = latestTweets[0].id;
await this.setLastCheckedTweetId(highestId);
}

return newMentions;
}

async setLastCheckedTweetId(tweetId: string) {
this.testLastCheckedTweetId = tweetId;
logger.info(`Last checked tweet ID updated to: ${tweetId}`);
}

async getTweet(tweetId: string): Promise<Tweet | null> {
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/routes/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function TestPage() {
const [username, setUsername] = useState("curator");

const prepareSubmit = () => {
setTweetText(`@curatedotfun !submit #${selectedFeed}`);
setTweetText(`@test_bot !submit #${selectedFeed}`);
setUsername("curator");
};

Expand All @@ -22,7 +22,7 @@ function TestPage() {
alert("Please submit content first");
return;
}
setTweetText(`!approve @curatedotfun #${selectedFeed}`);
setTweetText(`!approve @test_bot #${selectedFeed}`);
setUsername("moderator");
};

Expand All @@ -31,7 +31,7 @@ function TestPage() {
alert("Please submit content first");
return;
}
setTweetText(`!reject @curatedotfun #${selectedFeed} spam`);
setTweetText(`!reject @test_bot #${selectedFeed} spam`);
setUsername("moderator");
};

Expand Down

0 comments on commit 790ab71

Please sign in to comment.