From e55933715ab93d03dc2b51d65ec8354ebcd22dbc Mon Sep 17 00:00:00 2001 From: gintil Date: Mon, 9 Dec 2024 07:03:23 -0500 Subject: [PATCH] Fix feeds with certain passing comparisons always sending articles --- .../src/articles/articles.service.ts | 5 +- services/user-feeds/test/app.e2e-spec.ts | 141 ++++++++++-------- 2 files changed, 84 insertions(+), 62 deletions(-) diff --git a/services/user-feeds/src/articles/articles.service.ts b/services/user-feeds/src/articles/articles.service.ts index b6fc8b14c..785be37e7 100644 --- a/services/user-feeds/src/articles/articles.service.ts +++ b/services/user-feeds/src/articles/articles.service.ts @@ -5,7 +5,6 @@ import { Injectable } from "@nestjs/common"; import { FeedArticleCustomComparison } from "./entities"; import { Item } from "feedparser"; import { InvalidFeedException } from "./exceptions"; -import { getNestedPrimitiveValue } from "./utils/get-nested-primitive-value"; import { EntityManager, MikroORM, @@ -644,7 +643,7 @@ export class ArticlesService { const article = articles[i]; comparisonFields.forEach((field) => { - const fieldValue = getNestedPrimitiveValue(article.flattened, field); + const fieldValue = article.flattened[field]; if (fieldValue) { const hashedValue = sha1.copy().update(fieldValue).digest("hex"); @@ -766,7 +765,7 @@ export class ArticlesService { const queries: Array<{ name: string; value: string }> = []; for (const key of fieldKeys) { - const value = getNestedPrimitiveValue(article.flattened, key); + const value = article.flattened[key]; if (value) { const hashedValue = sha1.copy().update(value).digest("hex"); diff --git a/services/user-feeds/test/app.e2e-spec.ts b/services/user-feeds/test/app.e2e-spec.ts index 9bcc6dd09..d76597a24 100644 --- a/services/user-feeds/test/app.e2e-spec.ts +++ b/services/user-feeds/test/app.e2e-spec.ts @@ -83,20 +83,20 @@ describe("App (e2e)", () => { await runEvent(testFeedV2Event); }); - // it("sends new articles based on guid", async () => { - // feedFetcherService.fetch = async () => ({ - // requestStatus: FeedResponseRequestStatus.Success, - // body: getTestRssFeed([ - // { - // guid: "new-article", - // }, - // ]), - // bodyHash: randomUUID(), - // }); - - // const results = await runEvent(testFeedV2Event); - // deepStrictEqual(results?.length, 1); - // }); + it("sends new articles based on guid", async () => { + feedFetcherService.fetch = async () => ({ + requestStatus: FeedResponseRequestStatus.Success, + body: getTestRssFeed([ + { + guid: "new-article", + }, + ]), + bodyHash: randomUUID(), + }); + + const results = await runEvent(testFeedV2Event); + deepStrictEqual(results?.length, 1); + }); it("does not send new articles if blocked by comparisons", async () => { const feedEventWithBlockingComparisons = { @@ -127,52 +127,75 @@ describe("App (e2e)", () => { const results = await runEvent(feedEventWithBlockingComparisons); deepStrictEqual(results?.length, 0); + }); + + it("sends new articles based on passing comparisons", async () => { + const feedEventWithPassingComparisons = { + ...testFeedV2Event, + data: { + ...testFeedV2Event.data, + feed: { + ...testFeedV2Event.data.feed, + passingComparisons: ["title"], + }, + }, + }; + + const initialArticles = [ + { + guid: randomUUID(), + title: DEFAULT_TEST_ARTICLES[0].title, + }, + ]; - const results2 = await runEvent(feedEventWithBlockingComparisons); - console.log("🚀 ~ it ~ results2:", results2); + feedFetcherService.fetch = async () => ({ + requestStatus: FeedResponseRequestStatus.Success, + body: getTestRssFeed(initialArticles), + bodyHash: randomUUID(), + }); + + // Initialize the comparisons storage first + await runEvent(feedEventWithPassingComparisons); + + feedFetcherService.fetch = async () => ({ + requestStatus: FeedResponseRequestStatus.Success, + body: getTestRssFeed([ + { + guid: initialArticles[0].guid, + title: initialArticles[0].title + "-different", + }, + ]), + bodyHash: randomUUID(), + }); + + const results = await runEvent(feedEventWithPassingComparisons); + + deepStrictEqual(results?.length, 1); }); - // it("sends new articles based on passing comparisons", async () => { - // const feedEventWithPassingComparisons = { - // ...testFeedV2Event, - // data: { - // ...testFeedV2Event.data, - // feed: { - // ...testFeedV2Event.data.feed, - // passingComparisons: ["title"], - // }, - // }, - // }; - - // const initialArticles = [ - // { - // guid: randomUUID(), - // title: DEFAULT_TEST_ARTICLES[0].title, - // }, - // ]; - - // feedFetcherService.fetch = async () => ({ - // requestStatus: FeedResponseRequestStatus.Success, - // body: getTestRssFeed(initialArticles), - // bodyHash: randomUUID(), - // }); - - // // Initialize the comparisons storage first - // await runEvent(feedEventWithPassingComparisons); - - // feedFetcherService.fetch = async () => ({ - // requestStatus: FeedResponseRequestStatus.Success, - // body: getTestRssFeed([ - // { - // guid: initialArticles[0].guid, - // title: initialArticles[0].title + "-different", - // }, - // ]), - // bodyHash: randomUUID(), - // }); - - // const results = await runEvent(feedEventWithPassingComparisons); - - // deepStrictEqual(results?.length, 1); - // }); + it("does not send new articles based on passing comparisons if there are no new articles", async () => { + const feedEventWithPassingComparisons = { + ...testFeedV2Event, + data: { + ...testFeedV2Event.data, + feed: { + ...testFeedV2Event.data.feed, + passingComparisons: ["rss:title__#"], + }, + }, + }; + + // Initialize the comparisons storage first + await runEvent(feedEventWithPassingComparisons); + + feedFetcherService.fetch = async () => ({ + requestStatus: FeedResponseRequestStatus.Success, + body: getTestRssFeed(), + bodyHash: randomUUID(), + }); + + const results = await runEvent(feedEventWithPassingComparisons); + + deepStrictEqual(results?.length, 0); + }); });