From 037577696f3ef6c0274b63518c305d6edc392a49 Mon Sep 17 00:00:00 2001 From: Louis Grasset Date: Sun, 19 Nov 2023 17:51:23 +0100 Subject: [PATCH] test(bluesky): support link metadata --- .../__tests__/fetch-link-metadata.spec.ts | 27 ++++++++ .../get-bluesky-link-metadata.spec.ts | 69 +++++++++++++++++++ .../bluesky/__tests__/mocks/metadata.ts | 11 +++ 3 files changed, 107 insertions(+) create mode 100644 src/helpers/bluesky/__tests__/fetch-link-metadata.spec.ts create mode 100644 src/helpers/bluesky/__tests__/get-bluesky-link-metadata.spec.ts create mode 100644 src/helpers/bluesky/__tests__/mocks/metadata.ts diff --git a/src/helpers/bluesky/__tests__/fetch-link-metadata.spec.ts b/src/helpers/bluesky/__tests__/fetch-link-metadata.spec.ts new file mode 100644 index 0000000..186fe2a --- /dev/null +++ b/src/helpers/bluesky/__tests__/fetch-link-metadata.spec.ts @@ -0,0 +1,27 @@ +import { fetchLinkMetadata } from "../fetch-link-metadata.js"; +import { METADATA_MOCK } from "./mocks/metadata.js"; + +jest.mock("../../../constants.js", () => { + return { + TWITTER_HANDLE: "username", + MASTODON_INSTANCE: "mastodon.social", + MASTODON_MAX_POST_LENGTH: 500, + BLUESKY_MAX_POST_LENGTH: 300, + }; +}); + +describe("fetchLinkMetadata", () => { + it("should return the metadata if data is found", async () => { + const result = await fetchLinkMetadata( + "https://github.com/louisgrasset/touitomamout", + ); + expect(JSON.stringify(result)).toStrictEqual(JSON.stringify(METADATA_MOCK)); + }); + + it("should return null if no data is found", async () => { + const result = await fetchLinkMetadata( + "https://thisturldoesnotexist.example.com", + ); + expect(result).toBeNull(); + }); +}); diff --git a/src/helpers/bluesky/__tests__/get-bluesky-link-metadata.spec.ts b/src/helpers/bluesky/__tests__/get-bluesky-link-metadata.spec.ts new file mode 100644 index 0000000..fac6062 --- /dev/null +++ b/src/helpers/bluesky/__tests__/get-bluesky-link-metadata.spec.ts @@ -0,0 +1,69 @@ +import { exec } from "node:child_process"; + +import { BskyAgent } from "@atproto/api"; + +import { fetchLinkMetadata } from "../fetch-link-metadata.js"; +import { getBlueskyLinkMetadata } from "../get-bluesky-link-metadata.js"; +import { METADATA_MOCK } from "./mocks/metadata.js"; + +jest.mock("../../../services/index.js", () => ({ + mediaDownloaderService: jest.fn(() => ({ + blobData: "blobData", + mimeType: "mimeType", + })), +})); + +jest.mock("../../medias/parse-blob-for-bluesky.js", () => ({ + parseBlobForBluesky: jest.fn(() => ({ + blobData: "blobData", + mimeType: "mimeType", + })), +})); + +jest.mock("../../../constants.js", () => { + return { + TWITTER_HANDLE: "username", + MASTODON_INSTANCE: "mastodon.social", + MASTODON_MAX_POST_LENGTH: 500, + BLUESKY_MAX_POST_LENGTH: 300, + }; +}); + +const uploadBlobMock = jest.fn(() => ({ + success: true, + data: { + blob: { + original: "123456789", + }, + }, +})); + +describe("getBlueskyLinkMetadata", () => { + it("should return the metadata if data is found", async () => { + const result = await getBlueskyLinkMetadata( + "https://github.com/louisgrasset/touitomamout", + { uploadBlob: uploadBlobMock } as unknown as BskyAgent, + ); + expect(result).toStrictEqual({ + ...METADATA_MOCK, + image: { + success: true, + data: { + blob: { + original: "123456789", + }, + }, + }, + }); + }); + + it("should return the metadata if data is found", async () => { + const result = await getBlueskyLinkMetadata( + "https://thisturldoesnotexist.example.com", + { + uploadBlob: uploadBlobMock, + } as unknown as BskyAgent, + ); + expect(result).toBeNull(); + }); +}); diff --git a/src/helpers/bluesky/__tests__/mocks/metadata.ts b/src/helpers/bluesky/__tests__/mocks/metadata.ts new file mode 100644 index 0000000..6c34a69 --- /dev/null +++ b/src/helpers/bluesky/__tests__/mocks/metadata.ts @@ -0,0 +1,11 @@ +export const METADATA_MOCK = { + error: "", + likely_type: "html", + url: "https://github.com/louisgrasset/touitomamout", + title: + "GitHub - louisgrasset/touitomamout: Touitomamout is an easy way to synchronize your Twitter's tweets...", + description: + "Touitomamout is an easy way to synchronize your Twitter\u0026#39;s tweets 🦤 to Mastodon 🦣 and Bluesky post ☁️ (also known as Twitter to Mastodon \u0026amp; Bluesky crossposter) - GitHub - louisgrasset...", + image: + "https://cardyb.bsky.app/v1/image?url=https%3A%2F%2Fopengraph.githubassets.com%2Fc62e7ca13ff991d5b45a3218958a7bfefc990ee3386bd75fee3c6159e91c8859%2Flouisgrasset%2Ftouitomamout", +};