-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
279deb3
commit 015699a
Showing
24 changed files
with
357 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ dist/ | |
.DS_Store | ||
.env* | ||
!.env.example | ||
coverage/ | ||
|
||
# Temporary files | ||
cache*.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
24 changes: 24 additions & 0 deletions
24
src/helpers/medias/__tests__/helpers/make-blob-from-file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { readFileSync } from "fs"; | ||
import { resolve } from "path"; | ||
|
||
export const makeBlobFromFile = async ( | ||
fileName: string, | ||
mimeType: string, | ||
): Promise<Blob> => { | ||
const file = readFileSync(resolve(__dirname, `../files/${fileName}`)); | ||
return new Blob([file], { type: mimeType }); | ||
}; | ||
|
||
const makeUint8ArrayFromBlob = async (blob: Blob): Promise<Uint8Array> => { | ||
const arrayBuffer = await blob.arrayBuffer(); | ||
const buffer = Buffer.from(arrayBuffer); | ||
return new Uint8Array(buffer); | ||
}; | ||
|
||
export const makeUint8ArrayFromFile = async ( | ||
fileName: string, | ||
mimeType: string, | ||
): Promise<Uint8Array> => { | ||
const blob = await makeBlobFromFile(fileName, mimeType); | ||
return await makeUint8ArrayFromBlob(blob); | ||
}; |
51 changes: 51 additions & 0 deletions
51
src/helpers/medias/__tests__/parse-blob-for-bluesky.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { parseBlobForBluesky } from "../parse-blob-for-bluesky.js"; | ||
import { | ||
makeBlobFromFile, | ||
makeUint8ArrayFromFile, | ||
} from "./helpers/make-blob-from-file.js"; | ||
|
||
let imageBlob: Blob; | ||
let bskyBlobData: Uint8Array; | ||
const initBlobsForMime = async (mimeType: string) => { | ||
const [type, extension] = mimeType.split("/"); | ||
const fileName = `${type}-${extension}.${extension}`; | ||
|
||
bskyBlobData = await makeUint8ArrayFromFile(fileName, mimeType); | ||
imageBlob = await makeBlobFromFile(fileName, mimeType); | ||
}; | ||
|
||
describe("parseBlobForBluesky", () => { | ||
describe("when the mime type is not supported", () => { | ||
it.each` | ||
mimeType | ||
${"video/mp4"} | ||
${"image/webp"} | ||
`("should reject for $mimeType", async ({ mimeType }) => { | ||
await initBlobsForMime(mimeType); | ||
|
||
await expect(parseBlobForBluesky(imageBlob)).rejects.toContain( | ||
"not supported", | ||
); | ||
}); | ||
}); | ||
|
||
describe("when the mime type is supported", () => { | ||
it.each` | ||
mimeType | ||
${"image/jpeg"} | ||
${"image/jpg"} | ||
${"image/png"} | ||
`( | ||
"should resolve bluesky compatible blob for $mimeType", | ||
async ({ mimeType }) => { | ||
await initBlobsForMime(mimeType); | ||
|
||
const result = await parseBlobForBluesky(imageBlob); | ||
expect(result).toStrictEqual({ | ||
blobData: bskyBlobData, | ||
mimeType, | ||
}); | ||
}, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { getPostExcerpt } from "../get-post-excerpt.js"; | ||
|
||
describe("getPostExcerpt", () => { | ||
it("should return the 25 first characters and remove break lines", () => { | ||
const result = getPostExcerpt( | ||
"This is a test from\n the Potato Inc. tech lead", | ||
); | ||
expect(result).toBe("« This is a test from the P... »"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Tweet } from "@the-convocation/twitter-scraper"; | ||
|
||
import { getEligibleTweet } from "../get-eligible-tweet.js"; | ||
import { keepRecentTweets, keepSelfQuotes, keepSelfReplies } from "../index.js"; | ||
|
||
jest.mock("../../../constants.js", () => { | ||
return { | ||
TWITTER_HANDLE: "username", | ||
}; | ||
}); | ||
jest.mock("../index.js", () => { | ||
return { | ||
keepSelfReplies: jest.fn(), | ||
keepSelfQuotes: jest.fn(), | ||
keepRecentTweets: jest.fn(), | ||
}; | ||
}); | ||
|
||
describe("getEligibleTweet", () => { | ||
it.each` | ||
isNotRetweet | isSelfReply | isSelfQuote | isRecentTweet | keep | ||
${false} | ${false} | ${false} | ${false} | ${false} | ||
${true} | ${true} | ${false} | ${false} | ${false} | ||
${true} | ${false} | ${true} | ${false} | ${false} | ||
${true} | ${false} | ${false} | ${true} | ${false} | ||
${true} | ${true} | ${true} | ${true} | ${true} | ||
`( | ||
"should only return keep tweet when all conditions are met", | ||
async ({ isNotRetweet, isSelfReply, isSelfQuote, isRecentTweet, keep }) => { | ||
// Set mocks values | ||
(keepSelfReplies as jest.Mock).mockReturnValue(isSelfReply); | ||
(keepSelfQuotes as jest.Mock).mockReturnValue(isSelfQuote); | ||
(keepRecentTweets as jest.Mock).mockReturnValue(isRecentTweet); | ||
|
||
// Run test | ||
const result = await getEligibleTweet({ | ||
isRetweet: !isNotRetweet, | ||
} as unknown as Tweet); | ||
|
||
// We're only checking for the keep status | ||
expect(result).toStrictEqual( | ||
keep | ||
? { | ||
inReplyToStatus: undefined, | ||
inReplyToStatusId: undefined, | ||
isRetweet: !isNotRetweet, | ||
quotedStatus: undefined, | ||
quotedStatusId: undefined, | ||
} | ||
: undefined, | ||
); | ||
}, | ||
); | ||
}); |
10 changes: 10 additions & 0 deletions
10
src/helpers/tweet/__tests__/get-tweet-id-from-permalink.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { getTweetIdFromPermalink } from "../get-tweet-id-from-permalink.js"; | ||
|
||
describe("getTweetIdFromPermalink", () => { | ||
it("should return the tweet id", () => { | ||
const result = getTweetIdFromPermalink( | ||
"https://twitter.com/username/status/1234567890123456789", | ||
); | ||
expect(result).toBe("1234567890123456789"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Tweet } from "@the-convocation/twitter-scraper"; | ||
|
||
import { isTweetCached } from "../is-tweet-cached.js"; | ||
|
||
describe("isTweetCached", () => { | ||
it("should return true if the tweet is cached", () => { | ||
const result = isTweetCached( | ||
{ id: "1234567890123456789" } as unknown as Tweet, | ||
{ | ||
"1234567890123456789": { | ||
mastodon: "mastodonId", | ||
bluesky: { | ||
cid: "cid", | ||
rkey: "rkey", | ||
}, | ||
}, | ||
}, | ||
); | ||
expect(result).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Tweet } from "@the-convocation/twitter-scraper"; | ||
|
||
import { keepRecentTweets } from "../keep-recent-tweets.js"; | ||
|
||
describe("keepRecentTweets", () => { | ||
describe("when the tweet is recent", () => { | ||
it("should return true", () => { | ||
const result = keepRecentTweets({ | ||
timestamp: Date.now(), | ||
} as unknown as Tweet); | ||
expect(result).toBe(true); | ||
}); | ||
}); | ||
|
||
describe("when the tweet is old", () => { | ||
it("should return false", () => { | ||
const result = keepRecentTweets({ | ||
timestamp: new Date("1997-01-01").getTime(), | ||
} as unknown as Tweet); | ||
expect(result).toBe(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Tweet } from "@the-convocation/twitter-scraper"; | ||
|
||
import { keepSelfQuotes } from "../keep-self-quotes.js"; | ||
|
||
jest.mock("../../../constants.js", () => { | ||
return { | ||
TWITTER_HANDLE: "username", | ||
}; | ||
}); | ||
|
||
describe("keepSelfQuotes", () => { | ||
describe("when the tweet is a quote", () => { | ||
it("should return true when is from the same user", async () => { | ||
const result = await keepSelfQuotes({ | ||
quotedStatus: { username: "username" }, | ||
} as unknown as Tweet); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it("should return false when is from a different user", async () => { | ||
const result = await keepSelfQuotes({ | ||
quotedStatus: { username: "potatoinc" }, | ||
} as unknown as Tweet); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("when the tweet is not a quote", () => { | ||
it("should return true when is from the same user", async () => { | ||
const result = await keepSelfQuotes({} as unknown as Tweet); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Tweet } from "@the-convocation/twitter-scraper"; | ||
|
||
import { keepSelfReplies } from "../keep-self-replies.js"; | ||
|
||
jest.mock("../../../constants.js", () => { | ||
return { | ||
TWITTER_HANDLE: "username", | ||
}; | ||
}); | ||
|
||
describe("keepSelfReplies", () => { | ||
describe("when the tweet is a reply", () => { | ||
it("should return true when is to the same user", async () => { | ||
const result = await keepSelfReplies({ | ||
inReplyToStatus: { username: "username" }, | ||
} as unknown as Tweet); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it("should return false when is to a different user", async () => { | ||
const result = await keepSelfReplies({ | ||
inReplyToStatus: { username: "potatoinc" }, | ||
} as unknown as Tweet); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("when the tweet is not a reply", () => { | ||
it("should return true when is from the same user", async () => { | ||
const result = await keepSelfReplies({} as unknown as Tweet); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { getRedirectedUrl } from "../get-redirection.js"; | ||
|
||
describe("getRedirectedUrl", () => { | ||
describe("when the url is redirected", () => { | ||
it("should return the final url", async () => { | ||
const result = await getRedirectedUrl("https://t.co/bbJgfyzcJR"); | ||
expect(result).toStrictEqual("https://github.com/"); | ||
}); | ||
}); | ||
describe("when the url is not redirected", () => { | ||
it("should return null", async () => { | ||
const result = await getRedirectedUrl("https://t.co/_____null_____"); | ||
expect(result).toStrictEqual(null); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.