Skip to content

Commit

Permalink
Merge pull request #26 from PotLock/fix/clean-up
Browse files Browse the repository at this point in the history
Clean up unnecessary
  • Loading branch information
elliotBraem authored Jan 21, 2025
2 parents e84c2d3 + 053cd9d commit 9eeaf0a
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 243 deletions.
35 changes: 16 additions & 19 deletions backend/src/__tests__/mocks/scraper.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ export class MockScraper extends Scraper {
async login(): Promise<void> {
this._isLoggedIn = true;
// Set default cookies on login
this.cookies = [{
key: "mock_cookie",
value: "fresh_login",
domain: ".twitter.com",
path: "/",
secure: true,
httpOnly: true,
sameSite: "Lax"
}];
this.cookies = [
{
key: "mock_cookie",
value: "fresh_login",
domain: ".twitter.com",
path: "/",
secure: true,
httpOnly: true,
sameSite: "Lax",
},
];
}

async logout(): Promise<void> {
Expand All @@ -42,17 +44,17 @@ export class MockScraper extends Scraper {

async setCookies(cookieStrings: string[]): Promise<void> {
// Parse cookie strings into cookie objects
this.cookies = cookieStrings.map(cookie => {
const [nameValue] = cookie.split(';');
const [name, value] = nameValue.split('=');
this.cookies = cookieStrings.map((cookie) => {
const [nameValue] = cookie.split(";");
const [name, value] = nameValue.split("=");
return {
key: name.trim(),
value: value.trim(),
domain: ".twitter.com",
path: "/",
secure: true,
httpOnly: true,
sameSite: "Lax"
sameSite: "Lax",
};
});
this._isLoggedIn = true;
Expand All @@ -62,11 +64,6 @@ export class MockScraper extends Scraper {
return this.cookies;
}

async clearCookies(): Promise<void> {
this.cookies = [];
this._isLoggedIn = false;
}

async getUserIdByScreenName(screenName: string): Promise<string> {
return `mock-user-id-${screenName}`;
}
Expand All @@ -78,7 +75,7 @@ export class MockScraper extends Scraper {
cursor?: string,
): Promise<{ tweets: Tweet[] }> {
// Get all valid tweets (those with IDs)
const validTweets = this.mockTweets.filter(t => t.id);
const validTweets = this.mockTweets.filter((t) => t.id);

// Sort by ID descending (newest first) to match Twitter search behavior
const sortedTweets = validTweets.sort((a, b) => {
Expand Down
8 changes: 3 additions & 5 deletions backend/src/__tests__/submission.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("SubmissionService", () => {
curator1_reply: "1881064853743579530",
curator2_reply: "1881064853743579531",
mod1_reply: "1881064853743579532",
mod2_reply: "1881064853743579533"
mod2_reply: "1881064853743579533",
};

const botAccount = { id: "test_bot_id", username: "test_bot" }; // bot
Expand All @@ -33,10 +33,8 @@ describe("SubmissionService", () => {
maxDailySubmissionsPerUser: 5,
defaultStatus: SubmissionStatus.PENDING,
blacklist: {
twitter: [
"blocked_id"
]
}
twitter: ["blocked_id"],
},
},
feeds: [
{
Expand Down
36 changes: 19 additions & 17 deletions backend/src/__tests__/twitter-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ describe("TwitterService", () => {
beforeEach(async () => {
// Reset all mocks
Object.values(drizzleMock).forEach((mockFn) => mockFn.mockReset());

// Create fresh instances
twitterService = new TwitterService(mockConfig);
mockScraper = new MockScraper();
(twitterService as any).client = mockScraper;

// Initialize service
await twitterService.initialize();
});
Expand All @@ -46,11 +46,11 @@ describe("TwitterService", () => {

// Verify login was successful
expect(await mockScraper.isLoggedIn()).toBe(true);

// Verify new cookies were cached
expect(drizzleMock.setTwitterCookies).toHaveBeenCalledWith(
mockConfig.username,
expect.any(Array)
expect.any(Array),
);
});

Expand All @@ -69,7 +69,7 @@ describe("TwitterService", () => {
path: "/",
secure: true,
httpOnly: true,
}
},
];
drizzleMock.getTwitterCookies.mockReturnValue(mockCookies);
drizzleMock.getTwitterCacheValue.mockReturnValue("last_tweet_123");
Expand All @@ -78,13 +78,15 @@ describe("TwitterService", () => {

// Verify login was successful using cached cookies
expect(await mockScraper.isLoggedIn()).toBe(true);

// Verify cookies were set from cache
expect(drizzleMock.getTwitterCookies).toHaveBeenCalledWith(mockConfig.username);
expect(drizzleMock.getTwitterCookies).toHaveBeenCalledWith(
mockConfig.username,
);
// Should save cookies after verifying they work
expect(drizzleMock.setTwitterCookies).toHaveBeenCalledWith(
mockConfig.username,
expect.any(Array)
expect.any(Array),
);
});
});
Expand Down Expand Up @@ -164,7 +166,7 @@ describe("TwitterService", () => {
}));

// Add all tweets to mock
tweets.forEach(tweet => mockScraper.addMockTweet(tweet));
tweets.forEach((tweet) => mockScraper.addMockTweet(tweet));

const result = await twitterService.fetchAllNewMentions();

Expand Down Expand Up @@ -236,15 +238,15 @@ describe("TwitterService", () => {
},
];

tweets.forEach(tweet => mockScraper.addMockTweet(tweet));
tweets.forEach((tweet) => mockScraper.addMockTweet(tweet));

const result = await twitterService.fetchAllNewMentions();

// Should only include tweets newer than ID "2" (oldest to newest)
expect(result).toHaveLength(2);
expect(result[0].id).toBe("3");
expect(result[1].id).toBe("4");

// Should update last checked ID to newest tweet from original batch
expect(twitterService.getLastCheckedTweetId()).toBe("4");
});
Expand All @@ -254,11 +256,11 @@ describe("TwitterService", () => {
twitterService = new TwitterService(mockConfig);
mockScraper = new MockScraper();
(twitterService as any).client = mockScraper;

// Initialize with null last checked ID
drizzleMock.getTwitterCacheValue.mockReturnValue(null);
await twitterService.initialize();

// Clear any tweets and fetch
mockScraper.clearMockTweets();
const result = await twitterService.fetchAllNewMentions();
Expand Down Expand Up @@ -340,7 +342,7 @@ describe("TwitterService", () => {
},
];

tweets.forEach(tweet => mockScraper.addMockTweet(tweet));
tweets.forEach((tweet) => mockScraper.addMockTweet(tweet));

const result = await twitterService.fetchAllNewMentions();

Expand Down Expand Up @@ -420,19 +422,19 @@ describe("TwitterService", () => {
},
];

tweets.forEach(tweet => mockScraper.addMockTweet(tweet));
tweets.forEach((tweet) => mockScraper.addMockTweet(tweet));

// First set last checked to tweet 4
await twitterService.setLastCheckedTweetId("4");

// Initial fetch should only get tweet 5
let result = await twitterService.fetchAllNewMentions();
expect(result).toHaveLength(1);
expect(result[0].id).toBe("5");

// Now set last checked to tweet 2
await twitterService.setLastCheckedTweetId("2");

// Should now get tweets 3, 4, and 5 (oldest to newest)
result = await twitterService.fetchAllNewMentions();
expect(result).toHaveLength(3);
Expand Down
Loading

0 comments on commit 9eeaf0a

Please sign in to comment.