def test_get_new_results__goes_to_another_page(self, bot):
        api = MockExportAPI()
        api.with_browse_results([
            MockSubmission("1227"),
            MockSubmission("1225"),
            MockSubmission("1224")
        ],
                                page=1)
        api.with_browse_results([
            MockSubmission("1223"),
            MockSubmission("1222"),
            MockSubmission("1220")
        ],
                                page=2)
        watcher = SubscriptionWatcher(api, bot)
        watcher.latest_ids = collections.deque(maxlen=4)
        watcher.latest_ids.append("1220")
        watcher.running = True

        results = watcher._get_new_results()

        assert len(results) == 5
        assert results[0].submission_id == "1222"
        assert results[1].submission_id == "1223"
        assert results[2].submission_id == "1224"
        assert results[3].submission_id == "1225"
        assert results[4].submission_id == "1227"
    def test_run__passes_correct_blocklists_to_subscriptions(self, bot):
        submission = MockSubmission("12322")
        api = MockExportAPI().with_submission(submission)
        watcher = SubscriptionWatcher(api, bot)
        method_called = MockMethod([submission])
        watcher._get_new_results = method_called.call
        watcher.BACK_OFF = 1
        watcher.blocklists = {156: {"test", "ych"}, -200: {"example"}}
        sub1 = MockSubscription("deer", 156)
        sub2 = MockSubscription("dog", -232)
        watcher.subscriptions = [sub1, sub2]

        thread = Thread(target=lambda: self.watcher_killer(watcher))
        thread.start()
        # Run watcher
        watcher.run()
        thread.join()

        assert submission in sub1.submissions_checked
        assert len(sub1.blocklists) == 1
        assert len(sub1.blocklists[0]) == 2
        assert "test" in sub1.blocklists[0]
        assert "ych" in sub1.blocklists[0]
        assert submission in sub2.submissions_checked
        assert len(sub2.blocklists) == 1
        assert len(sub2.blocklists[0]) == 0
        assert method_called.called
    def test_run__calls_get_new_results(self, bot):
        api = MockExportAPI()
        watcher = SubscriptionWatcher(api, bot)
        method_called = MockMethod([])
        watcher._get_new_results = method_called.call
        # Shorten the wait
        watcher.BACK_OFF = 1

        thread = Thread(target=lambda: self.watcher_killer(watcher))
        thread.start()
        # Run watcher
        watcher.run()
        thread.join()

        assert method_called.called
    def test_get_new_results__returns_new_results(self, bot):
        api = MockExportAPI()
        api.with_browse_results([
            MockSubmission("1223"),
            MockSubmission("1222"),
            MockSubmission("1220")
        ])
        watcher = SubscriptionWatcher(api, bot)
        watcher.latest_ids.append("1220")
        watcher.running = True

        results = watcher._get_new_results()

        assert len(results) == 2
        assert results[0].submission_id == "1222"
        assert results[1].submission_id == "1223"
    def test_get_new_results__handles_empty_latest_ids(self, bot):
        api = MockExportAPI()
        api.with_browse_results([
            MockSubmission("1223"),
            MockSubmission("1222"),
            MockSubmission("1220")
        ])
        watcher = SubscriptionWatcher(api, bot)
        watcher.running = True

        results = watcher._get_new_results()

        assert len(results) == 0
        assert len(watcher.latest_ids) == 3
        assert watcher.latest_ids[0] == "1220"
        assert watcher.latest_ids[1] == "1222"
        assert watcher.latest_ids[2] == "1223"
    def test_get_new_results__respects_page_cap(self, bot):
        api = MockExportAPI()
        api.with_browse_results([MockSubmission("1300")], page=1)
        api.with_browse_results([MockSubmission("1298")], page=2)
        api.with_browse_results([MockSubmission("1297")], page=3)
        api.with_browse_results([MockSubmission("1295")], page=4)
        api.with_browse_results([MockSubmission("1280")], page=5)
        api.with_browse_results([MockSubmission("1272")], page=6)
        api.with_browse_results([MockSubmission("1250")], page=7)
        watcher = SubscriptionWatcher(api, bot)
        watcher.PAGE_CAP = 5
        watcher.latest_ids.append("1250")
        watcher.running = True

        results = watcher._get_new_results()

        assert len(results) == 5
        assert "1272" not in [x.submission_id for x in results]
    def test_run__calls_update_latest_ids(self, bot):
        submission1 = MockSubmission("12322")
        submission2 = MockSubmission("12324")
        api = MockExportAPI().with_submissions([submission1, submission2])
        watcher = SubscriptionWatcher(api, bot)
        mock_new_results = MockMethod([submission1, submission2])
        watcher._get_new_results = mock_new_results.call
        mock_update_latest = MockMethod()
        watcher._update_latest_ids = mock_update_latest.call
        # Shorten the wait
        watcher.BACK_OFF = 1

        thread = Thread(target=lambda: self.watcher_killer(watcher))
        thread.start()
        # Run watcher
        watcher.run()
        thread.join()

        assert mock_update_latest.called
        assert mock_update_latest.args[0] == [submission2]
    def test_run__checks_all_new_results(self, bot):
        submission1 = MockSubmission("12322")
        submission2 = MockSubmission("12324")
        api = MockExportAPI().with_submissions([submission1, submission2])
        watcher = SubscriptionWatcher(api, bot)
        method_called = MockMethod([submission1, submission2])
        watcher._get_new_results = method_called.call
        watcher.BACK_OFF = 1
        sub = MockSubscription("deer", 0)
        watcher.subscriptions = [sub]

        thread = Thread(target=lambda: self.watcher_killer(watcher))
        thread.start()
        # Run watcher
        watcher.run()
        thread.join()

        assert submission1 in sub.submissions_checked
        assert submission2 in sub.submissions_checked
        assert method_called.called