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

Test/score #815

Merged
merged 7 commits into from
Jul 9, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
Expand Down Expand Up @@ -62,4 +63,4 @@ apps/cli/lib
package-lock.json

test.png
.vscode
apps/web/public/sw.js.map
2 changes: 2 additions & 0 deletions apps/web/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const customJestConfig = {
displayName: pkg.name,
setupFilesAfterEnv: ['./jest.setup.js'],
testEnvironment: 'jsdom',
roots: ['<rootDir>/src/'],
moduleDirectories: ['node_modules', 'src'],
transformIgnorePatterns: ['node_modules/(?!use-debounce|uuid)'],
};

Expand Down
31 changes: 31 additions & 0 deletions apps/web/src/modules/score/calculateUserScore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { UserScore, Tweet } from 'types/Score';
import { getEmptyScore } from './getEmptyScore';

export function calculateUserScore(tweets: Tweet[]): UserScore {
const { length: tweet_count } = tweets;

return tweets.reduce(
(
accumulator: UserScore,
{
public_metrics: { retweet_count, reply_count, like_count, quote_count },
}: Tweet,
): UserScore => {
const retweets = retweet_count + accumulator.retweet_count;
const replies = reply_count + accumulator.reply_count;
const likes = like_count + accumulator.like_count;
const quotes = quote_count + accumulator.quote_count;
const total = tweet_count + retweets + replies + likes + quotes;

return {
tweet_count,
retweet_count: retweets,
reply_count: replies,
like_count: likes,
quote_count: quotes,
total,
};
},
getEmptyScore(),
);
}
21 changes: 21 additions & 0 deletions apps/web/src/modules/score/getElegibleTweets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getElegibleTweets } from './getElegibleTweets';
import { userTweets } from 'modules/twitter/twitterFollowersGet';

jest.mock('modules/twitter/twitterUserGet');
jest.mock('modules/twitter/twitterFollowersGet');

describe('GetElegibleTweets', () => {
test('Should return only one elegible tweet', async () => {
const { data: tweets } = await userTweets(
'1070750548608147456',
'accessToken',
Fixed Show fixed Hide fixed
).then((data) => data);
const result = getElegibleTweets({
tweets,
twitter_profile_id: '2313873457',
twitter_profile_user: 'sseraphini',
});

expect(result.length).toBe(1);
});
});
54 changes: 54 additions & 0 deletions apps/web/src/modules/score/getElegibleTweets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Tweet } from 'types/Score';

interface Props {
tweets: Tweet[];
twitter_profile_id: string;
twitter_profile_user: string;
}

interface filterProps {
[index: string]: string;
}

const filterElegibleTweets = ({
text,
in_reply_to_user_id,
twitter_profile_id,
twitter_profile_user,
}: filterProps): boolean => {
const isRT = !!text.match(/^RT\s@/g);

if (isRT) {
return false;
}

const isReplyingProfileId = in_reply_to_user_id === twitter_profile_id;

if (isReplyingProfileId) {
return true;
}

const isProfileUserMentioned = !!text.match(
new RegExp(`(cc(:?\\s*))?@${twitter_profile_user}`, 'gi'),
);

if (isProfileUserMentioned) {
return true;
}

return false;
};

export const getElegibleTweets = ({
tweets,
twitter_profile_id,
twitter_profile_user,
}: Props): Tweet[] =>
tweets.filter(({ text, in_reply_to_user_id }) =>
filterElegibleTweets({
text,
in_reply_to_user_id,
twitter_profile_id,
twitter_profile_user,
}),
);
12 changes: 12 additions & 0 deletions apps/web/src/modules/score/getEmptyScore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { UserScore } from 'types/Score';

export function getEmptyScore(): UserScore {
return {
tweet_count: 0,
retweet_count: 0,
reply_count: 0,
like_count: 0,
quote_count: 0,
total: 0,
};
}
85 changes: 85 additions & 0 deletions apps/web/src/modules/score/getUserScore.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { getUserScore } from './getUserScore';
import { config } from 'config';
import { getEmptyScore } from './getEmptyScore';

jest.mock('modules/twitter/twitterUserGet');
jest.mock('modules/twitter/twitterFollowersGet');

describe('GetUserScore', () => {
const filledVariable = {
value: '2313873457',
configurable: true,
writable: true,
};

const emptyVariable = {
value: '',
configurable: true,
writable: true,
};

test('Should return http status 401 when TWITTER_BEARER_TOKEN is not configured', async () => {
const result = await getUserScore('pjonatansr').catch((error) => error);

expect(result.statusCode).toEqual(401);
});

test('Should return http status 500 when TWITTER_PROFILE_ID is not configured', async () => {
Object.defineProperties(config, {
TWITTER_BEARER_TOKEN: filledVariable,
TWITTER_PROFILE_ID: emptyVariable,
TWITTER_PROFILE_USER: filledVariable,
});

const result = await getUserScore('pjonatansr').catch((error) => error);

expect(result.message).toEqual('Twitter profile id is not set');
expect(result.statusCode).toEqual(500);
});

test('Should return http status 500 when TWITTER_PROFILE_USER is not configured', async () => {
Object.defineProperties(config, {
TWITTER_BEARER_TOKEN: filledVariable,
TWITTER_PROFILE_ID: filledVariable,
TWITTER_PROFILE_USER: emptyVariable,
});

const result = await getUserScore('pjonatansr').catch((error) => error);

expect(result.message).toEqual('Twitter profile user is not set');
expect(result.statusCode).toEqual(500);
});

test('Should return empty score when user have tweets', async () => {
Object.defineProperties(config, {
TWITTER_BEARER_TOKEN: filledVariable,
TWITTER_PROFILE_ID: filledVariable,
TWITTER_PROFILE_USER: filledVariable,
});

const { userScore } = await getUserScore('pjonatansr').then((data) => data);

expect(userScore).toEqual({
retweet_count: 0,
reply_count: 0,
like_count: 1,
quote_count: 0,
tweet_count: 1,
total: 2,
});
});

test('Should return empty score when user not exists', async () => {
Object.defineProperties(config, {
TWITTER_BEARER_TOKEN: filledVariable,
TWITTER_PROFILE_ID: filledVariable,
TWITTER_PROFILE_USER: filledVariable,
});

const { userScore } = await getUserScore('pjon#at@ansr').catch(
(error) => error,
);

expect(userScore).toEqual(getEmptyScore());
});
});
69 changes: 16 additions & 53 deletions apps/web/src/modules/score/getUserScore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ import { User } from 'types/User';
import { config } from 'config';
import { userTweets } from 'modules/twitter/twitterFollowersGet';
import { userProfile } from 'modules/twitter/twitterUserGet';
import { UserScore, Tweet } from 'types/Score';
import { UserScore } from 'types/Score';
import { getElegibleTweets } from './getElegibleTweets';
import { getEmptyScore } from './getEmptyScore';
import { calculateUserScore } from './calculateUserScore';

interface Result {
user: User;
userScore: UserScore;
}

export async function getUserScore(username: string): Promise<Result> {
const access_token = config.TWITTER_BEARER_TOKEN;
const twitter_profile_id = config.TWITTER_PROFILE_ID;
const twitter_profile_user = config.TWITTER_PROFILE_USER;
const {
TWITTER_BEARER_TOKEN: access_token,
TWITTER_PROFILE_ID: twitter_profile_id,
TWITTER_PROFILE_USER: twitter_profile_user,
} = config;

if (!access_token) {
return Promise.reject({
Expand Down Expand Up @@ -49,61 +54,19 @@ export async function getUserScore(username: string): Promise<Result> {
access_token as string,
);

const emptyScore: UserScore = {
tweet_count: 0,
retweet_count: 0,
reply_count: 0,
like_count: 0,
quote_count: 0,
total: 0,
};

if (!tweets) {
return Promise.resolve({
user,
userScore: emptyScore,
userScore: getEmptyScore(),
});
}

const elegibleTweets: Tweet[] = tweets.filter(
({ text, in_reply_to_user_id }): boolean => {
const isReplyingProfileId = in_reply_to_user_id === twitter_profile_id;

const isProfileUserMentioned = !!text.match(
new RegExp(`(cc(:?\\s*))?@${twitter_profile_user}`, 'gi'),
);

const isRT = !!text.match(/^RT\s@/g);

return (isProfileUserMentioned || isReplyingProfileId) && !isRT;
},
);

const tweetCount = elegibleTweets.length;
const userScore: UserScore = elegibleTweets.reduce(
(
accumulator: UserScore,
{
public_metrics: { retweet_count, reply_count, like_count, quote_count },
}: Tweet,
): UserScore => {
const retweets = retweet_count + accumulator.retweet_count;
const replies = reply_count + accumulator.reply_count;
const likes = like_count + accumulator.like_count;
const quotes = quote_count + accumulator.quote_count;
const total = tweetCount + retweets + replies + likes + quotes;

return {
tweet_count: tweetCount,
retweet_count: retweets,
reply_count: replies,
like_count: likes,
quote_count: quotes,
total,
};
},
emptyScore,
);
const elegibleTweets = getElegibleTweets({
tweets,
twitter_profile_id,
twitter_profile_user,
});
const userScore = calculateUserScore(elegibleTweets);

return {
user,
Expand Down
Loading