From ea7c057c11208bcf12a5023f0dac12c13ce59a1c Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Thu, 27 Feb 2020 13:20:11 +0100 Subject: [PATCH] Add a unit test Splitting up the project in core/main/test --- mastodon-bot.cljs => mastodon_bot/core.cljs | 28 ----------- mastodon_bot/core_test.cljs | 12 +++++ mastodon_bot/main.cljs | 32 ++++++++++++ package.json | 3 +- test.edn | 55 +++++++++++++++++++++ 5 files changed, 101 insertions(+), 29 deletions(-) rename mastodon-bot.cljs => mastodon_bot/core.cljs (85%) mode change 100755 => 100644 create mode 100755 mastodon_bot/core_test.cljs create mode 100755 mastodon_bot/main.cljs create mode 100644 test.edn diff --git a/mastodon-bot.cljs b/mastodon_bot/core.cljs old mode 100755 new mode 100644 similarity index 85% rename from mastodon-bot.cljs rename to mastodon_bot/core.cljs index 2348851..d59c691 --- a/mastodon-bot.cljs +++ b/mastodon_bot/core.cljs @@ -1,4 +1,3 @@ -#!/usr/bin/env lumo (ns mastodon-bot.core (:require [cljs.core :refer [*command-line-args*]] @@ -9,7 +8,6 @@ ["request" :as request] ["fs" :as fs] ["mastodon-api" :as mastodon] - ["rss-parser" :as rss] ["tumblr" :as tumblr] ["twitter" :as twitter])) @@ -195,29 +193,3 @@ (catch js/Error e (exit-with-error (str "failed to connect to Tumblr account " account ": " (.-message e)))))) - -(get-mastodon-timeline - (fn [timeline] - (let [last-post-time (-> timeline first :created_at (js/Date.))] - ;;post from Twitter - (when-let [twitter-config (:twitter config)] - (let [{:keys [access-keys accounts include-replies? include-rts?]} twitter-config - client (twitter-client access-keys)] - (doseq [account accounts] - (.get client - "statuses/user_timeline" - #js {:screen_name account - :tweet_mode "extended" - :include_rts (boolean include-rts?) - :exclude_replies (not (boolean include-replies?))} - (post-tweets last-post-time))))) - ;;post from Tumblr - (when-let [{:keys [access-keys accounts limit tumblr-oauth]} (:tumblr config)] - (doseq [account accounts] - (let [client (tumblr-client access-keys account)] - (.posts client #js {:limit (or limit 5)} (post-tumblrs last-post-time))))) - ;;post from RSS - (when-let [feeds (some-> config :rss)] - (let [parser (rss.)] - (doseq [feed feeds] - (parse-feed last-post-time parser feed))))))) diff --git a/mastodon_bot/core_test.cljs b/mastodon_bot/core_test.cljs new file mode 100755 index 0000000..b3b03bb --- /dev/null +++ b/mastodon_bot/core_test.cljs @@ -0,0 +1,12 @@ +#!/usr/bin/env lumo + +(ns mastodon-bot.core_test + (:require + [cljs.test :refer-macros [deftest is testing run-tests]] + [mastodon-bot.core :as core] + )) + +(deftest test-read-config + (is (= 300 core/max-post-length))) + +(cljs.test/run-tests) diff --git a/mastodon_bot/main.cljs b/mastodon_bot/main.cljs new file mode 100755 index 0000000..17d0634 --- /dev/null +++ b/mastodon_bot/main.cljs @@ -0,0 +1,32 @@ +#!/usr/bin/env lumo + +(ns mastodon-bot.main + (:require + [mastodon-bot.core :as core] + ["rss-parser" :as rss])) + +(core/get-mastodon-timeline + (fn [timeline] + (let [last-post-time (-> timeline first :created_at (js/Date.))] + ;;post from Twitter + (when-let [twitter-config (:twitter core/config)] + (let [{:keys [access-keys accounts include-replies? include-rts?]} twitter-config + client (core/twitter-client access-keys)] + (doseq [account accounts] + (.get client + "statuses/user_timeline" + #js {:screen_name account + :tweet_mode "extended" + :include_rts (boolean include-rts?) + :exclude_replies (not (boolean include-replies?))} + (core/post-tweets last-post-time))))) + ;;post from Tumblr + (when-let [{:keys [access-keys accounts limit tumblr-oauth]} (:tumblr core/config)] + (doseq [account accounts] + (let [client (core/tumblr-client access-keys account)] + (.posts client #js {:limit (or limit 5)} (core/post-tumblrs last-post-time))))) + ;;post from RSS + (when-let [feeds (some-> core/config :rss)] + (let [parser (rss.)] + (doseq [feed feeds] + (core/parse-feed last-post-time parser feed))))))) diff --git a/package.json b/package.json index 00fe49c..50fa46a 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "request": "2.88.0" }, "scripts": { - "start": "./mastodon-bot.cljs" + "start": "./mastodon_bot/main.cljs", + "test": "MASTODON_BOT_CONFIG=test.edn ./mastodon_bot/core_test.cljs" } } diff --git a/test.edn b/test.edn new file mode 100644 index 0000000..6d30eaf --- /dev/null +++ b/test.edn @@ -0,0 +1,55 @@ +{;; add Twitter config to mirror Twitter accounts + :twitter {:access-keys + {:consumer_key "XXXX" + :consumer_secret "XXXX" + :access_token_key "XXXX" + :access_token_secret "XXXX"} + ;; optional, defaults to false + :include-replies? false + ;; optional, defaults to false + :include-rts? false + ;; accounts you wish to mirror + :accounts ["arstechnica" "WIRED"]} + ;; add Tumblr config to mirror Tumblr accounts + :tumblr {:access-keys + {:consumer_key "XXXX" + :consumer_secret "XXXX" + :token "XXXX" + :token_secret "XXXX"} + ;; optional limit for number of posts to retrieve, default: 5 + :limit 10 + :accounts ["cyberpunky.tumblr.com" "scipunk.tumblr.com"]} + ;; add RSS config to follow feeds + :rss {"Hacker News" "https://hnrss.org/newest" + "r/Clojure" "https://www.reddit.com/r/clojure/.rss"} + :mastodon {:access_token "XXXX" + ;; account number you see when you log in and go to your profile + ;; e.g: https://mastodon.social/web/accounts/294795 + :account-id "XXXX" + :api_url "https://botsin.space/api/v1/" + ;; optional boolean to mark content as sensitive + :sensitive? true + ;; optional boolean defaults to false + ;; only sources containing media will be posted when set to true + :media-only? true + ;; optional visibility flag: direct, private, unlisted, public + ;; defaults to public + :visibility "unlisted" + ;; optional limit for the post length + :max-post-length 300 + ;; optional flag specifying wether the name of the account + ;; will be appended in the post, defaults to false + :append-screen-name? false + ;; optional signature for posts + :signature "#newsbot" + ;; optionally try to resolve URLs in posts to skip URL shorteners + ;; defaults to false + :resolve-urls? true + ;; optional content filter regexes + ;; any posts matching the regexes will be filtered out + :content-filters [".*bannedsite.*"] + ;; optional keyword filter regexes + ;; any posts not matching the regexes will be filtered out + :keyword-filters [".*clojure.*"] + ;; Replace Twitter links by Nitter + :nitter-urls? false}}