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 #138

Merged
merged 3 commits into from
Nov 20, 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
38 changes: 19 additions & 19 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,44 @@ const trimTwitterHandle = (handle: string) => {
dotenv.config({ path: envPath });

export const TWITTER_HANDLE = trimTwitterHandle(
process.env.TWITTER_HANDLE || "",
process.env.TWITTER_HANDLE ?? "",
);
export const TWITTER_USERNAME = trimTwitterHandle(
process.env.TWITTER_USERNAME || "",
process.env.TWITTER_USERNAME ?? "",
);
export const TWITTER_PASSWORD = (process.env.TWITTER_PASSWORD || "").trim();
export const MASTODON_INSTANCE = (process.env.MASTODON_INSTANCE || "").trim();
export const TWITTER_PASSWORD = (process.env.TWITTER_PASSWORD ?? "").trim();
export const MASTODON_INSTANCE = (process.env.MASTODON_INSTANCE ?? "").trim();
export const MASTODON_ACCESS_TOKEN = (
process.env.MASTODON_ACCESS_TOKEN || ""
process.env.MASTODON_ACCESS_TOKEN ?? ""
).trim();
export const BLUESKY_INSTANCE = (process.env.BLUESKY_INSTANCE || "").trim();
export const BLUESKY_IDENTIFIER = (process.env.BLUESKY_IDENTIFIER || "").trim();
export const BLUESKY_PASSWORD = (process.env.BLUESKY_PASSWORD || "").trim();
export const BLUESKY_INSTANCE = (process.env.BLUESKY_INSTANCE ?? "").trim();
export const BLUESKY_IDENTIFIER = (process.env.BLUESKY_IDENTIFIER ?? "").trim();
export const BLUESKY_PASSWORD = (process.env.BLUESKY_PASSWORD ?? "").trim();
export const INSTANCE_ID = (TWITTER_HANDLE ?? "instance")
.toLowerCase()
.trim()
.replaceAll(" ", "_");
export const STORAGE_DIR = process.env.STORAGE_DIR ?? process.cwd();
export const CACHE_PATH = `${STORAGE_DIR}/cache.${INSTANCE_ID}.json`;
export const COOKIES_PATH = `${STORAGE_DIR}/cookies.${INSTANCE_ID}.json`;
export const SYNC_MASTODON = (process.env.SYNC_MASTODON || "false") === "true";
export const SYNC_BLUESKY = (process.env.SYNC_BLUESKY || "false") === "true";
export const SYNC_MASTODON = (process.env.SYNC_MASTODON ?? "false") === "true";
export const SYNC_BLUESKY = (process.env.SYNC_BLUESKY ?? "false") === "true";
export const SYNC_FREQUENCY_MIN = parseInt(
process.env.SYNC_FREQUENCY_MIN || "30",
process.env.SYNC_FREQUENCY_MIN ?? "30",
);
export const SYNC_PROFILE_DESCRIPTION =
(process.env.SYNC_PROFILE_DESCRIPTION || "false") === "true";
(process.env.SYNC_PROFILE_DESCRIPTION ?? "false") === "true";
export const SYNC_PROFILE_PICTURE =
(process.env.SYNC_PROFILE_PICTURE || "false") === "true";
(process.env.SYNC_PROFILE_PICTURE ?? "false") === "true";
export const SYNC_PROFILE_NAME =
(process.env.SYNC_PROFILE_NAME || "false") === "true";
(process.env.SYNC_PROFILE_NAME ?? "false") === "true";
export const SYNC_PROFILE_HEADER =
(process.env.SYNC_PROFILE_HEADER || "false") === "true";
export const SYNC_DRY_RUN = (process.env.SYNC_DRY_RUN || "false") === "true";
export const DEBUG = (process.env.TOUITOMAMOUT_DEBUG || "false") === "true";
export const DAEMON = (process.env.DAEMON || "false") === "true";
(process.env.SYNC_PROFILE_HEADER ?? "false") === "true";
export const SYNC_DRY_RUN = (process.env.SYNC_DRY_RUN ?? "false") === "true";
export const DEBUG = (process.env.TOUITOMAMOUT_DEBUG ?? "false") === "true";
export const DAEMON = (process.env.DAEMON ?? "false") === "true";
export const VOID = "∅";
export const API_RATE_LIMIT = parseInt(process.env.API_RATE_LIMIT ?? "30");
export const TOUITOMAMOUT_VERSION = buildInfo.version || "0.0.0";
export const TOUITOMAMOUT_VERSION = buildInfo.version ?? "0.0.0";
export const MASTODON_MAX_POST_LENGTH = 500;
export const BLUESKY_MAX_POST_LENGTH = 300;
4 changes: 1 addition & 3 deletions src/helpers/bluesky/__tests__/fetch-link-metadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ jest.mock("../../../constants.js", () => {

describe("fetchLinkMetadata", () => {
it("should return the metadata if data is found", async () => {
const result = await fetchLinkMetadata(
"https://github.com/louisgrasset/touitomamout",
);
const result = await fetchLinkMetadata("https://bsky.app");
expect(JSON.stringify(result)).toStrictEqual(JSON.stringify(METADATA_MOCK));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ const uploadBlobMock = jest.fn(() => ({

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,
);
const result = await getBlueskyLinkMetadata("https://bsky.app", {
uploadBlob: uploadBlobMock,
} as unknown as BskyAgent);
expect(result).toStrictEqual({
...METADATA_MOCK,
image: {
Expand Down
10 changes: 4 additions & 6 deletions src/helpers/bluesky/__tests__/mocks/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
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...",
url: "https://bsky.app",
title: "Bluesky Social",
description: "See what's next.",
image:
"https://cardyb.bsky.app/v1/image?url=https%3A%2F%2Fopengraph.githubassets.com%2Fc62e7ca13ff991d5b45a3218958a7bfefc990ee3386bd75fee3c6159e91c8859%2Flouisgrasset%2Ftouitomamout",
"https://cardyb.bsky.app/v1/image?url=https%3A%2F%2Fbsky.app%2Fstatic%2Fsocial-card-default.png",
};
21 changes: 8 additions & 13 deletions src/helpers/cache/save-post-to-cache.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
import {
BlueskyCache,
MastodonCache,
Platform,
PostsCache,
} from "../../types/index.js";
import { updateCacheFile } from "./update-cache.js";
import { BlueskyCache, MastodonCache, Platform } from "../../types/index.js";
import { getCachedPosts } from "./get-cached-posts.js";
import { updateCacheEntry } from "./update-cache-entry.js";

interface PostToCache {
cache: PostsCache;
data: MastodonCache | BlueskyCache;
tweetId?: string;
platform: Platform;
}

export const savePostToCache = async ({
cache,
tweetId = "",
data,
platform,
}: PostToCache) => {
const alreadyExistingCachedPostData = cache[tweetId] || {};
await updateCacheFile({
...cache,
const cachedPosts = await getCachedPosts();
const currentPostData = cachedPosts[tweetId] || {};
await updateCacheEntry("posts", {
...cachedPosts,
[tweetId]: {
...alreadyExistingCachedPostData,
...currentPostData,
[platform]: data,
},
});
Expand Down
17 changes: 0 additions & 17 deletions src/helpers/cache/update-cache.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/services/bluesky-sender.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Ora } from "ora";

import { DEBUG, VOID } from "../constants.js";
import { getBlueskyChunkLinkMetadata } from "../helpers/bluesky/index.js";
import { getCachedPosts } from "../helpers/cache/get-cached-posts.js";
import { savePostToCache } from "../helpers/cache/save-post-to-cache.js";
import { oraProgress } from "../helpers/logs/index.js";
import { parseBlobForBluesky } from "../helpers/medias/parse-blob-for-bluesky.js";
Expand Down Expand Up @@ -258,9 +257,7 @@ export const blueskySenderService = async (
}`,
);

const cache = await getCachedPosts();
await savePostToCache({
cache,
tweetId: post.tweet.id,
data: chunkReferences.map((ref) => ({
rkey: ref.rkey,
Expand Down
3 changes: 0 additions & 3 deletions src/services/mastodon-sender.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { mastodon } from "masto";
import { Ora } from "ora";

import { VOID } from "../constants.js";
import { getCachedPosts } from "../helpers/cache/get-cached-posts.js";
import { savePostToCache } from "../helpers/cache/save-post-to-cache.js";
import { oraProgress } from "../helpers/logs/index.js";
import { getPostExcerpt } from "../helpers/post/get-post-excerpt.js";
Expand Down Expand Up @@ -122,9 +121,7 @@ export const mastodonSenderService = async (
}`,
);

const cache = await getCachedPosts();
await savePostToCache({
cache,
tweetId: post.tweet.id,
data: chunkReferences,
platform: Platform.MASTODON,
Expand Down