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

chore: test coverage #145

Merged
merged 6 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 3 additions & 9 deletions meta/testing/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,8 @@ module.exports = {
setupFilesAfterEnv: ['<rootDir>/meta/testing/jest.setup.cjs'],
testRegex: '(/__tests__/.*\\.spec)\\.ts?$',
testTimeout: 10000,
transform: {
"^.+\\.ts?$": ["ts-jest", {
useESM: true,
}],
"^.+\\.js?$": ["ts-jest", {
useESM: true,
}]
},
preset: "ts-jest/presets/default-esm",
verbose: false,
maxWorkers: "50%"
maxWorkers: "50%",
transformIgnorePatterns: []
};
12 changes: 12 additions & 0 deletions src/__tests__/__mocks__/ora.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
name: "ora",
default: jest.fn(() => ({
start: jest
.fn()
.mockImplementation(() => ({ text: "", succeed: jest.fn() })),
succeed: jest.fn(),
fail: jest.fn(),
warn: jest.fn(),
})),
__esModule: true,
};
177 changes: 177 additions & 0 deletions src/services/__tests__/bluesky-sender.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import { BskyAgent } from "@atproto/api";
import ora from "ora";

import { makeBlobFromFile } from "../../helpers/medias/__tests__/helpers/make-blob-from-file.js";
import { Media } from "../../types/index.js";
import { BlueskyPost } from "../../types/post.js";
import { blueskySenderService } from "../bluesky-sender.service.js";
import { mediaDownloaderService } from "../media-downloader.service.js";
import { makeTweetMock } from "./helpers/make-tweet-mock.js";

jest.mock("ora");
jest.mock("../../constants.js", () => {
return {};
});
jest.mock("../../helpers/cache/save-post-to-cache.js", () => ({
savePostToCache: jest.fn().mockImplementation(() => Promise.resolve()),
}));
jest.mock("../../helpers/tweet/is-tweet-cached.js");

jest.mock("../media-downloader.service.js", () => ({
mediaDownloaderService: jest.fn(),
}));
const mediaDownloaderServiceMock = mediaDownloaderService as jest.Mock;
const client = new BskyAgent({
service: `https://bsky.social`,
});

const postSpy = jest
.fn()
.mockImplementation(() => Promise.resolve({ uri: "uri", cid: "cid" }));
client.post = postSpy;

const uploadBlobSpy = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
blob: {
original: {
$type: "blob",
ref: "blobRef",
mimeType: "image/png",
size: "1024",
},
},
},
}),
);
client.uploadBlob = uploadBlobSpy;

const log = ora();

const post = {
tweet: makeTweetMock({ text: "Tweet text" }),
chunks: ["Tweet text"],
username: "username",
quotePost: undefined,
replyPost: undefined,
} as BlueskyPost;

const media: Media = {
type: "image",
id: "id",
url: "https://sample-videos.com/img/Sample-png-image-100kb.png",
alt_text: "alt text",
};

const embedMedia = {
alt: "alt text",
image: {
$type: "blob",
mimeType: "image/png",
ref: "blobRef",
size: "1024",
},
};

describe("blueskySenderService", () => {
beforeEach(() => {
postSpy.mockClear();
uploadBlobSpy.mockClear();
});

it("should send the post", async () => {
await blueskySenderService(client, post, [], log);

expect(postSpy).toHaveBeenCalledTimes(1);
});

describe("when the post has some media", () => {
beforeAll(() => {
mediaDownloaderServiceMock.mockResolvedValue(
makeBlobFromFile("image-png.png", "image/png"),
);
});

it("should send the post with its media ", async () => {
const media: Media = {
type: "image",
id: "id",
url: "https://avatars.githubusercontent.com/u/9489181",
alt_text: "alt text",
};
await blueskySenderService(client, post, [media], log);

expect(uploadBlobSpy).toHaveBeenCalledTimes(1);
expect(postSpy).toHaveBeenCalledTimes(1);
expect(postSpy).toHaveBeenCalledWith({
$type: "app.bsky.feed.post",
createdAt: new Date(post.tweet.timestamp!).toISOString(),
text: "Tweet text",
facets: undefined,
embed: {
$type: "app.bsky.embed.images",
images: [embedMedia],
},
});
});
});

describe("when the tweet as more than 4 images", () => {
it("should send the post with only the first 4 images", async () => {
await blueskySenderService(
client,
post,
[media, media, media, media, media],
log,
);
expect(uploadBlobSpy).toHaveBeenCalledTimes(4);
expect(postSpy).toHaveBeenCalledTimes(1);
expect(postSpy).toHaveBeenCalledWith({
$type: "app.bsky.feed.post",
createdAt: new Date(post.tweet.timestamp!).toISOString(),
text: "Tweet text",
facets: undefined,
embed: {
$type: "app.bsky.embed.images",
images: [embedMedia, embedMedia, embedMedia, embedMedia],
},
});
});
});

describe("when the tweet as a video", () => {
beforeAll(() => {
mediaDownloaderServiceMock.mockResolvedValue(
makeBlobFromFile("video-mp4.mp4", "video/mp4"),
);
});

it("should send the post without media ", async () => {
const mediaVideo: Media = {
type: "video",
id: "id",
preview: "preview",
url: "https://sample-videos.com/video123/mp4/360/big_buck_bunny_360p_1mb.mp4",
};
await blueskySenderService(client, post, [mediaVideo], log);

expect(uploadBlobSpy).toHaveBeenCalledTimes(0);
expect(postSpy).toHaveBeenCalledTimes(1);
expect(postSpy).toHaveBeenCalledWith({
$type: "app.bsky.feed.post",
createdAt: new Date(post.tweet.timestamp!).toISOString(),
text: "Tweet text",
facets: undefined,
embed: undefined,
});
});
});

describe("when no post is given", () => {
it("should skip", async () => {
await blueskySenderService(client, null, [], log);

expect(postSpy).toHaveBeenCalledTimes(0);
});
});
});
44 changes: 44 additions & 0 deletions src/services/__tests__/helpers/make-tweet-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Tweet } from "@the-convocation/twitter-scraper";

export const makeTweetMock = (update: Partial<Tweet> = {}): Tweet => {
const text = update.text || "Hello World";
return {
id: Math.floor(
1000000000000000000 + Math.random() * 9000000000000000000,
).toString(),
conversationId: undefined,
hashtags: [],
html: text,
inReplyToStatus: undefined,
inReplyToStatusId: undefined,
isQuoted: undefined,
isReply: undefined,
isRetweet: undefined,
permanentUrl: undefined,
photos: [],
quotedStatus: undefined,
quotedStatusId: undefined,
text: text,
timestamp: Date.now(),
urls: [],
userId: "userId",
username: "username",
sensitiveContent: undefined,
...update,
// Rest, not used in the service
likes: undefined,
isPin: undefined,
isSelfThread: undefined,
mentions: [],
name: undefined,
place: undefined,
thread: [],
timeParsed: undefined,
replies: 0,
retweets: 0,
retweetedStatus: undefined,
retweetedStatusId: undefined,
videos: [],
views: undefined,
};
};
24 changes: 24 additions & 0 deletions src/services/__tests__/mocks/twitter-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Tweet } from "@the-convocation/twitter-scraper";

import { makeTweetMock } from "../helpers/make-tweet-mock.js";

export class MockTwitterClient {
constructor(tweetCount?: number) {
this.tweetCount = tweetCount || 200;
}

private readonly tweetCount: number;

public async *getTweets(
user: string,
maxTweets?: number,
): AsyncGenerator<Tweet> {
// Mocking the asynchronous generator function
for (let i = 0; i < (this.tweetCount ?? maxTweets); i++) {
yield {
...makeTweetMock({ username: user }),
id: i.toString(),
} as Tweet;
}
}
}
97 changes: 27 additions & 70 deletions src/services/__tests__/tweets-getter.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,37 @@
import { Scraper, Tweet } from "@the-convocation/twitter-scraper";
import { Scraper } from "@the-convocation/twitter-scraper";

import { isTweetCached } from "../../helpers/tweet/index.js";
import { tweetsGetterService } from "../tweets-getter.service.js";
import { MockTwitterClient } from "./mocks/twitter-client.js";

jest.mock("ora");
jest.mock("../../constants.js", () => ({}));
jest.mock("../../helpers/tweet/is-tweet-cached.js");

jest.mock("ora", () => ({
default: jest.fn(() => ({
start: jest
.fn()
.mockImplementation(() => ({ text: "", succeed: jest.fn() })),
})),
__esModule: true,
}));
const isTweetCachedMock = isTweetCached as jest.Mock;

const makeTweetMock = (update: Partial<Tweet>): Tweet => {
const text = update.text || "Hello World";
return {
id: Math.floor(
1000000000000000000 + Math.random() * 9000000000000000000,
).toString(),
conversationId: undefined,
hashtags: [],
html: text,
inReplyToStatus: undefined,
inReplyToStatusId: undefined,
isQuoted: undefined,
isReply: undefined,
isRetweet: undefined,
permanentUrl: undefined,
photos: [],
quotedStatus: undefined,
quotedStatusId: undefined,
text: text,
timestamp: Date.now(),
urls: [],
userId: "userId",
username: "username",
sensitiveContent: undefined,
...update,
// Rest, not used in the service
likes: undefined,
isPin: undefined,
isSelfThread: undefined,
mentions: [],
name: undefined,
place: undefined,
thread: [],
timeParsed: undefined,
replies: 0,
retweets: 0,
retweetedStatus: undefined,
retweetedStatusId: undefined,
videos: [],
views: undefined,
};
};
describe("tweetsGetterService", () => {
describe("when tweets are not cached", () => {
beforeEach(() => {
isTweetCachedMock.mockReturnValue(false);
});

it("should be kept", async () => {
const client = new MockTwitterClient(3);
const tweets = await tweetsGetterService(client as unknown as Scraper);
expect(tweets).toHaveLength(3);
});
});

class MockTwitterClient {
public async *getTweets(
user: string,
maxTweets?: number,
): AsyncGenerator<Tweet> {
// Mocking the asynchronous generator function
for (let i = 0; i < (maxTweets || 200); i++) {
yield {
...makeTweetMock({ username: user }),
id: i.toString(),
} as Tweet;
}
}
}
describe("when tweets are cached", () => {
beforeEach(() => {
isTweetCachedMock.mockReturnValue(true);
});

describe("tweetsGetterService", () => {
it("should return a list of tweets", async () => {
const client = new MockTwitterClient();
const tweets = await tweetsGetterService(client as Scraper);
expect(tweets).toHaveLength(200);
it("should be skipped", async () => {
const client = new MockTwitterClient(3);
const tweets = await tweetsGetterService(client as unknown as Scraper);
expect(tweets).toHaveLength(0);
});
});
});
Loading