news: singleton stories keep unique identities when URLs normalise alike
- Commit
f9ffb82fe25434f58d9ac3768daf41afd664ebd5- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 07:25 UTC (5 days ago)
- Parents
72d426d
StoryGrouping derived a singleton story's public id via FeedMerger.normaliseUrl, which is non-injective (strips case, fragments, utm_* params, trailing slashes) — while cached articles are keyed by the raw URL. Two distinct rows (https://EXAMPLE.com/a/?utm_source=rss#top and https://example.com/a, both storyId-less) grouped correctly into two stories but received the same id 'https://example.com/a'. Tapping the second card opened the first story, search deduped one away, and the feed's list keys collided so the two cards were the same item. A singleton's id is now its representative URL verbatim — injective over cached rows; server srv:<id> stories are untouched. Two regression tests failed pre-fix (duplicate ids; search surfacing one story instead of two). Feed refresh, read state, offline cache and the crawl fallback were audited and cleared. Verified: :news:testDevUnitTest 75 tests and :news:lint BUILD SUCCESSFUL.
Diff
news/src/main/java/com/vayunmathur/news/domain/Story.kt
diff --git a/news/src/main/java/com/vayunmathur/news/domain/Story.kt b/news/src/main/java/com/vayunmathur/news/domain/Story.ktindex baec15e41e..5589cce6f3 100644--- a/news/src/main/java/com/vayunmathur/news/domain/Story.kt+++ b/news/src/main/java/com/vayunmathur/news/domain/Story.kt@@ -12,8 +12,10 @@ package com.vayunmathur.news.domain * however similar they look. * * [id] is `srv:<serverStoryId>` for a server-grouped story, else the- * normalised URL of its sole article. It is carried in- * `Route.Story` — never in ViewModel selection state.+ * sole article's URL verbatim. (Verbatim, never normalised: cached rows+ * are keyed by the raw URL, and two distinct rows can normalise to the+ * same string, so a normalised id would give two stories one identity.)+ * It is carried in `Route.Story` — never in ViewModel selection state. * * [articles] are all members sorted earliest → latest (unknown * timestamps last, then URL), so a story page reads as a timeline of@@ -115,7 +117,7 @@ object StoryGrouping { .thenBy { it.url }, ) ?: members.first() Story(- id = if (key.startsWith("srv:")) key else FeedMerger.normaliseUrl(representative.url),+ id = if (key.startsWith("srv:")) key else representative.url, representative = representative, articles = members, )
news/src/test/java/com/vayunmathur/news/domain/StoryGroupingTest.kt
diff --git a/news/src/test/java/com/vayunmathur/news/domain/StoryGroupingTest.kt b/news/src/test/java/com/vayunmathur/news/domain/StoryGroupingTest.ktindex 816494b64a..6513284c2e 100644--- a/news/src/test/java/com/vayunmathur/news/domain/StoryGroupingTest.kt+++ b/news/src/test/java/com/vayunmathur/news/domain/StoryGroupingTest.kt@@ -63,6 +63,44 @@ class StoryGroupingTest { assertTrue(stories.all { it.id == FeedMerger.normaliseUrl(it.representative.url) }) } + @Test fun singletonIdsStayUniqueWhenUrlsNormaliseToTheSameString() {+ // Two distinct cached rows (Room's key is the raw URL) whose URLs+ // differ only in case, a tracking parameter, a fragment and a+ // trailing slash: FeedMerger.normaliseUrl maps both to the same+ // string, so a normalised singleton id is not an identity.+ val stories = StoryGrouping.group(+ listOf(+ a("bbc", "https://EXAMPLE.com/a/?utm_source=rss#top", "Storm hits coast", h(2.0)),+ a("dw", "https://example.com/a", "Storm hits coast", h(1.0)),+ ),+ )+ assertEquals(2, stories.size)+ assertEquals(+ 2, stories.map { it.id }.distinct().size,+ "singleton story ids must be unique per cached article, got ${stories.map { it.id }}",+ )+ // Each id must resolve to its own story (Route.Story / lookup by id).+ stories.forEach { story ->+ assertEquals(+ listOf(story.representative.url),+ stories.first { it.id == story.id }.articles.map { it.url },+ )+ }+ }++ @Test fun searchHitsSurfaceBothNormalisedUrlVariants() {+ val stories = StoryGrouping.group(+ listOf(+ a("bbc", "https://EXAMPLE.com/a/?utm_source=rss#top", "Storm hits coast", h(2.0)),+ a("dw", "https://example.com/a", "Storm hits coast", h(1.0)),+ ),+ )+ val surfaced = stories.orderedBySearchHits(+ listOf("https://example.com/a", "https://EXAMPLE.com/a/?utm_source=rss#top"),+ )+ assertEquals(2, surfaced.size, "each hit must surface its own singleton story")+ }+ @Test fun blankStoryIdIsSingleton() { val stories = StoryGrouping.group( listOf(