From ef1584e4a9291f3bea68363477534e8bcaced88b Mon Sep 17 00:00:00 2001 From: asemabdallah Date: Wed, 7 Oct 2026 00:04:30 +0200 Subject: [PATCH] feature: show what changed when a watched page changes --- apps/server/src/engine/page-diff.ts | 85 ++++++++++++++++++ apps/server/src/engine/service.ts | 45 +++++++++- tests/agent-api.test.ts | 133 ++++++++++++++++++++++++++++ tests/page-diff.test.ts | 106 ++++++++++++++++++++++ 4 files changed, 365 insertions(+), 4 deletions(-) create mode 100644 apps/server/src/engine/page-diff.ts create mode 100644 tests/page-diff.test.ts diff --git a/apps/server/src/engine/page-diff.ts b/apps/server/src/engine/page-diff.ts new file mode 100644 index 00000000..b5164732 --- /dev/null +++ b/apps/server/src/engine/page-diff.ts @@ -0,0 +1,85 @@ +/** Lines of page text for comparing two checks of a watched page. */ +export function pageLines(text: string, limit = 2000): string[] { + const lines = new Set(); + for (const raw of text.split(/\r?\n/)) { + const line = raw.replace(/\s+/g, " ").trim().slice(0, 300); + if (line) lines.add(line); + if (lines.size >= limit) break; + } + return [...lines]; +} + +export type PageDiff = { added: string[]; updated: string[]; removed: string[] }; + +// Relative times ("posted 1 day ago" → "posted 2 days ago", "58 minutes ago" → "1 hour ago") +// tick on their own, so a change in them alone is not news. +const relativeTime = + /\b(?:\d+|an?|one)\s+(?:sec(?:ond)?|min(?:ute)?|hour|hr|day|week|month|year)s?\s+ago\b|\bvor\s+(?:\d+|einer?|einem)\s+(?:sekunde|minute|stunde|tag|woche|monat|jahr)(?:e|en|n)?\b/giu; + +/** Text with relative times blanked out: equal results mean only those times changed. */ +export function withoutRelativeTimes(text: string) { + return text.replace(relativeTime, "