Skip to content

Commit

Permalink
Fix feeds with certain passing comparisons always sending articles
Browse files Browse the repository at this point in the history
  • Loading branch information
synzen committed Dec 9, 2024
1 parent 05e70b6 commit e559337
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 62 deletions.
5 changes: 2 additions & 3 deletions services/user-feeds/src/articles/articles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
141 changes: 82 additions & 59 deletions services/user-feeds/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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);
});
});

0 comments on commit e559337

Please sign in to comment.